]> git.proxmox.com Git - mirror_smartmontools-debian.git/blame - scsicmds.h
Closes #831504
[mirror_smartmontools-debian.git] / scsicmds.h
CommitLineData
832b75ed
GG
1/*
2 * scsicmds.h
3 *
6b80b4d2 4 * Home page of code is: http://www.smartmontools.org
832b75ed 5 *
293b5ab8 6 * Copyright (C) 2002-8 Bruce Allen
832b75ed 7 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
293b5ab8 8 * Copyright (C) 2003-15 Douglas Gilbert <dgilbert@interlog.com>
832b75ed
GG
9 *
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)
13 * any later version.
14 *
15 * You should have received a copy of the GNU General Public License
ee38a438
GI
16 * (for example COPYING); if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
832b75ed
GG
18 *
19 * This code was originally developed as a Senior Thesis by Michael Cornwell
20 * at the Concurrent Systems Laboratory (now part of the Storage Systems
21 * Research Center), Jack Baskin School of Engineering, University of
22 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
23 *
24 * N.B. What was formerly known as "SMART" are now called "informational
25 * exceptions" in recent t10.org drafts (i.e. recent SCSI).
26 *
27 */
28
29
30#ifndef SCSICMDS_H_
31#define SCSICMDS_H_
32
6b80b4d2 33#define SCSICMDS_H_CVSID "$Id: scsicmds.h 4152 2015-10-17 16:08:21Z chrfranke $\n"
832b75ed
GG
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
832b75ed
GG
38
39/* #define SCSI_DEBUG 1 */ /* Comment out to disable command debugging */
40
2127e193
GI
41/* Following conditional defines just in case OS already has them defined.
42 * If they are defined we hope they are defined correctly (for SCSI). */
832b75ed
GG
43#ifndef TEST_UNIT_READY
44#define TEST_UNIT_READY 0x0
45#endif
2127e193
GI
46#ifndef LOG_SELECT
47#define LOG_SELECT 0x4c
48#endif
832b75ed
GG
49#ifndef LOG_SENSE
50#define LOG_SENSE 0x4d
51#endif
52#ifndef MODE_SENSE
53#define MODE_SENSE 0x1a
54#endif
55#ifndef MODE_SENSE_10
56#define MODE_SENSE_10 0x5a
57#endif
58#ifndef MODE_SELECT
59#define MODE_SELECT 0x15
60#endif
61#ifndef MODE_SELECT_10
62#define MODE_SELECT_10 0x55
63#endif
64#ifndef INQUIRY
65#define INQUIRY 0x12
66#endif
67#ifndef REQUEST_SENSE
68#define REQUEST_SENSE 0x03
69#endif
70#ifndef RECEIVE_DIAGNOSTIC
71#define RECEIVE_DIAGNOSTIC 0x1c
72#endif
73#ifndef SEND_DIAGNOSTIC
74#define SEND_DIAGNOSTIC 0x1d
75#endif
76#ifndef READ_DEFECT_10
77#define READ_DEFECT_10 0x37
78#endif
ee38a438
GI
79#ifndef READ_DEFECT_12
80#define READ_DEFECT_12 0xb7
81#endif
cfbba5b9
GI
82#ifndef START_STOP_UNIT
83#define START_STOP_UNIT 0x1b
84#endif
85#ifndef REPORT_LUNS
86#define REPORT_LUNS 0xa0
87#endif
88#ifndef READ_CAPACITY_10
89#define READ_CAPACITY_10 0x25
90#endif
91#ifndef READ_CAPACITY_16
a7e8ffec
GI
92#define READ_CAPACITY_16 0x9e
93#endif
94#ifndef SAI_READ_CAPACITY_16 /* service action for READ_CAPACITY_16 */
95#define SAI_READ_CAPACITY_16 0x10
cfbba5b9 96#endif
832b75ed 97
4d59bff9
GG
98#ifndef SAT_ATA_PASSTHROUGH_12
99#define SAT_ATA_PASSTHROUGH_12 0xa1
100#endif
101#ifndef SAT_ATA_PASSTHROUGH_16
102#define SAT_ATA_PASSTHROUGH_16 0x85
103#endif
104
832b75ed 105typedef unsigned char UINT8;
a37e7145 106typedef signed char INT8;
832b75ed
GG
107typedef unsigned int UINT32;
108typedef int INT32;
109
110#define DXFER_NONE 0
111#define DXFER_FROM_DEVICE 1
112#define DXFER_TO_DEVICE 2
113
114struct scsi_cmnd_io
115{
116 UINT8 * cmnd; /* [in]: ptr to SCSI command block (cdb) */
117 size_t cmnd_len; /* [in]: number of bytes in SCSI command */
ee38a438 118 int dxfer_dir; /* [in]: DXFER_NONE, DXFER_FROM_DEVICE, or
832b75ed
GG
119 DXFER_TO_DEVICE */
120 UINT8 * dxferp; /* [in]: ptr to outgoing or incoming data buffer */
121 size_t dxfer_len; /* [in]: bytes to be transferred to/from dxferp */
ee38a438 122 UINT8 * sensep; /* [in]: ptr to sense buffer, filled when
832b75ed
GG
123 CHECK CONDITION status occurs */
124 size_t max_sense_len; /* [in]: max number of bytes to write to sensep */
125 unsigned timeout; /* [in]: seconds, 0-> default timeout (60 seconds?) */
126 size_t resp_sense_len; /* [out]: sense buffer length written */
127 UINT8 scsi_status; /* [out]: 0->ok, 2->CHECK CONDITION, etc ... */
128 int resid; /* [out]: Number of bytes requested to be transferred
129 less actual number transferred (0 if not
130 supported) */
131};
132
133struct scsi_sense_disect {
ee38a438 134 UINT8 resp_code;
832b75ed 135 UINT8 sense_key;
ee38a438 136 UINT8 asc;
832b75ed 137 UINT8 ascq;
ee38a438 138 int progress; /* -1 -> N/A, 0-65535 -> available */
832b75ed
GG
139};
140
141/* Useful data from Informational Exception Control mode page (0x1c) */
142#define SCSI_IECMP_RAW_LEN 64
143struct scsi_iec_mode_page {
144 UINT8 requestedCurrent;
145 UINT8 gotCurrent;
146 UINT8 requestedChangeable;
147 UINT8 gotChangeable;
148 UINT8 modese_len; /* 0 (don't know), 6 or 10 */
149 UINT8 raw_curr[SCSI_IECMP_RAW_LEN];
150 UINT8 raw_chg[SCSI_IECMP_RAW_LEN];
151};
152
153/* Carrier for Error counter log pages (e.g. read, write, verify ...) */
154struct scsiErrorCounter {
155 UINT8 gotPC[7];
156 UINT8 gotExtraPC;
157 uint64_t counter[8];
158};
159
160/* Carrier for Non-medium error log page */
161struct scsiNonMediumError {
162 UINT8 gotPC0;
163 UINT8 gotExtraPC;
164 uint64_t counterPC0;
165 UINT8 gotTFE_H;
166 uint64_t counterTFE_H; /* Track following errors [Hitachi] */
167 UINT8 gotPE_H;
168 uint64_t counterPE_H; /* Positioning errors [Hitachi] */
169};
170
171/* SCSI Peripheral types (of interest) */
172#define SCSI_PT_DIRECT_ACCESS 0x0
173#define SCSI_PT_SEQUENTIAL_ACCESS 0x1
174#define SCSI_PT_CDROM 0x5
175#define SCSI_PT_MEDIUM_CHANGER 0x8
176#define SCSI_PT_ENCLOSURE 0xd
177
178/* ANSI SCSI-3 Log Pages retrieved by LOG SENSE. */
179#define SUPPORTED_LPAGES 0x00
180#define BUFFER_OVERRUN_LPAGE 0x01
181#define WRITE_ERROR_COUNTER_LPAGE 0x02
182#define READ_ERROR_COUNTER_LPAGE 0x03
183#define READ_REVERSE_ERROR_COUNTER_LPAGE 0x04
184#define VERIFY_ERROR_COUNTER_LPAGE 0x05
185#define NON_MEDIUM_ERROR_LPAGE 0x06
186#define LAST_N_ERROR_LPAGE 0x07
187#define FORMAT_STATUS_LPAGE 0x08
d008864d 188#define LB_PROV_LPAGE 0x0c /* SBC-3 */
832b75ed
GG
189#define TEMPERATURE_LPAGE 0x0d
190#define STARTSTOP_CYCLE_COUNTER_LPAGE 0x0e
191#define APPLICATION_CLIENT_LPAGE 0x0f
192#define SELFTEST_RESULTS_LPAGE 0x10
d008864d 193#define SS_MEDIA_LPAGE 0x11 /* SBC-3 */
4d59bff9 194#define BACKGROUND_RESULTS_LPAGE 0x15 /* SBC-3 */
d008864d 195#define NONVOL_CACHE_LPAGE 0x17 /* SBC-3 */
2127e193 196#define PROTOCOL_SPECIFIC_LPAGE 0x18
832b75ed
GG
197#define IE_LPAGE 0x2f
198
199/* Seagate vendor specific log pages. */
200#define SEAGATE_CACHE_LPAGE 0x37
201#define SEAGATE_FACTORY_LPAGE 0x3e
202
203/* Log page response lengths */
204#define LOG_RESP_SELF_TEST_LEN 0x194
205
ee38a438 206/* See the SSC-2 document at www.t10.org . Earler note: From IBM
832b75ed
GG
207Documentation, see http://www.storage.ibm.com/techsup/hddtech/prodspecs.htm */
208#define TAPE_ALERTS_LPAGE 0x2e
209
210/* ANSI SCSI-3 Mode Pages */
211#define VENDOR_UNIQUE_PAGE 0x00
212#define READ_WRITE_ERROR_RECOVERY_PAGE 0x01
213#define DISCONNECT_RECONNECT_PAGE 0x02
214#define FORMAT_DEVICE_PAGE 0x03
215#define RIGID_DISK_DRIVE_GEOMETRY_PAGE 0x04
216#define FLEXIBLE_DISK_PAGE 0x05
217#define VERIFY_ERROR_RECOVERY_PAGE 0x07
218#define CACHING_PAGE 0x08
219#define PERIPHERAL_DEVICE_PAGE 0x09
220#define XOR_CONTROL_MODE_PAGE 0x10
221#define CONTROL_MODE_PAGE 0x0a
222#define MEDIUM_TYPES_SUPPORTED_PAGE 0x0b
223#define NOTCH_PAGE 0x0c
224#define CD_DEVICE_PAGE 0x0d
225#define CD_AUDIO_CONTROL_PAGE 0x0e
226#define DATA_COMPRESSION_PAGE 0x0f
227#define ENCLOSURE_SERVICES_MANAGEMENT_PAGE 0x14
228#define PROTOCOL_SPECIFIC_LUN_PAGE 0x18
229#define PROTOCOL_SPECIFIC_PORT_PAGE 0x19
230#define POWER_CONDITION_PAGE 0x1a
231#define INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE 0x1c
232#define FAULT_FAILURE_REPORTING_PAGE 0x1c
233
4d59bff9
GG
234/* Background control mode subpage is [0x1c,0x1] */
235#define BACKGROUND_CONTROL_M_SUBPAGE 0x1 /* SBC-2 */
236
832b75ed
GG
237#define ALL_MODE_PAGES 0x3f
238
239/* Mode page control field */
240#define MPAGE_CONTROL_CURRENT 0
241#define MPAGE_CONTROL_CHANGEABLE 1
242#define MPAGE_CONTROL_DEFAULT 2
243#define MPAGE_CONTROL_SAVED 3
244
ee38a438
GI
245/* SCSI Vital Product Data (VPD) pages */
246#define SCSI_VPD_SUPPORTED_VPD_PAGES 0x0
247#define SCSI_VPD_UNIT_SERIAL_NUMBER 0x80
248#define SCSI_VPD_DEVICE_IDENTIFICATION 0x83
249#define SCSI_VPD_EXTENDED_INQUIRY_DATA 0x86
250#define SCSI_VPD_ATA_INFORMATION 0x89
251#define SCSI_VPD_POWER_CONDITION 0x8a
252#define SCSI_VPD_POWER_CONSUMPTION 0x8d
253#define SCSI_VPD_BLOCK_LIMITS 0xb0
254#define SCSI_VPD_BLOCK_DEVICE_CHARACTERISTICS 0xb1
255#define SCSI_VPD_LOGICAL_BLOCK_PROVISIONING 0xb2
256
832b75ed
GG
257/* defines for useful SCSI Status codes */
258#define SCSI_STATUS_CHECK_CONDITION 0x2
259
260/* defines for useful Sense Key codes */
4d59bff9
GG
261#define SCSI_SK_NO_SENSE 0x0
262#define SCSI_SK_RECOVERED_ERR 0x1
832b75ed
GG
263#define SCSI_SK_NOT_READY 0x2
264#define SCSI_SK_MEDIUM_ERROR 0x3
265#define SCSI_SK_HARDWARE_ERROR 0x4
266#define SCSI_SK_ILLEGAL_REQUEST 0x5
267#define SCSI_SK_UNIT_ATTENTION 0x6
a37e7145 268#define SCSI_SK_ABORTED_COMMAND 0xb
832b75ed
GG
269
270/* defines for useful Additional Sense Codes (ASCs) */
271#define SCSI_ASC_NOT_READY 0x4 /* more info in ASCQ code */
272#define SCSI_ASC_NO_MEDIUM 0x3a /* more info in ASCQ code */
273#define SCSI_ASC_UNKNOWN_OPCODE 0x20
ee38a438 274#define SCSI_ASC_INVALID_FIELD 0x24
832b75ed
GG
275#define SCSI_ASC_UNKNOWN_PARAM 0x26
276#define SCSI_ASC_WARNING 0xb
277#define SCSI_ASC_IMPENDING_FAILURE 0x5d
278
4d59bff9
GG
279#define SCSI_ASCQ_ATA_PASS_THROUGH 0x1d
280
832b75ed
GG
281/* Simplified error code (negative values as per errno) */
282#define SIMPLE_NO_ERROR 0
283#define SIMPLE_ERR_NOT_READY 1
284#define SIMPLE_ERR_BAD_OPCODE 2
285#define SIMPLE_ERR_BAD_FIELD 3 /* in cbd */
286#define SIMPLE_ERR_BAD_PARAM 4 /* in data */
287#define SIMPLE_ERR_BAD_RESP 5 /* response fails sanity */
288#define SIMPLE_ERR_NO_MEDIUM 6 /* no medium present */
289#define SIMPLE_ERR_BECOMING_READY 7 /* device will be ready soon */
290#define SIMPLE_ERR_TRY_AGAIN 8 /* some warning, try again */
291#define SIMPLE_ERR_MEDIUM_HARDWARE 9 /* medium or hardware error */
a37e7145
GG
292#define SIMPLE_ERR_UNKNOWN 10 /* unknown sense value */
293#define SIMPLE_ERR_ABORTED_COMMAND 11 /* most likely transport error */
832b75ed
GG
294
295
296/* defines for functioncode parameter in SENDDIAGNOSTIC function */
297#define SCSI_DIAG_NO_SELF_TEST 0x00
298#define SCSI_DIAG_DEF_SELF_TEST 0xff
299#define SCSI_DIAG_BG_SHORT_SELF_TEST 0x01
300#define SCSI_DIAG_BG_EXTENDED_SELF_TEST 0x02
301#define SCSI_DIAG_FG_SHORT_SELF_TEST 0x05
302#define SCSI_DIAG_FG_EXTENDED_SELF_TEST 0x06
303#define SCSI_DIAG_ABORT_SELF_TEST 0x04
304
305
306/* SCSI command timeout values (units are seconds) */
eb07ddf2
GI
307#define SCSI_TIMEOUT_DEFAULT 20 // should be longer than the spin up time
308 // of a disk in standby mode.
832b75ed
GG
309#define SCSI_TIMEOUT_SELF_TEST (5 * 60 * 60) /* allow max 5 hours for */
310 /* extended foreground self test */
311
312
313
314#define LOGPAGEHDRSIZE 4
315
2127e193
GI
316class scsi_device;
317
ee38a438
GI
318// Set of supported SCSI VPD pages. Constructor fetches Supported VPD pages
319// VPD page and remembers the response for later queries.
320class supported_vpd_pages
321{
322public:
6b80b4d2 323 explicit supported_vpd_pages(scsi_device * device);
ee38a438
GI
324 ~supported_vpd_pages() { num_valid = 0; }
325
326 bool is_supported(int vpd_page_num) const;
327
328 /* Returns 0 or less for VPD pages not supported or error */
329 int num_pages() const { return num_valid; }
330
331private:
332 int num_valid; /* 0 or less for invalid */
333 unsigned char pages[256];
334};
335
336extern supported_vpd_pages * supported_vpd_pages_p;
337
338
cfbba5b9
GI
339// Print SCSI debug messages?
340extern unsigned char scsi_debugmode;
341
832b75ed
GG
342void scsi_do_sense_disect(const struct scsi_cmnd_io * in,
343 struct scsi_sense_disect * out);
344
4d59bff9
GG
345int scsiSimpleSenseFilter(const struct scsi_sense_disect * sinfo);
346
832b75ed
GG
347const char * scsiErrString(int scsiErr);
348
a7e8ffec
GI
349int scsi_vpd_dev_id_iter(const unsigned char * initial_desig_desc,
350 int page_len, int * off, int m_assoc,
351 int m_desig_type, int m_code_set);
352
353int scsi_decode_lu_dev_id(const unsigned char * b, int blen, char * s,
354 int slen, int * transport);
355
356
832b75ed 357/* STANDARD SCSI Commands */
2127e193 358int scsiTestUnitReady(scsi_device * device);
832b75ed 359
2127e193 360int scsiStdInquiry(scsi_device * device, UINT8 *pBuf, int bufLen);
832b75ed 361
2127e193 362int scsiInquiryVpd(scsi_device * device, int vpd_page, UINT8 *pBuf, int bufLen);
832b75ed 363
2127e193
GI
364int scsiLogSense(scsi_device * device, int pagenum, int subpagenum, UINT8 *pBuf,
365 int bufLen, int known_resp_len);
832b75ed 366
2127e193
GI
367int scsiLogSelect(scsi_device * device, int pcr, int sp, int pc, int pagenum,
368 int subpagenum, UINT8 *pBuf, int bufLen);
369
370int scsiModeSense(scsi_device * device, int pagenum, int subpagenum, int pc,
4d59bff9 371 UINT8 *pBuf, int bufLen);
832b75ed 372
2127e193 373int scsiModeSelect(scsi_device * device, int sp, UINT8 *pBuf, int bufLen);
832b75ed 374
2127e193
GI
375int scsiModeSense10(scsi_device * device, int pagenum, int subpagenum, int pc,
376 UINT8 *pBuf, int bufLen);
832b75ed 377
2127e193 378int scsiModeSelect10(scsi_device * device, int sp, UINT8 *pBuf, int bufLen);
832b75ed
GG
379
380int scsiModePageOffset(const UINT8 * resp, int len, int modese_len);
381
2127e193 382int scsiRequestSense(scsi_device * device, struct scsi_sense_disect * sense_info);
832b75ed 383
2127e193 384int scsiSendDiagnostic(scsi_device * device, int functioncode, UINT8 *pBuf, int bufLen);
832b75ed 385
2127e193 386int scsiReadDefect10(scsi_device * device, int req_plist, int req_glist, int dl_format,
832b75ed
GG
387 UINT8 *pBuf, int bufLen);
388
ee38a438
GI
389int scsiReadDefect12(scsi_device * device, int req_plist, int req_glist,
390 int dl_format, int addrDescIndex, UINT8 *pBuf, int bufLen);
391
a7e8ffec
GI
392int scsiReadCapacity10(scsi_device * device, unsigned int * last_lbp,
393 unsigned int * lb_sizep);
394
395int scsiReadCapacity16(scsi_device * device, UINT8 *pBuf, int bufLen);
396
832b75ed 397/* SMART specific commands */
2127e193 398int scsiCheckIE(scsi_device * device, int hasIELogPage, int hasTempLogPage, UINT8 *asc,
832b75ed
GG
399 UINT8 *ascq, UINT8 *currenttemp, UINT8 *triptemp);
400
2127e193 401int scsiFetchIECmpage(scsi_device * device, struct scsi_iec_mode_page *iecp,
832b75ed
GG
402 int modese_len);
403int scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page *iecp);
404int scsi_IsWarningEnabled(const struct scsi_iec_mode_page *iecp);
2127e193 405int scsiSetExceptionControlAndWarning(scsi_device * device, int enabled,
832b75ed
GG
406 const struct scsi_iec_mode_page *iecp);
407void scsiDecodeErrCounterPage(unsigned char * resp,
408 struct scsiErrorCounter *ecp);
409void scsiDecodeNonMediumErrPage(unsigned char * resp,
410 struct scsiNonMediumError *nmep);
2127e193 411int scsiFetchExtendedSelfTestTime(scsi_device * device, int * durationSec,
832b75ed 412 int modese_len);
2127e193
GI
413int scsiCountFailedSelfTests(scsi_device * device, int noisy);
414int scsiSelfTestInProgress(scsi_device * device, int * inProgress);
415int scsiFetchControlGLTSD(scsi_device * device, int modese_len, int current);
416int scsiSetControlGLTSD(scsi_device * device, int enabled, int modese_len);
417int scsiFetchTransportProtocol(scsi_device * device, int modese_len);
d2e702cf
GI
418int scsiGetRPM(scsi_device * device, int modese_len, int * form_factorp,
419 int * haw_zbcp);
ee38a438
GI
420int scsiGetSetCache(scsi_device * device, int modese_len, short int * wce,
421 short int * rcd);
422uint64_t scsiGetSize(scsi_device * device, unsigned int * lb_sizep,
423 int * lb_per_pb_expp);
424int scsiGetProtPBInfo(scsi_device * device, unsigned char * rc16_12_31p);
832b75ed 425
a7e8ffec 426/* T10 Standard IE Additional Sense Code strings taken from t10.org */
832b75ed 427const char* scsiGetIEString(UINT8 asc, UINT8 ascq);
2127e193 428int scsiGetTemp(scsi_device * device, UINT8 *currenttemp, UINT8 *triptemp);
832b75ed
GG
429
430
2127e193
GI
431int scsiSmartDefaultSelfTest(scsi_device * device);
432int scsiSmartShortSelfTest(scsi_device * device);
433int scsiSmartExtendSelfTest(scsi_device * device);
434int scsiSmartShortCapSelfTest(scsi_device * device);
435int scsiSmartExtendCapSelfTest(scsi_device * device);
436int scsiSmartSelfTestAbort(scsi_device * device);
832b75ed
GG
437
438const char * scsiTapeAlertsTapeDevice(unsigned short code);
439const char * scsiTapeAlertsChangerDevice(unsigned short code);
440
441const char * scsi_get_opcode_name(UINT8 opcode);
ee38a438
GI
442void scsi_format_id_string(char * out, const unsigned char * in, int n);
443
832b75ed 444void dStrHex(const char* str, int len, int no_ascii);
4d59bff9
GG
445inline void dStrHex(const unsigned char* str, int len, int no_ascii)
446 { dStrHex((const char *)str, len, no_ascii); }
447
ee38a438
GI
448/* Attempt to find the first SCSI sense data descriptor that matches the
449 given 'desc_type'. If found return pointer to start of sense data
450 descriptor; otherwise (including fixed format sense data) returns NULL. */
451const unsigned char * sg_scsi_sense_desc_find(const unsigned char * sensep,
452 int sense_len, int desc_type);
453
832b75ed
GG
454
455/* SCSI command transmission interface function declaration. Its
456 * definition is target OS specific (see os_<OS>.c file).
457 * Returns 0 if SCSI command successfully launched and response
458 * received. Even when 0 is returned the caller should check
459 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
460 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
461 * (e.g. device not present or not a SCSI device) or some other problem
462 * arises (e.g. timeout) then returns a negative errno value. */
2127e193
GI
463// Moved to C++ interface
464//int do_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report);
465
832b75ed
GG
466
467
832b75ed 468#endif