4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2002-8 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
9 * Additional SCSI work:
10 * Copyright (C) 2003-8 Douglas Gilbert <dougg@torque.net>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * You should have received a copy of the GNU General Public License
18 * (for example COPYING); if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * This code was originally developed as a Senior Thesis by Michael Cornwell
22 * at the Concurrent Systems Laboratory (now part of the Storage Systems
23 * Research Center), Jack Baskin School of Engineering, University of
24 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
27 * In the SCSI world "SMART" is a dead or withdrawn standard. In recent
28 * SCSI standards (since SCSI-3) it goes under the awkward name of
29 * "Informational Exceptions" ["IE" or "IEC" (with the "C" for "control")].
30 * The relevant information is spread around several SCSI draft
31 * standards available at http://www.t10.org . Reference is made in the
32 * code to the following acronyms:
33 * - SAM [SCSI Architectural model, versions 2 or 3]
34 * - SPC [SCSI Primary commands, versions 2 or 3]
35 * - SBC [SCSI Block commands, versions 2]
37 * Some SCSI disk vendors have snippets of "SMART" information in their
48 #include "atacmds.h" // FIXME: for smart_command_set only
49 #include "dev_interface.h"
52 const char *scsicmds_c_cvsid
="$Id: scsicmds.cpp,v 1.98 2009/06/24 04:10:10 dpgilbert Exp $"
53 CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID
;
55 /* for passing global control variables */
56 extern smartmonctrl
*con
;
58 /* output binary in hex and optionally ascii */
59 void dStrHex(const char* str
, int len
, int no_ascii
)
65 const int bpstart
= 5;
66 const int cpstart
= 60;
74 k
= sprintf(buff
+ 1, "%.2x", a
);
76 if (bpos
>= ((bpstart
+ (9 * 3))))
79 for(i
= 0; i
< len
; i
++)
83 if (bpos
== (bpstart
+ (9 * 3)))
85 sprintf(&buff
[bpos
], "%.2x", (int)(unsigned char)c
);
90 if ((c
< ' ') || (c
>= 0x7f))
94 if (cpos
> (cpstart
+15))
101 k
= sprintf(buff
+ 1, "%.2x", a
);
111 struct scsi_opcode_name
{
116 static struct scsi_opcode_name opcode_name_arr
[] = {
117 /* in ascending opcode order */
118 {TEST_UNIT_READY
, "test unit ready"}, /* 0x00 */
119 {REQUEST_SENSE
, "request sense"}, /* 0x03 */
120 {INQUIRY
, "inquiry"}, /* 0x12 */
121 {MODE_SELECT
, "mode select(6)"}, /* 0x15 */
122 {MODE_SENSE
, "mode sense(6)"}, /* 0x1a */
123 {RECEIVE_DIAGNOSTIC
, "receive diagnostic"}, /* 0x1c */
124 {SEND_DIAGNOSTIC
, "send diagnostic"}, /* 0x1d */
125 {READ_DEFECT_10
, "read defect list(10)"}, /* 0x37 */
126 {LOG_SELECT
, "log select"}, /* 0x4c */
127 {LOG_SENSE
, "log sense"}, /* 0x4d */
128 {MODE_SELECT_10
, "mode select(10)"}, /* 0x55 */
129 {MODE_SENSE_10
, "mode sense(10)"}, /* 0x5a */
130 {SAT_ATA_PASSTHROUGH_16
, "ata pass-through(16)"}, /* 0x85 */
131 {SAT_ATA_PASSTHROUGH_12
, "ata pass-through(12)"}, /* 0xa1 */
134 const char * scsi_get_opcode_name(UINT8 opcode
)
137 int len
= sizeof(opcode_name_arr
) / sizeof(opcode_name_arr
[0]);
138 struct scsi_opcode_name
* onp
;
140 for (k
= 0; k
< len
; ++k
) {
141 onp
= &opcode_name_arr
[k
];
142 if (opcode
== onp
->opcode
)
144 else if (opcode
< onp
->opcode
)
151 void scsi_do_sense_disect(const struct scsi_cmnd_io
* io_buf
,
152 struct scsi_sense_disect
* out
)
156 memset(out
, 0, sizeof(struct scsi_sense_disect
));
157 if (SCSI_STATUS_CHECK_CONDITION
== io_buf
->scsi_status
) {
158 resp_code
= (io_buf
->sensep
[0] & 0x7f);
159 out
->error_code
= resp_code
;
160 if (resp_code
>= 0x72) {
161 out
->sense_key
= (io_buf
->sensep
[1] & 0xf);
162 out
->asc
= io_buf
->sensep
[2];
163 out
->ascq
= io_buf
->sensep
[3];
164 } else if (resp_code
>= 0x70) {
165 out
->sense_key
= (io_buf
->sensep
[2] & 0xf);
166 if (io_buf
->resp_sense_len
> 13) {
167 out
->asc
= io_buf
->sensep
[12];
168 out
->ascq
= io_buf
->sensep
[13];
174 int scsiSimpleSenseFilter(const struct scsi_sense_disect
* sinfo
)
176 switch (sinfo
->sense_key
) {
177 case SCSI_SK_NO_SENSE
:
178 case SCSI_SK_RECOVERED_ERR
:
179 return SIMPLE_NO_ERROR
;
180 case SCSI_SK_NOT_READY
:
181 if (SCSI_ASC_NO_MEDIUM
== sinfo
->asc
)
182 return SIMPLE_ERR_NO_MEDIUM
;
183 else if (SCSI_ASC_NOT_READY
== sinfo
->asc
) {
184 if (0x1 == sinfo
->ascq
)
185 return SIMPLE_ERR_BECOMING_READY
;
187 return SIMPLE_ERR_NOT_READY
;
189 return SIMPLE_ERR_NOT_READY
;
190 case SCSI_SK_MEDIUM_ERROR
:
191 case SCSI_SK_HARDWARE_ERROR
:
192 return SIMPLE_ERR_MEDIUM_HARDWARE
;
193 case SCSI_SK_ILLEGAL_REQUEST
:
194 if (SCSI_ASC_UNKNOWN_OPCODE
== sinfo
->asc
)
195 return SIMPLE_ERR_BAD_OPCODE
;
196 else if (SCSI_ASC_UNKNOWN_FIELD
== sinfo
->asc
)
197 return SIMPLE_ERR_BAD_FIELD
;
198 else if (SCSI_ASC_UNKNOWN_PARAM
== sinfo
->asc
)
199 return SIMPLE_ERR_BAD_PARAM
;
201 return SIMPLE_ERR_BAD_PARAM
; /* all other illegal request */
202 case SCSI_SK_UNIT_ATTENTION
:
203 return SIMPLE_ERR_TRY_AGAIN
;
204 case SCSI_SK_ABORTED_COMMAND
:
205 return SIMPLE_ERR_ABORTED_COMMAND
;
207 return SIMPLE_ERR_UNKNOWN
;
211 const char * scsiErrString(int scsiErr
)
214 return strerror(-scsiErr
);
216 case SIMPLE_NO_ERROR
:
218 case SIMPLE_ERR_NOT_READY
:
219 return "device not ready";
220 case SIMPLE_ERR_BAD_OPCODE
:
221 return "unsupported scsi opcode";
222 case SIMPLE_ERR_BAD_FIELD
:
223 return "unsupported field in scsi command";
224 case SIMPLE_ERR_BAD_PARAM
:
225 return "badly formed scsi parameters";
226 case SIMPLE_ERR_BAD_RESP
:
227 return "scsi response fails sanity test";
228 case SIMPLE_ERR_NO_MEDIUM
:
229 return "no medium present";
230 case SIMPLE_ERR_BECOMING_READY
:
231 return "device will be ready soon";
232 case SIMPLE_ERR_TRY_AGAIN
:
233 return "unit attention reported, try again";
234 case SIMPLE_ERR_MEDIUM_HARDWARE
:
235 return "medium or hardware error (serious)";
236 case SIMPLE_ERR_UNKNOWN
:
237 return "unknown error (unexpected sense key)";
238 case SIMPLE_ERR_ABORTED_COMMAND
:
239 return "aborted command";
241 return "unknown error";
245 /* Sends LOG SENSE command. Returns 0 if ok, 1 if device NOT READY, 2 if
246 command not supported, 3 if field (within command) not supported or
247 returns negated errno. SPC-3 sections 6.6 and 7.2 (rec 22a).
248 N.B. Sets PC==1 to fetch "current cumulative" log pages.
249 If known_resp_len > 0 then a single fetch is done for this response
250 length. If known_resp_len == 0 then twin fetches are performed, the
251 first to deduce the response length, then send the same command again
252 requesting the deduced response length. This protects certain fragile
253 HBAs. The twin fetch technique should not be used with the TapeAlert
254 log page since it clears its state flags after each fetch. */
255 int scsiLogSense(scsi_device
* device
, int pagenum
, int subpagenum
, UINT8
*pBuf
,
256 int bufLen
, int known_resp_len
)
258 struct scsi_cmnd_io io_hdr
;
259 struct scsi_sense_disect sinfo
;
265 if (known_resp_len
> bufLen
)
267 if (known_resp_len
> 0)
268 pageLen
= known_resp_len
;
270 /* Starting twin fetch strategy: first fetch to find respone length */
272 if (pageLen
> bufLen
)
275 memset(pBuf
, 0, pageLen
);
277 memset(&io_hdr
, 0, sizeof(io_hdr
));
278 memset(cdb
, 0, sizeof(cdb
));
279 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
280 io_hdr
.dxfer_len
= pageLen
;
281 io_hdr
.dxferp
= pBuf
;
283 cdb
[2] = 0x40 | (pagenum
& 0x3f); /* Page control (PC)==1 */
285 cdb
[7] = (pageLen
>> 8) & 0xff;
286 cdb
[8] = pageLen
& 0xff;
288 io_hdr
.cmnd_len
= sizeof(cdb
);
289 io_hdr
.sensep
= sense
;
290 io_hdr
.max_sense_len
= sizeof(sense
);
291 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
293 if (!device
->scsi_pass_through(&io_hdr
))
294 return -device
->get_errno();
295 scsi_do_sense_disect(&io_hdr
, &sinfo
);
296 if ((res
= scsiSimpleSenseFilter(&sinfo
)))
298 /* sanity check on response */
299 if ((SUPPORTED_LPAGES
!= pagenum
) && (pBuf
[0] != pagenum
))
300 return SIMPLE_ERR_BAD_RESP
;
301 if (0 == ((pBuf
[2] << 8) + pBuf
[3]))
302 return SIMPLE_ERR_BAD_RESP
;
303 pageLen
= (pBuf
[2] << 8) + pBuf
[3] + 4;
304 if (4 == pageLen
) /* why define a lpage with no payload? */
305 pageLen
= 252; /* some IBM tape drives don't like double fetch */
306 /* some SCSI HBA don't like "odd" length transfers */
309 if (pageLen
> bufLen
)
313 memset(&io_hdr
, 0, sizeof(io_hdr
));
314 memset(cdb
, 0, sizeof(cdb
));
315 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
316 io_hdr
.dxfer_len
= pageLen
;
317 io_hdr
.dxferp
= pBuf
;
319 cdb
[2] = 0x40 | (pagenum
& 0x3f); /* Page control (PC)==1 */
320 cdb
[7] = (pageLen
>> 8) & 0xff;
321 cdb
[8] = pageLen
& 0xff;
323 io_hdr
.cmnd_len
= sizeof(cdb
);
324 io_hdr
.sensep
= sense
;
325 io_hdr
.max_sense_len
= sizeof(sense
);
326 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
328 if (!device
->scsi_pass_through(&io_hdr
))
329 return -device
->get_errno();
330 scsi_do_sense_disect(&io_hdr
, &sinfo
);
331 status
= scsiSimpleSenseFilter(&sinfo
);
334 /* sanity check on response */
335 if ((SUPPORTED_LPAGES
!= pagenum
) && (pBuf
[0] != pagenum
))
336 return SIMPLE_ERR_BAD_RESP
;
337 if (0 == ((pBuf
[2] << 8) + pBuf
[3]))
338 return SIMPLE_ERR_BAD_RESP
;
342 /* Sends a LOG SELECT command. Can be used to set log page values
343 * or reset one log page (or all of them) to its defaults (typically zero).
344 * Returns 0 if ok, 1 if NOT READY, 2 if command not supported, * 3 if
345 * field in command not supported, * 4 if bad parameter to command or
346 * returns negated errno. SPC-4 sections 6.5 and 7.2 (rev 20) */
347 int scsiLogSelect(scsi_device
* device
, int pcr
, int sp
, int pc
, int pagenum
,
348 int subpagenum
, UINT8
*pBuf
, int bufLen
)
350 struct scsi_cmnd_io io_hdr
;
351 struct scsi_sense_disect sinfo
;
355 memset(&io_hdr
, 0, sizeof(io_hdr
));
356 memset(cdb
, 0, sizeof(cdb
));
357 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
358 io_hdr
.dxfer_len
= bufLen
;
359 io_hdr
.dxferp
= pBuf
;
361 cdb
[1] = (pcr
? 2 : 0) | (sp
? 1 : 0);
362 cdb
[2] = ((pc
<< 6) & 0xc0) | (pagenum
& 0x3f);
363 cdb
[3] = (subpagenum
& 0xff);
364 cdb
[7] = ((bufLen
>> 8) & 0xff);
365 cdb
[8] = (bufLen
& 0xff);
367 io_hdr
.cmnd_len
= sizeof(cdb
);
368 io_hdr
.sensep
= sense
;
369 io_hdr
.max_sense_len
= sizeof(sense
);
370 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
372 if (!device
->scsi_pass_through(&io_hdr
))
373 return -device
->get_errno();
374 scsi_do_sense_disect(&io_hdr
, &sinfo
);
375 return scsiSimpleSenseFilter(&sinfo
);
378 /* Send MODE SENSE (6 byte) command. Returns 0 if ok, 1 if NOT READY,
379 * 2 if command not supported (then MODE SENSE(10) should be supported),
380 * 3 if field in command not supported or returns negated errno.
381 * SPC-3 sections 6.9 and 7.4 (rev 22a) [mode subpage==0] */
382 int scsiModeSense(scsi_device
* device
, int pagenum
, int subpagenum
, int pc
,
383 UINT8
*pBuf
, int bufLen
)
385 struct scsi_cmnd_io io_hdr
;
386 struct scsi_sense_disect sinfo
;
391 if ((bufLen
< 0) || (bufLen
> 255))
393 memset(&io_hdr
, 0, sizeof(io_hdr
));
394 memset(cdb
, 0, sizeof(cdb
));
395 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
396 io_hdr
.dxfer_len
= bufLen
;
397 io_hdr
.dxferp
= pBuf
;
399 cdb
[2] = (pc
<< 6) | (pagenum
& 0x3f);
403 io_hdr
.cmnd_len
= sizeof(cdb
);
404 io_hdr
.sensep
= sense
;
405 io_hdr
.max_sense_len
= sizeof(sense
);
406 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
408 if (!device
->scsi_pass_through(&io_hdr
))
409 return -device
->get_errno();
410 scsi_do_sense_disect(&io_hdr
, &sinfo
);
411 status
= scsiSimpleSenseFilter(&sinfo
);
412 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
413 if (!device
->scsi_pass_through(&io_hdr
))
414 return -device
->get_errno();
415 scsi_do_sense_disect(&io_hdr
, &sinfo
);
416 status
= scsiSimpleSenseFilter(&sinfo
);
418 if ((0 == status
) && (ALL_MODE_PAGES
!= pagenum
)) {
421 offset
= scsiModePageOffset(pBuf
, bufLen
, 0);
423 return SIMPLE_ERR_BAD_RESP
;
424 else if (pagenum
!= (pBuf
[offset
] & 0x3f))
425 return SIMPLE_ERR_BAD_RESP
;
430 /* Sends a 6 byte MODE SELECT command. Assumes given pBuf is the response
431 * from a corresponding 6 byte MODE SENSE command. Such a response should
432 * have a 4 byte header followed by 0 or more 8 byte block descriptors
433 * (normally 1) and then 1 mode page. Returns 0 if ok, 1 if NOT READY,
434 * 2 if command not supported (then MODE SELECT(10) may be supported),
435 * 3 if field in command not supported, 4 if bad parameter to command
436 * or returns negated errno. SPC-3 sections 6.7 and 7.4 (rev 22a) */
437 int scsiModeSelect(scsi_device
* device
, int sp
, UINT8
*pBuf
, int bufLen
)
439 struct scsi_cmnd_io io_hdr
;
440 struct scsi_sense_disect sinfo
;
443 int pg_offset
, pg_len
, hdr_plus_1_pg
;
445 pg_offset
= 4 + pBuf
[3];
446 if (pg_offset
+ 2 >= bufLen
)
448 pg_len
= pBuf
[pg_offset
+ 1] + 2;
449 hdr_plus_1_pg
= pg_offset
+ pg_len
;
450 if (hdr_plus_1_pg
> bufLen
)
452 pBuf
[0] = 0; /* Length of returned mode sense data reserved for SELECT */
453 pBuf
[pg_offset
] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
454 memset(&io_hdr
, 0, sizeof(io_hdr
));
455 memset(cdb
, 0, sizeof(cdb
));
456 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
457 io_hdr
.dxfer_len
= hdr_plus_1_pg
;
458 io_hdr
.dxferp
= pBuf
;
459 cdb
[0] = MODE_SELECT
;
460 cdb
[1] = 0x10 | (sp
& 1); /* set PF (page format) bit always */
461 cdb
[4] = hdr_plus_1_pg
; /* make sure only one page sent */
463 io_hdr
.cmnd_len
= sizeof(cdb
);
464 io_hdr
.sensep
= sense
;
465 io_hdr
.max_sense_len
= sizeof(sense
);
466 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
468 if (!device
->scsi_pass_through(&io_hdr
))
469 return -device
->get_errno();
470 scsi_do_sense_disect(&io_hdr
, &sinfo
);
471 return scsiSimpleSenseFilter(&sinfo
);
474 /* MODE SENSE (10 byte). Returns 0 if ok, 1 if NOT READY, 2 if command
475 * not supported (then MODE SENSE(6) might be supported), 3 if field in
476 * command not supported or returns negated errno.
477 * SPC-3 sections 6.10 and 7.4 (rev 22a) [mode subpage==0] */
478 int scsiModeSense10(scsi_device
* device
, int pagenum
, int subpagenum
, int pc
,
479 UINT8
*pBuf
, int bufLen
)
481 struct scsi_cmnd_io io_hdr
;
482 struct scsi_sense_disect sinfo
;
487 memset(&io_hdr
, 0, sizeof(io_hdr
));
488 memset(cdb
, 0, sizeof(cdb
));
489 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
490 io_hdr
.dxfer_len
= bufLen
;
491 io_hdr
.dxferp
= pBuf
;
492 cdb
[0] = MODE_SENSE_10
;
493 cdb
[2] = (pc
<< 6) | (pagenum
& 0x3f);
495 cdb
[7] = (bufLen
>> 8) & 0xff;
496 cdb
[8] = bufLen
& 0xff;
498 io_hdr
.cmnd_len
= sizeof(cdb
);
499 io_hdr
.sensep
= sense
;
500 io_hdr
.max_sense_len
= sizeof(sense
);
501 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
503 if (!device
->scsi_pass_through(&io_hdr
))
504 return -device
->get_errno();
505 scsi_do_sense_disect(&io_hdr
, &sinfo
);
506 status
= scsiSimpleSenseFilter(&sinfo
);
507 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
508 if (!device
->scsi_pass_through(&io_hdr
))
509 return -device
->get_errno();
510 scsi_do_sense_disect(&io_hdr
, &sinfo
);
511 status
= scsiSimpleSenseFilter(&sinfo
);
513 if ((0 == status
) && (ALL_MODE_PAGES
!= pagenum
)) {
516 offset
= scsiModePageOffset(pBuf
, bufLen
, 1);
518 return SIMPLE_ERR_BAD_RESP
;
519 else if (pagenum
!= (pBuf
[offset
] & 0x3f))
520 return SIMPLE_ERR_BAD_RESP
;
525 /* Sends a 10 byte MODE SELECT command. Assumes given pBuf is the response
526 * from a corresponding 10 byte MODE SENSE command. Such a response should
527 * have a 8 byte header followed by 0 or more 8 byte block descriptors
528 * (normally 1) and then 1 mode page. Returns 0 if ok, 1 NOT REAFY, 2 if
529 * command not supported (then MODE SELECT(6) may be supported), 3 if field
530 * in command not supported, 4 if bad parameter to command or returns
531 * negated errno. SPC-3 sections 6.8 and 7.4 (rev 22a) */
532 int scsiModeSelect10(scsi_device
* device
, int sp
, UINT8
*pBuf
, int bufLen
)
534 struct scsi_cmnd_io io_hdr
;
535 struct scsi_sense_disect sinfo
;
538 int pg_offset
, pg_len
, hdr_plus_1_pg
;
540 pg_offset
= 8 + (pBuf
[6] << 8) + pBuf
[7];
541 if (pg_offset
+ 2 >= bufLen
)
543 pg_len
= pBuf
[pg_offset
+ 1] + 2;
544 hdr_plus_1_pg
= pg_offset
+ pg_len
;
545 if (hdr_plus_1_pg
> bufLen
)
548 pBuf
[1] = 0; /* Length of returned mode sense data reserved for SELECT */
549 pBuf
[pg_offset
] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
550 memset(&io_hdr
, 0, sizeof(io_hdr
));
551 memset(cdb
, 0, sizeof(cdb
));
552 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
553 io_hdr
.dxfer_len
= hdr_plus_1_pg
;
554 io_hdr
.dxferp
= pBuf
;
555 cdb
[0] = MODE_SELECT_10
;
556 cdb
[1] = 0x10 | (sp
& 1); /* set PF (page format) bit always */
557 cdb
[8] = hdr_plus_1_pg
; /* make sure only one page sent */
559 io_hdr
.cmnd_len
= sizeof(cdb
);
560 io_hdr
.sensep
= sense
;
561 io_hdr
.max_sense_len
= sizeof(sense
);
562 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
564 if (!device
->scsi_pass_through(&io_hdr
))
565 return -device
->get_errno();
566 scsi_do_sense_disect(&io_hdr
, &sinfo
);
567 return scsiSimpleSenseFilter(&sinfo
);
570 /* Standard INQUIRY returns 0 for ok, anything else is a major problem.
571 * bufLen should be 36 for unsafe devices (like USB mass storage stuff)
572 * otherwise they can lock up! SPC-3 sections 6.4 and 7.6 (rev 22a) */
573 int scsiStdInquiry(scsi_device
* device
, UINT8
*pBuf
, int bufLen
)
575 struct scsi_sense_disect sinfo
;
576 struct scsi_cmnd_io io_hdr
;
580 if ((bufLen
< 0) || (bufLen
> 255))
582 memset(&io_hdr
, 0, sizeof(io_hdr
));
583 memset(cdb
, 0, sizeof(cdb
));
584 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
585 io_hdr
.dxfer_len
= bufLen
;
586 io_hdr
.dxferp
= pBuf
;
590 io_hdr
.cmnd_len
= sizeof(cdb
);
591 io_hdr
.sensep
= sense
;
592 io_hdr
.max_sense_len
= sizeof(sense
);
593 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
595 if (!device
->scsi_pass_through(&io_hdr
))
596 return -device
->get_errno();
597 scsi_do_sense_disect(&io_hdr
, &sinfo
);
598 return scsiSimpleSenseFilter(&sinfo
);
601 /* INQUIRY to fetch Vital Page Data. Returns 0 if ok, 1 if NOT READY
602 * (unlikely), 2 if command not supported, 3 if field in command not
603 * supported, 5 if response indicates that EVPD bit ignored or returns
604 * negated errno. SPC-3 section 6.4 and 7.6 (rev 22a) */
605 int scsiInquiryVpd(scsi_device
* device
, int vpd_page
, UINT8
*pBuf
, int bufLen
)
607 struct scsi_cmnd_io io_hdr
;
608 struct scsi_sense_disect sinfo
;
613 if ((bufLen
< 0) || (bufLen
> 255))
615 memset(&io_hdr
, 0, sizeof(io_hdr
));
616 memset(cdb
, 0, sizeof(cdb
));
619 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
620 io_hdr
.dxfer_len
= bufLen
;
621 io_hdr
.dxferp
= pBuf
;
623 cdb
[1] = 0x1; /* set EVPD bit (enable Vital Product Data) */
627 io_hdr
.cmnd_len
= sizeof(cdb
);
628 io_hdr
.sensep
= sense
;
629 io_hdr
.max_sense_len
= sizeof(sense
);
630 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
632 if (!device
->scsi_pass_through(&io_hdr
))
633 return -device
->get_errno();
634 scsi_do_sense_disect(&io_hdr
, &sinfo
);
635 if ((res
= scsiSimpleSenseFilter(&sinfo
)))
637 /* Guard against devices that ignore EVPD bit and do standard INQUIRY */
639 if (vpd_page
== pBuf
[1]) {
640 if ((0x80 == vpd_page
) && (bufLen
> 2) && (0x0 != pBuf
[2]))
641 return SIMPLE_ERR_BAD_RESP
;
643 return SIMPLE_ERR_BAD_RESP
;
648 /* REQUEST SENSE command. Returns 0 if ok, anything else major problem.
649 * SPC-3 section 6.27 (rev 22a) */
650 int scsiRequestSense(scsi_device
* device
, struct scsi_sense_disect
* sense_info
)
652 struct scsi_cmnd_io io_hdr
;
659 memset(&io_hdr
, 0, sizeof(io_hdr
));
660 memset(cdb
, 0, sizeof(cdb
));
661 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
662 io_hdr
.dxfer_len
= sizeof(buff
);
663 io_hdr
.dxferp
= buff
;
664 cdb
[0] = REQUEST_SENSE
;
665 cdb
[4] = sizeof(buff
);
667 io_hdr
.cmnd_len
= sizeof(cdb
);
668 io_hdr
.sensep
= sense
;
669 io_hdr
.max_sense_len
= sizeof(sense
);
670 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
672 if (!device
->scsi_pass_through(&io_hdr
))
673 return -device
->get_errno();
675 ecode
= buff
[0] & 0x7f;
676 sense_info
->error_code
= ecode
;
677 sense_info
->sense_key
= buff
[2] & 0xf;
679 sense_info
->ascq
= 0;
680 if ((0x70 == ecode
) || (0x71 == ecode
)) {
683 sense_info
->asc
= buff
[12];
684 sense_info
->ascq
= buff
[13];
691 /* SEND DIAGNOSTIC command. Returns 0 if ok, 1 if NOT READY, 2 if command
692 * not supported, 3 if field in command not supported or returns negated
693 * errno. SPC-3 section 6.28 (rev 22a) */
694 int scsiSendDiagnostic(scsi_device
* device
, int functioncode
, UINT8
*pBuf
, int bufLen
)
696 struct scsi_cmnd_io io_hdr
;
697 struct scsi_sense_disect sinfo
;
701 memset(&io_hdr
, 0, sizeof(io_hdr
));
702 memset(cdb
, 0, sizeof(cdb
));
703 io_hdr
.dxfer_dir
= bufLen
? DXFER_TO_DEVICE
: DXFER_NONE
;
704 io_hdr
.dxfer_len
= bufLen
;
705 io_hdr
.dxferp
= pBuf
;
706 cdb
[0] = SEND_DIAGNOSTIC
;
707 if (SCSI_DIAG_DEF_SELF_TEST
== functioncode
)
708 cdb
[1] = 0x4; /* SelfTest bit */
709 else if (SCSI_DIAG_NO_SELF_TEST
!= functioncode
)
710 cdb
[1] = (functioncode
& 0x7) << 5; /* SelfTest _code_ */
711 else /* SCSI_DIAG_NO_SELF_TEST == functioncode */
712 cdb
[1] = 0x10; /* PF bit */
713 cdb
[3] = (bufLen
>> 8) & 0xff;
714 cdb
[4] = bufLen
& 0xff;
716 io_hdr
.cmnd_len
= sizeof(cdb
);
717 io_hdr
.sensep
= sense
;
718 io_hdr
.max_sense_len
= sizeof(sense
);
719 /* worst case is an extended foreground self test on a big disk */
720 io_hdr
.timeout
= SCSI_TIMEOUT_SELF_TEST
;
722 if (!device
->scsi_pass_through(&io_hdr
))
723 return -device
->get_errno();
724 scsi_do_sense_disect(&io_hdr
, &sinfo
);
725 return scsiSimpleSenseFilter(&sinfo
);
728 /* RECEIVE DIAGNOSTIC command. Returns 0 if ok, 1 if NOT READY, 2 if
729 * command not supported, 3 if field in command not supported or returns
730 * negated errno. SPC-3 section 6.18 (rev 22a) */
731 int scsiReceiveDiagnostic(scsi_device
* device
, int pcv
, int pagenum
, UINT8
*pBuf
,
734 struct scsi_cmnd_io io_hdr
;
735 struct scsi_sense_disect sinfo
;
739 memset(&io_hdr
, 0, sizeof(io_hdr
));
740 memset(cdb
, 0, sizeof(cdb
));
741 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
742 io_hdr
.dxfer_len
= bufLen
;
743 io_hdr
.dxferp
= pBuf
;
744 cdb
[0] = RECEIVE_DIAGNOSTIC
;
747 cdb
[3] = (bufLen
>> 8) & 0xff;
748 cdb
[4] = bufLen
& 0xff;
750 io_hdr
.cmnd_len
= sizeof(cdb
);
751 io_hdr
.sensep
= sense
;
752 io_hdr
.max_sense_len
= sizeof(sense
);
753 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
755 if (!device
->scsi_pass_through(&io_hdr
))
756 return -device
->get_errno();
757 scsi_do_sense_disect(&io_hdr
, &sinfo
);
758 return scsiSimpleSenseFilter(&sinfo
);
761 /* TEST UNIT READY command. SPC-3 section 6.33 (rev 22a) */
762 static int _testunitready(scsi_device
* device
, struct scsi_sense_disect
* sinfo
)
764 struct scsi_cmnd_io io_hdr
;
768 memset(&io_hdr
, 0, sizeof(io_hdr
));
769 memset(cdb
, 0, sizeof(cdb
));
770 io_hdr
.dxfer_dir
= DXFER_NONE
;
771 io_hdr
.dxfer_len
= 0;
772 io_hdr
.dxferp
= NULL
;
773 cdb
[0] = TEST_UNIT_READY
;
775 io_hdr
.cmnd_len
= sizeof(cdb
);
776 io_hdr
.sensep
= sense
;
777 io_hdr
.max_sense_len
= sizeof(sense
);
778 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
780 if (!device
->scsi_pass_through(&io_hdr
))
781 return -device
->get_errno();
782 scsi_do_sense_disect(&io_hdr
, sinfo
);
786 /* Returns 0 for device responds and media ready, 1 for device responds and
787 media not ready, or returns a negated errno value */
788 int scsiTestUnitReady(scsi_device
* device
)
790 struct scsi_sense_disect sinfo
;
793 status
= _testunitready(device
, &sinfo
);
796 status
= scsiSimpleSenseFilter(&sinfo
);
797 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
798 /* power on reset, media changed, ok ... try again */
799 status
= _testunitready(device
, &sinfo
);
802 status
= scsiSimpleSenseFilter(&sinfo
);
807 /* READ DEFECT (10) command. Returns 0 if ok, 1 if NOT READY, 2 if
808 * command not supported, 3 if field in command not supported or returns
809 * negated errno. SBC-2 section 5.12 (rev 16) */
810 int scsiReadDefect10(scsi_device
* device
, int req_plist
, int req_glist
, int dl_format
,
811 UINT8
*pBuf
, int bufLen
)
813 struct scsi_cmnd_io io_hdr
;
814 struct scsi_sense_disect sinfo
;
818 memset(&io_hdr
, 0, sizeof(io_hdr
));
819 memset(cdb
, 0, sizeof(cdb
));
820 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
821 io_hdr
.dxfer_len
= bufLen
;
822 io_hdr
.dxferp
= pBuf
;
823 cdb
[0] = READ_DEFECT_10
;
824 cdb
[2] = (unsigned char)(((req_plist
<< 4) & 0x10) |
825 ((req_glist
<< 3) & 0x8) | (dl_format
& 0x7));
826 cdb
[7] = (bufLen
>> 8) & 0xff;
827 cdb
[8] = bufLen
& 0xff;
829 io_hdr
.cmnd_len
= sizeof(cdb
);
830 io_hdr
.sensep
= sense
;
831 io_hdr
.max_sense_len
= sizeof(sense
);
832 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
834 if (!device
->scsi_pass_through(&io_hdr
))
835 return -device
->get_errno();
836 scsi_do_sense_disect(&io_hdr
, &sinfo
);
837 return scsiSimpleSenseFilter(&sinfo
);
840 /* Offset into mode sense (6 or 10 byte) response that actual mode page
841 * starts at (relative to resp[0]). Returns -1 if problem */
842 int scsiModePageOffset(const UINT8
* resp
, int len
, int modese_len
)
844 int resp_len
, bd_len
;
848 if (10 == modese_len
) {
849 resp_len
= (resp
[0] << 8) + resp
[1] + 2;
850 bd_len
= (resp
[6] << 8) + resp
[7];
853 resp_len
= resp
[0] + 1;
857 if ((offset
+ 2) > len
) {
858 pout("scsiModePageOffset: raw_curr too small, offset=%d "
859 "resp_len=%d bd_len=%d\n", offset
, resp_len
, bd_len
);
861 } else if ((offset
+ 2) > resp_len
) {
862 if ((resp_len
> 2) || con
->reportscsiioctl
)
863 pout("scsiModePageOffset: response length too short, "
864 "resp_len=%d offset=%d bd_len=%d\n", resp_len
,
872 /* IEC mode page byte 2 bit masks */
873 #define DEXCPT_ENABLE 0x08
874 #define EWASC_ENABLE 0x10
875 #define DEXCPT_DISABLE 0xf7
876 #define EWASC_DISABLE 0xef
877 #define TEST_DISABLE 0xfb
879 /* Fetches the Informational Exceptions Control mode page. First tries
880 * the 6 byte MODE SENSE command and if that fails with an illegal opcode
881 * tries a 10 byte MODE SENSE command. Returns 0 if successful, a positive
882 * number if a known error (see SIMPLE_ERR_ ...) or a negative errno
884 int scsiFetchIECmpage(scsi_device
* device
, struct scsi_iec_mode_page
*iecp
, int modese_len
)
888 memset(iecp
, 0, sizeof(*iecp
));
889 iecp
->modese_len
= modese_len
;
890 iecp
->requestedCurrent
= 1;
891 if (iecp
->modese_len
<= 6) {
892 if ((err
= scsiModeSense(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
893 0, MPAGE_CONTROL_CURRENT
,
894 iecp
->raw_curr
, sizeof(iecp
->raw_curr
)))) {
895 if (SIMPLE_ERR_BAD_OPCODE
== err
)
896 iecp
->modese_len
= 10;
898 iecp
->modese_len
= 0;
901 } else if (0 == iecp
->modese_len
)
902 iecp
->modese_len
= 6;
904 if (10 == iecp
->modese_len
) {
905 err
= scsiModeSense10(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
906 0, MPAGE_CONTROL_CURRENT
,
907 iecp
->raw_curr
, sizeof(iecp
->raw_curr
));
909 iecp
->modese_len
= 0;
913 iecp
->gotCurrent
= 1;
914 iecp
->requestedChangeable
= 1;
915 if (10 == iecp
->modese_len
)
916 err
= scsiModeSense10(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
917 0, MPAGE_CONTROL_CHANGEABLE
,
918 iecp
->raw_chg
, sizeof(iecp
->raw_chg
));
919 else if (6 == iecp
->modese_len
)
920 err
= scsiModeSense(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
921 0, MPAGE_CONTROL_CHANGEABLE
,
922 iecp
->raw_chg
, sizeof(iecp
->raw_chg
));
925 iecp
->gotChangeable
= 1;
929 int scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page
*iecp
)
933 if (iecp
&& iecp
->gotCurrent
) {
934 offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
937 return (iecp
->raw_curr
[offset
+ 2] & DEXCPT_ENABLE
) ? 0 : 1;
944 int scsi_IsWarningEnabled(const struct scsi_iec_mode_page
*iecp
)
948 if (iecp
&& iecp
->gotCurrent
) {
949 offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
952 return (iecp
->raw_curr
[offset
+ 2] & EWASC_ENABLE
) ? 1 : 0;
959 /* set EWASC and clear PERF, EBF, DEXCPT TEST and LOGERR */
960 #define SCSI_IEC_MP_BYTE2_ENABLED 0x10
961 #define SCSI_IEC_MP_BYTE2_TEST_MASK 0x4
962 /* exception/warning via an unrequested REQUEST SENSE command */
963 #define SCSI_IEC_MP_MRIE 6
964 #define SCSI_IEC_MP_INTERVAL_T 0
965 #define SCSI_IEC_MP_REPORT_COUNT 1
967 /* Try to set (or clear) both Exception Control and Warning in the IE
968 * mode page subject to the "changeable" mask. The object pointed to
969 * by iecp is (possibly) inaccurate after this call, therefore
970 * scsiFetchIECmpage() should be called again if the IEC mode page
971 * is to be re-examined.
972 * When -r ioctl is invoked 3 or more time on 'smartctl -s on ...'
973 * then set the TEST bit (causes asc,ascq pair of 0x5d,0xff). */
974 int scsiSetExceptionControlAndWarning(scsi_device
* device
, int enabled
,
975 const struct scsi_iec_mode_page
*iecp
)
977 int k
, offset
, resp_len
;
979 UINT8 rout
[SCSI_IECMP_RAW_LEN
];
980 int sp
, eCEnabled
, wEnabled
;
982 if ((! iecp
) || (! iecp
->gotCurrent
))
984 offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
988 memcpy(rout
, iecp
->raw_curr
, SCSI_IECMP_RAW_LEN
);
989 if (10 == iecp
->modese_len
) {
990 resp_len
= (rout
[0] << 8) + rout
[1] + 2;
991 rout
[3] &= 0xef; /* for disks mask out DPOFUA bit */
993 resp_len
= rout
[0] + 1;
994 rout
[2] &= 0xef; /* for disks mask out DPOFUA bit */
996 sp
= (rout
[offset
] & 0x80) ? 1 : 0; /* PS bit becomes 'SELECT's SP bit */
998 rout
[offset
+ 2] = SCSI_IEC_MP_BYTE2_ENABLED
;
999 if (con
->reportscsiioctl
> 2)
1000 rout
[offset
+ 2] |= SCSI_IEC_MP_BYTE2_TEST_MASK
;
1001 rout
[offset
+ 3] = SCSI_IEC_MP_MRIE
;
1002 rout
[offset
+ 4] = (SCSI_IEC_MP_INTERVAL_T
>> 24) & 0xff;
1003 rout
[offset
+ 5] = (SCSI_IEC_MP_INTERVAL_T
>> 16) & 0xff;
1004 rout
[offset
+ 6] = (SCSI_IEC_MP_INTERVAL_T
>> 8) & 0xff;
1005 rout
[offset
+ 7] = SCSI_IEC_MP_INTERVAL_T
& 0xff;
1006 rout
[offset
+ 8] = (SCSI_IEC_MP_REPORT_COUNT
>> 24) & 0xff;
1007 rout
[offset
+ 9] = (SCSI_IEC_MP_REPORT_COUNT
>> 16) & 0xff;
1008 rout
[offset
+ 10] = (SCSI_IEC_MP_REPORT_COUNT
>> 8) & 0xff;
1009 rout
[offset
+ 11] = SCSI_IEC_MP_REPORT_COUNT
& 0xff;
1010 if (iecp
->gotChangeable
) {
1011 UINT8 chg2
= iecp
->raw_chg
[offset
+ 2];
1013 rout
[offset
+ 2] = chg2
? (rout
[offset
+ 2] & chg2
) :
1014 iecp
->raw_curr
[offset
+ 2];
1015 for (k
= 3; k
< 12; ++k
) {
1016 if (0 == iecp
->raw_chg
[offset
+ k
])
1017 rout
[offset
+ k
] = iecp
->raw_curr
[offset
+ k
];
1020 if (0 == memcmp(&rout
[offset
+ 2], &iecp
->raw_chg
[offset
+ 2], 10)) {
1021 if (con
->reportscsiioctl
> 0)
1022 pout("scsiSetExceptionControlAndWarning: already enabled\n");
1025 } else { /* disabling Exception Control and (temperature) Warnings */
1026 eCEnabled
= (rout
[offset
+ 2] & DEXCPT_ENABLE
) ? 0 : 1;
1027 wEnabled
= (rout
[offset
+ 2] & EWASC_ENABLE
) ? 1 : 0;
1028 if ((! eCEnabled
) && (! wEnabled
)) {
1029 if (con
->reportscsiioctl
> 0)
1030 pout("scsiSetExceptionControlAndWarning: already disabled\n");
1031 return 0; /* nothing to do, leave other setting alone */
1034 rout
[offset
+ 2] &= EWASC_DISABLE
;
1036 if (iecp
->gotChangeable
&&
1037 (iecp
->raw_chg
[offset
+ 2] & DEXCPT_ENABLE
))
1038 rout
[offset
+ 2] |= DEXCPT_ENABLE
;
1039 rout
[offset
+ 2] &= TEST_DISABLE
;/* clear TEST bit for spec */
1042 if (10 == iecp
->modese_len
)
1043 err
= scsiModeSelect10(device
, sp
, rout
, resp_len
);
1044 else if (6 == iecp
->modese_len
)
1045 err
= scsiModeSelect(device
, sp
, rout
, resp_len
);
1049 int scsiGetTemp(scsi_device
* device
, UINT8
*currenttemp
, UINT8
*triptemp
)
1054 memset(tBuf
, 0, sizeof(tBuf
));
1055 if ((err
= scsiLogSense(device
, TEMPERATURE_LPAGE
, 0, tBuf
,
1056 sizeof(tBuf
), 0))) {
1059 pout("Log Sense for temperature failed [%s]\n", scsiErrString(err
));
1062 *currenttemp
= tBuf
[9];
1063 *triptemp
= tBuf
[15];
1067 /* Read informational exception log page or Request Sense response.
1068 * Fetching asc/ascq code potentially flagging an exception or warning.
1069 * Returns 0 if ok, else error number. A current temperature of 255
1070 * (Celsius) implies that the temperature not available. */
1071 int scsiCheckIE(scsi_device
* device
, int hasIELogPage
, int hasTempLogPage
,
1072 UINT8
*asc
, UINT8
*ascq
, UINT8
*currenttemp
,
1076 struct scsi_sense_disect sense_info
;
1078 int temperatureSet
= 0;
1079 unsigned short pagesize
;
1080 UINT8 currTemp
, trTemp
;
1086 memset(tBuf
,0,sizeof(tBuf
)); // need to clear stack space of junk
1087 memset(&sense_info
, 0, sizeof(sense_info
));
1089 if ((err
= scsiLogSense(device
, IE_LPAGE
, 0, tBuf
,
1090 sizeof(tBuf
), 0))) {
1091 pout("Log Sense failed, IE page [%s]\n", scsiErrString(err
));
1094 // pull out page size from response, don't forget to add 4
1095 pagesize
= (unsigned short) ((tBuf
[2] << 8) | tBuf
[3]) + 4;
1096 if ((pagesize
< 4) || tBuf
[4] || tBuf
[5]) {
1097 pout("Log Sense failed, IE page, bad parameter code or length\n");
1098 return SIMPLE_ERR_BAD_PARAM
;
1101 sense_info
.asc
= tBuf
[8];
1102 sense_info
.ascq
= tBuf
[9];
1103 if (! hasTempLogPage
) {
1105 *currenttemp
= tBuf
[10];
1106 if (tBuf
[7] > 3) /* IBM extension in SMART (IE) lpage */
1107 *triptemp
= tBuf
[11];
1111 if (0 == sense_info
.asc
) {
1112 /* ties in with MRIE field of 6 in IEC mode page (0x1c) */
1113 if ((err
= scsiRequestSense(device
, &sense_info
))) {
1114 pout("Request Sense failed, [%s]\n", scsiErrString(err
));
1118 *asc
= sense_info
.asc
;
1119 *ascq
= sense_info
.ascq
;
1120 if ((! temperatureSet
) && hasTempLogPage
) {
1121 if (0 == scsiGetTemp(device
, &currTemp
, &trTemp
)) {
1122 *currenttemp
= currTemp
;
1129 // The first character (W, C, I) tells the severity
1130 static const char * TapeAlertsMessageTable
[]= {
1133 "W: The tape drive is having problems reading data. No data has been lost,\n"
1134 " but there has been a reduction in the performance of the tape.",
1136 "W: The tape drive is having problems writing data. No data has been lost,\n"
1137 " but there has been a reduction in the capacity of the tape.",
1139 "W: The operation has stopped because an error has occurred while reading\n"
1140 " or writing data that the drive cannot correct.",
1142 "C: Your data is at risk:\n"
1143 " 1. Copy any data you require from this tape. \n"
1144 " 2. Do not use this tape again.\n"
1145 " 3. Restart the operation with a different tape.",
1147 "C: The tape is damaged or the drive is faulty. Call the tape drive\n"
1148 " supplier helpline.",
1150 "C: The tape is from a faulty batch or the tape drive is faulty:\n"
1151 " 1. Use a good tape to test the drive.\n"
1152 " 2. If problem persists, call the tape drive supplier helpline.",
1154 "W: The tape cartridge has reached the end of its calculated useful life:\n"
1155 " 1. Copy data you need to another tape.\n"
1156 " 2. Discard the old tape.",
1158 "W: The tape cartridge is not data-grade. Any data you back up to the tape\n"
1159 " is at risk. Replace the cartridge with a data-grade tape.",
1161 "C: You are trying to write to a write-protected cartridge. Remove the\n"
1162 " write-protection or use another tape.",
1164 "I: You cannot eject the cartridge because the tape drive is in use. Wait\n"
1165 " until the operation is complete before ejecting the cartridge.",
1167 "I: The tape in the drive is a cleaning cartridge.",
1169 "I: You have tried to load a cartridge of a type which is not supported\n"
1172 "C: The operation has failed because the tape in the drive has experienced\n"
1173 " a mechanical failure:\n"
1174 " 1. Discard the old tape.\n"
1175 " 2. Restart the operation with a different tape.",
1177 "C: The operation has failed because the tape in the drive has experienced\n"
1178 " a mechanical failure:\n"
1179 " 1. Do not attempt to extract the tape cartridge\n"
1180 " 2. Call the tape drive supplier helpline.",
1182 "W: The memory in the tape cartridge has failed, which reduces\n"
1183 " performance. Do not use the cartridge for further write operations.",
1185 "C: The operation has failed because the tape cartridge was manually\n"
1186 " de-mounted while the tape drive was actively writing or reading.",
1188 "W: You have loaded a cartridge of a type that is read-only in this drive.\n"
1189 " The cartridge will appear as write-protected.",
1191 "W: The tape directory on the tape cartridge has been corrupted. File\n"
1192 " search performance will be degraded. The tape directory can be rebuilt\n"
1193 " by reading all the data on the cartridge.",
1195 "I: The tape cartridge is nearing the end of its calculated life. It is\n"
1196 " recommended that you:\n"
1197 " 1. Use another tape cartridge for your next backup.\n"
1198 " 2. Store this tape in a safe place in case you need to restore "
1201 "C: The tape drive needs cleaning:\n"
1202 " 1. If the operation has stopped, eject the tape and clean the drive.\n"
1203 " 2. If the operation has not stopped, wait for it to finish and then\n"
1204 " clean the drive.\n"
1205 " Check the tape drive users manual for device specific cleaning instructions.",
1207 "W: The tape drive is due for routine cleaning:\n"
1208 " 1. Wait for the current operation to finish.\n"
1209 " 2. The use a cleaning cartridge.\n"
1210 " Check the tape drive users manual for device specific cleaning instructions.",
1212 "C: The last cleaning cartridge used in the tape drive has worn out:\n"
1213 " 1. Discard the worn out cleaning cartridge.\n"
1214 " 2. Wait for the current operation to finish.\n"
1215 " 3. Then use a new cleaning cartridge.",
1217 "C: The last cleaning cartridge used in the tape drive was an invalid\n"
1219 " 1. Do not use this cleaning cartridge in this drive.\n"
1220 " 2. Wait for the current operation to finish.\n"
1221 " 3. Then use a new cleaning cartridge.",
1223 "W: The tape drive has requested a retention operation",
1225 "W: A redundant interface port on the tape drive has failed",
1227 "W: A tape drive cooling fan has failed",
1229 "W: A redundant power supply has failed inside the tape drive enclosure.\n"
1230 " Check the enclosure users manual for instructions on replacing the\n"
1231 " failed power supply.",
1233 "W: The tape drive power consumption is outside the specified range.",
1235 "W: Preventive maintenance of the tape drive is required. Check the tape\n"
1236 " drive users manual for device specific preventive maintenance\n"
1237 " tasks or call the tape drive supplier helpline.",
1239 "C: The tape drive has a hardware fault:\n"
1240 " 1. Eject the tape or magazine.\n"
1241 " 2. Reset the drive.\n"
1242 " 3. Restart the operation.",
1244 "C: The tape drive has a hardware fault:\n"
1245 " 1. Turn the tape drive off and then on again.\n"
1246 " 2. Restart the operation.\n"
1247 " 3. If the problem persists, call the tape drive supplier helpline.",
1249 "W: The tape drive has a problem with the application client interface:\n"
1250 " 1. Check the cables and cable connections.\n"
1251 " 2. Restart the operation.",
1253 "C: The operation has failed:\n"
1254 " 1. Eject the tape or magazine.\n"
1255 " 2. Insert the tape or magazine again.\n"
1256 " 3. Restart the operation.",
1258 "W: The firmware download has failed because you have tried to use the\n"
1259 " incorrect firmware for this tape drive. Obtain the correct\n"
1260 " firmware and try again.",
1262 "W: Environmental conditions inside the tape drive are outside the\n"
1263 " specified humidity range.",
1265 "W: Environmental conditions inside the tape drive are outside the\n"
1266 " specified temperature range.",
1268 "W: The voltage supply to the tape drive is outside the specified range.",
1270 "C: A hardware failure of the tape drive is predicted. Call the tape\n"
1271 " drive supplier helpline.",
1273 "W: The tape drive may have a hardware fault. Run extended diagnostics to\n"
1274 " verify and diagnose the problem. Check the tape drive users manual for\n"
1275 " device specific instructions on running extended diagnostic tests.",
1277 "C: The changer mechanism is having difficulty communicating with the tape\n"
1279 " 1. Turn the autoloader off then on.\n"
1280 " 2. Restart the operation.\n"
1281 " 3. If problem persists, call the tape drive supplier helpline.",
1283 "C: A tape has been left in the autoloader by a previous hardware fault:\n"
1284 " 1. Insert an empty magazine to clear the fault.\n"
1285 " 2. If the fault does not clear, turn the autoloader off and then\n"
1287 " 3. If the problem persists, call the tape drive supplier helpline.",
1289 "W: There is a problem with the autoloader mechanism.",
1291 "C: The operation has failed because the autoloader door is open:\n"
1292 " 1. Clear any obstructions from the autoloader door.\n"
1293 " 2. Eject the magazine and then insert it again.\n"
1294 " 3. If the fault does not clear, turn the autoloader off and then\n"
1296 " 4. If the problem persists, call the tape drive supplier helpline.",
1298 "C: The autoloader has a hardware fault:\n"
1299 " 1. Turn the autoloader off and then on again.\n"
1300 " 2. Restart the operation.\n"
1301 " 3. If the problem persists, call the tape drive supplier helpline.\n"
1302 " Check the autoloader users manual for device specific instructions\n"
1303 " on turning the device power on and off.",
1305 "C: The autoloader cannot operate without the magazine,\n"
1306 " 1. Insert the magazine into the autoloader.\n"
1307 " 2. Restart the operation.",
1309 "W: A hardware failure of the changer mechanism is predicted. Call the\n"
1310 " tape drive supplier helpline.",
1318 "W: Media statistics have been lost at some time in the past",
1320 "W: The tape directory on the tape cartridge just unloaded has been\n"
1321 " corrupted. File search performance will be degraded. The tape\n"
1322 " directory can be rebuilt by reading all the data.",
1324 "C: The tape just unloaded could not write its system area successfully:\n"
1325 " 1. Copy data to another tape cartridge.\n"
1326 " 2. Discard the old cartridge.",
1328 "C: The tape system are could not be read successfully at load time:\n"
1329 " 1. Copy data to another tape cartridge.\n",
1331 "C: The start or data could not be found on the tape:\n"
1332 " 1. Check you are using the correct format tape.\n"
1333 " 2. Discard the tape or return the tape to your supplier",
1335 "C: The operation has failed because the media cannot be loaded\n"
1337 " 1. Remove the cartridge, inspect it as specified in the product\n"
1338 " manual, and retry the operation.\n"
1339 " 2. If the problem persists, call the tape drive supplier help line.",
1341 "C: The operation has failed because the medium cannot be unloaded:\n"
1342 " 1. Do not attempt to extract the tape cartridge.\n"
1343 " 2. Call the tape driver supplier help line.",
1345 "C: The tape drive has a problem with the automation interface:\n"
1346 " 1. Check the power to the automation system.\n"
1347 " 2. Check the cables and cable connections.\n"
1348 " 3. Call the supplier help line if problem persists.",
1350 "W: The tape drive has reset itself due to a detected firmware\n"
1351 " fault. If problem persists, call the supplier help line.",
1354 const char * scsiTapeAlertsTapeDevice(unsigned short code
)
1356 const int num
= sizeof(TapeAlertsMessageTable
) /
1357 sizeof(TapeAlertsMessageTable
[0]);
1359 return (code
< num
) ? TapeAlertsMessageTable
[code
] : "Unknown Alert";
1362 // The first character (W, C, I) tells the severity
1363 static const char * ChangerTapeAlertsMessageTable
[]= {
1366 "C: The library mechanism is having difficulty communicating with the\n"
1368 " 1. Turn the library off then on.\n"
1369 " 2. Restart the operation.\n"
1370 " 3. If the problem persists, call the library supplier help line.",
1372 "W: There is a problem with the library mechanism. If problem persists,\n"
1373 " call the library supplier help line.",
1375 "C: The library has a hardware fault:\n"
1376 " 1. Reset the library.\n"
1377 " 2. Restart the operation.\n"
1378 " Check the library users manual for device specific instructions on resetting\n"
1381 "C: The library has a hardware fault:\n"
1382 " 1. Turn the library off then on again.\n"
1383 " 2. Restart the operation.\n"
1384 " 3. If the problem persists, call the library supplier help line.\n"
1385 " Check the library users manual for device specific instructions on turning the\n"
1386 " device power on and off.",
1388 "W: The library mechanism may have a hardware fault.\n"
1389 " Run extended diagnostics to verify and diagnose the problem. Check the library\n"
1390 " users manual for device specific instructions on running extended diagnostic\n"
1393 "C: The library has a problem with the host interface:\n"
1394 " 1. Check the cables and connections.\n"
1395 " 2. Restart the operation.",
1397 "W: A hardware failure of the library is predicted. Call the library\n"
1398 " supplier help line.",
1400 "W: Preventive maintenance of the library is required.\n"
1401 " Check the library users manual for device specific preventative maintenance\n"
1402 " tasks, or call your library supplier help line.",
1404 "C: General environmental conditions inside the library are outside the\n"
1405 " specified humidity range.",
1407 "C: General environmental conditions inside the library are outside the\n"
1408 " specified temperature range.",
1410 "C: The voltage supply to the library is outside the specified range.\n"
1411 " There is a potential problem with the power supply or failure of\n"
1412 " a redundant power supply.",
1414 "C: A cartridge has been left inside the library by a previous hardware\n"
1416 " 1. Insert an empty magazine to clear the fault.\n"
1417 " 2. If the fault does not clear, turn the library off and then on again.\n"
1418 " 3. If the problem persists, call the library supplier help line.",
1420 "W: There is a potential problem with the drive ejecting cartridges or\n"
1421 " with the library mechanism picking a cartridge from a slot.\n"
1422 " 1. No action needs to be taken at this time.\n"
1423 " 2. If the problem persists, call the library supplier help line.",
1425 "W: There is a potential problem with the library mechanism placing a\n"
1426 " cartridge into a slot.\n"
1427 " 1. No action needs to be taken at this time.\n"
1428 " 2. If the problem persists, call the library supplier help line.",
1430 "W: There is a potential problem with the drive or the library mechanism\n"
1431 " loading cartridges, or an incompatible cartridge.",
1433 "C: The library has failed because the door is open:\n"
1434 " 1. Clear any obstructions from the library door.\n"
1435 " 2. Close the library door.\n"
1436 " 3. If the problem persists, call the library supplier help line.",
1438 "C: There is a mechanical problem with the library media import/export\n"
1441 "C: The library cannot operate without the magazine.\n"
1442 " 1. Insert the magazine into the library.\n"
1443 " 2. Restart the operation.",
1445 "W: Library security has been compromised.",
1447 "I: The library security mode has been changed.\n"
1448 " The library has either been put into secure mode, or the library has exited\n"
1449 " the secure mode.\n"
1450 " This is for information purposes only. No action is required.",
1452 "I: The library has been manually turned offline and is unavailable for use.",
1454 "I: A drive inside the library has been taken offline.\n"
1455 " This is for information purposes only. No action is required.",
1457 "W: There is a potential problem with the bar code label or the scanner\n"
1458 " hardware in the library mechanism.\n"
1459 " 1. No action needs to be taken at this time.\n"
1460 " 2. If the problem persists, call the library supplier help line.",
1462 "C: The library has detected an inconsistency in its inventory.\n"
1463 " 1. Redo the library inventory to correct inconsistency.\n"
1464 " 2. Restart the operation.\n"
1465 " Check the applications users manual or the hardware users manual for\n"
1466 " specific instructions on redoing the library inventory.",
1468 "W: A library operation has been attempted that is invalid at this time.",
1470 "W: A redundant interface port on the library has failed.",
1472 "W: A library cooling fan has failed.",
1474 "W: A redundant power supply has failed inside the library. Check the\n"
1475 " library users manual for instructions on replacing the failed power supply.",
1477 "W: The library power consumption is outside the specified range.",
1479 "C: A failure has occurred in the cartridge pass-through mechanism between\n"
1480 " two library modules.",
1482 "C: A cartridge has been left in the pass-through mechanism from a\n"
1483 " previous hardware fault. Check the library users guide for instructions on\n"
1484 " clearing this fault.",
1486 "I: The library was unable to read the bar code on a cartridge.",
1489 const char * scsiTapeAlertsChangerDevice(unsigned short code
)
1491 const int num
= sizeof(ChangerTapeAlertsMessageTable
) /
1492 sizeof(ChangerTapeAlertsMessageTable
[0]);
1494 return (code
< num
) ? ChangerTapeAlertsMessageTable
[code
] : "Unknown Alert";
1498 /* this is a subset of the SCSI additional sense code strings indexed
1499 * by "ascq" for the case when asc==SCSI_ASC_IMPENDING_FAILURE (0x5d)
1501 static const char * strs_for_asc_5d
[] = {
1502 /* 0x00 */ "FAILURE PREDICTION THRESHOLD EXCEEDED",
1503 "MEDIA FAILURE PREDICTION THRESHOLD EXCEEDED",
1504 "LOGICAL UNIT FAILURE PREDICTION THRESHOLD EXCEEDED",
1505 "SPARE AREA EXHAUSTION PREDICTION THRESHOLD EXCEEDED",
1518 /* 0x10 */ "HARDWARE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1519 "HARDWARE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1520 "HARDWARE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1521 "HARDWARE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1522 "HARDWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1523 "HARDWARE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1524 "HARDWARE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1525 "HARDWARE IMPENDING FAILURE CHANNEL PARAMETRICS",
1526 "HARDWARE IMPENDING FAILURE CONTROLLER DETECTED",
1527 "HARDWARE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1528 "HARDWARE IMPENDING FAILURE SEEK TIME PERFORMANCE",
1529 "HARDWARE IMPENDING FAILURE SPIN-UP RETRY COUNT",
1530 "HARDWARE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1534 /* 0x20 */ "CONTROLLER IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1535 "CONTROLLER IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1536 "CONTROLLER IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1537 "CONTROLLER IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1538 "CONTROLLER IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1539 "CONTROLLER IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1540 "CONTROLLER IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1541 "CONTROLLER IMPENDING FAILURE CHANNEL PARAMETRICS",
1542 "CONTROLLER IMPENDING FAILURE CONTROLLER DETECTED",
1543 "CONTROLLER IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1544 "CONTROLLER IMPENDING FAILURE SEEK TIME PERFORMANCE",
1545 "CONTROLLER IMPENDING FAILURE SPIN-UP RETRY COUNT",
1546 "CONTROLLER IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1550 /* 0x30 */ "DATA CHANNEL IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1551 "DATA CHANNEL IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1552 "DATA CHANNEL IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1553 "DATA CHANNEL IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1554 "DATA CHANNEL IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1555 "DATA CHANNEL IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1556 "DATA CHANNEL IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1557 "DATA CHANNEL IMPENDING FAILURE CHANNEL PARAMETRICS",
1558 "DATA CHANNEL IMPENDING FAILURE CONTROLLER DETECTED",
1559 "DATA CHANNEL IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1560 "DATA CHANNEL IMPENDING FAILURE SEEK TIME PERFORMANCE",
1561 "DATA CHANNEL IMPENDING FAILURE SPIN-UP RETRY COUNT",
1562 "DATA CHANNEL IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1566 /* 0x40 */ "SERVO IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1567 "SERVO IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1568 "SERVO IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1569 "SERVO IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1570 "SERVO IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1571 "SERVO IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1572 "SERVO IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1573 "SERVO IMPENDING FAILURE CHANNEL PARAMETRICS",
1574 "SERVO IMPENDING FAILURE CONTROLLER DETECTED",
1575 "SERVO IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1576 "SERVO IMPENDING FAILURE SEEK TIME PERFORMANCE",
1577 "SERVO IMPENDING FAILURE SPIN-UP RETRY COUNT",
1578 "SERVO IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1582 /* 0x50 */ "SPINDLE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1583 "SPINDLE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1584 "SPINDLE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1585 "SPINDLE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1586 "SPINDLE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1587 "SPINDLE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1588 "SPINDLE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1589 "SPINDLE IMPENDING FAILURE CHANNEL PARAMETRICS",
1590 "SPINDLE IMPENDING FAILURE CONTROLLER DETECTED",
1591 "SPINDLE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1592 "SPINDLE IMPENDING FAILURE SEEK TIME PERFORMANCE",
1593 "SPINDLE IMPENDING FAILURE SPIN-UP RETRY COUNT",
1594 "SPINDLE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1598 /* 0x60 */ "FIRMWARE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1599 "FIRMWARE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1600 "FIRMWARE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1601 "FIRMWARE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1602 "FIRMWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1603 "FIRMWARE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1604 "FIRMWARE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1605 "FIRMWARE IMPENDING FAILURE CHANNEL PARAMETRICS",
1606 "FIRMWARE IMPENDING FAILURE CONTROLLER DETECTED",
1607 "FIRMWARE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1608 "FIRMWARE IMPENDING FAILURE SEEK TIME PERFORMANCE",
1609 "FIRMWARE IMPENDING FAILURE SPIN-UP RETRY COUNT",
1610 /* 0x6c */ "FIRMWARE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT"};
1613 /* this is a subset of the SCSI additional sense code strings indexed
1614 * * by "ascq" for the case when asc==SCSI_ASC_WARNING (0xb)
1616 static const char * strs_for_asc_b
[] = {
1617 /* 0x00 */ "WARNING",
1618 "WARNING - SPECIFIED TEMPERATURE EXCEEDED",
1619 "WARNING - ENCLOSURE DEGRADED"};
1621 static char spare_buff
[128];
1623 const char * scsiGetIEString(UINT8 asc
, UINT8 ascq
)
1627 if (SCSI_ASC_IMPENDING_FAILURE
== asc
) {
1629 return "FAILURE PREDICTION THRESHOLD EXCEEDED (FALSE)";
1631 (sizeof(strs_for_asc_5d
) / sizeof(strs_for_asc_5d
[0]))) {
1632 rp
= strs_for_asc_5d
[ascq
];
1636 snprintf(spare_buff
, sizeof(spare_buff
),
1637 "FAILURE PREDICTION THRESHOLD EXCEEDED: ascq=0x%x", ascq
);
1639 } else if (SCSI_ASC_WARNING
== asc
) {
1640 if (ascq
< (sizeof(strs_for_asc_b
) / sizeof(strs_for_asc_b
[0]))) {
1641 rp
= strs_for_asc_b
[ascq
];
1645 snprintf(spare_buff
, sizeof(spare_buff
), "WARNING: ascq=0x%x", ascq
);
1648 return NULL
; /* not a IE additional sense code */
1652 /* This is not documented in t10.org, page 0x80 is vendor specific */
1653 /* Some IBM disks do an offline read-scan when they get this command. */
1654 int scsiSmartIBMOfflineTest(scsi_device
* device
)
1659 memset(tBuf
, 0, sizeof(tBuf
));
1660 /* Build SMART Off-line Immediate Diag Header */
1661 tBuf
[0] = 0x80; /* Page Code */
1662 tBuf
[1] = 0x00; /* Reserved */
1663 tBuf
[2] = 0x00; /* Page Length MSB */
1664 tBuf
[3] = 0x04; /* Page Length LSB */
1665 tBuf
[4] = 0x03; /* SMART Revision */
1666 tBuf
[5] = 0x00; /* Reserved */
1667 tBuf
[6] = 0x00; /* Off-line Immediate Time MSB */
1668 tBuf
[7] = 0x00; /* Off-line Immediate Time LSB */
1669 res
= scsiSendDiagnostic(device
, SCSI_DIAG_NO_SELF_TEST
, tBuf
, 8);
1671 pout("IBM offline test failed [%s]\n", scsiErrString(res
));
1675 int scsiSmartDefaultSelfTest(scsi_device
* device
)
1679 res
= scsiSendDiagnostic(device
, SCSI_DIAG_DEF_SELF_TEST
, NULL
, 0);
1681 pout("Default self test failed [%s]\n", scsiErrString(res
));
1685 int scsiSmartShortSelfTest(scsi_device
* device
)
1689 res
= scsiSendDiagnostic(device
, SCSI_DIAG_BG_SHORT_SELF_TEST
, NULL
, 0);
1691 pout("Short offline self test failed [%s]\n", scsiErrString(res
));
1695 int scsiSmartExtendSelfTest(scsi_device
* device
)
1699 res
= scsiSendDiagnostic(device
, SCSI_DIAG_BG_EXTENDED_SELF_TEST
, NULL
, 0);
1701 pout("Long (extended) offline self test failed [%s]\n",
1702 scsiErrString(res
));
1706 int scsiSmartShortCapSelfTest(scsi_device
* device
)
1710 res
= scsiSendDiagnostic(device
, SCSI_DIAG_FG_SHORT_SELF_TEST
, NULL
, 0);
1712 pout("Short foreground self test failed [%s]\n", scsiErrString(res
));
1716 int scsiSmartExtendCapSelfTest(scsi_device
* device
)
1720 res
= scsiSendDiagnostic(device
, SCSI_DIAG_FG_EXTENDED_SELF_TEST
, NULL
, 0);
1722 pout("Long (extended) foreground self test failed [%s]\n",
1723 scsiErrString(res
));
1727 int scsiSmartSelfTestAbort(scsi_device
* device
)
1731 res
= scsiSendDiagnostic(device
, SCSI_DIAG_ABORT_SELF_TEST
, NULL
, 0);
1733 pout("Abort self test failed [%s]\n", scsiErrString(res
));
1737 /* Returns 0 and the expected duration of an extended self test (in seconds)
1738 if successful; any other return value indicates a failure. */
1739 int scsiFetchExtendedSelfTestTime(scsi_device
* device
, int * durationSec
, int modese_len
)
1741 int err
, offset
, res
;
1744 memset(buff
, 0, sizeof(buff
));
1745 if (modese_len
<= 6) {
1746 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
1747 MPAGE_CONTROL_CURRENT
,
1748 buff
, sizeof(buff
)))) {
1749 if (SIMPLE_ERR_BAD_OPCODE
== err
)
1753 } else if (0 == modese_len
)
1756 if (10 == modese_len
) {
1757 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
1758 MPAGE_CONTROL_CURRENT
,
1759 buff
, sizeof(buff
));
1763 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
1766 if (buff
[offset
+ 1] >= 0xa) {
1767 res
= (buff
[offset
+ 10] << 8) | buff
[offset
+ 11];
1775 void scsiDecodeErrCounterPage(unsigned char * resp
,
1776 struct scsiErrorCounter
*ecp
)
1778 int k
, j
, num
, pl
, pc
;
1779 unsigned char * ucp
;
1783 memset(ecp
, 0, sizeof(*ecp
));
1784 num
= (resp
[2] << 8) | resp
[3];
1787 pc
= (ucp
[0] << 8) | ucp
[1];
1798 ullp
= &ecp
->counter
[pc
];
1801 ecp
->gotExtraPC
= 1;
1802 ullp
= &ecp
->counter
[7];
1807 if (k
> (int)sizeof(*ullp
)) {
1808 xp
+= (k
- sizeof(*ullp
));
1812 for (j
= 0; j
< k
; ++j
) {
1822 void scsiDecodeNonMediumErrPage(unsigned char *resp
,
1823 struct scsiNonMediumError
*nmep
)
1825 int k
, j
, num
, pl
, pc
, szof
;
1826 unsigned char * ucp
;
1829 memset(nmep
, 0, sizeof(*nmep
));
1830 num
= (resp
[2] << 8) | resp
[3];
1832 szof
= sizeof(nmep
->counterPC0
);
1834 pc
= (ucp
[0] << 8) | ucp
[1];
1845 nmep
->counterPC0
= 0;
1846 for (j
= 0; j
< k
; ++j
) {
1848 nmep
->counterPC0
<<= 8;
1849 nmep
->counterPC0
|= xp
[j
];
1860 nmep
->counterTFE_H
= 0;
1861 for (j
= 0; j
< k
; ++j
) {
1863 nmep
->counterTFE_H
<<= 8;
1864 nmep
->counterTFE_H
|= xp
[j
];
1875 nmep
->counterPE_H
= 0;
1876 for (j
= 0; j
< k
; ++j
) {
1878 nmep
->counterPE_H
<<= 8;
1879 nmep
->counterPE_H
|= xp
[j
];
1883 nmep
->gotExtraPC
= 1;
1891 /* Counts number of failed self-tests. Also encodes the poweron_hour
1892 of the most recent failed self-test. Return value is negative if
1893 this function has a problem (typically -1), otherwise the bottom 8
1894 bits are the number of failed self tests and the 16 bits above that
1895 are the poweron hour of the most recent failure. Note: aborted self
1896 tests (typically by the user) and self tests in progress are not
1897 considered failures. See Working Draft SCSI Primary Commands - 3
1898 (SPC-3) section 7.2.10 T10/1416-D (rev 22a) */
1899 int scsiCountFailedSelfTests(scsi_device
* fd
, int noisy
)
1901 int num
, k
, n
, err
, res
, fails
, fail_hour
;
1903 unsigned char resp
[LOG_RESP_SELF_TEST_LEN
];
1905 if ((err
= scsiLogSense(fd
, SELFTEST_RESULTS_LPAGE
, 0, resp
,
1906 LOG_RESP_SELF_TEST_LEN
, 0))) {
1908 pout("scsiCountSelfTests Failed [%s]\n", scsiErrString(err
));
1911 if ((resp
[0] & 0x3f) != SELFTEST_RESULTS_LPAGE
) {
1913 pout("Self-test Log Sense Failed, page mismatch\n");
1916 // compute page length
1917 num
= (resp
[2] << 8) + resp
[3];
1918 // Log sense page length 0x190 bytes
1921 pout("Self-test Log Sense length is 0x%x not 0x190 bytes\n", num
);
1926 // loop through the twenty possible entries
1927 for (k
= 0, ucp
= resp
+ 4; k
< 20; ++k
, ucp
+= 20 ) {
1929 // timestamp in power-on hours (or zero if test in progress)
1930 n
= (ucp
[6] << 8) | ucp
[7];
1932 // The spec says "all 20 bytes will be zero if no test" but
1933 // DG has found otherwise. So this is a heuristic.
1934 if ((0 == n
) && (0 == ucp
[4]))
1937 if ((res
> 2) && (res
< 8)) {
1940 fail_hour
= (ucp
[6] << 8) + ucp
[7];
1943 return (fail_hour
<< 8) + fails
;
1946 /* Returns 0 if able to read self test log page; then outputs 1 into
1947 *inProgress if self test still in progress, else outputs 0. */
1948 int scsiSelfTestInProgress(scsi_device
* fd
, int * inProgress
)
1952 unsigned char resp
[LOG_RESP_SELF_TEST_LEN
];
1954 if (scsiLogSense(fd
, SELFTEST_RESULTS_LPAGE
, 0, resp
,
1955 LOG_RESP_SELF_TEST_LEN
, 0))
1957 if (resp
[0] != SELFTEST_RESULTS_LPAGE
)
1959 // compute page length
1960 num
= (resp
[2] << 8) + resp
[3];
1961 // Log sense page length 0x190 bytes
1967 *inProgress
= (0xf == (ucp
[4] & 0xf)) ? 1 : 0;
1971 /* Returns a negative value if failed to fetch Contol mode page or it was
1972 malformed. Returns 0 if GLTSD bit is zero and returns 1 if the GLTSD
1973 bit is set. Examines default mode page when current==0 else examines
1974 current mode page. */
1975 int scsiFetchControlGLTSD(scsi_device
* device
, int modese_len
, int current
)
1979 int pc
= current
? MPAGE_CONTROL_CURRENT
: MPAGE_CONTROL_DEFAULT
;
1981 memset(buff
, 0, sizeof(buff
));
1982 if (modese_len
<= 6) {
1983 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0, pc
,
1984 buff
, sizeof(buff
)))) {
1985 if (SIMPLE_ERR_BAD_OPCODE
== err
)
1989 } else if (0 == modese_len
)
1992 if (10 == modese_len
) {
1993 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0, pc
,
1994 buff
, sizeof(buff
));
1998 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
1999 if ((offset
>= 0) && (buff
[offset
+ 1] >= 0xa))
2000 return (buff
[offset
+ 2] & 2) ? 1 : 0;
2004 /* Attempts to set or clear GLTSD bit in Control mode page. If enabled is
2005 0 attempts to clear GLTSD otherwise it attempts to set it. Returns 0 if
2006 successful, negative if low level error, > 0 if higher level error (e.g.
2007 SIMPLE_ERR_BAD_PARAM if GLTSD bit is not changeable). */
2008 int scsiSetControlGLTSD(scsi_device
* device
, int enabled
, int modese_len
)
2010 int err
, offset
, resp_len
, sp
;
2014 memset(buff
, 0, sizeof(buff
));
2015 if (modese_len
<= 6) {
2016 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2017 MPAGE_CONTROL_CURRENT
,
2018 buff
, sizeof(buff
)))) {
2019 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2023 } else if (0 == modese_len
)
2026 if (10 == modese_len
) {
2027 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2028 MPAGE_CONTROL_CURRENT
,
2029 buff
, sizeof(buff
));
2033 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2034 if ((offset
< 0) || (buff
[offset
+ 1] < 0xa))
2035 return SIMPLE_ERR_BAD_RESP
;
2039 if (enabled
== (buff
[offset
+ 2] & 2))
2040 return 0; /* GLTSD already in wanted state so nothing to do */
2042 if (modese_len
== 6)
2043 err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2044 MPAGE_CONTROL_CHANGEABLE
,
2045 ch_buff
, sizeof(ch_buff
));
2047 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2048 MPAGE_CONTROL_CHANGEABLE
,
2049 ch_buff
, sizeof(ch_buff
));
2052 if (0 == (ch_buff
[offset
+ 2] & 2))
2053 return SIMPLE_ERR_BAD_PARAM
; /* GLTSD bit not chageable */
2055 if (10 == modese_len
) {
2056 resp_len
= (buff
[0] << 8) + buff
[1] + 2;
2057 buff
[3] &= 0xef; /* for disks mask out DPOFUA bit */
2059 resp_len
= buff
[0] + 1;
2060 buff
[2] &= 0xef; /* for disks mask out DPOFUA bit */
2062 sp
= (buff
[offset
] & 0x80) ? 1 : 0; /* PS bit becomes 'SELECT's SP bit */
2064 buff
[offset
+ 2] |= 0x2; /* set GLTSD bit */
2066 buff
[offset
+ 2] &= 0xfd; /* clear GLTSD bit */
2067 if (10 == modese_len
)
2068 err
= scsiModeSelect10(device
, sp
, buff
, resp_len
);
2069 else if (6 == modese_len
)
2070 err
= scsiModeSelect(device
, sp
, buff
, resp_len
);
2074 /* Returns a negative value if failed to fetch Protocol specific port mode
2075 page or it was malformed. Returns transport protocol identifier when
2077 int scsiFetchTransportProtocol(scsi_device
* device
, int modese_len
)
2082 memset(buff
, 0, sizeof(buff
));
2083 if (modese_len
<= 6) {
2084 if ((err
= scsiModeSense(device
, PROTOCOL_SPECIFIC_PORT_PAGE
, 0,
2085 MPAGE_CONTROL_CURRENT
,
2086 buff
, sizeof(buff
)))) {
2087 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2091 } else if (0 == modese_len
)
2094 if (10 == modese_len
) {
2095 err
= scsiModeSense10(device
, PROTOCOL_SPECIFIC_PORT_PAGE
, 0,
2096 MPAGE_CONTROL_CURRENT
,
2097 buff
, sizeof(buff
));
2101 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2102 if ((offset
>= 0) && (buff
[offset
+ 1] > 1)) {
2103 if ((0 == (buff
[offset
] & 0x40)) && /* SPF==0 */
2104 (PROTOCOL_SPECIFIC_PORT_PAGE
== (buff
[offset
] & 0x3f)))
2105 return (buff
[offset
+ 2] & 0xf);