]> git.proxmox.com Git - mirror_smartmontools-debian.git/blame - atacmds.h
Imported smartmontools-5.27.cvs20061002
[mirror_smartmontools-debian.git] / atacmds.h
CommitLineData
832b75ed
GG
1/*
2 * atacmds.h
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2002-6 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
8 *
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)
12 * any later version.
13 *
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.
17 *
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/
22 *
23 */
24
25#ifndef ATACMDS_H_
26#define ATACMDS_H_
27
4d59bff9 28#define ATACMDS_H_CVSID "$Id: atacmds.h,v 1.83 2006/08/25 06:06:24 sxzzsf Exp $\n"
832b75ed
GG
29
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
34// your compiler.
35#define ASSERT_SIZEOF_STRUCT(s, n) \
36 typedef char assert_sizeof_struct_##s[(sizeof(struct s) == (n)) ? 1 : -1]
37
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))
42#else
43#define ATTR_PACKED
44#endif
45
46typedef enum {
47 // returns no data, just succeeds or fails
48 ENABLE,
49 DISABLE,
50 AUTOSAVE,
51 IMMEDIATE_OFFLINE,
52 AUTO_OFFLINE,
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:
56 READ_VALUES,
57 READ_THRESHOLDS,
58 READ_LOG,
59 IDENTIFY,
60 PIDENTIFY,
61 // returns 1 byte of data
62 CHECK_POWER_MODE,
63 // writes 512 bytes of data:
64 WRITE_LOG
65} smart_command_set;
66
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
72
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
87// Subcommand)
88#define ATA_SMART_AUTO_OFFLINE 0xdb
89
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)
102
103// Maximum allowed number of SMART Attributes
104#define NUMBER_ATA_SMART_ATTRIBUTES 30
105
106// Needed parts of the ATA DRIVE IDENTIFY Structure. Those labeled
107// word* are NOT used.
108#pragma pack(1)
109struct 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];
125} ATTR_PACKED;
126#pragma pack()
127ASSERT_SIZEOF_STRUCT(ata_identify_device, 512);
128
129/* ata_smart_attribute is the vendor specific in SFF-8035 spec */
130#pragma pack(1)
131struct ata_smart_attribute {
132 unsigned char id;
133 // meaning of flag bits: see MACROS just below
134 // WARNING: MISALIGNED!
135 unsigned short flags;
136 unsigned char current;
137 unsigned char worst;
138 unsigned char raw[6];
139 unsigned char reserv;
140} ATTR_PACKED;
141#pragma pack()
142ASSERT_SIZEOF_STRUCT(ata_smart_attribute, 12);
143
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.
147
148// 0: Prefailure bit
149
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)
159
160// 1: Online bit
161
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)
169
170
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)
175
176// 3: Errorrate type bit
177#define ATTRIBUTE_FLAGS_ERRORRATE(x) (x & 0x08)
178
179// 4: Eventcount bit
180#define ATTRIBUTE_FLAGS_EVENTCOUNT(x) (x & 0x10)
181
182// 5: Selfpereserving bit
183#define ATTRIBUTE_FLAGS_SELFPRESERVING(x) (x & 0x20)
184
185
186// Last ten bits are reserved for future use
187
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 */
190#pragma pack(1)
191struct 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;
208} ATTR_PACKED;
209#pragma pack()
210ASSERT_SIZEOF_STRUCT(ata_smart_values, 512);
211
212/* Maxtor, IBM: self-test failure checkpoint byte meaning:
213 00 - write test
214 01 - servo basic
215 02 - servo random
216 03 - G-list scan
217 04 - Handling damage
218 05 - Read scan
219*/
220
221/* Vendor attribute of SMART Threshold (compare to ata_smart_attribute above) */
222#pragma pack(1)
223struct ata_smart_threshold_entry {
224 unsigned char id;
225 unsigned char threshold;
226 unsigned char reserved[10];
227} ATTR_PACKED;
228#pragma pack()
229ASSERT_SIZEOF_STRUCT(ata_smart_threshold_entry, 12);
230
231/* Format of Read SMART THreshold Command */
232/* Compare to ata_smart_values above */
233#pragma pack(1)
234struct 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;
239} ATTR_PACKED;
240#pragma pack()
241ASSERT_SIZEOF_STRUCT(ata_smart_thresholds_pvt, 512);
242
243
244// Table 42 of T13/1321D Rev 1 spec (Error Data Structure)
245#pragma pack(1)
246struct 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];
256 unsigned char state;
257 unsigned short timestamp;
258} ATTR_PACKED;
259#pragma pack()
260ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_error_struct, 30);
261
262
263// Table 41 of T13/1321D Rev 1 spec (Command Data Structure)
264#pragma pack(1)
265struct 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;
275} ATTR_PACKED;
276#pragma pack()
277ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_command_struct, 12);
278
279// Table 40 of T13/1321D Rev 1 spec (Error log data structure)
280#pragma pack(1)
281struct ata_smart_errorlog_struct {
282 struct ata_smart_errorlog_command_struct commands[5];
283 struct ata_smart_errorlog_error_struct error_struct;
284} ATTR_PACKED;
285#pragma pack()
286ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_struct, 90);
287
288// Table 39 of T13/1321D Rev 1 spec (SMART error log sector)
289#pragma pack(1)
290struct 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;
297} ATTR_PACKED;
298#pragma pack()
299ASSERT_SIZEOF_STRUCT(ata_smart_errorlog, 512);
300
301// Table 45 of T13/1321D Rev 1 spec (Self-test log descriptor entry)
302#pragma pack(1)
303struct 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];
310} ATTR_PACKED;
311#pragma pack()
312ASSERT_SIZEOF_STRUCT(ata_smart_selftestlog_struct, 24);
313
314// Table 44 of T13/1321D Rev 1 spec (Self-test log data structure)
315#pragma pack(1)
316struct 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;
323} ATTR_PACKED;
324#pragma pack()
325ASSERT_SIZEOF_STRUCT(ata_smart_selftestlog, 512);
326
327// SMART LOG DIRECTORY Table 52 of T13/1532D Vol 1 Rev 1a
328#pragma pack(1)
329struct ata_smart_log_entry {
330 unsigned char numsectors;
331 unsigned char reserved;
332} ATTR_PACKED;
333#pragma pack()
334ASSERT_SIZEOF_STRUCT(ata_smart_log_entry, 2);
335
336#pragma pack(1)
337struct ata_smart_log_directory {
338 unsigned short int logversion;
339 struct ata_smart_log_entry entry[255];
340} ATTR_PACKED;
341#pragma pack()
342ASSERT_SIZEOF_STRUCT(ata_smart_log_directory, 512);
343
344// SMART SELECTIVE SELF-TEST LOG Table 61 of T13/1532D Volume 1
345// Revision 3
346#pragma pack(1)
347struct test_span {
348 uint64_t start;
349 uint64_t end;
350} ATTR_PACKED;
351#pragma pack()
352ASSERT_SIZEOF_STRUCT(test_span, 16);
353
354#pragma pack(1)
355struct 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];
360 uint64_t currentlba;
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;
367} ATTR_PACKED;
368#pragma pack()
369ASSERT_SIZEOF_STRUCT(ata_selective_self_test_log, 512);
370
371#define SELECTIVE_FLAG_DOSCAN (0x0002)
372#define SELECTIVE_FLAG_PENDING (0x0008)
373#define SELECTIVE_FLAG_ACTIVE (0x0010)
374
375// Get information from drive
376int ataReadHDIdentity(int device, struct ata_identify_device *buf);
377int ataCheckPowerMode(int device);
378
379/* Read S.M.A.R.T information from drive */
380int ataReadSmartValues(int device,struct ata_smart_values *);
381int ataReadSmartThresholds(int device, struct ata_smart_thresholds_pvt *);
382int ataReadErrorLog(int device, struct ata_smart_errorlog *);
383int ataReadSelfTestLog(int device, struct ata_smart_selftestlog *);
384int ataReadSelectiveSelfTestLog(int device, struct ata_selective_self_test_log *data);
385int ataSmartStatus(int device);
386int ataSetSmartThresholds(int device, struct ata_smart_thresholds_pvt *);
387int ataReadLogDirectory(int device, struct ata_smart_log_directory *);
388
389/* Enable/Disable SMART on device */
390int ataEnableSmart ( int device );
391int ataDisableSmart (int device );
392int ataEnableAutoSave(int device);
393int ataDisableAutoSave(int device);
394
395/* Automatic Offline Testing */
396int ataEnableAutoOffline ( int device );
397int ataDisableAutoOffline (int device );
398
399/* S.M.A.R.T. test commands */
400int ataSmartOfflineTest (int device);
401int ataSmartExtendSelfTest (int device);
402int ataSmartShortSelfTest (int device);
403int ataSmartShortCapSelfTest (int device);
404int ataSmartExtendCapSelfTest (int device);
405int ataSmartSelfTestAbort (int device);
406
407// Returns the latest compatibility of ATA/ATAPI Version the device
408// supports. Returns -1 if Version command is not supported
409int ataVersionInfo (const char **description, struct ata_identify_device *drive, unsigned short *minor);
410
411// If SMART supported, this is guaranteed to return 1 if SMART is enabled, else 0.
412int ataDoesSmartWork(int device);
413
414// returns 1 if SMART supported, 0 if not supported or can't tell
415int ataSmartSupport ( struct ata_identify_device *drive);
416
417// Return values:
418// 1: SMART enabled
419// 0: SMART disabled
420// -1: can't tell if SMART is enabled -- try issuing ataDoesSmartWork command to see
421int ataIsSmartEnabled(struct ata_identify_device *drive);
422
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
426int ataCheckSmart ( struct ata_smart_values *data, struct ata_smart_thresholds_pvt *thresholds, int onlyfailed);
427
428int ataSmartStatus2(int device);
429
430// int isOfflineTestTime ( struct ata_smart_values data)
431// returns S.M.A.R.T. Offline Test Time in seconds
432int isOfflineTestTime ( struct ata_smart_values *data);
433
434int isShortSelfTestTime ( struct ata_smart_values *data);
435
436int isExtendedSelfTestTime ( struct ata_smart_values *data);
437
438int isSmartErrorLogCapable(struct ata_smart_values *data, struct ata_identify_device *identity);
439
440int isSmartTestLogCapable(struct ata_smart_values *data, struct ata_identify_device *identity);
441
442int isGeneralPurposeLoggingCapable(struct ata_identify_device *identity);
443
444int isSupportExecuteOfflineImmediate ( struct ata_smart_values *data);
445
446int isSupportAutomaticTimer ( struct ata_smart_values *data);
447
448int isSupportOfflineAbort ( struct ata_smart_values *data);
449
450int isSupportOfflineSurfaceScan ( struct ata_smart_values *data);
451
452int isSupportSelfTest (struct ata_smart_values *data);
453
454int isSupportConveyanceSelfTest(struct ata_smart_values *data);
455
456int isSupportSelectiveSelfTest(struct ata_smart_values *data);
457
458int ataSmartTest(int device, int testtype, struct ata_smart_values *data);
459
460int TestTime(struct ata_smart_values *data,int testtype);
461
462// Prints the raw value (with appropriate formatting) into the
463// character string out.
464int64_t ataPrintSmartAttribRawValue(char *out,
465 struct ata_smart_attribute *attribute,
466 unsigned char *defs);
467
468// Prints Attribute Name for standard SMART attributes. Writes a
469// 30 byte string with attribute name into output
470void ataPrintSmartAttribName(char *output, unsigned char id, unsigned char *definitions);
471
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.
478int ataCheckAttribute(struct ata_smart_values *data,
479 struct ata_smart_thresholds_pvt *thresholds,
480 int n);
481
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.
486void checksumwarning(const char *string);
487
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,
490// return -1.
491int64_t ATAReturnAttributeRawValue(unsigned char id, struct ata_smart_values *data);
492
4d59bff9
GG
493// Return Temperature Attribute raw value selected according to possible
494// non-default interpretations. If the Attribute does not exist, return 0
495unsigned char ATAReturnTemperatureValue(/*const*/ struct ata_smart_values *data, const unsigned char *defs);
496
832b75ed
GG
497
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
503// not recognized.
504const char *SelfTestFailureCodeName(unsigned char which);
505
506
507#define MAX_ATTRIBUTE_NUM 256
508
509extern const char *vendorattributeargs[];
510
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.
515int parse_attribute_def(char *pair, unsigned char **defs);
516
517// Function to return a string containing a list of the arguments in
518// vendorattributeargs[]. Returns NULL if the required memory can't
519// be allocated.
520char *create_vendor_attribute_arg_list(void);
521
522
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.
525int ata_command_interface(int device, smart_command_set command, int select, char *data);
526int escalade_command_interface(int fd, int escalade_port, int escalade_type, smart_command_set command, int select, char *data);
527int marvell_command_interface(int device, smart_command_set command, int select, char *data);
4d59bff9 528int highpoint_command_interface(int device, smart_command_set command, int select, char *data);
832b75ed
GG
529// Optional functions of os_*.c
530#ifdef HAVE_ATA_IDENTIFY_IS_CACHED
531// Return true if OS caches the ATA identify sector
532int ata_identify_is_cached(int fd);
533#endif
534
535// This function is exported to give low-level capability
536int smartcommandhandler(int device, smart_command_set command, int select, char *data);
537
538// Utility routines.
539void swap2(char *location);
540void swap4(char *location);
541void swap8(char *location);
542#endif /* ATACMDS_H_ */