Home | History | Annotate | Line # | Download | only in atactl
atactl.c revision 1.41
      1  1.41       dbj /*	$NetBSD: atactl.c,v 1.41 2005/11/29 08:47:22 dbj 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.41       dbj __RCSID("$NetBSD: atactl.c,v 1.41 2005/11/29 08:47:22 dbj 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.33   mycroft struct ata_smart_error {
     64  1.33   mycroft 	struct {
     65  1.33   mycroft 		u_int8_t device_control;
     66  1.33   mycroft 		u_int8_t features;
     67  1.33   mycroft 		u_int8_t sector_count;
     68  1.33   mycroft 		u_int8_t sector_number;
     69  1.33   mycroft 		u_int8_t cylinder_low;
     70  1.33   mycroft 		u_int8_t cylinder_high;
     71  1.33   mycroft 		u_int8_t device_head;
     72  1.33   mycroft 		u_int8_t command;
     73  1.33   mycroft 		u_int8_t timestamp[4];
     74  1.33   mycroft 	} command[5];
     75  1.33   mycroft 	struct {
     76  1.33   mycroft 		u_int8_t reserved;
     77  1.33   mycroft 		u_int8_t error;
     78  1.33   mycroft 		u_int8_t sector_count;
     79  1.33   mycroft 		u_int8_t sector_number;
     80  1.33   mycroft 		u_int8_t cylinder_low;
     81  1.33   mycroft 		u_int8_t cylinder_high;
     82  1.33   mycroft 		u_int8_t device_head;
     83  1.33   mycroft 		u_int8_t status;
     84  1.33   mycroft 		u_int8_t extended_error[19];
     85  1.33   mycroft 		u_int8_t state;
     86  1.33   mycroft 		u_int8_t lifetime[2];
     87  1.33   mycroft 	} error_data;
     88  1.33   mycroft } __attribute__((packed));
     89  1.33   mycroft 
     90  1.33   mycroft struct ata_smart_errorlog {
     91  1.33   mycroft 	u_int8_t		data_structure_revision;
     92  1.33   mycroft 	u_int8_t		mostrecenterror;
     93  1.33   mycroft 	struct ata_smart_error	log_entries[5];
     94  1.33   mycroft 	u_int16_t		device_error_count;
     95  1.33   mycroft 	u_int8_t		reserved[57];
     96  1.33   mycroft 	u_int8_t		checksum;
     97  1.33   mycroft } __attribute__((packed));
     98  1.33   mycroft 
     99   1.1      kenh struct command {
    100   1.1      kenh 	const char *cmd_name;
    101   1.5     soren 	const char *arg_names;
    102  1.13    simonb 	void (*cmd_func)(int, char *[]);
    103   1.1      kenh };
    104   1.1      kenh 
    105   1.1      kenh struct bitinfo {
    106   1.1      kenh 	u_int bitmask;
    107   1.1      kenh 	const char *string;
    108   1.1      kenh };
    109   1.1      kenh 
    110  1.13    simonb void	usage(void);
    111  1.13    simonb void	ata_command(struct atareq *);
    112  1.13    simonb void	print_bitinfo(const char *, const char *, u_int, struct bitinfo *);
    113  1.33   mycroft void	print_bitinfo2(const char *, const char *, u_int, u_int, struct bitinfo *);
    114  1.24       lha void	print_smart_status(void *, void *);
    115  1.33   mycroft void	print_error_entry(int, struct ata_smart_error *);
    116  1.24       lha void	print_selftest_entry(int, struct ata_smart_selftest *);
    117  1.24       lha 
    118  1.33   mycroft void	print_error(void *);
    119  1.24       lha void	print_selftest(void *);
    120  1.24       lha 
    121  1.38  drochner struct ataparams *getataparams(void);
    122  1.38  drochner 
    123  1.20   mycroft int	is_smart(void);
    124   1.1      kenh 
    125   1.1      kenh int	fd;				/* file descriptor for device */
    126   1.1      kenh const	char *dvname;			/* device name */
    127   1.1      kenh char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
    128   1.1      kenh const	char *cmdname;			/* command user issued */
    129   1.5     soren const	char *argnames;			/* helpstring: expected arguments */
    130   1.1      kenh 
    131  1.13    simonb void	device_identify(int, char *[]);
    132  1.13    simonb void	device_setidle(int, char *[]);
    133  1.13    simonb void	device_idle(int, char *[]);
    134  1.13    simonb void	device_checkpower(int, char *[]);
    135  1.15     soren void	device_smart(int, char *[]);
    136  1.38  drochner void	device_security(int, char *[]);
    137   1.1      kenh 
    138  1.30    bouyer void	device_smart_temp(struct ata_smart_attr *, uint64_t);
    139  1.24       lha 
    140  1.30    bouyer struct command device_commands[] = {
    141   1.5     soren 	{ "identify",	"",			device_identify },
    142   1.5     soren 	{ "setidle",	"idle-timer",		device_setidle },
    143   1.5     soren 	{ "setstandby",	"standby-timer",	device_setidle },
    144   1.5     soren 	{ "idle",	"",			device_idle },
    145   1.5     soren 	{ "standby",	"",			device_idle },
    146   1.5     soren 	{ "sleep",	"",			device_idle },
    147   1.5     soren 	{ "checkpower",	"",			device_checkpower },
    148  1.34     soren 	{ "smart",	"enable|disable|status|offline #|error-log|selftest-log",
    149  1.34     soren 						device_smart },
    150  1.38  drochner 	{ "security",	"freeze|status",	device_security },
    151   1.5     soren 	{ NULL,		NULL,			NULL },
    152   1.1      kenh };
    153   1.1      kenh 
    154  1.37   xtraeme void	bus_reset(int, char *[]);
    155  1.30    bouyer 
    156  1.30    bouyer struct command bus_commands[] = {
    157  1.30    bouyer 	{ "reset",	"",			bus_reset },
    158  1.30    bouyer 	{ NULL,		NULL,			NULL },
    159  1.30    bouyer };
    160  1.30    bouyer 
    161   1.1      kenh /*
    162   1.1      kenh  * Tables containing bitmasks used for error reporting and
    163   1.1      kenh  * device identification.
    164   1.1      kenh  */
    165   1.1      kenh 
    166   1.1      kenh struct bitinfo ata_caps[] = {
    167  1.23      yamt 	{ WDC_CAP_DMA, "DMA" },
    168  1.23      yamt 	{ WDC_CAP_LBA, "LBA" },
    169   1.1      kenh 	{ ATA_CAP_STBY, "ATA standby timer values" },
    170   1.1      kenh 	{ WDC_CAP_IORDY, "IORDY operation" },
    171   1.1      kenh 	{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
    172  1.22      fvdl 	{ 0, NULL },
    173   1.1      kenh };
    174   1.1      kenh 
    175   1.1      kenh struct bitinfo ata_vers[] = {
    176   1.1      kenh 	{ WDC_VER_ATA1,	"ATA-1" },
    177   1.1      kenh 	{ WDC_VER_ATA2,	"ATA-2" },
    178   1.1      kenh 	{ WDC_VER_ATA3,	"ATA-3" },
    179   1.1      kenh 	{ WDC_VER_ATA4,	"ATA-4" },
    180  1.23      yamt 	{ WDC_VER_ATA5,	"ATA-5" },
    181  1.23      yamt 	{ WDC_VER_ATA6,	"ATA-6" },
    182  1.23      yamt 	{ WDC_VER_ATA7,	"ATA-7" },
    183  1.22      fvdl 	{ 0, NULL },
    184   1.1      kenh };
    185   1.1      kenh 
    186   1.1      kenh struct bitinfo ata_cmd_set1[] = {
    187   1.1      kenh 	{ WDC_CMD1_NOP, "NOP command" },
    188   1.1      kenh 	{ WDC_CMD1_RB, "READ BUFFER command" },
    189   1.1      kenh 	{ WDC_CMD1_WB, "WRITE BUFFER command" },
    190   1.1      kenh 	{ WDC_CMD1_HPA, "Host Protected Area feature set" },
    191   1.1      kenh 	{ WDC_CMD1_DVRST, "DEVICE RESET command" },
    192   1.1      kenh 	{ WDC_CMD1_SRV, "SERVICE interrupt" },
    193   1.1      kenh 	{ WDC_CMD1_RLSE, "release interrupt" },
    194   1.1      kenh 	{ WDC_CMD1_AHEAD, "look-ahead" },
    195   1.1      kenh 	{ WDC_CMD1_CACHE, "write cache" },
    196   1.1      kenh 	{ WDC_CMD1_PKT, "PACKET command feature set" },
    197   1.1      kenh 	{ WDC_CMD1_PM, "Power Management feature set" },
    198   1.1      kenh 	{ WDC_CMD1_REMOV, "Removable Media feature set" },
    199   1.1      kenh 	{ WDC_CMD1_SEC, "Security Mode feature set" },
    200   1.1      kenh 	{ WDC_CMD1_SMART, "SMART feature set" },
    201  1.22      fvdl 	{ 0, NULL },
    202   1.1      kenh };
    203   1.1      kenh 
    204   1.1      kenh struct bitinfo ata_cmd_set2[] = {
    205  1.23      yamt 	{ ATA_CMD2_FCE, "FLUSH CACHE EXT command" },
    206  1.23      yamt 	{ WDC_CMD2_FC, "FLUSH CACHE command" },
    207  1.23      yamt 	{ WDC_CMD2_DCO, "Device Configuration Overlay feature set" },
    208  1.23      yamt 	{ ATA_CMD2_LBA48, "48-bit Address feature set" },
    209  1.23      yamt 	{ WDC_CMD2_AAM, "Automatic Acoustic Management feature set" },
    210  1.28       wiz 	{ WDC_CMD2_SM, "SET MAX security extension" },
    211  1.23      yamt 	{ WDC_CMD2_SFREQ, "SET FEATURES required to spin-up after power-up" },
    212  1.23      yamt 	{ WDC_CMD2_PUIS, "Power-Up In Standby feature set" },
    213   1.1      kenh 	{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
    214   1.1      kenh 	{ ATA_CMD2_APM, "Advanced Power Management feature set" },
    215   1.1      kenh 	{ ATA_CMD2_CFA, "CFA feature set" },
    216   1.6     soren 	{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
    217   1.1      kenh 	{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
    218  1.22      fvdl 	{ 0, NULL },
    219   1.1      kenh };
    220   1.1      kenh 
    221  1.23      yamt struct bitinfo ata_cmd_ext[] = {
    222  1.23      yamt 	{ ATA_CMDE_TLCONT, "Time-limited R/W feature set R/W Continuous mode" },
    223  1.23      yamt 	{ ATA_CMDE_TL, "Time-limited Read/Write" },
    224  1.23      yamt 	{ ATA_CMDE_URGW, "URG bit for WRITE STREAM DMA/PIO" },
    225  1.23      yamt 	{ ATA_CMDE_URGR, "URG bit for READ STREAM DMA/PIO" },
    226  1.23      yamt 	{ ATA_CMDE_WWN, "World Wide name" },
    227  1.23      yamt 	{ ATA_CMDE_WQFE, "WRITE DMA QUEUED FUA EXT command" },
    228  1.23      yamt 	{ ATA_CMDE_WFE, "WRITE DMA/MULTIPLE FUA EXT commands" },
    229  1.23      yamt 	{ ATA_CMDE_GPL, "General Purpose Logging feature set" },
    230  1.23      yamt 	{ ATA_CMDE_STREAM, "Streaming feature set" },
    231  1.23      yamt 	{ ATA_CMDE_MCPTC, "Media Card Pass Through Command feature set" },
    232  1.23      yamt 	{ ATA_CMDE_MS, "Media serial number" },
    233  1.23      yamt 	{ ATA_CMDE_SST, "SMART self-test" },
    234  1.23      yamt 	{ ATA_CMDE_SEL, "SMART error logging" },
    235  1.23      yamt 	{ 0, NULL },
    236  1.23      yamt };
    237  1.23      yamt 
    238  1.17     soren static const struct {
    239  1.17     soren 	const int	id;
    240  1.17     soren 	const char	*name;
    241  1.29   mycroft 	void (*special)(struct ata_smart_attr *, uint64_t);
    242  1.17     soren } smart_attrs[] = {
    243  1.32    atatat 	{   1,		"Raw read error rate" },
    244  1.32    atatat 	{   2,		"Throughput performance" },
    245  1.32    atatat 	{   3,		"Spin-up time" },
    246  1.32    atatat 	{   4,		"Start/stop count" },
    247  1.32    atatat 	{   5,		"Reallocated sector count" },
    248  1.36    dogcow 	{   6,		"Read channel margin" },
    249  1.32    atatat 	{   7,		"Seek error rate" },
    250  1.32    atatat 	{   8,		"Seek time performance" },
    251  1.32    atatat 	{   9,		"Power-on hours count" },
    252  1.32    atatat 	{  10,		"Spin retry count" },
    253  1.32    atatat 	{  11,		"Calibration retry count" },
    254  1.32    atatat 	{  12,		"Device power cycle count" },
    255  1.17     soren 	{ 191,		"Gsense error rate" },
    256  1.17     soren 	{ 192,		"Power-off retract count" },
    257  1.17     soren 	{ 193,		"Load cycle count" },
    258  1.30    bouyer 	{ 194,		"Temperature",			device_smart_temp},
    259  1.24       lha 	{ 195,		"Hardware ECC Recovered" },
    260  1.17     soren 	{ 196,		"Reallocated event count" },
    261  1.17     soren 	{ 197,		"Current pending sector" },
    262  1.17     soren 	{ 198,		"Offline uncorrectable" },
    263  1.17     soren 	{ 199,		"Ultra DMA CRC error count" },
    264  1.32    atatat 	{ 200,		"Write error rate" },
    265  1.32    atatat 	{ 201,		"Soft read error rate" },
    266  1.32    atatat 	{ 202,		"Data address mark errors" },
    267  1.32    atatat 	{ 203,		"Run out cancel" },
    268  1.32    atatat 	{ 204,		"Soft ECC correction" },
    269  1.32    atatat 	{ 205,		"Thermal asperity check" },
    270  1.32    atatat 	{ 206,		"Flying height" },
    271  1.32    atatat 	{ 207,		"Spin high current" },
    272  1.32    atatat 	{ 208,		"Spin buzz" },
    273  1.32    atatat 	{ 209,		"Offline seek performance" },
    274  1.32    atatat 	{ 220,		"Disk shift" },
    275  1.32    atatat 	{ 221,		"G-Sense error rate" },
    276  1.32    atatat 	{ 222,		"Loaded hours" },
    277  1.32    atatat 	{ 223,		"Load/unload retry count" },
    278  1.32    atatat 	{ 224,		"Load friction" },
    279  1.32    atatat 	{ 225,		"Load/unload cycle count" },
    280  1.32    atatat 	{ 226,		"Load-in time" },
    281  1.32    atatat 	{ 227,		"Torque amplification count" },
    282  1.32    atatat 	{ 228,		"Power-off retract count" },
    283  1.32    atatat 	{ 230,		"GMR head amplitude" },
    284  1.32    atatat 	{ 231,		"Temperature",			device_smart_temp },
    285  1.32    atatat 	{ 240,		"Head flying hours" },
    286  1.32    atatat 	{ 250,		"Read error retry rate" },
    287  1.32    atatat 	{   0,		"Unknown" },
    288  1.17     soren };
    289  1.17     soren 
    290  1.38  drochner struct bitinfo ata_sec_st[] = {
    291  1.38  drochner 	{ WDC_SEC_SUPP,		"supported" },
    292  1.38  drochner 	{ WDC_SEC_EN,		"enabled" },
    293  1.38  drochner 	{ WDC_SEC_LOCKED,	"locked" },
    294  1.38  drochner 	{ WDC_SEC_FROZEN,	"frozen" },
    295  1.38  drochner 	{ WDC_SEC_EXP,		"expired" },
    296  1.38  drochner 	{ WDC_SEC_ESE_SUPP,	"enhanced erase support" },
    297  1.38  drochner 	{ WDC_SEC_LEV_MAX,	"maximum level" },
    298  1.38  drochner 	{ 0,			NULL },
    299  1.38  drochner };
    300  1.38  drochner 
    301   1.1      kenh int
    302  1.13    simonb main(int argc, char *argv[])
    303   1.1      kenh {
    304   1.1      kenh 	int i;
    305  1.30    bouyer 	struct command *commands = NULL;
    306   1.1      kenh 
    307   1.1      kenh 	/* Must have at least: device command */
    308   1.1      kenh 	if (argc < 3)
    309   1.1      kenh 		usage();
    310   1.1      kenh 
    311   1.1      kenh 	/* Skip program name, get and skip device name and command. */
    312   1.1      kenh 	dvname = argv[1];
    313   1.1      kenh 	cmdname = argv[2];
    314   1.1      kenh 	argv += 3;
    315   1.1      kenh 	argc -= 3;
    316   1.1      kenh 
    317   1.1      kenh 	/*
    318   1.1      kenh 	 * Open the device
    319   1.1      kenh 	 */
    320   1.1      kenh 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    321   1.1      kenh 	if (fd == -1) {
    322   1.1      kenh 		if (errno == ENOENT) {
    323   1.1      kenh 			/*
    324   1.1      kenh 			 * Device doesn't exist.  Probably trying to open
    325   1.1      kenh 			 * a device which doesn't use disk semantics for
    326   1.1      kenh 			 * device name.  Try again, specifying "cooked",
    327   1.1      kenh 			 * which leaves off the "r" in front of the device's
    328   1.1      kenh 			 * name.
    329   1.1      kenh 			 */
    330   1.1      kenh 			fd = opendisk(dvname, O_RDWR, dvname_store,
    331   1.1      kenh 			    sizeof(dvname_store), 1);
    332   1.1      kenh 			if (fd == -1)
    333   1.1      kenh 				err(1, "%s", dvname);
    334   1.4     jwise 		} else
    335   1.4     jwise 			err(1, "%s", dvname);
    336   1.1      kenh 	}
    337   1.1      kenh 
    338   1.1      kenh 	/*
    339   1.1      kenh 	 * Point the dvname at the actual device name that opendisk() opened.
    340   1.1      kenh 	 */
    341   1.1      kenh 	dvname = dvname_store;
    342   1.1      kenh 
    343   1.1      kenh 	/* Look up and call the command. */
    344  1.30    bouyer 	for (i = 0; device_commands[i].cmd_name != NULL; i++) {
    345  1.30    bouyer 		if (strcmp(cmdname, device_commands[i].cmd_name) == 0) {
    346  1.30    bouyer 			commands = &device_commands[i];
    347   1.1      kenh 			break;
    348  1.30    bouyer 		}
    349  1.30    bouyer 	}
    350  1.30    bouyer 	if (commands == NULL) {
    351  1.30    bouyer 		for (i = 0; bus_commands[i].cmd_name != NULL; i++) {
    352  1.30    bouyer 			if (strcmp(cmdname, bus_commands[i].cmd_name) == 0) {
    353  1.30    bouyer 				commands = &bus_commands[i];
    354  1.30    bouyer 				break;
    355  1.30    bouyer 			}
    356  1.30    bouyer 		}
    357  1.30    bouyer 	}
    358  1.30    bouyer 	if (commands == NULL)
    359  1.12        ad 		errx(1, "unknown command: %s", cmdname);
    360   1.1      kenh 
    361  1.30    bouyer 	argnames = commands->arg_names;
    362   1.5     soren 
    363  1.30    bouyer 	(*commands->cmd_func)(argc, argv);
    364   1.1      kenh 	exit(0);
    365   1.1      kenh }
    366   1.1      kenh 
    367   1.1      kenh void
    368  1.13    simonb usage(void)
    369   1.1      kenh {
    370   1.5     soren 	int i;
    371   1.1      kenh 
    372  1.27      jmmv 	fprintf(stderr, "usage: %s device command [arg [...]]\n",
    373  1.11       cgd 	    getprogname());
    374   1.5     soren 
    375   1.5     soren 	fprintf(stderr, "   Available device commands:\n");
    376  1.30    bouyer 	for (i=0; device_commands[i].cmd_name != NULL; i++)
    377  1.30    bouyer 		fprintf(stderr, "\t%s %s\n", device_commands[i].cmd_name,
    378  1.30    bouyer 					    device_commands[i].arg_names);
    379  1.30    bouyer 
    380  1.30    bouyer 	fprintf(stderr, "   Available bus commands:\n");
    381  1.30    bouyer 	for (i=0; bus_commands[i].cmd_name != NULL; i++)
    382  1.30    bouyer 		fprintf(stderr, "\t%s %s\n", bus_commands[i].cmd_name,
    383  1.30    bouyer 					    bus_commands[i].arg_names);
    384   1.5     soren 
    385   1.1      kenh 	exit(1);
    386   1.1      kenh }
    387   1.1      kenh 
    388   1.1      kenh /*
    389   1.1      kenh  * Wrapper that calls ATAIOCCOMMAND and checks for errors
    390   1.1      kenh  */
    391   1.1      kenh 
    392   1.1      kenh void
    393  1.13    simonb ata_command(struct atareq *req)
    394   1.1      kenh {
    395   1.1      kenh 	int error;
    396   1.1      kenh 
    397   1.1      kenh 	error = ioctl(fd, ATAIOCCOMMAND, req);
    398   1.1      kenh 
    399   1.1      kenh 	if (error == -1)
    400   1.1      kenh 		err(1, "ATAIOCCOMMAND failed");
    401   1.1      kenh 
    402   1.1      kenh 	switch (req->retsts) {
    403   1.1      kenh 
    404   1.1      kenh 	case ATACMD_OK:
    405   1.1      kenh 		return;
    406   1.1      kenh 	case ATACMD_TIMEOUT:
    407   1.1      kenh 		fprintf(stderr, "ATA command timed out\n");
    408   1.1      kenh 		exit(1);
    409   1.1      kenh 	case ATACMD_DF:
    410   1.1      kenh 		fprintf(stderr, "ATA device returned a Device Fault\n");
    411   1.1      kenh 		exit(1);
    412   1.1      kenh 	case ATACMD_ERROR:
    413   1.1      kenh 		if (req->error & WDCE_ABRT)
    414   1.1      kenh 			fprintf(stderr, "ATA device returned Aborted "
    415   1.1      kenh 				"Command\n");
    416   1.1      kenh 		else
    417   1.1      kenh 			fprintf(stderr, "ATA device returned error register "
    418   1.1      kenh 				"%0x\n", req->error);
    419   1.1      kenh 		exit(1);
    420   1.1      kenh 	default:
    421   1.1      kenh 		fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
    422   1.1      kenh 			"%d\n", req->retsts);
    423   1.1      kenh 		exit(1);
    424   1.1      kenh 	}
    425   1.1      kenh }
    426   1.1      kenh 
    427   1.1      kenh /*
    428   1.1      kenh  * Print out strings associated with particular bitmasks
    429   1.1      kenh  */
    430   1.1      kenh 
    431   1.1      kenh void
    432  1.13    simonb print_bitinfo(const char *bf, const char *af, u_int bits, struct bitinfo *binfo)
    433   1.1      kenh {
    434   1.1      kenh 
    435  1.22      fvdl 	for (; binfo->bitmask != 0; binfo++)
    436   1.1      kenh 		if (bits & binfo->bitmask)
    437  1.10        is 			printf("%s%s%s", bf, binfo->string, af);
    438   1.1      kenh }
    439   1.1      kenh 
    440  1.33   mycroft void
    441  1.33   mycroft print_bitinfo2(const char *bf, const char *af, u_int bits, u_int enables, struct bitinfo *binfo)
    442  1.33   mycroft {
    443  1.33   mycroft 
    444  1.33   mycroft 	for (; binfo->bitmask != 0; binfo++)
    445  1.33   mycroft 		if (bits & binfo->bitmask)
    446  1.33   mycroft 			printf("%s%s (%s)%s", bf, binfo->string,
    447  1.33   mycroft 			    (enables & binfo->bitmask) ? "enabled" : "disabled",
    448  1.33   mycroft 			    af);
    449  1.33   mycroft }
    450  1.33   mycroft 
    451  1.24       lha 
    452  1.24       lha /*
    453  1.24       lha  * Try to print SMART temperature field
    454  1.24       lha  */
    455  1.24       lha 
    456  1.24       lha void
    457  1.30    bouyer device_smart_temp(struct ata_smart_attr *attr, uint64_t raw_value)
    458  1.24       lha {
    459  1.29   mycroft 	printf("%" PRIu8, attr->raw[0]);
    460  1.24       lha 	if (attr->raw[0] != raw_value)
    461  1.29   mycroft 		printf(" Lifetime max/min %" PRIu8 "/%" PRIu8,
    462  1.29   mycroft 		    attr->raw[2], attr->raw[4]);
    463  1.24       lha }
    464  1.24       lha 
    465  1.24       lha 
    466   1.1      kenh /*
    467  1.15     soren  * Print out SMART attribute thresholds and values
    468  1.15     soren  */
    469  1.15     soren 
    470  1.15     soren void
    471  1.15     soren print_smart_status(void *vbuf, void *tbuf)
    472  1.15     soren {
    473  1.15     soren 	struct ata_smart_attributes *value_buf = vbuf;
    474  1.15     soren 	struct ata_smart_thresholds *threshold_buf = tbuf;
    475  1.24       lha 	struct ata_smart_attr *attr;
    476  1.29   mycroft 	uint64_t raw_value;
    477  1.24       lha 	int flags;
    478  1.17     soren 	int i, j;
    479  1.24       lha 	int aid;
    480  1.33   mycroft 	u_int8_t checksum;
    481  1.15     soren 
    482  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    483  1.33   mycroft 		checksum += ((u_int8_t *) value_buf)[i];
    484  1.33   mycroft 	if (checksum != 0) {
    485  1.15     soren 		fprintf(stderr, "SMART attribute values checksum error\n");
    486  1.15     soren 		return;
    487  1.15     soren 	}
    488  1.15     soren 
    489  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    490  1.33   mycroft 		checksum += ((u_int8_t *) threshold_buf)[i];
    491  1.33   mycroft 	if (checksum != 0) {
    492  1.15     soren 		fprintf(stderr, "SMART attribute thresholds checksum error\n");
    493  1.15     soren 		return;
    494  1.15     soren 	}
    495  1.15     soren 
    496  1.24       lha 	printf("id value thresh crit collect reliability description\t\t\traw\n");
    497  1.24       lha 	for (i = 0; i < 256; i++) {
    498  1.24       lha 		int thresh = 0;
    499  1.24       lha 
    500  1.24       lha 		attr = NULL;
    501  1.24       lha 
    502  1.24       lha 		for (j = 0; j < 30; j++) {
    503  1.24       lha 			if (value_buf->attributes[j].id == i)
    504  1.24       lha 				attr = &value_buf->attributes[j];
    505  1.24       lha 			if (threshold_buf->thresholds[j].id == i)
    506  1.24       lha 				thresh = threshold_buf->thresholds[j].value;
    507  1.31    atatat 		}
    508  1.15     soren 
    509  1.24       lha 		if (thresh && attr == NULL)
    510  1.24       lha 			errx(1, "threshold but not attr %d", i);
    511  1.24       lha 		if (attr == NULL)
    512  1.24       lha 			continue;
    513  1.24       lha 
    514  1.24       lha 		if (attr->value == 0||attr->value == 0xFE||attr->value == 0xFF)
    515  1.24       lha 			continue;
    516  1.24       lha 
    517  1.24       lha 		for (aid = 0;
    518  1.24       lha 		     smart_attrs[aid].id != i && smart_attrs[aid].id != 0;
    519  1.24       lha 		     aid++)
    520  1.24       lha 			;
    521  1.24       lha 
    522  1.35      fvdl 		flags = le16toh(attr->flags);
    523  1.24       lha 
    524  1.29   mycroft 		printf("%3d %3d  %3d     %-3s %-7s %stive    %-24s\t",
    525  1.24       lha 		    i, attr->value, thresh,
    526  1.24       lha 		    flags & WDSM_ATTR_ADVISORY ? "yes" : "no",
    527  1.24       lha 		    flags & WDSM_ATTR_COLLECTIVE ? "online" : "offline",
    528  1.24       lha 		    attr->value > thresh ? "posi" : "nega",
    529  1.24       lha 		    smart_attrs[aid].name);
    530  1.24       lha 
    531  1.24       lha 		for (j = 0, raw_value = 0; j < 6; j++)
    532  1.29   mycroft 			raw_value += ((uint64_t)attr->raw[j]) << (8*j);
    533  1.24       lha 
    534  1.24       lha 		if (smart_attrs[aid].special)
    535  1.24       lha 			(*smart_attrs[aid].special)(attr, raw_value);
    536  1.29   mycroft 		else
    537  1.29   mycroft 			printf("%" PRIu64, raw_value);
    538  1.24       lha 		printf("\n");
    539  1.15     soren 		}
    540  1.15     soren 	}
    541  1.24       lha 
    542  1.24       lha struct {
    543  1.24       lha 	int number;
    544  1.24       lha 	const char *name;
    545  1.24       lha } selftest_name[] = {
    546  1.24       lha 	{ 0, "Off-line" },
    547  1.24       lha 	{ 1, "Short off-line" },
    548  1.24       lha 	{ 2, "Extended off-line" },
    549  1.24       lha 	{ 127, "Abort off-line test" },
    550  1.24       lha 	{ 129, "Short captive" },
    551  1.24       lha 	{ 130, "Extended captive" },
    552  1.24       lha 	{ 256, "Unknown test" }, /* larger then u_int8_t */
    553  1.24       lha 	{ 0, NULL }
    554  1.24       lha };
    555  1.24       lha 
    556  1.24       lha const char *selftest_status[] = {
    557  1.24       lha 	"No error",
    558  1.24       lha 	"Aborted by the host",
    559  1.24       lha 	"Interruped by the host by reset",
    560  1.24       lha 	"Fatal error or unknown test error",
    561  1.24       lha 	"Unknown test element failed",
    562  1.24       lha 	"Electrical test element failed",
    563  1.24       lha 	"The Servo (and/or seek) test element failed",
    564  1.24       lha 	"Read element of test failed",
    565  1.24       lha 	"Reserved",
    566  1.24       lha 	"Reserved",
    567  1.24       lha 	"Reserved",
    568  1.24       lha 	"Reserved",
    569  1.24       lha 	"Reserved",
    570  1.24       lha 	"Reserved",
    571  1.24       lha 	"Reserved",
    572  1.24       lha 	"Self-test in progress"
    573  1.24       lha };
    574  1.24       lha 
    575  1.24       lha void
    576  1.33   mycroft print_error_entry(int num, struct ata_smart_error *le)
    577  1.33   mycroft {
    578  1.33   mycroft 	int i;
    579  1.33   mycroft 
    580  1.33   mycroft 	printf("Log entry: %d\n", num);
    581  1.33   mycroft 
    582  1.33   mycroft 	for (i = 0; i < 5; i++)
    583  1.33   mycroft 		printf("\tCommand %d: dc=%02x sf=%02x sc=%02x sn=%02x cl=%02x ch=%02x dh=%02x cmd=%02x time=%02x%02x%02x%02x\n", i,
    584  1.33   mycroft 		    le->command[i].device_control,
    585  1.33   mycroft 		    le->command[i].features,
    586  1.33   mycroft 		    le->command[i].sector_count,
    587  1.33   mycroft 		    le->command[i].sector_number,
    588  1.33   mycroft 		    le->command[i].cylinder_low,
    589  1.33   mycroft 		    le->command[i].cylinder_high,
    590  1.33   mycroft 		    le->command[i].device_head,
    591  1.33   mycroft 		    le->command[i].command,
    592  1.33   mycroft 		    le->command[i].timestamp[3],
    593  1.33   mycroft 		    le->command[i].timestamp[2],
    594  1.33   mycroft 		    le->command[i].timestamp[1],
    595  1.33   mycroft 		    le->command[i].timestamp[0]);
    596  1.33   mycroft 	printf("\tError: err=%02x sc=%02x sn=%02x cl=%02x ch=%02x dh=%02x status=%02x state=%02x lifetime=%02x%02x\n",
    597  1.33   mycroft 	    le->error_data.error,
    598  1.33   mycroft 	    le->error_data.sector_count,
    599  1.33   mycroft 	    le->error_data.sector_number,
    600  1.33   mycroft 	    le->error_data.cylinder_low,
    601  1.33   mycroft 	    le->error_data.cylinder_high,
    602  1.33   mycroft 	    le->error_data.device_head,
    603  1.33   mycroft 	    le->error_data.status,
    604  1.33   mycroft 	    le->error_data.state,
    605  1.33   mycroft 	    le->error_data.lifetime[1],
    606  1.33   mycroft 	    le->error_data.lifetime[0]);
    607  1.33   mycroft 	printf("\tExtended: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    608  1.33   mycroft 	    le->error_data.extended_error[0],
    609  1.33   mycroft 	    le->error_data.extended_error[1],
    610  1.33   mycroft 	    le->error_data.extended_error[2],
    611  1.33   mycroft 	    le->error_data.extended_error[3],
    612  1.33   mycroft 	    le->error_data.extended_error[4],
    613  1.33   mycroft 	    le->error_data.extended_error[5],
    614  1.33   mycroft 	    le->error_data.extended_error[6],
    615  1.33   mycroft 	    le->error_data.extended_error[7],
    616  1.33   mycroft 	    le->error_data.extended_error[8],
    617  1.33   mycroft 	    le->error_data.extended_error[9],
    618  1.33   mycroft 	    le->error_data.extended_error[10],
    619  1.33   mycroft 	    le->error_data.extended_error[11],
    620  1.33   mycroft 	    le->error_data.extended_error[12],
    621  1.33   mycroft 	    le->error_data.extended_error[13],
    622  1.33   mycroft 	    le->error_data.extended_error[14],
    623  1.33   mycroft 	    le->error_data.extended_error[15],
    624  1.33   mycroft 	    le->error_data.extended_error[15],
    625  1.33   mycroft 	    le->error_data.extended_error[17],
    626  1.33   mycroft 	    le->error_data.extended_error[18]);
    627  1.33   mycroft }
    628  1.33   mycroft 
    629  1.33   mycroft void
    630  1.33   mycroft print_error(void *buf)
    631  1.33   mycroft {
    632  1.33   mycroft 	struct ata_smart_errorlog *erlog = buf;
    633  1.33   mycroft 	u_int8_t checksum;
    634  1.33   mycroft 	int i;
    635  1.33   mycroft 
    636  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    637  1.33   mycroft 		checksum += ((u_int8_t *) buf)[i];
    638  1.33   mycroft 	if (checksum != 0) {
    639  1.33   mycroft 		fprintf(stderr, "SMART error log checksum error\n");
    640  1.33   mycroft 		return;
    641  1.33   mycroft 	}
    642  1.33   mycroft 
    643  1.33   mycroft 	if (erlog->data_structure_revision != 1) {
    644  1.41       dbj 		fprintf(stderr, "Error log revision not 1 (found 0x%04x)\n",
    645  1.41       dbj 		    erlog->data_structure_revision);
    646  1.33   mycroft 		return;
    647  1.33   mycroft 	}
    648  1.33   mycroft 
    649  1.33   mycroft 	if (erlog->mostrecenterror == 0) {
    650  1.33   mycroft 		printf("No errors have been logged\n");
    651  1.33   mycroft 		return;
    652  1.33   mycroft 	}
    653  1.33   mycroft 
    654  1.33   mycroft 	if (erlog->mostrecenterror > 5) {
    655  1.33   mycroft 		fprintf(stderr, "Most recent error is too large\n");
    656  1.33   mycroft 		return;
    657  1.33   mycroft 	}
    658  1.33   mycroft 
    659  1.33   mycroft 	for (i = erlog->mostrecenterror; i < 5; i++)
    660  1.33   mycroft 		print_error_entry(i, &erlog->log_entries[i]);
    661  1.33   mycroft 	for (i = 0; i < erlog->mostrecenterror; i++)
    662  1.33   mycroft 		print_error_entry(i, &erlog->log_entries[i]);
    663  1.33   mycroft 	printf("device error count: %d\n", erlog->device_error_count);
    664  1.33   mycroft }
    665  1.33   mycroft 
    666  1.33   mycroft void
    667  1.24       lha print_selftest_entry(int num, struct ata_smart_selftest *le)
    668  1.24       lha {
    669  1.24       lha 	unsigned char *p;
    670  1.24       lha 	int i;
    671  1.24       lha 
    672  1.24       lha 	/* check if all zero */
    673  1.24       lha 	for (p = (void *)le, i = 0; i < sizeof(*le); i++)
    674  1.24       lha 		if (p[i] != 0)
    675  1.24       lha 			break;
    676  1.24       lha 	if (i == sizeof(*le))
    677  1.24       lha 		return;
    678  1.24       lha 
    679  1.24       lha 	printf("Log entry: %d\n", num);
    680  1.24       lha 
    681  1.24       lha 	/* Get test name */
    682  1.24       lha 	for (i = 0; selftest_name[i].name != NULL; i++)
    683  1.24       lha 		if (selftest_name[i].number == le->number)
    684  1.24       lha 			break;
    685  1.24       lha 
    686  1.33   mycroft 	if (selftest_name[i].name == NULL)
    687  1.33   mycroft 		printf("\tName: (%d)\n", le->number);
    688  1.33   mycroft 	else
    689  1.33   mycroft 		printf("\tName: %s\n", selftest_name[i].name);
    690  1.24       lha 	printf("\tStatus: %s\n", selftest_status[le->status >> 4]);
    691  1.33   mycroft 	/* XXX This generally should not be set when a self-test is completed,
    692  1.33   mycroft 	   and at any rate is useless.  - mycroft */
    693  1.24       lha 	if (le->status >> 4 == 15)
    694  1.33   mycroft 		printf("\tPercent of test remaining: %1d0\n", le->status & 0xf);
    695  1.33   mycroft 	else if (le->status >> 4 != 0)
    696  1.35      fvdl 		printf("\tLBA first error: %d\n", le32toh(le->lba_first_error));
    697  1.24       lha }
    698  1.24       lha 
    699  1.24       lha void
    700  1.24       lha print_selftest(void *buf)
    701  1.24       lha {
    702  1.24       lha 	struct ata_smart_selftestlog *stlog = buf;
    703  1.33   mycroft 	u_int8_t checksum;
    704  1.24       lha 	int i;
    705  1.24       lha 
    706  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    707  1.33   mycroft 		checksum += ((u_int8_t *) buf)[i];
    708  1.33   mycroft 	if (checksum != 0) {
    709  1.24       lha 		fprintf(stderr, "SMART selftest log checksum error\n");
    710  1.24       lha 		return;
    711  1.24       lha 	}
    712  1.24       lha 
    713  1.41       dbj 	if (le16toh(stlog->data_structure_revision) != 1) {
    714  1.41       dbj 		fprintf(stderr, "Self-test log revision not 1 (found 0x%04x)\n",
    715  1.41       dbj 		    le16toh(stlog->data_structure_revision));
    716  1.24       lha 		return;
    717  1.24       lha 	}
    718  1.24       lha 
    719  1.24       lha 	if (stlog->mostrecenttest == 0) {
    720  1.24       lha 		printf("No self-tests have been logged\n");
    721  1.24       lha 		return;
    722  1.24       lha 	}
    723  1.24       lha 
    724  1.24       lha 	if (stlog->mostrecenttest > 22) {
    725  1.24       lha 		fprintf(stderr, "Most recent test is too large\n");
    726  1.24       lha 		return;
    727  1.24       lha 	}
    728  1.24       lha 
    729  1.24       lha 	for (i = stlog->mostrecenttest; i < 22; i++)
    730  1.24       lha 		print_selftest_entry(i, &stlog->log_entries[i]);
    731  1.24       lha 	for (i = 0; i < stlog->mostrecenttest; i++)
    732  1.24       lha 		print_selftest_entry(i, &stlog->log_entries[i]);
    733  1.15     soren }
    734  1.15     soren 
    735  1.38  drochner struct ataparams *
    736  1.38  drochner getataparams()
    737  1.38  drochner {
    738  1.38  drochner 	struct atareq req;
    739  1.38  drochner 	static union {
    740  1.38  drochner 		unsigned char inbuf[DEV_BSIZE];
    741  1.38  drochner 		struct ataparams inqbuf;
    742  1.38  drochner 	} inbuf;
    743  1.38  drochner 
    744  1.38  drochner 	memset(&inbuf, 0, sizeof(inbuf));
    745  1.38  drochner 	memset(&req, 0, sizeof(req));
    746  1.38  drochner 
    747  1.38  drochner 	req.flags = ATACMD_READ;
    748  1.38  drochner 	req.command = WDCC_IDENTIFY;
    749  1.38  drochner 	req.databuf = (caddr_t)&inbuf;
    750  1.38  drochner 	req.datalen = sizeof(inbuf);
    751  1.38  drochner 	req.timeout = 1000;
    752  1.38  drochner 
    753  1.38  drochner 	ata_command(&req);
    754  1.38  drochner 
    755  1.38  drochner 	return (&inbuf.inqbuf);
    756  1.38  drochner }
    757  1.38  drochner 
    758  1.15     soren /*
    759  1.15     soren  * is_smart:
    760  1.15     soren  *
    761  1.15     soren  *	Detect whether device supports SMART and SMART is enabled.
    762  1.15     soren  */
    763  1.15     soren 
    764  1.15     soren int
    765  1.20   mycroft is_smart(void)
    766  1.15     soren {
    767  1.15     soren 	int retval = 0;
    768  1.15     soren 	struct ataparams *inqbuf;
    769  1.39  christos 	const char *status;
    770  1.15     soren 
    771  1.38  drochner 	inqbuf = getataparams();
    772  1.15     soren 
    773  1.15     soren 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    774  1.15     soren 		if (!(inqbuf->atap_cmd_set1 & WDC_CMD1_SMART)) {
    775  1.15     soren 			fprintf(stderr, "SMART unsupported\n");
    776  1.15     soren 		} else {
    777  1.15     soren 			if (inqbuf->atap_ata_major <= WDC_VER_ATA5 ||
    778  1.15     soren 			    inqbuf->atap_cmd_set2 == 0xffff ||
    779  1.15     soren 			    inqbuf->atap_cmd_set2 == 0x0000) {
    780  1.15     soren 				status = "status unknown";
    781  1.15     soren 				retval = 2;
    782  1.15     soren 			} else {
    783  1.18   mycroft 				if (inqbuf->atap_cmd1_en & WDC_CMD1_SMART) {
    784  1.15     soren 					status = "enabled";
    785  1.15     soren 					retval = 1;
    786  1.15     soren 				} else {
    787  1.15     soren 					status = "disabled";
    788  1.15     soren 				}
    789  1.15     soren 			}
    790  1.20   mycroft 			printf("SMART supported, SMART %s\n", status);
    791  1.15     soren 		}
    792  1.15     soren 	}
    793  1.15     soren 	return retval;
    794  1.15     soren }
    795  1.15     soren 
    796  1.15     soren /*
    797   1.1      kenh  * DEVICE COMMANDS
    798   1.1      kenh  */
    799   1.1      kenh 
    800   1.1      kenh /*
    801   1.1      kenh  * device_identify:
    802   1.1      kenh  *
    803   1.1      kenh  *	Display the identity of the device
    804   1.1      kenh  */
    805   1.1      kenh void
    806  1.13    simonb device_identify(int argc, char *argv[])
    807   1.1      kenh {
    808   1.1      kenh 	struct ataparams *inqbuf;
    809   1.2      kenh #if BYTE_ORDER == LITTLE_ENDIAN
    810   1.1      kenh 	int i;
    811   1.1      kenh 	u_int16_t *p;
    812   1.1      kenh #endif
    813   1.1      kenh 
    814   1.1      kenh 	/* No arguments. */
    815   1.1      kenh 	if (argc != 0)
    816   1.5     soren 		usage();
    817   1.1      kenh 
    818  1.38  drochner 	inqbuf = getataparams();
    819   1.1      kenh 
    820   1.1      kenh #if BYTE_ORDER == LITTLE_ENDIAN
    821   1.1      kenh 	/*
    822   1.1      kenh 	 * On little endian machines, we need to shuffle the string
    823   1.1      kenh 	 * byte order.  However, we don't have to do this for NEC or
    824   1.1      kenh 	 * Mitsumi ATAPI devices
    825   1.1      kenh 	 */
    826   1.1      kenh 
    827   1.1      kenh 	if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
    828   1.1      kenh 	      ((inqbuf->atap_model[0] == 'N' &&
    829   1.1      kenh 		  inqbuf->atap_model[1] == 'E') ||
    830   1.1      kenh 	       (inqbuf->atap_model[0] == 'F' &&
    831   1.1      kenh 		  inqbuf->atap_model[1] == 'X')))) {
    832   1.1      kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_model); i += 2) {
    833   1.1      kenh 			p = (u_short *) (inqbuf->atap_model + i);
    834   1.1      kenh 			*p = ntohs(*p);
    835   1.1      kenh 		}
    836   1.1      kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_serial); i += 2) {
    837   1.1      kenh 			p = (u_short *) (inqbuf->atap_serial + i);
    838   1.1      kenh 			*p = ntohs(*p);
    839   1.1      kenh 		}
    840   1.1      kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_revision); i += 2) {
    841   1.1      kenh 			p = (u_short *) (inqbuf->atap_revision + i);
    842   1.1      kenh 			*p = ntohs(*p);
    843   1.1      kenh 		}
    844   1.1      kenh 	}
    845   1.1      kenh #endif
    846   1.1      kenh 
    847   1.1      kenh 	/*
    848   1.1      kenh 	 * Strip blanks off of the info strings.  Yuck, I wish this was
    849   1.1      kenh 	 * cleaner.
    850   1.1      kenh 	 */
    851   1.1      kenh 
    852   1.1      kenh 	if (inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] == ' ') {
    853   1.1      kenh 		inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] = '\0';
    854   1.1      kenh 		while (inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] == ' ')
    855   1.1      kenh 			inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] = '\0';
    856   1.1      kenh 	}
    857   1.1      kenh 
    858   1.1      kenh 	if (inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] == ' ') {
    859   1.1      kenh 		inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] = '\0';
    860   1.1      kenh 		while (inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] == ' ')
    861   1.1      kenh 			inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] = '\0';
    862   1.1      kenh 	}
    863   1.1      kenh 
    864   1.1      kenh 	if (inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] == ' ') {
    865   1.1      kenh 		inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] = '\0';
    866   1.1      kenh 		while (inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] == ' ')
    867   1.1      kenh 			inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] = '\0';
    868   1.1      kenh 	}
    869   1.1      kenh 
    870   1.1      kenh 	printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
    871   1.1      kenh 	       (int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
    872   1.1      kenh 	       (int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
    873   1.1      kenh 	       (int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
    874   1.1      kenh 
    875   1.1      kenh 	printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
    876   1.1      kenh 	       "ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
    877   1.1      kenh 	       "removable");
    878   1.1      kenh 
    879   1.1      kenh 	if ((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == 0)
    880   1.1      kenh 		printf("Cylinders: %d, heads: %d, sec/track: %d, total "
    881   1.1      kenh 		       "sectors: %d\n", inqbuf->atap_cylinders,
    882   1.1      kenh 		       inqbuf->atap_heads, inqbuf->atap_sectors,
    883   1.1      kenh 		       (inqbuf->atap_capacity[1] << 16) |
    884   1.1      kenh 		       inqbuf->atap_capacity[0]);
    885   1.1      kenh 
    886   1.1      kenh 	if (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK)
    887   1.1      kenh 		printf("Device supports command queue depth of %d\n",
    888   1.1      kenh 		       inqbuf->atap_queuedepth & 0xf);
    889   1.1      kenh 
    890   1.1      kenh 	printf("Device capabilities:\n");
    891  1.10        is 	print_bitinfo("\t", "\n", inqbuf->atap_capabilities1, ata_caps);
    892   1.1      kenh 
    893   1.1      kenh 	if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
    894   1.1      kenh 		printf("Device supports following standards:\n");
    895  1.10        is 		print_bitinfo("", " ", inqbuf->atap_ata_major, ata_vers);
    896   1.1      kenh 		printf("\n");
    897   1.1      kenh 	}
    898   1.1      kenh 
    899   1.1      kenh 	if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
    900   1.1      kenh 	    inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
    901   1.1      kenh 		printf("Command set support:\n");
    902  1.33   mycroft 		if (inqbuf->atap_cmd1_en != 0 && inqbuf->atap_cmd1_en != 0xffff)
    903  1.33   mycroft 			print_bitinfo2("\t", "\n", inqbuf->atap_cmd_set1,
    904  1.33   mycroft 			    inqbuf->atap_cmd1_en, ata_cmd_set1);
    905  1.33   mycroft 		else
    906  1.33   mycroft 			print_bitinfo("\t", "\n", inqbuf->atap_cmd_set1,
    907  1.33   mycroft 			    ata_cmd_set1);
    908  1.33   mycroft 		if (inqbuf->atap_cmd2_en != 0 && inqbuf->atap_cmd2_en != 0xffff)
    909  1.33   mycroft 			print_bitinfo2("\t", "\n", inqbuf->atap_cmd_set2,
    910  1.33   mycroft 			    inqbuf->atap_cmd2_en, ata_cmd_set2);
    911  1.33   mycroft 		else
    912  1.33   mycroft 			print_bitinfo("\t", "\n", inqbuf->atap_cmd_set2,
    913  1.33   mycroft 			    ata_cmd_set2);
    914  1.23      yamt 		if (inqbuf->atap_cmd_ext != 0 && inqbuf->atap_cmd_ext != 0xffff)
    915  1.23      yamt 			print_bitinfo("\t", "\n", inqbuf->atap_cmd_ext,
    916  1.23      yamt 			    ata_cmd_ext);
    917   1.1      kenh 	}
    918   1.1      kenh 
    919   1.1      kenh 	return;
    920   1.1      kenh }
    921   1.1      kenh 
    922   1.1      kenh /*
    923   1.1      kenh  * device idle:
    924   1.1      kenh  *
    925   1.1      kenh  * issue the IDLE IMMEDIATE command to the drive
    926   1.1      kenh  */
    927   1.1      kenh 
    928   1.1      kenh void
    929  1.13    simonb device_idle(int argc, char *argv[])
    930   1.1      kenh {
    931   1.1      kenh 	struct atareq req;
    932   1.1      kenh 
    933   1.1      kenh 	/* No arguments. */
    934   1.1      kenh 	if (argc != 0)
    935   1.5     soren 		usage();
    936   1.1      kenh 
    937   1.1      kenh 	memset(&req, 0, sizeof(req));
    938   1.1      kenh 
    939   1.1      kenh 	if (strcmp(cmdname, "idle") == 0)
    940   1.1      kenh 		req.command = WDCC_IDLE_IMMED;
    941   1.1      kenh 	else if (strcmp(cmdname, "standby") == 0)
    942   1.1      kenh 		req.command = WDCC_STANDBY_IMMED;
    943   1.1      kenh 	else
    944   1.1      kenh 		req.command = WDCC_SLEEP;
    945   1.1      kenh 
    946   1.1      kenh 	req.timeout = 1000;
    947   1.1      kenh 
    948   1.1      kenh 	ata_command(&req);
    949   1.1      kenh 
    950   1.1      kenh 	return;
    951   1.1      kenh }
    952   1.1      kenh 
    953   1.1      kenh /*
    954   1.1      kenh  * Set the idle timer on the disk.  Set it for either idle mode or
    955   1.1      kenh  * standby mode, depending on how we were invoked.
    956   1.1      kenh  */
    957   1.1      kenh 
    958   1.1      kenh void
    959  1.13    simonb device_setidle(int argc, char *argv[])
    960   1.1      kenh {
    961   1.1      kenh 	unsigned long idle;
    962   1.1      kenh 	struct atareq req;
    963   1.1      kenh 	char *end;
    964   1.1      kenh 
    965   1.1      kenh 	/* Only one argument */
    966   1.1      kenh 	if (argc != 1)
    967   1.5     soren 		usage();
    968   1.1      kenh 
    969   1.1      kenh 	idle = strtoul(argv[0], &end, 0);
    970   1.1      kenh 
    971   1.1      kenh 	if (*end != '\0') {
    972   1.1      kenh 		fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
    973   1.1      kenh 		exit(1);
    974   1.1      kenh 	}
    975   1.1      kenh 
    976   1.1      kenh 	if (idle > 19800) {
    977   1.1      kenh 		fprintf(stderr, "Idle time has a maximum value of 5.5 "
    978   1.1      kenh 			"hours\n");
    979   1.1      kenh 		exit(1);
    980   1.1      kenh 	}
    981   1.1      kenh 
    982   1.1      kenh 	if (idle != 0 && idle < 5) {
    983   1.1      kenh 		fprintf(stderr, "Idle timer must be at least 5 seconds\n");
    984   1.1      kenh 		exit(1);
    985   1.1      kenh 	}
    986   1.1      kenh 
    987   1.1      kenh 	memset(&req, 0, sizeof(req));
    988   1.1      kenh 
    989   1.1      kenh 	if (idle <= 240*5)
    990   1.1      kenh 		req.sec_count = idle / 5;
    991   1.1      kenh 	else
    992   1.1      kenh 		req.sec_count = idle / (30*60) + 240;
    993   1.1      kenh 
    994   1.1      kenh 	req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
    995   1.1      kenh 	req.timeout = 1000;
    996   1.1      kenh 
    997   1.1      kenh 	ata_command(&req);
    998   1.1      kenh 
    999   1.1      kenh 	return;
   1000   1.3      kenh }
   1001   1.3      kenh 
   1002   1.3      kenh /*
   1003   1.3      kenh  * Query the device for the current power mode
   1004   1.3      kenh  */
   1005   1.3      kenh 
   1006   1.3      kenh void
   1007  1.13    simonb device_checkpower(int argc, char *argv[])
   1008   1.3      kenh {
   1009   1.3      kenh 	struct atareq req;
   1010   1.3      kenh 
   1011   1.3      kenh 	/* No arguments. */
   1012   1.3      kenh 	if (argc != 0)
   1013   1.5     soren 		usage();
   1014   1.3      kenh 
   1015   1.3      kenh 	memset(&req, 0, sizeof(req));
   1016   1.3      kenh 
   1017   1.3      kenh 	req.command = WDCC_CHECK_PWR;
   1018   1.3      kenh 	req.timeout = 1000;
   1019   1.3      kenh 	req.flags = ATACMD_READREG;
   1020   1.3      kenh 
   1021   1.3      kenh 	ata_command(&req);
   1022   1.3      kenh 
   1023   1.3      kenh 	printf("Current power status: ");
   1024   1.3      kenh 
   1025   1.3      kenh 	switch (req.sec_count) {
   1026   1.3      kenh 	case 0x00:
   1027   1.3      kenh 		printf("Standby mode\n");
   1028   1.3      kenh 		break;
   1029   1.3      kenh 	case 0x80:
   1030   1.3      kenh 		printf("Idle mode\n");
   1031   1.3      kenh 		break;
   1032   1.3      kenh 	case 0xff:
   1033   1.3      kenh 		printf("Active mode\n");
   1034   1.3      kenh 		break;
   1035   1.3      kenh 	default:
   1036   1.3      kenh 		printf("Unknown power code (%02x)\n", req.sec_count);
   1037   1.3      kenh 	}
   1038   1.3      kenh 
   1039  1.15     soren 	return;
   1040  1.15     soren }
   1041  1.15     soren 
   1042  1.15     soren /*
   1043  1.15     soren  * device_smart:
   1044  1.15     soren  *
   1045  1.15     soren  *	Display SMART status
   1046  1.15     soren  */
   1047  1.15     soren void
   1048  1.15     soren device_smart(int argc, char *argv[])
   1049  1.15     soren {
   1050  1.15     soren 	struct atareq req;
   1051  1.15     soren 	unsigned char inbuf[DEV_BSIZE];
   1052  1.15     soren 	unsigned char inbuf2[DEV_BSIZE];
   1053  1.15     soren 
   1054  1.33   mycroft 	if (argc < 1)
   1055  1.15     soren 		usage();
   1056  1.15     soren 
   1057  1.15     soren 	if (strcmp(argv[0], "enable") == 0) {
   1058  1.20   mycroft 		memset(&req, 0, sizeof(req));
   1059  1.15     soren 
   1060  1.20   mycroft 		req.features = WDSM_ENABLE_OPS;
   1061  1.20   mycroft 		req.command = WDCC_SMART;
   1062  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1063  1.20   mycroft 		req.timeout = 1000;
   1064  1.15     soren 
   1065  1.20   mycroft 		ata_command(&req);
   1066  1.15     soren 
   1067  1.20   mycroft 		is_smart();
   1068  1.15     soren 	} else if (strcmp(argv[0], "disable") == 0) {
   1069  1.20   mycroft 		memset(&req, 0, sizeof(req));
   1070  1.15     soren 
   1071  1.20   mycroft 		req.features = WDSM_DISABLE_OPS;
   1072  1.20   mycroft 		req.command = WDCC_SMART;
   1073  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1074  1.20   mycroft 		req.timeout = 1000;
   1075  1.15     soren 
   1076  1.20   mycroft 		ata_command(&req);
   1077  1.15     soren 
   1078  1.20   mycroft 		is_smart();
   1079  1.16     soren 	} else if (strcmp(argv[0], "status") == 0) {
   1080  1.24       lha 		if (!is_smart()) {
   1081  1.24       lha 			fprintf(stderr, "SMART not supported\n");
   1082  1.24       lha 			return;
   1083  1.24       lha 		}
   1084  1.24       lha 
   1085  1.15     soren 			memset(&inbuf, 0, sizeof(inbuf));
   1086  1.15     soren 			memset(&req, 0, sizeof(req));
   1087  1.15     soren 
   1088  1.15     soren 			req.features = WDSM_STATUS;
   1089  1.15     soren 			req.command = WDCC_SMART;
   1090  1.35      fvdl 			req.cylinder = WDSMART_CYL;
   1091  1.15     soren 			req.timeout = 1000;
   1092  1.15     soren 
   1093  1.15     soren 			ata_command(&req);
   1094  1.15     soren 
   1095  1.35      fvdl 			if (req.cylinder != WDSMART_CYL) {
   1096  1.15     soren 				fprintf(stderr, "Threshold exceeds condition\n");
   1097  1.15     soren 			}
   1098  1.15     soren 
   1099  1.15     soren 			/* WDSM_RD_DATA and WDSM_RD_THRESHOLDS are optional
   1100  1.15     soren 			 * features, the following ata_command()'s may error
   1101  1.15     soren 			 * and exit().
   1102  1.15     soren 			 */
   1103  1.15     soren 
   1104  1.15     soren 			memset(&inbuf, 0, sizeof(inbuf));
   1105  1.15     soren 			memset(&req, 0, sizeof(req));
   1106  1.15     soren 
   1107  1.15     soren 			req.flags = ATACMD_READ;
   1108  1.15     soren 			req.features = WDSM_RD_DATA;
   1109  1.15     soren 			req.command = WDCC_SMART;
   1110  1.15     soren 			req.databuf = (caddr_t) inbuf;
   1111  1.15     soren 			req.datalen = sizeof(inbuf);
   1112  1.35      fvdl 			req.cylinder = WDSMART_CYL;
   1113  1.15     soren 			req.timeout = 1000;
   1114  1.15     soren 
   1115  1.15     soren 			ata_command(&req);
   1116  1.15     soren 
   1117  1.15     soren 			memset(&inbuf2, 0, sizeof(inbuf2));
   1118  1.15     soren 			memset(&req, 0, sizeof(req));
   1119  1.15     soren 
   1120  1.15     soren 			req.flags = ATACMD_READ;
   1121  1.15     soren 			req.features = WDSM_RD_THRESHOLDS;
   1122  1.15     soren 			req.command = WDCC_SMART;
   1123  1.15     soren 			req.databuf = (caddr_t) inbuf2;
   1124  1.15     soren 			req.datalen = sizeof(inbuf2);
   1125  1.35      fvdl 			req.cylinder = WDSMART_CYL;
   1126  1.15     soren 			req.timeout = 1000;
   1127  1.15     soren 
   1128  1.15     soren 			ata_command(&req);
   1129  1.15     soren 
   1130  1.15     soren 			print_smart_status(inbuf, inbuf2);
   1131  1.24       lha 
   1132  1.33   mycroft 	} else if (strcmp(argv[0], "offline") == 0) {
   1133  1.34     soren 		if (argc != 2)
   1134  1.34     soren 			usage();
   1135  1.33   mycroft 		if (!is_smart()) {
   1136  1.33   mycroft 			fprintf(stderr, "SMART not supported\n");
   1137  1.33   mycroft 			return;
   1138  1.33   mycroft 		}
   1139  1.33   mycroft 
   1140  1.33   mycroft 		memset(&req, 0, sizeof(req));
   1141  1.33   mycroft 
   1142  1.33   mycroft 		req.features = WDSM_EXEC_OFFL_IMM;
   1143  1.33   mycroft 		req.command = WDCC_SMART;
   1144  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1145  1.33   mycroft 		req.sec_num = atol(argv[1]);
   1146  1.33   mycroft 		req.timeout = 10000;
   1147  1.33   mycroft 
   1148  1.33   mycroft 		ata_command(&req);
   1149  1.33   mycroft 	} else if (strcmp(argv[0], "error-log") == 0) {
   1150  1.33   mycroft 		if (!is_smart()) {
   1151  1.33   mycroft 			fprintf(stderr, "SMART not supported\n");
   1152  1.33   mycroft 			return;
   1153  1.33   mycroft 		}
   1154  1.33   mycroft 
   1155  1.33   mycroft 		memset(&inbuf, 0, sizeof(inbuf));
   1156  1.33   mycroft 		memset(&req, 0, sizeof(req));
   1157  1.33   mycroft 
   1158  1.33   mycroft 		req.flags = ATACMD_READ;
   1159  1.33   mycroft 		req.features = WDSM_RD_LOG;
   1160  1.33   mycroft 		req.sec_count = 1;
   1161  1.33   mycroft 		req.sec_num = 1;
   1162  1.33   mycroft 		req.command = WDCC_SMART;
   1163  1.33   mycroft 		req.databuf = (caddr_t) inbuf;
   1164  1.33   mycroft 		req.datalen = sizeof(inbuf);
   1165  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1166  1.33   mycroft 		req.timeout = 1000;
   1167  1.33   mycroft 
   1168  1.33   mycroft 		ata_command(&req);
   1169  1.33   mycroft 
   1170  1.33   mycroft 		print_error(inbuf);
   1171  1.24       lha 	} else if (strcmp(argv[0], "selftest-log") == 0) {
   1172  1.24       lha 		if (!is_smart()) {
   1173  1.15     soren 			fprintf(stderr, "SMART not supported\n");
   1174  1.24       lha 			return;
   1175  1.15     soren 		}
   1176  1.24       lha 
   1177  1.24       lha 		memset(&inbuf, 0, sizeof(inbuf));
   1178  1.24       lha 		memset(&req, 0, sizeof(req));
   1179  1.24       lha 
   1180  1.24       lha 		req.flags = ATACMD_READ;
   1181  1.24       lha 		req.features = WDSM_RD_LOG;
   1182  1.24       lha 		req.sec_count = 1;
   1183  1.24       lha 		req.sec_num = 6;
   1184  1.24       lha 		req.command = WDCC_SMART;
   1185  1.24       lha 		req.databuf = (caddr_t) inbuf;
   1186  1.24       lha 		req.datalen = sizeof(inbuf);
   1187  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1188  1.24       lha 		req.timeout = 1000;
   1189  1.24       lha 
   1190  1.24       lha 		ata_command(&req);
   1191  1.24       lha 
   1192  1.24       lha 		print_selftest(inbuf);
   1193  1.24       lha 
   1194  1.15     soren 	} else {
   1195  1.15     soren 		usage();
   1196  1.15     soren 	}
   1197   1.3      kenh 	return;
   1198   1.1      kenh }
   1199  1.30    bouyer 
   1200  1.38  drochner void
   1201  1.38  drochner device_security(int argc, char *argv[])
   1202  1.38  drochner {
   1203  1.38  drochner 	struct atareq req;
   1204  1.38  drochner 	struct ataparams *inqbuf;
   1205  1.38  drochner 
   1206  1.38  drochner 	/* need subcommand */
   1207  1.38  drochner 	if (argc < 1)
   1208  1.38  drochner 		usage();
   1209  1.38  drochner 
   1210  1.38  drochner 	if (strcmp(argv[0], "freeze") == 0) {
   1211  1.38  drochner 		memset(&req, 0, sizeof(req));
   1212  1.38  drochner 		req.command = WCDD_SECURITY_FREEZE;
   1213  1.38  drochner 		req.timeout = 1000;
   1214  1.38  drochner 		ata_command(&req);
   1215  1.38  drochner 	} else if (strcmp(argv[0], "status") == 0) {
   1216  1.38  drochner 		inqbuf = getataparams();
   1217  1.38  drochner 		print_bitinfo("\t", "\n", inqbuf->atap_sec_st, ata_sec_st);
   1218  1.38  drochner 	} else
   1219  1.38  drochner 		usage();
   1220  1.38  drochner 
   1221  1.38  drochner 	return;
   1222  1.38  drochner }
   1223  1.38  drochner 
   1224  1.30    bouyer /*
   1225  1.30    bouyer  * bus_reset:
   1226  1.30    bouyer  *	Reset an ATA bus (will reset all devices on the bus)
   1227  1.30    bouyer  */
   1228  1.30    bouyer void
   1229  1.30    bouyer bus_reset(int argc, char *argv[])
   1230  1.30    bouyer {
   1231  1.30    bouyer 	int error;
   1232  1.30    bouyer 
   1233  1.30    bouyer 	/* no args */
   1234  1.30    bouyer 	if (argc != 0)
   1235  1.30    bouyer 		usage();
   1236  1.30    bouyer 
   1237  1.30    bouyer 	error = ioctl(fd, ATABUSIORESET, NULL);
   1238  1.30    bouyer 
   1239  1.30    bouyer 	if (error == -1)
   1240  1.30    bouyer 		err(1, "ATABUSIORESET failed");
   1241  1.30    bouyer }
   1242