4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2002-6 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
9 * Additional SCSI work:
10 * Copyright (C) 2003-6 Douglas Gilbert <dougg@torque.net>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * You should have received a copy of the GNU General Public License
18 * (for example COPYING); if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * This code was originally developed as a Senior Thesis by Michael Cornwell
22 * at the Concurrent Systems Laboratory (now part of the Storage Systems
23 * Research Center), Jack Baskin School of Engineering, University of
24 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
26 * N.B. What was formerly known as "SMART" are now called "informational
27 * exceptions" in recent t10.org drafts (i.e. recent SCSI).
35 #define SCSICMDS_H_CVSID "$Id: scsicmds.h,v 1.63 2006/11/12 04:46:32 dpgilbert Exp $\n"
42 /* #define SCSI_DEBUG 1 */ /* Comment out to disable command debugging */
44 /* Following conditional defines bypass inclusion of scsi/scsi.h and
45 * scsi/scsi_ioctl.h . Issue will be resolved later ... */
46 #ifndef TEST_UNIT_READY
47 #define TEST_UNIT_READY 0x0
50 #define LOG_SENSE 0x4d
53 #define MODE_SENSE 0x1a
56 #define MODE_SENSE_10 0x5a
59 #define MODE_SELECT 0x15
61 #ifndef MODE_SELECT_10
62 #define MODE_SELECT_10 0x55
68 #define REQUEST_SENSE 0x03
70 #ifndef RECEIVE_DIAGNOSTIC
71 #define RECEIVE_DIAGNOSTIC 0x1c
73 #ifndef SEND_DIAGNOSTIC
74 #define SEND_DIAGNOSTIC 0x1d
76 #ifndef READ_DEFECT_10
77 #define READ_DEFECT_10 0x37
80 #ifndef SAT_ATA_PASSTHROUGH_12
81 #define SAT_ATA_PASSTHROUGH_12 0xa1
83 #ifndef SAT_ATA_PASSTHROUGH_16
84 #define SAT_ATA_PASSTHROUGH_16 0x85
87 typedef unsigned char UINT8
;
89 typedef unsigned int UINT32
;
93 #define DXFER_FROM_DEVICE 1
94 #define DXFER_TO_DEVICE 2
98 UINT8
* cmnd
; /* [in]: ptr to SCSI command block (cdb) */
99 size_t cmnd_len
; /* [in]: number of bytes in SCSI command */
100 int dxfer_dir
; /* [in]: DXFER_NONE, DXFER_FROM_DEVICE, or
102 UINT8
* dxferp
; /* [in]: ptr to outgoing or incoming data buffer */
103 size_t dxfer_len
; /* [in]: bytes to be transferred to/from dxferp */
104 UINT8
* sensep
; /* [in]: ptr to sense buffer, filled when
105 CHECK CONDITION status occurs */
106 size_t max_sense_len
; /* [in]: max number of bytes to write to sensep */
107 unsigned timeout
; /* [in]: seconds, 0-> default timeout (60 seconds?) */
108 size_t resp_sense_len
; /* [out]: sense buffer length written */
109 UINT8 scsi_status
; /* [out]: 0->ok, 2->CHECK CONDITION, etc ... */
110 int resid
; /* [out]: Number of bytes requested to be transferred
111 less actual number transferred (0 if not
115 struct scsi_sense_disect
{
122 /* Useful data from Informational Exception Control mode page (0x1c) */
123 #define SCSI_IECMP_RAW_LEN 64
124 struct scsi_iec_mode_page
{
125 UINT8 requestedCurrent
;
127 UINT8 requestedChangeable
;
129 UINT8 modese_len
; /* 0 (don't know), 6 or 10 */
130 UINT8 raw_curr
[SCSI_IECMP_RAW_LEN
];
131 UINT8 raw_chg
[SCSI_IECMP_RAW_LEN
];
134 /* Carrier for Error counter log pages (e.g. read, write, verify ...) */
135 struct scsiErrorCounter
{
141 /* Carrier for Non-medium error log page */
142 struct scsiNonMediumError
{
147 uint64_t counterTFE_H
; /* Track following errors [Hitachi] */
149 uint64_t counterPE_H
; /* Positioning errors [Hitachi] */
152 /* SCSI Peripheral types (of interest) */
153 #define SCSI_PT_DIRECT_ACCESS 0x0
154 #define SCSI_PT_SEQUENTIAL_ACCESS 0x1
155 #define SCSI_PT_CDROM 0x5
156 #define SCSI_PT_MEDIUM_CHANGER 0x8
157 #define SCSI_PT_ENCLOSURE 0xd
159 /* ANSI SCSI-3 Log Pages retrieved by LOG SENSE. */
160 #define SUPPORTED_LPAGES 0x00
161 #define BUFFER_OVERRUN_LPAGE 0x01
162 #define WRITE_ERROR_COUNTER_LPAGE 0x02
163 #define READ_ERROR_COUNTER_LPAGE 0x03
164 #define READ_REVERSE_ERROR_COUNTER_LPAGE 0x04
165 #define VERIFY_ERROR_COUNTER_LPAGE 0x05
166 #define NON_MEDIUM_ERROR_LPAGE 0x06
167 #define LAST_N_ERROR_LPAGE 0x07
168 #define FORMAT_STATUS_LPAGE 0x08
169 #define TEMPERATURE_LPAGE 0x0d
170 #define STARTSTOP_CYCLE_COUNTER_LPAGE 0x0e
171 #define APPLICATION_CLIENT_LPAGE 0x0f
172 #define SELFTEST_RESULTS_LPAGE 0x10
173 #define BACKGROUND_RESULTS_LPAGE 0x15 /* SBC-3 */
174 #define IE_LPAGE 0x2f
176 /* Seagate vendor specific log pages. */
177 #define SEAGATE_CACHE_LPAGE 0x37
178 #define SEAGATE_FACTORY_LPAGE 0x3e
180 /* Log page response lengths */
181 #define LOG_RESP_SELF_TEST_LEN 0x194
183 /* See the SSC-2 document at www.t10.org . Earler note: From IBM
184 Documentation, see http://www.storage.ibm.com/techsup/hddtech/prodspecs.htm */
185 #define TAPE_ALERTS_LPAGE 0x2e
187 /* ANSI SCSI-3 Mode Pages */
188 #define VENDOR_UNIQUE_PAGE 0x00
189 #define READ_WRITE_ERROR_RECOVERY_PAGE 0x01
190 #define DISCONNECT_RECONNECT_PAGE 0x02
191 #define FORMAT_DEVICE_PAGE 0x03
192 #define RIGID_DISK_DRIVE_GEOMETRY_PAGE 0x04
193 #define FLEXIBLE_DISK_PAGE 0x05
194 #define VERIFY_ERROR_RECOVERY_PAGE 0x07
195 #define CACHING_PAGE 0x08
196 #define PERIPHERAL_DEVICE_PAGE 0x09
197 #define XOR_CONTROL_MODE_PAGE 0x10
198 #define CONTROL_MODE_PAGE 0x0a
199 #define MEDIUM_TYPES_SUPPORTED_PAGE 0x0b
200 #define NOTCH_PAGE 0x0c
201 #define CD_DEVICE_PAGE 0x0d
202 #define CD_AUDIO_CONTROL_PAGE 0x0e
203 #define DATA_COMPRESSION_PAGE 0x0f
204 #define ENCLOSURE_SERVICES_MANAGEMENT_PAGE 0x14
205 #define PROTOCOL_SPECIFIC_LUN_PAGE 0x18
206 #define PROTOCOL_SPECIFIC_PORT_PAGE 0x19
207 #define POWER_CONDITION_PAGE 0x1a
208 #define INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE 0x1c
209 #define FAULT_FAILURE_REPORTING_PAGE 0x1c
211 /* Background control mode subpage is [0x1c,0x1] */
212 #define BACKGROUND_CONTROL_M_SUBPAGE 0x1 /* SBC-2 */
214 #define ALL_MODE_PAGES 0x3f
216 /* Mode page control field */
217 #define MPAGE_CONTROL_CURRENT 0
218 #define MPAGE_CONTROL_CHANGEABLE 1
219 #define MPAGE_CONTROL_DEFAULT 2
220 #define MPAGE_CONTROL_SAVED 3
222 /* defines for useful SCSI Status codes */
223 #define SCSI_STATUS_CHECK_CONDITION 0x2
225 /* defines for useful Sense Key codes */
226 #define SCSI_SK_NO_SENSE 0x0
227 #define SCSI_SK_RECOVERED_ERR 0x1
228 #define SCSI_SK_NOT_READY 0x2
229 #define SCSI_SK_MEDIUM_ERROR 0x3
230 #define SCSI_SK_HARDWARE_ERROR 0x4
231 #define SCSI_SK_ILLEGAL_REQUEST 0x5
232 #define SCSI_SK_UNIT_ATTENTION 0x6
234 /* defines for useful Additional Sense Codes (ASCs) */
235 #define SCSI_ASC_NOT_READY 0x4 /* more info in ASCQ code */
236 #define SCSI_ASC_NO_MEDIUM 0x3a /* more info in ASCQ code */
237 #define SCSI_ASC_UNKNOWN_OPCODE 0x20
238 #define SCSI_ASC_UNKNOWN_FIELD 0x24
239 #define SCSI_ASC_UNKNOWN_PARAM 0x26
240 #define SCSI_ASC_WARNING 0xb
241 #define SCSI_ASC_IMPENDING_FAILURE 0x5d
243 #define SCSI_ASCQ_ATA_PASS_THROUGH 0x1d
245 /* Simplified error code (negative values as per errno) */
246 #define SIMPLE_NO_ERROR 0
247 #define SIMPLE_ERR_NOT_READY 1
248 #define SIMPLE_ERR_BAD_OPCODE 2
249 #define SIMPLE_ERR_BAD_FIELD 3 /* in cbd */
250 #define SIMPLE_ERR_BAD_PARAM 4 /* in data */
251 #define SIMPLE_ERR_BAD_RESP 5 /* response fails sanity */
252 #define SIMPLE_ERR_NO_MEDIUM 6 /* no medium present */
253 #define SIMPLE_ERR_BECOMING_READY 7 /* device will be ready soon */
254 #define SIMPLE_ERR_TRY_AGAIN 8 /* some warning, try again */
255 #define SIMPLE_ERR_MEDIUM_HARDWARE 9 /* medium or hardware error */
258 /* defines for functioncode parameter in SENDDIAGNOSTIC function */
259 #define SCSI_DIAG_NO_SELF_TEST 0x00
260 #define SCSI_DIAG_DEF_SELF_TEST 0xff
261 #define SCSI_DIAG_BG_SHORT_SELF_TEST 0x01
262 #define SCSI_DIAG_BG_EXTENDED_SELF_TEST 0x02
263 #define SCSI_DIAG_FG_SHORT_SELF_TEST 0x05
264 #define SCSI_DIAG_FG_EXTENDED_SELF_TEST 0x06
265 #define SCSI_DIAG_ABORT_SELF_TEST 0x04
268 /* SCSI command timeout values (units are seconds) */
269 #define SCSI_TIMEOUT_DEFAULT 6 /* 6 seconds should be ample */
270 #define SCSI_TIMEOUT_SELF_TEST (5 * 60 * 60) /* allow max 5 hours for */
271 /* extended foreground self test */
275 #define LOGPAGEHDRSIZE 4
277 void scsi_do_sense_disect(const struct scsi_cmnd_io
* in
,
278 struct scsi_sense_disect
* out
);
280 int scsiSimpleSenseFilter(const struct scsi_sense_disect
* sinfo
);
282 const char * scsiErrString(int scsiErr
);
284 /* STANDARD SCSI Commands */
285 int scsiTestUnitReady(int device
);
287 int scsiStdInquiry(int device
, UINT8
*pBuf
, int bufLen
);
289 int scsiInquiryVpd(int device
, int vpd_page
, UINT8
*pBuf
, int bufLen
);
291 int scsiLogSense(int device
, int pagenum
, int subpagenum
, UINT8
*pBuf
,
292 int bufLen
, int known_resp_len
);
294 int scsiModeSense(int device
, int pagenum
, int subpagenum
, int pc
,
295 UINT8
*pBuf
, int bufLen
);
297 int scsiModeSelect(int device
, int sp
, UINT8
*pBuf
, int bufLen
);
299 int scsiModeSense10(int device
, int pagenum
, int subpagenum
, int pc
,
300 UINT8
*pBuf
, int bufLen
);
302 int scsiModeSelect10(int device
, int sp
, UINT8
*pBuf
, int bufLen
);
304 int scsiModePageOffset(const UINT8
* resp
, int len
, int modese_len
);
306 int scsiRequestSense(int device
, struct scsi_sense_disect
* sense_info
);
308 int scsiSendDiagnostic(int device
, int functioncode
, UINT8
*pBuf
, int bufLen
);
310 int scsiReceiveDiagnostic(int device
, int pcv
, int pagenum
, UINT8
*pBuf
,
313 int scsiReadDefect10(int device
, int req_plist
, int req_glist
, int dl_format
,
314 UINT8
*pBuf
, int bufLen
);
316 /* SMART specific commands */
317 int scsiCheckIE(int device
, int hasIELogPage
, int hasTempLogPage
, UINT8
*asc
,
318 UINT8
*ascq
, UINT8
*currenttemp
, UINT8
*triptemp
);
320 int scsiFetchIECmpage(int device
, struct scsi_iec_mode_page
*iecp
,
322 int scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page
*iecp
);
323 int scsi_IsWarningEnabled(const struct scsi_iec_mode_page
*iecp
);
324 int scsiSetExceptionControlAndWarning(int device
, int enabled
,
325 const struct scsi_iec_mode_page
*iecp
);
326 void scsiDecodeErrCounterPage(unsigned char * resp
,
327 struct scsiErrorCounter
*ecp
);
328 void scsiDecodeNonMediumErrPage(unsigned char * resp
,
329 struct scsiNonMediumError
*nmep
);
330 int scsiFetchExtendedSelfTestTime(int device
, int * durationSec
,
332 int scsiCountFailedSelfTests(int fd
, int noisy
);
333 int scsiSelfTestInProgress(int fd
, int * inProgress
);
334 int scsiFetchControlGLTSD(int device
, int modese_len
, int current
);
335 int scsiSetControlGLTSD(int device
, int enabled
, int modese_len
);
336 int scsiFetchTransportProtocol(int device
, int modese_len
);
338 /* T10 Standard IE Additional Sense Code strings taken from t10.org */
340 const char* scsiGetIEString(UINT8 asc
, UINT8 ascq
);
341 int scsiGetTemp(int device
, UINT8
*currenttemp
, UINT8
*triptemp
);
344 int scsiSmartIBMOfflineTest(int device
);
346 int scsiSmartDefaultSelfTest(int device
);
347 int scsiSmartShortSelfTest(int device
);
348 int scsiSmartExtendSelfTest(int device
);
349 int scsiSmartShortCapSelfTest(int device
);
350 int scsiSmartExtendCapSelfTest(int device
);
351 int scsiSmartSelfTestAbort(int device
);
353 const char * scsiTapeAlertsTapeDevice(unsigned short code
);
354 const char * scsiTapeAlertsChangerDevice(unsigned short code
);
356 const char * scsi_get_opcode_name(UINT8 opcode
);
357 void dStrHex(const char* str
, int len
, int no_ascii
);
358 inline void dStrHex(const unsigned char* str
, int len
, int no_ascii
)
359 { dStrHex((const char *)str
, len
, no_ascii
); }
362 /* SCSI command transmission interface function declaration. Its
363 * definition is target OS specific (see os_<OS>.c file).
364 * Returns 0 if SCSI command successfully launched and response
365 * received. Even when 0 is returned the caller should check
366 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
367 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
368 * (e.g. device not present or not a SCSI device) or some other problem
369 * arises (e.g. timeout) then returns a negative errno value. */
370 int do_scsi_cmnd_io(int dev_fd
, struct scsi_cmnd_io
* iop
, int report
);