X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=atacmds.h;h=5ac43ffc80228f601b5c6baabbe86975286cc374;hb=6c5eb127c873bd2414f28b25ba1f9bae494cf516;hp=01698d22cecba7fe925d980e6efd73daa5c339eb;hpb=832b75ede04683f4f493716791e44174e2d3694d;p=mirror_smartmontools-debian.git diff --git a/atacmds.h b/atacmds.h index 01698d2..5ac43ff 100644 --- a/atacmds.h +++ b/atacmds.h @@ -3,7 +3,8 @@ * * Home page of code is: http://smartmontools.sourceforge.net * - * Copyright (C) 2002-6 Bruce Allen + * Copyright (C) 2002-11 Bruce Allen + * Copyright (C) 2008-15 Christian Franke * Copyright (C) 1999-2000 Michael Cornwell * * This program is free software; you can redistribute it and/or modify @@ -12,8 +13,7 @@ * any later version. * * You should have received a copy of the GNU General Public License - * (for example COPYING); if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * (for example COPYING); If not, see . * * This code was originally developed as a Senior Thesis by Michael Cornwell * at the Concurrent Systems Laboratory (now part of the Storage Systems @@ -25,7 +25,9 @@ #ifndef ATACMDS_H_ #define ATACMDS_H_ -#define ATACMDS_H_CVSID "$Id: atacmds.h,v 1.81 2006/04/12 14:54:28 ballen4705 Exp $\n" +#define ATACMDS_H_CVSID "$Id: atacmds.h 4048 2015-03-29 16:09:04Z chrfranke $" + +#include "dev_interface.h" // ata_device // Macro to check expected size of struct at compile time using a // dummy typedef. On size mismatch, compiler reports a negative array @@ -64,11 +66,29 @@ typedef enum { WRITE_LOG } smart_command_set; + // ATA Specification Command Register Values (Commands) -#define ATA_IDENTIFY_DEVICE 0xec +#define ATA_CHECK_POWER_MODE 0xe5 +#define ATA_IDENTIFY_DEVICE 0xec #define ATA_IDENTIFY_PACKET_DEVICE 0xa1 +#define ATA_IDLE 0xe3 #define ATA_SMART_CMD 0xb0 -#define ATA_CHECK_POWER_MODE 0xe5 +#define ATA_SECURITY_FREEZE_LOCK 0xf5 +#define ATA_SET_FEATURES 0xef +#define ATA_STANDBY_IMMEDIATE 0xe0 + +// SET_FEATURES subcommands +#define ATA_DISABLE_AAM 0xc2 +#define ATA_DISABLE_APM 0x85 +#define ATA_DISABLE_WRITE_CACHE 0x82 +#define ATA_DISABLE_READ_LOOK_AHEAD 0x55 +#define ATA_ENABLE_AAM 0x42 +#define ATA_ENABLE_APM 0x05 +#define ATA_ENABLE_WRITE_CACHE 0x02 +#define ATA_ENABLE_READ_LOOK_AHEAD 0xaa + +// 48-bit commands +#define ATA_READ_LOG_EXT 0x2F // ATA Specification Feature Register Values (SMART Subcommands). // Note that some are obsolete as of ATA-7. @@ -182,11 +202,12 @@ ASSERT_SIZEOF_STRUCT(ata_smart_attribute, 12); // 5: Selfpereserving bit #define ATTRIBUTE_FLAGS_SELFPRESERVING(x) (x & 0x20) +// 6-15: Reserved for future use +#define ATTRIBUTE_FLAGS_OTHER(x) ((x) & 0xffc0) -// Last ten bits are reserved for future use -/* ata_smart_values is format of the read drive Attribute command */ -/* see Table 34 of T13/1321D Rev 1 spec (Device SMART data structure) for *some* info */ +// Format of data returned by SMART READ DATA +// Table 62 of T13/1699-D (ATA8-ACS) Revision 6a, September 2008 #pragma pack(1) struct ata_smart_values { unsigned short int revnumber; @@ -200,9 +221,10 @@ struct ata_smart_values { unsigned char errorlog_capability; unsigned char vendor_specific_371; // Maxtor, IBM: self-test failure checkpoint see below! unsigned char short_test_completion_time; - unsigned char extend_test_completion_time; + unsigned char extend_test_completion_time_b; // If 0xff, use 16-bit value below unsigned char conveyance_test_completion_time; - unsigned char reserved_375_385[11]; + unsigned short extend_test_completion_time_w; // e04130r2, added to T13/1699-D Revision 1c, April 2005 + unsigned char reserved_377_385[9]; unsigned char vendor_specific_386_510[125]; // Maxtor bytes 508-509 Attribute/Threshold Revision # unsigned char chksum; } ATTR_PACKED; @@ -298,6 +320,90 @@ struct ata_smart_errorlog { #pragma pack() ASSERT_SIZEOF_STRUCT(ata_smart_errorlog, 512); + +// Extended Comprehensive SMART Error Log data structures +// See Section A.7 of +// AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS) +// T13/1699-D Revision 6a (Working Draft), September 6, 2008. + +// Command data structure +// Table A.9 of T13/1699-D Revision 6a +#pragma pack(1) +struct ata_smart_exterrlog_command +{ + unsigned char device_control_register; + unsigned char features_register; + unsigned char features_register_hi; + unsigned char count_register; + unsigned char count_register_hi; + unsigned char lba_low_register; + unsigned char lba_low_register_hi; + unsigned char lba_mid_register; + unsigned char lba_mid_register_hi; + unsigned char lba_high_register; + unsigned char lba_high_register_hi; + unsigned char device_register; + unsigned char command_register; + + unsigned char reserved; + unsigned int timestamp; +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog_command, 18); + +// Error data structure +// Table A.10 T13/1699-D Revision 6a +#pragma pack(1) +struct ata_smart_exterrlog_error +{ + unsigned char device_control_register; + unsigned char error_register; + unsigned char count_register; + unsigned char count_register_hi; + unsigned char lba_low_register; + unsigned char lba_low_register_hi; + unsigned char lba_mid_register; + unsigned char lba_mid_register_hi; + unsigned char lba_high_register; + unsigned char lba_high_register_hi; + unsigned char device_register; + unsigned char status_register; + + unsigned char extended_error[19]; + unsigned char state; + unsigned short timestamp; +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog_error, 34); + +// Error log data structure +// Table A.8 of T13/1699-D Revision 6a +#pragma pack(1) +struct ata_smart_exterrlog_error_log +{ + ata_smart_exterrlog_command commands[5]; + ata_smart_exterrlog_error error; +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog_error_log, 124); + +// Ext. Comprehensive SMART error log +// Table A.7 of T13/1699-D Revision 6a +#pragma pack(1) +struct ata_smart_exterrlog +{ + unsigned char version; + unsigned char reserved1; + unsigned short error_log_index; + ata_smart_exterrlog_error_log error_logs[4]; + unsigned short device_error_count; + unsigned char reserved2[9]; + unsigned char checksum; +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog, 512); + + // Table 45 of T13/1321D Rev 1 spec (Self-test log descriptor entry) #pragma pack(1) struct ata_smart_selftestlog_struct { @@ -324,6 +430,42 @@ struct ata_smart_selftestlog { #pragma pack() ASSERT_SIZEOF_STRUCT(ata_smart_selftestlog, 512); +// Extended SMART Self-test log data structures +// See Section A.8 of +// AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS) +// T13/1699-D Revision 6a (Working Draft), September 6, 2008. + +// Extended Self-test log descriptor entry +// Table A.13 of T13/1699-D Revision 6a +#pragma pack(1) +struct ata_smart_extselftestlog_desc +{ + unsigned char self_test_type; + unsigned char self_test_status; + unsigned short timestamp; + unsigned char checkpoint; + unsigned char failing_lba[6]; + unsigned char vendorspecific[15]; +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_smart_extselftestlog_desc, 26); + +// Extended Self-test log data structure +// Table A.12 of T13/1699-D Revision 6a +#pragma pack(1) +struct ata_smart_extselftestlog +{ + unsigned char version; + unsigned char reserved1; + unsigned short log_desc_index; + struct ata_smart_extselftestlog_desc log_descs[19]; + unsigned char vendor_specifc[2]; + unsigned char reserved2[11]; + unsigned char chksum; +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_smart_extselftestlog, 512); + // SMART LOG DIRECTORY Table 52 of T13/1532D Vol 1 Rev 1a #pragma pack(1) struct ata_smart_log_entry { @@ -372,112 +514,406 @@ ASSERT_SIZEOF_STRUCT(ata_selective_self_test_log, 512); #define SELECTIVE_FLAG_PENDING (0x0008) #define SELECTIVE_FLAG_ACTIVE (0x0010) + +// SCT (SMART Command Transport) data structures +// See Sections 8.2 and 8.3 of: +// AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS) +// T13/1699-D Revision 3f (Working Draft), December 11, 2006. + +// SCT Status response (read with SMART_READ_LOG page 0xe0) +// Table 182 of T13/BSR INCITS 529 (ACS-4) Revision 04, August 25, 2014 +#pragma pack(1) +struct ata_sct_status_response +{ + unsigned short format_version; // 0-1: Status response format version number (2, 3) + unsigned short sct_version; // 2-3: Vendor specific version number + unsigned short sct_spec; // 4-5: SCT level supported (1) + unsigned int status_flags; // 6-9: Status flags (Bit 0: Segment initialized, Bits 1-31: reserved) + unsigned char device_state; // 10: Device State (0-5) + unsigned char bytes011_013[3]; // 11-13: reserved + unsigned short ext_status_code; // 14-15: Status of last SCT command (0xffff if executing) + unsigned short action_code; // 16-17: Action code of last SCT command + unsigned short function_code; // 18-19: Function code of last SCT command + unsigned char bytes020_039[20]; // 20-39: reserved + uint64_t lba_current; // 40-47: LBA of SCT command executing in background + unsigned char bytes048_199[152]; // 48-199: reserved + signed char hda_temp; // 200: Current temperature in Celsius (0x80 = invalid) + signed char min_temp; // 201: Minimum temperature this power cycle + signed char max_temp; // 202: Maximum temperature this power cycle + signed char life_min_temp; // 203: Minimum lifetime temperature + signed char life_max_temp; // 204: Maximum lifetime temperature + unsigned char byte205; // 205: reserved (T13/e06152r0-2: Average lifetime temperature) + unsigned int over_limit_count; // 206-209: # intervals since last reset with temperature > Max Op Limit + unsigned int under_limit_count; // 210-213: # intervals since last reset with temperature < Min Op Limit + unsigned short smart_status; // 214-215: LBA(32:8) of SMART RETURN STATUS (0, 0x2cf4, 0xc24f) (ACS-4) + unsigned char bytes216_479[479-216+1]; // 216-479: reserved + unsigned char vendor_specific[32];// 480-511: vendor specific +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_sct_status_response, 512); + +// SCT Error Recovery Control command (send with SMART_WRITE_LOG page 0xe0) +// Table 88 of T13/1699-D Revision 6a +#pragma pack(1) +struct ata_sct_error_recovery_control_command +{ + unsigned short action_code; // 3 = Error Recovery Control + unsigned short function_code; // 1 = Set, 2 = Return + unsigned short selection_code; // 1 = Read Timer, 2 = Write Timer + unsigned short time_limit; // If set: Recovery time limit in 100ms units + unsigned short words004_255[252]; // reserved +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_sct_error_recovery_control_command, 512); + +// SCT Feature Control command (send with SMART_WRITE_LOG page 0xe0) +// Table 72 of T13/1699-D Revision 3f +#pragma pack(1) +struct ata_sct_feature_control_command +{ + unsigned short action_code; // 4 = Feature Control + unsigned short function_code; // 1 = Set, 2 = Return, 3 = Return options + unsigned short feature_code; // 3 = Temperature logging interval + unsigned short state; // Interval + unsigned short option_flags; // Bit 0: persistent, Bits 1-15: reserved + unsigned short words005_255[251]; // reserved +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_sct_feature_control_command, 512); + +// SCT Data Table command (send with SMART_WRITE_LOG page 0xe0) +// Table 73 of T13/1699-D Revision 3f +#pragma pack(1) +struct ata_sct_data_table_command +{ + unsigned short action_code; // 5 = Data Table + unsigned short function_code; // 1 = Read Table + unsigned short table_id; // 2 = Temperature History + unsigned short words003_255[253]; // reserved +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_sct_data_table_command, 512); + +// SCT Temperature History Table (read with SMART_READ_LOG page 0xe1) +// Table 75 of T13/1699-D Revision 3f +#pragma pack(1) +struct ata_sct_temperature_history_table +{ + unsigned short format_version; // 0-1: Data table format version number (2) + unsigned short sampling_period; // 2-3: Temperature sampling period in minutes + unsigned short interval; // 4-5: Timer interval between history entries + signed char max_op_limit; // 6: Maximum recommended continuous operating temperature + signed char over_limit; // 7: Maximum temperature limit + signed char min_op_limit; // 8: Minimum recommended continuous operating limit + signed char under_limit; // 9: Minimum temperature limit + unsigned char bytes010_029[20]; // 10-29: reserved + unsigned short cb_size; // 30-31: Number of history entries (range 128-478) + unsigned short cb_index; // 32-33: Index of last updated entry (zero-based) + signed char cb[478]; // 34-(34+cb_size-1): Circular buffer of temperature values +} ATTR_PACKED; +#pragma pack() +ASSERT_SIZEOF_STRUCT(ata_sct_temperature_history_table, 512); + +// Possible values for span_args.mode +enum { + SEL_RANGE, // MIN-MAX + SEL_REDO, // redo this + SEL_NEXT, // do next range + SEL_CONT // redo or next depending of last test status +}; + +// Arguments for selective self-test +struct ata_selective_selftest_args +{ + // Arguments for each span + struct span_args + { + uint64_t start; // First block + uint64_t end; // Last block + int mode; // SEL_*, see above + + span_args() + : start(0), end(0), mode(SEL_RANGE) { } + }; + + span_args span[5]; // Range and mode for 5 spans + int num_spans; // Number of spans + int pending_time; // One plus time in minutes to wait after powerup before restarting + // interrupted offline scan after selective self-test. + int scan_after_select; // Run offline scan after selective self-test: + // 0: don't change, + // 1: turn off scan after selective self-test, + // 2: turn on scan after selective self-test. + + ata_selective_selftest_args() + : num_spans(0), pending_time(0), scan_after_select(0) { } +}; + +// Priority for vendor attribute defs +enum ata_vendor_def_prior +{ + PRIOR_DEFAULT, + PRIOR_DATABASE, + PRIOR_USER +}; + +// Raw attribute value print formats +enum ata_attr_raw_format +{ + RAWFMT_DEFAULT, + RAWFMT_RAW8, + RAWFMT_RAW16, + RAWFMT_RAW48, + RAWFMT_HEX48, + RAWFMT_RAW56, + RAWFMT_HEX56, + RAWFMT_RAW64, + RAWFMT_HEX64, + RAWFMT_RAW16_OPT_RAW16, + RAWFMT_RAW16_OPT_AVG16, + RAWFMT_RAW24_OPT_RAW8, + RAWFMT_RAW24_DIV_RAW24, + RAWFMT_RAW24_DIV_RAW32, + RAWFMT_SEC2HOUR, + RAWFMT_MIN2HOUR, + RAWFMT_HALFMIN2HOUR, + RAWFMT_MSEC24_HOUR32, + RAWFMT_TEMPMINMAX, + RAWFMT_TEMP10X, +}; + +// Attribute flags +enum { + ATTRFLAG_INCREASING = 0x01, // Value not reset (for reallocated/pending counts) + ATTRFLAG_NO_NORMVAL = 0x02, // Normalized value not valid + ATTRFLAG_NO_WORSTVAL = 0x04 // Worst value not valid +}; + +// Vendor attribute display defs for all attribute ids +class ata_vendor_attr_defs +{ +public: + struct entry + { + std::string name; // Attribute name, empty for default + ata_attr_raw_format raw_format; // Raw value print format + ata_vendor_def_prior priority; // Setting priority + unsigned flags; // ATTRFLAG_* + char byteorder[8+1]; // String [012345rvwz] to define byte order + + entry() + : raw_format(RAWFMT_DEFAULT), + priority(PRIOR_DEFAULT), + flags(0) + { byteorder[0] = 0; } + }; + + entry & operator[](unsigned char id) + { return m_defs[id]; } + + const entry & operator[](unsigned char id) const + { return m_defs[id]; } + +private: + entry m_defs[256]; +}; + + +// Possible values for firmwarebugs +enum firmwarebug_t { + BUG_NONE = 0, + BUG_NOLOGDIR, + BUG_SAMSUNG, + BUG_SAMSUNG2, + BUG_SAMSUNG3, + BUG_XERRORLBA +}; + +// Set of firmware bugs +class firmwarebug_defs +{ +public: + firmwarebug_defs() + : m_bugs(0) { } + + bool is_set(firmwarebug_t bug) const + { return !!(m_bugs & (1 << bug)); } + + void set(firmwarebug_t bug) + { m_bugs |= (1 << bug); } + + void set(firmwarebug_defs bugs) + { m_bugs |= bugs.m_bugs; } + +private: + unsigned m_bugs; +}; + + +// Print ATA debug messages? +extern unsigned char ata_debugmode; + +// Suppress serial number? +extern bool dont_print_serial_number; + // Get information from drive -int ataReadHDIdentity(int device, struct ata_identify_device *buf); -int ataCheckPowerMode(int device); +int ata_read_identity(ata_device * device, ata_identify_device * buf, bool fix_swapped_id, + unsigned char * raw_buf = 0); +int ataCheckPowerMode(ata_device * device); + +// Issue a no-data ATA command with optional sector count register value +bool ata_nodata_command(ata_device * device, unsigned char command, int sector_count = -1); + +// Issue SET FEATURES command with optional sector count register value +bool ata_set_features(ata_device * device, unsigned char features, int sector_count = -1); /* Read S.M.A.R.T information from drive */ -int ataReadSmartValues(int device,struct ata_smart_values *); -int ataReadSmartThresholds(int device, struct ata_smart_thresholds_pvt *); -int ataReadErrorLog(int device, struct ata_smart_errorlog *); -int ataReadSelfTestLog(int device, struct ata_smart_selftestlog *); -int ataReadSelectiveSelfTestLog(int device, struct ata_selective_self_test_log *data); -int ataSmartStatus(int device); -int ataSetSmartThresholds(int device, struct ata_smart_thresholds_pvt *); -int ataReadLogDirectory(int device, struct ata_smart_log_directory *); +int ataReadSmartValues(ata_device * device,struct ata_smart_values *); +int ataReadSmartThresholds(ata_device * device, struct ata_smart_thresholds_pvt *); +int ataReadErrorLog (ata_device * device, ata_smart_errorlog *data, + firmwarebug_defs firmwarebugs); +int ataReadSelfTestLog(ata_device * device, ata_smart_selftestlog * data, + firmwarebug_defs firmwarebugs); +int ataReadSelectiveSelfTestLog(ata_device * device, struct ata_selective_self_test_log *data); +int ataReadLogDirectory(ata_device * device, ata_smart_log_directory *, bool gpl); + +// Read GP Log page(s) +bool ataReadLogExt(ata_device * device, unsigned char logaddr, + unsigned char features, unsigned page, + void * data, unsigned nsectors); +// Read SMART Log page(s) +bool ataReadSmartLog(ata_device * device, unsigned char logaddr, + void * data, unsigned nsectors); +// Read SMART Extended Comprehensive Error Log +bool ataReadExtErrorLog(ata_device * device, ata_smart_exterrlog * log, + unsigned page, unsigned nsectors, firmwarebug_defs firmwarebugs); +// Read SMART Extended Self-test Log +bool ataReadExtSelfTestLog(ata_device * device, ata_smart_extselftestlog * log, + unsigned nsectors); + +// Read SCT information +int ataReadSCTStatus(ata_device * device, ata_sct_status_response * sts); +int ataReadSCTTempHist(ata_device * device, ata_sct_temperature_history_table * tmh, + ata_sct_status_response * sts); +// Set SCT temperature logging interval +int ataSetSCTTempInterval(ata_device * device, unsigned interval, bool persistent); + +// Get/Set SCT Error Recovery Control +int ataGetSCTErrorRecoveryControltime(ata_device * device, unsigned type, unsigned short & time_limit); +int ataSetSCTErrorRecoveryControltime(ata_device * device, unsigned type, unsigned short time_limit); + /* Enable/Disable SMART on device */ -int ataEnableSmart ( int device ); -int ataDisableSmart (int device ); -int ataEnableAutoSave(int device); -int ataDisableAutoSave(int device); +int ataEnableSmart (ata_device * device); +int ataDisableSmart (ata_device * device); +int ataEnableAutoSave(ata_device * device); +int ataDisableAutoSave(ata_device * device); /* Automatic Offline Testing */ -int ataEnableAutoOffline ( int device ); -int ataDisableAutoOffline (int device ); +int ataEnableAutoOffline (ata_device * device); +int ataDisableAutoOffline (ata_device * device); /* S.M.A.R.T. test commands */ -int ataSmartOfflineTest (int device); -int ataSmartExtendSelfTest (int device); -int ataSmartShortSelfTest (int device); -int ataSmartShortCapSelfTest (int device); -int ataSmartExtendCapSelfTest (int device); -int ataSmartSelfTestAbort (int device); +int ataSmartTest(ata_device * device, int testtype, bool force, + const ata_selective_selftest_args & args, + const ata_smart_values * sv, uint64_t num_sectors); + +int ataWriteSelectiveSelfTestLog(ata_device * device, ata_selective_selftest_args & args, + const ata_smart_values * sv, uint64_t num_sectors, + const ata_selective_selftest_args * prev_spans = 0); + +// Get World Wide Name (WWN) fields. +// Return NAA field or -1 if WWN is unsupported. +int ata_get_wwn(const ata_identify_device * id, unsigned & oui, uint64_t & unique_id); -// Returns the latest compatibility of ATA/ATAPI Version the device -// supports. Returns -1 if Version command is not supported -int ataVersionInfo (const char **description, struct ata_identify_device *drive, unsigned short *minor); +// Get nominal media rotation rate. +// Returns: 0 = not reported, 1 = SSD, >1 = HDD rpm, < 0 = -(Unknown value) +int ata_get_rotation_rate(const ata_identify_device * id); // If SMART supported, this is guaranteed to return 1 if SMART is enabled, else 0. -int ataDoesSmartWork(int device); +int ataDoesSmartWork(ata_device * device); // returns 1 if SMART supported, 0 if not supported or can't tell -int ataSmartSupport ( struct ata_identify_device *drive); +int ataSmartSupport(const ata_identify_device * drive); + +// Return values: +// 1: Write Cache Reordering enabled +// 2: Write Cache Reordering disabled +// -1: error +int ataGetSetSCTWriteCacheReordering(ata_device * device, bool enable, bool persistent, bool set); // Return values: // 1: SMART enabled // 0: SMART disabled // -1: can't tell if SMART is enabled -- try issuing ataDoesSmartWork command to see -int ataIsSmartEnabled(struct ata_identify_device *drive); +int ataIsSmartEnabled(const ata_identify_device * drive); + +int ataSmartStatus2(ata_device * device); -/* Check SMART for Threshold failure */ -// onlyfailed=0 : are or were any age or prefailure attributes <= threshold -// onlyfailed=1: are any prefailure attributes <= threshold now -int ataCheckSmart ( struct ata_smart_values *data, struct ata_smart_thresholds_pvt *thresholds, int onlyfailed); +int isSmartErrorLogCapable(const ata_smart_values * data, const ata_identify_device * identity); -int ataSmartStatus2(int device); +int isSmartTestLogCapable(const ata_smart_values * data, const ata_identify_device * identity); -// int isOfflineTestTime ( struct ata_smart_values data) -// returns S.M.A.R.T. Offline Test Time in seconds -int isOfflineTestTime ( struct ata_smart_values *data); +int isGeneralPurposeLoggingCapable(const ata_identify_device * identity); -int isShortSelfTestTime ( struct ata_smart_values *data); +int isSupportExecuteOfflineImmediate(const ata_smart_values * data); -int isExtendedSelfTestTime ( struct ata_smart_values *data); +int isSupportAutomaticTimer(const ata_smart_values * data); -int isSmartErrorLogCapable(struct ata_smart_values *data, struct ata_identify_device *identity); +int isSupportOfflineAbort(const ata_smart_values * data); -int isSmartTestLogCapable(struct ata_smart_values *data, struct ata_identify_device *identity); +int isSupportOfflineSurfaceScan(const ata_smart_values * data); -int isGeneralPurposeLoggingCapable(struct ata_identify_device *identity); +int isSupportSelfTest(const ata_smart_values * data); -int isSupportExecuteOfflineImmediate ( struct ata_smart_values *data); +int isSupportConveyanceSelfTest(const ata_smart_values * data); -int isSupportAutomaticTimer ( struct ata_smart_values *data); +int isSupportSelectiveSelfTest(const ata_smart_values * data); -int isSupportOfflineAbort ( struct ata_smart_values *data); +inline bool isSCTCapable(const ata_identify_device *drive) + { return !!(drive->words088_255[206-88] & 0x01); } // 0x01 = SCT support -int isSupportOfflineSurfaceScan ( struct ata_smart_values *data); +inline bool isSCTErrorRecoveryControlCapable(const ata_identify_device *drive) + { return ((drive->words088_255[206-88] & 0x09) == 0x09); } // 0x08 = SCT Error Recovery Control support -int isSupportSelfTest (struct ata_smart_values *data); +inline bool isSCTFeatureControlCapable(const ata_identify_device *drive) + { return ((drive->words088_255[206-88] & 0x11) == 0x11); } // 0x10 = SCT Feature Control support -int isSupportConveyanceSelfTest(struct ata_smart_values *data); +inline bool isSCTDataTableCapable(const ata_identify_device *drive) + { return ((drive->words088_255[206-88] & 0x21) == 0x21); } // 0x20 = SCT Data Table support -int isSupportSelectiveSelfTest(struct ata_smart_values *data); +int TestTime(const ata_smart_values * data, int testtype); -int ataSmartTest(int device, int testtype, struct ata_smart_values *data); +// Attribute state +enum ata_attr_state +{ + ATTRSTATE_NON_EXISTING, // No such Attribute + ATTRSTATE_NO_NORMVAL, // Normalized value not valid + ATTRSTATE_NO_THRESHOLD, // Unknown or no threshold + ATTRSTATE_OK, // Never failed + ATTRSTATE_FAILED_PAST, // Failed in the past + ATTRSTATE_FAILED_NOW // Failed now +}; -int TestTime(struct ata_smart_values *data,int testtype); +// Get attribute state +ata_attr_state ata_get_attr_state(const ata_smart_attribute & attr, + int attridx, + const ata_smart_threshold_entry * thresholds, + const ata_vendor_attr_defs & defs, + unsigned char * threshval = 0); -// Prints the raw value (with appropriate formatting) into the -// character string out. -int64_t ataPrintSmartAttribRawValue(char *out, - struct ata_smart_attribute *attribute, - unsigned char *defs); +// Get attribute raw value. +uint64_t ata_get_attr_raw_value(const ata_smart_attribute & attr, + const ata_vendor_attr_defs & defs); -// Prints Attribute Name for standard SMART attributes. Writes a -// 30 byte string with attribute name into output -void ataPrintSmartAttribName(char *output, unsigned char id, unsigned char *definitions); +// Format attribute raw value. +std::string ata_format_attr_raw_value(const ata_smart_attribute & attr, + const ata_vendor_attr_defs & defs); -// This checks the n'th attribute in the attribute list, NOT the -// attribute with id==n. If the attribute does not exist, or the -// attribute is > threshold, then returns zero. If the attribute is -// <= threshold (failing) then we the attribute number if it is a -// prefail attribute. Else we return minus the attribute number if it -// is a usage attribute. -int ataCheckAttribute(struct ata_smart_values *data, - struct ata_smart_thresholds_pvt *thresholds, - int n); +// Get attribute name +std::string ata_get_smart_attr_name(unsigned char id, + const ata_vendor_attr_defs & defs, + int rpm = 0); // External handler function, for when a checksum is not correct. Can // simply return if no action is desired, or can print error messages @@ -485,53 +921,97 @@ int ataCheckAttribute(struct ata_smart_values *data, // Structure with the incorrect checksum. void checksumwarning(const char *string); -// Returns raw value of Attribute with ID==id. This will be in the -// range 0 to 2^48-1 inclusive. If the Attribute does not exist, -// return -1. -int64_t ATAReturnAttributeRawValue(unsigned char id, struct ata_smart_values *data); +// Find attribute index for attribute id, -1 if not found. +int ata_find_attr_index(unsigned char id, const ata_smart_values & smartval); - -// This are the meanings of the Self-test failure checkpoint byte. -// This is in the self-test log at offset 4 bytes into the self-test -// descriptor and in the SMART READ DATA structure at byte offset -// 371. These codes are not well documented. The meanings returned by -// this routine are used (at least) by Maxtor and IBM. Returns NULL if -// not recognized. -const char *SelfTestFailureCodeName(unsigned char which); +// Return Temperature Attribute raw value selected according to possible +// non-default interpretations. If the Attribute does not exist, return 0 +unsigned char ata_return_temperature_value(const ata_smart_values * data, const ata_vendor_attr_defs & defs); #define MAX_ATTRIBUTE_NUM 256 -extern const char *vendorattributeargs[]; +// Parse vendor attribute display def (-v option). +// Return false on error. +bool parse_attribute_def(const char * opt, ata_vendor_attr_defs & defs, + ata_vendor_def_prior priority); + +// Get ID and increase flag of current pending or offline +// uncorrectable attribute. +unsigned char get_unc_attr_id(bool offline, const ata_vendor_attr_defs & defs, + bool & increase); -// function to parse pairs like "9,minutes" or "220,temp". See end of -// extern.h for definition of defs[]. Returns 0 if pair recognized, -// else 1 if there is a problem. Allocates memory for array if the -// array address is *defs==NULL. -int parse_attribute_def(char *pair, unsigned char **defs); +// Return a multiline string containing a list of valid arguments for +// parse_attribute_def(). +std::string create_vendor_attribute_arg_list(); -// Function to return a string containing a list of the arguments in -// vendorattributeargs[]. Returns NULL if the required memory can't -// be allocated. -char *create_vendor_attribute_arg_list(void); +// Parse firmwarebug def (-F option). +// Return false on error. +bool parse_firmwarebug_def(const char * opt, firmwarebug_defs & firmwarebugs); + +// Return a string of valid argument words for parse_firmwarebug_def() +const char * get_valid_firmwarebug_args(); // These are two of the functions that are defined in os_*.c and need // to be ported to get smartmontools onto another OS. -int ata_command_interface(int device, smart_command_set command, int select, char *data); -int escalade_command_interface(int fd, int escalade_port, int escalade_type, smart_command_set command, int select, char *data); -int marvell_command_interface(int device, smart_command_set command, int select, char *data); -// Optional functions of os_*.c -#ifdef HAVE_ATA_IDENTIFY_IS_CACHED -// Return true if OS caches the ATA identify sector -int ata_identify_is_cached(int fd); -#endif +// Moved to C++ interface +//int ata_command_interface(int device, smart_command_set command, int select, char *data); +//int escalade_command_interface(int fd, int escalade_port, int escalade_type, smart_command_set command, int select, char *data); +//int marvell_command_interface(int device, smart_command_set command, int select, char *data); +//int highpoint_command_interface(int device, smart_command_set command, int select, char *data); +//int areca_command_interface(int fd, int disknum, smart_command_set command, int select, char *data); + // This function is exported to give low-level capability -int smartcommandhandler(int device, smart_command_set command, int select, char *data); +int smartcommandhandler(ata_device * device, smart_command_set command, int select, char *data); + +// Print one self-test log entry. +// Returns: +// -1: failed self-test +// 1: extended self-test completed without error +// 0: otherwise +int ataPrintSmartSelfTestEntry(unsigned testnum, unsigned char test_type, + unsigned char test_status, + unsigned short timestamp, + uint64_t failing_lba, + bool print_error_only, bool & print_header); + +// Print Smart self-test log, used by smartctl and smartd. +int ataPrintSmartSelfTestlog(const ata_smart_selftestlog * data, bool allentries, + firmwarebug_defs firmwarebugs); + +// Get capacity and sector sizes from IDENTIFY data +struct ata_size_info +{ + uint64_t sectors; + uint64_t capacity; + unsigned log_sector_size; + unsigned phy_sector_size; + unsigned log_sector_offset; +}; + +void ata_get_size_info(const ata_identify_device * id, ata_size_info & sizes); + +// Convenience function for formatting strings from ata_identify_device. +void ata_format_id_string(char * out, const unsigned char * in, int n); // Utility routines. +unsigned char checksum(const void * data); + void swap2(char *location); void swap4(char *location); void swap8(char *location); +// Typesafe variants using overloading +inline void swapx(unsigned short * p) + { swap2((char*)p); } +inline void swapx(unsigned int * p) + { swap4((char*)p); } +inline void swapx(uint64_t * p) + { swap8((char*)p); } + +// Return pseudo-device to parse "smartctl -r ataioctl,2 ..." output +// and simulate an ATA device with same behaviour +ata_device * get_parsed_ata_device(smart_interface * intf, const char * dev_name); + #endif /* ATACMDS_H_ */