*
* Copyright (C) 2002-8 Bruce Allen
* Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
- * Copyright (C) 2003-15 Douglas Gilbert <dgilbert@interlog.com>
+ * Copyright (C) 2003-18 Douglas Gilbert <dgilbert@interlog.com>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
+ * SPDX-License-Identifier: GPL-2.0-or-later
*
- * You should have received a copy of the GNU General Public License
- * (for example COPYING); if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * This code was originally developed as a Senior Thesis by Michael Cornwell
- * at the Concurrent Systems Laboratory (now part of the Storage Systems
- * Research Center), Jack Baskin School of Engineering, University of
- * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
*
* N.B. What was formerly known as "SMART" are now called "informational
* exceptions" in recent t10.org drafts (i.e. recent SCSI).
#ifndef SCSICMDS_H_
#define SCSICMDS_H_
-#define SCSICMDS_H_CVSID "$Id: scsicmds.h 4557 2017-10-19 19:17:29Z samm2 $\n"
+#define SCSICMDS_H_CVSID "$Id: scsicmds.h 4842 2018-12-02 16:07:26Z chrfranke $\n"
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
/* #define SCSI_DEBUG 1 */ /* Comment out to disable command debugging */
#define SAT_ATA_PASSTHROUGH_16 0x85
#endif
-typedef unsigned char UINT8;
-typedef signed char INT8;
-typedef unsigned int UINT32;
-typedef int INT32;
#define DXFER_NONE 0
#define DXFER_FROM_DEVICE 1
struct scsi_cmnd_io
{
- UINT8 * cmnd; /* [in]: ptr to SCSI command block (cdb) */
+ uint8_t * cmnd; /* [in]: ptr to SCSI command block (cdb) */
size_t cmnd_len; /* [in]: number of bytes in SCSI command */
int dxfer_dir; /* [in]: DXFER_NONE, DXFER_FROM_DEVICE, or
DXFER_TO_DEVICE */
- UINT8 * dxferp; /* [in]: ptr to outgoing or incoming data buffer */
+ uint8_t * dxferp; /* [in]: ptr to outgoing or incoming data buffer */
size_t dxfer_len; /* [in]: bytes to be transferred to/from dxferp */
- UINT8 * sensep; /* [in]: ptr to sense buffer, filled when
+ uint8_t * sensep; /* [in]: ptr to sense buffer, filled when
CHECK CONDITION status occurs */
size_t max_sense_len; /* [in]: max number of bytes to write to sensep */
unsigned timeout; /* [in]: seconds, 0-> default timeout (60 seconds?) */
size_t resp_sense_len; /* [out]: sense buffer length written */
- UINT8 scsi_status; /* [out]: 0->ok, 2->CHECK CONDITION, etc ... */
+ uint8_t scsi_status; /* [out]: 0->ok, 2->CHECK CONDITION, etc ... */
int resid; /* [out]: Number of bytes requested to be transferred
less actual number transferred (0 if not
supported) */
};
struct scsi_sense_disect {
- UINT8 resp_code;
- UINT8 sense_key;
- UINT8 asc;
- UINT8 ascq;
+ uint8_t resp_code;
+ uint8_t sense_key;
+ uint8_t asc;
+ uint8_t ascq;
int progress; /* -1 -> N/A, 0-65535 -> available */
};
/* Useful data from Informational Exception Control mode page (0x1c) */
#define SCSI_IECMP_RAW_LEN 64
struct scsi_iec_mode_page {
- UINT8 requestedCurrent;
- UINT8 gotCurrent;
- UINT8 requestedChangeable;
- UINT8 gotChangeable;
- UINT8 modese_len; /* 0 (don't know), 6 or 10 */
- UINT8 raw_curr[SCSI_IECMP_RAW_LEN];
- UINT8 raw_chg[SCSI_IECMP_RAW_LEN];
+ uint8_t requestedCurrent;
+ uint8_t gotCurrent;
+ uint8_t requestedChangeable;
+ uint8_t gotChangeable;
+ uint8_t modese_len; /* 0 (don't know), 6 or 10 */
+ uint8_t raw_curr[SCSI_IECMP_RAW_LEN];
+ uint8_t raw_chg[SCSI_IECMP_RAW_LEN];
};
/* Carrier for Error counter log pages (e.g. read, write, verify ...) */
struct scsiErrorCounter {
- UINT8 gotPC[7];
- UINT8 gotExtraPC;
+ uint8_t gotPC[7];
+ uint8_t gotExtraPC;
uint64_t counter[8];
};
/* Carrier for Non-medium error log page */
struct scsiNonMediumError {
- UINT8 gotPC0;
- UINT8 gotExtraPC;
+ uint8_t gotPC0;
+ uint8_t gotExtraPC;
uint64_t counterPC0;
- UINT8 gotTFE_H;
+ uint8_t gotTFE_H;
uint64_t counterTFE_H; /* Track following errors [Hitachi] */
- UINT8 gotPE_H;
+ uint8_t gotPE_H;
uint64_t counterPE_H; /* Positioning errors [Hitachi] */
};
+struct scsi_readcap_resp {
+ uint64_t num_lblocks; /* Number of Logical Blocks on device */
+ uint32_t lb_size; /* should be available in all non-error cases */
+ /* following fields from READ CAPACITY(16) or set to 0 */
+ uint8_t prot_type; /* 0, 1, 2 or 3 protection type, deduced from
+ * READ CAPACITY(16) P_TYPE and PROT_EN fields */
+ uint8_t p_i_exp; /* Protection information Intervals Exponent */
+ uint8_t lb_p_pb_exp;/* Logical Blocks per Physical Block Exponent */
+ bool lbpme; /* Logical Block Provisioning Management Enabled */
+ bool lbprz; /* Logical Block Provisioning Read Zeros */
+ uint16_t l_a_lba; /* Lowest Aligned Logical Block Address */
+};
+
/* SCSI Peripheral types (of interest) */
#define SCSI_PT_DIRECT_ACCESS 0x0
#define SCSI_PT_SEQUENTIAL_ACCESS 0x1
#define SCSI_PT_CDROM 0x5
#define SCSI_PT_MEDIUM_CHANGER 0x8
#define SCSI_PT_ENCLOSURE 0xd
-
-/* ANSI SCSI-3 Log Pages retrieved by LOG SENSE. */
-#define SUPPORTED_LPAGES 0x00
-#define BUFFER_OVERRUN_LPAGE 0x01
-#define WRITE_ERROR_COUNTER_LPAGE 0x02
-#define READ_ERROR_COUNTER_LPAGE 0x03
-#define READ_REVERSE_ERROR_COUNTER_LPAGE 0x04
-#define VERIFY_ERROR_COUNTER_LPAGE 0x05
-#define NON_MEDIUM_ERROR_LPAGE 0x06
-#define LAST_N_ERROR_LPAGE 0x07
-#define FORMAT_STATUS_LPAGE 0x08
-#define LB_PROV_LPAGE 0x0c /* SBC-3 */
-#define TEMPERATURE_LPAGE 0x0d
-#define STARTSTOP_CYCLE_COUNTER_LPAGE 0x0e
-#define APPLICATION_CLIENT_LPAGE 0x0f
-#define SELFTEST_RESULTS_LPAGE 0x10
-#define SS_MEDIA_LPAGE 0x11 /* SBC-3 */
-#define BACKGROUND_RESULTS_LPAGE 0x15 /* SBC-3 */
-#define NONVOL_CACHE_LPAGE 0x17 /* SBC-3 */
-#define PROTOCOL_SPECIFIC_LPAGE 0x18
-#define IE_LPAGE 0x2f
+#define SCSI_PT_HOST_MANAGED 0x14
+
+/* Transport protocol identifiers or just Protocol identifiers */
+#define SCSI_TPROTO_FCP 0
+#define SCSI_TPROTO_SPI 1
+#define SCSI_TPROTO_SSA 2
+#define SCSI_TPROTO_1394 3
+#define SCSI_TPROTO_SRP 4 /* SCSI over RDMA */
+#define SCSI_TPROTO_ISCSI 5
+#define SCSI_TPROTO_SAS 6
+#define SCSI_TPROTO_ADT 7
+#define SCSI_TPROTO_ATA 8
+#define SCSI_TPROTO_UAS 9 /* USB attached SCSI */
+#define SCSI_TPROTO_SOP 0xa /* SCSI over PCIe */
+#define SCSI_TPROTO_PCIE 0xb /* includes NVMe */
+#define SCSI_TPROTO_NONE 0xf
+
+
+/* SCSI Log Pages retrieved by LOG SENSE. 0x0 to 0x3f, 0x30 to 0x3e vendor */
+#define SUPPORTED_LPAGES 0x00
+#define BUFFER_OVERRUN_LPAGE 0x01
+#define WRITE_ERROR_COUNTER_LPAGE 0x02
+#define READ_ERROR_COUNTER_LPAGE 0x03
+#define READ_REVERSE_ERROR_COUNTER_LPAGE 0x04
+#define VERIFY_ERROR_COUNTER_LPAGE 0x05
+#define NON_MEDIUM_ERROR_LPAGE 0x06
+#define LAST_N_ERROR_EVENTS_LPAGE 0x07
+#define FORMAT_STATUS_LPAGE 0x08
+#define LAST_N_DEFERRED_LPAGE 0x0b /* or async events */
+#define LB_PROV_LPAGE 0x0c /* SBC-3 */
+#define TEMPERATURE_LPAGE 0x0d
+#define STARTSTOP_CYCLE_COUNTER_LPAGE 0x0e
+#define APPLICATION_CLIENT_LPAGE 0x0f
+#define SELFTEST_RESULTS_LPAGE 0x10
+#define SS_MEDIA_LPAGE 0x11 /* SBC-3 */
+#define BACKGROUND_RESULTS_LPAGE 0x15 /* SBC-3 */
+#define ATA_PT_RESULTS_LPAGE 0x16 /* SAT */
+#define NONVOL_CACHE_LPAGE 0x17 /* SBC-3 */
+#define PROTOCOL_SPECIFIC_LPAGE 0x18
+#define GEN_STATS_PERF_LPAGE 0x19
+#define POWER_COND_TRANS_LPAGE 0x1a
+#define IE_LPAGE 0x2f
+
+/* SCSI Log subpages (8 bits), added spc4r05 2006, standardized SPC-4 2015 */
+#define NO_SUBPAGE_L_SPAGE 0x0 /* 0x0-0x3f,0x0 */
+#define LAST_N_INQ_DAT_L_SPAGE 0x1 /* 0xb,0x1 */
+#define LAST_N_MODE_PG_L_SPAGE 0x2 /* 0xb,0x2 */
+#define ENVIRO_REP_L_SPAGE 0x1 /* 0xd,0x1 */
+#define ENVIRO_LIMITS_L_SPAGE 0x2 /* 0xd,0x2 */
+#define UTILIZATION_L_SPAGE 0x1 /* 0xe,0x1 */
+#define ZB_DEV_STATS_L_SPAGE 0x1 /* 0x14,0x1 */
+#define PEND_DEFECTS_L_SPAGE 0x1 /* 0x15,0x1 */
+#define BACKGROUND_OP_L_SPAGE 0x2 /* 0x15,0x2 */
+#define LPS_MISALIGN_L_SPAGE 0x3 /* 0x15,0x3 */
+#define SUPP_SPAGE_L_SPAGE 0xff /* 0x0,0xff pages+subpages */
/* Seagate vendor specific log pages. */
-#define SEAGATE_CACHE_LPAGE 0x37
-#define SEAGATE_FACTORY_LPAGE 0x3e
+#define SEAGATE_CACHE_LPAGE 0x37
+#define SEAGATE_FACTORY_LPAGE 0x3e
/* Log page response lengths */
#define LOG_RESP_SELF_TEST_LEN 0x194
-/* See the SSC-2 document at www.t10.org . Earler note: From IBM
+/* See the SSC-2 document at www.t10.org . Earlier note: From IBM
Documentation, see http://www.storage.ibm.com/techsup/hddtech/prodspecs.htm */
#define TAPE_ALERTS_LPAGE 0x2e
#define SIMPLE_ERR_TRY_AGAIN 8 /* some warning, try again */
#define SIMPLE_ERR_MEDIUM_HARDWARE 9 /* medium or hardware error */
#define SIMPLE_ERR_UNKNOWN 10 /* unknown sense value */
-#define SIMPLE_ERR_ABORTED_COMMAND 11 /* most likely transport error */
+#define SIMPLE_ERR_ABORTED_COMMAND 11 /* probably transport error */
/* defines for functioncode parameter in SENDDIAGNOSTIC function */
extern supported_vpd_pages * supported_vpd_pages_p;
+/* This is a heuristic that takes into account the command bytes and length
+ * to decide whether the presented unstructured sequence of bytes could be
+ * a SCSI command. If so it returns true otherwise false. Vendor specific
+ * SCSI commands (i.e. opcodes from 0xc0 to 0xff), if presented, are assumed
+ * to follow SCSI conventions (i.e. length of 6, 10, 12 or 16 bytes). The
+ * only SCSI commands considered above 16 bytes of length are the Variable
+ * Length Commands (opcode 0x7f) and the XCDB wrapped commands (opcode 0x7e).
+ * Both have an inbuilt length field which can be cross checked with clen.
+ * No NVMe commands (64 bytes long plus some extra added by some OSes) have
+ * opcodes 0x7e or 0x7f yet. ATA is register based but SATA has FIS
+ * structures that are sent across the wire. The FIS register structure is
+ * used to move a command from a SATA host to device, but the ATA 'command'
+ * is not the first byte. So it is harder to say what will happen if a
+ * FIS structure is presented as a SCSI command, hopefully there is a low
+ * probability this function will yield true in that case. */
+bool is_scsi_cdb(const uint8_t * cdbp, int clen);
// Print SCSI debug messages?
extern unsigned char scsi_debugmode;
/* STANDARD SCSI Commands */
int scsiTestUnitReady(scsi_device * device);
-int scsiStdInquiry(scsi_device * device, UINT8 *pBuf, int bufLen);
+int scsiStdInquiry(scsi_device * device, uint8_t *pBuf, int bufLen);
-int scsiInquiryVpd(scsi_device * device, int vpd_page, UINT8 *pBuf, int bufLen);
+int scsiInquiryVpd(scsi_device * device, int vpd_page, uint8_t *pBuf,
+ int bufLen);
-int scsiLogSense(scsi_device * device, int pagenum, int subpagenum, UINT8 *pBuf,
- int bufLen, int known_resp_len);
+int scsiLogSense(scsi_device * device, int pagenum, int subpagenum,
+ uint8_t *pBuf, int bufLen, int known_resp_len);
int scsiLogSelect(scsi_device * device, int pcr, int sp, int pc, int pagenum,
- int subpagenum, UINT8 *pBuf, int bufLen);
+ int subpagenum, uint8_t *pBuf, int bufLen);
int scsiModeSense(scsi_device * device, int pagenum, int subpagenum, int pc,
- UINT8 *pBuf, int bufLen);
+ uint8_t *pBuf, int bufLen);
-int scsiModeSelect(scsi_device * device, int sp, UINT8 *pBuf, int bufLen);
+int scsiModeSelect(scsi_device * device, int sp, uint8_t *pBuf, int bufLen);
int scsiModeSense10(scsi_device * device, int pagenum, int subpagenum, int pc,
- UINT8 *pBuf, int bufLen);
+ uint8_t *pBuf, int bufLen);
-int scsiModeSelect10(scsi_device * device, int sp, UINT8 *pBuf, int bufLen);
+int scsiModeSelect10(scsi_device * device, int sp, uint8_t *pBuf, int bufLen);
-int scsiModePageOffset(const UINT8 * resp, int len, int modese_len);
+int scsiModePageOffset(const uint8_t * resp, int len, int modese_len);
-int scsiRequestSense(scsi_device * device, struct scsi_sense_disect * sense_info);
+int scsiRequestSense(scsi_device * device,
+ struct scsi_sense_disect * sense_info);
-int scsiSendDiagnostic(scsi_device * device, int functioncode, UINT8 *pBuf, int bufLen);
+int scsiSendDiagnostic(scsi_device * device, int functioncode, uint8_t *pBuf,
+ int bufLen);
-int scsiReadDefect10(scsi_device * device, int req_plist, int req_glist, int dl_format,
- UINT8 *pBuf, int bufLen);
+int scsiReadDefect10(scsi_device * device, int req_plist, int req_glist,
+ int dl_format, uint8_t *pBuf, int bufLen);
int scsiReadDefect12(scsi_device * device, int req_plist, int req_glist,
- int dl_format, int addrDescIndex, UINT8 *pBuf, int bufLen);
+ int dl_format, int addrDescIndex, uint8_t *pBuf,
+ int bufLen);
int scsiReadCapacity10(scsi_device * device, unsigned int * last_lbp,
unsigned int * lb_sizep);
-int scsiReadCapacity16(scsi_device * device, UINT8 *pBuf, int bufLen);
+int scsiReadCapacity16(scsi_device * device, uint8_t *pBuf, int bufLen);
/* SMART specific commands */
-int scsiCheckIE(scsi_device * device, int hasIELogPage, int hasTempLogPage, UINT8 *asc,
- UINT8 *ascq, UINT8 *currenttemp, UINT8 *triptemp);
+int scsiCheckIE(scsi_device * device, int hasIELogPage, int hasTempLogPage,
+ uint8_t *asc, uint8_t *ascq, uint8_t *currenttemp,
+ uint8_t *triptemp);
int scsiFetchIECmpage(scsi_device * device, struct scsi_iec_mode_page *iecp,
int modese_len);
int * haw_zbcp);
int scsiGetSetCache(scsi_device * device, int modese_len, short int * wce,
short int * rcd);
-uint64_t scsiGetSize(scsi_device * device, unsigned int * lb_sizep,
- int * lb_per_pb_expp);
-int scsiGetProtPBInfo(scsi_device * device, unsigned char * rc16_12_31p);
+uint64_t scsiGetSize(scsi_device * device, bool avoid_rcap16,
+ struct scsi_readcap_resp * srrp);
/* T10 Standard IE Additional Sense Code strings taken from t10.org */
-const char* scsiGetIEString(UINT8 asc, UINT8 ascq);
-int scsiGetTemp(scsi_device * device, UINT8 *currenttemp, UINT8 *triptemp);
+const char* scsiGetIEString(uint8_t asc, uint8_t ascq);
+int scsiGetTemp(scsi_device * device, uint8_t *currenttemp, uint8_t *triptemp);
int scsiSmartDefaultSelfTest(scsi_device * device);
const char * scsiTapeAlertsTapeDevice(unsigned short code);
const char * scsiTapeAlertsChangerDevice(unsigned short code);
-const char * scsi_get_opcode_name(UINT8 opcode);
-void scsi_format_id_string(char * out, const unsigned char * in, int n);
+const char * scsi_get_opcode_name(uint8_t opcode);
+void scsi_format_id_string(char * out, const uint8_t * in, int n);
-void dStrHex(const char* str, int len, int no_ascii);
-inline void dStrHex(const unsigned char* str, int len, int no_ascii)
- { dStrHex((const char *)str, len, no_ascii); }
+void dStrHex(const uint8_t * up, int len, int no_ascii);
/* Attempt to find the first SCSI sense data descriptor that matches the
given 'desc_type'. If found return pointer to start of sense data
descriptor; otherwise (including fixed format sense data) returns NULL. */
const unsigned char * sg_scsi_sense_desc_find(const unsigned char * sensep,
- int sense_len, int desc_type);
+ int sense_len, int desc_type);
/* SCSI command transmission interface function declaration. Its