4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2006 Douglas Gilbert <dougg@torque.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 * The code in this file is based on the SCSI to ATA Translation (SAT)
18 * draft found at http://www.t10.org . The original draft used for this
19 * code is sat-r08.pdf which is not too far away from becoming a
20 * standard. The SAT commands of interest to smartmontools are the
21 * ATA PASS THROUGH SCSI (16) and ATA PASS THROUGH SCSI (12) defined in
22 * section 12 of that document.
24 * With more transports "hiding" SATA disks (and other S-ATAPI devices)
25 * behind a SCSI command set, accessing special features like SMART
26 * information becomes a challenge. The SAT standard offers ATA PASS
27 * THROUGH commands for special usages. Note that the SAT layer may
28 * be inside a generic OS layer (e.g. libata in linux), in a host
29 * adapter (HA or HBA) firmware, or somewhere on the interconnect
30 * between the host computer and the SATA devices (e.g. a RAID made
31 * of SATA disks and the RAID talks "SCSI" to the host computer).
32 * Note that in the latter case, this code does not solve the
33 * addressing issue (i.e. which SATA disk to address behind the logical
34 * SCSI (RAID) interface).
48 const char *scsiata_c_cvsid
="$Id: scsiata.cpp,v 1.7 2006/08/09 20:40:19 chrfranke Exp $"
49 CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID SCSICMDS_H_CVSID SCSIATA_H_CVSID UTILITY_H_CVSID
;
51 /* for passing global control variables */
52 extern smartmonctrl
*con
;
54 #define DEF_SAT_ATA_PASSTHRU_SIZE 16
57 // cdb[0]: ATA PASS THROUGH (16) SCSI command opcode byte (0x85)
58 // cdb[1]: multiple_count, protocol + extend
59 // cdb[2]: offline, ck_cond, t_dir, byte_block + t_length
60 // cdb[3]: features (15:8)
61 // cdb[4]: features (7:0)
62 // cdb[5]: sector_count (15:8)
63 // cdb[6]: sector_count (7:0)
64 // cdb[7]: lba_low (15:8)
65 // cdb[8]: lba_low (7:0)
66 // cdb[9]: lba_mid (15:8)
67 // cdb[10]: lba_mid (7:0)
68 // cdb[11]: lba_high (15:8)
69 // cdb[12]: lba_high (7:0)
71 // cdb[14]: (ata) command
72 // cdb[15]: control (SCSI, leave as zero)
74 // 24 bit lba (from MSB): cdb[12] cdb[10] cdb[8]
75 // 48 bit lba (from MSB): cdb[11] cdb[9] cdb[7] cdb[12] cdb[10] cdb[8]
78 // cdb[0]: ATA PASS THROUGH (12) SCSI command opcode byte (0xa1)
79 // cdb[1]: multiple_count, protocol + extend
80 // cdb[2]: offline, ck_cond, t_dir, byte_block + t_length
81 // cdb[3]: features (7:0)
82 // cdb[4]: sector_count (7:0)
83 // cdb[5]: lba_low (7:0)
84 // cdb[6]: lba_mid (7:0)
85 // cdb[7]: lba_high (7:0)
87 // cdb[9]: (ata) command
89 // cdb[11]: control (SCSI, leave as zero)
92 // ATA status return descriptor (component of descriptor sense data)
93 // des[0]: descriptor code (0x9)
94 // des[1]: additional descriptor length (0xc)
95 // des[2]: extend (bit 0)
97 // des[4]: sector_count (15:8)
98 // des[5]: sector_count (7:0)
99 // des[6]: lba_low (15:8)
100 // des[7]: lba_low (7:0)
101 // des[8]: lba_mid (15:8)
102 // des[9]: lba_mid (7:0)
103 // des[10]: lba_high (15:8)
104 // des[11]: lba_high (7:0)
111 // This interface routine takes ATA SMART commands and packages
112 // them in the SAT-defined ATA PASS THROUGH SCSI commands. There are
113 // two available SCSI commands: a 12 byte and 16 byte variant; the
114 // one used is chosen via con->satpassthrulen .
115 // DETAILED DESCRIPTION OF ARGUMENTS
116 // device: is the file descriptor provided by (a SCSI dvice type) open()
117 // command: defines the different ATA operations.
118 // select: additional input data if needed (which log, which type of
120 // data: location to write output data, if needed (512 bytes).
121 // Note: not all commands use all arguments.
123 // -1 if the command failed
124 // 0 if the command succeeded,
125 // STATUS_CHECK routine:
126 // -1 if the command failed
127 // 0 if the command succeeded and disk SMART status is "OK"
128 // 1 if the command succeeded and disk SMART status is "FAILING"
130 int sat_command_interface(int device
, smart_command_set command
, int select
,
133 struct scsi_cmnd_io io_hdr
;
134 struct scsi_sense_disect sinfo
;
135 struct sg_scsi_sense_hdr ssh
;
136 unsigned char cdb
[SAT_ATA_PASSTHROUGH_16LEN
];
137 unsigned char sense
[32];
138 const unsigned char * ucp
;
143 int chk_cond
= 0; /* set to 1 to read register(s) back */
144 int protocol
= 3; /* non-data */
145 int t_dir
= 1; /* 0 -> to device, 1 -> from device */
146 int byte_block
= 1; /* 0 -> bytes, 1 -> 512 byte blocks */
147 int t_length
= 0; /* 0 -> no data transferred */
150 int sector_count
= 0;
154 int passthru_size
= DEF_SAT_ATA_PASSTHRU_SIZE
;
156 memset(cdb
, 0, sizeof(cdb
));
157 memset(sense
, 0, sizeof(sense
));
159 ata_command
= ATA_SMART_CMD
;
161 case CHECK_POWER_MODE
:
162 ata_command
= ATA_CHECK_POWER_MODE
;
166 case READ_VALUES
: /* READ DATA */
167 feature
= ATA_SMART_READ_VALUES
;
168 sector_count
= 1; /* one (512 byte) block */
169 protocol
= 4; /* PIO data-in */
170 t_length
= 2; /* sector count holds count */
173 case READ_THRESHOLDS
: /* obsolete */
174 feature
= ATA_SMART_READ_THRESHOLDS
;
175 sector_count
= 1; /* one (512 byte) block */
177 protocol
= 4; /* PIO data-in */
178 t_length
= 2; /* sector count holds count */
182 feature
= ATA_SMART_READ_LOG_SECTOR
;
183 sector_count
= 1; /* one (512 byte) block */
185 protocol
= 4; /* PIO data-in */
186 t_length
= 2; /* sector count holds count */
190 feature
= ATA_SMART_WRITE_LOG_SECTOR
;
191 sector_count
= 1; /* one (512 byte) block */
193 protocol
= 5; /* PIO data-out */
194 t_length
= 2; /* sector count holds count */
195 t_dir
= 0; /* to device */
199 ata_command
= ATA_IDENTIFY_DEVICE
;
200 sector_count
= 1; /* one (512 byte) block */
201 protocol
= 4; /* PIO data-in */
202 t_length
= 2; /* sector count holds count */
206 ata_command
= ATA_IDENTIFY_PACKET_DEVICE
;
207 sector_count
= 1; /* one (512 byte) block */
208 protocol
= 4; /* PIO data-in */
209 t_length
= 2; /* sector count (7:0) holds count */
213 feature
= ATA_SMART_ENABLE
;
217 feature
= ATA_SMART_DISABLE
;
221 // this command only says if SMART is working. It could be
222 // replaced with STATUS_CHECK below.
223 feature
= ATA_SMART_STATUS
;
227 feature
= ATA_SMART_AUTO_OFFLINE
;
228 sector_count
= select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
231 feature
= ATA_SMART_AUTOSAVE
;
232 sector_count
= select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
234 case IMMEDIATE_OFFLINE
:
235 feature
= ATA_SMART_IMMEDIATE_OFFLINE
;
239 // This command uses HDIO_DRIVE_TASK and has different syntax than
240 // the other commands.
241 feature
= ATA_SMART_STATUS
; /* SMART RETURN STATUS */
245 pout("Unrecognized command %d in sat_command_interface()\n"
246 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
250 if (ATA_SMART_CMD
== ata_command
) {
255 if ((SAT_ATA_PASSTHROUGH_12LEN
== con
->satpassthrulen
) ||
256 (SAT_ATA_PASSTHROUGH_16LEN
== con
->satpassthrulen
))
257 passthru_size
= con
->satpassthrulen
;
258 cdb
[0] = (SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ?
259 SAT_ATA_PASSTHROUGH_12
: SAT_ATA_PASSTHROUGH_16
;
261 cdb
[1] = (protocol
<< 1) | extend
;
262 cdb
[2] = (chk_cond
<< 5) | (t_dir
<< 3) |
263 (byte_block
<< 2) | t_length
;
264 cdb
[(SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ? 3 : 4] = feature
;
265 cdb
[(SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ? 4 : 6] = sector_count
;
266 cdb
[(SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ? 5 : 8] = lba_low
;
267 cdb
[(SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ? 6 : 10] = lba_mid
;
268 cdb
[(SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ? 7 : 12] = lba_high
;
269 cdb
[(SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ? 9 : 14] = ata_command
;
271 memset(&io_hdr
, 0, sizeof(io_hdr
));
273 io_hdr
.dxfer_dir
= DXFER_NONE
;
274 io_hdr
.dxfer_len
= 0;
275 } else if (t_dir
) { /* from device */
276 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
277 io_hdr
.dxfer_len
= copydata
;
278 io_hdr
.dxferp
= (unsigned char *)data
;
279 memset(data
, 0, copydata
); /* prefill with zeroes */
280 } else { /* to device */
281 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
282 io_hdr
.dxfer_len
= outlen
;
283 io_hdr
.dxferp
= (unsigned char *)data
;
286 io_hdr
.cmnd_len
= passthru_size
;
287 io_hdr
.sensep
= sense
;
288 io_hdr
.max_sense_len
= sizeof(sense
);
289 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
291 status
= do_scsi_cmnd_io(device
, &io_hdr
, con
->reportscsiioctl
);
293 if (con
->reportscsiioctl
> 0)
294 pout("sat_command_interface: do_scsi_cmnd_io() failed, "
295 "status=%d\n", status
);
298 if (chk_cond
) { /* expecting SAT specific sense data */
300 if (sg_scsi_normalize_sense(io_hdr
.sensep
, io_hdr
.resp_sense_len
,
302 /* look for SAT extended ATA status return descriptor (9) */
303 ucp
= sg_scsi_sense_desc_find(io_hdr
.sensep
,
304 io_hdr
.resp_sense_len
, 9);
311 if (con
->reportscsiioctl
> 1) {
312 pout("Values from ATA status return descriptor are:\n");
313 dStrHex((const char *)ucp
, len
, 1);
315 if (ATA_CHECK_POWER_MODE
== cdb
[14])
316 data
[0] = ucp
[5]; /* sector count (0:7) */
317 else if (STATUS_CHECK
== command
) {
318 if ((ucp
[9] == 0x4f) && (ucp
[11] == 0xc2))
319 return 0; /* GOOD smart status */
320 if ((ucp
[9] == 0xf4) && (ucp
[11] == 0x2c))
321 return 1; // smart predicting failure, "bad" status
322 // We haven't gotten output that makes sense so
323 // print out some debugging info
324 syserror("Error SMART Status command failed");
325 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
326 pout("Values from ATA status return descriptor are:\n");
327 dStrHex((const char *)ucp
, len
, 1);
333 chk_cond
= 0; /* not the type of sense data expected */
334 if (t_dir
&& (t_length
> 0))
340 if (sg_scsi_normalize_sense(io_hdr
.sensep
, io_hdr
.resp_sense_len
,
342 if ((ssh
.response_code
>= 0x72) &&
343 ((SCSI_SK_NO_SENSE
== ssh
.sense_key
) ||
344 (SCSI_SK_RECOVERED_ERR
== ssh
.sense_key
)) &&
346 (SCSI_ASCQ_ATA_PASS_THROUGH
== ssh
.ascq
)) {
347 /* look for SAT extended ATA status return descriptor (9) */
348 ucp
= sg_scsi_sense_desc_find(io_hdr
.sensep
,
349 io_hdr
.resp_sense_len
, 9);
351 if (con
->reportscsiioctl
> 0) {
352 pout("Values from ATA status return descriptor are:\n");
358 dStrHex((const char *)ucp
, len
, 1);
363 scsi_do_sense_disect(&io_hdr
, &sinfo
);
364 status
= scsiSimpleSenseFilter(&sinfo
);
366 if (con
->reportscsiioctl
> 0)
367 pout("sat_command_interface: scsi error: %s\n",
368 scsiErrString(status
));
376 /* Attempt an IDENTIFY DEVICE ATA command via SATL when packet_interface
377 is 0 otherwise attempt IDENTIFY PACKET DEVICE. If successful
379 int has_sat_pass_through(int device
, int packet_interface
)
382 smart_command_set command
;
384 command
= packet_interface
? PIDENTIFY
: IDENTIFY
;
385 if (0 == sat_command_interface(device
, command
, 0, data
))
391 /* Next two functions are borrowed from sg_lib.c in the sg3_utils
392 package. Same copyrght owner, same license as this file. */
393 int sg_scsi_normalize_sense(const unsigned char * sensep
, int sb_len
,
394 struct sg_scsi_sense_hdr
* sshp
)
397 memset(sshp
, 0, sizeof(struct sg_scsi_sense_hdr
));
398 if ((NULL
== sensep
) || (0 == sb_len
) || (0x70 != (0x70 & sensep
[0])))
401 sshp
->response_code
= (0x7f & sensep
[0]);
402 if (sshp
->response_code
>= 0x72) { /* descriptor format */
404 sshp
->sense_key
= (0xf & sensep
[1]);
406 sshp
->asc
= sensep
[2];
408 sshp
->ascq
= sensep
[3];
410 sshp
->additional_length
= sensep
[7];
411 } else { /* fixed format */
413 sshp
->sense_key
= (0xf & sensep
[2]);
415 sb_len
= (sb_len
< (sensep
[7] + 8)) ? sb_len
:
418 sshp
->asc
= sensep
[12];
420 sshp
->ascq
= sensep
[13];
428 const unsigned char * sg_scsi_sense_desc_find(const unsigned char * sensep
,
429 int sense_len
, int desc_type
)
431 int add_sen_len
, add_len
, desc_len
, k
;
432 const unsigned char * descp
;
434 if ((sense_len
< 8) || (0 == (add_sen_len
= sensep
[7])))
436 if ((sensep
[0] < 0x72) || (sensep
[0] > 0x73))
438 add_sen_len
= (add_sen_len
< (sense_len
- 8)) ?
439 add_sen_len
: (sense_len
- 8);
441 for (desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
443 add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
444 desc_len
= add_len
+ 2;
445 if (descp
[0] == desc_type
)
447 if (add_len
< 0) /* short descriptor ?? */