4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2002-6 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * You should have received a copy of the GNU General Public License
15 * (for example COPYING); if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * This code was originally developed as a Senior Thesis by Michael Cornwell
19 * at the Concurrent Systems Laboratory (now part of the Storage Systems
20 * Research Center), Jack Baskin School of Engineering, University of
21 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
28 #define ATACMDS_H_CVSID "$Id: atacmds.h,v 1.83 2006/08/25 06:06:24 sxzzsf Exp $\n"
30 // Macro to check expected size of struct at compile time using a
31 // dummy typedef. On size mismatch, compiler reports a negative array
32 // size. If you see an error message of this form, it means that the
33 // #pragma pack(1) pragma below is not having the desired effect on
35 #define ASSERT_SIZEOF_STRUCT(s, n) \
36 typedef char assert_sizeof_struct_##s[(sizeof(struct s) == (n)) ? 1 : -1]
38 // Add __attribute__((packed)) if compiler supports it
39 // because some gcc versions (at least ARM) lack support of #pragma pack()
40 #ifdef HAVE_ATTR_PACKED
41 #define ATTR_PACKED __attribute__((packed))
47 // returns no data, just succeeds or fails
53 STATUS
, // just says if SMART is working or not
54 STATUS_CHECK
, // says if disk's SMART status is healthy, or failing
55 // return 512 bytes of data:
61 // returns 1 byte of data
63 // writes 512 bytes of data:
67 // ATA Specification Command Register Values (Commands)
68 #define ATA_IDENTIFY_DEVICE 0xec
69 #define ATA_IDENTIFY_PACKET_DEVICE 0xa1
70 #define ATA_SMART_CMD 0xb0
71 #define ATA_CHECK_POWER_MODE 0xe5
73 // ATA Specification Feature Register Values (SMART Subcommands).
74 // Note that some are obsolete as of ATA-7.
75 #define ATA_SMART_READ_VALUES 0xd0
76 #define ATA_SMART_READ_THRESHOLDS 0xd1
77 #define ATA_SMART_AUTOSAVE 0xd2
78 #define ATA_SMART_SAVE 0xd3
79 #define ATA_SMART_IMMEDIATE_OFFLINE 0xd4
80 #define ATA_SMART_READ_LOG_SECTOR 0xd5
81 #define ATA_SMART_WRITE_LOG_SECTOR 0xd6
82 #define ATA_SMART_WRITE_THRESHOLDS 0xd7
83 #define ATA_SMART_ENABLE 0xd8
84 #define ATA_SMART_DISABLE 0xd9
85 #define ATA_SMART_STATUS 0xda
86 // SFF 8035i Revision 2 Specification Feature Register Value (SMART
88 #define ATA_SMART_AUTO_OFFLINE 0xdb
90 // Sector Number values for ATA_SMART_IMMEDIATE_OFFLINE Subcommand
91 #define OFFLINE_FULL_SCAN 0
92 #define SHORT_SELF_TEST 1
93 #define EXTEND_SELF_TEST 2
94 #define CONVEYANCE_SELF_TEST 3
95 #define SELECTIVE_SELF_TEST 4
96 #define ABORT_SELF_TEST 127
97 #define SHORT_CAPTIVE_SELF_TEST 129
98 #define EXTEND_CAPTIVE_SELF_TEST 130
99 #define CONVEYANCE_CAPTIVE_SELF_TEST 131
100 #define SELECTIVE_CAPTIVE_SELF_TEST 132
101 #define CAPTIVE_MASK (0x01<<7)
103 // Maximum allowed number of SMART Attributes
104 #define NUMBER_ATA_SMART_ATTRIBUTES 30
106 // Needed parts of the ATA DRIVE IDENTIFY Structure. Those labeled
107 // word* are NOT used.
109 struct ata_identify_device
{
110 unsigned short words000_009
[10];
111 unsigned char serial_no
[20];
112 unsigned short words020_022
[3];
113 unsigned char fw_rev
[8];
114 unsigned char model
[40];
115 unsigned short words047_079
[33];
116 unsigned short major_rev_num
;
117 unsigned short minor_rev_num
;
118 unsigned short command_set_1
;
119 unsigned short command_set_2
;
120 unsigned short command_set_extension
;
121 unsigned short cfs_enable_1
;
122 unsigned short word086
;
123 unsigned short csf_default
;
124 unsigned short words088_255
[168];
127 ASSERT_SIZEOF_STRUCT(ata_identify_device
, 512);
129 /* ata_smart_attribute is the vendor specific in SFF-8035 spec */
131 struct ata_smart_attribute
{
133 // meaning of flag bits: see MACROS just below
134 // WARNING: MISALIGNED!
135 unsigned short flags
;
136 unsigned char current
;
138 unsigned char raw
[6];
139 unsigned char reserv
;
142 ASSERT_SIZEOF_STRUCT(ata_smart_attribute
, 12);
144 // MACROS to interpret the flags bits in the previous structure.
145 // These have not been implemented using bitflags and a union, to make
146 // it portable across bit/little endian and different platforms.
150 // From SFF 8035i Revision 2 page 19: Bit 0 (pre-failure/advisory bit)
151 // - If the value of this bit equals zero, an attribute value less
152 // than or equal to its corresponding attribute threshold indicates an
153 // advisory condition where the usage or age of the device has
154 // exceeded its intended design life period. If the value of this bit
155 // equals one, an attribute value less than or equal to its
156 // corresponding attribute threshold indicates a prefailure condition
157 // where imminent loss of data is being predicted.
158 #define ATTRIBUTE_FLAGS_PREFAILURE(x) (x & 0x01)
162 // From SFF 8035i Revision 2 page 19: Bit 1 (on-line data collection
163 // bit) - If the value of this bit equals zero, then the attribute
164 // value is updated only during off-line data collection
165 // activities. If the value of this bit equals one, then the attribute
166 // value is updated during normal operation of the device or during
167 // both normal operation and off-line testing.
168 #define ATTRIBUTE_FLAGS_ONLINE(x) (x & 0x02)
171 // The following are (probably) IBM's, Maxtors and Quantum's definitions for the
172 // vendor-specific bits:
173 // 2: Performance type bit
174 #define ATTRIBUTE_FLAGS_PERFORMANCE(x) (x & 0x04)
176 // 3: Errorrate type bit
177 #define ATTRIBUTE_FLAGS_ERRORRATE(x) (x & 0x08)
180 #define ATTRIBUTE_FLAGS_EVENTCOUNT(x) (x & 0x10)
182 // 5: Selfpereserving bit
183 #define ATTRIBUTE_FLAGS_SELFPRESERVING(x) (x & 0x20)
186 // Last ten bits are reserved for future use
188 /* ata_smart_values is format of the read drive Attribute command */
189 /* see Table 34 of T13/1321D Rev 1 spec (Device SMART data structure) for *some* info */
191 struct ata_smart_values
{
192 unsigned short int revnumber
;
193 struct ata_smart_attribute vendor_attributes
[NUMBER_ATA_SMART_ATTRIBUTES
];
194 unsigned char offline_data_collection_status
;
195 unsigned char self_test_exec_status
; //IBM # segments for offline collection
196 unsigned short int total_time_to_complete_off_line
; // IBM different
197 unsigned char vendor_specific_366
; // Maxtor & IBM curent segment pointer
198 unsigned char offline_data_collection_capability
;
199 unsigned short int smart_capability
;
200 unsigned char errorlog_capability
;
201 unsigned char vendor_specific_371
; // Maxtor, IBM: self-test failure checkpoint see below!
202 unsigned char short_test_completion_time
;
203 unsigned char extend_test_completion_time
;
204 unsigned char conveyance_test_completion_time
;
205 unsigned char reserved_375_385
[11];
206 unsigned char vendor_specific_386_510
[125]; // Maxtor bytes 508-509 Attribute/Threshold Revision #
207 unsigned char chksum
;
210 ASSERT_SIZEOF_STRUCT(ata_smart_values
, 512);
212 /* Maxtor, IBM: self-test failure checkpoint byte meaning:
221 /* Vendor attribute of SMART Threshold (compare to ata_smart_attribute above) */
223 struct ata_smart_threshold_entry
{
225 unsigned char threshold
;
226 unsigned char reserved
[10];
229 ASSERT_SIZEOF_STRUCT(ata_smart_threshold_entry
, 12);
231 /* Format of Read SMART THreshold Command */
232 /* Compare to ata_smart_values above */
234 struct ata_smart_thresholds_pvt
{
235 unsigned short int revnumber
;
236 struct ata_smart_threshold_entry thres_entries
[NUMBER_ATA_SMART_ATTRIBUTES
];
237 unsigned char reserved
[149];
238 unsigned char chksum
;
241 ASSERT_SIZEOF_STRUCT(ata_smart_thresholds_pvt
, 512);
244 // Table 42 of T13/1321D Rev 1 spec (Error Data Structure)
246 struct ata_smart_errorlog_error_struct
{
247 unsigned char reserved
;
248 unsigned char error_register
;
249 unsigned char sector_count
;
250 unsigned char sector_number
;
251 unsigned char cylinder_low
;
252 unsigned char cylinder_high
;
253 unsigned char drive_head
;
254 unsigned char status
;
255 unsigned char extended_error
[19];
257 unsigned short timestamp
;
260 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_error_struct
, 30);
263 // Table 41 of T13/1321D Rev 1 spec (Command Data Structure)
265 struct ata_smart_errorlog_command_struct
{
266 unsigned char devicecontrolreg
;
267 unsigned char featuresreg
;
268 unsigned char sector_count
;
269 unsigned char sector_number
;
270 unsigned char cylinder_low
;
271 unsigned char cylinder_high
;
272 unsigned char drive_head
;
273 unsigned char commandreg
;
274 unsigned int timestamp
;
277 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_command_struct
, 12);
279 // Table 40 of T13/1321D Rev 1 spec (Error log data structure)
281 struct ata_smart_errorlog_struct
{
282 struct ata_smart_errorlog_command_struct commands
[5];
283 struct ata_smart_errorlog_error_struct error_struct
;
286 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_struct
, 90);
288 // Table 39 of T13/1321D Rev 1 spec (SMART error log sector)
290 struct ata_smart_errorlog
{
291 unsigned char revnumber
;
292 unsigned char error_log_pointer
;
293 struct ata_smart_errorlog_struct errorlog_struct
[5];
294 unsigned short int ata_error_count
;
295 unsigned char reserved
[57];
296 unsigned char checksum
;
299 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog
, 512);
301 // Table 45 of T13/1321D Rev 1 spec (Self-test log descriptor entry)
303 struct ata_smart_selftestlog_struct
{
304 unsigned char selftestnumber
; // Sector number register
305 unsigned char selfteststatus
;
306 unsigned short int timestamp
;
307 unsigned char selftestfailurecheckpoint
;
308 unsigned int lbafirstfailure
;
309 unsigned char vendorspecific
[15];
312 ASSERT_SIZEOF_STRUCT(ata_smart_selftestlog_struct
, 24);
314 // Table 44 of T13/1321D Rev 1 spec (Self-test log data structure)
316 struct ata_smart_selftestlog
{
317 unsigned short int revnumber
;
318 struct ata_smart_selftestlog_struct selftest_struct
[21];
319 unsigned char vendorspecific
[2];
320 unsigned char mostrecenttest
;
321 unsigned char reserved
[2];
322 unsigned char chksum
;
325 ASSERT_SIZEOF_STRUCT(ata_smart_selftestlog
, 512);
327 // SMART LOG DIRECTORY Table 52 of T13/1532D Vol 1 Rev 1a
329 struct ata_smart_log_entry
{
330 unsigned char numsectors
;
331 unsigned char reserved
;
334 ASSERT_SIZEOF_STRUCT(ata_smart_log_entry
, 2);
337 struct ata_smart_log_directory
{
338 unsigned short int logversion
;
339 struct ata_smart_log_entry entry
[255];
342 ASSERT_SIZEOF_STRUCT(ata_smart_log_directory
, 512);
344 // SMART SELECTIVE SELF-TEST LOG Table 61 of T13/1532D Volume 1
352 ASSERT_SIZEOF_STRUCT(test_span
, 16);
355 struct ata_selective_self_test_log
{
356 unsigned short logversion
;
357 struct test_span span
[5];
358 unsigned char reserved1
[337-82+1];
359 unsigned char vendor_specific1
[491-338+1];
361 unsigned short currentspan
;
362 unsigned short flags
;
363 unsigned char vendor_specific2
[507-504+1];
364 unsigned short pendingtime
;
365 unsigned char reserved2
;
366 unsigned char checksum
;
369 ASSERT_SIZEOF_STRUCT(ata_selective_self_test_log
, 512);
371 #define SELECTIVE_FLAG_DOSCAN (0x0002)
372 #define SELECTIVE_FLAG_PENDING (0x0008)
373 #define SELECTIVE_FLAG_ACTIVE (0x0010)
375 // Get information from drive
376 int ataReadHDIdentity(int device
, struct ata_identify_device
*buf
);
377 int ataCheckPowerMode(int device
);
379 /* Read S.M.A.R.T information from drive */
380 int ataReadSmartValues(int device
,struct ata_smart_values
*);
381 int ataReadSmartThresholds(int device
, struct ata_smart_thresholds_pvt
*);
382 int ataReadErrorLog(int device
, struct ata_smart_errorlog
*);
383 int ataReadSelfTestLog(int device
, struct ata_smart_selftestlog
*);
384 int ataReadSelectiveSelfTestLog(int device
, struct ata_selective_self_test_log
*data
);
385 int ataSmartStatus(int device
);
386 int ataSetSmartThresholds(int device
, struct ata_smart_thresholds_pvt
*);
387 int ataReadLogDirectory(int device
, struct ata_smart_log_directory
*);
389 /* Enable/Disable SMART on device */
390 int ataEnableSmart ( int device
);
391 int ataDisableSmart (int device
);
392 int ataEnableAutoSave(int device
);
393 int ataDisableAutoSave(int device
);
395 /* Automatic Offline Testing */
396 int ataEnableAutoOffline ( int device
);
397 int ataDisableAutoOffline (int device
);
399 /* S.M.A.R.T. test commands */
400 int ataSmartOfflineTest (int device
);
401 int ataSmartExtendSelfTest (int device
);
402 int ataSmartShortSelfTest (int device
);
403 int ataSmartShortCapSelfTest (int device
);
404 int ataSmartExtendCapSelfTest (int device
);
405 int ataSmartSelfTestAbort (int device
);
407 // Returns the latest compatibility of ATA/ATAPI Version the device
408 // supports. Returns -1 if Version command is not supported
409 int ataVersionInfo (const char **description
, struct ata_identify_device
*drive
, unsigned short *minor
);
411 // If SMART supported, this is guaranteed to return 1 if SMART is enabled, else 0.
412 int ataDoesSmartWork(int device
);
414 // returns 1 if SMART supported, 0 if not supported or can't tell
415 int ataSmartSupport ( struct ata_identify_device
*drive
);
420 // -1: can't tell if SMART is enabled -- try issuing ataDoesSmartWork command to see
421 int ataIsSmartEnabled(struct ata_identify_device
*drive
);
423 /* Check SMART for Threshold failure */
424 // onlyfailed=0 : are or were any age or prefailure attributes <= threshold
425 // onlyfailed=1: are any prefailure attributes <= threshold now
426 int ataCheckSmart ( struct ata_smart_values
*data
, struct ata_smart_thresholds_pvt
*thresholds
, int onlyfailed
);
428 int ataSmartStatus2(int device
);
430 // int isOfflineTestTime ( struct ata_smart_values data)
431 // returns S.M.A.R.T. Offline Test Time in seconds
432 int isOfflineTestTime ( struct ata_smart_values
*data
);
434 int isShortSelfTestTime ( struct ata_smart_values
*data
);
436 int isExtendedSelfTestTime ( struct ata_smart_values
*data
);
438 int isSmartErrorLogCapable(struct ata_smart_values
*data
, struct ata_identify_device
*identity
);
440 int isSmartTestLogCapable(struct ata_smart_values
*data
, struct ata_identify_device
*identity
);
442 int isGeneralPurposeLoggingCapable(struct ata_identify_device
*identity
);
444 int isSupportExecuteOfflineImmediate ( struct ata_smart_values
*data
);
446 int isSupportAutomaticTimer ( struct ata_smart_values
*data
);
448 int isSupportOfflineAbort ( struct ata_smart_values
*data
);
450 int isSupportOfflineSurfaceScan ( struct ata_smart_values
*data
);
452 int isSupportSelfTest (struct ata_smart_values
*data
);
454 int isSupportConveyanceSelfTest(struct ata_smart_values
*data
);
456 int isSupportSelectiveSelfTest(struct ata_smart_values
*data
);
458 int ataSmartTest(int device
, int testtype
, struct ata_smart_values
*data
);
460 int TestTime(struct ata_smart_values
*data
,int testtype
);
462 // Prints the raw value (with appropriate formatting) into the
463 // character string out.
464 int64_t ataPrintSmartAttribRawValue(char *out
,
465 struct ata_smart_attribute
*attribute
,
466 unsigned char *defs
);
468 // Prints Attribute Name for standard SMART attributes. Writes a
469 // 30 byte string with attribute name into output
470 void ataPrintSmartAttribName(char *output
, unsigned char id
, unsigned char *definitions
);
472 // This checks the n'th attribute in the attribute list, NOT the
473 // attribute with id==n. If the attribute does not exist, or the
474 // attribute is > threshold, then returns zero. If the attribute is
475 // <= threshold (failing) then we the attribute number if it is a
476 // prefail attribute. Else we return minus the attribute number if it
477 // is a usage attribute.
478 int ataCheckAttribute(struct ata_smart_values
*data
,
479 struct ata_smart_thresholds_pvt
*thresholds
,
482 // External handler function, for when a checksum is not correct. Can
483 // simply return if no action is desired, or can print error messages
484 // as needed, or exit. Is passed a string with the name of the Data
485 // Structure with the incorrect checksum.
486 void checksumwarning(const char *string
);
488 // Returns raw value of Attribute with ID==id. This will be in the
489 // range 0 to 2^48-1 inclusive. If the Attribute does not exist,
491 int64_t ATAReturnAttributeRawValue(unsigned char id
, struct ata_smart_values
*data
);
493 // Return Temperature Attribute raw value selected according to possible
494 // non-default interpretations. If the Attribute does not exist, return 0
495 unsigned char ATAReturnTemperatureValue(/*const*/ struct ata_smart_values
*data
, const unsigned char *defs
);
498 // This are the meanings of the Self-test failure checkpoint byte.
499 // This is in the self-test log at offset 4 bytes into the self-test
500 // descriptor and in the SMART READ DATA structure at byte offset
501 // 371. These codes are not well documented. The meanings returned by
502 // this routine are used (at least) by Maxtor and IBM. Returns NULL if
504 const char *SelfTestFailureCodeName(unsigned char which
);
507 #define MAX_ATTRIBUTE_NUM 256
509 extern const char *vendorattributeargs
[];
511 // function to parse pairs like "9,minutes" or "220,temp". See end of
512 // extern.h for definition of defs[]. Returns 0 if pair recognized,
513 // else 1 if there is a problem. Allocates memory for array if the
514 // array address is *defs==NULL.
515 int parse_attribute_def(char *pair
, unsigned char **defs
);
517 // Function to return a string containing a list of the arguments in
518 // vendorattributeargs[]. Returns NULL if the required memory can't
520 char *create_vendor_attribute_arg_list(void);
523 // These are two of the functions that are defined in os_*.c and need
524 // to be ported to get smartmontools onto another OS.
525 int ata_command_interface(int device
, smart_command_set command
, int select
, char *data
);
526 int escalade_command_interface(int fd
, int escalade_port
, int escalade_type
, smart_command_set command
, int select
, char *data
);
527 int marvell_command_interface(int device
, smart_command_set command
, int select
, char *data
);
528 int highpoint_command_interface(int device
, smart_command_set command
, int select
, char *data
);
529 // Optional functions of os_*.c
530 #ifdef HAVE_ATA_IDENTIFY_IS_CACHED
531 // Return true if OS caches the ATA identify sector
532 int ata_identify_is_cached(int fd
);
535 // This function is exported to give low-level capability
536 int smartcommandhandler(int device
, smart_command_set command
, int select
, char *data
);
539 void swap2(char *location
);
540 void swap4(char *location
);
541 void swap8(char *location
);
542 #endif /* ATACMDS_H_ */