Home | History | Annotate | Line # | Download | only in atactl
atactl.c revision 1.32
      1  1.32   atatat /*	$NetBSD: atactl.c,v 1.32 2004/09/10 04:11:09 atatat Exp $	*/
      2   1.1     kenh 
      3   1.1     kenh /*-
      4   1.1     kenh  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1     kenh  * All rights reserved.
      6   1.1     kenh  *
      7   1.1     kenh  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     kenh  * by Ken Hornstein.
      9   1.1     kenh  *
     10   1.1     kenh  * Redistribution and use in source and binary forms, with or without
     11   1.1     kenh  * modification, are permitted provided that the following conditions
     12   1.1     kenh  * are met:
     13   1.1     kenh  * 1. Redistributions of source code must retain the above copyright
     14   1.1     kenh  *    notice, this list of conditions and the following disclaimer.
     15   1.1     kenh  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     kenh  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     kenh  *    documentation and/or other materials provided with the distribution.
     18   1.1     kenh  * 3. All advertising materials mentioning features or use of this software
     19   1.1     kenh  *    must display the following acknowledgement:
     20   1.1     kenh  *	This product includes software developed by the NetBSD
     21   1.1     kenh  *	Foundation, Inc. and its contributors.
     22   1.1     kenh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1     kenh  *    contributors may be used to endorse or promote products derived
     24   1.1     kenh  *    from this software without specific prior written permission.
     25   1.1     kenh  *
     26   1.1     kenh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1     kenh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1     kenh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1     kenh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1     kenh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1     kenh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1     kenh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1     kenh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1     kenh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1     kenh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1     kenh  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     kenh  */
     38   1.1     kenh 
     39   1.1     kenh /*
     40   1.4    jwise  * atactl(8) - a program to control ATA devices.
     41   1.1     kenh  */
     42  1.21      agc #include <sys/cdefs.h>
     43  1.21      agc 
     44  1.21      agc #ifndef lint
     45  1.32   atatat __RCSID("$NetBSD: atactl.c,v 1.32 2004/09/10 04:11:09 atatat Exp $");
     46  1.21      agc #endif
     47  1.21      agc 
     48   1.1     kenh 
     49   1.1     kenh #include <sys/param.h>
     50   1.1     kenh #include <sys/ioctl.h>
     51   1.1     kenh #include <err.h>
     52   1.1     kenh #include <errno.h>
     53   1.1     kenh #include <fcntl.h>
     54   1.1     kenh #include <stdio.h>
     55   1.1     kenh #include <stdlib.h>
     56   1.1     kenh #include <string.h>
     57   1.1     kenh #include <unistd.h>
     58   1.1     kenh #include <util.h>
     59   1.1     kenh 
     60   1.1     kenh #include <dev/ata/atareg.h>
     61   1.1     kenh #include <sys/ataio.h>
     62   1.1     kenh 
     63   1.1     kenh struct command {
     64   1.1     kenh 	const char *cmd_name;
     65   1.5    soren 	const char *arg_names;
     66  1.13   simonb 	void (*cmd_func)(int, char *[]);
     67   1.1     kenh };
     68   1.1     kenh 
     69   1.1     kenh struct bitinfo {
     70   1.1     kenh 	u_int bitmask;
     71   1.1     kenh 	const char *string;
     72   1.1     kenh };
     73   1.1     kenh 
     74  1.13   simonb int	main(int, char *[]);
     75  1.13   simonb void	usage(void);
     76  1.13   simonb void	ata_command(struct atareq *);
     77  1.13   simonb void	print_bitinfo(const char *, const char *, u_int, struct bitinfo *);
     78  1.24      lha void	print_smart_status(void *, void *);
     79  1.24      lha void	print_selftest_entry(int, struct ata_smart_selftest *);
     80  1.24      lha 
     81  1.24      lha void	print_selftest(void *);
     82  1.24      lha 
     83  1.20  mycroft int	is_smart(void);
     84   1.1     kenh 
     85   1.1     kenh int	fd;				/* file descriptor for device */
     86   1.1     kenh const	char *dvname;			/* device name */
     87   1.1     kenh char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     88   1.1     kenh const	char *cmdname;			/* command user issued */
     89   1.5    soren const	char *argnames;			/* helpstring: expected arguments */
     90   1.1     kenh 
     91  1.13   simonb void	device_identify(int, char *[]);
     92  1.13   simonb void	device_setidle(int, char *[]);
     93  1.13   simonb void	device_idle(int, char *[]);
     94  1.13   simonb void	device_checkpower(int, char *[]);
     95  1.15    soren void	device_smart(int, char *[]);
     96   1.1     kenh 
     97  1.30   bouyer void	device_smart_temp(struct ata_smart_attr *, uint64_t);
     98  1.24      lha 
     99  1.30   bouyer struct command device_commands[] = {
    100   1.5    soren 	{ "identify",	"",			device_identify },
    101   1.5    soren 	{ "setidle",	"idle-timer",		device_setidle },
    102   1.5    soren 	{ "setstandby",	"standby-timer",	device_setidle },
    103   1.5    soren 	{ "idle",	"",			device_idle },
    104   1.5    soren 	{ "standby",	"",			device_idle },
    105   1.5    soren 	{ "sleep",	"",			device_idle },
    106   1.5    soren 	{ "checkpower",	"",			device_checkpower },
    107  1.24      lha 	{ "smart",	"enable|disable|status|selftest-log", device_smart },
    108   1.5    soren 	{ NULL,		NULL,			NULL },
    109   1.1     kenh };
    110   1.1     kenh 
    111  1.30   bouyer void	bus_reset __P((int, char *[]));
    112  1.30   bouyer 
    113  1.30   bouyer struct command bus_commands[] = {
    114  1.30   bouyer 	{ "reset",	"",			bus_reset },
    115  1.30   bouyer 	{ NULL,		NULL,			NULL },
    116  1.30   bouyer };
    117  1.30   bouyer 
    118   1.1     kenh /*
    119   1.1     kenh  * Tables containing bitmasks used for error reporting and
    120   1.1     kenh  * device identification.
    121   1.1     kenh  */
    122   1.1     kenh 
    123   1.1     kenh struct bitinfo ata_caps[] = {
    124  1.23     yamt 	{ WDC_CAP_DMA, "DMA" },
    125  1.23     yamt 	{ WDC_CAP_LBA, "LBA" },
    126   1.1     kenh 	{ ATA_CAP_STBY, "ATA standby timer values" },
    127   1.1     kenh 	{ WDC_CAP_IORDY, "IORDY operation" },
    128   1.1     kenh 	{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
    129  1.22     fvdl 	{ 0, NULL },
    130   1.1     kenh };
    131   1.1     kenh 
    132   1.1     kenh struct bitinfo ata_vers[] = {
    133   1.1     kenh 	{ WDC_VER_ATA1,	"ATA-1" },
    134   1.1     kenh 	{ WDC_VER_ATA2,	"ATA-2" },
    135   1.1     kenh 	{ WDC_VER_ATA3,	"ATA-3" },
    136   1.1     kenh 	{ WDC_VER_ATA4,	"ATA-4" },
    137  1.23     yamt 	{ WDC_VER_ATA5,	"ATA-5" },
    138  1.23     yamt 	{ WDC_VER_ATA6,	"ATA-6" },
    139  1.23     yamt 	{ WDC_VER_ATA7,	"ATA-7" },
    140  1.22     fvdl 	{ 0, NULL },
    141   1.1     kenh };
    142   1.1     kenh 
    143   1.1     kenh struct bitinfo ata_cmd_set1[] = {
    144   1.1     kenh 	{ WDC_CMD1_NOP, "NOP command" },
    145   1.1     kenh 	{ WDC_CMD1_RB, "READ BUFFER command" },
    146   1.1     kenh 	{ WDC_CMD1_WB, "WRITE BUFFER command" },
    147   1.1     kenh 	{ WDC_CMD1_HPA, "Host Protected Area feature set" },
    148   1.1     kenh 	{ WDC_CMD1_DVRST, "DEVICE RESET command" },
    149   1.1     kenh 	{ WDC_CMD1_SRV, "SERVICE interrupt" },
    150   1.1     kenh 	{ WDC_CMD1_RLSE, "release interrupt" },
    151   1.1     kenh 	{ WDC_CMD1_AHEAD, "look-ahead" },
    152   1.1     kenh 	{ WDC_CMD1_CACHE, "write cache" },
    153   1.1     kenh 	{ WDC_CMD1_PKT, "PACKET command feature set" },
    154   1.1     kenh 	{ WDC_CMD1_PM, "Power Management feature set" },
    155   1.1     kenh 	{ WDC_CMD1_REMOV, "Removable Media feature set" },
    156   1.1     kenh 	{ WDC_CMD1_SEC, "Security Mode feature set" },
    157   1.1     kenh 	{ WDC_CMD1_SMART, "SMART feature set" },
    158  1.22     fvdl 	{ 0, NULL },
    159   1.1     kenh };
    160   1.1     kenh 
    161   1.1     kenh struct bitinfo ata_cmd_set2[] = {
    162  1.23     yamt 	{ ATA_CMD2_FCE, "FLUSH CACHE EXT command" },
    163  1.23     yamt 	{ WDC_CMD2_FC, "FLUSH CACHE command" },
    164  1.23     yamt 	{ WDC_CMD2_DCO, "Device Configuration Overlay feature set" },
    165  1.23     yamt 	{ ATA_CMD2_LBA48, "48-bit Address feature set" },
    166  1.23     yamt 	{ WDC_CMD2_AAM, "Automatic Acoustic Management feature set" },
    167  1.28      wiz 	{ WDC_CMD2_SM, "SET MAX security extension" },
    168  1.23     yamt 	{ WDC_CMD2_SFREQ, "SET FEATURES required to spin-up after power-up" },
    169  1.23     yamt 	{ WDC_CMD2_PUIS, "Power-Up In Standby feature set" },
    170   1.1     kenh 	{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
    171   1.1     kenh 	{ ATA_CMD2_APM, "Advanced Power Management feature set" },
    172   1.1     kenh 	{ ATA_CMD2_CFA, "CFA feature set" },
    173   1.6    soren 	{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
    174   1.1     kenh 	{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
    175  1.22     fvdl 	{ 0, NULL },
    176   1.1     kenh };
    177   1.1     kenh 
    178  1.23     yamt struct bitinfo ata_cmd_ext[] = {
    179  1.23     yamt 	{ ATA_CMDE_TLCONT, "Time-limited R/W feature set R/W Continuous mode" },
    180  1.23     yamt 	{ ATA_CMDE_TL, "Time-limited Read/Write" },
    181  1.23     yamt 	{ ATA_CMDE_URGW, "URG bit for WRITE STREAM DMA/PIO" },
    182  1.23     yamt 	{ ATA_CMDE_URGR, "URG bit for READ STREAM DMA/PIO" },
    183  1.23     yamt 	{ ATA_CMDE_WWN, "World Wide name" },
    184  1.23     yamt 	{ ATA_CMDE_WQFE, "WRITE DMA QUEUED FUA EXT command" },
    185  1.23     yamt 	{ ATA_CMDE_WFE, "WRITE DMA/MULTIPLE FUA EXT commands" },
    186  1.23     yamt 	{ ATA_CMDE_GPL, "General Purpose Logging feature set" },
    187  1.23     yamt 	{ ATA_CMDE_STREAM, "Streaming feature set" },
    188  1.23     yamt 	{ ATA_CMDE_MCPTC, "Media Card Pass Through Command feature set" },
    189  1.23     yamt 	{ ATA_CMDE_MS, "Media serial number" },
    190  1.23     yamt 	{ ATA_CMDE_SST, "SMART self-test" },
    191  1.23     yamt 	{ ATA_CMDE_SEL, "SMART error logging" },
    192  1.23     yamt 	{ 0, NULL },
    193  1.23     yamt };
    194  1.23     yamt 
    195  1.17    soren static const struct {
    196  1.17    soren 	const int	id;
    197  1.17    soren 	const char	*name;
    198  1.29  mycroft 	void (*special)(struct ata_smart_attr *, uint64_t);
    199  1.17    soren } smart_attrs[] = {
    200  1.32   atatat 	{   1,		"Raw read error rate" },
    201  1.32   atatat 	{   2,		"Throughput performance" },
    202  1.32   atatat 	{   3,		"Spin-up time" },
    203  1.32   atatat 	{   4,		"Start/stop count" },
    204  1.32   atatat 	{   5,		"Reallocated sector count" },
    205  1.32   atatat 	{   7,		"Seek error rate" },
    206  1.32   atatat 	{   8,		"Seek time performance" },
    207  1.32   atatat 	{   9,		"Power-on hours count" },
    208  1.32   atatat 	{  10,		"Spin retry count" },
    209  1.32   atatat 	{  11,		"Calibration retry count" },
    210  1.32   atatat 	{  12,		"Device power cycle count" },
    211  1.17    soren 	{ 191,		"Gsense error rate" },
    212  1.17    soren 	{ 192,		"Power-off retract count" },
    213  1.17    soren 	{ 193,		"Load cycle count" },
    214  1.30   bouyer 	{ 194,		"Temperature",			device_smart_temp},
    215  1.24      lha 	{ 195,		"Hardware ECC Recovered" },
    216  1.17    soren 	{ 196,		"Reallocated event count" },
    217  1.17    soren 	{ 197,		"Current pending sector" },
    218  1.17    soren 	{ 198,		"Offline uncorrectable" },
    219  1.17    soren 	{ 199,		"Ultra DMA CRC error count" },
    220  1.32   atatat 	{ 200,		"Write error rate" },
    221  1.32   atatat 	{ 201,		"Soft read error rate" },
    222  1.32   atatat 	{ 202,		"Data address mark errors" },
    223  1.32   atatat 	{ 203,		"Run out cancel" },
    224  1.32   atatat 	{ 204,		"Soft ECC correction" },
    225  1.32   atatat 	{ 205,		"Thermal asperity check" },
    226  1.32   atatat 	{ 206,		"Flying height" },
    227  1.32   atatat 	{ 207,		"Spin high current" },
    228  1.32   atatat 	{ 208,		"Spin buzz" },
    229  1.32   atatat 	{ 209,		"Offline seek performance" },
    230  1.32   atatat 	{ 220,		"Disk shift" },
    231  1.32   atatat 	{ 221,		"G-Sense error rate" },
    232  1.32   atatat 	{ 222,		"Loaded hours" },
    233  1.32   atatat 	{ 223,		"Load/unload retry count" },
    234  1.32   atatat 	{ 224,		"Load friction" },
    235  1.32   atatat 	{ 225,		"Load/unload cycle count" },
    236  1.32   atatat 	{ 226,		"Load-in time" },
    237  1.32   atatat 	{ 227,		"Torque amplification count" },
    238  1.32   atatat 	{ 228,		"Power-off retract count" },
    239  1.32   atatat 	{ 230,		"GMR head amplitude" },
    240  1.32   atatat 	{ 231,		"Temperature",			device_smart_temp },
    241  1.32   atatat 	{ 240,		"Head flying hours" },
    242  1.32   atatat 	{ 250,		"Read error retry rate" },
    243  1.32   atatat 	{   0,		"Unknown" },
    244  1.17    soren };
    245  1.17    soren 
    246   1.1     kenh int
    247  1.13   simonb main(int argc, char *argv[])
    248   1.1     kenh {
    249   1.1     kenh 	int i;
    250  1.30   bouyer 	struct command *commands = NULL;
    251   1.1     kenh 
    252   1.1     kenh 	/* Must have at least: device command */
    253   1.1     kenh 	if (argc < 3)
    254   1.1     kenh 		usage();
    255   1.1     kenh 
    256   1.1     kenh 	/* Skip program name, get and skip device name and command. */
    257   1.1     kenh 	dvname = argv[1];
    258   1.1     kenh 	cmdname = argv[2];
    259   1.1     kenh 	argv += 3;
    260   1.1     kenh 	argc -= 3;
    261   1.1     kenh 
    262   1.1     kenh 	/*
    263   1.1     kenh 	 * Open the device
    264   1.1     kenh 	 */
    265   1.1     kenh 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    266   1.1     kenh 	if (fd == -1) {
    267   1.1     kenh 		if (errno == ENOENT) {
    268   1.1     kenh 			/*
    269   1.1     kenh 			 * Device doesn't exist.  Probably trying to open
    270   1.1     kenh 			 * a device which doesn't use disk semantics for
    271   1.1     kenh 			 * device name.  Try again, specifying "cooked",
    272   1.1     kenh 			 * which leaves off the "r" in front of the device's
    273   1.1     kenh 			 * name.
    274   1.1     kenh 			 */
    275   1.1     kenh 			fd = opendisk(dvname, O_RDWR, dvname_store,
    276   1.1     kenh 			    sizeof(dvname_store), 1);
    277   1.1     kenh 			if (fd == -1)
    278   1.1     kenh 				err(1, "%s", dvname);
    279   1.4    jwise 		} else
    280   1.4    jwise 			err(1, "%s", dvname);
    281   1.1     kenh 	}
    282   1.1     kenh 
    283   1.1     kenh 	/*
    284   1.1     kenh 	 * Point the dvname at the actual device name that opendisk() opened.
    285   1.1     kenh 	 */
    286   1.1     kenh 	dvname = dvname_store;
    287   1.1     kenh 
    288   1.1     kenh 	/* Look up and call the command. */
    289  1.30   bouyer 	for (i = 0; device_commands[i].cmd_name != NULL; i++) {
    290  1.30   bouyer 		if (strcmp(cmdname, device_commands[i].cmd_name) == 0) {
    291  1.30   bouyer 			commands = &device_commands[i];
    292   1.1     kenh 			break;
    293  1.30   bouyer 		}
    294  1.30   bouyer 	}
    295  1.30   bouyer 	if (commands == NULL) {
    296  1.30   bouyer 		for (i = 0; bus_commands[i].cmd_name != NULL; i++) {
    297  1.30   bouyer 			if (strcmp(cmdname, bus_commands[i].cmd_name) == 0) {
    298  1.30   bouyer 				commands = &bus_commands[i];
    299  1.30   bouyer 				break;
    300  1.30   bouyer 			}
    301  1.30   bouyer 		}
    302  1.30   bouyer 	}
    303  1.30   bouyer 	if (commands == NULL)
    304  1.12       ad 		errx(1, "unknown command: %s", cmdname);
    305   1.1     kenh 
    306  1.30   bouyer 	argnames = commands->arg_names;
    307   1.5    soren 
    308  1.30   bouyer 	(*commands->cmd_func)(argc, argv);
    309   1.1     kenh 	exit(0);
    310   1.1     kenh }
    311   1.1     kenh 
    312   1.1     kenh void
    313  1.13   simonb usage(void)
    314   1.1     kenh {
    315   1.5    soren 	int i;
    316   1.1     kenh 
    317  1.27     jmmv 	fprintf(stderr, "usage: %s device command [arg [...]]\n",
    318  1.11      cgd 	    getprogname());
    319   1.5    soren 
    320   1.5    soren 	fprintf(stderr, "   Available device commands:\n");
    321  1.30   bouyer 	for (i=0; device_commands[i].cmd_name != NULL; i++)
    322  1.30   bouyer 		fprintf(stderr, "\t%s %s\n", device_commands[i].cmd_name,
    323  1.30   bouyer 					    device_commands[i].arg_names);
    324  1.30   bouyer 
    325  1.30   bouyer 	fprintf(stderr, "   Available bus commands:\n");
    326  1.30   bouyer 	for (i=0; bus_commands[i].cmd_name != NULL; i++)
    327  1.30   bouyer 		fprintf(stderr, "\t%s %s\n", bus_commands[i].cmd_name,
    328  1.30   bouyer 					    bus_commands[i].arg_names);
    329   1.5    soren 
    330   1.1     kenh 	exit(1);
    331   1.1     kenh }
    332   1.1     kenh 
    333   1.1     kenh /*
    334   1.1     kenh  * Wrapper that calls ATAIOCCOMMAND and checks for errors
    335   1.1     kenh  */
    336   1.1     kenh 
    337   1.1     kenh void
    338  1.13   simonb ata_command(struct atareq *req)
    339   1.1     kenh {
    340   1.1     kenh 	int error;
    341   1.1     kenh 
    342   1.1     kenh 	error = ioctl(fd, ATAIOCCOMMAND, req);
    343   1.1     kenh 
    344   1.1     kenh 	if (error == -1)
    345   1.1     kenh 		err(1, "ATAIOCCOMMAND failed");
    346   1.1     kenh 
    347   1.1     kenh 	switch (req->retsts) {
    348   1.1     kenh 
    349   1.1     kenh 	case ATACMD_OK:
    350   1.1     kenh 		return;
    351   1.1     kenh 	case ATACMD_TIMEOUT:
    352   1.1     kenh 		fprintf(stderr, "ATA command timed out\n");
    353   1.1     kenh 		exit(1);
    354   1.1     kenh 	case ATACMD_DF:
    355   1.1     kenh 		fprintf(stderr, "ATA device returned a Device Fault\n");
    356   1.1     kenh 		exit(1);
    357   1.1     kenh 	case ATACMD_ERROR:
    358   1.1     kenh 		if (req->error & WDCE_ABRT)
    359   1.1     kenh 			fprintf(stderr, "ATA device returned Aborted "
    360   1.1     kenh 				"Command\n");
    361   1.1     kenh 		else
    362   1.1     kenh 			fprintf(stderr, "ATA device returned error register "
    363   1.1     kenh 				"%0x\n", req->error);
    364   1.1     kenh 		exit(1);
    365   1.1     kenh 	default:
    366   1.1     kenh 		fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
    367   1.1     kenh 			"%d\n", req->retsts);
    368   1.1     kenh 		exit(1);
    369   1.1     kenh 	}
    370   1.1     kenh }
    371   1.1     kenh 
    372   1.1     kenh /*
    373   1.1     kenh  * Print out strings associated with particular bitmasks
    374   1.1     kenh  */
    375   1.1     kenh 
    376   1.1     kenh void
    377  1.13   simonb print_bitinfo(const char *bf, const char *af, u_int bits, struct bitinfo *binfo)
    378   1.1     kenh {
    379   1.1     kenh 
    380  1.22     fvdl 	for (; binfo->bitmask != 0; binfo++)
    381   1.1     kenh 		if (bits & binfo->bitmask)
    382  1.10       is 			printf("%s%s%s", bf, binfo->string, af);
    383   1.1     kenh }
    384   1.1     kenh 
    385  1.24      lha 
    386  1.24      lha /*
    387  1.24      lha  * Try to print SMART temperature field
    388  1.24      lha  */
    389  1.24      lha 
    390  1.24      lha void
    391  1.30   bouyer device_smart_temp(struct ata_smart_attr *attr, uint64_t raw_value)
    392  1.24      lha {
    393  1.29  mycroft 	printf("%" PRIu8, attr->raw[0]);
    394  1.24      lha 	if (attr->raw[0] != raw_value)
    395  1.29  mycroft 		printf(" Lifetime max/min %" PRIu8 "/%" PRIu8,
    396  1.29  mycroft 		    attr->raw[2], attr->raw[4]);
    397  1.24      lha }
    398  1.24      lha 
    399  1.24      lha 
    400   1.1     kenh /*
    401  1.15    soren  * Print out SMART attribute thresholds and values
    402  1.15    soren  */
    403  1.15    soren 
    404  1.15    soren void
    405  1.15    soren print_smart_status(void *vbuf, void *tbuf)
    406  1.15    soren {
    407  1.15    soren 	struct ata_smart_attributes *value_buf = vbuf;
    408  1.15    soren 	struct ata_smart_thresholds *threshold_buf = tbuf;
    409  1.24      lha 	struct ata_smart_attr *attr;
    410  1.29  mycroft 	uint64_t raw_value;
    411  1.24      lha 	int flags;
    412  1.17    soren 	int i, j;
    413  1.24      lha 	int aid;
    414  1.15    soren 	int8_t checksum;
    415  1.15    soren 
    416  1.15    soren 	for (i = checksum = 0; i < 511; i++)
    417  1.15    soren 		checksum += ((int8_t *) value_buf)[i];
    418  1.15    soren 	checksum *= -1;
    419  1.15    soren 	if (checksum != value_buf->checksum) {
    420  1.15    soren 		fprintf(stderr, "SMART attribute values checksum error\n");
    421  1.15    soren 		return;
    422  1.15    soren 	}
    423  1.15    soren 
    424  1.15    soren 	for (i = checksum = 0; i < 511; i++)
    425  1.15    soren 		checksum += ((int8_t *) threshold_buf)[i];
    426  1.15    soren 	checksum *= -1;
    427  1.15    soren 	if (checksum != threshold_buf->checksum) {
    428  1.15    soren 		fprintf(stderr, "SMART attribute thresholds checksum error\n");
    429  1.15    soren 		return;
    430  1.15    soren 	}
    431  1.15    soren 
    432  1.24      lha 	printf("id value thresh crit collect reliability description\t\t\traw\n");
    433  1.24      lha 	for (i = 0; i < 256; i++) {
    434  1.24      lha 		int thresh = 0;
    435  1.24      lha 
    436  1.24      lha 		attr = NULL;
    437  1.24      lha 
    438  1.24      lha 		for (j = 0; j < 30; j++) {
    439  1.24      lha 			if (value_buf->attributes[j].id == i)
    440  1.24      lha 				attr = &value_buf->attributes[j];
    441  1.24      lha 			if (threshold_buf->thresholds[j].id == i)
    442  1.24      lha 				thresh = threshold_buf->thresholds[j].value;
    443  1.31   atatat 		}
    444  1.15    soren 
    445  1.24      lha 		if (thresh && attr == NULL)
    446  1.24      lha 			errx(1, "threshold but not attr %d", i);
    447  1.24      lha 		if (attr == NULL)
    448  1.24      lha 			continue;
    449  1.24      lha 
    450  1.24      lha 		if (attr->value == 0||attr->value == 0xFE||attr->value == 0xFF)
    451  1.24      lha 			continue;
    452  1.24      lha 
    453  1.24      lha 		for (aid = 0;
    454  1.24      lha 		     smart_attrs[aid].id != i && smart_attrs[aid].id != 0;
    455  1.24      lha 		     aid++)
    456  1.24      lha 			;
    457  1.24      lha 
    458  1.24      lha 		flags = attr->flags;
    459  1.24      lha 
    460  1.29  mycroft 		printf("%3d %3d  %3d     %-3s %-7s %stive    %-24s\t",
    461  1.24      lha 		    i, attr->value, thresh,
    462  1.24      lha 		    flags & WDSM_ATTR_ADVISORY ? "yes" : "no",
    463  1.24      lha 		    flags & WDSM_ATTR_COLLECTIVE ? "online" : "offline",
    464  1.24      lha 		    attr->value > thresh ? "posi" : "nega",
    465  1.24      lha 		    smart_attrs[aid].name);
    466  1.24      lha 
    467  1.24      lha 		for (j = 0, raw_value = 0; j < 6; j++)
    468  1.29  mycroft 			raw_value += ((uint64_t)attr->raw[j]) << (8*j);
    469  1.24      lha 
    470  1.24      lha 		if (smart_attrs[aid].special)
    471  1.24      lha 			(*smart_attrs[aid].special)(attr, raw_value);
    472  1.29  mycroft 		else
    473  1.29  mycroft 			printf("%" PRIu64, raw_value);
    474  1.24      lha 		printf("\n");
    475  1.15    soren 		}
    476  1.15    soren 	}
    477  1.24      lha 
    478  1.24      lha struct {
    479  1.24      lha 	int number;
    480  1.24      lha 	const char *name;
    481  1.24      lha } selftest_name[] = {
    482  1.24      lha 	{ 0, "Off-line" },
    483  1.24      lha 	{ 1, "Short off-line" },
    484  1.24      lha 	{ 2, "Extended off-line" },
    485  1.24      lha 	{ 127, "Abort off-line test" },
    486  1.24      lha 	{ 129, "Short captive" },
    487  1.24      lha 	{ 130, "Extended captive" },
    488  1.24      lha 	{ 256, "Unknown test" }, /* larger then u_int8_t */
    489  1.24      lha 	{ 0, NULL }
    490  1.24      lha };
    491  1.24      lha 
    492  1.24      lha const char *selftest_status[] = {
    493  1.24      lha 	"No error",
    494  1.24      lha 	"Aborted by the host",
    495  1.24      lha 	"Interruped by the host by reset",
    496  1.24      lha 	"Fatal error or unknown test error",
    497  1.24      lha 	"Unknown test element failed",
    498  1.24      lha 	"Electrical test element failed",
    499  1.24      lha 	"The Servo (and/or seek) test element failed",
    500  1.24      lha 	"Read element of test failed",
    501  1.24      lha 	"Reserved",
    502  1.24      lha 	"Reserved",
    503  1.24      lha 	"Reserved",
    504  1.24      lha 	"Reserved",
    505  1.24      lha 	"Reserved",
    506  1.24      lha 	"Reserved",
    507  1.24      lha 	"Reserved",
    508  1.24      lha 	"Self-test in progress"
    509  1.24      lha };
    510  1.24      lha 
    511  1.24      lha void
    512  1.24      lha print_selftest_entry(int num, struct ata_smart_selftest *le)
    513  1.24      lha {
    514  1.24      lha 	unsigned char *p;
    515  1.24      lha 	int i;
    516  1.24      lha 
    517  1.24      lha 	/* check if all zero */
    518  1.24      lha 	for (p = (void *)le, i = 0; i < sizeof(*le); i++)
    519  1.24      lha 		if (p[i] != 0)
    520  1.24      lha 			break;
    521  1.24      lha 	if (i == sizeof(*le))
    522  1.24      lha 		return;
    523  1.24      lha 
    524  1.24      lha 	printf("Log entry: %d\n", num);
    525  1.24      lha 
    526  1.24      lha 	/* Get test name */
    527  1.24      lha 	for (i = 0; selftest_name[i].name != NULL; i++)
    528  1.24      lha 		if (selftest_name[i].number == le->number)
    529  1.24      lha 			break;
    530  1.24      lha 	if (selftest_name[i].number == 0)
    531  1.24      lha 		i = 255; /* unknown test */
    532  1.24      lha 
    533  1.24      lha 	printf("\tName: %s\n", selftest_name[i].name);
    534  1.24      lha 	printf("\tStatus: %s\n", selftest_status[le->status >> 4]);
    535  1.24      lha 	if (le->status >> 4 == 15)
    536  1.24      lha 		printf("\tPrecent of test remaning: %1d0\n", le->status & 0xf);
    537  1.24      lha 	if (le->status)
    538  1.24      lha 		printf("LBA first error: %d\n", le->lba_first_error);
    539  1.24      lha }
    540  1.24      lha 
    541  1.24      lha void
    542  1.24      lha print_selftest(void *buf)
    543  1.24      lha {
    544  1.24      lha 	struct ata_smart_selftestlog *stlog = buf;
    545  1.24      lha 	int8_t checksum;
    546  1.24      lha 	int i;
    547  1.24      lha 
    548  1.24      lha 	for (i = checksum = 0; i < 511; i++)
    549  1.24      lha 		checksum += ((int8_t *) buf)[i];
    550  1.24      lha   	checksum *= -1;
    551  1.24      lha 	if ((u_int8_t)checksum != stlog->checksum) {
    552  1.24      lha 		fprintf(stderr, "SMART selftest log checksum error\n");
    553  1.24      lha 		return;
    554  1.24      lha 	}
    555  1.24      lha 
    556  1.24      lha 	if (stlog->data_structure_revision != 1) {
    557  1.24      lha 		fprintf(stderr, "Log revision not 1");
    558  1.24      lha 		return;
    559  1.24      lha 	}
    560  1.24      lha 
    561  1.24      lha 	if (stlog->mostrecenttest == 0) {
    562  1.24      lha 		printf("No self-tests have been logged\n");
    563  1.24      lha 		return;
    564  1.24      lha 	}
    565  1.24      lha 
    566  1.24      lha 	if (stlog->mostrecenttest > 22) {
    567  1.24      lha 		fprintf(stderr, "Most recent test is too large\n");
    568  1.24      lha 		return;
    569  1.24      lha 	}
    570  1.24      lha 
    571  1.24      lha 	for (i = stlog->mostrecenttest; i < 22; i++)
    572  1.24      lha 		print_selftest_entry(i, &stlog->log_entries[i]);
    573  1.24      lha 	for (i = 0; i < stlog->mostrecenttest; i++)
    574  1.24      lha 		print_selftest_entry(i, &stlog->log_entries[i]);
    575  1.15    soren }
    576  1.15    soren 
    577  1.15    soren /*
    578  1.15    soren  * is_smart:
    579  1.15    soren  *
    580  1.15    soren  *	Detect whether device supports SMART and SMART is enabled.
    581  1.15    soren  */
    582  1.15    soren 
    583  1.15    soren int
    584  1.20  mycroft is_smart(void)
    585  1.15    soren {
    586  1.15    soren 	int retval = 0;
    587  1.15    soren 	struct atareq req;
    588  1.15    soren 	unsigned char inbuf[DEV_BSIZE];
    589  1.15    soren 	struct ataparams *inqbuf;
    590  1.15    soren 	char *status;
    591  1.15    soren 
    592  1.15    soren 	memset(&inbuf, 0, sizeof(inbuf));
    593  1.15    soren 	memset(&req, 0, sizeof(req));
    594  1.15    soren 
    595  1.15    soren 	inqbuf = (struct ataparams *) inbuf;
    596  1.15    soren 
    597  1.15    soren 	req.flags = ATACMD_READ;
    598  1.15    soren 	req.command = WDCC_IDENTIFY;
    599  1.15    soren 	req.databuf = (caddr_t) inbuf;
    600  1.15    soren 	req.datalen = sizeof(inbuf);
    601  1.15    soren 	req.timeout = 1000;
    602  1.15    soren 
    603  1.15    soren 	ata_command(&req);
    604  1.15    soren 
    605  1.15    soren 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    606  1.15    soren 		if (!(inqbuf->atap_cmd_set1 & WDC_CMD1_SMART)) {
    607  1.15    soren 			fprintf(stderr, "SMART unsupported\n");
    608  1.15    soren 		} else {
    609  1.15    soren 			if (inqbuf->atap_ata_major <= WDC_VER_ATA5 ||
    610  1.15    soren 			    inqbuf->atap_cmd_set2 == 0xffff ||
    611  1.15    soren 			    inqbuf->atap_cmd_set2 == 0x0000) {
    612  1.15    soren 				status = "status unknown";
    613  1.15    soren 				retval = 2;
    614  1.15    soren 			} else {
    615  1.18  mycroft 				if (inqbuf->atap_cmd1_en & WDC_CMD1_SMART) {
    616  1.15    soren 					status = "enabled";
    617  1.15    soren 					retval = 1;
    618  1.15    soren 				} else {
    619  1.15    soren 					status = "disabled";
    620  1.15    soren 				}
    621  1.15    soren 			}
    622  1.20  mycroft 			printf("SMART supported, SMART %s\n", status);
    623  1.15    soren 		}
    624  1.15    soren 	}
    625  1.15    soren 	return retval;
    626  1.15    soren }
    627  1.15    soren 
    628  1.15    soren /*
    629   1.1     kenh  * DEVICE COMMANDS
    630   1.1     kenh  */
    631   1.1     kenh 
    632   1.1     kenh /*
    633   1.1     kenh  * device_identify:
    634   1.1     kenh  *
    635   1.1     kenh  *	Display the identity of the device
    636   1.1     kenh  */
    637   1.1     kenh void
    638  1.13   simonb device_identify(int argc, char *argv[])
    639   1.1     kenh {
    640   1.1     kenh 	struct ataparams *inqbuf;
    641   1.1     kenh 	struct atareq req;
    642   1.1     kenh 	unsigned char inbuf[DEV_BSIZE];
    643   1.2     kenh #if BYTE_ORDER == LITTLE_ENDIAN
    644   1.1     kenh 	int i;
    645   1.1     kenh 	u_int16_t *p;
    646   1.1     kenh #endif
    647   1.1     kenh 
    648   1.1     kenh 	/* No arguments. */
    649   1.1     kenh 	if (argc != 0)
    650   1.5    soren 		usage();
    651   1.1     kenh 
    652   1.1     kenh 	memset(&inbuf, 0, sizeof(inbuf));
    653   1.1     kenh 	memset(&req, 0, sizeof(req));
    654   1.1     kenh 
    655   1.1     kenh 	inqbuf = (struct ataparams *) inbuf;
    656   1.1     kenh 
    657   1.1     kenh 	req.flags = ATACMD_READ;
    658   1.1     kenh 	req.command = WDCC_IDENTIFY;
    659   1.1     kenh 	req.databuf = (caddr_t) inbuf;
    660   1.1     kenh 	req.datalen = sizeof(inbuf);
    661   1.1     kenh 	req.timeout = 1000;
    662   1.1     kenh 
    663   1.1     kenh 	ata_command(&req);
    664   1.1     kenh 
    665   1.1     kenh #if BYTE_ORDER == LITTLE_ENDIAN
    666   1.1     kenh 	/*
    667   1.1     kenh 	 * On little endian machines, we need to shuffle the string
    668   1.1     kenh 	 * byte order.  However, we don't have to do this for NEC or
    669   1.1     kenh 	 * Mitsumi ATAPI devices
    670   1.1     kenh 	 */
    671   1.1     kenh 
    672   1.1     kenh 	if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
    673   1.1     kenh 	      ((inqbuf->atap_model[0] == 'N' &&
    674   1.1     kenh 		  inqbuf->atap_model[1] == 'E') ||
    675   1.1     kenh 	       (inqbuf->atap_model[0] == 'F' &&
    676   1.1     kenh 		  inqbuf->atap_model[1] == 'X')))) {
    677   1.1     kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_model); i += 2) {
    678   1.1     kenh 			p = (u_short *) (inqbuf->atap_model + i);
    679   1.1     kenh 			*p = ntohs(*p);
    680   1.1     kenh 		}
    681   1.1     kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_serial); i += 2) {
    682   1.1     kenh 			p = (u_short *) (inqbuf->atap_serial + i);
    683   1.1     kenh 			*p = ntohs(*p);
    684   1.1     kenh 		}
    685   1.1     kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_revision); i += 2) {
    686   1.1     kenh 			p = (u_short *) (inqbuf->atap_revision + i);
    687   1.1     kenh 			*p = ntohs(*p);
    688   1.1     kenh 		}
    689   1.1     kenh 	}
    690   1.1     kenh #endif
    691   1.1     kenh 
    692   1.1     kenh 	/*
    693   1.1     kenh 	 * Strip blanks off of the info strings.  Yuck, I wish this was
    694   1.1     kenh 	 * cleaner.
    695   1.1     kenh 	 */
    696   1.1     kenh 
    697   1.1     kenh 	if (inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] == ' ') {
    698   1.1     kenh 		inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] = '\0';
    699   1.1     kenh 		while (inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] == ' ')
    700   1.1     kenh 			inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] = '\0';
    701   1.1     kenh 	}
    702   1.1     kenh 
    703   1.1     kenh 	if (inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] == ' ') {
    704   1.1     kenh 		inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] = '\0';
    705   1.1     kenh 		while (inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] == ' ')
    706   1.1     kenh 			inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] = '\0';
    707   1.1     kenh 	}
    708   1.1     kenh 
    709   1.1     kenh 	if (inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] == ' ') {
    710   1.1     kenh 		inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] = '\0';
    711   1.1     kenh 		while (inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] == ' ')
    712   1.1     kenh 			inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] = '\0';
    713   1.1     kenh 	}
    714   1.1     kenh 
    715   1.1     kenh 	printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
    716   1.1     kenh 	       (int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
    717   1.1     kenh 	       (int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
    718   1.1     kenh 	       (int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
    719   1.1     kenh 
    720   1.1     kenh 	printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
    721   1.1     kenh 	       "ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
    722   1.1     kenh 	       "removable");
    723   1.1     kenh 
    724   1.1     kenh 	if ((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == 0)
    725   1.1     kenh 		printf("Cylinders: %d, heads: %d, sec/track: %d, total "
    726   1.1     kenh 		       "sectors: %d\n", inqbuf->atap_cylinders,
    727   1.1     kenh 		       inqbuf->atap_heads, inqbuf->atap_sectors,
    728   1.1     kenh 		       (inqbuf->atap_capacity[1] << 16) |
    729   1.1     kenh 		       inqbuf->atap_capacity[0]);
    730   1.1     kenh 
    731   1.1     kenh 	if (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK)
    732   1.1     kenh 		printf("Device supports command queue depth of %d\n",
    733   1.1     kenh 		       inqbuf->atap_queuedepth & 0xf);
    734   1.1     kenh 
    735   1.1     kenh 	printf("Device capabilities:\n");
    736  1.10       is 	print_bitinfo("\t", "\n", inqbuf->atap_capabilities1, ata_caps);
    737   1.1     kenh 
    738   1.1     kenh 	if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
    739   1.1     kenh 		printf("Device supports following standards:\n");
    740  1.10       is 		print_bitinfo("", " ", inqbuf->atap_ata_major, ata_vers);
    741   1.1     kenh 		printf("\n");
    742   1.1     kenh 	}
    743   1.1     kenh 
    744   1.1     kenh 	if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
    745   1.1     kenh 	    inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
    746   1.1     kenh 		printf("Command set support:\n");
    747  1.10       is 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set1, ata_cmd_set1);
    748  1.10       is 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set2, ata_cmd_set2);
    749  1.23     yamt 		if (inqbuf->atap_cmd_ext != 0 && inqbuf->atap_cmd_ext != 0xffff)
    750  1.23     yamt 			print_bitinfo("\t", "\n", inqbuf->atap_cmd_ext,
    751  1.23     yamt 			    ata_cmd_ext);
    752   1.1     kenh 	}
    753   1.1     kenh 
    754   1.1     kenh 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    755   1.1     kenh 		printf("Command sets/features enabled:\n");
    756  1.14   simonb 		print_bitinfo("\t", "\n", inqbuf->atap_cmd1_en &
    757   1.1     kenh 			      (WDC_CMD1_SRV | WDC_CMD1_RLSE | WDC_CMD1_AHEAD |
    758   1.1     kenh 			       WDC_CMD1_CACHE | WDC_CMD1_SEC | WDC_CMD1_SMART),
    759   1.1     kenh 			       ata_cmd_set1);
    760  1.14   simonb 		print_bitinfo("\t", "\n", inqbuf->atap_cmd2_en &
    761   1.1     kenh 			      (WDC_CMD2_RMSN | ATA_CMD2_APM), ata_cmd_set2);
    762   1.1     kenh 	}
    763   1.1     kenh 
    764   1.1     kenh 	return;
    765   1.1     kenh }
    766   1.1     kenh 
    767   1.1     kenh /*
    768   1.1     kenh  * device idle:
    769   1.1     kenh  *
    770   1.1     kenh  * issue the IDLE IMMEDIATE command to the drive
    771   1.1     kenh  */
    772   1.1     kenh 
    773   1.1     kenh void
    774  1.13   simonb device_idle(int argc, char *argv[])
    775   1.1     kenh {
    776   1.1     kenh 	struct atareq req;
    777   1.1     kenh 
    778   1.1     kenh 	/* No arguments. */
    779   1.1     kenh 	if (argc != 0)
    780   1.5    soren 		usage();
    781   1.1     kenh 
    782   1.1     kenh 	memset(&req, 0, sizeof(req));
    783   1.1     kenh 
    784   1.1     kenh 	if (strcmp(cmdname, "idle") == 0)
    785   1.1     kenh 		req.command = WDCC_IDLE_IMMED;
    786   1.1     kenh 	else if (strcmp(cmdname, "standby") == 0)
    787   1.1     kenh 		req.command = WDCC_STANDBY_IMMED;
    788   1.1     kenh 	else
    789   1.1     kenh 		req.command = WDCC_SLEEP;
    790   1.1     kenh 
    791   1.1     kenh 	req.timeout = 1000;
    792   1.1     kenh 
    793   1.1     kenh 	ata_command(&req);
    794   1.1     kenh 
    795   1.1     kenh 	return;
    796   1.1     kenh }
    797   1.1     kenh 
    798   1.1     kenh /*
    799   1.1     kenh  * Set the idle timer on the disk.  Set it for either idle mode or
    800   1.1     kenh  * standby mode, depending on how we were invoked.
    801   1.1     kenh  */
    802   1.1     kenh 
    803   1.1     kenh void
    804  1.13   simonb device_setidle(int argc, char *argv[])
    805   1.1     kenh {
    806   1.1     kenh 	unsigned long idle;
    807   1.1     kenh 	struct atareq req;
    808   1.1     kenh 	char *end;
    809   1.1     kenh 
    810   1.1     kenh 	/* Only one argument */
    811   1.1     kenh 	if (argc != 1)
    812   1.5    soren 		usage();
    813   1.1     kenh 
    814   1.1     kenh 	idle = strtoul(argv[0], &end, 0);
    815   1.1     kenh 
    816   1.1     kenh 	if (*end != '\0') {
    817   1.1     kenh 		fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
    818   1.1     kenh 		exit(1);
    819   1.1     kenh 	}
    820   1.1     kenh 
    821   1.1     kenh 	if (idle > 19800) {
    822   1.1     kenh 		fprintf(stderr, "Idle time has a maximum value of 5.5 "
    823   1.1     kenh 			"hours\n");
    824   1.1     kenh 		exit(1);
    825   1.1     kenh 	}
    826   1.1     kenh 
    827   1.1     kenh 	if (idle != 0 && idle < 5) {
    828   1.1     kenh 		fprintf(stderr, "Idle timer must be at least 5 seconds\n");
    829   1.1     kenh 		exit(1);
    830   1.1     kenh 	}
    831   1.1     kenh 
    832   1.1     kenh 	memset(&req, 0, sizeof(req));
    833   1.1     kenh 
    834   1.1     kenh 	if (idle <= 240*5)
    835   1.1     kenh 		req.sec_count = idle / 5;
    836   1.1     kenh 	else
    837   1.1     kenh 		req.sec_count = idle / (30*60) + 240;
    838   1.1     kenh 
    839   1.1     kenh 	req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
    840   1.1     kenh 	req.timeout = 1000;
    841   1.1     kenh 
    842   1.1     kenh 	ata_command(&req);
    843   1.1     kenh 
    844   1.1     kenh 	return;
    845   1.3     kenh }
    846   1.3     kenh 
    847   1.3     kenh /*
    848   1.3     kenh  * Query the device for the current power mode
    849   1.3     kenh  */
    850   1.3     kenh 
    851   1.3     kenh void
    852  1.13   simonb device_checkpower(int argc, char *argv[])
    853   1.3     kenh {
    854   1.3     kenh 	struct atareq req;
    855   1.3     kenh 
    856   1.3     kenh 	/* No arguments. */
    857   1.3     kenh 	if (argc != 0)
    858   1.5    soren 		usage();
    859   1.3     kenh 
    860   1.3     kenh 	memset(&req, 0, sizeof(req));
    861   1.3     kenh 
    862   1.3     kenh 	req.command = WDCC_CHECK_PWR;
    863   1.3     kenh 	req.timeout = 1000;
    864   1.3     kenh 	req.flags = ATACMD_READREG;
    865   1.3     kenh 
    866   1.3     kenh 	ata_command(&req);
    867   1.3     kenh 
    868   1.3     kenh 	printf("Current power status: ");
    869   1.3     kenh 
    870   1.3     kenh 	switch (req.sec_count) {
    871   1.3     kenh 	case 0x00:
    872   1.3     kenh 		printf("Standby mode\n");
    873   1.3     kenh 		break;
    874   1.3     kenh 	case 0x80:
    875   1.3     kenh 		printf("Idle mode\n");
    876   1.3     kenh 		break;
    877   1.3     kenh 	case 0xff:
    878   1.3     kenh 		printf("Active mode\n");
    879   1.3     kenh 		break;
    880   1.3     kenh 	default:
    881   1.3     kenh 		printf("Unknown power code (%02x)\n", req.sec_count);
    882   1.3     kenh 	}
    883   1.3     kenh 
    884  1.15    soren 	return;
    885  1.15    soren }
    886  1.15    soren 
    887  1.15    soren /*
    888  1.15    soren  * device_smart:
    889  1.15    soren  *
    890  1.15    soren  *	Display SMART status
    891  1.15    soren  */
    892  1.15    soren void
    893  1.15    soren device_smart(int argc, char *argv[])
    894  1.15    soren {
    895  1.15    soren 	struct atareq req;
    896  1.15    soren 	unsigned char inbuf[DEV_BSIZE];
    897  1.15    soren 	unsigned char inbuf2[DEV_BSIZE];
    898  1.15    soren 
    899  1.15    soren 	/* Only one argument */
    900  1.15    soren 	if (argc != 1)
    901  1.15    soren 		usage();
    902  1.15    soren 
    903  1.15    soren 	if (strcmp(argv[0], "enable") == 0) {
    904  1.20  mycroft 		memset(&req, 0, sizeof(req));
    905  1.15    soren 
    906  1.20  mycroft 		req.features = WDSM_ENABLE_OPS;
    907  1.20  mycroft 		req.command = WDCC_SMART;
    908  1.20  mycroft 		req.cylinder = htole16(WDSMART_CYL);
    909  1.20  mycroft 		req.timeout = 1000;
    910  1.15    soren 
    911  1.20  mycroft 		ata_command(&req);
    912  1.15    soren 
    913  1.20  mycroft 		is_smart();
    914  1.15    soren 	} else if (strcmp(argv[0], "disable") == 0) {
    915  1.20  mycroft 		memset(&req, 0, sizeof(req));
    916  1.15    soren 
    917  1.20  mycroft 		req.features = WDSM_DISABLE_OPS;
    918  1.20  mycroft 		req.command = WDCC_SMART;
    919  1.20  mycroft 		req.cylinder = htole16(WDSMART_CYL);
    920  1.20  mycroft 		req.timeout = 1000;
    921  1.15    soren 
    922  1.20  mycroft 		ata_command(&req);
    923  1.15    soren 
    924  1.20  mycroft 		is_smart();
    925  1.16    soren 	} else if (strcmp(argv[0], "status") == 0) {
    926  1.24      lha 		if (!is_smart()) {
    927  1.24      lha 			fprintf(stderr, "SMART not supported\n");
    928  1.24      lha 			return;
    929  1.24      lha 		}
    930  1.24      lha 
    931  1.15    soren 			memset(&inbuf, 0, sizeof(inbuf));
    932  1.15    soren 			memset(&req, 0, sizeof(req));
    933  1.15    soren 
    934  1.15    soren 			req.features = WDSM_STATUS;
    935  1.15    soren 			req.command = WDCC_SMART;
    936  1.15    soren 			req.cylinder = htole16(WDSMART_CYL);
    937  1.15    soren 			req.timeout = 1000;
    938  1.15    soren 
    939  1.15    soren 			ata_command(&req);
    940  1.15    soren 
    941  1.15    soren 			if (req.cylinder != htole16(WDSMART_CYL)) {
    942  1.15    soren 				fprintf(stderr, "Threshold exceeds condition\n");
    943  1.15    soren 			}
    944  1.15    soren 
    945  1.15    soren 			/* WDSM_RD_DATA and WDSM_RD_THRESHOLDS are optional
    946  1.15    soren 			 * features, the following ata_command()'s may error
    947  1.15    soren 			 * and exit().
    948  1.15    soren 			 */
    949  1.15    soren 
    950  1.15    soren 			memset(&inbuf, 0, sizeof(inbuf));
    951  1.15    soren 			memset(&req, 0, sizeof(req));
    952  1.15    soren 
    953  1.15    soren 			req.flags = ATACMD_READ;
    954  1.15    soren 			req.features = WDSM_RD_DATA;
    955  1.15    soren 			req.command = WDCC_SMART;
    956  1.15    soren 			req.databuf = (caddr_t) inbuf;
    957  1.15    soren 			req.datalen = sizeof(inbuf);
    958  1.15    soren 			req.cylinder = htole16(WDSMART_CYL);
    959  1.15    soren 			req.timeout = 1000;
    960  1.15    soren 
    961  1.15    soren 			ata_command(&req);
    962  1.15    soren 
    963  1.15    soren 			memset(&inbuf2, 0, sizeof(inbuf2));
    964  1.15    soren 			memset(&req, 0, sizeof(req));
    965  1.15    soren 
    966  1.15    soren 			req.flags = ATACMD_READ;
    967  1.15    soren 			req.features = WDSM_RD_THRESHOLDS;
    968  1.15    soren 			req.command = WDCC_SMART;
    969  1.15    soren 			req.databuf = (caddr_t) inbuf2;
    970  1.15    soren 			req.datalen = sizeof(inbuf2);
    971  1.15    soren 			req.cylinder = htole16(WDSMART_CYL);
    972  1.15    soren 			req.timeout = 1000;
    973  1.15    soren 
    974  1.15    soren 			ata_command(&req);
    975  1.15    soren 
    976  1.15    soren 			print_smart_status(inbuf, inbuf2);
    977  1.24      lha 
    978  1.24      lha 	} else if (strcmp(argv[0], "selftest-log") == 0) {
    979  1.24      lha 		if (!is_smart()) {
    980  1.15    soren 			fprintf(stderr, "SMART not supported\n");
    981  1.24      lha 			return;
    982  1.15    soren 		}
    983  1.24      lha 
    984  1.24      lha 		memset(&inbuf, 0, sizeof(inbuf));
    985  1.24      lha 		memset(&req, 0, sizeof(req));
    986  1.24      lha 
    987  1.24      lha 		req.flags = ATACMD_READ;
    988  1.24      lha 		req.features = WDSM_RD_LOG;
    989  1.24      lha 		req.sec_count = 1;
    990  1.24      lha 		req.sec_num = 6;
    991  1.24      lha 		req.command = WDCC_SMART;
    992  1.24      lha 		req.databuf = (caddr_t) inbuf;
    993  1.24      lha 		req.datalen = sizeof(inbuf);
    994  1.24      lha 		req.cylinder = htole16(WDSMART_CYL);
    995  1.24      lha 		req.timeout = 1000;
    996  1.24      lha 
    997  1.24      lha 		ata_command(&req);
    998  1.24      lha 
    999  1.24      lha 		print_selftest(inbuf);
   1000  1.24      lha 
   1001  1.15    soren 	} else {
   1002  1.15    soren 		usage();
   1003  1.15    soren 	}
   1004   1.3     kenh 	return;
   1005   1.1     kenh }
   1006  1.30   bouyer 
   1007  1.30   bouyer /*
   1008  1.30   bouyer  * bus_reset:
   1009  1.30   bouyer  *	Reset an ATA bus (will reset all devices on the bus)
   1010  1.30   bouyer  */
   1011  1.30   bouyer void
   1012  1.30   bouyer bus_reset(int argc, char *argv[])
   1013  1.30   bouyer {
   1014  1.30   bouyer 	int error;
   1015  1.30   bouyer 
   1016  1.30   bouyer 	/* no args */
   1017  1.30   bouyer 	if (argc != 0)
   1018  1.30   bouyer 		usage();
   1019  1.30   bouyer 
   1020  1.30   bouyer 	error = ioctl(fd, ATABUSIORESET, NULL);
   1021  1.30   bouyer 
   1022  1.30   bouyer 	if (error == -1)
   1023  1.30   bouyer 		err(1, "ATABUSIORESET failed");
   1024  1.30   bouyer }
   1025