]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - scsicmds.h
Imported Upstream version 5.39.1+svn3124
[mirror_smartmontools-debian.git] / scsicmds.h
1 /*
2 * scsicmds.h
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2002-8 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
8 *
9 * Additional SCSI work:
10 * Copyright (C) 2003-10 Douglas Gilbert <dgilbert@interlog.com>
11 *
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)
15 * any later version.
16 *
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.
20 *
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/
25 *
26 * N.B. What was formerly known as "SMART" are now called "informational
27 * exceptions" in recent t10.org drafts (i.e. recent SCSI).
28 *
29 */
30
31
32 #ifndef SCSICMDS_H_
33 #define SCSICMDS_H_
34
35 #define SCSICMDS_H_CVSID "$Id: scsicmds.h 3095 2010-04-30 12:33:27Z dpgilbert $\n"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41
42 /* #define SCSI_DEBUG 1 */ /* Comment out to disable command debugging */
43
44 /* Following conditional defines just in case OS already has them defined.
45 * If they are defined we hope they are defined correctly (for SCSI). */
46 #ifndef TEST_UNIT_READY
47 #define TEST_UNIT_READY 0x0
48 #endif
49 #ifndef LOG_SELECT
50 #define LOG_SELECT 0x4c
51 #endif
52 #ifndef LOG_SENSE
53 #define LOG_SENSE 0x4d
54 #endif
55 #ifndef MODE_SENSE
56 #define MODE_SENSE 0x1a
57 #endif
58 #ifndef MODE_SENSE_10
59 #define MODE_SENSE_10 0x5a
60 #endif
61 #ifndef MODE_SELECT
62 #define MODE_SELECT 0x15
63 #endif
64 #ifndef MODE_SELECT_10
65 #define MODE_SELECT_10 0x55
66 #endif
67 #ifndef INQUIRY
68 #define INQUIRY 0x12
69 #endif
70 #ifndef REQUEST_SENSE
71 #define REQUEST_SENSE 0x03
72 #endif
73 #ifndef RECEIVE_DIAGNOSTIC
74 #define RECEIVE_DIAGNOSTIC 0x1c
75 #endif
76 #ifndef SEND_DIAGNOSTIC
77 #define SEND_DIAGNOSTIC 0x1d
78 #endif
79 #ifndef READ_DEFECT_10
80 #define READ_DEFECT_10 0x37
81 #endif
82
83 #ifndef SAT_ATA_PASSTHROUGH_12
84 #define SAT_ATA_PASSTHROUGH_12 0xa1
85 #endif
86 #ifndef SAT_ATA_PASSTHROUGH_16
87 #define SAT_ATA_PASSTHROUGH_16 0x85
88 #endif
89
90 typedef unsigned char UINT8;
91 typedef signed char INT8;
92 typedef unsigned int UINT32;
93 typedef int INT32;
94
95 #define DXFER_NONE 0
96 #define DXFER_FROM_DEVICE 1
97 #define DXFER_TO_DEVICE 2
98
99 struct scsi_cmnd_io
100 {
101 UINT8 * cmnd; /* [in]: ptr to SCSI command block (cdb) */
102 size_t cmnd_len; /* [in]: number of bytes in SCSI command */
103 int dxfer_dir; /* [in]: DXFER_NONE, DXFER_FROM_DEVICE, or
104 DXFER_TO_DEVICE */
105 UINT8 * dxferp; /* [in]: ptr to outgoing or incoming data buffer */
106 size_t dxfer_len; /* [in]: bytes to be transferred to/from dxferp */
107 UINT8 * sensep; /* [in]: ptr to sense buffer, filled when
108 CHECK CONDITION status occurs */
109 size_t max_sense_len; /* [in]: max number of bytes to write to sensep */
110 unsigned timeout; /* [in]: seconds, 0-> default timeout (60 seconds?) */
111 size_t resp_sense_len; /* [out]: sense buffer length written */
112 UINT8 scsi_status; /* [out]: 0->ok, 2->CHECK CONDITION, etc ... */
113 int resid; /* [out]: Number of bytes requested to be transferred
114 less actual number transferred (0 if not
115 supported) */
116 };
117
118 struct scsi_sense_disect {
119 UINT8 error_code;
120 UINT8 sense_key;
121 UINT8 asc;
122 UINT8 ascq;
123 };
124
125 /* Useful data from Informational Exception Control mode page (0x1c) */
126 #define SCSI_IECMP_RAW_LEN 64
127 struct scsi_iec_mode_page {
128 UINT8 requestedCurrent;
129 UINT8 gotCurrent;
130 UINT8 requestedChangeable;
131 UINT8 gotChangeable;
132 UINT8 modese_len; /* 0 (don't know), 6 or 10 */
133 UINT8 raw_curr[SCSI_IECMP_RAW_LEN];
134 UINT8 raw_chg[SCSI_IECMP_RAW_LEN];
135 };
136
137 /* Carrier for Error counter log pages (e.g. read, write, verify ...) */
138 struct scsiErrorCounter {
139 UINT8 gotPC[7];
140 UINT8 gotExtraPC;
141 uint64_t counter[8];
142 };
143
144 /* Carrier for Non-medium error log page */
145 struct scsiNonMediumError {
146 UINT8 gotPC0;
147 UINT8 gotExtraPC;
148 uint64_t counterPC0;
149 UINT8 gotTFE_H;
150 uint64_t counterTFE_H; /* Track following errors [Hitachi] */
151 UINT8 gotPE_H;
152 uint64_t counterPE_H; /* Positioning errors [Hitachi] */
153 };
154
155 /* SCSI Peripheral types (of interest) */
156 #define SCSI_PT_DIRECT_ACCESS 0x0
157 #define SCSI_PT_SEQUENTIAL_ACCESS 0x1
158 #define SCSI_PT_CDROM 0x5
159 #define SCSI_PT_MEDIUM_CHANGER 0x8
160 #define SCSI_PT_ENCLOSURE 0xd
161
162 /* ANSI SCSI-3 Log Pages retrieved by LOG SENSE. */
163 #define SUPPORTED_LPAGES 0x00
164 #define BUFFER_OVERRUN_LPAGE 0x01
165 #define WRITE_ERROR_COUNTER_LPAGE 0x02
166 #define READ_ERROR_COUNTER_LPAGE 0x03
167 #define READ_REVERSE_ERROR_COUNTER_LPAGE 0x04
168 #define VERIFY_ERROR_COUNTER_LPAGE 0x05
169 #define NON_MEDIUM_ERROR_LPAGE 0x06
170 #define LAST_N_ERROR_LPAGE 0x07
171 #define FORMAT_STATUS_LPAGE 0x08
172 #define TEMPERATURE_LPAGE 0x0d
173 #define STARTSTOP_CYCLE_COUNTER_LPAGE 0x0e
174 #define APPLICATION_CLIENT_LPAGE 0x0f
175 #define SELFTEST_RESULTS_LPAGE 0x10
176 #define BACKGROUND_RESULTS_LPAGE 0x15 /* SBC-3 */
177 #define PROTOCOL_SPECIFIC_LPAGE 0x18
178 #define IE_LPAGE 0x2f
179
180 /* Seagate vendor specific log pages. */
181 #define SEAGATE_CACHE_LPAGE 0x37
182 #define SEAGATE_FACTORY_LPAGE 0x3e
183
184 /* Log page response lengths */
185 #define LOG_RESP_SELF_TEST_LEN 0x194
186
187 /* See the SSC-2 document at www.t10.org . Earler note: From IBM
188 Documentation, see http://www.storage.ibm.com/techsup/hddtech/prodspecs.htm */
189 #define TAPE_ALERTS_LPAGE 0x2e
190
191 /* ANSI SCSI-3 Mode Pages */
192 #define VENDOR_UNIQUE_PAGE 0x00
193 #define READ_WRITE_ERROR_RECOVERY_PAGE 0x01
194 #define DISCONNECT_RECONNECT_PAGE 0x02
195 #define FORMAT_DEVICE_PAGE 0x03
196 #define RIGID_DISK_DRIVE_GEOMETRY_PAGE 0x04
197 #define FLEXIBLE_DISK_PAGE 0x05
198 #define VERIFY_ERROR_RECOVERY_PAGE 0x07
199 #define CACHING_PAGE 0x08
200 #define PERIPHERAL_DEVICE_PAGE 0x09
201 #define XOR_CONTROL_MODE_PAGE 0x10
202 #define CONTROL_MODE_PAGE 0x0a
203 #define MEDIUM_TYPES_SUPPORTED_PAGE 0x0b
204 #define NOTCH_PAGE 0x0c
205 #define CD_DEVICE_PAGE 0x0d
206 #define CD_AUDIO_CONTROL_PAGE 0x0e
207 #define DATA_COMPRESSION_PAGE 0x0f
208 #define ENCLOSURE_SERVICES_MANAGEMENT_PAGE 0x14
209 #define PROTOCOL_SPECIFIC_LUN_PAGE 0x18
210 #define PROTOCOL_SPECIFIC_PORT_PAGE 0x19
211 #define POWER_CONDITION_PAGE 0x1a
212 #define INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE 0x1c
213 #define FAULT_FAILURE_REPORTING_PAGE 0x1c
214
215 /* Background control mode subpage is [0x1c,0x1] */
216 #define BACKGROUND_CONTROL_M_SUBPAGE 0x1 /* SBC-2 */
217
218 #define ALL_MODE_PAGES 0x3f
219
220 /* Mode page control field */
221 #define MPAGE_CONTROL_CURRENT 0
222 #define MPAGE_CONTROL_CHANGEABLE 1
223 #define MPAGE_CONTROL_DEFAULT 2
224 #define MPAGE_CONTROL_SAVED 3
225
226 /* defines for useful SCSI Status codes */
227 #define SCSI_STATUS_CHECK_CONDITION 0x2
228
229 /* defines for useful Sense Key codes */
230 #define SCSI_SK_NO_SENSE 0x0
231 #define SCSI_SK_RECOVERED_ERR 0x1
232 #define SCSI_SK_NOT_READY 0x2
233 #define SCSI_SK_MEDIUM_ERROR 0x3
234 #define SCSI_SK_HARDWARE_ERROR 0x4
235 #define SCSI_SK_ILLEGAL_REQUEST 0x5
236 #define SCSI_SK_UNIT_ATTENTION 0x6
237 #define SCSI_SK_ABORTED_COMMAND 0xb
238
239 /* defines for useful Additional Sense Codes (ASCs) */
240 #define SCSI_ASC_NOT_READY 0x4 /* more info in ASCQ code */
241 #define SCSI_ASC_NO_MEDIUM 0x3a /* more info in ASCQ code */
242 #define SCSI_ASC_UNKNOWN_OPCODE 0x20
243 #define SCSI_ASC_UNKNOWN_FIELD 0x24
244 #define SCSI_ASC_UNKNOWN_PARAM 0x26
245 #define SCSI_ASC_WARNING 0xb
246 #define SCSI_ASC_IMPENDING_FAILURE 0x5d
247
248 #define SCSI_ASCQ_ATA_PASS_THROUGH 0x1d
249
250 /* Simplified error code (negative values as per errno) */
251 #define SIMPLE_NO_ERROR 0
252 #define SIMPLE_ERR_NOT_READY 1
253 #define SIMPLE_ERR_BAD_OPCODE 2
254 #define SIMPLE_ERR_BAD_FIELD 3 /* in cbd */
255 #define SIMPLE_ERR_BAD_PARAM 4 /* in data */
256 #define SIMPLE_ERR_BAD_RESP 5 /* response fails sanity */
257 #define SIMPLE_ERR_NO_MEDIUM 6 /* no medium present */
258 #define SIMPLE_ERR_BECOMING_READY 7 /* device will be ready soon */
259 #define SIMPLE_ERR_TRY_AGAIN 8 /* some warning, try again */
260 #define SIMPLE_ERR_MEDIUM_HARDWARE 9 /* medium or hardware error */
261 #define SIMPLE_ERR_UNKNOWN 10 /* unknown sense value */
262 #define SIMPLE_ERR_ABORTED_COMMAND 11 /* most likely transport error */
263
264
265 /* defines for functioncode parameter in SENDDIAGNOSTIC function */
266 #define SCSI_DIAG_NO_SELF_TEST 0x00
267 #define SCSI_DIAG_DEF_SELF_TEST 0xff
268 #define SCSI_DIAG_BG_SHORT_SELF_TEST 0x01
269 #define SCSI_DIAG_BG_EXTENDED_SELF_TEST 0x02
270 #define SCSI_DIAG_FG_SHORT_SELF_TEST 0x05
271 #define SCSI_DIAG_FG_EXTENDED_SELF_TEST 0x06
272 #define SCSI_DIAG_ABORT_SELF_TEST 0x04
273
274
275 /* SCSI command timeout values (units are seconds) */
276 #define SCSI_TIMEOUT_DEFAULT 20 // should be longer than the spin up time
277 // of a disk in standby mode.
278 #define SCSI_TIMEOUT_SELF_TEST (5 * 60 * 60) /* allow max 5 hours for */
279 /* extended foreground self test */
280
281
282
283 #define LOGPAGEHDRSIZE 4
284
285 class scsi_device;
286
287 void scsi_do_sense_disect(const struct scsi_cmnd_io * in,
288 struct scsi_sense_disect * out);
289
290 int scsiSimpleSenseFilter(const struct scsi_sense_disect * sinfo);
291
292 const char * scsiErrString(int scsiErr);
293
294 /* STANDARD SCSI Commands */
295 int scsiTestUnitReady(scsi_device * device);
296
297 int scsiStdInquiry(scsi_device * device, UINT8 *pBuf, int bufLen);
298
299 int scsiInquiryVpd(scsi_device * device, int vpd_page, UINT8 *pBuf, int bufLen);
300
301 int scsiLogSense(scsi_device * device, int pagenum, int subpagenum, UINT8 *pBuf,
302 int bufLen, int known_resp_len);
303
304 int scsiLogSelect(scsi_device * device, int pcr, int sp, int pc, int pagenum,
305 int subpagenum, UINT8 *pBuf, int bufLen);
306
307 int scsiModeSense(scsi_device * device, int pagenum, int subpagenum, int pc,
308 UINT8 *pBuf, int bufLen);
309
310 int scsiModeSelect(scsi_device * device, int sp, UINT8 *pBuf, int bufLen);
311
312 int scsiModeSense10(scsi_device * device, int pagenum, int subpagenum, int pc,
313 UINT8 *pBuf, int bufLen);
314
315 int scsiModeSelect10(scsi_device * device, int sp, UINT8 *pBuf, int bufLen);
316
317 int scsiModePageOffset(const UINT8 * resp, int len, int modese_len);
318
319 int scsiRequestSense(scsi_device * device, struct scsi_sense_disect * sense_info);
320
321 int scsiSendDiagnostic(scsi_device * device, int functioncode, UINT8 *pBuf, int bufLen);
322
323 int scsiReceiveDiagnostic(scsi_device * device, int pcv, int pagenum, UINT8 *pBuf,
324 int bufLen);
325
326 int scsiReadDefect10(scsi_device * device, int req_plist, int req_glist, int dl_format,
327 UINT8 *pBuf, int bufLen);
328
329 /* SMART specific commands */
330 int scsiCheckIE(scsi_device * device, int hasIELogPage, int hasTempLogPage, UINT8 *asc,
331 UINT8 *ascq, UINT8 *currenttemp, UINT8 *triptemp);
332
333 int scsiFetchIECmpage(scsi_device * device, struct scsi_iec_mode_page *iecp,
334 int modese_len);
335 int scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page *iecp);
336 int scsi_IsWarningEnabled(const struct scsi_iec_mode_page *iecp);
337 int scsiSetExceptionControlAndWarning(scsi_device * device, int enabled,
338 const struct scsi_iec_mode_page *iecp);
339 void scsiDecodeErrCounterPage(unsigned char * resp,
340 struct scsiErrorCounter *ecp);
341 void scsiDecodeNonMediumErrPage(unsigned char * resp,
342 struct scsiNonMediumError *nmep);
343 int scsiFetchExtendedSelfTestTime(scsi_device * device, int * durationSec,
344 int modese_len);
345 int scsiCountFailedSelfTests(scsi_device * device, int noisy);
346 int scsiSelfTestInProgress(scsi_device * device, int * inProgress);
347 int scsiFetchControlGLTSD(scsi_device * device, int modese_len, int current);
348 int scsiSetControlGLTSD(scsi_device * device, int enabled, int modese_len);
349 int scsiFetchTransportProtocol(scsi_device * device, int modese_len);
350
351 /* T10 Standard IE Additional Sense Code strings taken from t10.org */
352
353 const char* scsiGetIEString(UINT8 asc, UINT8 ascq);
354 int scsiGetTemp(scsi_device * device, UINT8 *currenttemp, UINT8 *triptemp);
355
356
357 int scsiSmartIBMOfflineTest(scsi_device * device);
358
359 int scsiSmartDefaultSelfTest(scsi_device * device);
360 int scsiSmartShortSelfTest(scsi_device * device);
361 int scsiSmartExtendSelfTest(scsi_device * device);
362 int scsiSmartShortCapSelfTest(scsi_device * device);
363 int scsiSmartExtendCapSelfTest(scsi_device * device);
364 int scsiSmartSelfTestAbort(scsi_device * device);
365
366 const char * scsiTapeAlertsTapeDevice(unsigned short code);
367 const char * scsiTapeAlertsChangerDevice(unsigned short code);
368
369 const char * scsi_get_opcode_name(UINT8 opcode);
370 void dStrHex(const char* str, int len, int no_ascii);
371 inline void dStrHex(const unsigned char* str, int len, int no_ascii)
372 { dStrHex((const char *)str, len, no_ascii); }
373
374
375 /* SCSI command transmission interface function declaration. Its
376 * definition is target OS specific (see os_<OS>.c file).
377 * Returns 0 if SCSI command successfully launched and response
378 * received. Even when 0 is returned the caller should check
379 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
380 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
381 * (e.g. device not present or not a SCSI device) or some other problem
382 * arises (e.g. timeout) then returns a negative errno value. */
383 // Moved to C++ interface
384 //int do_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report);
385
386
387
388 #endif
389