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