]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - atacmds.h
cbb20b62628969ccd11ec1adaa5e8c7d45e8c5fa
[mirror_smartmontools-debian.git] / atacmds.h
1 /*
2 * atacmds.h
3 *
4 * Home page of code is: http://www.smartmontools.org
5 *
6 * Copyright (C) 2002-11 Bruce Allen
7 * Copyright (C) 2008-17 Christian Franke
8 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * You should have received a copy of the GNU General Public License
16 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
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
28 #define ATACMDS_H_CVSID "$Id: atacmds.h 4419 2017-04-17 13:20:31Z chrfranke $"
29
30 #include "dev_interface.h" // ata_device
31
32 // Macro to check expected size of struct at compile time using a
33 // dummy typedef. On size mismatch, compiler reports a negative array
34 // size. If you see an error message of this form, it means that the
35 // #pragma pack(1) pragma below is not having the desired effect on
36 // your compiler.
37 #define ASSERT_SIZEOF_STRUCT(s, n) \
38 typedef char assert_sizeof_struct_##s[(sizeof(struct s) == (n)) ? 1 : -1]
39
40 // Add __attribute__((packed)) if compiler supports it
41 // because some gcc versions (at least ARM) lack support of #pragma pack()
42 #ifdef HAVE_ATTR_PACKED
43 #define ATTR_PACKED __attribute__((packed))
44 #else
45 #define ATTR_PACKED
46 #endif
47
48 typedef enum {
49 // returns no data, just succeeds or fails
50 ENABLE,
51 DISABLE,
52 AUTOSAVE,
53 IMMEDIATE_OFFLINE,
54 AUTO_OFFLINE,
55 STATUS, // just says if SMART is working or not
56 STATUS_CHECK, // says if disk's SMART status is healthy, or failing
57 // return 512 bytes of data:
58 READ_VALUES,
59 READ_THRESHOLDS,
60 READ_LOG,
61 IDENTIFY,
62 PIDENTIFY,
63 // returns 1 byte of data
64 CHECK_POWER_MODE,
65 // writes 512 bytes of data:
66 WRITE_LOG
67 } smart_command_set;
68
69
70 // ATA Specification Command Register Values (Commands)
71 #define ATA_CHECK_POWER_MODE 0xe5
72 #define ATA_IDENTIFY_DEVICE 0xec
73 #define ATA_IDENTIFY_PACKET_DEVICE 0xa1
74 #define ATA_IDLE 0xe3
75 #define ATA_SMART_CMD 0xb0
76 #define ATA_SECURITY_FREEZE_LOCK 0xf5
77 #ifndef ATA_SET_FEATURES
78 #define ATA_SET_FEATURES 0xef
79 #endif
80 #define ATA_STANDBY 0xe2
81 #define ATA_STANDBY_IMMEDIATE 0xe0
82
83 // SET_FEATURES subcommands
84 #define ATA_DISABLE_AAM 0xc2
85 #define ATA_DISABLE_APM 0x85
86 #define ATA_DISABLE_WRITE_CACHE 0x82
87 #define ATA_DISABLE_READ_LOOK_AHEAD 0x55
88 #define ATA_ENABLE_AAM 0x42
89 #define ATA_ENABLE_APM 0x05
90 #define ATA_ENABLE_WRITE_CACHE 0x02
91 #define ATA_ENABLE_READ_LOOK_AHEAD 0xaa
92 #define ATA_ENABLE_DISABLE_DSN 0x63
93
94 // 48-bit commands
95 #define ATA_READ_LOG_EXT 0x2F
96 #define ATA_WRITE_LOG_EXT 0x3F
97
98 // ATA Specification Feature Register Values (SMART Subcommands).
99 // Note that some are obsolete as of ATA-7.
100 #define ATA_SMART_READ_VALUES 0xd0
101 #define ATA_SMART_READ_THRESHOLDS 0xd1
102 #define ATA_SMART_AUTOSAVE 0xd2
103 #define ATA_SMART_SAVE 0xd3
104 #define ATA_SMART_IMMEDIATE_OFFLINE 0xd4
105 #define ATA_SMART_READ_LOG_SECTOR 0xd5
106 #define ATA_SMART_WRITE_LOG_SECTOR 0xd6
107 #define ATA_SMART_WRITE_THRESHOLDS 0xd7
108 #define ATA_SMART_ENABLE 0xd8
109 #define ATA_SMART_DISABLE 0xd9
110 #define ATA_SMART_STATUS 0xda
111 // SFF 8035i Revision 2 Specification Feature Register Value (SMART
112 // Subcommand)
113 #define ATA_SMART_AUTO_OFFLINE 0xdb
114
115 // Sector Number values for ATA_SMART_IMMEDIATE_OFFLINE Subcommand
116 #define OFFLINE_FULL_SCAN 0
117 #define SHORT_SELF_TEST 1
118 #define EXTEND_SELF_TEST 2
119 #define CONVEYANCE_SELF_TEST 3
120 #define SELECTIVE_SELF_TEST 4
121 #define ABORT_SELF_TEST 127
122 #define SHORT_CAPTIVE_SELF_TEST 129
123 #define EXTEND_CAPTIVE_SELF_TEST 130
124 #define CONVEYANCE_CAPTIVE_SELF_TEST 131
125 #define SELECTIVE_CAPTIVE_SELF_TEST 132
126 #define CAPTIVE_MASK (0x01<<7)
127
128 // Maximum allowed number of SMART Attributes
129 #define NUMBER_ATA_SMART_ATTRIBUTES 30
130
131 // Needed parts of the ATA DRIVE IDENTIFY Structure. Those labeled
132 // word* are NOT used.
133 #pragma pack(1)
134 struct ata_identify_device {
135 unsigned short words000_009[10];
136 unsigned char serial_no[20];
137 unsigned short words020_022[3];
138 unsigned char fw_rev[8];
139 unsigned char model[40];
140 unsigned short words047_079[33];
141 unsigned short major_rev_num;
142 unsigned short minor_rev_num;
143 unsigned short command_set_1;
144 unsigned short command_set_2;
145 unsigned short command_set_extension;
146 unsigned short cfs_enable_1;
147 unsigned short word086;
148 unsigned short csf_default;
149 unsigned short words088_255[168];
150 } ATTR_PACKED;
151 #pragma pack()
152 ASSERT_SIZEOF_STRUCT(ata_identify_device, 512);
153
154 /* ata_smart_attribute is the vendor specific in SFF-8035 spec */
155 #pragma pack(1)
156 struct ata_smart_attribute {
157 unsigned char id;
158 // meaning of flag bits: see MACROS just below
159 // WARNING: MISALIGNED!
160 unsigned short flags;
161 unsigned char current;
162 unsigned char worst;
163 unsigned char raw[6];
164 unsigned char reserv;
165 } ATTR_PACKED;
166 #pragma pack()
167 ASSERT_SIZEOF_STRUCT(ata_smart_attribute, 12);
168
169 // MACROS to interpret the flags bits in the previous structure.
170 // These have not been implemented using bitflags and a union, to make
171 // it portable across bit/little endian and different platforms.
172
173 // 0: Prefailure bit
174
175 // From SFF 8035i Revision 2 page 19: Bit 0 (pre-failure/advisory bit)
176 // - If the value of this bit equals zero, an attribute value less
177 // than or equal to its corresponding attribute threshold indicates an
178 // advisory condition where the usage or age of the device has
179 // exceeded its intended design life period. If the value of this bit
180 // equals one, an attribute value less than or equal to its
181 // corresponding attribute threshold indicates a prefailure condition
182 // where imminent loss of data is being predicted.
183 #define ATTRIBUTE_FLAGS_PREFAILURE(x) (x & 0x01)
184
185 // 1: Online bit
186
187 // From SFF 8035i Revision 2 page 19: Bit 1 (on-line data collection
188 // bit) - If the value of this bit equals zero, then the attribute
189 // value is updated only during off-line data collection
190 // activities. If the value of this bit equals one, then the attribute
191 // value is updated during normal operation of the device or during
192 // both normal operation and off-line testing.
193 #define ATTRIBUTE_FLAGS_ONLINE(x) (x & 0x02)
194
195
196 // The following are (probably) IBM's, Maxtors and Quantum's definitions for the
197 // vendor-specific bits:
198 // 2: Performance type bit
199 #define ATTRIBUTE_FLAGS_PERFORMANCE(x) (x & 0x04)
200
201 // 3: Errorrate type bit
202 #define ATTRIBUTE_FLAGS_ERRORRATE(x) (x & 0x08)
203
204 // 4: Eventcount bit
205 #define ATTRIBUTE_FLAGS_EVENTCOUNT(x) (x & 0x10)
206
207 // 5: Selfpereserving bit
208 #define ATTRIBUTE_FLAGS_SELFPRESERVING(x) (x & 0x20)
209
210 // 6-15: Reserved for future use
211 #define ATTRIBUTE_FLAGS_OTHER(x) ((x) & 0xffc0)
212
213
214 // Format of data returned by SMART READ DATA
215 // Table 62 of T13/1699-D (ATA8-ACS) Revision 6a, September 2008
216 #pragma pack(1)
217 struct ata_smart_values {
218 unsigned short int revnumber;
219 struct ata_smart_attribute vendor_attributes [NUMBER_ATA_SMART_ATTRIBUTES];
220 unsigned char offline_data_collection_status;
221 unsigned char self_test_exec_status; //IBM # segments for offline collection
222 unsigned short int total_time_to_complete_off_line; // IBM different
223 unsigned char vendor_specific_366; // Maxtor & IBM curent segment pointer
224 unsigned char offline_data_collection_capability;
225 unsigned short int smart_capability;
226 unsigned char errorlog_capability;
227 unsigned char vendor_specific_371; // Maxtor, IBM: self-test failure checkpoint see below!
228 unsigned char short_test_completion_time;
229 unsigned char extend_test_completion_time_b; // If 0xff, use 16-bit value below
230 unsigned char conveyance_test_completion_time;
231 unsigned short extend_test_completion_time_w; // e04130r2, added to T13/1699-D Revision 1c, April 2005
232 unsigned char reserved_377_385[9];
233 unsigned char vendor_specific_386_510[125]; // Maxtor bytes 508-509 Attribute/Threshold Revision #
234 unsigned char chksum;
235 } ATTR_PACKED;
236 #pragma pack()
237 ASSERT_SIZEOF_STRUCT(ata_smart_values, 512);
238
239 /* Maxtor, IBM: self-test failure checkpoint byte meaning:
240 00 - write test
241 01 - servo basic
242 02 - servo random
243 03 - G-list scan
244 04 - Handling damage
245 05 - Read scan
246 */
247
248 /* Vendor attribute of SMART Threshold (compare to ata_smart_attribute above) */
249 #pragma pack(1)
250 struct ata_smart_threshold_entry {
251 unsigned char id;
252 unsigned char threshold;
253 unsigned char reserved[10];
254 } ATTR_PACKED;
255 #pragma pack()
256 ASSERT_SIZEOF_STRUCT(ata_smart_threshold_entry, 12);
257
258 /* Format of Read SMART THreshold Command */
259 /* Compare to ata_smart_values above */
260 #pragma pack(1)
261 struct ata_smart_thresholds_pvt {
262 unsigned short int revnumber;
263 struct ata_smart_threshold_entry thres_entries[NUMBER_ATA_SMART_ATTRIBUTES];
264 unsigned char reserved[149];
265 unsigned char chksum;
266 } ATTR_PACKED;
267 #pragma pack()
268 ASSERT_SIZEOF_STRUCT(ata_smart_thresholds_pvt, 512);
269
270
271 // Table 42 of T13/1321D Rev 1 spec (Error Data Structure)
272 #pragma pack(1)
273 struct ata_smart_errorlog_error_struct {
274 unsigned char reserved;
275 unsigned char error_register;
276 unsigned char sector_count;
277 unsigned char sector_number;
278 unsigned char cylinder_low;
279 unsigned char cylinder_high;
280 unsigned char drive_head;
281 unsigned char status;
282 unsigned char extended_error[19];
283 unsigned char state;
284 unsigned short timestamp;
285 } ATTR_PACKED;
286 #pragma pack()
287 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_error_struct, 30);
288
289
290 // Table 41 of T13/1321D Rev 1 spec (Command Data Structure)
291 #pragma pack(1)
292 struct ata_smart_errorlog_command_struct {
293 unsigned char devicecontrolreg;
294 unsigned char featuresreg;
295 unsigned char sector_count;
296 unsigned char sector_number;
297 unsigned char cylinder_low;
298 unsigned char cylinder_high;
299 unsigned char drive_head;
300 unsigned char commandreg;
301 unsigned int timestamp;
302 } ATTR_PACKED;
303 #pragma pack()
304 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_command_struct, 12);
305
306 // Table 40 of T13/1321D Rev 1 spec (Error log data structure)
307 #pragma pack(1)
308 struct ata_smart_errorlog_struct {
309 struct ata_smart_errorlog_command_struct commands[5];
310 struct ata_smart_errorlog_error_struct error_struct;
311 } ATTR_PACKED;
312 #pragma pack()
313 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog_struct, 90);
314
315 // Table 39 of T13/1321D Rev 1 spec (SMART error log sector)
316 #pragma pack(1)
317 struct ata_smart_errorlog {
318 unsigned char revnumber;
319 unsigned char error_log_pointer;
320 struct ata_smart_errorlog_struct errorlog_struct[5];
321 unsigned short int ata_error_count;
322 unsigned char reserved[57];
323 unsigned char checksum;
324 } ATTR_PACKED;
325 #pragma pack()
326 ASSERT_SIZEOF_STRUCT(ata_smart_errorlog, 512);
327
328
329 // Extended Comprehensive SMART Error Log data structures
330 // See Section A.7 of
331 // AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
332 // T13/1699-D Revision 6a (Working Draft), September 6, 2008.
333
334 // Command data structure
335 // Table A.9 of T13/1699-D Revision 6a
336 #pragma pack(1)
337 struct ata_smart_exterrlog_command
338 {
339 unsigned char device_control_register;
340 unsigned char features_register;
341 unsigned char features_register_hi;
342 unsigned char count_register;
343 unsigned char count_register_hi;
344 unsigned char lba_low_register;
345 unsigned char lba_low_register_hi;
346 unsigned char lba_mid_register;
347 unsigned char lba_mid_register_hi;
348 unsigned char lba_high_register;
349 unsigned char lba_high_register_hi;
350 unsigned char device_register;
351 unsigned char command_register;
352
353 unsigned char reserved;
354 unsigned int timestamp;
355 } ATTR_PACKED;
356 #pragma pack()
357 ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog_command, 18);
358
359 // Error data structure
360 // Table A.10 T13/1699-D Revision 6a
361 #pragma pack(1)
362 struct ata_smart_exterrlog_error
363 {
364 unsigned char device_control_register;
365 unsigned char error_register;
366 unsigned char count_register;
367 unsigned char count_register_hi;
368 unsigned char lba_low_register;
369 unsigned char lba_low_register_hi;
370 unsigned char lba_mid_register;
371 unsigned char lba_mid_register_hi;
372 unsigned char lba_high_register;
373 unsigned char lba_high_register_hi;
374 unsigned char device_register;
375 unsigned char status_register;
376
377 unsigned char extended_error[19];
378 unsigned char state;
379 unsigned short timestamp;
380 } ATTR_PACKED;
381 #pragma pack()
382 ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog_error, 34);
383
384 // Error log data structure
385 // Table A.8 of T13/1699-D Revision 6a
386 #pragma pack(1)
387 struct ata_smart_exterrlog_error_log
388 {
389 ata_smart_exterrlog_command commands[5];
390 ata_smart_exterrlog_error error;
391 } ATTR_PACKED;
392 #pragma pack()
393 ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog_error_log, 124);
394
395 // Ext. Comprehensive SMART error log
396 // Table A.7 of T13/1699-D Revision 6a
397 #pragma pack(1)
398 struct ata_smart_exterrlog
399 {
400 unsigned char version;
401 unsigned char reserved1;
402 unsigned short error_log_index;
403 ata_smart_exterrlog_error_log error_logs[4];
404 unsigned short device_error_count;
405 unsigned char reserved2[9];
406 unsigned char checksum;
407 } ATTR_PACKED;
408 #pragma pack()
409 ASSERT_SIZEOF_STRUCT(ata_smart_exterrlog, 512);
410
411
412 // Table 45 of T13/1321D Rev 1 spec (Self-test log descriptor entry)
413 #pragma pack(1)
414 struct ata_smart_selftestlog_struct {
415 unsigned char selftestnumber; // Sector number register
416 unsigned char selfteststatus;
417 unsigned short int timestamp;
418 unsigned char selftestfailurecheckpoint;
419 unsigned int lbafirstfailure;
420 unsigned char vendorspecific[15];
421 } ATTR_PACKED;
422 #pragma pack()
423 ASSERT_SIZEOF_STRUCT(ata_smart_selftestlog_struct, 24);
424
425 // Table 44 of T13/1321D Rev 1 spec (Self-test log data structure)
426 #pragma pack(1)
427 struct ata_smart_selftestlog {
428 unsigned short int revnumber;
429 struct ata_smart_selftestlog_struct selftest_struct[21];
430 unsigned char vendorspecific[2];
431 unsigned char mostrecenttest;
432 unsigned char reserved[2];
433 unsigned char chksum;
434 } ATTR_PACKED;
435 #pragma pack()
436 ASSERT_SIZEOF_STRUCT(ata_smart_selftestlog, 512);
437
438 // Extended SMART Self-test log data structures
439 // See Section A.8 of
440 // AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
441 // T13/1699-D Revision 6a (Working Draft), September 6, 2008.
442
443 // Extended Self-test log descriptor entry
444 // Table A.13 of T13/1699-D Revision 6a
445 #pragma pack(1)
446 struct ata_smart_extselftestlog_desc
447 {
448 unsigned char self_test_type;
449 unsigned char self_test_status;
450 unsigned short timestamp;
451 unsigned char checkpoint;
452 unsigned char failing_lba[6];
453 unsigned char vendorspecific[15];
454 } ATTR_PACKED;
455 #pragma pack()
456 ASSERT_SIZEOF_STRUCT(ata_smart_extselftestlog_desc, 26);
457
458 // Extended Self-test log data structure
459 // Table A.12 of T13/1699-D Revision 6a
460 #pragma pack(1)
461 struct ata_smart_extselftestlog
462 {
463 unsigned char version;
464 unsigned char reserved1;
465 unsigned short log_desc_index;
466 struct ata_smart_extselftestlog_desc log_descs[19];
467 unsigned char vendor_specifc[2];
468 unsigned char reserved2[11];
469 unsigned char chksum;
470 } ATTR_PACKED;
471 #pragma pack()
472 ASSERT_SIZEOF_STRUCT(ata_smart_extselftestlog, 512);
473
474 // SMART LOG DIRECTORY Table 52 of T13/1532D Vol 1 Rev 1a
475 #pragma pack(1)
476 struct ata_smart_log_entry {
477 unsigned char numsectors;
478 unsigned char reserved;
479 } ATTR_PACKED;
480 #pragma pack()
481 ASSERT_SIZEOF_STRUCT(ata_smart_log_entry, 2);
482
483 #pragma pack(1)
484 struct ata_smart_log_directory {
485 unsigned short int logversion;
486 struct ata_smart_log_entry entry[255];
487 } ATTR_PACKED;
488 #pragma pack()
489 ASSERT_SIZEOF_STRUCT(ata_smart_log_directory, 512);
490
491 // SMART SELECTIVE SELF-TEST LOG Table 61 of T13/1532D Volume 1
492 // Revision 3
493 #pragma pack(1)
494 struct test_span {
495 uint64_t start;
496 uint64_t end;
497 } ATTR_PACKED;
498 #pragma pack()
499 ASSERT_SIZEOF_STRUCT(test_span, 16);
500
501 #pragma pack(1)
502 struct ata_selective_self_test_log {
503 unsigned short logversion;
504 struct test_span span[5];
505 unsigned char reserved1[337-82+1];
506 unsigned char vendor_specific1[491-338+1];
507 uint64_t currentlba;
508 unsigned short currentspan;
509 unsigned short flags;
510 unsigned char vendor_specific2[507-504+1];
511 unsigned short pendingtime;
512 unsigned char reserved2;
513 unsigned char checksum;
514 } ATTR_PACKED;
515 #pragma pack()
516 ASSERT_SIZEOF_STRUCT(ata_selective_self_test_log, 512);
517
518 #define SELECTIVE_FLAG_DOSCAN (0x0002)
519 #define SELECTIVE_FLAG_PENDING (0x0008)
520 #define SELECTIVE_FLAG_ACTIVE (0x0010)
521
522
523 // SCT (SMART Command Transport) data structures
524 // See Sections 8.2 and 8.3 of:
525 // AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
526 // T13/1699-D Revision 3f (Working Draft), December 11, 2006.
527
528 // SCT Status response (read with SMART_READ_LOG page 0xe0)
529 // Table 182 of T13/BSR INCITS 529 (ACS-4) Revision 04, August 25, 2014
530 #pragma pack(1)
531 struct ata_sct_status_response
532 {
533 unsigned short format_version; // 0-1: Status response format version number (2, 3)
534 unsigned short sct_version; // 2-3: Vendor specific version number
535 unsigned short sct_spec; // 4-5: SCT level supported (1)
536 unsigned int status_flags; // 6-9: Status flags (Bit 0: Segment initialized, Bits 1-31: reserved)
537 unsigned char device_state; // 10: Device State (0-5)
538 unsigned char bytes011_013[3]; // 11-13: reserved
539 unsigned short ext_status_code; // 14-15: Status of last SCT command (0xffff if executing)
540 unsigned short action_code; // 16-17: Action code of last SCT command
541 unsigned short function_code; // 18-19: Function code of last SCT command
542 unsigned char bytes020_039[20]; // 20-39: reserved
543 uint64_t lba_current; // 40-47: LBA of SCT command executing in background
544 unsigned char bytes048_199[152]; // 48-199: reserved
545 signed char hda_temp; // 200: Current temperature in Celsius (0x80 = invalid)
546 signed char min_temp; // 201: Minimum temperature this power cycle
547 signed char max_temp; // 202: Maximum temperature this power cycle
548 signed char life_min_temp; // 203: Minimum lifetime temperature
549 signed char life_max_temp; // 204: Maximum lifetime temperature
550 unsigned char byte205; // 205: reserved (T13/e06152r0-2: Average lifetime temperature)
551 unsigned int over_limit_count; // 206-209: # intervals since last reset with temperature > Max Op Limit
552 unsigned int under_limit_count; // 210-213: # intervals since last reset with temperature < Min Op Limit
553 unsigned short smart_status; // 214-215: LBA(32:8) of SMART RETURN STATUS (0, 0x2cf4, 0xc24f) (ACS-4)
554 unsigned short min_erc_time; // 216-217: Minimum supported value for ERC (ACS-4)
555 unsigned char bytes216_479[479-218+1]; // 218-479: reserved
556 unsigned char vendor_specific[32];// 480-511: vendor specific
557 } ATTR_PACKED;
558 #pragma pack()
559 ASSERT_SIZEOF_STRUCT(ata_sct_status_response, 512);
560
561 // SCT Error Recovery Control command (send with SMART_WRITE_LOG page 0xe0)
562 // Table 88 of T13/1699-D Revision 6a
563 #pragma pack(1)
564 struct ata_sct_error_recovery_control_command
565 {
566 unsigned short action_code; // 3 = Error Recovery Control
567 unsigned short function_code; // 1 = Set, 2 = Return
568 unsigned short selection_code; // 1 = Read Timer, 2 = Write Timer
569 unsigned short time_limit; // If set: Recovery time limit in 100ms units
570 unsigned short words004_255[252]; // reserved
571 } ATTR_PACKED;
572 #pragma pack()
573 ASSERT_SIZEOF_STRUCT(ata_sct_error_recovery_control_command, 512);
574
575 // SCT Feature Control command (send with SMART_WRITE_LOG page 0xe0)
576 // Table 72 of T13/1699-D Revision 3f
577 #pragma pack(1)
578 struct ata_sct_feature_control_command
579 {
580 unsigned short action_code; // 4 = Feature Control
581 unsigned short function_code; // 1 = Set, 2 = Return, 3 = Return options
582 unsigned short feature_code; // 3 = Temperature logging interval
583 unsigned short state; // Interval
584 unsigned short option_flags; // Bit 0: persistent, Bits 1-15: reserved
585 unsigned short words005_255[251]; // reserved
586 } ATTR_PACKED;
587 #pragma pack()
588 ASSERT_SIZEOF_STRUCT(ata_sct_feature_control_command, 512);
589
590 // SCT Data Table command (send with SMART_WRITE_LOG page 0xe0)
591 // Table 73 of T13/1699-D Revision 3f
592 #pragma pack(1)
593 struct ata_sct_data_table_command
594 {
595 unsigned short action_code; // 5 = Data Table
596 unsigned short function_code; // 1 = Read Table
597 unsigned short table_id; // 2 = Temperature History
598 unsigned short words003_255[253]; // reserved
599 } ATTR_PACKED;
600 #pragma pack()
601 ASSERT_SIZEOF_STRUCT(ata_sct_data_table_command, 512);
602
603 // SCT Temperature History Table (read with SMART_READ_LOG page 0xe1)
604 // Table 75 of T13/1699-D Revision 3f
605 #pragma pack(1)
606 struct ata_sct_temperature_history_table
607 {
608 unsigned short format_version; // 0-1: Data table format version number (2)
609 unsigned short sampling_period; // 2-3: Temperature sampling period in minutes
610 unsigned short interval; // 4-5: Timer interval between history entries
611 signed char max_op_limit; // 6: Maximum recommended continuous operating temperature
612 signed char over_limit; // 7: Maximum temperature limit
613 signed char min_op_limit; // 8: Minimum recommended continuous operating limit
614 signed char under_limit; // 9: Minimum temperature limit
615 unsigned char bytes010_029[20]; // 10-29: reserved
616 unsigned short cb_size; // 30-31: Number of history entries (range 128-478)
617 unsigned short cb_index; // 32-33: Index of last updated entry (zero-based)
618 signed char cb[478]; // 34-(34+cb_size-1): Circular buffer of temperature values
619 } ATTR_PACKED;
620 #pragma pack()
621 ASSERT_SIZEOF_STRUCT(ata_sct_temperature_history_table, 512);
622
623 // Possible values for span_args.mode
624 enum {
625 SEL_RANGE, // MIN-MAX
626 SEL_REDO, // redo this
627 SEL_NEXT, // do next range
628 SEL_CONT // redo or next depending of last test status
629 };
630
631 // Arguments for selective self-test
632 struct ata_selective_selftest_args
633 {
634 // Arguments for each span
635 struct span_args
636 {
637 uint64_t start; // First block
638 uint64_t end; // Last block
639 int mode; // SEL_*, see above
640
641 span_args()
642 : start(0), end(0), mode(SEL_RANGE) { }
643 };
644
645 span_args span[5]; // Range and mode for 5 spans
646 int num_spans; // Number of spans
647 int pending_time; // One plus time in minutes to wait after powerup before restarting
648 // interrupted offline scan after selective self-test.
649 int scan_after_select; // Run offline scan after selective self-test:
650 // 0: don't change,
651 // 1: turn off scan after selective self-test,
652 // 2: turn on scan after selective self-test.
653
654 ata_selective_selftest_args()
655 : num_spans(0), pending_time(0), scan_after_select(0) { }
656 };
657
658 // Priority for vendor attribute defs
659 enum ata_vendor_def_prior
660 {
661 PRIOR_DEFAULT,
662 PRIOR_DATABASE,
663 PRIOR_USER
664 };
665
666 // Raw attribute value print formats
667 enum ata_attr_raw_format
668 {
669 RAWFMT_DEFAULT,
670 RAWFMT_RAW8,
671 RAWFMT_RAW16,
672 RAWFMT_RAW48,
673 RAWFMT_HEX48,
674 RAWFMT_RAW56,
675 RAWFMT_HEX56,
676 RAWFMT_RAW64,
677 RAWFMT_HEX64,
678 RAWFMT_RAW16_OPT_RAW16,
679 RAWFMT_RAW16_OPT_AVG16,
680 RAWFMT_RAW24_OPT_RAW8,
681 RAWFMT_RAW24_DIV_RAW24,
682 RAWFMT_RAW24_DIV_RAW32,
683 RAWFMT_SEC2HOUR,
684 RAWFMT_MIN2HOUR,
685 RAWFMT_HALFMIN2HOUR,
686 RAWFMT_MSEC24_HOUR32,
687 RAWFMT_TEMPMINMAX,
688 RAWFMT_TEMP10X,
689 };
690
691 // Attribute flags
692 enum {
693 ATTRFLAG_INCREASING = 0x01, // Value not reset (for reallocated/pending counts)
694 ATTRFLAG_NO_NORMVAL = 0x02, // Normalized value not valid
695 ATTRFLAG_NO_WORSTVAL = 0x04, // Worst value not valid
696 ATTRFLAG_HDD_ONLY = 0x08, // DEFAULT setting for HDD only
697 ATTRFLAG_SSD_ONLY = 0x10, // DEFAULT setting for SSD only
698 };
699
700 // Vendor attribute display defs for all attribute ids
701 class ata_vendor_attr_defs
702 {
703 public:
704 struct entry
705 {
706 std::string name; // Attribute name, empty for default
707 ata_attr_raw_format raw_format; // Raw value print format
708 ata_vendor_def_prior priority; // Setting priority
709 unsigned flags; // ATTRFLAG_*
710 char byteorder[8+1]; // String [012345rvwz] to define byte order
711
712 entry()
713 : raw_format(RAWFMT_DEFAULT),
714 priority(PRIOR_DEFAULT),
715 flags(0)
716 { byteorder[0] = 0; }
717 };
718
719 entry & operator[](unsigned char id)
720 { return m_defs[id]; }
721
722 const entry & operator[](unsigned char id) const
723 { return m_defs[id]; }
724
725 private:
726 entry m_defs[256];
727 };
728
729
730 // Possible values for firmwarebugs
731 enum firmwarebug_t {
732 BUG_NONE = 0,
733 BUG_NOLOGDIR,
734 BUG_SAMSUNG,
735 BUG_SAMSUNG2,
736 BUG_SAMSUNG3,
737 BUG_XERRORLBA
738 };
739
740 // Set of firmware bugs
741 class firmwarebug_defs
742 {
743 public:
744 firmwarebug_defs()
745 : m_bugs(0) { }
746
747 bool is_set(firmwarebug_t bug) const
748 { return !!(m_bugs & (1 << bug)); }
749
750 void set(firmwarebug_t bug)
751 { m_bugs |= (1 << bug); }
752
753 void set(firmwarebug_defs bugs)
754 { m_bugs |= bugs.m_bugs; }
755
756 private:
757 unsigned m_bugs;
758 };
759
760
761 // Print ATA debug messages?
762 extern unsigned char ata_debugmode;
763
764 // Suppress serial number?
765 extern bool dont_print_serial_number;
766
767 // Get information from drive
768 int ata_read_identity(ata_device * device, ata_identify_device * buf, bool fix_swapped_id,
769 unsigned char * raw_buf = 0);
770 int ataCheckPowerMode(ata_device * device);
771
772 // Issue a no-data ATA command with optional sector count register value
773 bool ata_nodata_command(ata_device * device, unsigned char command, int sector_count = -1);
774
775 // Issue SET FEATURES command with optional sector count register value
776 bool ata_set_features(ata_device * device, unsigned char features, int sector_count = -1);
777
778 /* Read S.M.A.R.T information from drive */
779 int ataReadSmartValues(ata_device * device,struct ata_smart_values *);
780 int ataReadSmartThresholds(ata_device * device, struct ata_smart_thresholds_pvt *);
781 int ataReadErrorLog (ata_device * device, ata_smart_errorlog *data,
782 firmwarebug_defs firmwarebugs);
783 int ataReadSelfTestLog(ata_device * device, ata_smart_selftestlog * data,
784 firmwarebug_defs firmwarebugs);
785 int ataReadSelectiveSelfTestLog(ata_device * device, struct ata_selective_self_test_log *data);
786 int ataReadLogDirectory(ata_device * device, ata_smart_log_directory *, bool gpl);
787
788 // Write GP Log page(s)
789 bool ataWriteLogExt(ata_device * device, unsigned char logaddr,
790 unsigned page, void * data, unsigned nsectors);
791
792 // Read GP Log page(s)
793 bool ataReadLogExt(ata_device * device, unsigned char logaddr,
794 unsigned char features, unsigned page,
795 void * data, unsigned nsectors);
796 // Read SMART Log page(s)
797 bool ataReadSmartLog(ata_device * device, unsigned char logaddr,
798 void * data, unsigned nsectors);
799 // Read SMART Extended Comprehensive Error Log
800 bool ataReadExtErrorLog(ata_device * device, ata_smart_exterrlog * log,
801 unsigned page, unsigned nsectors, firmwarebug_defs firmwarebugs);
802 // Read SMART Extended Self-test Log
803 bool ataReadExtSelfTestLog(ata_device * device, ata_smart_extselftestlog * log,
804 unsigned nsectors);
805
806 // Read SCT information
807 int ataReadSCTStatus(ata_device * device, ata_sct_status_response * sts);
808 int ataReadSCTTempHist(ata_device * device, ata_sct_temperature_history_table * tmh,
809 ata_sct_status_response * sts);
810 // Set SCT temperature logging interval
811 int ataSetSCTTempInterval(ata_device * device, unsigned interval, bool persistent);
812
813 // Get/Set SCT Error Recovery Control
814 int ataGetSCTErrorRecoveryControltime(ata_device * device, unsigned type, unsigned short & time_limit);
815 int ataSetSCTErrorRecoveryControltime(ata_device * device, unsigned type, unsigned short time_limit);
816
817
818 /* Enable/Disable SMART on device */
819 int ataEnableSmart (ata_device * device);
820 int ataDisableSmart (ata_device * device);
821 int ataEnableAutoSave(ata_device * device);
822 int ataDisableAutoSave(ata_device * device);
823
824 /* Automatic Offline Testing */
825 int ataEnableAutoOffline (ata_device * device);
826 int ataDisableAutoOffline (ata_device * device);
827
828 /* S.M.A.R.T. test commands */
829 int ataSmartTest(ata_device * device, int testtype, bool force,
830 const ata_selective_selftest_args & args,
831 const ata_smart_values * sv, uint64_t num_sectors);
832
833 int ataWriteSelectiveSelfTestLog(ata_device * device, ata_selective_selftest_args & args,
834 const ata_smart_values * sv, uint64_t num_sectors,
835 const ata_selective_selftest_args * prev_spans = 0);
836
837 // Get World Wide Name (WWN) fields.
838 // Return NAA field or -1 if WWN is unsupported.
839 int ata_get_wwn(const ata_identify_device * id, unsigned & oui, uint64_t & unique_id);
840
841 // Get nominal media rotation rate.
842 // Returns: 0 = not reported, 1 = SSD, >1 = HDD rpm, < 0 = -(Unknown value)
843 int ata_get_rotation_rate(const ata_identify_device * id);
844
845 // If SMART supported, this is guaranteed to return 1 if SMART is enabled, else 0.
846 int ataDoesSmartWork(ata_device * device);
847
848 // returns 1 if SMART supported, 0 if not supported or can't tell
849 int ataSmartSupport(const ata_identify_device * drive);
850
851 // Return values:
852 // 1: Write Cache Reordering enabled
853 // 2: Write Cache Reordering disabled
854 // -1: error
855 int ataGetSetSCTWriteCacheReordering(ata_device * device, bool enable, bool persistent, bool set);
856
857 // Return values:
858 // 1: Write cache controled by ATA Set Features command
859 // 2: Force enable write cache
860 // 3: Force disable write cache
861 int ataGetSetSCTWriteCache(ata_device * device, unsigned short state, bool persistent, bool set);
862
863 // Return values:
864 // 1: SMART enabled
865 // 0: SMART disabled
866 // -1: can't tell if SMART is enabled -- try issuing ataDoesSmartWork command to see
867 int ataIsSmartEnabled(const ata_identify_device * drive);
868
869 int ataSmartStatus2(ata_device * device);
870
871 int isSmartErrorLogCapable(const ata_smart_values * data, const ata_identify_device * identity);
872
873 int isSmartTestLogCapable(const ata_smart_values * data, const ata_identify_device * identity);
874
875 int isGeneralPurposeLoggingCapable(const ata_identify_device * identity);
876
877 int isSupportExecuteOfflineImmediate(const ata_smart_values * data);
878
879 int isSupportAutomaticTimer(const ata_smart_values * data);
880
881 int isSupportOfflineAbort(const ata_smart_values * data);
882
883 int isSupportOfflineSurfaceScan(const ata_smart_values * data);
884
885 int isSupportSelfTest(const ata_smart_values * data);
886
887 int isSupportConveyanceSelfTest(const ata_smart_values * data);
888
889 int isSupportSelectiveSelfTest(const ata_smart_values * data);
890
891 inline bool isSCTCapable(const ata_identify_device *drive)
892 { return !!(drive->words088_255[206-88] & 0x01); } // 0x01 = SCT support
893
894 inline bool isSCTErrorRecoveryControlCapable(const ata_identify_device *drive)
895 { return ((drive->words088_255[206-88] & 0x09) == 0x09); } // 0x08 = SCT Error Recovery Control support
896
897 inline bool isSCTFeatureControlCapable(const ata_identify_device *drive)
898 { return ((drive->words088_255[206-88] & 0x11) == 0x11); } // 0x10 = SCT Feature Control support
899
900 inline bool isSCTDataTableCapable(const ata_identify_device *drive)
901 { return ((drive->words088_255[206-88] & 0x21) == 0x21); } // 0x20 = SCT Data Table support
902
903 int TestTime(const ata_smart_values * data, int testtype);
904
905 // Attribute state
906 enum ata_attr_state
907 {
908 ATTRSTATE_NON_EXISTING, // No such Attribute
909 ATTRSTATE_NO_NORMVAL, // Normalized value not valid
910 ATTRSTATE_NO_THRESHOLD, // Unknown or no threshold
911 ATTRSTATE_OK, // Never failed
912 ATTRSTATE_FAILED_PAST, // Failed in the past
913 ATTRSTATE_FAILED_NOW // Failed now
914 };
915
916 // Get attribute state
917 ata_attr_state ata_get_attr_state(const ata_smart_attribute & attr,
918 int attridx,
919 const ata_smart_threshold_entry * thresholds,
920 const ata_vendor_attr_defs & defs,
921 unsigned char * threshval = 0);
922
923 // Get attribute raw value.
924 uint64_t ata_get_attr_raw_value(const ata_smart_attribute & attr,
925 const ata_vendor_attr_defs & defs);
926
927 // Format attribute raw value.
928 std::string ata_format_attr_raw_value(const ata_smart_attribute & attr,
929 const ata_vendor_attr_defs & defs);
930
931 // Get attribute name
932 std::string ata_get_smart_attr_name(unsigned char id,
933 const ata_vendor_attr_defs & defs,
934 int rpm = 0);
935
936 // External handler function, for when a checksum is not correct. Can
937 // simply return if no action is desired, or can print error messages
938 // as needed, or exit. Is passed a string with the name of the Data
939 // Structure with the incorrect checksum.
940 void checksumwarning(const char *string);
941
942 // Find attribute index for attribute id, -1 if not found.
943 int ata_find_attr_index(unsigned char id, const ata_smart_values & smartval);
944
945 // Return Temperature Attribute raw value selected according to possible
946 // non-default interpretations. If the Attribute does not exist, return 0
947 unsigned char ata_return_temperature_value(const ata_smart_values * data, const ata_vendor_attr_defs & defs);
948
949
950 #define MAX_ATTRIBUTE_NUM 256
951
952 // Parse vendor attribute display def (-v option).
953 // Return false on error.
954 bool parse_attribute_def(const char * opt, ata_vendor_attr_defs & defs,
955 ata_vendor_def_prior priority);
956
957 // Get ID and increase flag of current pending or offline
958 // uncorrectable attribute.
959 unsigned char get_unc_attr_id(bool offline, const ata_vendor_attr_defs & defs,
960 bool & increase);
961
962 // Return a multiline string containing a list of valid arguments for
963 // parse_attribute_def().
964 std::string create_vendor_attribute_arg_list();
965
966 // Parse firmwarebug def (-F option).
967 // Return false on error.
968 bool parse_firmwarebug_def(const char * opt, firmwarebug_defs & firmwarebugs);
969
970 // Return a string of valid argument words for parse_firmwarebug_def()
971 const char * get_valid_firmwarebug_args();
972
973
974 // These are two of the functions that are defined in os_*.c and need
975 // to be ported to get smartmontools onto another OS.
976 // Moved to C++ interface
977 //int ata_command_interface(int device, smart_command_set command, int select, char *data);
978 //int escalade_command_interface(int fd, int escalade_port, int escalade_type, smart_command_set command, int select, char *data);
979 //int marvell_command_interface(int device, smart_command_set command, int select, char *data);
980 //int highpoint_command_interface(int device, smart_command_set command, int select, char *data);
981 //int areca_command_interface(int fd, int disknum, smart_command_set command, int select, char *data);
982
983
984 // This function is exported to give low-level capability
985 int smartcommandhandler(ata_device * device, smart_command_set command, int select, char *data);
986
987 // Print one self-test log entry.
988 // Returns:
989 // -1: failed self-test
990 // 1: extended self-test completed without error
991 // 0: otherwise
992 int ataPrintSmartSelfTestEntry(unsigned testnum, unsigned char test_type,
993 unsigned char test_status,
994 unsigned short timestamp,
995 uint64_t failing_lba,
996 bool print_error_only, bool & print_header);
997
998 // Print Smart self-test log, used by smartctl and smartd.
999 int ataPrintSmartSelfTestlog(const ata_smart_selftestlog * data, bool allentries,
1000 firmwarebug_defs firmwarebugs);
1001
1002 // Get capacity and sector sizes from IDENTIFY data
1003 struct ata_size_info
1004 {
1005 uint64_t sectors;
1006 uint64_t capacity;
1007 unsigned log_sector_size;
1008 unsigned phy_sector_size;
1009 unsigned log_sector_offset;
1010 };
1011
1012 void ata_get_size_info(const ata_identify_device * id, ata_size_info & sizes);
1013
1014 // Convenience function for formatting strings from ata_identify_device.
1015 void ata_format_id_string(char * out, const unsigned char * in, int n);
1016
1017 // Utility routines.
1018 unsigned char checksum(const void * data);
1019
1020 void swap2(char *location);
1021 void swap4(char *location);
1022 void swap8(char *location);
1023 // Typesafe variants using overloading
1024 inline void swapx(unsigned short * p)
1025 { swap2((char*)p); }
1026 inline void swapx(unsigned int * p)
1027 { swap4((char*)p); }
1028 inline void swapx(uint64_t * p)
1029 { swap8((char*)p); }
1030
1031 // Return pseudo-device to parse "smartctl -r ataioctl,2 ..." output
1032 // and simulate an ATA device with same behaviour
1033 ata_device * get_parsed_ata_device(smart_interface * intf, const char * dev_name);
1034
1035 #endif /* ATACMDS_H_ */