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>
8 * Copyright (C) 2003-15 Douglas Gilbert <dgilbert@interlog.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * You should have received a copy of the GNU General Public License
16 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
18 * This code was originally developed as a Senior Thesis by Michael Cornwell
19 * at the Concurrent Systems Laboratory (now part of the Storage Systems
20 * Research Center), Jack Baskin School of Engineering, University of
21 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
24 * In the SCSI world "SMART" is a dead or withdrawn standard. In recent
25 * SCSI standards (since SCSI-3) it goes under the awkward name of
26 * "Informational Exceptions" ["IE" or "IEC" (with the "C" for "control")].
27 * The relevant information is spread around several SCSI draft
28 * standards available at http://www.t10.org . Reference is made in the
29 * code to the following acronyms:
30 * - SAM [SCSI Architectural model, versions 2 or 3]
31 * - SPC [SCSI Primary commands, versions 2 or 3]
32 * - SBC [SCSI Block commands, versions 2]
34 * Some SCSI disk vendors have snippets of "SMART" information in their
46 #include "atacmds.h" // FIXME: for smart_command_set only
47 #include "dev_interface.h"
50 const char *scsicmds_c_cvsid
="$Id: scsicmds.cpp 4157 2015-10-20 16:03:57Z chrfranke $"
53 // Print SCSI debug messages?
54 unsigned char scsi_debugmode
= 0;
56 supported_vpd_pages
* supported_vpd_pages_p
= NULL
;
59 supported_vpd_pages::supported_vpd_pages(scsi_device
* device
) : num_valid(0)
61 unsigned char b
[0xfc]; /* pre SPC-3 INQUIRY max response size */
62 memset(b
, 0, sizeof(b
));
63 if (device
&& (0 == scsiInquiryVpd(device
, SCSI_VPD_SUPPORTED_VPD_PAGES
,
65 num_valid
= (b
[2] << 8) + b
[3];
66 int n
= sizeof(pages
);
69 memcpy(pages
, b
+ 4, num_valid
);
74 supported_vpd_pages::is_supported(int vpd_page_num
) const
76 /* Supported VPD pages numbers start at offset 4 and should be in
77 * ascending order but don't assume that. */
78 for (int k
= 0; k
< num_valid
; ++k
) {
79 if (vpd_page_num
== pages
[k
])
85 /* output binary in hex and optionally ascii */
87 dStrHex(const char* str
, int len
, int no_ascii
)
92 const int bpstart
= 5;
93 const int cpstart
= 60;
101 k
= snprintf(buff
+1, sizeof(buff
)-1, "%.2x", a
);
103 if (bpos
>= ((bpstart
+ (9 * 3))))
106 for(i
= 0; i
< len
; i
++)
108 unsigned char c
= *p
++;
110 if (bpos
== (bpstart
+ (9 * 3)))
112 snprintf(buff
+bpos
, sizeof(buff
)-bpos
, "%.2x", (int)(unsigned char)c
);
113 buff
[bpos
+ 2] = ' ';
117 if ((c
< ' ') || (c
>= 0x7f))
121 if (cpos
> (cpstart
+15))
128 k
= snprintf(buff
+1, sizeof(buff
)-1, "%.2x", a
);
138 struct scsi_opcode_name
{
143 static struct scsi_opcode_name opcode_name_arr
[] = {
144 /* in ascending opcode order */
145 {TEST_UNIT_READY
, "test unit ready"}, /* 0x00 */
146 {REQUEST_SENSE
, "request sense"}, /* 0x03 */
147 {INQUIRY
, "inquiry"}, /* 0x12 */
148 {MODE_SELECT
, "mode select(6)"}, /* 0x15 */
149 {MODE_SENSE
, "mode sense(6)"}, /* 0x1a */
150 {START_STOP_UNIT
, "start stop unit"}, /* 0x1b */
151 {RECEIVE_DIAGNOSTIC
, "receive diagnostic"}, /* 0x1c */
152 {SEND_DIAGNOSTIC
, "send diagnostic"}, /* 0x1d */
153 {READ_CAPACITY_10
, "read capacity(10)"}, /* 0x25 */
154 {READ_DEFECT_10
, "read defect list(10)"}, /* 0x37 */
155 {LOG_SELECT
, "log select"}, /* 0x4c */
156 {LOG_SENSE
, "log sense"}, /* 0x4d */
157 {MODE_SELECT_10
, "mode select(10)"}, /* 0x55 */
158 {MODE_SENSE_10
, "mode sense(10)"}, /* 0x5a */
159 {SAT_ATA_PASSTHROUGH_16
, "ata pass-through(16)"}, /* 0x85 */
160 {READ_CAPACITY_16
, "read capacity(16)"}, /* 0x9e,0x10 */
161 {REPORT_LUNS
, "report luns"}, /* 0xa0 */
162 {SAT_ATA_PASSTHROUGH_12
, "ata pass-through(12)"}, /* 0xa1 */
163 {READ_DEFECT_12
, "read defect list(12)"}, /* 0xb7 */
166 static const char * vendor_specific
= "<vendor specific>";
168 /* Need to expand to take service action into account. For commands
169 * of interest the service action is in the 2nd command byte */
171 scsi_get_opcode_name(UINT8 opcode
)
173 int len
= sizeof(opcode_name_arr
) / sizeof(opcode_name_arr
[0]);
176 return vendor_specific
;
177 for (int k
= 0; k
< len
; ++k
) {
178 struct scsi_opcode_name
* onp
= &opcode_name_arr
[k
];
179 if (opcode
== onp
->opcode
)
181 else if (opcode
< onp
->opcode
)
188 scsi_do_sense_disect(const struct scsi_cmnd_io
* io_buf
,
189 struct scsi_sense_disect
* out
)
191 memset(out
, 0, sizeof(struct scsi_sense_disect
));
192 if (SCSI_STATUS_CHECK_CONDITION
== io_buf
->scsi_status
) {
193 int resp_code
= (io_buf
->sensep
[0] & 0x7f);
194 out
->resp_code
= resp_code
;
195 if (resp_code
>= 0x72) {
196 out
->sense_key
= (io_buf
->sensep
[1] & 0xf);
197 out
->asc
= io_buf
->sensep
[2];
198 out
->ascq
= io_buf
->sensep
[3];
199 } else if (resp_code
>= 0x70) {
200 out
->sense_key
= (io_buf
->sensep
[2] & 0xf);
201 if (io_buf
->resp_sense_len
> 13) {
202 out
->asc
= io_buf
->sensep
[12];
203 out
->ascq
= io_buf
->sensep
[13];
210 scsiSimpleSenseFilter(const struct scsi_sense_disect
* sinfo
)
212 switch (sinfo
->sense_key
) {
213 case SCSI_SK_NO_SENSE
:
214 case SCSI_SK_RECOVERED_ERR
:
215 return SIMPLE_NO_ERROR
;
216 case SCSI_SK_NOT_READY
:
217 if (SCSI_ASC_NO_MEDIUM
== sinfo
->asc
)
218 return SIMPLE_ERR_NO_MEDIUM
;
219 else if (SCSI_ASC_NOT_READY
== sinfo
->asc
) {
220 if (0x1 == sinfo
->ascq
)
221 return SIMPLE_ERR_BECOMING_READY
;
223 return SIMPLE_ERR_NOT_READY
;
225 return SIMPLE_ERR_NOT_READY
;
226 case SCSI_SK_MEDIUM_ERROR
:
227 case SCSI_SK_HARDWARE_ERROR
:
228 return SIMPLE_ERR_MEDIUM_HARDWARE
;
229 case SCSI_SK_ILLEGAL_REQUEST
:
230 if (SCSI_ASC_UNKNOWN_OPCODE
== sinfo
->asc
)
231 return SIMPLE_ERR_BAD_OPCODE
;
232 else if (SCSI_ASC_INVALID_FIELD
== sinfo
->asc
)
233 return SIMPLE_ERR_BAD_FIELD
;
234 else if (SCSI_ASC_UNKNOWN_PARAM
== sinfo
->asc
)
235 return SIMPLE_ERR_BAD_PARAM
;
237 return SIMPLE_ERR_BAD_PARAM
; /* all other illegal request */
238 case SCSI_SK_UNIT_ATTENTION
:
239 return SIMPLE_ERR_TRY_AGAIN
;
240 case SCSI_SK_ABORTED_COMMAND
:
241 return SIMPLE_ERR_ABORTED_COMMAND
;
243 return SIMPLE_ERR_UNKNOWN
;
248 scsiErrString(int scsiErr
)
251 return strerror(-scsiErr
);
253 case SIMPLE_NO_ERROR
:
255 case SIMPLE_ERR_NOT_READY
:
256 return "device not ready";
257 case SIMPLE_ERR_BAD_OPCODE
:
258 return "unsupported scsi opcode";
259 case SIMPLE_ERR_BAD_FIELD
:
260 return "unsupported field in scsi command";
261 case SIMPLE_ERR_BAD_PARAM
:
262 return "badly formed scsi parameters";
263 case SIMPLE_ERR_BAD_RESP
:
264 return "scsi response fails sanity test";
265 case SIMPLE_ERR_NO_MEDIUM
:
266 return "no medium present";
267 case SIMPLE_ERR_BECOMING_READY
:
268 return "device will be ready soon";
269 case SIMPLE_ERR_TRY_AGAIN
:
270 return "unit attention reported, try again";
271 case SIMPLE_ERR_MEDIUM_HARDWARE
:
272 return "medium or hardware error (serious)";
273 case SIMPLE_ERR_UNKNOWN
:
274 return "unknown error (unexpected sense key)";
275 case SIMPLE_ERR_ABORTED_COMMAND
:
276 return "aborted command";
278 return "unknown error";
282 /* Iterates to next designation descriptor in the device identification
283 * VPD page. The 'initial_desig_desc' should point to start of first
284 * descriptor with 'page_len' being the number of valid bytes in that
285 * and following descriptors. To start, 'off' should point to a negative
286 * value, thereafter it should point to the value yielded by the previous
287 * call. If 0 returned then 'initial_desig_desc + *off' should be a valid
288 * descriptor; returns -1 if normal end condition and -2 for an abnormal
289 * termination. Matches association, designator_type and/or code_set when
290 * any of those values are greater than or equal to zero. */
292 scsi_vpd_dev_id_iter(const unsigned char * initial_desig_desc
, int page_len
,
293 int * off
, int m_assoc
, int m_desig_type
, int m_code_set
)
295 const unsigned char * ucp
;
298 for (k
= *off
, ucp
= initial_desig_desc
; (k
+ 3) < page_len
; ) {
299 k
= (k
< 0) ? 0 : (k
+ ucp
[k
+ 3] + 4);
300 if ((k
+ 4) > page_len
)
302 int c_set
= (ucp
[k
] & 0xf);
303 if ((m_code_set
>= 0) && (m_code_set
!= c_set
))
305 int assoc
= ((ucp
[k
+ 1] >> 4) & 0x3);
306 if ((m_assoc
>= 0) && (m_assoc
!= assoc
))
308 int desig_type
= (ucp
[k
+ 1] & 0xf);
309 if ((m_desig_type
>= 0) && (m_desig_type
!= desig_type
))
314 return (k
== page_len
) ? -1 : -2;
317 /* Decode VPD page 0x83 logical unit designator into a string. If both
318 * numeric address and SCSI name string present, prefer the former.
319 * Returns 0 on success, -1 on error with error string in s. */
321 scsi_decode_lu_dev_id(const unsigned char * b
, int blen
, char * s
, int slen
,
334 int have_scsi_ns
= 0;
337 while ((u
= scsi_vpd_dev_id_iter(b
, blen
, &off
, -1, -1, -1)) == 0) {
338 const unsigned char * ucp
= b
+ off
;
340 if ((off
+ i_len
+ 4) > blen
) {
341 snprintf(s
+si
, slen
-si
, "error: designator length");
344 int assoc
= ((ucp
[1] >> 4) & 0x3);
345 if (transport
&& assoc
&& (ucp
[1] & 0x80) && (*transport
< 0))
346 *transport
= (ucp
[0] >> 4) & 0xf;
349 const unsigned char * ip
= ucp
+ 4;
350 int c_set
= (ucp
[0] & 0xf);
351 int desig_type
= (ucp
[1] & 0xf);
354 switch (desig_type
) {
355 case 0: /* vendor specific */
356 case 1: /* T10 vendor identification */
358 case 2: /* EUI-64 based */
359 if ((8 != i_len
) && (12 != i_len
) && (16 != i_len
)) {
360 snprintf(s
+si
, slen
-si
, "error: EUI-64 length");
365 si
+= snprintf(s
+si
, slen
-si
, "0x");
366 for (int m
= 0; m
< i_len
; ++m
)
367 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
371 snprintf(s
+si
, slen
-si
, "error: NAA bad code_set");
374 naa
= (ip
[0] >> 4) & 0xff;
375 if ((naa
< 2) || (naa
> 6) || (4 == naa
)) {
376 snprintf(s
+si
, slen
-si
, "error: unexpected NAA");
381 if (2 == naa
) { /* NAA IEEE Extended */
383 snprintf(s
+si
, slen
-si
, "error: NAA 2 length");
386 si
+= snprintf(s
+si
, slen
-si
, "0x");
387 for (int m
= 0; m
< 8; ++m
)
388 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
389 } else if ((3 == naa
) || (5 == naa
)) {
390 /* NAA=3 Locally assigned; NAA=5 IEEE Registered */
392 snprintf(s
+si
, slen
-si
, "error: NAA 3 or 5 length");
395 si
+= snprintf(s
+si
, slen
-si
, "0x");
396 for (int m
= 0; m
< 8; ++m
)
397 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
398 } else if (6 == naa
) { /* NAA IEEE Registered extended */
400 snprintf(s
+si
, slen
-si
, "error: NAA 6 length");
403 si
+= snprintf(s
+si
, slen
-si
, "0x");
404 for (int m
= 0; m
< 16; ++m
)
405 si
+= snprintf(s
+si
, slen
-si
, "%02x", (unsigned int)ip
[m
]);
408 case 4: /* Relative target port */
409 case 5: /* (primary) Target port group */
410 case 6: /* Logical unit group */
411 case 7: /* MD5 logical unit identifier */
413 case 8: /* SCSI name string */
415 snprintf(s
+si
, slen
-si
, "error: SCSI name string");
418 /* does %s print out UTF-8 ok?? */
420 si
+= snprintf(s
+si
, slen
-si
, "%s", (const char *)ip
);
424 default: /* reserved */
429 snprintf(s
+si
, slen
-si
, "error: bad structure");
435 /* Sends LOG SENSE command. Returns 0 if ok, 1 if device NOT READY, 2 if
436 command not supported, 3 if field (within command) not supported or
437 returns negated errno. SPC-3 sections 6.6 and 7.2 (rec 22a).
438 N.B. Sets PC==1 to fetch "current cumulative" log pages.
439 If known_resp_len > 0 then a single fetch is done for this response
440 length. If known_resp_len == 0 then twin fetches are performed, the
441 first to deduce the response length, then send the same command again
442 requesting the deduced response length. This protects certain fragile
443 HBAs. The twin fetch technique should not be used with the TapeAlert
444 log page since it clears its state flags after each fetch. */
446 scsiLogSense(scsi_device
* device
, int pagenum
, int subpagenum
, UINT8
*pBuf
,
447 int bufLen
, int known_resp_len
)
449 struct scsi_cmnd_io io_hdr
;
450 struct scsi_sense_disect sinfo
;
455 if (known_resp_len
> bufLen
)
457 if (known_resp_len
> 0)
458 pageLen
= known_resp_len
;
460 /* Starting twin fetch strategy: first fetch to find respone length */
462 if (pageLen
> bufLen
)
465 memset(pBuf
, 0, pageLen
);
467 memset(&io_hdr
, 0, sizeof(io_hdr
));
468 memset(cdb
, 0, sizeof(cdb
));
469 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
470 io_hdr
.dxfer_len
= pageLen
;
471 io_hdr
.dxferp
= pBuf
;
473 cdb
[2] = 0x40 | (pagenum
& 0x3f); /* Page control (PC)==1 */
475 cdb
[7] = (pageLen
>> 8) & 0xff;
476 cdb
[8] = pageLen
& 0xff;
478 io_hdr
.cmnd_len
= sizeof(cdb
);
479 io_hdr
.sensep
= sense
;
480 io_hdr
.max_sense_len
= sizeof(sense
);
481 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
483 if (!device
->scsi_pass_through(&io_hdr
))
484 return -device
->get_errno();
485 scsi_do_sense_disect(&io_hdr
, &sinfo
);
487 if ((res
= scsiSimpleSenseFilter(&sinfo
)))
489 /* sanity check on response */
490 if ((SUPPORTED_LPAGES
!= pagenum
) && ((pBuf
[0] & 0x3f) != pagenum
))
491 return SIMPLE_ERR_BAD_RESP
;
492 if (0 == ((pBuf
[2] << 8) + pBuf
[3]))
493 return SIMPLE_ERR_BAD_RESP
;
494 pageLen
= (pBuf
[2] << 8) + pBuf
[3] + 4;
495 if (4 == pageLen
) /* why define a lpage with no payload? */
496 pageLen
= 252; /* some IBM tape drives don't like double fetch */
497 /* some SCSI HBA don't like "odd" length transfers */
500 if (pageLen
> bufLen
)
504 memset(&io_hdr
, 0, sizeof(io_hdr
));
505 memset(cdb
, 0, sizeof(cdb
));
506 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
507 io_hdr
.dxfer_len
= pageLen
;
508 io_hdr
.dxferp
= pBuf
;
510 cdb
[2] = 0x40 | (pagenum
& 0x3f); /* Page control (PC)==1 */
511 cdb
[7] = (pageLen
>> 8) & 0xff;
512 cdb
[8] = pageLen
& 0xff;
514 io_hdr
.cmnd_len
= sizeof(cdb
);
515 io_hdr
.sensep
= sense
;
516 io_hdr
.max_sense_len
= sizeof(sense
);
517 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
519 if (!device
->scsi_pass_through(&io_hdr
))
520 return -device
->get_errno();
521 scsi_do_sense_disect(&io_hdr
, &sinfo
);
522 int status
= scsiSimpleSenseFilter(&sinfo
);
525 /* sanity check on response */
526 if ((SUPPORTED_LPAGES
!= pagenum
) && ((pBuf
[0] & 0x3f) != pagenum
))
527 return SIMPLE_ERR_BAD_RESP
;
528 if (0 == ((pBuf
[2] << 8) + pBuf
[3]))
529 return SIMPLE_ERR_BAD_RESP
;
533 /* Sends a LOG SELECT command. Can be used to set log page values
534 * or reset one log page (or all of them) to its defaults (typically zero).
535 * Returns 0 if ok, 1 if NOT READY, 2 if command not supported, * 3 if
536 * field in command not supported, * 4 if bad parameter to command or
537 * returns negated errno. SPC-4 sections 6.5 and 7.2 (rev 20) */
539 scsiLogSelect(scsi_device
* device
, int pcr
, int sp
, int pc
, int pagenum
,
540 int subpagenum
, UINT8
*pBuf
, int bufLen
)
542 struct scsi_cmnd_io io_hdr
;
543 struct scsi_sense_disect sinfo
;
547 memset(&io_hdr
, 0, sizeof(io_hdr
));
548 memset(cdb
, 0, sizeof(cdb
));
549 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
550 io_hdr
.dxfer_len
= bufLen
;
551 io_hdr
.dxferp
= pBuf
;
553 cdb
[1] = (pcr
? 2 : 0) | (sp
? 1 : 0);
554 cdb
[2] = ((pc
<< 6) & 0xc0) | (pagenum
& 0x3f);
555 cdb
[3] = (subpagenum
& 0xff);
556 cdb
[7] = ((bufLen
>> 8) & 0xff);
557 cdb
[8] = (bufLen
& 0xff);
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 /* Send MODE SENSE (6 byte) command. Returns 0 if ok, 1 if NOT READY,
571 * 2 if command not supported (then MODE SENSE(10) should be supported),
572 * 3 if field in command not supported or returns negated errno.
573 * SPC-3 sections 6.9 and 7.4 (rev 22a) [mode subpage==0] */
575 scsiModeSense(scsi_device
* device
, int pagenum
, int subpagenum
, int pc
,
576 UINT8
*pBuf
, int bufLen
)
578 struct scsi_cmnd_io io_hdr
;
579 struct scsi_sense_disect sinfo
;
583 if ((bufLen
< 0) || (bufLen
> 255))
585 memset(&io_hdr
, 0, sizeof(io_hdr
));
586 memset(cdb
, 0, sizeof(cdb
));
587 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
588 io_hdr
.dxfer_len
= bufLen
;
589 io_hdr
.dxferp
= pBuf
;
591 cdb
[2] = (pc
<< 6) | (pagenum
& 0x3f);
595 io_hdr
.cmnd_len
= sizeof(cdb
);
596 io_hdr
.sensep
= sense
;
597 io_hdr
.max_sense_len
= sizeof(sense
);
598 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
600 if (!device
->scsi_pass_through(&io_hdr
))
601 return -device
->get_errno();
602 scsi_do_sense_disect(&io_hdr
, &sinfo
);
603 int status
= scsiSimpleSenseFilter(&sinfo
);
604 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
605 if (!device
->scsi_pass_through(&io_hdr
))
606 return -device
->get_errno();
607 scsi_do_sense_disect(&io_hdr
, &sinfo
);
608 status
= scsiSimpleSenseFilter(&sinfo
);
610 if ((0 == status
) && (ALL_MODE_PAGES
!= pagenum
)) {
613 offset
= scsiModePageOffset(pBuf
, bufLen
, 0);
615 return SIMPLE_ERR_BAD_RESP
;
616 else if (pagenum
!= (pBuf
[offset
] & 0x3f))
617 return SIMPLE_ERR_BAD_RESP
;
622 /* Sends a 6 byte MODE SELECT command. Assumes given pBuf is the response
623 * from a corresponding 6 byte MODE SENSE command. Such a response should
624 * have a 4 byte header followed by 0 or more 8 byte block descriptors
625 * (normally 1) and then 1 mode page. Returns 0 if ok, 1 if NOT READY,
626 * 2 if command not supported (then MODE SELECT(10) may be supported),
627 * 3 if field in command not supported, 4 if bad parameter to command
628 * or returns negated errno. SPC-3 sections 6.7 and 7.4 (rev 22a) */
630 scsiModeSelect(scsi_device
* device
, int sp
, UINT8
*pBuf
, int bufLen
)
632 struct scsi_cmnd_io io_hdr
;
633 struct scsi_sense_disect sinfo
;
636 int pg_offset
, pg_len
, hdr_plus_1_pg
;
638 pg_offset
= 4 + pBuf
[3];
639 if (pg_offset
+ 2 >= bufLen
)
641 pg_len
= pBuf
[pg_offset
+ 1] + 2;
642 hdr_plus_1_pg
= pg_offset
+ pg_len
;
643 if (hdr_plus_1_pg
> bufLen
)
645 pBuf
[0] = 0; /* Length of returned mode sense data reserved for SELECT */
646 pBuf
[pg_offset
] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
647 memset(&io_hdr
, 0, sizeof(io_hdr
));
648 memset(cdb
, 0, sizeof(cdb
));
649 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
650 io_hdr
.dxfer_len
= hdr_plus_1_pg
;
651 io_hdr
.dxferp
= pBuf
;
652 cdb
[0] = MODE_SELECT
;
653 cdb
[1] = 0x10 | (sp
& 1); /* set PF (page format) bit always */
654 cdb
[4] = hdr_plus_1_pg
; /* make sure only one page sent */
656 io_hdr
.cmnd_len
= sizeof(cdb
);
657 io_hdr
.sensep
= sense
;
658 io_hdr
.max_sense_len
= sizeof(sense
);
659 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
661 if (!device
->scsi_pass_through(&io_hdr
))
662 return -device
->get_errno();
663 scsi_do_sense_disect(&io_hdr
, &sinfo
);
664 return scsiSimpleSenseFilter(&sinfo
);
667 /* MODE SENSE (10 byte). Returns 0 if ok, 1 if NOT READY, 2 if command
668 * not supported (then MODE SENSE(6) might be supported), 3 if field in
669 * command not supported or returns negated errno.
670 * SPC-3 sections 6.10 and 7.4 (rev 22a) [mode subpage==0] */
672 scsiModeSense10(scsi_device
* device
, int pagenum
, int subpagenum
, int pc
,
673 UINT8
*pBuf
, int bufLen
)
675 struct scsi_cmnd_io io_hdr
;
676 struct scsi_sense_disect sinfo
;
680 memset(&io_hdr
, 0, sizeof(io_hdr
));
681 memset(cdb
, 0, sizeof(cdb
));
682 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
683 io_hdr
.dxfer_len
= bufLen
;
684 io_hdr
.dxferp
= pBuf
;
685 cdb
[0] = MODE_SENSE_10
;
686 cdb
[2] = (pc
<< 6) | (pagenum
& 0x3f);
688 cdb
[7] = (bufLen
>> 8) & 0xff;
689 cdb
[8] = bufLen
& 0xff;
691 io_hdr
.cmnd_len
= sizeof(cdb
);
692 io_hdr
.sensep
= sense
;
693 io_hdr
.max_sense_len
= sizeof(sense
);
694 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
696 if (!device
->scsi_pass_through(&io_hdr
))
697 return -device
->get_errno();
698 scsi_do_sense_disect(&io_hdr
, &sinfo
);
699 int status
= scsiSimpleSenseFilter(&sinfo
);
700 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
701 if (!device
->scsi_pass_through(&io_hdr
))
702 return -device
->get_errno();
703 scsi_do_sense_disect(&io_hdr
, &sinfo
);
704 status
= scsiSimpleSenseFilter(&sinfo
);
706 if ((0 == status
) && (ALL_MODE_PAGES
!= pagenum
)) {
709 offset
= scsiModePageOffset(pBuf
, bufLen
, 1);
711 return SIMPLE_ERR_BAD_RESP
;
712 else if (pagenum
!= (pBuf
[offset
] & 0x3f))
713 return SIMPLE_ERR_BAD_RESP
;
718 /* Sends a 10 byte MODE SELECT command. Assumes given pBuf is the response
719 * from a corresponding 10 byte MODE SENSE command. Such a response should
720 * have a 8 byte header followed by 0 or more 8 byte block descriptors
721 * (normally 1) and then 1 mode page. Returns 0 if ok, 1 NOT REAFY, 2 if
722 * command not supported (then MODE SELECT(6) may be supported), 3 if field
723 * in command not supported, 4 if bad parameter to command or returns
724 * negated errno. SPC-3 sections 6.8 and 7.4 (rev 22a) */
726 scsiModeSelect10(scsi_device
* device
, int sp
, UINT8
*pBuf
, int bufLen
)
728 struct scsi_cmnd_io io_hdr
;
729 struct scsi_sense_disect sinfo
;
732 int pg_offset
, pg_len
, hdr_plus_1_pg
;
734 pg_offset
= 8 + (pBuf
[6] << 8) + pBuf
[7];
735 if (pg_offset
+ 2 >= bufLen
)
737 pg_len
= pBuf
[pg_offset
+ 1] + 2;
738 hdr_plus_1_pg
= pg_offset
+ pg_len
;
739 if (hdr_plus_1_pg
> bufLen
)
742 pBuf
[1] = 0; /* Length of returned mode sense data reserved for SELECT */
743 pBuf
[pg_offset
] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
744 memset(&io_hdr
, 0, sizeof(io_hdr
));
745 memset(cdb
, 0, sizeof(cdb
));
746 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
747 io_hdr
.dxfer_len
= hdr_plus_1_pg
;
748 io_hdr
.dxferp
= pBuf
;
749 cdb
[0] = MODE_SELECT_10
;
750 cdb
[1] = 0x10 | (sp
& 1); /* set PF (page format) bit always */
751 cdb
[8] = hdr_plus_1_pg
; /* make sure only one page sent */
753 io_hdr
.cmnd_len
= sizeof(cdb
);
754 io_hdr
.sensep
= sense
;
755 io_hdr
.max_sense_len
= sizeof(sense
);
756 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
758 if (!device
->scsi_pass_through(&io_hdr
))
759 return -device
->get_errno();
760 scsi_do_sense_disect(&io_hdr
, &sinfo
);
761 return scsiSimpleSenseFilter(&sinfo
);
764 /* Standard INQUIRY returns 0 for ok, anything else is a major problem.
765 * bufLen should be 36 for unsafe devices (like USB mass storage stuff)
766 * otherwise they can lock up! SPC-3 sections 6.4 and 7.6 (rev 22a) */
768 scsiStdInquiry(scsi_device
* device
, UINT8
*pBuf
, int bufLen
)
770 struct scsi_sense_disect sinfo
;
771 struct scsi_cmnd_io io_hdr
;
775 if ((bufLen
< 0) || (bufLen
> 1023))
777 memset(&io_hdr
, 0, sizeof(io_hdr
));
778 memset(cdb
, 0, sizeof(cdb
));
779 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
780 io_hdr
.dxfer_len
= bufLen
;
781 io_hdr
.dxferp
= pBuf
;
783 cdb
[3] = (bufLen
>> 8) & 0xff;
784 cdb
[4] = (bufLen
& 0xff);
786 io_hdr
.cmnd_len
= sizeof(cdb
);
787 io_hdr
.sensep
= sense
;
788 io_hdr
.max_sense_len
= sizeof(sense
);
789 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
791 if (!device
->scsi_pass_through(&io_hdr
))
792 return -device
->get_errno();
793 scsi_do_sense_disect(&io_hdr
, &sinfo
);
794 return scsiSimpleSenseFilter(&sinfo
);
797 /* INQUIRY to fetch Vital Page Data. Returns 0 if ok, 1 if NOT READY
798 * (unlikely), 2 if command not supported, 3 if field in command not
799 * supported, 5 if response indicates that EVPD bit ignored or returns
800 * negated errno. SPC-3 section 6.4 and 7.6 (rev 22a) */
802 scsiInquiryVpd(scsi_device
* device
, int vpd_page
, UINT8
*pBuf
, int bufLen
)
804 struct scsi_cmnd_io io_hdr
;
805 struct scsi_sense_disect sinfo
;
810 /* Assume SCSI_VPD_SUPPORTED_VPD_PAGES is first VPD page fetched */
811 if ((SCSI_VPD_SUPPORTED_VPD_PAGES
!= vpd_page
) &&
812 supported_vpd_pages_p
&&
813 (! supported_vpd_pages_p
->is_supported(vpd_page
)))
816 if ((bufLen
< 0) || (bufLen
> 1023))
819 memset(&io_hdr
, 0, sizeof(io_hdr
));
820 memset(cdb
, 0, sizeof(cdb
));
823 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
824 io_hdr
.dxfer_len
= bufLen
;
825 io_hdr
.dxferp
= pBuf
;
827 cdb
[1] = 0x1; /* set EVPD bit (enable Vital Product Data) */
829 cdb
[3] = (bufLen
>> 8) & 0xff;
830 cdb
[4] = (bufLen
& 0xff);
832 io_hdr
.cmnd_len
= sizeof(cdb
);
833 io_hdr
.sensep
= sense
;
834 io_hdr
.max_sense_len
= sizeof(sense
);
835 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
837 if (!device
->scsi_pass_through(&io_hdr
))
838 return -device
->get_errno();
839 scsi_do_sense_disect(&io_hdr
, &sinfo
);
840 if ((SCSI_STATUS_CHECK_CONDITION
== io_hdr
.scsi_status
) &&
841 (SCSI_SK_ILLEGAL_REQUEST
== sinfo
.sense_key
) &&
842 (SCSI_ASC_INVALID_FIELD
== sinfo
.asc
) &&
844 bufLen
&= 0xff; /* make sure cdb[3] is 0 next time around */
848 if ((res
= scsiSimpleSenseFilter(&sinfo
)))
850 /* Guard against devices that ignore EVPD bit and do standard INQUIRY */
852 if (vpd_page
== pBuf
[1]) {
853 if ((0x80 == vpd_page
) && (bufLen
> 2) && (0x0 != pBuf
[2]))
854 return SIMPLE_ERR_BAD_RESP
;
856 return SIMPLE_ERR_BAD_RESP
;
861 /* REQUEST SENSE command. Returns 0 if ok, anything else major problem.
862 * SPC-3 section 6.27 (rev 22a) */
864 scsiRequestSense(scsi_device
* device
, struct scsi_sense_disect
* sense_info
)
866 struct scsi_cmnd_io io_hdr
;
871 memset(&io_hdr
, 0, sizeof(io_hdr
));
872 memset(cdb
, 0, sizeof(cdb
));
873 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
874 io_hdr
.dxfer_len
= sizeof(buff
);
875 io_hdr
.dxferp
= buff
;
876 cdb
[0] = REQUEST_SENSE
;
877 cdb
[4] = sizeof(buff
);
879 io_hdr
.cmnd_len
= sizeof(cdb
);
880 io_hdr
.sensep
= sense
;
881 io_hdr
.max_sense_len
= sizeof(sense
);
882 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
884 if (!device
->scsi_pass_through(&io_hdr
))
885 return -device
->get_errno();
887 UINT8 resp_code
= buff
[0] & 0x7f;
888 sense_info
->resp_code
= resp_code
;
889 sense_info
->sense_key
= buff
[2] & 0xf;
891 sense_info
->ascq
= 0;
892 if ((0x70 == resp_code
) || (0x71 == resp_code
)) {
893 int len
= buff
[7] + 8;
895 sense_info
->asc
= buff
[12];
896 sense_info
->ascq
= buff
[13];
899 // fill progrss indicator, if available
900 sense_info
->progress
= -1;
902 const unsigned char * ucp
;
906 sk
= (buff
[2] & 0xf);
907 if ((sizeof(buff
) < 18) ||
908 ((SCSI_SK_NO_SENSE
!= sk
) && (SCSI_SK_NOT_READY
!= sk
))) {
911 if (buff
[15] & 0x80) { /* SKSV bit set */
912 sense_info
->progress
= (buff
[16] << 8) + buff
[17];
919 /* sense key specific progress (0x2) or progress descriptor (0xa) */
920 sk
= (buff
[1] & 0xf);
921 sk_pr
= (SCSI_SK_NO_SENSE
== sk
) || (SCSI_SK_NOT_READY
== sk
);
922 if (sk_pr
&& ((ucp
= sg_scsi_sense_desc_find(buff
, sizeof(buff
), 2))) &&
923 (0x6 == ucp
[1]) && (0x80 & ucp
[4])) {
924 sense_info
->progress
= (ucp
[5] << 8) + ucp
[6];
926 } else if (((ucp
= sg_scsi_sense_desc_find(buff
, sizeof(buff
), 0xa))) &&
928 sense_info
->progress
= (ucp
[6] << 8) + ucp
[7];
939 /* SEND DIAGNOSTIC command. Returns 0 if ok, 1 if NOT READY, 2 if command
940 * not supported, 3 if field in command not supported or returns negated
941 * errno. SPC-3 section 6.28 (rev 22a) */
943 scsiSendDiagnostic(scsi_device
* device
, int functioncode
, UINT8
*pBuf
,
946 struct scsi_cmnd_io io_hdr
;
947 struct scsi_sense_disect sinfo
;
951 memset(&io_hdr
, 0, sizeof(io_hdr
));
952 memset(cdb
, 0, sizeof(cdb
));
953 io_hdr
.dxfer_dir
= bufLen
? DXFER_TO_DEVICE
: DXFER_NONE
;
954 io_hdr
.dxfer_len
= bufLen
;
955 io_hdr
.dxferp
= pBuf
;
956 cdb
[0] = SEND_DIAGNOSTIC
;
957 if (SCSI_DIAG_DEF_SELF_TEST
== functioncode
)
958 cdb
[1] = 0x4; /* SelfTest bit */
959 else if (SCSI_DIAG_NO_SELF_TEST
!= functioncode
)
960 cdb
[1] = (functioncode
& 0x7) << 5; /* SelfTest _code_ */
961 else /* SCSI_DIAG_NO_SELF_TEST == functioncode */
962 cdb
[1] = 0x10; /* PF bit */
963 cdb
[3] = (bufLen
>> 8) & 0xff;
964 cdb
[4] = bufLen
& 0xff;
966 io_hdr
.cmnd_len
= sizeof(cdb
);
967 io_hdr
.sensep
= sense
;
968 io_hdr
.max_sense_len
= sizeof(sense
);
969 /* worst case is an extended foreground self test on a big disk */
970 io_hdr
.timeout
= SCSI_TIMEOUT_SELF_TEST
;
972 if (!device
->scsi_pass_through(&io_hdr
))
973 return -device
->get_errno();
974 scsi_do_sense_disect(&io_hdr
, &sinfo
);
975 return scsiSimpleSenseFilter(&sinfo
);
978 /* TEST UNIT READY command. SPC-3 section 6.33 (rev 22a) */
980 _testunitready(scsi_device
* device
, struct scsi_sense_disect
* sinfo
)
982 struct scsi_cmnd_io io_hdr
;
986 memset(&io_hdr
, 0, sizeof(io_hdr
));
987 memset(cdb
, 0, sizeof(cdb
));
988 io_hdr
.dxfer_dir
= DXFER_NONE
;
989 io_hdr
.dxfer_len
= 0;
990 io_hdr
.dxferp
= NULL
;
991 cdb
[0] = TEST_UNIT_READY
;
993 io_hdr
.cmnd_len
= sizeof(cdb
);
994 io_hdr
.sensep
= sense
;
995 io_hdr
.max_sense_len
= sizeof(sense
);
996 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
998 if (!device
->scsi_pass_through(&io_hdr
))
999 return -device
->get_errno();
1000 scsi_do_sense_disect(&io_hdr
, sinfo
);
1004 /* Returns 0 for device responds and media ready, 1 for device responds and
1005 media not ready, or returns a negated errno value */
1007 scsiTestUnitReady(scsi_device
* device
)
1009 struct scsi_sense_disect sinfo
;
1012 status
= _testunitready(device
, &sinfo
);
1015 status
= scsiSimpleSenseFilter(&sinfo
);
1016 if (SIMPLE_ERR_TRY_AGAIN
== status
) {
1017 /* power on reset, media changed, ok ... try again */
1018 status
= _testunitready(device
, &sinfo
);
1021 status
= scsiSimpleSenseFilter(&sinfo
);
1026 /* READ DEFECT (10) command. Returns 0 if ok, 1 if NOT READY, 2 if
1027 * command not supported, 3 if field in command not supported, 101 if
1028 * defect list not found (e.g. SSD may not have defect list) or returns
1029 * negated errno. SBC-2 section 5.12 (rev 16) */
1031 scsiReadDefect10(scsi_device
* device
, int req_plist
, int req_glist
,
1032 int dl_format
, UINT8
*pBuf
, int bufLen
)
1034 struct scsi_cmnd_io io_hdr
;
1035 struct scsi_sense_disect sinfo
;
1039 memset(&io_hdr
, 0, sizeof(io_hdr
));
1040 memset(cdb
, 0, sizeof(cdb
));
1041 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1042 io_hdr
.dxfer_len
= bufLen
;
1043 io_hdr
.dxferp
= pBuf
;
1044 cdb
[0] = READ_DEFECT_10
;
1045 cdb
[2] = (unsigned char)(((req_plist
<< 4) & 0x10) |
1046 ((req_glist
<< 3) & 0x8) | (dl_format
& 0x7));
1047 cdb
[7] = (bufLen
>> 8) & 0xff;
1048 cdb
[8] = bufLen
& 0xff;
1050 io_hdr
.cmnd_len
= sizeof(cdb
);
1051 io_hdr
.sensep
= sense
;
1052 io_hdr
.max_sense_len
= sizeof(sense
);
1053 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1055 if (!device
->scsi_pass_through(&io_hdr
))
1056 return -device
->get_errno();
1057 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1058 /* Look for "(Primary|Grown) defect list not found" */
1059 if ((sinfo
.resp_code
>= 0x70) && (0x1c == sinfo
.asc
))
1061 return scsiSimpleSenseFilter(&sinfo
);
1064 /* READ DEFECT (12) command. Returns 0 if ok, 1 if NOT READY, 2 if
1065 * command not supported, 3 if field in command not supported, 101 if
1066 * defect list not found (e.g. SSD may not have defect list) or returns
1067 * negated errno. SBC-3 section 5.18 (rev 35; vale Mark Evans) */
1069 scsiReadDefect12(scsi_device
* device
, int req_plist
, int req_glist
,
1070 int dl_format
, int addrDescIndex
, UINT8
*pBuf
, int bufLen
)
1072 struct scsi_cmnd_io io_hdr
;
1073 struct scsi_sense_disect sinfo
;
1077 memset(&io_hdr
, 0, sizeof(io_hdr
));
1078 memset(cdb
, 0, sizeof(cdb
));
1079 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1080 io_hdr
.dxfer_len
= bufLen
;
1081 io_hdr
.dxferp
= pBuf
;
1082 cdb
[0] = READ_DEFECT_12
;
1083 cdb
[1] = (unsigned char)(((req_plist
<< 4) & 0x10) |
1084 ((req_glist
<< 3) & 0x8) | (dl_format
& 0x7));
1085 cdb
[2] = (addrDescIndex
>> 24) & 0xff;
1086 cdb
[3] = (addrDescIndex
>> 16) & 0xff;
1087 cdb
[4] = (addrDescIndex
>> 8) & 0xff;
1088 cdb
[5] = addrDescIndex
& 0xff;
1089 cdb
[6] = (bufLen
>> 24) & 0xff;
1090 cdb
[7] = (bufLen
>> 16) & 0xff;
1091 cdb
[8] = (bufLen
>> 8) & 0xff;
1092 cdb
[9] = bufLen
& 0xff;
1094 io_hdr
.cmnd_len
= sizeof(cdb
);
1095 io_hdr
.sensep
= sense
;
1096 io_hdr
.max_sense_len
= sizeof(sense
);
1097 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1099 if (!device
->scsi_pass_through(&io_hdr
))
1100 return -device
->get_errno();
1101 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1102 /* Look for "(Primary|Grown) defect list not found" */
1103 if ((sinfo
.resp_code
>= 0x70) && (0x1c == sinfo
.asc
))
1105 return scsiSimpleSenseFilter(&sinfo
);
1108 /* READ CAPACITY (10) command. Returns 0 if ok, 1 if NOT READY, 2 if
1109 * command not supported, 3 if field in command not supported or returns
1110 * negated errno. SBC-3 section 5.15 (rev 26) */
1112 scsiReadCapacity10(scsi_device
* device
, unsigned int * last_lbap
,
1113 unsigned int * lb_sizep
)
1116 struct scsi_cmnd_io io_hdr
;
1117 struct scsi_sense_disect sinfo
;
1122 memset(&io_hdr
, 0, sizeof(io_hdr
));
1123 memset(cdb
, 0, sizeof(cdb
));
1124 memset(resp
, 0, sizeof(resp
));
1125 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1126 io_hdr
.dxfer_len
= sizeof(resp
);
1127 io_hdr
.dxferp
= resp
;
1128 cdb
[0] = READ_CAPACITY_10
;
1130 io_hdr
.cmnd_len
= sizeof(cdb
);
1131 io_hdr
.sensep
= sense
;
1132 io_hdr
.max_sense_len
= sizeof(sense
);
1133 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1135 if (!device
->scsi_pass_through(&io_hdr
))
1136 return -device
->get_errno();
1137 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1138 res
= scsiSimpleSenseFilter(&sinfo
);
1142 *last_lbap
= (resp
[0] << 24) | (resp
[1] << 16) | (resp
[2] << 8) |
1145 *lb_sizep
= (resp
[4] << 24) | (resp
[5] << 16) | (resp
[6] << 8) |
1150 /* READ CAPACITY (16) command. The bufLen argument should be 32. Returns 0
1151 * if ok, 1 if NOT READY, 2 if command not supported, 3 if field in command
1152 * not supported or returns negated errno. SBC-3 section 5.16 (rev 26) */
1154 scsiReadCapacity16(scsi_device
* device
, UINT8
*pBuf
, int bufLen
)
1156 struct scsi_cmnd_io io_hdr
;
1157 struct scsi_sense_disect sinfo
;
1161 memset(&io_hdr
, 0, sizeof(io_hdr
));
1162 memset(cdb
, 0, sizeof(cdb
));
1163 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1164 io_hdr
.dxfer_len
= bufLen
;
1165 io_hdr
.dxferp
= pBuf
;
1166 cdb
[0] = READ_CAPACITY_16
;
1167 cdb
[1] = SAI_READ_CAPACITY_16
;
1168 cdb
[10] = (bufLen
>> 24) & 0xff;
1169 cdb
[11] = (bufLen
>> 16) & 0xff;
1170 cdb
[12] = (bufLen
>> 8) & 0xff;
1171 cdb
[13] = bufLen
& 0xff;
1173 io_hdr
.cmnd_len
= sizeof(cdb
);
1174 io_hdr
.sensep
= sense
;
1175 io_hdr
.max_sense_len
= sizeof(sense
);
1176 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
1178 if (!device
->scsi_pass_through(&io_hdr
))
1179 return -device
->get_errno();
1180 scsi_do_sense_disect(&io_hdr
, &sinfo
);
1181 return scsiSimpleSenseFilter(&sinfo
);
1184 /* Return number of bytes of storage in 'device' or 0 if error. If
1185 * successful and lb_sizep is not NULL then the logical block size
1186 * in bytes is written to the location pointed to by lb_sizep. */
1188 scsiGetSize(scsi_device
* device
, unsigned int * lb_sizep
,
1189 int * lb_per_pb_expp
)
1191 unsigned int last_lba
= 0, lb_size
= 0;
1193 uint64_t ret_val
= 0;
1196 res
= scsiReadCapacity10(device
, &last_lba
, &lb_size
);
1199 pout("scsiGetSize: READ CAPACITY(10) failed, res=%d\n", res
);
1202 if (0xffffffff == last_lba
) {
1203 res
= scsiReadCapacity16(device
, rc16resp
, sizeof(rc16resp
));
1206 pout("scsiGetSize: READ CAPACITY(16) failed, res=%d\n", res
);
1209 for (int k
= 0; k
< 8; ++k
) {
1212 ret_val
|= rc16resp
[k
+ 0];
1215 *lb_per_pb_expp
= (rc16resp
[13] & 0xf);
1219 *lb_per_pb_expp
= 0;
1222 *lb_sizep
= lb_size
;
1223 ++ret_val
; /* last_lba is origin 0 so need to bump to get number of */
1224 return ret_val
* lb_size
;
1227 /* Gets drive Protection and Logical/Physical block information. Writes
1228 * back bytes 12 to 31 from a READ CAPACITY 16 command to the rc16_12_31p
1229 * pointer. So rc16_12_31p should point to an array of 20 bytes. Returns 0
1230 * if ok, 1 if NOT READY, 2 if command not supported, 3 if field in command
1231 * not supported or returns negated errno. */
1233 scsiGetProtPBInfo(scsi_device
* device
, unsigned char * rc16_12_31p
)
1238 res
= scsiReadCapacity16(device
, rc16resp
, sizeof(rc16resp
));
1241 pout("scsiGetSize: READ CAPACITY(16) failed, res=%d\n", res
);
1245 memcpy(rc16_12_31p
, rc16resp
+ 12, 20);
1249 /* Offset into mode sense (6 or 10 byte) response that actual mode page
1250 * starts at (relative to resp[0]). Returns -1 if problem */
1252 scsiModePageOffset(const UINT8
* resp
, int len
, int modese_len
)
1257 int resp_len
, bd_len
;
1258 if (10 == modese_len
) {
1259 resp_len
= (resp
[0] << 8) + resp
[1] + 2;
1260 bd_len
= (resp
[6] << 8) + resp
[7];
1261 offset
= bd_len
+ 8;
1263 resp_len
= resp
[0] + 1;
1265 offset
= bd_len
+ 4;
1267 if ((offset
+ 2) > len
) {
1268 pout("scsiModePageOffset: raw_curr too small, offset=%d "
1269 "resp_len=%d bd_len=%d\n", offset
, resp_len
, bd_len
);
1271 } else if ((offset
+ 2) > resp_len
) {
1272 if ((resp_len
> 2) || scsi_debugmode
)
1273 pout("scsiModePageOffset: response length too short, "
1274 "resp_len=%d offset=%d bd_len=%d\n", resp_len
,
1282 /* IEC mode page byte 2 bit masks */
1283 #define DEXCPT_ENABLE 0x08
1284 #define EWASC_ENABLE 0x10
1285 #define DEXCPT_DISABLE 0xf7
1286 #define EWASC_DISABLE 0xef
1287 #define TEST_DISABLE 0xfb
1289 /* Fetches the Informational Exceptions Control mode page. First tries
1290 * the 6 byte MODE SENSE command and if that fails with an illegal opcode
1291 * tries a 10 byte MODE SENSE command. Returns 0 if successful, a positive
1292 * number if a known error (see SIMPLE_ERR_ ...) or a negative errno
1295 scsiFetchIECmpage(scsi_device
* device
, struct scsi_iec_mode_page
*iecp
,
1300 memset(iecp
, 0, sizeof(*iecp
));
1301 iecp
->modese_len
= modese_len
;
1302 iecp
->requestedCurrent
= 1;
1303 if (iecp
->modese_len
<= 6) {
1304 if ((err
= scsiModeSense(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1305 0, MPAGE_CONTROL_CURRENT
,
1306 iecp
->raw_curr
, sizeof(iecp
->raw_curr
)))) {
1307 if (SIMPLE_ERR_BAD_OPCODE
== err
)
1308 iecp
->modese_len
= 10;
1310 iecp
->modese_len
= 0;
1313 } else if (0 == iecp
->modese_len
)
1314 iecp
->modese_len
= 6;
1316 if (10 == iecp
->modese_len
) {
1317 err
= scsiModeSense10(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1318 0, MPAGE_CONTROL_CURRENT
,
1319 iecp
->raw_curr
, sizeof(iecp
->raw_curr
));
1321 iecp
->modese_len
= 0;
1325 iecp
->gotCurrent
= 1;
1326 iecp
->requestedChangeable
= 1;
1327 if (10 == iecp
->modese_len
)
1328 err
= scsiModeSense10(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1329 0, MPAGE_CONTROL_CHANGEABLE
,
1330 iecp
->raw_chg
, sizeof(iecp
->raw_chg
));
1331 else if (6 == iecp
->modese_len
)
1332 err
= scsiModeSense(device
, INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE
,
1333 0, MPAGE_CONTROL_CHANGEABLE
,
1334 iecp
->raw_chg
, sizeof(iecp
->raw_chg
));
1337 iecp
->gotChangeable
= 1;
1342 scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page
*iecp
)
1344 if (iecp
&& iecp
->gotCurrent
) {
1345 int offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
1348 return (iecp
->raw_curr
[offset
+ 2] & DEXCPT_ENABLE
) ? 0 : 1;
1356 scsi_IsWarningEnabled(const struct scsi_iec_mode_page
*iecp
)
1358 if (iecp
&& iecp
->gotCurrent
) {
1359 int offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
1362 return (iecp
->raw_curr
[offset
+ 2] & EWASC_ENABLE
) ? 1 : 0;
1369 /* set EWASC and clear PERF, EBF, DEXCPT TEST and LOGERR */
1370 #define SCSI_IEC_MP_BYTE2_ENABLED 0x10
1371 #define SCSI_IEC_MP_BYTE2_TEST_MASK 0x4
1372 /* exception/warning via an unrequested REQUEST SENSE command */
1373 #define SCSI_IEC_MP_MRIE 6
1374 #define SCSI_IEC_MP_INTERVAL_T 0
1375 #define SCSI_IEC_MP_REPORT_COUNT 1
1377 /* Try to set (or clear) both Exception Control and Warning in the IE
1378 * mode page subject to the "changeable" mask. The object pointed to
1379 * by iecp is (possibly) inaccurate after this call, therefore
1380 * scsiFetchIECmpage() should be called again if the IEC mode page
1381 * is to be re-examined.
1382 * When -r ioctl is invoked 3 or more time on 'smartctl -s on ...'
1383 * then set the TEST bit (causes asc,ascq pair of 0x5d,0xff). */
1385 scsiSetExceptionControlAndWarning(scsi_device
* device
, int enabled
,
1386 const struct scsi_iec_mode_page
*iecp
)
1388 int offset
, resp_len
;
1390 UINT8 rout
[SCSI_IECMP_RAW_LEN
];
1392 if ((! iecp
) || (! iecp
->gotCurrent
))
1394 offset
= scsiModePageOffset(iecp
->raw_curr
, sizeof(iecp
->raw_curr
),
1398 memcpy(rout
, iecp
->raw_curr
, SCSI_IECMP_RAW_LEN
);
1399 /* mask out DPOFUA device specific (disk) parameter bit */
1400 if (10 == iecp
->modese_len
) {
1401 resp_len
= (rout
[0] << 8) + rout
[1] + 2;
1404 resp_len
= rout
[0] + 1;
1407 int sp
= (rout
[offset
] & 0x80) ? 1 : 0; /* PS bit becomes 'SELECT's SP bit */
1409 rout
[offset
+ 2] = SCSI_IEC_MP_BYTE2_ENABLED
;
1410 if (scsi_debugmode
> 2)
1411 rout
[offset
+ 2] |= SCSI_IEC_MP_BYTE2_TEST_MASK
;
1412 rout
[offset
+ 3] = SCSI_IEC_MP_MRIE
;
1413 rout
[offset
+ 4] = (SCSI_IEC_MP_INTERVAL_T
>> 24) & 0xff;
1414 rout
[offset
+ 5] = (SCSI_IEC_MP_INTERVAL_T
>> 16) & 0xff;
1415 rout
[offset
+ 6] = (SCSI_IEC_MP_INTERVAL_T
>> 8) & 0xff;
1416 rout
[offset
+ 7] = SCSI_IEC_MP_INTERVAL_T
& 0xff;
1417 rout
[offset
+ 8] = (SCSI_IEC_MP_REPORT_COUNT
>> 24) & 0xff;
1418 rout
[offset
+ 9] = (SCSI_IEC_MP_REPORT_COUNT
>> 16) & 0xff;
1419 rout
[offset
+ 10] = (SCSI_IEC_MP_REPORT_COUNT
>> 8) & 0xff;
1420 rout
[offset
+ 11] = SCSI_IEC_MP_REPORT_COUNT
& 0xff;
1421 if (iecp
->gotChangeable
) {
1422 UINT8 chg2
= iecp
->raw_chg
[offset
+ 2];
1424 rout
[offset
+ 2] = chg2
? (rout
[offset
+ 2] & chg2
) :
1425 iecp
->raw_curr
[offset
+ 2];
1426 for (int k
= 3; k
< 12; ++k
) {
1427 if (0 == iecp
->raw_chg
[offset
+ k
])
1428 rout
[offset
+ k
] = iecp
->raw_curr
[offset
+ k
];
1431 if (0 == memcmp(&rout
[offset
+ 2], &iecp
->raw_chg
[offset
+ 2], 10)) {
1432 if (scsi_debugmode
> 0)
1433 pout("scsiSetExceptionControlAndWarning: already enabled\n");
1436 } else { /* disabling Exception Control and (temperature) Warnings */
1437 int eCEnabled
= (rout
[offset
+ 2] & DEXCPT_ENABLE
) ? 0 : 1;
1438 int wEnabled
= (rout
[offset
+ 2] & EWASC_ENABLE
) ? 1 : 0;
1439 if ((! eCEnabled
) && (! wEnabled
)) {
1440 if (scsi_debugmode
> 0)
1441 pout("scsiSetExceptionControlAndWarning: already disabled\n");
1442 return 0; /* nothing to do, leave other setting alone */
1445 rout
[offset
+ 2] &= EWASC_DISABLE
;
1447 if (iecp
->gotChangeable
&&
1448 (iecp
->raw_chg
[offset
+ 2] & DEXCPT_ENABLE
))
1449 rout
[offset
+ 2] |= DEXCPT_ENABLE
;
1450 rout
[offset
+ 2] &= TEST_DISABLE
;/* clear TEST bit for spec */
1453 if (10 == iecp
->modese_len
)
1454 err
= scsiModeSelect10(device
, sp
, rout
, resp_len
);
1455 else if (6 == iecp
->modese_len
)
1456 err
= scsiModeSelect(device
, sp
, rout
, resp_len
);
1461 scsiGetTemp(scsi_device
* device
, UINT8
*currenttemp
, UINT8
*triptemp
)
1466 memset(tBuf
, 0, sizeof(tBuf
));
1467 if ((err
= scsiLogSense(device
, TEMPERATURE_LPAGE
, 0, tBuf
,
1468 sizeof(tBuf
), 0))) {
1471 pout("Log Sense for temperature failed [%s]\n", scsiErrString(err
));
1474 *currenttemp
= tBuf
[9];
1475 *triptemp
= tBuf
[15];
1479 /* Read informational exception log page or Request Sense response.
1480 * Fetching asc/ascq code potentially flagging an exception or warning.
1481 * Returns 0 if ok, else error number. A current temperature of 255
1482 * (Celsius) implies that the temperature not available. */
1484 scsiCheckIE(scsi_device
* device
, int hasIELogPage
, int hasTempLogPage
,
1485 UINT8
*asc
, UINT8
*ascq
, UINT8
*currenttemp
, UINT8
*triptemp
)
1488 struct scsi_sense_disect sense_info
;
1490 int temperatureSet
= 0;
1491 UINT8 currTemp
, trTemp
;
1497 memset(tBuf
,0,sizeof(tBuf
)); // need to clear stack space of junk
1498 memset(&sense_info
, 0, sizeof(sense_info
));
1500 if ((err
= scsiLogSense(device
, IE_LPAGE
, 0, tBuf
,
1501 sizeof(tBuf
), 0))) {
1502 pout("Log Sense failed, IE page [%s]\n", scsiErrString(err
));
1505 // pull out page size from response, don't forget to add 4
1506 unsigned short pagesize
= (unsigned short) ((tBuf
[2] << 8) | tBuf
[3]) + 4;
1507 if ((pagesize
< 4) || tBuf
[4] || tBuf
[5]) {
1508 pout("Log Sense failed, IE page, bad parameter code or length\n");
1509 return SIMPLE_ERR_BAD_PARAM
;
1512 sense_info
.asc
= tBuf
[8];
1513 sense_info
.ascq
= tBuf
[9];
1514 if (! hasTempLogPage
) {
1516 *currenttemp
= tBuf
[10];
1517 if (tBuf
[7] > 3) /* IBM extension in SMART (IE) lpage */
1518 *triptemp
= tBuf
[11];
1522 if (0 == sense_info
.asc
) {
1523 /* ties in with MRIE field of 6 in IEC mode page (0x1c) */
1524 if ((err
= scsiRequestSense(device
, &sense_info
))) {
1525 pout("Request Sense failed, [%s]\n", scsiErrString(err
));
1529 *asc
= sense_info
.asc
;
1530 *ascq
= sense_info
.ascq
;
1531 if ((! temperatureSet
) && hasTempLogPage
) {
1532 if (0 == scsiGetTemp(device
, &currTemp
, &trTemp
)) {
1533 *currenttemp
= currTemp
;
1540 // The first character (W, C, I) tells the severity
1541 static const char * TapeAlertsMessageTable
[]= {
1544 "W: The tape drive is having problems reading data. No data has been lost,\n"
1545 " but there has been a reduction in the performance of the tape.",
1547 "W: The tape drive is having problems writing data. No data has been lost,\n"
1548 " but there has been a reduction in the capacity of the tape.",
1550 "W: The operation has stopped because an error has occurred while reading\n"
1551 " or writing data that the drive cannot correct.",
1553 "C: Your data is at risk:\n"
1554 " 1. Copy any data you require from this tape. \n"
1555 " 2. Do not use this tape again.\n"
1556 " 3. Restart the operation with a different tape.",
1558 "C: The tape is damaged or the drive is faulty. Call the tape drive\n"
1559 " supplier helpline.",
1561 "C: The tape is from a faulty batch or the tape drive is faulty:\n"
1562 " 1. Use a good tape to test the drive.\n"
1563 " 2. If problem persists, call the tape drive supplier helpline.",
1565 "W: The tape cartridge has reached the end of its calculated useful life:\n"
1566 " 1. Copy data you need to another tape.\n"
1567 " 2. Discard the old tape.",
1569 "W: The tape cartridge is not data-grade. Any data you back up to the tape\n"
1570 " is at risk. Replace the cartridge with a data-grade tape.",
1572 "C: You are trying to write to a write-protected cartridge. Remove the\n"
1573 " write-protection or use another tape.",
1575 "I: You cannot eject the cartridge because the tape drive is in use. Wait\n"
1576 " until the operation is complete before ejecting the cartridge.",
1578 "I: The tape in the drive is a cleaning cartridge.",
1580 "I: You have tried to load a cartridge of a type which is not supported\n"
1583 "C: The operation has failed because the tape in the drive has experienced\n"
1584 " a mechanical failure:\n"
1585 " 1. Discard the old tape.\n"
1586 " 2. Restart the operation with a different tape.",
1588 "C: The operation has failed because the tape in the drive has experienced\n"
1589 " a mechanical failure:\n"
1590 " 1. Do not attempt to extract the tape cartridge\n"
1591 " 2. Call the tape drive supplier helpline.",
1593 "W: The memory in the tape cartridge has failed, which reduces\n"
1594 " performance. Do not use the cartridge for further write operations.",
1596 "C: The operation has failed because the tape cartridge was manually\n"
1597 " de-mounted while the tape drive was actively writing or reading.",
1599 "W: You have loaded a cartridge of a type that is read-only in this drive.\n"
1600 " The cartridge will appear as write-protected.",
1602 "W: The tape directory on the tape cartridge has been corrupted. File\n"
1603 " search performance will be degraded. The tape directory can be rebuilt\n"
1604 " by reading all the data on the cartridge.",
1606 "I: The tape cartridge is nearing the end of its calculated life. It is\n"
1607 " recommended that you:\n"
1608 " 1. Use another tape cartridge for your next backup.\n"
1609 " 2. Store this tape in a safe place in case you need to restore "
1612 "C: The tape drive needs cleaning:\n"
1613 " 1. If the operation has stopped, eject the tape and clean the drive.\n"
1614 " 2. If the operation has not stopped, wait for it to finish and then\n"
1615 " clean the drive.\n"
1616 " Check the tape drive users manual for device specific cleaning instructions.",
1618 "W: The tape drive is due for routine cleaning:\n"
1619 " 1. Wait for the current operation to finish.\n"
1620 " 2. The use a cleaning cartridge.\n"
1621 " Check the tape drive users manual for device specific cleaning instructions.",
1623 "C: The last cleaning cartridge used in the tape drive has worn out:\n"
1624 " 1. Discard the worn out cleaning cartridge.\n"
1625 " 2. Wait for the current operation to finish.\n"
1626 " 3. Then use a new cleaning cartridge.",
1628 "C: The last cleaning cartridge used in the tape drive was an invalid\n"
1630 " 1. Do not use this cleaning cartridge in this drive.\n"
1631 " 2. Wait for the current operation to finish.\n"
1632 " 3. Then use a new cleaning cartridge.",
1634 "W: The tape drive has requested a retention operation",
1636 "W: A redundant interface port on the tape drive has failed",
1638 "W: A tape drive cooling fan has failed",
1640 "W: A redundant power supply has failed inside the tape drive enclosure.\n"
1641 " Check the enclosure users manual for instructions on replacing the\n"
1642 " failed power supply.",
1644 "W: The tape drive power consumption is outside the specified range.",
1646 "W: Preventive maintenance of the tape drive is required. Check the tape\n"
1647 " drive users manual for device specific preventive maintenance\n"
1648 " tasks or call the tape drive supplier helpline.",
1650 "C: The tape drive has a hardware fault:\n"
1651 " 1. Eject the tape or magazine.\n"
1652 " 2. Reset the drive.\n"
1653 " 3. Restart the operation.",
1655 "C: The tape drive has a hardware fault:\n"
1656 " 1. Turn the tape drive off and then on again.\n"
1657 " 2. Restart the operation.\n"
1658 " 3. If the problem persists, call the tape drive supplier helpline.",
1660 "W: The tape drive has a problem with the application client interface:\n"
1661 " 1. Check the cables and cable connections.\n"
1662 " 2. Restart the operation.",
1664 "C: The operation has failed:\n"
1665 " 1. Eject the tape or magazine.\n"
1666 " 2. Insert the tape or magazine again.\n"
1667 " 3. Restart the operation.",
1669 "W: The firmware download has failed because you have tried to use the\n"
1670 " incorrect firmware for this tape drive. Obtain the correct\n"
1671 " firmware and try again.",
1673 "W: Environmental conditions inside the tape drive are outside the\n"
1674 " specified humidity range.",
1676 "W: Environmental conditions inside the tape drive are outside the\n"
1677 " specified temperature range.",
1679 "W: The voltage supply to the tape drive is outside the specified range.",
1681 "C: A hardware failure of the tape drive is predicted. Call the tape\n"
1682 " drive supplier helpline.",
1684 "W: The tape drive may have a hardware fault. Run extended diagnostics to\n"
1685 " verify and diagnose the problem. Check the tape drive users manual for\n"
1686 " device specific instructions on running extended diagnostic tests.",
1688 "C: The changer mechanism is having difficulty communicating with the tape\n"
1690 " 1. Turn the autoloader off then on.\n"
1691 " 2. Restart the operation.\n"
1692 " 3. If problem persists, call the tape drive supplier helpline.",
1694 "C: A tape has been left in the autoloader by a previous hardware fault:\n"
1695 " 1. Insert an empty magazine to clear the fault.\n"
1696 " 2. If the fault does not clear, turn the autoloader off and then\n"
1698 " 3. If the problem persists, call the tape drive supplier helpline.",
1700 "W: There is a problem with the autoloader mechanism.",
1702 "C: The operation has failed because the autoloader door is open:\n"
1703 " 1. Clear any obstructions from the autoloader door.\n"
1704 " 2. Eject the magazine and then insert it again.\n"
1705 " 3. If the fault does not clear, turn the autoloader off and then\n"
1707 " 4. If the problem persists, call the tape drive supplier helpline.",
1709 "C: The autoloader has a hardware fault:\n"
1710 " 1. Turn the autoloader off and then on again.\n"
1711 " 2. Restart the operation.\n"
1712 " 3. If the problem persists, call the tape drive supplier helpline.\n"
1713 " Check the autoloader users manual for device specific instructions\n"
1714 " on turning the device power on and off.",
1716 "C: The autoloader cannot operate without the magazine,\n"
1717 " 1. Insert the magazine into the autoloader.\n"
1718 " 2. Restart the operation.",
1720 "W: A hardware failure of the changer mechanism is predicted. Call the\n"
1721 " tape drive supplier helpline.",
1729 "W: Media statistics have been lost at some time in the past",
1731 "W: The tape directory on the tape cartridge just unloaded has been\n"
1732 " corrupted. File search performance will be degraded. The tape\n"
1733 " directory can be rebuilt by reading all the data.",
1735 "C: The tape just unloaded could not write its system area successfully:\n"
1736 " 1. Copy data to another tape cartridge.\n"
1737 " 2. Discard the old cartridge.",
1739 "C: The tape system are could not be read successfully at load time:\n"
1740 " 1. Copy data to another tape cartridge.\n",
1742 "C: The start or data could not be found on the tape:\n"
1743 " 1. Check you are using the correct format tape.\n"
1744 " 2. Discard the tape or return the tape to your supplier",
1746 "C: The operation has failed because the media cannot be loaded\n"
1748 " 1. Remove the cartridge, inspect it as specified in the product\n"
1749 " manual, and retry the operation.\n"
1750 " 2. If the problem persists, call the tape drive supplier help line.",
1752 "C: The operation has failed because the medium cannot be unloaded:\n"
1753 " 1. Do not attempt to extract the tape cartridge.\n"
1754 " 2. Call the tape driver supplier help line.",
1756 "C: The tape drive has a problem with the automation interface:\n"
1757 " 1. Check the power to the automation system.\n"
1758 " 2. Check the cables and cable connections.\n"
1759 " 3. Call the supplier help line if problem persists.",
1761 "W: The tape drive has reset itself due to a detected firmware\n"
1762 " fault. If problem persists, call the supplier help line.",
1766 scsiTapeAlertsTapeDevice(unsigned short code
)
1768 const int num
= sizeof(TapeAlertsMessageTable
) /
1769 sizeof(TapeAlertsMessageTable
[0]);
1771 return (code
< num
) ? TapeAlertsMessageTable
[code
] : "Unknown Alert";
1774 // The first character (W, C, I) tells the severity
1775 static const char * ChangerTapeAlertsMessageTable
[]= {
1778 "C: The library mechanism is having difficulty communicating with the\n"
1780 " 1. Turn the library off then on.\n"
1781 " 2. Restart the operation.\n"
1782 " 3. If the problem persists, call the library supplier help line.",
1784 "W: There is a problem with the library mechanism. If problem persists,\n"
1785 " call the library supplier help line.",
1787 "C: The library has a hardware fault:\n"
1788 " 1. Reset the library.\n"
1789 " 2. Restart the operation.\n"
1790 " Check the library users manual for device specific instructions on resetting\n"
1793 "C: The library has a hardware fault:\n"
1794 " 1. Turn the library off then on again.\n"
1795 " 2. Restart the operation.\n"
1796 " 3. If the problem persists, call the library supplier help line.\n"
1797 " Check the library users manual for device specific instructions on turning the\n"
1798 " device power on and off.",
1800 "W: The library mechanism may have a hardware fault.\n"
1801 " Run extended diagnostics to verify and diagnose the problem. Check the library\n"
1802 " users manual for device specific instructions on running extended diagnostic\n"
1805 "C: The library has a problem with the host interface:\n"
1806 " 1. Check the cables and connections.\n"
1807 " 2. Restart the operation.",
1809 "W: A hardware failure of the library is predicted. Call the library\n"
1810 " supplier help line.",
1812 "W: Preventive maintenance of the library is required.\n"
1813 " Check the library users manual for device specific preventative maintenance\n"
1814 " tasks, or call your library supplier help line.",
1816 "C: General environmental conditions inside the library are outside the\n"
1817 " specified humidity range.",
1819 "C: General environmental conditions inside the library are outside the\n"
1820 " specified temperature range.",
1822 "C: The voltage supply to the library is outside the specified range.\n"
1823 " There is a potential problem with the power supply or failure of\n"
1824 " a redundant power supply.",
1826 "C: A cartridge has been left inside the library by a previous hardware\n"
1828 " 1. Insert an empty magazine to clear the fault.\n"
1829 " 2. If the fault does not clear, turn the library off and then on again.\n"
1830 " 3. If the problem persists, call the library supplier help line.",
1832 "W: There is a potential problem with the drive ejecting cartridges or\n"
1833 " with the library mechanism picking a cartridge from a slot.\n"
1834 " 1. No action needs to be taken at this time.\n"
1835 " 2. If the problem persists, call the library supplier help line.",
1837 "W: There is a potential problem with the library mechanism placing a\n"
1838 " cartridge into a slot.\n"
1839 " 1. No action needs to be taken at this time.\n"
1840 " 2. If the problem persists, call the library supplier help line.",
1842 "W: There is a potential problem with the drive or the library mechanism\n"
1843 " loading cartridges, or an incompatible cartridge.",
1845 "C: The library has failed because the door is open:\n"
1846 " 1. Clear any obstructions from the library door.\n"
1847 " 2. Close the library door.\n"
1848 " 3. If the problem persists, call the library supplier help line.",
1850 "C: There is a mechanical problem with the library media import/export\n"
1853 "C: The library cannot operate without the magazine.\n"
1854 " 1. Insert the magazine into the library.\n"
1855 " 2. Restart the operation.",
1857 "W: Library security has been compromised.",
1859 "I: The library security mode has been changed.\n"
1860 " The library has either been put into secure mode, or the library has exited\n"
1861 " the secure mode.\n"
1862 " This is for information purposes only. No action is required.",
1864 "I: The library has been manually turned offline and is unavailable for use.",
1866 "I: A drive inside the library has been taken offline.\n"
1867 " This is for information purposes only. No action is required.",
1869 "W: There is a potential problem with the bar code label or the scanner\n"
1870 " hardware in the library mechanism.\n"
1871 " 1. No action needs to be taken at this time.\n"
1872 " 2. If the problem persists, call the library supplier help line.",
1874 "C: The library has detected an inconsistency in its inventory.\n"
1875 " 1. Redo the library inventory to correct inconsistency.\n"
1876 " 2. Restart the operation.\n"
1877 " Check the applications users manual or the hardware users manual for\n"
1878 " specific instructions on redoing the library inventory.",
1880 "W: A library operation has been attempted that is invalid at this time.",
1882 "W: A redundant interface port on the library has failed.",
1884 "W: A library cooling fan has failed.",
1886 "W: A redundant power supply has failed inside the library. Check the\n"
1887 " library users manual for instructions on replacing the failed power supply.",
1889 "W: The library power consumption is outside the specified range.",
1891 "C: A failure has occurred in the cartridge pass-through mechanism between\n"
1892 " two library modules.",
1894 "C: A cartridge has been left in the pass-through mechanism from a\n"
1895 " previous hardware fault. Check the library users guide for instructions on\n"
1896 " clearing this fault.",
1898 "I: The library was unable to read the bar code on a cartridge.",
1902 scsiTapeAlertsChangerDevice(unsigned short code
)
1904 const int num
= sizeof(ChangerTapeAlertsMessageTable
) /
1905 sizeof(ChangerTapeAlertsMessageTable
[0]);
1907 return (code
< num
) ? ChangerTapeAlertsMessageTable
[code
] : "Unknown Alert";
1911 /* this is a subset of the SCSI additional sense code strings indexed
1912 * by "ascq" for the case when asc==SCSI_ASC_IMPENDING_FAILURE (0x5d)
1914 static const char * strs_for_asc_5d
[] = {
1915 /* 0x00 */ "FAILURE PREDICTION THRESHOLD EXCEEDED",
1916 "MEDIA FAILURE PREDICTION THRESHOLD EXCEEDED",
1917 "LOGICAL UNIT FAILURE PREDICTION THRESHOLD EXCEEDED",
1918 "SPARE AREA EXHAUSTION PREDICTION THRESHOLD EXCEEDED",
1931 /* 0x10 */ "HARDWARE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1932 "HARDWARE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1933 "HARDWARE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1934 "HARDWARE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1935 "HARDWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1936 "HARDWARE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1937 "HARDWARE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1938 "HARDWARE IMPENDING FAILURE CHANNEL PARAMETRICS",
1939 "HARDWARE IMPENDING FAILURE CONTROLLER DETECTED",
1940 "HARDWARE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1941 "HARDWARE IMPENDING FAILURE SEEK TIME PERFORMANCE",
1942 "HARDWARE IMPENDING FAILURE SPIN-UP RETRY COUNT",
1943 "HARDWARE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1947 /* 0x20 */ "CONTROLLER IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1948 "CONTROLLER IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1949 "CONTROLLER IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1950 "CONTROLLER IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1951 "CONTROLLER IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1952 "CONTROLLER IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1953 "CONTROLLER IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1954 "CONTROLLER IMPENDING FAILURE CHANNEL PARAMETRICS",
1955 "CONTROLLER IMPENDING FAILURE CONTROLLER DETECTED",
1956 "CONTROLLER IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1957 "CONTROLLER IMPENDING FAILURE SEEK TIME PERFORMANCE",
1958 "CONTROLLER IMPENDING FAILURE SPIN-UP RETRY COUNT",
1959 "CONTROLLER IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1963 /* 0x30 */ "DATA CHANNEL IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1964 "DATA CHANNEL IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1965 "DATA CHANNEL IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1966 "DATA CHANNEL IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1967 "DATA CHANNEL IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1968 "DATA CHANNEL IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1969 "DATA CHANNEL IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1970 "DATA CHANNEL IMPENDING FAILURE CHANNEL PARAMETRICS",
1971 "DATA CHANNEL IMPENDING FAILURE CONTROLLER DETECTED",
1972 "DATA CHANNEL IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1973 "DATA CHANNEL IMPENDING FAILURE SEEK TIME PERFORMANCE",
1974 "DATA CHANNEL IMPENDING FAILURE SPIN-UP RETRY COUNT",
1975 "DATA CHANNEL IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1979 /* 0x40 */ "SERVO IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1980 "SERVO IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1981 "SERVO IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1982 "SERVO IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1983 "SERVO IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
1984 "SERVO IMPENDING FAILURE ACCESS TIMES TOO HIGH",
1985 "SERVO IMPENDING FAILURE START UNIT TIMES TOO HIGH",
1986 "SERVO IMPENDING FAILURE CHANNEL PARAMETRICS",
1987 "SERVO IMPENDING FAILURE CONTROLLER DETECTED",
1988 "SERVO IMPENDING FAILURE THROUGHPUT PERFORMANCE",
1989 "SERVO IMPENDING FAILURE SEEK TIME PERFORMANCE",
1990 "SERVO IMPENDING FAILURE SPIN-UP RETRY COUNT",
1991 "SERVO IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
1995 /* 0x50 */ "SPINDLE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
1996 "SPINDLE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
1997 "SPINDLE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
1998 "SPINDLE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
1999 "SPINDLE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
2000 "SPINDLE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
2001 "SPINDLE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
2002 "SPINDLE IMPENDING FAILURE CHANNEL PARAMETRICS",
2003 "SPINDLE IMPENDING FAILURE CONTROLLER DETECTED",
2004 "SPINDLE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
2005 "SPINDLE IMPENDING FAILURE SEEK TIME PERFORMANCE",
2006 "SPINDLE IMPENDING FAILURE SPIN-UP RETRY COUNT",
2007 "SPINDLE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT",
2011 /* 0x60 */ "FIRMWARE IMPENDING FAILURE GENERAL HARD DRIVE FAILURE",
2012 "FIRMWARE IMPENDING FAILURE DRIVE ERROR RATE TOO HIGH",
2013 "FIRMWARE IMPENDING FAILURE DATA ERROR RATE TOO HIGH",
2014 "FIRMWARE IMPENDING FAILURE SEEK ERROR RATE TOO HIGH",
2015 "FIRMWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS",
2016 "FIRMWARE IMPENDING FAILURE ACCESS TIMES TOO HIGH",
2017 "FIRMWARE IMPENDING FAILURE START UNIT TIMES TOO HIGH",
2018 "FIRMWARE IMPENDING FAILURE CHANNEL PARAMETRICS",
2019 "FIRMWARE IMPENDING FAILURE CONTROLLER DETECTED",
2020 "FIRMWARE IMPENDING FAILURE THROUGHPUT PERFORMANCE",
2021 "FIRMWARE IMPENDING FAILURE SEEK TIME PERFORMANCE",
2022 "FIRMWARE IMPENDING FAILURE SPIN-UP RETRY COUNT",
2023 /* 0x6c */ "FIRMWARE IMPENDING FAILURE DRIVE CALIBRATION RETRY COUNT"};
2026 /* this is a subset of the SCSI additional sense code strings indexed
2027 * * by "ascq" for the case when asc==SCSI_ASC_WARNING (0xb)
2029 static const char * strs_for_asc_b
[] = {
2030 /* 0x00 */ "WARNING",
2031 "WARNING - SPECIFIED TEMPERATURE EXCEEDED",
2032 "WARNING - ENCLOSURE DEGRADED"};
2034 static char spare_buff
[128];
2037 scsiGetIEString(UINT8 asc
, UINT8 ascq
)
2041 if (SCSI_ASC_IMPENDING_FAILURE
== asc
) {
2043 return "FAILURE PREDICTION THRESHOLD EXCEEDED (FALSE)";
2045 (sizeof(strs_for_asc_5d
) / sizeof(strs_for_asc_5d
[0]))) {
2046 rp
= strs_for_asc_5d
[ascq
];
2050 snprintf(spare_buff
, sizeof(spare_buff
),
2051 "FAILURE PREDICTION THRESHOLD EXCEEDED: ascq=0x%x", ascq
);
2053 } else if (SCSI_ASC_WARNING
== asc
) {
2054 if (ascq
< (sizeof(strs_for_asc_b
) / sizeof(strs_for_asc_b
[0]))) {
2055 rp
= strs_for_asc_b
[ascq
];
2059 snprintf(spare_buff
, sizeof(spare_buff
), "WARNING: ascq=0x%x", ascq
);
2062 return NULL
; /* not a IE additional sense code */
2067 scsiSmartDefaultSelfTest(scsi_device
* device
)
2071 res
= scsiSendDiagnostic(device
, SCSI_DIAG_DEF_SELF_TEST
, NULL
, 0);
2073 pout("Default self test failed [%s]\n", scsiErrString(res
));
2078 scsiSmartShortSelfTest(scsi_device
* device
)
2082 res
= scsiSendDiagnostic(device
, SCSI_DIAG_BG_SHORT_SELF_TEST
, NULL
, 0);
2084 pout("Short offline self test failed [%s]\n", scsiErrString(res
));
2089 scsiSmartExtendSelfTest(scsi_device
* device
)
2093 res
= scsiSendDiagnostic(device
, SCSI_DIAG_BG_EXTENDED_SELF_TEST
, NULL
, 0);
2095 pout("Long (extended) offline self test failed [%s]\n",
2096 scsiErrString(res
));
2101 scsiSmartShortCapSelfTest(scsi_device
* device
)
2105 res
= scsiSendDiagnostic(device
, SCSI_DIAG_FG_SHORT_SELF_TEST
, NULL
, 0);
2107 pout("Short foreground self test failed [%s]\n", scsiErrString(res
));
2112 scsiSmartExtendCapSelfTest(scsi_device
* device
)
2116 res
= scsiSendDiagnostic(device
, SCSI_DIAG_FG_EXTENDED_SELF_TEST
, NULL
, 0);
2118 pout("Long (extended) foreground self test failed [%s]\n",
2119 scsiErrString(res
));
2124 scsiSmartSelfTestAbort(scsi_device
* device
)
2128 res
= scsiSendDiagnostic(device
, SCSI_DIAG_ABORT_SELF_TEST
, NULL
, 0);
2130 pout("Abort self test failed [%s]\n", scsiErrString(res
));
2134 /* Returns 0 and the expected duration of an extended self test (in seconds)
2135 if successful; any other return value indicates a failure. */
2137 scsiFetchExtendedSelfTestTime(scsi_device
* device
, int * durationSec
,
2143 memset(buff
, 0, sizeof(buff
));
2144 if (modese_len
<= 6) {
2145 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2146 MPAGE_CONTROL_CURRENT
,
2147 buff
, sizeof(buff
)))) {
2148 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2152 } else if (0 == modese_len
)
2155 if (10 == modese_len
) {
2156 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2157 MPAGE_CONTROL_CURRENT
,
2158 buff
, sizeof(buff
));
2162 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2165 if (buff
[offset
+ 1] >= 0xa) {
2166 int res
= (buff
[offset
+ 10] << 8) | buff
[offset
+ 11];
2175 scsiDecodeErrCounterPage(unsigned char * resp
, struct scsiErrorCounter
*ecp
)
2177 memset(ecp
, 0, sizeof(*ecp
));
2178 int num
= (resp
[2] << 8) | resp
[3];
2179 unsigned char * ucp
= &resp
[0] + 4;
2181 int pc
= (ucp
[0] << 8) | ucp
[1];
2182 int pl
= ucp
[3] + 4;
2193 ullp
= &ecp
->counter
[pc
];
2196 ecp
->gotExtraPC
= 1;
2197 ullp
= &ecp
->counter
[7];
2201 unsigned char * xp
= ucp
+ 4;
2202 if (k
> (int)sizeof(*ullp
)) {
2203 xp
+= (k
- sizeof(*ullp
));
2207 for (int j
= 0; j
< k
; ++j
) {
2218 scsiDecodeNonMediumErrPage(unsigned char *resp
,
2219 struct scsiNonMediumError
*nmep
)
2221 memset(nmep
, 0, sizeof(*nmep
));
2222 int num
= (resp
[2] << 8) | resp
[3];
2223 unsigned char * ucp
= &resp
[0] + 4;
2224 int szof
= sizeof(nmep
->counterPC0
);
2226 int pc
= (ucp
[0] << 8) | ucp
[1];
2227 int pl
= ucp
[3] + 4;
2239 nmep
->counterPC0
= 0;
2240 for (int j
= 0; j
< k
; ++j
) {
2242 nmep
->counterPC0
<<= 8;
2243 nmep
->counterPC0
|= xp
[j
];
2254 nmep
->counterTFE_H
= 0;
2255 for (int j
= 0; j
< k
; ++j
) {
2257 nmep
->counterTFE_H
<<= 8;
2258 nmep
->counterTFE_H
|= xp
[j
];
2269 nmep
->counterPE_H
= 0;
2270 for (int j
= 0; j
< k
; ++j
) {
2272 nmep
->counterPE_H
<<= 8;
2273 nmep
->counterPE_H
|= xp
[j
];
2277 nmep
->gotExtraPC
= 1;
2285 /* Counts number of failed self-tests. Also encodes the poweron_hour
2286 of the most recent failed self-test. Return value is negative if
2287 this function has a problem (typically -1), otherwise the bottom 8
2288 bits are the number of failed self tests and the 16 bits above that
2289 are the poweron hour of the most recent failure. Note: aborted self
2290 tests (typically by the user) and self tests in progress are not
2291 considered failures. See Working Draft SCSI Primary Commands - 3
2292 (SPC-3) section 7.2.10 T10/1416-D (rev 22a) */
2294 scsiCountFailedSelfTests(scsi_device
* fd
, int noisy
)
2296 int num
, k
, err
, fails
, fail_hour
;
2298 unsigned char resp
[LOG_RESP_SELF_TEST_LEN
];
2300 if ((err
= scsiLogSense(fd
, SELFTEST_RESULTS_LPAGE
, 0, resp
,
2301 LOG_RESP_SELF_TEST_LEN
, 0))) {
2303 pout("scsiCountSelfTests Failed [%s]\n", scsiErrString(err
));
2306 if ((resp
[0] & 0x3f) != SELFTEST_RESULTS_LPAGE
) {
2308 pout("Self-test Log Sense Failed, page mismatch\n");
2311 // compute page length
2312 num
= (resp
[2] << 8) + resp
[3];
2313 // Log sense page length 0x190 bytes
2316 pout("Self-test Log Sense length is 0x%x not 0x190 bytes\n", num
);
2321 // loop through the twenty possible entries
2322 for (k
= 0, ucp
= resp
+ 4; k
< 20; ++k
, ucp
+= 20 ) {
2324 // timestamp in power-on hours (or zero if test in progress)
2325 int n
= (ucp
[6] << 8) | ucp
[7];
2327 // The spec says "all 20 bytes will be zero if no test" but
2328 // DG has found otherwise. So this is a heuristic.
2329 if ((0 == n
) && (0 == ucp
[4]))
2331 int res
= ucp
[4] & 0xf;
2332 if ((res
> 2) && (res
< 8)) {
2335 fail_hour
= (ucp
[6] << 8) + ucp
[7];
2338 return (fail_hour
<< 8) + fails
;
2341 /* Returns 0 if able to read self test log page; then outputs 1 into
2342 *inProgress if self test still in progress, else outputs 0. */
2344 scsiSelfTestInProgress(scsi_device
* fd
, int * inProgress
)
2348 unsigned char resp
[LOG_RESP_SELF_TEST_LEN
];
2350 if (scsiLogSense(fd
, SELFTEST_RESULTS_LPAGE
, 0, resp
,
2351 LOG_RESP_SELF_TEST_LEN
, 0))
2353 if (resp
[0] != SELFTEST_RESULTS_LPAGE
)
2355 // compute page length
2356 num
= (resp
[2] << 8) + resp
[3];
2357 // Log sense page length 0x190 bytes
2363 *inProgress
= (0xf == (ucp
[4] & 0xf)) ? 1 : 0;
2367 /* Returns a negative value if failed to fetch Contol mode page or it was
2368 malformed. Returns 0 if GLTSD bit is zero and returns 1 if the GLTSD
2369 bit is set. Examines default mode page when current==0 else examines
2370 current mode page. */
2372 scsiFetchControlGLTSD(scsi_device
* device
, int modese_len
, int current
)
2376 int pc
= current
? MPAGE_CONTROL_CURRENT
: MPAGE_CONTROL_DEFAULT
;
2378 memset(buff
, 0, sizeof(buff
));
2379 if (modese_len
<= 6) {
2380 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0, pc
,
2381 buff
, sizeof(buff
)))) {
2382 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2386 } else if (0 == modese_len
)
2389 if (10 == modese_len
) {
2390 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0, pc
,
2391 buff
, sizeof(buff
));
2395 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2396 if ((offset
>= 0) && (buff
[offset
+ 1] >= 0xa))
2397 return (buff
[offset
+ 2] & 2) ? 1 : 0;
2401 /* Returns a negative value on error, 0 if unknown and 1 if SSD,
2402 * otherwise the positive returned value is the speed in rpm. First checks
2403 * the Block Device Characteristics VPD page and if that fails it tries the
2404 * RIGID_DISK_DRIVE_GEOMETRY_PAGE mode page. */
2407 scsiGetRPM(scsi_device
* device
, int modese_len
, int * form_factorp
,
2412 int pc
= MPAGE_CONTROL_DEFAULT
;
2414 memset(buff
, 0, sizeof(buff
));
2415 if ((0 == scsiInquiryVpd(device
, SCSI_VPD_BLOCK_DEVICE_CHARACTERISTICS
,
2416 buff
, sizeof(buff
))) &&
2417 (((buff
[2] << 8) + buff
[3]) > 2)) {
2418 int speed
= (buff
[4] << 8) + buff
[5];
2420 *form_factorp
= buff
[7] & 0xf;
2422 *haw_zbcp
= !!(0x10 & buff
[8]);
2429 if (modese_len
<= 6) {
2430 if ((err
= scsiModeSense(device
, RIGID_DISK_DRIVE_GEOMETRY_PAGE
, 0, pc
,
2431 buff
, sizeof(buff
)))) {
2432 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2436 } else if (0 == modese_len
)
2439 if (10 == modese_len
) {
2440 err
= scsiModeSense10(device
, RIGID_DISK_DRIVE_GEOMETRY_PAGE
, 0, pc
,
2441 buff
, sizeof(buff
));
2445 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2446 return (buff
[offset
+ 20] << 8) | buff
[offset
+ 21];
2449 /* Returns a non-zero value in case of error, wcep/rcdp == -1 - get value,
2450 0 - clear bit, 1 - set bit */
2453 scsiGetSetCache(scsi_device
* device
, int modese_len
, short int * wcep
,
2456 int err
, offset
, resp_len
, sp
;
2457 UINT8 buff
[64], ch_buff
[64];
2458 short set_wce
= *wcep
;
2459 short set_rcd
= *rcdp
;
2461 memset(buff
, 0, sizeof(buff
));
2462 if (modese_len
<= 6) {
2463 if ((err
= scsiModeSense(device
, CACHING_PAGE
, 0, MPAGE_CONTROL_CURRENT
,
2464 buff
, sizeof(buff
)))) {
2465 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2468 device
->set_err(EINVAL
, "SCSI MODE SENSE failed");
2471 } else if (0 == modese_len
)
2475 if (10 == modese_len
) {
2476 err
= scsiModeSense10(device
, CACHING_PAGE
, 0, MPAGE_CONTROL_CURRENT
,
2477 buff
, sizeof(buff
));
2479 device
->set_err(EINVAL
, "SCSI MODE SENSE failed");
2483 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2484 if ((offset
< 0) || (buff
[offset
+ 1] < 0xa)) {
2485 device
->set_err(EINVAL
, "Bad response");
2486 return SIMPLE_ERR_BAD_RESP
;
2489 *wcep
= ((buff
[offset
+ 2] & 0x04) != 0);
2490 *rcdp
= ((buff
[offset
+ 2] & 0x01) != 0);
2492 if((*wcep
== set_wce
|| set_wce
== -1)
2493 && ((*rcdp
== set_rcd
) || set_rcd
== -1))
2494 return 0; // no changes needed or nothing to set
2496 if (modese_len
== 6)
2497 err
= scsiModeSense(device
, CACHING_PAGE
, 0,
2498 MPAGE_CONTROL_CHANGEABLE
,
2499 ch_buff
, sizeof(ch_buff
));
2501 err
= scsiModeSense10(device
, CACHING_PAGE
, 0,
2502 MPAGE_CONTROL_CHANGEABLE
,
2503 ch_buff
, sizeof(ch_buff
));
2505 device
->set_err(EINVAL
, "WCE/RCD bits not changable");
2510 if(set_wce
>= 0 && *wcep
!= set_wce
) {
2511 if (0 == (ch_buff
[offset
+ 2] & 0x04)) {
2512 device
->set_err(EINVAL
, "WCE bit not changable");
2516 buff
[offset
+ 2] |= 0x04; // set bit
2518 buff
[offset
+ 2] &= 0xfb; // clear bit
2521 if(set_rcd
>= 0 && *rcdp
!= set_rcd
) {
2522 if (0 == (ch_buff
[offset
+ 2] & 0x01)) {
2523 device
->set_err(EINVAL
, "RCD bit not changable");
2527 buff
[offset
+ 2] |= 0x01; // set bit
2529 buff
[offset
+ 2] &= 0xfe; // clear bit
2532 /* mask out DPOFUA device specific (disk) parameter bit */
2533 if (10 == modese_len
) {
2534 resp_len
= (buff
[0] << 8) + buff
[1] + 2;
2537 resp_len
= buff
[0] + 1;
2540 sp
= 0; /* Do not change saved values */
2541 if (10 == modese_len
)
2542 err
= scsiModeSelect10(device
, sp
, buff
, resp_len
);
2543 else if (6 == modese_len
)
2544 err
= scsiModeSelect(device
, sp
, buff
, resp_len
);
2546 device
->set_err(EINVAL
, "MODE SELECT command failed");
2551 /* Attempts to set or clear GLTSD bit in Control mode page. If enabled is
2552 0 attempts to clear GLTSD otherwise it attempts to set it. Returns 0 if
2553 successful, negative if low level error, > 0 if higher level error (e.g.
2554 SIMPLE_ERR_BAD_PARAM if GLTSD bit is not changeable). */
2556 scsiSetControlGLTSD(scsi_device
* device
, int enabled
, int modese_len
)
2558 int err
, offset
, resp_len
, sp
;
2562 memset(buff
, 0, sizeof(buff
));
2563 if (modese_len
<= 6) {
2564 if ((err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2565 MPAGE_CONTROL_CURRENT
,
2566 buff
, sizeof(buff
)))) {
2567 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2571 } else if (0 == modese_len
)
2574 if (10 == modese_len
) {
2575 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2576 MPAGE_CONTROL_CURRENT
,
2577 buff
, sizeof(buff
));
2581 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2582 if ((offset
< 0) || (buff
[offset
+ 1] < 0xa))
2583 return SIMPLE_ERR_BAD_RESP
;
2587 if (enabled
== (buff
[offset
+ 2] & 2))
2588 return 0; /* GLTSD already in wanted state so nothing to do */
2590 if (modese_len
== 6)
2591 err
= scsiModeSense(device
, CONTROL_MODE_PAGE
, 0,
2592 MPAGE_CONTROL_CHANGEABLE
,
2593 ch_buff
, sizeof(ch_buff
));
2595 err
= scsiModeSense10(device
, CONTROL_MODE_PAGE
, 0,
2596 MPAGE_CONTROL_CHANGEABLE
,
2597 ch_buff
, sizeof(ch_buff
));
2600 if (0 == (ch_buff
[offset
+ 2] & 2))
2601 return SIMPLE_ERR_BAD_PARAM
; /* GLTSD bit not changeable */
2603 /* mask out DPOFUA device specific (disk) parameter bit */
2604 if (10 == modese_len
) {
2605 resp_len
= (buff
[0] << 8) + buff
[1] + 2;
2608 resp_len
= buff
[0] + 1;
2611 sp
= (buff
[offset
] & 0x80) ? 1 : 0; /* PS bit becomes 'SELECT's SP bit */
2613 buff
[offset
+ 2] |= 0x2; /* set GLTSD bit */
2615 buff
[offset
+ 2] &= 0xfd; /* clear GLTSD bit */
2616 if (10 == modese_len
)
2617 err
= scsiModeSelect10(device
, sp
, buff
, resp_len
);
2618 else if (6 == modese_len
)
2619 err
= scsiModeSelect(device
, sp
, buff
, resp_len
);
2623 /* Returns a negative value if failed to fetch Protocol specific port mode
2624 page or it was malformed. Returns transport protocol identifier when
2627 scsiFetchTransportProtocol(scsi_device
* device
, int modese_len
)
2632 memset(buff
, 0, sizeof(buff
));
2633 if (modese_len
<= 6) {
2634 if ((err
= scsiModeSense(device
, PROTOCOL_SPECIFIC_PORT_PAGE
, 0,
2635 MPAGE_CONTROL_CURRENT
,
2636 buff
, sizeof(buff
)))) {
2637 if (SIMPLE_ERR_BAD_OPCODE
== err
)
2641 } else if (0 == modese_len
)
2644 if (10 == modese_len
) {
2645 err
= scsiModeSense10(device
, PROTOCOL_SPECIFIC_PORT_PAGE
, 0,
2646 MPAGE_CONTROL_CURRENT
,
2647 buff
, sizeof(buff
));
2651 offset
= scsiModePageOffset(buff
, sizeof(buff
), modese_len
);
2652 if ((offset
>= 0) && (buff
[offset
+ 1] > 1)) {
2653 if ((0 == (buff
[offset
] & 0x40)) && /* SPF==0 */
2654 (PROTOCOL_SPECIFIC_PORT_PAGE
== (buff
[offset
] & 0x3f)))
2655 return (buff
[offset
+ 2] & 0xf);
2660 const unsigned char *
2661 sg_scsi_sense_desc_find(const unsigned char * sensep
, int sense_len
,
2665 const unsigned char * descp
;
2667 if ((sense_len
< 8) || (0 == (add_sen_len
= sensep
[7])))
2669 if ((sensep
[0] < 0x72) || (sensep
[0] > 0x73))
2671 add_sen_len
= (add_sen_len
< (sense_len
- 8)) ?
2672 add_sen_len
: (sense_len
- 8);
2674 for (int desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
2676 int add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
2677 desc_len
= add_len
+ 2;
2678 if (descp
[0] == desc_type
)
2680 if (add_len
< 0) /* short descriptor ?? */
2686 // Convenience function for formatting strings from SCSI identify
2688 scsi_format_id_string(char * out
, const unsigned char * in
, int n
)
2691 n
= n
> 64 ? 64 : n
;
2692 strncpy(tmp
, (const char *)in
, n
);
2695 // Find the first non-space character (maybe none).
2698 for (i
= 0; tmp
[i
]; i
++)
2699 if (!isspace((int)tmp
[i
])) {
2705 // There are no non-space characters.
2710 // Find the last non-space character.
2711 for (i
= strlen(tmp
)-1; i
>= first
&& isspace((int)tmp
[i
]); i
--);
2714 strncpy(out
, tmp
+first
, last
-first
+1);
2715 out
[last
-first
+1] = '\0';