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