]> git.proxmox.com Git - mirror_smartmontools-debian.git/blame - scsicmds.h
import upstream version 5.36
[mirror_smartmontools-debian.git] / scsicmds.h
CommitLineData
832b75ed
GG
1/*
2 * scsicmds.h
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2002-6 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-6 Douglas Gilbert <dougg@torque.net>
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,v 1.57 2006/04/12 14:54:28 ballen4705 Exp $\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 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
48#endif
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
79
80typedef unsigned char UINT8;
81typedef char INT8;
82typedef unsigned int UINT32;
83typedef int INT32;
84
85#define DXFER_NONE 0
86#define DXFER_FROM_DEVICE 1
87#define DXFER_TO_DEVICE 2
88
89struct scsi_cmnd_io
90{
91 UINT8 * cmnd; /* [in]: ptr to SCSI command block (cdb) */
92 size_t cmnd_len; /* [in]: number of bytes in SCSI command */
93 int dxfer_dir; /* [in]: DXFER_NONE, DXFER_FROM_DEVICE, or
94 DXFER_TO_DEVICE */
95 UINT8 * dxferp; /* [in]: ptr to outgoing or incoming data buffer */
96 size_t dxfer_len; /* [in]: bytes to be transferred to/from dxferp */
97 UINT8 * sensep; /* [in]: ptr to sense buffer, filled when
98 CHECK CONDITION status occurs */
99 size_t max_sense_len; /* [in]: max number of bytes to write to sensep */
100 unsigned timeout; /* [in]: seconds, 0-> default timeout (60 seconds?) */
101 size_t resp_sense_len; /* [out]: sense buffer length written */
102 UINT8 scsi_status; /* [out]: 0->ok, 2->CHECK CONDITION, etc ... */
103 int resid; /* [out]: Number of bytes requested to be transferred
104 less actual number transferred (0 if not
105 supported) */
106};
107
108struct scsi_sense_disect {
109 UINT8 error_code;
110 UINT8 sense_key;
111 UINT8 asc;
112 UINT8 ascq;
113};
114
115/* Useful data from Informational Exception Control mode page (0x1c) */
116#define SCSI_IECMP_RAW_LEN 64
117struct scsi_iec_mode_page {
118 UINT8 requestedCurrent;
119 UINT8 gotCurrent;
120 UINT8 requestedChangeable;
121 UINT8 gotChangeable;
122 UINT8 modese_len; /* 0 (don't know), 6 or 10 */
123 UINT8 raw_curr[SCSI_IECMP_RAW_LEN];
124 UINT8 raw_chg[SCSI_IECMP_RAW_LEN];
125};
126
127/* Carrier for Error counter log pages (e.g. read, write, verify ...) */
128struct scsiErrorCounter {
129 UINT8 gotPC[7];
130 UINT8 gotExtraPC;
131 uint64_t counter[8];
132};
133
134/* Carrier for Non-medium error log page */
135struct scsiNonMediumError {
136 UINT8 gotPC0;
137 UINT8 gotExtraPC;
138 uint64_t counterPC0;
139 UINT8 gotTFE_H;
140 uint64_t counterTFE_H; /* Track following errors [Hitachi] */
141 UINT8 gotPE_H;
142 uint64_t counterPE_H; /* Positioning errors [Hitachi] */
143};
144
145/* SCSI Peripheral types (of interest) */
146#define SCSI_PT_DIRECT_ACCESS 0x0
147#define SCSI_PT_SEQUENTIAL_ACCESS 0x1
148#define SCSI_PT_CDROM 0x5
149#define SCSI_PT_MEDIUM_CHANGER 0x8
150#define SCSI_PT_ENCLOSURE 0xd
151
152/* ANSI SCSI-3 Log Pages retrieved by LOG SENSE. */
153#define SUPPORTED_LPAGES 0x00
154#define BUFFER_OVERRUN_LPAGE 0x01
155#define WRITE_ERROR_COUNTER_LPAGE 0x02
156#define READ_ERROR_COUNTER_LPAGE 0x03
157#define READ_REVERSE_ERROR_COUNTER_LPAGE 0x04
158#define VERIFY_ERROR_COUNTER_LPAGE 0x05
159#define NON_MEDIUM_ERROR_LPAGE 0x06
160#define LAST_N_ERROR_LPAGE 0x07
161#define FORMAT_STATUS_LPAGE 0x08
162#define TEMPERATURE_LPAGE 0x0d
163#define STARTSTOP_CYCLE_COUNTER_LPAGE 0x0e
164#define APPLICATION_CLIENT_LPAGE 0x0f
165#define SELFTEST_RESULTS_LPAGE 0x10
166#define IE_LPAGE 0x2f
167
168/* Seagate vendor specific log pages. */
169#define SEAGATE_CACHE_LPAGE 0x37
170#define SEAGATE_FACTORY_LPAGE 0x3e
171
172/* Log page response lengths */
173#define LOG_RESP_SELF_TEST_LEN 0x194
174
175/* See the SSC-2 document at www.t10.org . Earler note: From IBM
176Documentation, see http://www.storage.ibm.com/techsup/hddtech/prodspecs.htm */
177#define TAPE_ALERTS_LPAGE 0x2e
178
179/* ANSI SCSI-3 Mode Pages */
180#define VENDOR_UNIQUE_PAGE 0x00
181#define READ_WRITE_ERROR_RECOVERY_PAGE 0x01
182#define DISCONNECT_RECONNECT_PAGE 0x02
183#define FORMAT_DEVICE_PAGE 0x03
184#define RIGID_DISK_DRIVE_GEOMETRY_PAGE 0x04
185#define FLEXIBLE_DISK_PAGE 0x05
186#define VERIFY_ERROR_RECOVERY_PAGE 0x07
187#define CACHING_PAGE 0x08
188#define PERIPHERAL_DEVICE_PAGE 0x09
189#define XOR_CONTROL_MODE_PAGE 0x10
190#define CONTROL_MODE_PAGE 0x0a
191#define MEDIUM_TYPES_SUPPORTED_PAGE 0x0b
192#define NOTCH_PAGE 0x0c
193#define CD_DEVICE_PAGE 0x0d
194#define CD_AUDIO_CONTROL_PAGE 0x0e
195#define DATA_COMPRESSION_PAGE 0x0f
196#define ENCLOSURE_SERVICES_MANAGEMENT_PAGE 0x14
197#define PROTOCOL_SPECIFIC_LUN_PAGE 0x18
198#define PROTOCOL_SPECIFIC_PORT_PAGE 0x19
199#define POWER_CONDITION_PAGE 0x1a
200#define INFORMATIONAL_EXCEPTIONS_CONTROL_PAGE 0x1c
201#define FAULT_FAILURE_REPORTING_PAGE 0x1c
202
203#define ALL_MODE_PAGES 0x3f
204
205/* Mode page control field */
206#define MPAGE_CONTROL_CURRENT 0
207#define MPAGE_CONTROL_CHANGEABLE 1
208#define MPAGE_CONTROL_DEFAULT 2
209#define MPAGE_CONTROL_SAVED 3
210
211/* defines for useful SCSI Status codes */
212#define SCSI_STATUS_CHECK_CONDITION 0x2
213
214/* defines for useful Sense Key codes */
215#define SCSI_SK_NOT_READY 0x2
216#define SCSI_SK_MEDIUM_ERROR 0x3
217#define SCSI_SK_HARDWARE_ERROR 0x4
218#define SCSI_SK_ILLEGAL_REQUEST 0x5
219#define SCSI_SK_UNIT_ATTENTION 0x6
220
221/* defines for useful Additional Sense Codes (ASCs) */
222#define SCSI_ASC_NOT_READY 0x4 /* more info in ASCQ code */
223#define SCSI_ASC_NO_MEDIUM 0x3a /* more info in ASCQ code */
224#define SCSI_ASC_UNKNOWN_OPCODE 0x20
225#define SCSI_ASC_UNKNOWN_FIELD 0x24
226#define SCSI_ASC_UNKNOWN_PARAM 0x26
227#define SCSI_ASC_WARNING 0xb
228#define SCSI_ASC_IMPENDING_FAILURE 0x5d
229
230/* Simplified error code (negative values as per errno) */
231#define SIMPLE_NO_ERROR 0
232#define SIMPLE_ERR_NOT_READY 1
233#define SIMPLE_ERR_BAD_OPCODE 2
234#define SIMPLE_ERR_BAD_FIELD 3 /* in cbd */
235#define SIMPLE_ERR_BAD_PARAM 4 /* in data */
236#define SIMPLE_ERR_BAD_RESP 5 /* response fails sanity */
237#define SIMPLE_ERR_NO_MEDIUM 6 /* no medium present */
238#define SIMPLE_ERR_BECOMING_READY 7 /* device will be ready soon */
239#define SIMPLE_ERR_TRY_AGAIN 8 /* some warning, try again */
240#define SIMPLE_ERR_MEDIUM_HARDWARE 9 /* medium or hardware error */
241
242
243/* defines for functioncode parameter in SENDDIAGNOSTIC function */
244#define SCSI_DIAG_NO_SELF_TEST 0x00
245#define SCSI_DIAG_DEF_SELF_TEST 0xff
246#define SCSI_DIAG_BG_SHORT_SELF_TEST 0x01
247#define SCSI_DIAG_BG_EXTENDED_SELF_TEST 0x02
248#define SCSI_DIAG_FG_SHORT_SELF_TEST 0x05
249#define SCSI_DIAG_FG_EXTENDED_SELF_TEST 0x06
250#define SCSI_DIAG_ABORT_SELF_TEST 0x04
251
252
253/* SCSI command timeout values (units are seconds) */
254#define SCSI_TIMEOUT_DEFAULT 6 /* 6 seconds should be ample */
255#define SCSI_TIMEOUT_SELF_TEST (5 * 60 * 60) /* allow max 5 hours for */
256 /* extended foreground self test */
257
258
259
260#define LOGPAGEHDRSIZE 4
261
262void scsi_do_sense_disect(const struct scsi_cmnd_io * in,
263 struct scsi_sense_disect * out);
264
265const char * scsiErrString(int scsiErr);
266
267/* STANDARD SCSI Commands */
268int scsiTestUnitReady(int device);
269
270int scsiStdInquiry(int device, UINT8 *pBuf, int bufLen);
271
272int scsiInquiryVpd(int device, int vpd_page, UINT8 *pBuf, int bufLen);
273
274int scsiLogSense(int device, int pagenum, UINT8 *pBuf, int bufLen,
275 int known_resp_len);
276
277int scsiModeSense(int device, int pagenum, int pc, UINT8 *pBuf, int bufLen);
278
279int scsiModeSelect(int device, int sp, UINT8 *pBuf, int bufLen);
280
281int scsiModeSense10(int device, int pagenum, int pc, UINT8 *pBuf, int bufLen);
282
283int scsiModeSelect10(int device, int sp, UINT8 *pBuf, int bufLen);
284
285int scsiModePageOffset(const UINT8 * resp, int len, int modese_len);
286
287int scsiRequestSense(int device, struct scsi_sense_disect * sense_info);
288
289int scsiSendDiagnostic(int device, int functioncode, UINT8 *pBuf, int bufLen);
290
291int scsiReceiveDiagnostic(int device, int pcv, int pagenum, UINT8 *pBuf,
292 int bufLen);
293
294int scsiReadDefect10(int device, int req_plist, int req_glist, int dl_format,
295 UINT8 *pBuf, int bufLen);
296
297/* SMART specific commands */
298int scsiCheckIE(int device, int hasIELogPage, int hasTempLogPage, UINT8 *asc,
299 UINT8 *ascq, UINT8 *currenttemp, UINT8 *triptemp);
300
301int scsiFetchIECmpage(int device, struct scsi_iec_mode_page *iecp,
302 int modese_len);
303int scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page *iecp);
304int scsi_IsWarningEnabled(const struct scsi_iec_mode_page *iecp);
305int scsiSetExceptionControlAndWarning(int device, int enabled,
306 const struct scsi_iec_mode_page *iecp);
307void scsiDecodeErrCounterPage(unsigned char * resp,
308 struct scsiErrorCounter *ecp);
309void scsiDecodeNonMediumErrPage(unsigned char * resp,
310 struct scsiNonMediumError *nmep);
311int scsiFetchExtendedSelfTestTime(int device, int * durationSec,
312 int modese_len);
313int scsiCountFailedSelfTests(int fd, int noisy);
314int scsiSelfTestInProgress(int fd, int * inProgress);
315int scsiFetchControlGLTSD(int device, int modese_len, int current);
316int scsiSetControlGLTSD(int device, int enabled, int modese_len);
317int scsiFetchTransportProtocol(int device, int modese_len);
318
319/* T10 Standard IE Additional Sense Code strings taken from t10.org */
320
321const char* scsiGetIEString(UINT8 asc, UINT8 ascq);
322int scsiGetTemp(int device, UINT8 *currenttemp, UINT8 *triptemp);
323
324
325int scsiSmartIBMOfflineTest(int device);
326
327int scsiSmartDefaultSelfTest(int device);
328int scsiSmartShortSelfTest(int device);
329int scsiSmartExtendSelfTest(int device);
330int scsiSmartShortCapSelfTest(int device);
331int scsiSmartExtendCapSelfTest(int device);
332int scsiSmartSelfTestAbort(int device);
333
334const char * scsiTapeAlertsTapeDevice(unsigned short code);
335const char * scsiTapeAlertsChangerDevice(unsigned short code);
336
337const char * scsi_get_opcode_name(UINT8 opcode);
338void dStrHex(const char* str, int len, int no_ascii);
339
340/* SCSI command transmission interface function declaration. Its
341 * definition is target OS specific (see os_<OS>.c file).
342 * Returns 0 if SCSI command successfully launched and response
343 * received. Even when 0 is returned the caller should check
344 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
345 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
346 * (e.g. device not present or not a SCSI device) or some other problem
347 * arises (e.g. timeout) then returns a negative errno value. */
348int do_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report);
349
350
351/* This is Linux specific and will be generalized later. */
352int isLinuxLibAta(unsigned char * vpd_di_buff, int len);
353
354#endif
355