4 * Home page of code is: http://www.smartmontools.org
6 * Copyright (C) 2002-8 Bruce Allen
7 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
9 * Copyright (C) 2003-16 Douglas Gilbert <dgilbert@interlog.com>
12 * Additional SCSI work:
13 * Copyright (C) 2003-15 Douglas Gilbert <dgilbert@interlog.com>
14 >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2, or (at your option)
21 * You should have received a copy of the GNU General Public License
22 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
24 * This code was originally developed as a Senior Thesis by Michael Cornwell
25 * at the Concurrent Systems Laboratory (now part of the Storage Systems
26 * Research Center), Jack Baskin School of Engineering, University of
27 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
30 * In the SCSI world "SMART" is a dead or withdrawn standard. In recent
31 * SCSI standards (since SCSI-3) it goes under the awkward name of
32 * "Informational Exceptions" ["IE" or "IEC" (with the "C" for "control")].
33 * The relevant information is spread around several SCSI draft
34 * standards available at http://www.t10.org . Reference is made in the
35 * code to the following acronyms:
36 * - SAM [SCSI Architectural model, versions 2 or 3]
37 * - SPC [SCSI Primary commands, versions 2 or 3]
38 * - SBC [SCSI Block commands, versions 2]
40 * Some SCSI disk vendors have snippets of "SMART" information in their
52 #include "atacmds.h" // FIXME: for smart_command_set only
53 #include "dev_interface.h"
57 const char *scsicmds_c_cvsid
="$Id: scsicmds.cpp 4243 2016-03-20 18:29:36Z chrfranke $"
59 const char *scsicmds_c_cvsid
="$Id: scsicmds.cpp 4081 2015-05-10 16:42:50Z chrfranke $"
60 >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74
63 // Print SCSI debug messages?
64 unsigned char scsi_debugmode
= 0;
66 supported_vpd_pages
* supported_vpd_pages_p
= NULL
;
69 supported_vpd_pages::supported_vpd_pages(scsi_device
* device
) : num_valid(0)
71 unsigned char b
[0xfc]; /* pre SPC-3 INQUIRY max response size */
72 memset(b
, 0, sizeof(b
));
73 if (device
&& (0 == scsiInquiryVpd(device
, SCSI_VPD_SUPPORTED_VPD_PAGES
,
75 num_valid
= (b
[2] << 8) + b
[3];
76 int n
= sizeof(pages
);
79 memcpy(pages
, b
+ 4, num_valid
);
84 supported_vpd_pages::is_supported(int vpd_page_num
) const
86 /* Supported VPD pages numbers start at offset 4 and should be in
87 * ascending order but don't assume that. */
88 for (int k
= 0; k
< num_valid
; ++k
) {
89 if (vpd_page_num
== pages
[k
])
95 /* output binary in hex and optionally ascii */
97 dStrHex(const char* str
, int len
, int no_ascii
)
102 const int bpstart
= 5;
103 const int cpstart
= 60;
108 if (len
<= 0) return;
111 k
= snprintf(buff
+1, sizeof(buff
)-1, "%.2x", a
);
113 if (bpos
>= ((bpstart
+ (9 * 3))))
116 for(i
= 0; i
< len
; i
++)
118 unsigned char c
= *p
++;
120 if (bpos
== (bpstart
+ (9 * 3)))
122 snprintf(buff
+bpos
, sizeof(buff
)-bpos
, "%.2x", (int)(unsigned char)c
);
123 buff
[bpos
+ 2] = ' ';
127 if ((c
< ' ') || (c
>= 0x7f))
131 if (cpos
> (cpstart
+15))
133 while (cpos
> 0 && buff
[cpos
-1] == ' ')
141 k
= snprintf(buff
+1, sizeof(buff
)-1, "%.2x", a
);
147 while (cpos
> 0 && buff
[cpos
-1] == ' ')
154 struct scsi_opcode_name
{
159 static struct scsi_opcode_name opcode_name_arr
[] = {
160 /* in ascending opcode order */
161 {TEST_UNIT_READY
, "test unit ready"}, /* 0x00 */
162 {REQUEST_SENSE
, "request sense"}, /* 0x03 */
163 {INQUIRY
, "inquiry"}, /* 0x12 */
164 {MODE_SELECT
, "mode select(6)"}, /* 0x15 */
165 {MODE_SENSE
, "mode sense(6)"}, /* 0x1a */
166 {START_STOP_UNIT
, "start stop unit"}, /* 0x1b */
167 {RECEIVE_DIAGNOSTIC
, "receive diagnostic"}, /* 0x1c */
168 {SEND_DIAGNOSTIC
, "send diagnostic"}, /* 0x1d */
169 {READ_CAPACITY_10
, "read capacity(10)"}, /* 0x25 */
170 {READ_DEFECT_10
, "read defect list(10)"}, /* 0x37 */
171 {LOG_SELECT
, "log select"}, /* 0x4c */
172 {LOG_SENSE
, "log sense"}, /* 0x4d */
173 {MODE_SELECT_10
, "mode select(10)"}, /* 0x55 */
174 {MODE_SENSE_10
, "mode sense(10)"}, /* 0x5a */
175 {SAT_ATA_PASSTHROUGH_16
, "ata pass-through(16)"}, /* 0x85 */
176 {READ_CAPACITY_16
, "read capacity(16)"}, /* 0x9e,0x10 */
177 {REPORT_LUNS
, "report luns"}, /* 0xa0 */
178 {SAT_ATA_PASSTHROUGH_12
, "ata pass-through(12)"}, /* 0xa1 */
179 {READ_DEFECT_12
, "read defect list(12)"}, /* 0xb7 */
182 static const char * vendor_specific
= "<vendor specific>";
184 /* Need to expand to take service action into account. For commands
185 * of interest the service action is in the 2nd command byte */
187 scsi_get_opcode_name(UINT8 opcode
)
189 int len
= sizeof(opcode_name_arr
) / sizeof(opcode_name_arr
[0]);
192 return vendor_specific
;
193 for (int k
= 0; k
< len
; ++k
) {
194 struct scsi_opcode_name
* onp
= &opcode_name_arr
[k
];
195 if (opcode
== onp
->opcode
)
197 else if (opcode
< onp
->opcode
)
204 scsi_do_sense_disect(const struct scsi_cmnd_io
* io_buf
,
205 struct scsi_sense_disect
* out
)
207 memset(out
, 0, sizeof(struct scsi_sense_disect
));
208 if (SCSI_STATUS_CHECK_CONDITION
== io_buf
->scsi_status
) {
209 int resp_code
= (io_buf
->sensep
[0] & 0x7f);
210 out
->resp_code
= resp_code
;
211 if (resp_code
>= 0x72) {
212 out
->sense_key
= (io_buf
->sensep
[1] & 0xf);
213 out
->asc
= io_buf
->sensep
[2];
214 out
->ascq
= io_buf
->sensep
[3];
215 } else if (resp_code
>= 0x70) {
216 out
->sense_key
= (io_buf
->sensep
[2] & 0xf);
217 if (io_buf
->resp_sense_len
> 13) {
218 out
->asc
= io_buf
->sensep
[12];
219 out
->ascq
= io_buf
->sensep
[13];
226 scsiSimpleSenseFilter(const struct scsi_sense_disect
* sinfo
)
228 switch (sinfo
->sense_key
) {
229 case SCSI_SK_NO_SENSE
:
230 case SCSI_SK_RECOVERED_ERR
:
231 return SIMPLE_NO_ERROR
;
232 case SCSI_SK_NOT_READY
:
233 if (SCSI_ASC_NO_MEDIUM
== sinfo
->asc
)
234 return SIMPLE_ERR_NO_MEDIUM
;
235 else if (SCSI_ASC_NOT_READY
== sinfo
->asc
) {
236 if (0x1 == sinfo
->ascq
)
237 return SIMPLE_ERR_BECOMING_READY
;
239 return SIMPLE_ERR_NOT_READY
;
241 return SIMPLE_ERR_NOT_READY
;
242 case SCSI_SK_MEDIUM_ERROR
:
243 case SCSI_SK_HARDWARE_ERROR
:
244 return SIMPLE_ERR_MEDIUM_HARDWARE
;
245 case SCSI_SK_ILLEGAL_REQUEST
:
246 if (SCSI_ASC_UNKNOWN_OPCODE
== sinfo
->asc
)
247 return SIMPLE_ERR_BAD_OPCODE
;
248 else if (SCSI_ASC_INVALID_FIELD
== sinfo
->asc
)
249 return SIMPLE_ERR_BAD_FIELD
;
250 else if (SCSI_ASC_UNKNOWN_PARAM
== sinfo
->asc
)
251 return SIMPLE_ERR_BAD_PARAM
;
253 return SIMPLE_ERR_BAD_PARAM
; /* all other illegal request */
254 case SCSI_SK_UNIT_ATTENTION
:
255 return SIMPLE_ERR_TRY_AGAIN
;
256 case SCSI_SK_ABORTED_COMMAND
:
257 return SIMPLE_ERR_ABORTED_COMMAND
;
259 return SIMPLE_ERR_UNKNOWN
;
264 scsiErrString(int scsiErr
)
267 return strerror(-scsiErr
);
269 case SIMPLE_NO_ERROR
:
271 case SIMPLE_ERR_NOT_READY
:
272 return "device not ready";
273 case SIMPLE_ERR_BAD_OPCODE
:
274 return "unsupported scsi opcode";
275 case SIMPLE_ERR_BAD_FIELD
:
276 return "unsupported field in scsi command";
277 case SIMPLE_ERR_BAD_PARAM
:
278 return "badly formed scsi parameters";
279 case SIMPLE_ERR_BAD_RESP
:
280 return "scsi response fails sanity test";
281 case SIMPLE_ERR_NO_MEDIUM
:
282 return "no medium present";
283 case SIMPLE_ERR_BECOMING_READY
:
284 return "device will be ready soon";
285 case SIMPLE_ERR_TRY_AGAIN
:
286 return "unit attention reported, try again";
287 case SIMPLE_ERR_MEDIUM_HARDWARE
:
288 return "medium or hardware error (serious)";
289 case SIMPLE_ERR_UNKNOWN
:
290 return "unknown error (unexpected sense key)";
291 case SIMPLE_ERR_ABORTED_COMMAND
:
292 return "aborted command";
294 return "unknown error";
298 /* Iterates to next designation descriptor in the device identification
299 * VPD page. The 'initial_desig_desc' should point to start of first
300 * descriptor with 'page_len' being the number of valid bytes in that
301 * and following descriptors. To start, 'off' should point to a negative
302 * value, thereafter it should point to the value yielded by the previous
303 * call. If 0 returned then 'initial_desig_desc + *off' should be a valid
304 * descriptor; returns -1 if normal end condition and -2 for an abnormal
305 * termination. Matches association, designator_type and/or code_set when
306 * any of those values are greater than or equal to zero. */
308 scsi_vpd_dev_id_iter(const unsigned char * initial_desig_desc
, int page_len
,
309 int * off
, int m_assoc
, int m_desig_type
, int m_code_set
)
311 const unsigned char * ucp
;
314 for (k
= *off
, ucp
= initial_desig_desc
; (k
+ 3) < page_len
; ) {
315 k
= (k
< 0) ? 0 : (k
+ ucp
[k
+ 3] + 4);
316 if ((k
+ 4) > page_len
)
318 int c_set
= (ucp
[k
] & 0xf);
319 if ((m_code_set
>= 0) && (m_code_set
!= c_set
))
321 int assoc
= ((ucp
[k
+ 1] >> 4) & 0x3);
322 if ((m_assoc
>= 0) && (m_assoc
!= assoc
))
324 int desig_type
= (ucp
[k
+ 1] & 0xf);
325 if ((m_desig_type
>= 0) && (m_desig_type
!= desig_type
))
330 return (k
== page_len
) ? -1 : -2;
333 /* Decode VPD page 0x83 logical unit designator into a string. If both
334 * numeric address and SCSI name string present, prefer the former.
335 * Returns 0 on success, -1 on error with error string in s. */
337 scsi_decode_lu_dev_id(const unsigned char * b
, int blen
, char * s
, int slen
,
350 int have_scsi_ns
= 0;
353 while ((u
= scsi_vpd_dev_id_iter(b
, blen
, &off
, -1, -1, -1)) == 0) {
354 const unsigned char * ucp
= b
+ off
;
356 if ((off
+ i_len
+ 4) > blen
) {
357 snprintf(s
+si
, slen
-si
, "error: designator length");
360 int assoc
= ((ucp
[1] >> 4) & 0x3);
361 if (transport
&& assoc
&& (ucp
[1] & 0x80) && (*transport
< 0))
362 *transport
= (ucp
[0] >> 4) & 0xf;
365 const unsigned char * ip
= ucp
+ 4;
366 int c_set
= (ucp
[0] & 0xf);
367 int desig_type
= (ucp
[1] & 0xf);
370 switch (desig_type
) {
371 case 0: /* vendor specific */
372 case 1: /* T10 vendor identification */
374 case 2: /* EUI-64 based */
375 if ((8 != i_len
) && (12 != i_len
) && (16 != i_len
)) {
376 snprintf(s
+si
, slen
-si
, "error: EUI-64 length");
381 si
+= snprintf(s
+si
, slen
-si
, "0x");
382 for (int m
= 0; m
< i_len
; ++m
)
383 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
387 snprintf(s
+si
, slen
-si
, "error: NAA bad code_set");
390 naa
= (ip
[0] >> 4) & 0xff;
391 if ((naa
< 2) || (naa
> 6) || (4 == naa
)) {
392 snprintf(s
+si
, slen
-si
, "error: unexpected NAA");
397 if (2 == naa
) { /* NAA IEEE Extended */
399 snprintf(s
+si
, slen
-si
, "error: NAA 2 length");
402 si
+= snprintf(s
+si
, slen
-si
, "0x");
403 for (int m
= 0; m
< 8; ++m
)
404 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
405 } else if ((3 == naa
) || (5 == naa
)) {
406 /* NAA=3 Locally assigned; NAA=5 IEEE Registered */
408 snprintf(s
+si
, slen
-si
, "error: NAA 3 or 5 length");
411 si
+= snprintf(s
+si
, slen
-si
, "0x");
412 for (int m
= 0; m
< 8; ++m
)
413 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
414 } else if (6 == naa
) { /* NAA IEEE Registered extended */
416 snprintf(s
+si
, slen
-si
, "error: NAA 6 length");
419 si
+= snprintf(s
+si
, slen
-si
, "0x");
420 for (int m
= 0; m
< 16; ++m
)
421 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
424 case 4: /* Relative target port */
425 case 5: /* (primary) Target port group */
426 case 6: /* Logical unit group */
427 case 7: /* MD5 logical unit identifier */
429 case 8: /* SCSI name string */
431 snprintf(s
+si
, slen
-si
, "error: SCSI name string");
434 /* does %s print out UTF-8 ok?? */
436 si
+= snprintf(s
+si
, slen
-si
, "%s", (const char *)ip
);
440 default: /* reserved */
445 snprintf(s
+si
, slen
-si
, "error: bad structure");
451 /* Sends LOG SENSE command. Returns 0 if ok, 1 if device NOT READY, 2 if
452 command not supported, 3 if field (within command) not supported or
453 returns negated errno. SPC-3 sections 6.6 and 7.2 (rec 22a).
454 N.B. Sets PC==1 to fetch "current cumulative" log pages.
455 If known_resp_len > 0 then a single fetch is done for this response
456 length. If known_resp_len == 0 then twin fetches are performed, the
457 first to deduce the response length, then send the same command again
458 requesting the deduced response length. This protects certain fragile
459 HBAs. The twin fetch technique should not be used with the TapeAlert
460 log page since it clears its state flags after each fetch. */
462 scsiLogSense(scsi_device
* device
, int pagenum
, int subpagenum
, UINT8
*pBuf
,
463 int bufLen
, int known_resp_len
)
465 struct scsi_cmnd_io io_hdr
;
466 struct scsi_sense_disect sinfo
;
471 if (known_resp_len
> bufLen
)
473 if (known_resp_len
> 0)
474 pageLen
= known_resp_len
;
476 /* Starting twin fetch strategy: first fetch to find respone length */
478 if (pageLen
> bufLen
)
481 memset(pBuf
, 0, pageLen
);
483 memset(&io_hdr
, 0, sizeof(io_hdr
));
484 memset(cdb
, 0, sizeof(cdb
));
485 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
486 io_hdr
.dxfer_len
= pageLen
;
487 io_hdr
.dxferp
= pBuf
;
489 cdb
[2] = 0x40 | (pagenum
& 0x3f); /* Page control (PC)==1 */
491 cdb
[7] = (pageLen
>> 8) & 0xff;
492 cdb
[8] = pageLen
& 0xff;
494 io_hdr
.cmnd_len
= sizeof(cdb
);
495 io_hdr
.sensep
= sense
;
496 io_hdr
.max_sense_len
= sizeof(sense
);
497 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
499 if (!device
->scsi_pass_through(&io_hdr
))
500 return -device
->get_errno();
501 scsi_do_sense_disect(&io_hdr
, &sinfo
);
503 if ((res
= scsiSimpleSenseFilter(&sinfo
)))
505 /* sanity check on response */
506 if ((SUPPORTED_LPAGES
!= pagenum
) && ((pBuf
[0] & 0x3f) != pagenum
))
507 return SIMPLE_ERR_BAD_RESP
;
508 if (0 == ((pBuf
[2] << 8) + pBuf
[3]))
509 return SIMPLE_ERR_BAD_RESP
;
510 pageLen
= (pBuf
[2] << 8) + pBuf
[3] + 4;
511 if (4 == pageLen
) /* why define a lpage with no payload? */
512 pageLen
= 252; /* some IBM tape drives don't like double fetch */
513 /* some SCSI HBA don't like "odd" length transfers */
516 if (pageLen
> bufLen
)
520 memset(&io_hdr
, 0, sizeof(io_hdr
));
521 memset(cdb
, 0, sizeof(cdb
));
522 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
523 io_hdr
.dxfer_len
= pageLen
;
524 io_hdr
.dxferp
= pBuf
;
526 cdb
[2] = 0x40 | (pagenum
& 0x3f); /* Page control (PC)==1 */
527 cdb
[7] = (pageLen
>> 8) & 0xff;
528 cdb
[8] = pageLen
& 0xff;
530 io_hdr
.cmnd_len
= sizeof(cdb
);
531 io_hdr
.sensep
= sense
;
532 io_hdr
.max_sense_len
= sizeof(sense
);
533 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
535 if (!device
->scsi_pass_through(&io_hdr
))
536 return -device
->get_errno();
537 scsi_do_sense_disect(&io_hdr
, &sinfo
);
538 int status
= scsiSimpleSenseFilter(&sinfo
);
541 /* sanity check on response */
542 if ((SUPPORTED_LPAGES
!= pagenum
) && ((pBuf
[0] & 0x3f) != pagenum
))
543 return SIMPLE_ERR_BAD_RESP
;
544 if (0 == ((pBuf
[2] << 8) + pBuf
[3]))
545 return SIMPLE_ERR_BAD_RESP
;
549 /* Sends a LOG SELECT command. Can be used to set log page values
550 * or reset one log page (or all of them) to its defaults (typically zero).
551 * Returns 0 if ok, 1 if NOT READY, 2 if command not supported, * 3 if
552 * field in command not supported, * 4 if bad parameter to command or
553 * returns negated errno. SPC-4 sections 6.5 and 7.2 (rev 20) */
555 scsiLogSelect(scsi_device
* device
, int pcr
, int sp
, int pc
, int pagenum
,
556 int subpagenum
, UINT8
*pBuf
, int bufLen
)
558 struct scsi_cmnd_io io_hdr
;
559 struct scsi_sense_disect sinfo
;
563 memset(&io_hdr
, 0, sizeof(io_hdr
));
564 memset(cdb
, 0, sizeof(cdb
));
565 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
566 io_hdr
.dxfer_len
= bufLen
;
567 io_hdr
.dxferp
= pBuf
;
569 cdb
[1] = (pcr
? 2 : 0) | (sp
? 1 : 0);
570 cdb
[2] = ((pc
<< 6) & 0xc0) | (pagenum
& 0x3f);
571 cdb
[3] = (subpagenum
& 0xff);
572 cdb
[7] = ((bufLen
>> 8) & 0xff);
573 cdb
[8] = (bufLen
& 0xff);
575 io_hdr
.cmnd_len
= sizeof(cdb
);
576 io_hdr
.sensep
= sense
;
577 io_hdr
.max_sense_len
= sizeof(sense
);
578 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
580 if (!device
->scsi_pass_through(&io_hdr
))
581 return -device
->get_errno();
582 scsi_do_sense_disect(&io_hdr
, &sinfo
);
583 return scsiSimpleSenseFilter(&sinfo
);
586 /* Send MODE SENSE (6 byte) command. Returns 0 if ok, 1 if NOT READY,
587 * 2 if command not supported (then MODE SENSE(10) should be supported),
588 * 3 if field in command not supported or returns negated errno.
589 * SPC-3 sections 6.9 and 7.4 (rev 22a) [mode subpage==0] */
591 scsiModeSense(scsi_device
* device
, int pagenum
, int subpagenum
, int pc
,
592 UINT8
*pBuf
, int bufLen
)
594 struct scsi_cmnd_io io_hdr
;
595 struct scsi_sense_disect sinfo
;
599 if ((bufLen
< 0) || (bufLen
> 255))
601 memset(&io_hdr
, 0, sizeof(io_hdr
));
602 memset(cdb
, 0, sizeof(cdb
));
603 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
604 io_hdr
.dxfer_len
= bufLen
;
605 io_hdr
.dxferp
= pBuf
;
607 cdb
[2] = (pc
<< 6) | (pagenum
& 0x3f);
611 io_hdr
.cmnd_len
= sizeof(cdb
);
612 io_hdr
.sensep
= sense
;
613 io_hdr
.max_sense_len
= sizeof(sense
);
614 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
616 if (!device
->scsi_pass_through(&io_hdr
))
617 return -device
->get_errno();
618 scsi_do_sense_disect(&io_hdr
, &sinfo
);
619 int status
= scsiSimpleSenseFilter(&sinfo
);
620 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
621 if (!device
->scsi_pass_through(&io_hdr
))
622 return -device
->get_errno();
623 scsi_do_sense_disect(&io_hdr
, &sinfo
);
624 status
= scsiSimpleSenseFilter(&sinfo
);
626 if ((0 == status
) && (ALL_MODE_PAGES
!= pagenum
)) {
629 offset
= scsiModePageOffset(pBuf
, bufLen
, 0);
631 return SIMPLE_ERR_BAD_RESP
;
632 else if (pagenum
!= (pBuf
[offset
] & 0x3f))
633 return SIMPLE_ERR_BAD_RESP
;
638 /* Sends a 6 byte MODE SELECT command. Assumes given pBuf is the response
639 * from a corresponding 6 byte MODE SENSE command. Such a response should
640 * have a 4 byte header followed by 0 or more 8 byte block descriptors
641 * (normally 1) and then 1 mode page. Returns 0 if ok, 1 if NOT READY,
642 * 2 if command not supported (then MODE SELECT(10) may be supported),
643 * 3 if field in command not supported, 4 if bad parameter to command
644 * or returns negated errno. SPC-3 sections 6.7 and 7.4 (rev 22a) */
646 scsiModeSelect(scsi_device
* device
, int sp
, UINT8
*pBuf
, int bufLen
)
648 struct scsi_cmnd_io io_hdr
;
649 struct scsi_sense_disect sinfo
;
652 int pg_offset
, pg_len
, hdr_plus_1_pg
;
654 pg_offset
= 4 + pBuf
[3];
655 if (pg_offset
+ 2 >= bufLen
)
657 pg_len
= pBuf
[pg_offset
+ 1] + 2;
658 hdr_plus_1_pg
= pg_offset
+ pg_len
;
659 if (hdr_plus_1_pg
> bufLen
)
661 pBuf
[0] = 0; /* Length of returned mode sense data reserved for SELECT */
662 pBuf
[pg_offset
] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
663 memset(&io_hdr
, 0, sizeof(io_hdr
));
664 memset(cdb
, 0, sizeof(cdb
));
665 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
666 io_hdr
.dxfer_len
= hdr_plus_1_pg
;
667 io_hdr
.dxferp
= pBuf
;
668 cdb
[0] = MODE_SELECT
;
669 cdb
[1] = 0x10 | (sp
& 1); /* set PF (page format) bit always */
670 cdb
[4] = hdr_plus_1_pg
; /* make sure only one page sent */
672 io_hdr
.cmnd_len
= sizeof(cdb
);
673 io_hdr
.sensep
= sense
;
674 io_hdr
.max_sense_len
= sizeof(sense
);
675 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
677 if (!device
->scsi_pass_through(&io_hdr
))
678 return -device
->get_errno();
679 scsi_do_sense_disect(&io_hdr
, &sinfo
);
680 return scsiSimpleSenseFilter(&sinfo
);
683 /* MODE SENSE (10 byte). Returns 0 if ok, 1 if NOT READY, 2 if command
684 * not supported (then MODE SENSE(6) might be supported), 3 if field in
685 * command not supported or returns negated errno.
686 * SPC-3 sections 6.10 and 7.4 (rev 22a) [mode subpage==0] */
688 scsiModeSense10(scsi_device
* device
, int pagenum
, int subpagenum
, int pc
,
689 UINT8
*pBuf
, int bufLen
)
691 struct scsi_cmnd_io io_hdr
;
692 struct scsi_sense_disect sinfo
;
696 memset(&io_hdr
, 0, sizeof(io_hdr
));
697 memset(cdb
, 0, sizeof(cdb
));
698 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
699 io_hdr
.dxfer_len
= bufLen
;
700 io_hdr
.dxferp
= pBuf
;
701 cdb
[0] = MODE_SENSE_10
;
702 cdb
[2] = (pc
<< 6) | (pagenum
& 0x3f);
704 cdb
[7] = (bufLen
>> 8) & 0xff;
705 cdb
[8] = bufLen
& 0xff;
707 io_hdr
.cmnd_len
= sizeof(cdb
);
708 io_hdr
.sensep
= sense
;
709 io_hdr
.max_sense_len
= sizeof(sense
);
710 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
712 if (!device
->scsi_pass_through(&io_hdr
))
713 return -device
->get_errno();
714 scsi_do_sense_disect(&io_hdr
, &sinfo
);
715 int status
= scsiSimpleSenseFilter(&sinfo
);
716 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
717 if (!device
->scsi_pass_through(&io_hdr
))
718 return -device
->get_errno();
719 scsi_do_sense_disect(&io_hdr
, &sinfo
);
720 status
= scsiSimpleSenseFilter(&sinfo
);
722 if ((0 == status
) && (ALL_MODE_PAGES
!= pagenum
)) {
725 offset
= scsiModePageOffset(pBuf
, bufLen
, 1);
727 return SIMPLE_ERR_BAD_RESP
;
728 else if (pagenum
!= (pBuf
[offset
] & 0x3f))
729 return SIMPLE_ERR_BAD_RESP
;
734 /* Sends a 10 byte MODE SELECT command. Assumes given pBuf is the response
735 * from a corresponding 10 byte MODE SENSE command. Such a response should
736 * have a 8 byte header followed by 0 or more 8 byte block descriptors
737 * (normally 1) and then 1 mode page. Returns 0 if ok, 1 NOT REAFY, 2 if
738 * command not supported (then MODE SELECT(6) may be supported), 3 if field
739 * in command not supported, 4 if bad parameter to command or returns
740 * negated errno. SPC-3 sections 6.8 and 7.4 (rev 22a) */
742 scsiModeSelect10(scsi_device
* device
, int sp
, UINT8
*pBuf
, int bufLen
)
744 struct scsi_cmnd_io io_hdr
;
745 struct scsi_sense_disect sinfo
;
748 int pg_offset
, pg_len
, hdr_plus_1_pg
;
750 pg_offset
= 8 + (pBuf
[6] << 8) + pBuf
[7];
751 if (pg_offset
+ 2 >= bufLen
)
753 pg_len
= pBuf
[pg_offset
+ 1] + 2;
754 hdr_plus_1_pg
= pg_offset
+ pg_len
;
755 if (hdr_plus_1_pg
> bufLen
)
758 pBuf
[1] = 0; /* Length of returned mode sense data reserved for SELECT */
759 pBuf
[pg_offset
] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
760 memset(&io_hdr
, 0, sizeof(io_hdr
));
761 memset(cdb
, 0, sizeof(cdb
));
762 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
763 io_hdr
.dxfer_len
= hdr_plus_1_pg
;
764 io_hdr
.dxferp
= pBuf
;
765 cdb
[0] = MODE_SELECT_10
;
766 cdb
[1] = 0x10 | (sp
& 1); /* set PF (page format) bit always */
767 cdb
[8] = hdr_plus_1_pg
; /* make sure only one page sent */
769 io_hdr
.cmnd_len
= sizeof(cdb
);
770 io_hdr
.sensep
= sense
;
771 io_hdr
.max_sense_len
= sizeof(sense
);
772 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
774 if (!device
->scsi_pass_through(&io_hdr
))
775 return -device
->get_errno();
776 scsi_do_sense_disect(&io_hdr
, &sinfo
);
777 return scsiSimpleSenseFilter(&sinfo
);
780 /* Standard INQUIRY returns 0 for ok, anything else is a major problem.
781 * bufLen should be 36 for unsafe devices (like USB mass storage stuff)
782 * otherwise they can lock up! SPC-3 sections 6.4 and 7.6 (rev 22a) */
784 scsiStdInquiry(scsi_device
* device
, UINT8
*pBuf
, int bufLen
)
786 struct scsi_sense_disect sinfo
;
787 struct scsi_cmnd_io io_hdr
;
791 if ((bufLen
< 0) || (bufLen
> 1023))
793 memset(&io_hdr
, 0, sizeof(io_hdr
));
794 memset(cdb
, 0, sizeof(cdb
));
795 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
796 io_hdr
.dxfer_len
= bufLen
;
797 io_hdr
.dxferp
= pBuf
;
799 cdb
[3] = (bufLen
>> 8) & 0xff;
800 cdb
[4] = (bufLen
& 0xff);
802 io_hdr
.cmnd_len
= sizeof(cdb
);
803 io_hdr
.sensep
= sense
;
804 io_hdr
.max_sense_len
= sizeof(sense
);
805 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
807 if (!device
->scsi_pass_through(&io_hdr
))
808 return -device
->get_errno();
809 scsi_do_sense_disect(&io_hdr
, &sinfo
);
810 return scsiSimpleSenseFilter(&sinfo
);
813 /* INQUIRY to fetch Vital Page Data. Returns 0 if ok, 1 if NOT READY
814 * (unlikely), 2 if command not supported, 3 if field in command not
815 * supported, 5 if response indicates that EVPD bit ignored or returns
816 * negated errno. SPC-3 section 6.4 and 7.6 (rev 22a) */
818 scsiInquiryVpd(scsi_device
* device
, int vpd_page
, UINT8
*pBuf
, int bufLen
)
820 struct scsi_cmnd_io io_hdr
;
821 struct scsi_sense_disect sinfo
;
826 /* Assume SCSI_VPD_SUPPORTED_VPD_PAGES is first VPD page fetched */
827 if ((SCSI_VPD_SUPPORTED_VPD_PAGES
!= vpd_page
) &&
828 supported_vpd_pages_p
&&
829 (! supported_vpd_pages_p
->is_supported(vpd_page
)))
832 if ((bufLen
< 0) || (bufLen
> 1023))
835 memset(&io_hdr
, 0, sizeof(io_hdr
));
836 memset(cdb
, 0, sizeof(cdb
));
839 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
840 io_hdr
.dxfer_len
= bufLen
;
841 io_hdr
.dxferp
= pBuf
;
843 cdb
[1] = 0x1; /* set EVPD bit (enable Vital Product Data) */
845 cdb
[3] = (bufLen
>> 8) & 0xff;
846 cdb
[4] = (bufLen
& 0xff);
848 io_hdr
.cmnd_len
= sizeof(cdb
);
849 io_hdr
.sensep
= sense
;
850 io_hdr
.max_sense_len
= sizeof(sense
);
851 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
853 if (!device
->scsi_pass_through(&io_hdr
))
854 return -device
->get_errno();
855 scsi_do_sense_disect(&io_hdr
, &sinfo
);
856 if ((SCSI_STATUS_CHECK_CONDITION
== io_hdr
.scsi_status
) &&
857 (SCSI_SK_ILLEGAL_REQUEST
== sinfo
.sense_key
) &&
858 (SCSI_ASC_INVALID_FIELD
== sinfo
.asc
) &&
860 bufLen
&= 0xff; /* make sure cdb[3] is 0 next time around */
864 if ((res
= scsiSimpleSenseFilter(&sinfo
)))
866 /* Guard against devices that ignore EVPD bit and do standard INQUIRY */
868 if (vpd_page
== pBuf
[1]) {
869 if ((0x80 == vpd_page
) && (bufLen
> 2) && (0x0 != pBuf
[2]))
870 return SIMPLE_ERR_BAD_RESP
;
872 return SIMPLE_ERR_BAD_RESP
;
877 /* REQUEST SENSE command. Returns 0 if ok, anything else major problem.
878 * SPC-3 section 6.27 (rev 22a) */
880 scsiRequestSense(scsi_device
* device
, struct scsi_sense_disect
* sense_info
)
882 struct scsi_cmnd_io io_hdr
;
887 memset(&io_hdr
, 0, sizeof(io_hdr
));
888 memset(cdb
, 0, sizeof(cdb
));
889 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
890 io_hdr
.dxfer_len
= sizeof(buff
);
891 io_hdr
.dxferp
= buff
;
892 cdb
[0] = REQUEST_SENSE
;
893 cdb
[4] = sizeof(buff
);
895 io_hdr
.cmnd_len
= sizeof(cdb
);
896 io_hdr
.sensep
= sense
;
897 io_hdr
.max_sense_len
= sizeof(sense
);
898 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
900 if (!device
->scsi_pass_through(&io_hdr
))
901 return -device
->get_errno();
903 UINT8 resp_code
= buff
[0] & 0x7f;
904 sense_info
->resp_code
= resp_code
;
905 sense_info
->sense_key
= buff
[2] & 0xf;
907 sense_info
->ascq
= 0;
908 if ((0x70 == resp_code
) || (0x71 == resp_code
)) {
909 int len
= buff
[7] + 8;
911 sense_info
->asc
= buff
[12];
912 sense_info
->ascq
= buff
[13];
915 // fill progrss indicator, if available
916 sense_info
->progress
= -1;
918 const unsigned char * ucp
;
922 sk
= (buff
[2] & 0xf);
923 if ((sizeof(buff
) < 18) ||
924 ((SCSI_SK_NO_SENSE
!= sk
) && (SCSI_SK_NOT_READY
!= sk
))) {
927 if (buff
[15] & 0x80) { /* SKSV bit set */
928 sense_info
->progress
= (buff
[16] << 8) + buff
[17];
935 /* sense key specific progress (0x2) or progress descriptor (0xa) */
936 sk
= (buff
[1] & 0xf);
937 sk_pr
= (SCSI_SK_NO_SENSE
== sk
) || (SCSI_SK_NOT_READY
== sk
);
938 if (sk_pr
&& ((ucp
= sg_scsi_sense_desc_find(buff
, sizeof(buff
), 2))) &&
939 (0x6 == ucp
[1]) && (0x80 & ucp
[4])) {
940 sense_info
->progress
= (ucp
[5] << 8) + ucp
[6];
942 } else if (((ucp
= sg_scsi_sense_desc_find(buff
, sizeof(buff
), 0xa))) &&
944 sense_info
->progress
= (ucp
[6] << 8) + ucp
[7];
955 /* SEND DIAGNOSTIC command. Returns 0 if ok, 1 if NOT READY, 2 if command
956 * not supported, 3 if field in command not supported or returns negated
957 * errno. SPC-3 section 6.28 (rev 22a) */
959 scsiSendDiagnostic(scsi_device
* device
, int functioncode
, UINT8
*pBuf
,
962 struct scsi_cmnd_io io_hdr
;
963 struct scsi_sense_disect sinfo
;
967 memset(&io_hdr
, 0, sizeof(io_hdr
));
968 memset(cdb
, 0, sizeof(cdb
));
969 io_hdr
.dxfer_dir
= bufLen
? DXFER_TO_DEVICE
: DXFER_NONE
;
970 io_hdr
.dxfer_len
= bufLen
;
971 io_hdr
.dxferp
= pBuf
;
972 cdb
[0] = SEND_DIAGNOSTIC
;
973 if (SCSI_DIAG_DEF_SELF_TEST
== functioncode
)
974 cdb
[1] = 0x4; /* SelfTest bit */
975 else if (SCSI_DIAG_NO_SELF_TEST
!= functioncode
)
976 cdb
[1] = (functioncode
& 0x7) << 5; /* SelfTest _code_ */
977 else /* SCSI_DIAG_NO_SELF_TEST == functioncode */
978 cdb
[1] = 0x10; /* PF bit */
979 cdb
[3] = (bufLen
>> 8) & 0xff;
980 cdb
[4] = bufLen
& 0xff;
982 io_hdr
.cmnd_len
= sizeof(cdb
);
983 io_hdr
.sensep
= sense
;
984 io_hdr
.max_sense_len
= sizeof(sense
);
985 /* worst case is an extended foreground self test on a big disk */
986 io_hdr
.timeout
= SCSI_TIMEOUT_SELF_TEST
;
988 if (!device
->scsi_pass_through(&io_hdr
))
989 return -device
->get_errno();
990 scsi_do_sense_disect(&io_hdr
, &sinfo
);
991 return scsiSimpleSenseFilter(&sinfo
);
994 /* TEST UNIT READY command. SPC-3 section 6.33 (rev 22a) */
996 _testunitready(scsi_device
* device
, struct scsi_sense_disect
* sinfo
)
998 struct scsi_cmnd_io io_hdr
;
1002 memset(&io_hdr
, 0, sizeof(io_hdr
));
1003 memset(cdb
, 0, sizeof(cdb
));
1004 io_hdr
.dxfer_dir
= DXFER_NONE
;
1005 io_hdr
.dxfer_len
= 0;
1006 io_hdr
.dxferp
= NULL
;
1007 cdb
[0] = TEST_UNIT_READY
;
1009 io_hdr
.cmnd_len
= sizeof(cdb
);
1010 io_hdr
.sensep
= sense
;
1011 io_hdr
.max_sense_len
= sizeof(sense
);
1012 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1014 if (!device
->scsi_pass_through(&io_hdr
))
1015 return -device
->get_errno();
1016 scsi_do_sense_disect(&io_hdr
, sinfo
);
1020 /* Returns 0 for device responds and media ready, 1 for device responds and
1021 media not ready, or returns a negated errno value */
1023 scsiTestUnitReady(scsi_device
* device
)
1025 struct scsi_sense_disect sinfo
;
1028 status
= _testunitready(device
, &sinfo
);
1031 status
= scsiSimpleSenseFilter(&sinfo
);
1032 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
1033 /* power on reset, media changed, ok ... try again */
1034 status
= _testunitready(device
, &sinfo
);
1037 status
= scsiSimpleSenseFilter(&sinfo
);
1042 /* READ DEFECT (10) command. Returns 0 if ok, 1 if NOT READY, 2 if
1043 * command not supported, 3 if field in command not supported, 101 if
1044 * defect list not found (e.g. SSD may not have defect list) or returns
1045 * negated errno. SBC-2 section 5.12 (rev 16) */
1047 scsiReadDefect10(scsi_device
* device
, int req_plist
, int req_glist
,
1048 int dl_format
, UINT8
*pBuf
, int bufLen
)
1050 struct scsi_cmnd_io io_hdr
;
1051 struct scsi_sense_disect sinfo
;
1055 memset(&io_hdr
, 0, sizeof(io_hdr
));
1056 memset(cdb
, 0, sizeof(cdb
));
1057 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1058 io_hdr
.dxfer_len
= bufLen
;
1059 io_hdr
.dxferp
= pBuf
;
1060 cdb
[0] = READ_DEFECT_10
;
1061 cdb
[2] = (unsigned char)(((req_plist
<< 4) & 0x10) |
1062 ((req_glist
<< 3) & 0x8) | (dl_format
& 0x7));
1063 cdb
[7] = (bufLen
>> 8) & 0xff;
1064 cdb
[8] = bufLen
& 0xff;
1066 io_hdr
.cmnd_len
= sizeof(cdb
);
1067 io_hdr
.sensep
= sense
;
1068 io_hdr
.max_sense_len
= sizeof(sense
);
1069 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1071 if (!device
->scsi_pass_through(&io_hdr
))
1072 return -device
->get_errno();
1073 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1074 /* Look for "(Primary|Grown) defect list not found" */
1075 if ((sinfo
.resp_code
>= 0x70) && (0x1c == sinfo
.asc
))
1077 return scsiSimpleSenseFilter(&sinfo
);
1080 /* READ DEFECT (12) command. Returns 0 if ok, 1 if NOT READY, 2 if
1081 * command not supported, 3 if field in command not supported, 101 if
1082 * defect list not found (e.g. SSD may not have defect list) or returns
1083 * negated errno. SBC-3 section 5.18 (rev 35; vale Mark Evans) */
1085 scsiReadDefect12(scsi_device
* device
, int req_plist
, int req_glist
,
1086 int dl_format
, int addrDescIndex
, UINT8
*pBuf
, int bufLen
)
1088 struct scsi_cmnd_io io_hdr
;
1089 struct scsi_sense_disect sinfo
;
1093 memset(&io_hdr
, 0, sizeof(io_hdr
));
1094 memset(cdb
, 0, sizeof(cdb
));
1095 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1096 io_hdr
.dxfer_len
= bufLen
;
1097 io_hdr
.dxferp
= pBuf
;
1098 cdb
[0] = READ_DEFECT_12
;
1099 cdb
[1] = (unsigned char)(((req_plist
<< 4) & 0x10) |
1100 ((req_glist
<< 3) & 0x8) | (dl_format
& 0x7));
1101 cdb
[2] = (addrDescIndex
>> 24) & 0xff;
1102 cdb
[3] = (addrDescIndex
>> 16) & 0xff;
1103 cdb
[4] = (addrDescIndex
>> 8) & 0xff;
1104 cdb
[5] = addrDescIndex
& 0xff;
1105 cdb
[6] = (bufLen
>> 24) & 0xff;
1106 cdb
[7] = (bufLen
>> 16) & 0xff;
1107 cdb
[8] = (bufLen
>> 8) & 0xff;
1108 cdb
[9] = bufLen
& 0xff;
1110 io_hdr
.cmnd_len
= sizeof(cdb
);
1111 io_hdr
.sensep
= sense
;
1112 io_hdr
.max_sense_len
= sizeof(sense
);
1113 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1115 if (!device
->scsi_pass_through(&io_hdr
))
1116 return -device
->get_errno();
1117 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1118 /* Look for "(Primary|Grown) defect list not found" */
1119 if ((sinfo
.resp_code
>= 0x70) && (0x1c == sinfo
.asc
))
1121 return scsiSimpleSenseFilter(&sinfo
);
1124 /* READ CAPACITY (10) command. Returns 0 if ok, 1 if NOT READY, 2 if
1125 * command not supported, 3 if field in command not supported or returns
1126 * negated errno. SBC-3 section 5.15 (rev 26) */
1128 scsiReadCapacity10(scsi_device
* device
, unsigned int * last_lbap
,
1129 unsigned int * lb_sizep
)
1132 struct scsi_cmnd_io io_hdr
;
1133 struct scsi_sense_disect sinfo
;
1138 memset(&io_hdr
, 0, sizeof(io_hdr
));
1139 memset(cdb
, 0, sizeof(cdb
));
1140 memset(resp
, 0, sizeof(resp
));
1141 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1142 io_hdr
.dxfer_len
= sizeof(resp
);
1143 io_hdr
.dxferp
= resp
;
1144 cdb
[0] = READ_CAPACITY_10
;
1146 io_hdr
.cmnd_len
= sizeof(cdb
);
1147 io_hdr
.sensep
= sense
;
1148 io_hdr
.max_sense_len
= sizeof(sense
);
1149 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1151 if (!device
->scsi_pass_through(&io_hdr
))
1152 return -device
->get_errno();
1153 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1154 res
= scsiSimpleSenseFilter(&sinfo
);
1158 *last_lbap
= (resp
[0] << 24) | (resp
[1] << 16) | (resp
[2] << 8) |
1161 *lb_sizep
= (resp
[4] << 24) | (resp
[5] << 16) | (resp
[6] << 8) |
1166 /* READ CAPACITY (16) command. The bufLen argument should be 32. Returns 0
1167 * if ok, 1 if NOT READY, 2 if command not supported, 3 if field in command
1168 * not supported or returns negated errno. SBC-3 section 5.16 (rev 26) */
1170 scsiReadCapacity16(scsi_device
* device
, UINT8
*pBuf
, int bufLen
)
1172 struct scsi_cmnd_io io_hdr
;
1173 struct scsi_sense_disect sinfo
;
1177 memset(&io_hdr
, 0, sizeof(io_hdr
));
1178 memset(cdb
, 0, sizeof(cdb
));
1179 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1180 io_hdr
.dxfer_len
= bufLen
;
1181 io_hdr
.dxferp
= pBuf
;
1182 cdb
[0] = READ_CAPACITY_16
;
1183 cdb
[1] = SAI_READ_CAPACITY_16
;
1184 cdb
[10] = (bufLen
>> 24) & 0xff;
1185 cdb
[11] = (bufLen
>> 16) & 0xff;
1186 cdb
[12] = (bufLen
>> 8) & 0xff;
1187 cdb
[13] = bufLen
& 0xff;
1189 io_hdr
.cmnd_len
= sizeof(cdb
);
1190 io_hdr
.sensep
= sense
;
1191 io_hdr
.max_sense_len
= sizeof(sense
);
1192 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1194 if (!device
->scsi_pass_through(&io_hdr
))
1195 return -device
->get_errno();
1196 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1197 return scsiSimpleSenseFilter(&sinfo
);
1200 /* Return number of bytes of storage in 'device' or 0 if error. If
1201 * successful and lb_sizep is not NULL then the logical block size
1202 * in bytes is written to the location pointed to by lb_sizep. */
1204 scsiGetSize(scsi_device
* device
, unsigned int * lb_sizep
,
1205 int * lb_per_pb_expp
)
1207 unsigned int last_lba
= 0, lb_size
= 0;
1209 uint64_t ret_val
= 0;
1212 res
= scsiReadCapacity10(device
, &last_lba
, &lb_size
);
1215 pout("scsiGetSize: READ CAPACITY(10) failed, res=%d\n", res
);
1218 if (0xffffffff == last_lba
) {
1219 res
= scsiReadCapacity16(device
, rc16resp
, sizeof(rc16resp
));
1222 pout("scsiGetSize: READ CAPACITY(16) failed, res=%d\n", res
);
1225 for (int k
= 0; k
< 8; ++k
) {
1228 ret_val
|= rc16resp
[k
+ 0];
1231 *lb_per_pb_expp
= (rc16resp
[13] & 0xf);
1235 *lb_per_pb_expp
= 0;
1238 *lb_sizep
= lb_size
;
1239 ++ret_val
; /* last_lba is origin 0 so need to bump to get number of */
1240 return ret_val
* lb_size
;
1243 /* Gets drive Protection and Logical/Physical block information. Writes
1244 * back bytes 12 to 31 from a READ CAPACITY 16 command to the rc16_12_31p
1245 * pointer. So rc16_12_31p should point to an array of 20 bytes. Returns 0
1246 * if ok, 1 if NOT READY, 2 if command not supported, 3 if field in command
1247 * not supported or returns negated errno. */
1249 scsiGetProtPBInfo(scsi_device
* device
, unsigned char * rc16_12_31p
)
1254 res
= scsiReadCapacity16(device
, rc16resp
, sizeof(rc16resp
));
1257 pout("scsiGetSize: READ CAPACITY(16) failed, res=%d\n", res
);
1261 memcpy(rc16_12_31p
, rc16resp
+ 12, 20);
1265 /* Offset into mode sense (6 or 10 byte) response that actual mode page
1266 * starts at (relative to resp[0]). Returns -1 if problem */
1268 scsiModePageOffset(const UINT8
* resp
, int len
, int modese_len
)
1273 int resp_len
, bd_len
;
1274 if (10 == modese_len
) {
1275 resp_len
= (resp
[0] << 8) + resp
[1] + 2;
1276 bd_len
= (resp
[6] << 8) + resp
[7];
1277 offset
= bd_len
+ 8;
1279 resp_len
= resp
[0] + 1;
1281 offset
= bd_len
+ 4;
1283 if ((offset
+ 2) > len
) {
1284 pout("scsiModePageOffset: raw_curr too small, offset=%d "
1285 "resp_len=%d bd_len=%d\n", offset
, resp_len
, bd_len
);
1287 } else if ((offset
+ 2) > resp_len
) {
1288 if ((resp_len
> 2) || scsi_debugmode
)
1289 pout("scsiModePageOffset: response length too short, "
1290 "resp_len=%d offset=%d bd_len=%d\n", resp_len
,
1298 /* IEC mode page byte 2 bit masks */
1299 #define DEXCPT_ENABLE 0x08
1300 #define EWASC_ENABLE 0x10
1301 #define DEXCPT_DISABLE 0xf7
1302 #define EWASC_DISABLE 0xef
1303 #define TEST_DISABLE 0xfb
1305 /* Fetches the Informational Exceptions Control mode page. First tries
1306 * the 6 byte MODE SENSE command and if that fails with an illegal opcode
1307 * tries a 10 byte MODE SENSE command. Returns 0 if successful, a positive
1308 * number if a known error (see SIMPLE_ERR_ ...) or a negative errno
1311 scsiFetchIECmpage(scsi_device
* device
, struct scsi_iec_mode_page
*iecp
,
1316 memset(iecp
, 0, sizeof(*iecp
));
1317 iecp
->modese_len
= modese_len
;
1318 iecp
->requestedCurrent
= 1;
1319 if (iecp
->modese_len
<= 6) {
1320 if ((err
= scsiModeSense(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1321 0, MPAGE_CONTROL_CURRENT
,
1322 iecp
->raw_curr
, sizeof(iecp
->raw_curr
)))) {
1323 if (SIMPLE_ERR_BAD_OPCODE
== err
)
1324 iecp
->modese_len
= 10;
1326 iecp
->modese_len
= 0;
1329 } else if (0 == iecp
->modese_len
)
1330 iecp
->modese_len
= 6;
1332 if (10 == iecp
->modese_len
) {
1333 err
= scsiModeSense10(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1334 0, MPAGE_CONTROL_CURRENT
,
1335 iecp
->raw_curr
, sizeof(iecp
->raw_curr
));
1337 iecp
->modese_len
= 0;
1341 iecp
->gotCurrent
= 1;
1342 iecp
->requestedChangeable
= 1;
1343 if (10 == iecp
->modese_len
)
1344 err
= scsiModeSense10(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1345 0, MPAGE_CONTROL_CHANGEABLE
,
1346 iecp
->raw_chg
, sizeof(iecp
->raw_chg
));
1347 else if (6 == iecp
->modese_len
)
1348 err
= scsiModeSense(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1349 0, MPAGE_CONTROL_CHANGEABLE
,
1350 iecp
->raw_chg
, sizeof(iecp
->raw_chg
));
1353 iecp
->gotChangeable
= 1;
1358 scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page
*iecp
)
1360 if (iecp
&& iecp
->gotCurrent
) {
1361 int offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
1364 return (iecp
->raw_curr
[offset
+ 2] & DEXCPT_ENABLE
) ? 0 : 1;
1372 scsi_IsWarningEnabled(const struct scsi_iec_mode_page
*iecp
)
1374 if (iecp
&& iecp
->gotCurrent
) {
1375 int offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
1378 return (iecp
->raw_curr
[offset
+ 2] & EWASC_ENABLE
) ? 1 : 0;
1385 /* set EWASC and clear PERF, EBF, DEXCPT TEST and LOGERR */
1386 #define SCSI_IEC_MP_BYTE2_ENABLED 0x10
1387 #define SCSI_IEC_MP_BYTE2_TEST_MASK 0x4
1388 /* exception/warning via an unrequested REQUEST SENSE command */
1389 #define SCSI_IEC_MP_MRIE 6
1390 #define SCSI_IEC_MP_INTERVAL_T 0
1391 #define SCSI_IEC_MP_REPORT_COUNT 1
1393 /* Try to set (or clear) both Exception Control and Warning in the IE
1394 * mode page subject to the "changeable" mask. The object pointed to
1395 * by iecp is (possibly) inaccurate after this call, therefore
1396 * scsiFetchIECmpage() should be called again if the IEC mode page
1397 * is to be re-examined.
1398 * When -r ioctl is invoked 3 or more time on 'smartctl -s on ...'
1399 * then set the TEST bit (causes asc,ascq pair of 0x5d,0xff). */
1401 scsiSetExceptionControlAndWarning(scsi_device
* device
, int enabled
,
1402 const struct scsi_iec_mode_page
*iecp
)
1404 int offset
, resp_len
;
1406 UINT8 rout
[SCSI_IECMP_RAW_LEN
];
1408 if ((! iecp
) || (! iecp
->gotCurrent
))
1410 offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
1414 memcpy(rout
, iecp
->raw_curr
, SCSI_IECMP_RAW_LEN
);
1415 /* mask out DPOFUA device specific (disk) parameter bit */
1416 if (10 == iecp
->modese_len
) {
1417 resp_len
= (rout
[0] << 8) + rout
[1] + 2;
1420 resp_len
= rout
[0] + 1;
1423 int sp
= (rout
[offset
] & 0x80) ? 1 : 0; /* PS bit becomes 'SELECT's SP bit */
1425 rout
[offset
+ 2] = SCSI_IEC_MP_BYTE2_ENABLED
;
1426 if (scsi_debugmode
> 2)
1427 rout
[offset
+ 2] |= SCSI_IEC_MP_BYTE2_TEST_MASK
;
1428 rout
[offset
+ 3] = SCSI_IEC_MP_MRIE
;
1429 rout
[offset
+ 4] = (SCSI_IEC_MP_INTERVAL_T
>> 24) & 0xff;
1430 rout
[offset
+ 5] = (SCSI_IEC_MP_INTERVAL_T
>> 16) & 0xff;
1431 rout
[offset
+ 6] = (SCSI_IEC_MP_INTERVAL_T
>> 8) & 0xff;
1432 rout
[offset
+ 7] = SCSI_IEC_MP_INTERVAL_T
& 0xff;
1433 rout
[offset
+ 8] = (SCSI_IEC_MP_REPORT_COUNT
>> 24) & 0xff;
1434 rout
[offset
+ 9] = (SCSI_IEC_MP_REPORT_COUNT
>> 16) & 0xff;
1435 rout
[offset
+ 10] = (SCSI_IEC_MP_REPORT_COUNT
>> 8) & 0xff;
1436 rout
[offset
+ 11] = SCSI_IEC_MP_REPORT_COUNT
& 0xff;
1437 if (iecp
->gotChangeable
) {
1438 UINT8 chg2
= iecp
->raw_chg
[offset
+ 2];
1440 rout
[offset
+ 2] = chg2
? (rout
[offset
+ 2] & chg2
) :
1441 iecp
->raw_curr
[offset
+ 2];
1442 for (int k
= 3; k
< 12; ++k
) {
1443 if (0 == iecp
->raw_chg
[offset
+ k
])
1444 rout
[offset
+ k
] = iecp
->raw_curr
[offset
+ k
];
1447 if (0 == memcmp(&rout
[offset
+ 2], &iecp
->raw_chg
[offset
+ 2], 10)) {
1448 if (scsi_debugmode
> 0)
1449 pout("scsiSetExceptionControlAndWarning: already enabled\n");
1452 } else { /* disabling Exception Control and (temperature) Warnings */
1453 int eCEnabled
= (rout
[offset
+ 2] & DEXCPT_ENABLE
) ? 0 : 1;
1454 int wEnabled
= (rout
[offset
+ 2] & EWASC_ENABLE
) ? 1 : 0;
1455 if ((! eCEnabled
) && (! wEnabled
)) {
1456 if (scsi_debugmode
> 0)
1457 pout("scsiSetExceptionControlAndWarning: already disabled\n");
1458 return 0; /* nothing to do, leave other setting alone */
1461 rout
[offset
+ 2] &= EWASC_DISABLE
;
1463 if (iecp
->gotChangeable
&&
1464 (iecp
->raw_chg
[offset
+ 2] & DEXCPT_ENABLE
))
1465 rout
[offset
+ 2] |= DEXCPT_ENABLE
;
1466 rout
[offset
+ 2] &= TEST_DISABLE
;/* clear TEST bit for spec */
1469 if (10 == iecp
->modese_len
)
1470 err
= scsiModeSelect10(device
, sp
, rout
, resp_len
);
1471 else if (6 == iecp
->modese_len
)
1472 err
= scsiModeSelect(device
, sp
, rout
, resp_len
);
1477 scsiGetTemp(scsi_device
* device
, UINT8
*currenttemp
, UINT8
*triptemp
)
1482 memset(tBuf
, 0, sizeof(tBuf
));
1483 if ((err
= scsiLogSense(device
, TEMPERATURE_LPAGE
, 0, tBuf
,
1484 sizeof(tBuf
), 0))) {
1487 pout("Log Sense for temperature failed [%s]\n", scsiErrString(err
));
1490 *currenttemp
= tBuf
[9];
1491 *triptemp
= tBuf
[15];
1495 /* Read informational exception log page or Request Sense response.
1496 * Fetching asc/ascq code potentially flagging an exception or warning.
1497 * Returns 0 if ok, else error number. A current temperature of 255
1498 * (Celsius) implies that the temperature not available. */
1500 scsiCheckIE(scsi_device
* device
, int hasIELogPage
, int hasTempLogPage
,
1501 UINT8
*asc
, UINT8
*ascq
, UINT8
*currenttemp
, UINT8
*triptemp
)
1504 struct scsi_sense_disect sense_info
;
1506 int temperatureSet
= 0;
1507 UINT8 currTemp
, trTemp
;
1513 memset(tBuf
,0,sizeof(tBuf
)); // need to clear stack space of junk
1514 memset(&sense_info
, 0, sizeof(sense_info
));
1516 if ((err
= scsiLogSense(device
, IE_LPAGE
, 0, tBuf
,
1517 sizeof(tBuf
), 0))) {
1518 pout("Log Sense failed, IE page [%s]\n", scsiErrString(err
));
1521 // pull out page size from response, don't forget to add 4
1522 unsigned short pagesize
= (unsigned short) ((tBuf
[2] << 8) | tBuf
[3]) + 4;
1523 if ((pagesize
< 4) || tBuf
[4] || tBuf
[5]) {
1524 pout("Log Sense failed, IE page, bad parameter code or length\n");
1525 return SIMPLE_ERR_BAD_PARAM
;
1528 sense_info
.asc
= tBuf
[8];
1529 sense_info
.ascq
= tBuf
[9];
1530 if (! hasTempLogPage
) {
1532 *currenttemp
= tBuf
[10];
1533 if (tBuf
[7] > 3) /* IBM extension in SMART (IE) lpage */
1534 *triptemp
= tBuf
[11];
1538 if (0 == sense_info
.asc
) {
1539 /* ties in with MRIE field of 6 in IEC mode page (0x1c) */
1540 if ((err
= scsiRequestSense(device
, &sense_info
))) {
1541 pout("Request Sense failed, [%s]\n", scsiErrString(err
));
1545 *asc
= sense_info
.asc
;
1546 *ascq
= sense_info
.ascq
;
1547 if ((! temperatureSet
) && hasTempLogPage
) {
1548 if (0 == scsiGetTemp(device
, &currTemp
, &trTemp
)) {
1549 *currenttemp
= currTemp
;
1556 // The first character (W, C, I) tells the severity
1557 static const char * TapeAlertsMessageTable
[]= {
1560 "W: The tape drive is having problems reading data. No data has been lost,\n"
1561 " but there has been a reduction in the performance of the tape.",
1563 "W: The tape drive is having problems writing data. No data has been lost,\n"
1564 " but there has been a reduction in the capacity of the tape.",
1566 "W: The operation has stopped because an error has occurred while reading\n"
1567 " or writing data that the drive cannot correct.",
1569 "C: Your data is at risk:\n"
1570 " 1. Copy any data you require from this tape. \n"
1571 " 2. Do not use this tape again.\n"
1572 " 3. Restart the operation with a different tape.",
1574 "C: The tape is damaged or the drive is faulty. Call the tape drive\n"
1575 " supplier helpline.",
1577 "C: The tape is from a faulty batch or the tape drive is faulty:\n"
1578 " 1. Use a good tape to test the drive.\n"
1579 " 2. If problem persists, call the tape drive supplier helpline.",
1581 "W: The tape cartridge has reached the end of its calculated useful life:\n"
1582 " 1. Copy data you need to another tape.\n"
1583 " 2. Discard the old tape.",
1585 "W: The tape cartridge is not data-grade. Any data you back up to the tape\n"
1586 " is at risk. Replace the cartridge with a data-grade tape.",
1588 "C: You are trying to write to a write-protected cartridge. Remove the\n"
1589 " write-protection or use another tape.",
1591 "I: You cannot eject the cartridge because the tape drive is in use. Wait\n"
1592 " until the operation is complete before ejecting the cartridge.",
1594 "I: The tape in the drive is a cleaning cartridge.",
1596 "I: You have tried to load a cartridge of a type which is not supported\n"
1599 "C: The operation has failed because the tape in the drive has experienced\n"
1600 " a mechanical failure:\n"
1601 " 1. Discard the old tape.\n"
1602 " 2. Restart the operation with a different tape.",
1604 "C: The operation has failed because the tape in the drive has experienced\n"
1605 " a mechanical failure:\n"
1606 " 1. Do not attempt to extract the tape cartridge\n"
1607 " 2. Call the tape drive supplier helpline.",
1609 "W: The memory in the tape cartridge has failed, which reduces\n"
1610 " performance. Do not use the cartridge for further write operations.",
1612 "C: The operation has failed because the tape cartridge was manually\n"
1613 " de-mounted while the tape drive was actively writing or reading.",
1615 "W: You have loaded a cartridge of a type that is read-only in this drive.\n"
1616 " The cartridge will appear as write-protected.",
1618 "W: The tape directory on the tape cartridge has been corrupted. File\n"
1619 " search performance will be degraded. The tape directory can be rebuilt\n"
1620 " by reading all the data on the cartridge.",
1622 "I: The tape cartridge is nearing the end of its calculated life. It is\n"
1623 " recommended that you:\n"
1624 " 1. Use another tape cartridge for your next backup.\n"
1625 " 2. Store this tape in a safe place in case you need to restore "
1628 "C: The tape drive needs cleaning:\n"
1629 " 1. If the operation has stopped, eject the tape and clean the drive.\n"
1630 " 2. If the operation has not stopped, wait for it to finish and then\n"
1631 " clean the drive.\n"
1632 " Check the tape drive users manual for device specific cleaning instructions.",
1634 "W: The tape drive is due for routine cleaning:\n"
1635 " 1. Wait for the current operation to finish.\n"
1636 " 2. The use a cleaning cartridge.\n"
1637 " Check the tape drive users manual for device specific cleaning instructions.",
1639 "C: The last cleaning cartridge used in the tape drive has worn out:\n"
1640 " 1. Discard the worn out cleaning cartridge.\n"
1641 " 2. Wait for the current operation to finish.\n"
1642 " 3. Then use a new cleaning cartridge.",
1644 "C: The last cleaning cartridge used in the tape drive was an invalid\n"
1646 " 1. Do not use this cleaning cartridge in this drive.\n"
1647 " 2. Wait for the current operation to finish.\n"
1648 " 3. Then use a new cleaning cartridge.",
1650 "W: The tape drive has requested a retention operation",
1652 "W: A redundant interface port on the tape drive has failed",
1654 "W: A tape drive cooling fan has failed",
1656 "W: A redundant power supply has failed inside the tape drive enclosure.\n"
1657 " Check the enclosure users manual for instructions on replacing the\n"
1658 " failed power supply.",
1660 "W: The tape drive power consumption is outside the specified range.",
1662 "W: Preventive maintenance of the tape drive is required. Check the tape\n"
1663 " drive users manual for device specific preventive maintenance\n"
1664 " tasks or call the tape drive supplier helpline.",
1666 "C: The tape drive has a hardware fault:\n"
1667 " 1. Eject the tape or magazine.\n"
1668 " 2. Reset the drive.\n"
1669 " 3. Restart the operation.",
1671 "C: The tape drive has a hardware fault:\n"
1672 " 1. Turn the tape drive off and then on again.\n"
1673 " 2. Restart the operation.\n"
1674 " 3. If the problem persists, call the tape drive supplier helpline.",
1676 "W: The tape drive has a problem with the application client interface:\n"
1677 " 1. Check the cables and cable connections.\n"
1678 " 2. Restart the operation.",
1680 "C: The operation has failed:\n"
1681 " 1. Eject the tape or magazine.\n"
1682 " 2. Insert the tape or magazine again.\n"
1683 " 3. Restart the operation.",
1685 "W: The firmware download has failed because you have tried to use the\n"
1686 " incorrect firmware for this tape drive. Obtain the correct\n"
1687 " firmware and try again.",
1689 "W: Environmental conditions inside the tape drive are outside the\n"
1690 " specified humidity range.",
1692 "W: Environmental conditions inside the tape drive are outside the\n"
1693 " specified temperature range.",
1695 "W: The voltage supply to the tape drive is outside the specified range.",
1697 "C: A hardware failure of the tape drive is predicted. Call the tape\n"
1698 " drive supplier helpline.",
1700 "W: The tape drive may have a hardware fault. Run extended diagnostics to\n"
1701 " verify and diagnose the problem. Check the tape drive users manual for\n"
1702 " device specific instructions on running extended diagnostic tests.",
1704 "C: The changer mechanism is having difficulty communicating with the tape\n"
1706 " 1. Turn the autoloader off then on.\n"
1707 " 2. Restart the operation.\n"
1708 " 3. If problem persists, call the tape drive supplier helpline.",
1710 "C: A tape has been left in the autoloader by a previous hardware fault:\n"
1711 " 1. Insert an empty magazine to clear the fault.\n"
1712 " 2. If the fault does not clear, turn the autoloader off and then\n"
1714 " 3. If the problem persists, call the tape drive supplier helpline.",
1716 "W: There is a problem with the autoloader mechanism.",
1718 "C: The operation has failed because the autoloader door is open:\n"
1719 " 1. Clear any obstructions from the autoloader door.\n"
1720 " 2. Eject the magazine and then insert it again.\n"
1721 " 3. If the fault does not clear, turn the autoloader off and then\n"
1723 " 4. If the problem persists, call the tape drive supplier helpline.",
1725 "C: The autoloader has a hardware fault:\n"
1726 " 1. Turn the autoloader off and then on again.\n"
1727 " 2. Restart the operation.\n"
1728 " 3. If the problem persists, call the tape drive supplier helpline.\n"
1729 " Check the autoloader users manual for device specific instructions\n"
1730 " on turning the device power on and off.",
1732 "C: The autoloader cannot operate without the magazine,\n"
1733 " 1. Insert the magazine into the autoloader.\n"
1734 " 2. Restart the operation.",
1736 "W: A hardware failure of the changer mechanism is predicted. Call the\n"
1737 " tape drive supplier helpline.",
1745 "W: Media statistics have been lost at some time in the past",
1747 "W: The tape directory on the tape cartridge just unloaded has been\n"
1748 " corrupted. File search performance will be degraded. The tape\n"
1749 " directory can be rebuilt by reading all the data.",
1751 "C: The tape just unloaded could not write its system area successfully:\n"
1752 " 1. Copy data to another tape cartridge.\n"
1753 " 2. Discard the old cartridge.",
1755 "C: The tape system are could not be read successfully at load time:\n"
1756 " 1. Copy data to another tape cartridge.\n",
1758 "C: The start or data could not be found on the tape:\n"
1759 " 1. Check you are using the correct format tape.\n"
1760 " 2. Discard the tape or return the tape to your supplier",
1762 "C: The operation has failed because the media cannot be loaded\n"
1764 " 1. Remove the cartridge, inspect it as specified in the product\n"
1765 " manual, and retry the operation.\n"
1766 " 2. If the problem persists, call the tape drive supplier help line.",
1768 "C: The operation has failed because the medium cannot be unloaded:\n"
1769 " 1. Do not attempt to extract the tape cartridge.\n"
1770 " 2. Call the tape driver supplier help line.",
1772 "C: The tape drive has a problem with the automation interface:\n"
1773 " 1. Check the power to the automation system.\n"
1774 " 2. Check the cables and cable connections.\n"
1775 " 3. Call the supplier help line if problem persists.",
1777 "W: The tape drive has reset itself due to a detected firmware\n"
1778 " fault. If problem persists, call the supplier help line.",
1782 scsiTapeAlertsTapeDevice(unsigned short code
)
1784 const int num
= sizeof(TapeAlertsMessageTable
) /
1785 sizeof(TapeAlertsMessageTable
[0]);
1787 return (code
< num
) ? TapeAlertsMessageTable
[code
] : "Unknown Alert";
1790 // The first character (W, C, I) tells the severity
1791 static const char * ChangerTapeAlertsMessageTable
[]= {
1794 "C: The library mechanism is having difficulty communicating with the\n"
1796 " 1. Turn the library off then on.\n"
1797 " 2. Restart the operation.\n"
1798 " 3. If the problem persists, call the library supplier help line.",
1800 "W: There is a problem with the library mechanism. If problem persists,\n"
1801 " call the library supplier help line.",
1803 "C: The library has a hardware fault:\n"
1804 " 1. Reset the library.\n"
1805 " 2. Restart the operation.\n"
1806 " Check the library users manual for device specific instructions on resetting\n"
1809 "C: The library has a hardware fault:\n"
1810 " 1. Turn the library off then on again.\n"
1811 " 2. Restart the operation.\n"
1812 " 3. If the problem persists, call the library supplier help line.\n"
1813 " Check the library users manual for device specific instructions on turning the\n"
1814 " device power on and off.",
1816 "W: The library mechanism may have a hardware fault.\n"
1817 " Run extended diagnostics to verify and diagnose the problem. Check the library\n"
1818 " users manual for device specific instructions on running extended diagnostic\n"
1821 "C: The library has a problem with the host interface:\n"
1822 " 1. Check the cables and connections.\n"
1823 " 2. Restart the operation.",
1825 "W: A hardware failure of the library is predicted. Call the library\n"
1826 " supplier help line.",
1828 "W: Preventive maintenance of the library is required.\n"
1829 " Check the library users manual for device specific preventative maintenance\n"
1830 " tasks, or call your library supplier help line.",
1832 "C: General environmental conditions inside the library are outside the\n"
1833 " specified humidity range.",
1835 "C: General environmental conditions inside the library are outside the\n"
1836 " specified temperature range.",
1838 "C: The voltage supply to the library is outside the specified range.\n"
1839 " There is a potential problem with the power supply or failure of\n"
1840 " a redundant power supply.",
1842 "C: A cartridge has been left inside the library by a previous hardware\n"
1844 " 1. Insert an empty magazine to clear the fault.\n"
1845 " 2. If the fault does not clear, turn the library off and then on again.\n"
1846 " 3. If the problem persists, call the library supplier help line.",
1848 "W: There is a potential problem with the drive ejecting cartridges or\n"
1849 " with the library mechanism picking a cartridge from a slot.\n"
1850 " 1. No action needs to be taken at this time.\n"
1851 " 2. If the problem persists, call the library supplier help line.",
1853 "W: There is a potential problem with the library mechanism placing a\n"
1854 " cartridge into a slot.\n"
1855 " 1. No action needs to be taken at this time.\n"
1856 " 2. If the problem persists, call the library supplier help line.",
1858 "W: There is a potential problem with the drive or the library mechanism\n"
1859 " loading cartridges, or an incompatible cartridge.",
1861 "C: The library has failed because the door is open:\n"
1862 " 1. Clear any obstructions from the library door.\n"
1863 " 2. Close the library door.\n"
1864 " 3. If the problem persists, call the library supplier help line.",
1866 "C: There is a mechanical problem with the library media import/export\n"
1869 "C: The library cannot operate without the magazine.\n"
1870 " 1. Insert the magazine into the library.\n"
1871 " 2. Restart the operation.",
1873 "W: Library security has been compromised.",
1875 "I: The library security mode has been changed.\n"
1876 " The library has either been put into secure mode, or the library has exited\n"
1877 " the secure mode.\n"
1878 " This is for information purposes only. No action is required.",
1880 "I: The library has been manually turned offline and is unavailable for use.",
1882 "I: A drive inside the library has been taken offline.\n"
1883 " This is for information purposes only. No action is required.",
1885 "W: There is a potential problem with the bar code label or the scanner\n"
1886 " hardware in the library mechanism.\n"
1887 " 1. No action needs to be taken at this time.\n"
1888 " 2. If the problem persists, call the library supplier help line.",
1890 "C: The library has detected an inconsistency in its inventory.\n"
1891 " 1. Redo the library inventory to correct inconsistency.\n"
1892 " 2. Restart the operation.\n"
1893 " Check the applications users manual or the hardware users manual for\n"
1894 " specific instructions on redoing the library inventory.",
1896 "W: A library operation has been attempted that is invalid at this time.",
1898 "W: A redundant interface port on the library has failed.",
1900 "W: A library cooling fan has failed.",
1902 "W: A redundant power supply has failed inside the library. Check the\n"
1903 " library users manual for instructions on replacing the failed power supply.",
1905 "W: The library power consumption is outside the specified range.",
1907 "C: A failure has occurred in the cartridge pass-through mechanism between\n"
1908 " two library modules.",
1910 "C: A cartridge has been left in the pass-through mechanism from a\n"
1911 " previous hardware fault. Check the library users guide for instructions on\n"
1912 " clearing this fault.",
1914 "I: The library was unable to read the bar code on a cartridge.",
1918 scsiTapeAlertsChangerDevice(unsigned short code
)
1920 const int num
= sizeof(ChangerTapeAlertsMessageTable
) /
1921 sizeof(ChangerTapeAlertsMessageTable
[0]);
1923 return (code
< num
) ? ChangerTapeAlertsMessageTable
[code
] : "Unknown Alert";
1927 /* this is a subset of the SCSI additional sense code strings indexed
1928 * by "ascq" for the case when asc==SCSI_ASC_IMPENDING_FAILURE (0x5d)
1930 static const char * strs_for_asc_5d
[] = {
1931 /* 0x00 */ "FAILURE PREDICTION THRESHOLD EXCEEDED",
1932 "MEDIA FAILURE PREDICTION THRESHOLD EXCEEDED",
1933 "LOGICAL UNIT FAILURE PREDICTION THRESHOLD EXCEEDED",
1934 "SPARE AREA EXHAUSTION PREDICTION THRESHOLD EXCEEDED",
1947 /* 0x10 */ "HARDWARE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1948 "HARDWARE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1949 "HARDWARE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1950 "HARDWARE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1951 "HARDWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1952 "HARDWARE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1953 "HARDWARE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1954 "HARDWARE IMPENDING FAILURE CHANNEL PARAMETRICS",
1955 "HARDWARE IMPENDING FAILURE CONTROLLER DETECTED",
1956 "HARDWARE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1957 "HARDWARE IMPENDING FAILURE SEEK TIME PERFORMANCE",
1958 "HARDWARE IMPENDING FAILURE SPIN-UP RETRY COUNT",
1959 "HARDWARE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1963 /* 0x20 */ "CONTROLLER IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1964 "CONTROLLER IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1965 "CONTROLLER IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1966 "CONTROLLER IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1967 "CONTROLLER IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1968 "CONTROLLER IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1969 "CONTROLLER IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1970 "CONTROLLER IMPENDING FAILURE CHANNEL PARAMETRICS",
1971 "CONTROLLER IMPENDING FAILURE CONTROLLER DETECTED",
1972 "CONTROLLER IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1973 "CONTROLLER IMPENDING FAILURE SEEK TIME PERFORMANCE",
1974 "CONTROLLER IMPENDING FAILURE SPIN-UP RETRY COUNT",
1975 "CONTROLLER IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1979 /* 0x30 */ "DATA CHANNEL IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1980 "DATA CHANNEL IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1981 "DATA CHANNEL IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1982 "DATA CHANNEL IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1983 "DATA CHANNEL IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1984 "DATA CHANNEL IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1985 "DATA CHANNEL IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1986 "DATA CHANNEL IMPENDING FAILURE CHANNEL PARAMETRICS",
1987 "DATA CHANNEL IMPENDING FAILURE CONTROLLER DETECTED",
1988 "DATA CHANNEL IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1989 "DATA CHANNEL IMPENDING FAILURE SEEK TIME PERFORMANCE",
1990 "DATA CHANNEL IMPENDING FAILURE SPIN-UP RETRY COUNT",
1991 "DATA CHANNEL IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1995 /* 0x40 */ "SERVO IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1996 "SERVO IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1997 "SERVO IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1998 "SERVO IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1999 "SERVO IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
2000 "SERVO IMPENDING FAILURE ACCESS TIMES TOO HIGH",
2001 "SERVO IMPENDING FAILURE START UNIT TIMES TOO HIGH",
2002 "SERVO IMPENDING FAILURE CHANNEL PARAMETRICS",
2003 "SERVO IMPENDING FAILURE CONTROLLER DETECTED",
2004 "SERVO IMPENDING FAILURE THROUGHPUT PERFORMANCE",
2005 "SERVO IMPENDING FAILURE SEEK TIME PERFORMANCE",
2006 "SERVO IMPENDING FAILURE SPIN-UP RETRY COUNT",
2007 "SERVO IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
2011 /* 0x50 */ "SPINDLE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
2012 "SPINDLE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
2013 "SPINDLE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
2014 "SPINDLE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
2015 "SPINDLE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
2016 "SPINDLE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
2017 "SPINDLE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
2018 "SPINDLE IMPENDING FAILURE CHANNEL PARAMETRICS",
2019 "SPINDLE IMPENDING FAILURE CONTROLLER DETECTED",
2020 "SPINDLE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
2021 "SPINDLE IMPENDING FAILURE SEEK TIME PERFORMANCE",
2022 "SPINDLE IMPENDING FAILURE SPIN-UP RETRY COUNT",
2023 "SPINDLE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
2027 /* 0x60 */ "FIRMWARE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
2028 "FIRMWARE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
2029 "FIRMWARE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
2030 "FIRMWARE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
2031 "FIRMWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
2032 "FIRMWARE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
2033 "FIRMWARE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
2034 "FIRMWARE IMPENDING FAILURE CHANNEL PARAMETRICS",
2035 "FIRMWARE IMPENDING FAILURE CONTROLLER DETECTED",
2036 "FIRMWARE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
2037 "FIRMWARE IMPENDING FAILURE SEEK TIME PERFORMANCE",
2038 "FIRMWARE IMPENDING FAILURE SPIN-UP RETRY COUNT",
2039 /* 0x6c */ "FIRMWARE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT"};
2042 /* this is a subset of the SCSI additional sense code strings indexed
2043 * * by "ascq" for the case when asc==SCSI_ASC_WARNING (0xb)
2045 static const char * strs_for_asc_b
[] = {
2046 /* 0x00 */ "WARNING",
2047 "WARNING - SPECIFIED TEMPERATURE EXCEEDED",
2048 "WARNING - ENCLOSURE DEGRADED"};
2050 static char spare_buff
[128];
2053 scsiGetIEString(UINT8 asc
, UINT8 ascq
)
2057 if (SCSI_ASC_IMPENDING_FAILURE
== asc
) {
2059 return "FAILURE PREDICTION THRESHOLD EXCEEDED (FALSE)";
2061 (sizeof(strs_for_asc_5d
) / sizeof(strs_for_asc_5d
[0]))) {
2062 rp
= strs_for_asc_5d
[ascq
];
2066 snprintf(spare_buff
, sizeof(spare_buff
),
2067 "FAILURE PREDICTION THRESHOLD EXCEEDED: ascq=0x%x", ascq
);
2069 } else if (SCSI_ASC_WARNING
== asc
) {
2070 if (ascq
< (sizeof(strs_for_asc_b
) / sizeof(strs_for_asc_b
[0]))) {
2071 rp
= strs_for_asc_b
[ascq
];
2075 snprintf(spare_buff
, sizeof(spare_buff
), "WARNING: ascq=0x%x", ascq
);
2078 return NULL
; /* not a IE additional sense code */
2083 scsiSmartDefaultSelfTest(scsi_device
* device
)
2087 res
= scsiSendDiagnostic(device
, SCSI_DIAG_DEF_SELF_TEST
, NULL
, 0);
2089 pout("Default self test failed [%s]\n", scsiErrString(res
));
2094 scsiSmartShortSelfTest(scsi_device
* device
)
2098 res
= scsiSendDiagnostic(device
, SCSI_DIAG_BG_SHORT_SELF_TEST
, NULL
, 0);
2100 pout("Short offline self test failed [%s]\n", scsiErrString(res
));
2105 scsiSmartExtendSelfTest(scsi_device
* device
)
2109 res
= scsiSendDiagnostic(device
, SCSI_DIAG_BG_EXTENDED_SELF_TEST
, NULL
, 0);
2111 pout("Long (extended) offline self test failed [%s]\n",
2112 scsiErrString(res
));
2117 scsiSmartShortCapSelfTest(scsi_device
* device
)
2121 res
= scsiSendDiagnostic(device
, SCSI_DIAG_FG_SHORT_SELF_TEST
, NULL
, 0);
2123 pout("Short foreground self test failed [%s]\n", scsiErrString(res
));
2128 scsiSmartExtendCapSelfTest(scsi_device
* device
)
2132 res
= scsiSendDiagnostic(device
, SCSI_DIAG_FG_EXTENDED_SELF_TEST
, NULL
, 0);
2134 pout("Long (extended) foreground self test failed [%s]\n",
2135 scsiErrString(res
));
2140 scsiSmartSelfTestAbort(scsi_device
* device
)
2144 res
= scsiSendDiagnostic(device
, SCSI_DIAG_ABORT_SELF_TEST
, NULL
, 0);
2146 pout("Abort self test failed [%s]\n", scsiErrString(res
));
2150 /* Returns 0 and the expected duration of an extended self test (in seconds)
2151 if successful; any other return value indicates a failure. */
2153 scsiFetchExtendedSelfTestTime(scsi_device
* device
, int * durationSec
,
2159 memset(buff
, 0, sizeof(buff
));
2160 if (modese_len
<= 6) {
2161 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2162 MPAGE_CONTROL_CURRENT
,
2163 buff
, sizeof(buff
)))) {
2164 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2168 } else if (0 == modese_len
)
2171 if (10 == modese_len
) {
2172 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2173 MPAGE_CONTROL_CURRENT
,
2174 buff
, sizeof(buff
));
2178 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2181 if (buff
[offset
+ 1] >= 0xa) {
2182 int res
= (buff
[offset
+ 10] << 8) | buff
[offset
+ 11];
2191 scsiDecodeErrCounterPage(unsigned char * resp
, struct scsiErrorCounter
*ecp
)
2193 memset(ecp
, 0, sizeof(*ecp
));
2194 int num
= (resp
[2] << 8) | resp
[3];
2195 unsigned char * ucp
= &resp
[0] + 4;
2197 int pc
= (ucp
[0] << 8) | ucp
[1];
2198 int pl
= ucp
[3] + 4;
2209 ullp
= &ecp
->counter
[pc
];
2212 ecp
->gotExtraPC
= 1;
2213 ullp
= &ecp
->counter
[7];
2217 unsigned char * xp
= ucp
+ 4;
2218 if (k
> (int)sizeof(*ullp
)) {
2219 xp
+= (k
- sizeof(*ullp
));
2223 for (int j
= 0; j
< k
; ++j
) {
2234 scsiDecodeNonMediumErrPage(unsigned char *resp
,
2235 struct scsiNonMediumError
*nmep
)
2237 memset(nmep
, 0, sizeof(*nmep
));
2238 int num
= (resp
[2] << 8) | resp
[3];
2239 unsigned char * ucp
= &resp
[0] + 4;
2240 int szof
= sizeof(nmep
->counterPC0
);
2242 int pc
= (ucp
[0] << 8) | ucp
[1];
2243 int pl
= ucp
[3] + 4;
2255 nmep
->counterPC0
= 0;
2256 for (int j
= 0; j
< k
; ++j
) {
2258 nmep
->counterPC0
<<= 8;
2259 nmep
->counterPC0
|= xp
[j
];
2270 nmep
->counterTFE_H
= 0;
2271 for (int j
= 0; j
< k
; ++j
) {
2273 nmep
->counterTFE_H
<<= 8;
2274 nmep
->counterTFE_H
|= xp
[j
];
2285 nmep
->counterPE_H
= 0;
2286 for (int j
= 0; j
< k
; ++j
) {
2288 nmep
->counterPE_H
<<= 8;
2289 nmep
->counterPE_H
|= xp
[j
];
2293 nmep
->gotExtraPC
= 1;
2301 /* Counts number of failed self-tests. Also encodes the poweron_hour
2302 of the most recent failed self-test. Return value is negative if
2303 this function has a problem (typically -1), otherwise the bottom 8
2304 bits are the number of failed self tests and the 16 bits above that
2305 are the poweron hour of the most recent failure. Note: aborted self
2306 tests (typically by the user) and self tests in progress are not
2307 considered failures. See Working Draft SCSI Primary Commands - 3
2308 (SPC-3) section 7.2.10 T10/1416-D (rev 22a) */
2310 scsiCountFailedSelfTests(scsi_device
* fd
, int noisy
)
2312 int num
, k
, err
, fails
, fail_hour
;
2314 unsigned char resp
[LOG_RESP_SELF_TEST_LEN
];
2316 if ((err
= scsiLogSense(fd
, SELFTEST_RESULTS_LPAGE
, 0, resp
,
2317 LOG_RESP_SELF_TEST_LEN
, 0))) {
2319 pout("scsiCountSelfTests Failed [%s]\n", scsiErrString(err
));
2322 if ((resp
[0] & 0x3f) != SELFTEST_RESULTS_LPAGE
) {
2324 pout("Self-test Log Sense Failed, page mismatch\n");
2327 // compute page length
2328 num
= (resp
[2] << 8) + resp
[3];
2329 // Log sense page length 0x190 bytes
2332 pout("Self-test Log Sense length is 0x%x not 0x190 bytes\n", num
);
2337 // loop through the twenty possible entries
2338 for (k
= 0, ucp
= resp
+ 4; k
< 20; ++k
, ucp
+= 20 ) {
2340 // timestamp in power-on hours (or zero if test in progress)
2341 int n
= (ucp
[6] << 8) | ucp
[7];
2343 // The spec says "all 20 bytes will be zero if no test" but
2344 // DG has found otherwise. So this is a heuristic.
2345 if ((0 == n
) && (0 == ucp
[4]))
2347 int res
= ucp
[4] & 0xf;
2348 if ((res
> 2) && (res
< 8)) {
2351 fail_hour
= (ucp
[6] << 8) + ucp
[7];
2354 return (fail_hour
<< 8) + fails
;
2357 /* Returns 0 if able to read self test log page; then outputs 1 into
2358 *inProgress if self test still in progress, else outputs 0. */
2360 scsiSelfTestInProgress(scsi_device
* fd
, int * inProgress
)
2364 unsigned char resp
[LOG_RESP_SELF_TEST_LEN
];
2366 if (scsiLogSense(fd
, SELFTEST_RESULTS_LPAGE
, 0, resp
,
2367 LOG_RESP_SELF_TEST_LEN
, 0))
2369 if (resp
[0] != SELFTEST_RESULTS_LPAGE
)
2371 // compute page length
2372 num
= (resp
[2] << 8) + resp
[3];
2373 // Log sense page length 0x190 bytes
2379 *inProgress
= (0xf == (ucp
[4] & 0xf)) ? 1 : 0;
2383 /* Returns a negative value if failed to fetch Contol mode page or it was
2384 malformed. Returns 0 if GLTSD bit is zero and returns 1 if the GLTSD
2385 bit is set. Examines default mode page when current==0 else examines
2386 current mode page. */
2388 scsiFetchControlGLTSD(scsi_device
* device
, int modese_len
, int current
)
2392 int pc
= current
? MPAGE_CONTROL_CURRENT
: MPAGE_CONTROL_DEFAULT
;
2394 memset(buff
, 0, sizeof(buff
));
2395 if (modese_len
<= 6) {
2396 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0, pc
,
2397 buff
, sizeof(buff
)))) {
2398 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2402 } else if (0 == modese_len
)
2405 if (10 == modese_len
) {
2406 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0, pc
,
2407 buff
, sizeof(buff
));
2411 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2412 if ((offset
>= 0) && (buff
[offset
+ 1] >= 0xa))
2413 return (buff
[offset
+ 2] & 2) ? 1 : 0;
2417 /* Returns a negative value on error, 0 if unknown and 1 if SSD,
2418 * otherwise the positive returned value is the speed in rpm. First checks
2419 * the Block Device Characteristics VPD page and if that fails it tries the
2420 * RIGID_DISK_DRIVE_GEOMETRY_PAGE mode page. */
2423 scsiGetRPM(scsi_device
* device
, int modese_len
, int * form_factorp
,
2428 int pc
= MPAGE_CONTROL_DEFAULT
;
2430 memset(buff
, 0, sizeof(buff
));
2431 if ((0 == scsiInquiryVpd(device
, SCSI_VPD_BLOCK_DEVICE_CHARACTERISTICS
,
2432 buff
, sizeof(buff
))) &&
2433 (((buff
[2] << 8) + buff
[3]) > 2)) {
2434 int speed
= (buff
[4] << 8) + buff
[5];
2436 *form_factorp
= buff
[7] & 0xf;
2438 *haw_zbcp
= !!(0x10 & buff
[8]);
2445 if (modese_len
<= 6) {
2446 if ((err
= scsiModeSense(device
, RIGID_DISK_DRIVE_GEOMETRY_PAGE
, 0, pc
,
2447 buff
, sizeof(buff
)))) {
2448 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2452 } else if (0 == modese_len
)
2455 if (10 == modese_len
) {
2456 err
= scsiModeSense10(device
, RIGID_DISK_DRIVE_GEOMETRY_PAGE
, 0, pc
,
2457 buff
, sizeof(buff
));
2461 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2462 return (buff
[offset
+ 20] << 8) | buff
[offset
+ 21];
2465 /* Returns a non-zero value in case of error, wcep/rcdp == -1 - get value,
2466 0 - clear bit, 1 - set bit */
2469 scsiGetSetCache(scsi_device
* device
, int modese_len
, short int * wcep
,
2472 int err
, offset
, resp_len
, sp
;
2473 UINT8 buff
[64], ch_buff
[64];
2474 short set_wce
= *wcep
;
2475 short set_rcd
= *rcdp
;
2477 memset(buff
, 0, sizeof(buff
));
2478 if (modese_len
<= 6) {
2479 if ((err
= scsiModeSense(device
, CACHING_PAGE
, 0, MPAGE_CONTROL_CURRENT
,
2480 buff
, sizeof(buff
)))) {
2481 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2484 device
->set_err(EINVAL
, "SCSI MODE SENSE failed");
2487 } else if (0 == modese_len
)
2491 if (10 == modese_len
) {
2492 err
= scsiModeSense10(device
, CACHING_PAGE
, 0, MPAGE_CONTROL_CURRENT
,
2493 buff
, sizeof(buff
));
2495 device
->set_err(EINVAL
, "SCSI MODE SENSE failed");
2499 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2500 if ((offset
< 0) || (buff
[offset
+ 1] < 0xa)) {
2501 device
->set_err(EINVAL
, "Bad response");
2502 return SIMPLE_ERR_BAD_RESP
;
2505 *wcep
= ((buff
[offset
+ 2] & 0x04) != 0);
2506 *rcdp
= ((buff
[offset
+ 2] & 0x01) != 0);
2508 if((*wcep
== set_wce
|| set_wce
== -1)
2509 && ((*rcdp
== set_rcd
) || set_rcd
== -1))
2510 return 0; // no changes needed or nothing to set
2512 if (modese_len
== 6)
2513 err
= scsiModeSense(device
, CACHING_PAGE
, 0,
2514 MPAGE_CONTROL_CHANGEABLE
,
2515 ch_buff
, sizeof(ch_buff
));
2517 err
= scsiModeSense10(device
, CACHING_PAGE
, 0,
2518 MPAGE_CONTROL_CHANGEABLE
,
2519 ch_buff
, sizeof(ch_buff
));
2521 device
->set_err(EINVAL
, "WCE/RCD bits not changable");
2526 if(set_wce
>= 0 && *wcep
!= set_wce
) {
2527 if (0 == (ch_buff
[offset
+ 2] & 0x04)) {
2528 device
->set_err(EINVAL
, "WCE bit not changable");
2532 buff
[offset
+ 2] |= 0x04; // set bit
2534 buff
[offset
+ 2] &= 0xfb; // clear bit
2537 if(set_rcd
>= 0 && *rcdp
!= set_rcd
) {
2538 if (0 == (ch_buff
[offset
+ 2] & 0x01)) {
2539 device
->set_err(EINVAL
, "RCD bit not changable");
2543 buff
[offset
+ 2] |= 0x01; // set bit
2545 buff
[offset
+ 2] &= 0xfe; // clear bit
2548 /* mask out DPOFUA device specific (disk) parameter bit */
2549 if (10 == modese_len
) {
2550 resp_len
= (buff
[0] << 8) + buff
[1] + 2;
2553 resp_len
= buff
[0] + 1;
2556 sp
= 0; /* Do not change saved values */
2557 if (10 == modese_len
)
2558 err
= scsiModeSelect10(device
, sp
, buff
, resp_len
);
2559 else if (6 == modese_len
)
2560 err
= scsiModeSelect(device
, sp
, buff
, resp_len
);
2562 device
->set_err(EINVAL
, "MODE SELECT command failed");
2567 /* Attempts to set or clear GLTSD bit in Control mode page. If enabled is
2568 0 attempts to clear GLTSD otherwise it attempts to set it. Returns 0 if
2569 successful, negative if low level error, > 0 if higher level error (e.g.
2570 SIMPLE_ERR_BAD_PARAM if GLTSD bit is not changeable). */
2572 scsiSetControlGLTSD(scsi_device
* device
, int enabled
, int modese_len
)
2574 int err
, offset
, resp_len
, sp
;
2578 memset(buff
, 0, sizeof(buff
));
2579 if (modese_len
<= 6) {
2580 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2581 MPAGE_CONTROL_CURRENT
,
2582 buff
, sizeof(buff
)))) {
2583 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2587 } else if (0 == modese_len
)
2590 if (10 == modese_len
) {
2591 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2592 MPAGE_CONTROL_CURRENT
,
2593 buff
, sizeof(buff
));
2597 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2598 if ((offset
< 0) || (buff
[offset
+ 1] < 0xa))
2599 return SIMPLE_ERR_BAD_RESP
;
2603 if (enabled
== (buff
[offset
+ 2] & 2))
2604 return 0; /* GLTSD already in wanted state so nothing to do */
2606 if (modese_len
== 6)
2607 err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2608 MPAGE_CONTROL_CHANGEABLE
,
2609 ch_buff
, sizeof(ch_buff
));
2611 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2612 MPAGE_CONTROL_CHANGEABLE
,
2613 ch_buff
, sizeof(ch_buff
));
2616 if (0 == (ch_buff
[offset
+ 2] & 2))
2617 return SIMPLE_ERR_BAD_PARAM
; /* GLTSD bit not changeable */
2619 /* mask out DPOFUA device specific (disk) parameter bit */
2620 if (10 == modese_len
) {
2621 resp_len
= (buff
[0] << 8) + buff
[1] + 2;
2624 resp_len
= buff
[0] + 1;
2627 sp
= (buff
[offset
] & 0x80) ? 1 : 0; /* PS bit becomes 'SELECT's SP bit */
2629 buff
[offset
+ 2] |= 0x2; /* set GLTSD bit */
2631 buff
[offset
+ 2] &= 0xfd; /* clear GLTSD bit */
2632 if (10 == modese_len
)
2633 err
= scsiModeSelect10(device
, sp
, buff
, resp_len
);
2634 else if (6 == modese_len
)
2635 err
= scsiModeSelect(device
, sp
, buff
, resp_len
);
2639 /* Returns a negative value if failed to fetch Protocol specific port mode
2640 page or it was malformed. Returns transport protocol identifier when
2643 scsiFetchTransportProtocol(scsi_device
* device
, int modese_len
)
2648 memset(buff
, 0, sizeof(buff
));
2649 if (modese_len
<= 6) {
2650 if ((err
= scsiModeSense(device
, PROTOCOL_SPECIFIC_PORT_PAGE
, 0,
2651 MPAGE_CONTROL_CURRENT
,
2652 buff
, sizeof(buff
)))) {
2653 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2657 } else if (0 == modese_len
)
2660 if (10 == modese_len
) {
2661 err
= scsiModeSense10(device
, PROTOCOL_SPECIFIC_PORT_PAGE
, 0,
2662 MPAGE_CONTROL_CURRENT
,
2663 buff
, sizeof(buff
));
2667 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2668 if ((offset
>= 0) && (buff
[offset
+ 1] > 1)) {
2669 if ((0 == (buff
[offset
] & 0x40)) && /* SPF==0 */
2670 (PROTOCOL_SPECIFIC_PORT_PAGE
== (buff
[offset
] & 0x3f)))
2671 return (buff
[offset
+ 2] & 0xf);
2676 const unsigned char *
2677 sg_scsi_sense_desc_find(const unsigned char * sensep
, int sense_len
,
2681 const unsigned char * descp
;
2683 if ((sense_len
< 8) || (0 == (add_sen_len
= sensep
[7])))
2685 if ((sensep
[0] < 0x72) || (sensep
[0] > 0x73))
2687 add_sen_len
= (add_sen_len
< (sense_len
- 8)) ?
2688 add_sen_len
: (sense_len
- 8);
2690 for (int desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
2692 int add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
2693 desc_len
= add_len
+ 2;
2694 if (descp
[0] == desc_type
)
2696 if (add_len
< 0) /* short descriptor ?? */
2702 // Convenience function for formatting strings from SCSI identify
2704 scsi_format_id_string(char * out
, const unsigned char * in
, int n
)
2707 n
= n
> 64 ? 64 : n
;
2708 strncpy(tmp
, (const char *)in
, n
);
2711 // Find the first non-space character (maybe none).
2714 for (i
= 0; tmp
[i
]; i
++)
2715 if (!isspace((int)tmp
[i
])) {
2721 // There are no non-space characters.
2726 // Find the last non-space character.
2727 for (i
= strlen(tmp
)-1; i
>= first
&& isspace((int)tmp
[i
]); i
--);
2730 strncpy(out
, tmp
+first
, last
-first
+1);
2731 out
[last
-first
+1] = '\0';