Home | History | Annotate | Line # | Download | only in atactl
atactl.c revision 1.19
      1 /*	$NetBSD: atactl.c,v 1.19 2002/08/15 19:17:04 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/ata/atavar.h>
     56 #include <dev/ic/wdcreg.h>
     57 #include <sys/ataio.h>
     58 
     59 struct command {
     60 	const char *cmd_name;
     61 	const char *arg_names;
     62 	void (*cmd_func)(int, char *[]);
     63 };
     64 
     65 struct bitinfo {
     66 	u_int bitmask;
     67 	const char *string;
     68 };
     69 
     70 int	main(int, char *[]);
     71 void	usage(void);
     72 void	ata_command(struct atareq *);
     73 void	print_bitinfo(const char *, const char *, u_int, struct bitinfo *);
     74 void	print_smart_status(void *vbuf, void *tbuf);
     75 int	is_smart(int silent);
     76 
     77 int	fd;				/* file descriptor for device */
     78 const	char *dvname;			/* device name */
     79 char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     80 const	char *cmdname;			/* command user issued */
     81 const	char *argnames;			/* helpstring: expected arguments */
     82 
     83 void	device_identify(int, char *[]);
     84 void	device_setidle(int, char *[]);
     85 void	device_idle(int, char *[]);
     86 void	device_checkpower(int, char *[]);
     87 void	device_smart(int, char *[]);
     88 
     89 struct command commands[] = {
     90 	{ "identify",	"",			device_identify },
     91 	{ "setidle",	"idle-timer",		device_setidle },
     92 	{ "setstandby",	"standby-timer",	device_setidle },
     93 	{ "idle",	"",			device_idle },
     94 	{ "standby",	"",			device_idle },
     95 	{ "sleep",	"",			device_idle },
     96 	{ "checkpower",	"",			device_checkpower },
     97 	{ "smart",	"enable|disable|status", device_smart },
     98 	{ NULL,		NULL,			NULL },
     99 };
    100 
    101 /*
    102  * Tables containing bitmasks used for error reporting and
    103  * device identification.
    104  */
    105 
    106 struct bitinfo ata_caps[] = {
    107 	{ ATA_CAP_STBY, "ATA standby timer values" },
    108 	{ WDC_CAP_IORDY, "IORDY operation" },
    109 	{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
    110 	{ NULL, NULL },
    111 };
    112 
    113 struct bitinfo ata_vers[] = {
    114 	{ WDC_VER_ATA1,	"ATA-1" },
    115 	{ WDC_VER_ATA2,	"ATA-2" },
    116 	{ WDC_VER_ATA3,	"ATA-3" },
    117 	{ WDC_VER_ATA4,	"ATA-4" },
    118 	{ NULL, NULL },
    119 };
    120 
    121 struct bitinfo ata_cmd_set1[] = {
    122 	{ WDC_CMD1_NOP, "NOP command" },
    123 	{ WDC_CMD1_RB, "READ BUFFER command" },
    124 	{ WDC_CMD1_WB, "WRITE BUFFER command" },
    125 	{ WDC_CMD1_HPA, "Host Protected Area feature set" },
    126 	{ WDC_CMD1_DVRST, "DEVICE RESET command" },
    127 	{ WDC_CMD1_SRV, "SERVICE interrupt" },
    128 	{ WDC_CMD1_RLSE, "release interrupt" },
    129 	{ WDC_CMD1_AHEAD, "look-ahead" },
    130 	{ WDC_CMD1_CACHE, "write cache" },
    131 	{ WDC_CMD1_PKT, "PACKET command feature set" },
    132 	{ WDC_CMD1_PM, "Power Management feature set" },
    133 	{ WDC_CMD1_REMOV, "Removable Media feature set" },
    134 	{ WDC_CMD1_SEC, "Security Mode feature set" },
    135 	{ WDC_CMD1_SMART, "SMART feature set" },
    136 	{ NULL, NULL },
    137 };
    138 
    139 struct bitinfo ata_cmd_set2[] = {
    140 	{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
    141 	{ ATA_CMD2_APM, "Advanced Power Management feature set" },
    142 	{ ATA_CMD2_CFA, "CFA feature set" },
    143 	{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
    144 	{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
    145 	{ NULL, NULL },
    146 };
    147 
    148 static const struct {
    149 	const int	id;
    150 	const char	*name;
    151 } smart_attrs[] = {
    152 	{ 1,		"Raw read error rate" },
    153 	{ 2,		"Throughput performance" },
    154 	{ 3,		"Spin-up time" },
    155 	{ 4,		"Start/stop count" },
    156 	{ 5,		"Reallocated sector count" },
    157 	{ 7,		"Seek error rate" },
    158 	{ 8,		"Seek time performance" },
    159 	{ 9,		"Power-on hours count" },
    160 	{ 10,		"Spin retry count" },
    161 	{ 11,		"Calibration retry count" },
    162 	{ 12,		"Device power cycle count" },
    163 	{ 191,		"Gsense error rate" },
    164 	{ 192,		"Power-off retract count" },
    165 	{ 193,		"Load cycle count" },
    166 	{ 194,		"Temperature" },
    167 	{ 196,		"Reallocated event count" },
    168 	{ 197,		"Current pending sector" },
    169 	{ 198,		"Offline uncorrectable" },
    170 	{ 199,		"Ultra DMA CRC error count" },
    171 	{ 0,		"" },
    172 };
    173 
    174 int
    175 main(int argc, char *argv[])
    176 {
    177 	int i;
    178 
    179 	/* Must have at least: device command */
    180 	if (argc < 3)
    181 		usage();
    182 
    183 	/* Skip program name, get and skip device name and command. */
    184 	dvname = argv[1];
    185 	cmdname = argv[2];
    186 	argv += 3;
    187 	argc -= 3;
    188 
    189 	/*
    190 	 * Open the device
    191 	 */
    192 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    193 	if (fd == -1) {
    194 		if (errno == ENOENT) {
    195 			/*
    196 			 * Device doesn't exist.  Probably trying to open
    197 			 * a device which doesn't use disk semantics for
    198 			 * device name.  Try again, specifying "cooked",
    199 			 * which leaves off the "r" in front of the device's
    200 			 * name.
    201 			 */
    202 			fd = opendisk(dvname, O_RDWR, dvname_store,
    203 			    sizeof(dvname_store), 1);
    204 			if (fd == -1)
    205 				err(1, "%s", dvname);
    206 		} else
    207 			err(1, "%s", dvname);
    208 	}
    209 
    210 	/*
    211 	 * Point the dvname at the actual device name that opendisk() opened.
    212 	 */
    213 	dvname = dvname_store;
    214 
    215 	/* Look up and call the command. */
    216 	for (i = 0; commands[i].cmd_name != NULL; i++)
    217 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    218 			break;
    219 	if (commands[i].cmd_name == NULL)
    220 		errx(1, "unknown command: %s", cmdname);
    221 
    222 	argnames = commands[i].arg_names;
    223 
    224 	(*commands[i].cmd_func)(argc, argv);
    225 	exit(0);
    226 }
    227 
    228 void
    229 usage(void)
    230 {
    231 	int i;
    232 
    233 	fprintf(stderr, "Usage: %s device command [arg [...]]\n",
    234 	    getprogname());
    235 
    236 	fprintf(stderr, "   Available device commands:\n");
    237 	for (i=0; commands[i].cmd_name != NULL; i++)
    238 		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
    239 					    commands[i].arg_names);
    240 
    241 	exit(1);
    242 }
    243 
    244 /*
    245  * Wrapper that calls ATAIOCCOMMAND and checks for errors
    246  */
    247 
    248 void
    249 ata_command(struct atareq *req)
    250 {
    251 	int error;
    252 
    253 	error = ioctl(fd, ATAIOCCOMMAND, req);
    254 
    255 	if (error == -1)
    256 		err(1, "ATAIOCCOMMAND failed");
    257 
    258 	switch (req->retsts) {
    259 
    260 	case ATACMD_OK:
    261 		return;
    262 	case ATACMD_TIMEOUT:
    263 		fprintf(stderr, "ATA command timed out\n");
    264 		exit(1);
    265 	case ATACMD_DF:
    266 		fprintf(stderr, "ATA device returned a Device Fault\n");
    267 		exit(1);
    268 	case ATACMD_ERROR:
    269 		if (req->error & WDCE_ABRT)
    270 			fprintf(stderr, "ATA device returned Aborted "
    271 				"Command\n");
    272 		else
    273 			fprintf(stderr, "ATA device returned error register "
    274 				"%0x\n", req->error);
    275 		exit(1);
    276 	default:
    277 		fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
    278 			"%d\n", req->retsts);
    279 		exit(1);
    280 	}
    281 }
    282 
    283 /*
    284  * Print out strings associated with particular bitmasks
    285  */
    286 
    287 void
    288 print_bitinfo(const char *bf, const char *af, u_int bits, struct bitinfo *binfo)
    289 {
    290 
    291 	for (; binfo->bitmask != NULL; binfo++)
    292 		if (bits & binfo->bitmask)
    293 			printf("%s%s%s", bf, binfo->string, af);
    294 }
    295 
    296 /*
    297  * Print out SMART attribute thresholds and values
    298  */
    299 
    300 void
    301 print_smart_status(void *vbuf, void *tbuf)
    302 {
    303 	struct ata_smart_attributes *value_buf = vbuf;
    304 	struct ata_smart_thresholds *threshold_buf = tbuf;
    305 	int values[256];
    306 	int thresholds[256];
    307 	int flags[256];
    308 	int i, j;
    309 	int id;
    310 	int8_t checksum;
    311 
    312 	for (i = checksum = 0; i < 511; i++)
    313 		checksum += ((int8_t *) value_buf)[i];
    314 	checksum *= -1;
    315 	if (checksum != value_buf->checksum) {
    316 		fprintf(stderr, "SMART attribute values checksum error\n");
    317 		return;
    318 	}
    319 
    320 	for (i = checksum = 0; i < 511; i++)
    321 		checksum += ((int8_t *) threshold_buf)[i];
    322 	checksum *= -1;
    323 	if (checksum != threshold_buf->checksum) {
    324 		fprintf(stderr, "SMART attribute thresholds checksum error\n");
    325 		return;
    326 	}
    327 
    328 	memset(values, 0, sizeof(values));
    329 	memset(thresholds, 0, sizeof(thresholds));
    330 	memset(flags, 0, sizeof(flags));
    331 
    332 	for (i = 0; i < 30; i++) {
    333 		id = value_buf->attributes[i].id;
    334 		values[id] = value_buf->attributes[i].value;
    335 		flags[id] = value_buf->attributes[i].flags;
    336 		id = threshold_buf->thresholds[i].id;
    337 		thresholds[id] = threshold_buf->thresholds[i].value;
    338 	}
    339 
    340 	printf("id\tvalue\tthresh\tcrit\tcollect\treliability description\n");
    341 	for (i = 0; i < 256; i++) {
    342 		if (values[i] != 00 && values[i] != 0xFE && values[i] != 0xFF) {
    343 			for (j = 0; smart_attrs[j].id != i && smart_attrs[j].id != 0; j++);
    344 			printf("%3d\t%3d\t%3d\t%s\t%sline\t%stive    %s\n",
    345 			       i, values[i], thresholds[i],
    346 			       flags[i] & WDSM_ATTR_ADVISORY ? "yes" : "no",
    347 			       flags[i] & WDSM_ATTR_COLLECTIVE ? "on" : "off",
    348 			       values[i] > thresholds[i] ? "posi" : "nega",
    349 			       smart_attrs[j].name);
    350 		}
    351 	}
    352 }
    353 
    354 /*
    355  * is_smart:
    356  *
    357  *	Detect whether device supports SMART and SMART is enabled.
    358  */
    359 
    360 int
    361 is_smart(int silent)
    362 {
    363 	int retval = 0;
    364 	struct atareq req;
    365 	unsigned char inbuf[DEV_BSIZE];
    366 	struct ataparams *inqbuf;
    367 	char *status;
    368 
    369 	memset(&inbuf, 0, sizeof(inbuf));
    370 	memset(&req, 0, sizeof(req));
    371 
    372 	inqbuf = (struct ataparams *) inbuf;
    373 
    374 	req.flags = ATACMD_READ;
    375 	req.command = WDCC_IDENTIFY;
    376 	req.databuf = (caddr_t) inbuf;
    377 	req.datalen = sizeof(inbuf);
    378 	req.timeout = 1000;
    379 
    380 	ata_command(&req);
    381 
    382 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    383 		if (!(inqbuf->atap_cmd_set1 & WDC_CMD1_SMART)) {
    384 			fprintf(stderr, "SMART unsupported\n");
    385 		} else {
    386 			if (inqbuf->atap_ata_major <= WDC_VER_ATA5 ||
    387 			    inqbuf->atap_cmd_set2 == 0xffff ||
    388 			    inqbuf->atap_cmd_set2 == 0x0000) {
    389 				status = "status unknown";
    390 				retval = 2;
    391 			} else {
    392 				if (inqbuf->atap_cmd1_en & WDC_CMD1_SMART) {
    393 					status = "enabled";
    394 					retval = 1;
    395 				} else {
    396 					status = "disabled";
    397 				}
    398 			}
    399 			if (!silent || retval == 0) {
    400 				printf("SMART supported, SMART %s\n", status);
    401 			}
    402 		}
    403 	}
    404 	return retval;
    405 }
    406 
    407 /*
    408  * DEVICE COMMANDS
    409  */
    410 
    411 /*
    412  * device_identify:
    413  *
    414  *	Display the identity of the device
    415  */
    416 void
    417 device_identify(int argc, char *argv[])
    418 {
    419 	struct ataparams *inqbuf;
    420 	struct atareq req;
    421 	unsigned char inbuf[DEV_BSIZE];
    422 #if BYTE_ORDER == LITTLE_ENDIAN
    423 	int i;
    424 	u_int16_t *p;
    425 #endif
    426 
    427 	/* No arguments. */
    428 	if (argc != 0)
    429 		usage();
    430 
    431 	memset(&inbuf, 0, sizeof(inbuf));
    432 	memset(&req, 0, sizeof(req));
    433 
    434 	inqbuf = (struct ataparams *) inbuf;
    435 
    436 	req.flags = ATACMD_READ;
    437 	req.command = WDCC_IDENTIFY;
    438 	req.databuf = (caddr_t) inbuf;
    439 	req.datalen = sizeof(inbuf);
    440 	req.timeout = 1000;
    441 
    442 	ata_command(&req);
    443 
    444 #if BYTE_ORDER == LITTLE_ENDIAN
    445 	/*
    446 	 * On little endian machines, we need to shuffle the string
    447 	 * byte order.  However, we don't have to do this for NEC or
    448 	 * Mitsumi ATAPI devices
    449 	 */
    450 
    451 	if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
    452 	      ((inqbuf->atap_model[0] == 'N' &&
    453 		  inqbuf->atap_model[1] == 'E') ||
    454 	       (inqbuf->atap_model[0] == 'F' &&
    455 		  inqbuf->atap_model[1] == 'X')))) {
    456 		for (i = 0 ; i < sizeof(inqbuf->atap_model); i += 2) {
    457 			p = (u_short *) (inqbuf->atap_model + i);
    458 			*p = ntohs(*p);
    459 		}
    460 		for (i = 0 ; i < sizeof(inqbuf->atap_serial); i += 2) {
    461 			p = (u_short *) (inqbuf->atap_serial + i);
    462 			*p = ntohs(*p);
    463 		}
    464 		for (i = 0 ; i < sizeof(inqbuf->atap_revision); i += 2) {
    465 			p = (u_short *) (inqbuf->atap_revision + i);
    466 			*p = ntohs(*p);
    467 		}
    468 	}
    469 #endif
    470 
    471 	/*
    472 	 * Strip blanks off of the info strings.  Yuck, I wish this was
    473 	 * cleaner.
    474 	 */
    475 
    476 	if (inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] == ' ') {
    477 		inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] = '\0';
    478 		while (inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] == ' ')
    479 			inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] = '\0';
    480 	}
    481 
    482 	if (inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] == ' ') {
    483 		inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] = '\0';
    484 		while (inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] == ' ')
    485 			inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] = '\0';
    486 	}
    487 
    488 	if (inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] == ' ') {
    489 		inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] = '\0';
    490 		while (inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] == ' ')
    491 			inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] = '\0';
    492 	}
    493 
    494 	printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
    495 	       (int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
    496 	       (int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
    497 	       (int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
    498 
    499 	printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
    500 	       "ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
    501 	       "removable");
    502 
    503 	if ((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == 0)
    504 		printf("Cylinders: %d, heads: %d, sec/track: %d, total "
    505 		       "sectors: %d\n", inqbuf->atap_cylinders,
    506 		       inqbuf->atap_heads, inqbuf->atap_sectors,
    507 		       (inqbuf->atap_capacity[1] << 16) |
    508 		       inqbuf->atap_capacity[0]);
    509 
    510 	if (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK)
    511 		printf("Device supports command queue depth of %d\n",
    512 		       inqbuf->atap_queuedepth & 0xf);
    513 
    514 	printf("Device capabilities:\n");
    515 	print_bitinfo("\t", "\n", inqbuf->atap_capabilities1, ata_caps);
    516 
    517 	if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
    518 		printf("Device supports following standards:\n");
    519 		print_bitinfo("", " ", inqbuf->atap_ata_major, ata_vers);
    520 		printf("\n");
    521 	}
    522 
    523 	if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
    524 	    inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
    525 		printf("Command set support:\n");
    526 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set1, ata_cmd_set1);
    527 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set2, ata_cmd_set2);
    528 	}
    529 
    530 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    531 		printf("Command sets/features enabled:\n");
    532 		print_bitinfo("\t", "\n", inqbuf->atap_cmd1_en &
    533 			      (WDC_CMD1_SRV | WDC_CMD1_RLSE | WDC_CMD1_AHEAD |
    534 			       WDC_CMD1_CACHE | WDC_CMD1_SEC | WDC_CMD1_SMART),
    535 			       ata_cmd_set1);
    536 		print_bitinfo("\t", "\n", inqbuf->atap_cmd2_en &
    537 			      (WDC_CMD2_RMSN | ATA_CMD2_APM), ata_cmd_set2);
    538 	}
    539 
    540 	return;
    541 }
    542 
    543 /*
    544  * device idle:
    545  *
    546  * issue the IDLE IMMEDIATE command to the drive
    547  */
    548 
    549 void
    550 device_idle(int argc, char *argv[])
    551 {
    552 	struct atareq req;
    553 
    554 	/* No arguments. */
    555 	if (argc != 0)
    556 		usage();
    557 
    558 	memset(&req, 0, sizeof(req));
    559 
    560 	if (strcmp(cmdname, "idle") == 0)
    561 		req.command = WDCC_IDLE_IMMED;
    562 	else if (strcmp(cmdname, "standby") == 0)
    563 		req.command = WDCC_STANDBY_IMMED;
    564 	else
    565 		req.command = WDCC_SLEEP;
    566 
    567 	req.timeout = 1000;
    568 
    569 	ata_command(&req);
    570 
    571 	return;
    572 }
    573 
    574 /*
    575  * Set the idle timer on the disk.  Set it for either idle mode or
    576  * standby mode, depending on how we were invoked.
    577  */
    578 
    579 void
    580 device_setidle(int argc, char *argv[])
    581 {
    582 	unsigned long idle;
    583 	struct atareq req;
    584 	char *end;
    585 
    586 	/* Only one argument */
    587 	if (argc != 1)
    588 		usage();
    589 
    590 	idle = strtoul(argv[0], &end, 0);
    591 
    592 	if (*end != '\0') {
    593 		fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
    594 		exit(1);
    595 	}
    596 
    597 	if (idle > 19800) {
    598 		fprintf(stderr, "Idle time has a maximum value of 5.5 "
    599 			"hours\n");
    600 		exit(1);
    601 	}
    602 
    603 	if (idle != 0 && idle < 5) {
    604 		fprintf(stderr, "Idle timer must be at least 5 seconds\n");
    605 		exit(1);
    606 	}
    607 
    608 	memset(&req, 0, sizeof(req));
    609 
    610 	if (idle <= 240*5)
    611 		req.sec_count = idle / 5;
    612 	else
    613 		req.sec_count = idle / (30*60) + 240;
    614 
    615 	req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
    616 	req.timeout = 1000;
    617 
    618 	ata_command(&req);
    619 
    620 	return;
    621 }
    622 
    623 /*
    624  * Query the device for the current power mode
    625  */
    626 
    627 void
    628 device_checkpower(int argc, char *argv[])
    629 {
    630 	struct atareq req;
    631 
    632 	/* No arguments. */
    633 	if (argc != 0)
    634 		usage();
    635 
    636 	memset(&req, 0, sizeof(req));
    637 
    638 	req.command = WDCC_CHECK_PWR;
    639 	req.timeout = 1000;
    640 	req.flags = ATACMD_READREG;
    641 
    642 	ata_command(&req);
    643 
    644 	printf("Current power status: ");
    645 
    646 	switch (req.sec_count) {
    647 	case 0x00:
    648 		printf("Standby mode\n");
    649 		break;
    650 	case 0x80:
    651 		printf("Idle mode\n");
    652 		break;
    653 	case 0xff:
    654 		printf("Active mode\n");
    655 		break;
    656 	default:
    657 		printf("Unknown power code (%02x)\n", req.sec_count);
    658 	}
    659 
    660 	return;
    661 }
    662 
    663 /*
    664  * device_smart:
    665  *
    666  *	Display SMART status
    667  */
    668 void
    669 device_smart(int argc, char *argv[])
    670 {
    671 	struct atareq req;
    672 	unsigned char inbuf[DEV_BSIZE];
    673 	unsigned char inbuf2[DEV_BSIZE];
    674 
    675 	/* Only one argument */
    676 	if (argc != 1)
    677 		usage();
    678 
    679 	if (strcmp(argv[0], "enable") == 0) {
    680 		if (is_smart(1)) {
    681 			memset(&req, 0, sizeof(req));
    682 
    683 			req.features = WDSM_ENABLE_OPS;
    684 			req.command = WDCC_SMART;
    685 			req.cylinder = htole16(WDSMART_CYL);
    686 			req.timeout = 1000;
    687 
    688 			ata_command(&req);
    689 
    690 			is_smart(0);
    691 		}
    692 	} else if (strcmp(argv[0], "disable") == 0) {
    693 		if (is_smart(1)) {
    694 			memset(&req, 0, sizeof(req));
    695 
    696 			req.features = WDSM_DISABLE_OPS;
    697 			req.command = WDCC_SMART;
    698 			req.cylinder = htole16(WDSMART_CYL);
    699 			req.timeout = 1000;
    700 
    701 			ata_command(&req);
    702 
    703 			is_smart(0);
    704 		}
    705 	} else if (strcmp(argv[0], "status") == 0) {
    706 		if (is_smart(0)) {
    707 			memset(&inbuf, 0, sizeof(inbuf));
    708 			memset(&req, 0, sizeof(req));
    709 
    710 			req.features = WDSM_STATUS;
    711 			req.command = WDCC_SMART;
    712 			req.cylinder = htole16(WDSMART_CYL);
    713 			req.timeout = 1000;
    714 
    715 			ata_command(&req);
    716 
    717 			if (req.cylinder != htole16(WDSMART_CYL)) {
    718 				fprintf(stderr, "Threshold exceeds condition\n");
    719 			}
    720 
    721 			/* WDSM_RD_DATA and WDSM_RD_THRESHOLDS are optional
    722 			 * features, the following ata_command()'s may error
    723 			 * and exit().
    724 			 */
    725 
    726 			memset(&inbuf, 0, sizeof(inbuf));
    727 			memset(&req, 0, sizeof(req));
    728 
    729 			req.flags = ATACMD_READ;
    730 			req.features = WDSM_RD_DATA;
    731 			req.command = WDCC_SMART;
    732 			req.databuf = (caddr_t) inbuf;
    733 			req.datalen = sizeof(inbuf);
    734 			req.cylinder = htole16(WDSMART_CYL);
    735 			req.timeout = 1000;
    736 
    737 			ata_command(&req);
    738 
    739 			memset(&inbuf2, 0, sizeof(inbuf2));
    740 			memset(&req, 0, sizeof(req));
    741 
    742 			req.flags = ATACMD_READ;
    743 			req.features = WDSM_RD_THRESHOLDS;
    744 			req.command = WDCC_SMART;
    745 			req.databuf = (caddr_t) inbuf2;
    746 			req.datalen = sizeof(inbuf2);
    747 			req.cylinder = htole16(WDSMART_CYL);
    748 			req.timeout = 1000;
    749 
    750 			ata_command(&req);
    751 
    752 			print_smart_status(inbuf, inbuf2);
    753 		} else {
    754 			fprintf(stderr, "SMART not supported\n");
    755 		}
    756 	} else {
    757 		usage();
    758 	}
    759 	return;
    760 }
    761