utils.h (5589B)
1#ifndef SCSI_UTILS_H 2#define SCSI_UTILS_H 3 4#ifdef CONFIG_LINUX 5#include <scsi/sg.h> 6#endif 7 8#define SCSI_CMD_BUF_SIZE 16 9#define SCSI_SENSE_LEN 18 10#define SCSI_SENSE_LEN_SCANNER 32 11#define SCSI_INQUIRY_LEN 36 12 13enum SCSIXferMode { 14 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */ 15 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */ 16 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */ 17}; 18 19enum SCSIHostStatus { 20 SCSI_HOST_OK, 21 SCSI_HOST_NO_LUN, 22 SCSI_HOST_BUSY, 23 SCSI_HOST_TIME_OUT, 24 SCSI_HOST_BAD_RESPONSE, 25 SCSI_HOST_ABORTED, 26 SCSI_HOST_ERROR = 0x07, 27 SCSI_HOST_RESET = 0x08, 28 SCSI_HOST_TRANSPORT_DISRUPTED = 0xe, 29 SCSI_HOST_TARGET_FAILURE = 0x10, 30 SCSI_HOST_RESERVATION_ERROR = 0x11, 31 SCSI_HOST_ALLOCATION_FAILURE = 0x12, 32 SCSI_HOST_MEDIUM_ERROR = 0x13, 33}; 34 35typedef struct SCSICommand { 36 uint8_t buf[SCSI_CMD_BUF_SIZE]; 37 int len; 38 size_t xfer; 39 uint64_t lba; 40 enum SCSIXferMode mode; 41} SCSICommand; 42 43typedef struct SCSISense { 44 uint8_t key; 45 uint8_t asc; 46 uint8_t ascq; 47} SCSISense; 48 49int scsi_build_sense(uint8_t *buf, SCSISense sense); 50SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len); 51int scsi_build_sense_buf(uint8_t *buf, size_t max_size, SCSISense sense, 52 bool fixed_sense); 53 54/* 55 * Predefined sense codes 56 */ 57 58/* No sense data available */ 59extern const struct SCSISense sense_code_NO_SENSE; 60/* LUN not ready, Manual intervention required */ 61extern const struct SCSISense sense_code_LUN_NOT_READY; 62/* LUN not ready, Medium not present */ 63extern const struct SCSISense sense_code_NO_MEDIUM; 64/* LUN not ready, medium removal prevented */ 65extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED; 66/* Hardware error, internal target failure */ 67extern const struct SCSISense sense_code_TARGET_FAILURE; 68/* Illegal request, invalid command operation code */ 69extern const struct SCSISense sense_code_INVALID_OPCODE; 70/* Illegal request, LBA out of range */ 71extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE; 72/* Illegal request, Invalid field in CDB */ 73extern const struct SCSISense sense_code_INVALID_FIELD; 74/* Illegal request, Invalid field in parameter list */ 75extern const struct SCSISense sense_code_INVALID_PARAM; 76/* Illegal request, Invalid value in parameter list */ 77extern const struct SCSISense sense_code_INVALID_PARAM_VALUE; 78/* Illegal request, Parameter list length error */ 79extern const struct SCSISense sense_code_INVALID_PARAM_LEN; 80/* Illegal request, LUN not supported */ 81extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED; 82/* Illegal request, Saving parameters not supported */ 83extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED; 84/* Illegal request, Incompatible format */ 85extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT; 86/* Illegal request, medium removal prevented */ 87extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED; 88/* Illegal request, Invalid Transfer Tag */ 89extern const struct SCSISense sense_code_INVALID_TAG; 90/* Command aborted, I/O process terminated */ 91extern const struct SCSISense sense_code_IO_ERROR; 92/* Command aborted, I_T Nexus loss occurred */ 93extern const struct SCSISense sense_code_I_T_NEXUS_LOSS; 94/* Command aborted, Logical Unit failure */ 95extern const struct SCSISense sense_code_LUN_FAILURE; 96/* Command aborted, LUN Communication failure */ 97extern const struct SCSISense sense_code_LUN_COMM_FAILURE; 98/* Command aborted, Overlapped Commands Attempted */ 99extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS; 100/* Medium error, Unrecovered read error */ 101extern const struct SCSISense sense_code_READ_ERROR; 102/* LUN not ready, Cause not reportable */ 103extern const struct SCSISense sense_code_NOT_READY; 104/* Unit attention, Capacity data has changed */ 105extern const struct SCSISense sense_code_CAPACITY_CHANGED; 106/* Unit attention, SCSI bus reset */ 107extern const struct SCSISense sense_code_SCSI_BUS_RESET; 108/* LUN not ready, Medium not present */ 109extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM; 110/* Unit attention, Power on, reset or bus device reset occurred */ 111extern const struct SCSISense sense_code_RESET; 112/* Unit attention, Medium may have changed*/ 113extern const struct SCSISense sense_code_MEDIUM_CHANGED; 114/* Unit attention, Reported LUNs data has changed */ 115extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED; 116/* Unit attention, Device internal reset */ 117extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET; 118/* Data Protection, Write Protected */ 119extern const struct SCSISense sense_code_WRITE_PROTECTED; 120/* Data Protection, Space Allocation Failed Write Protect */ 121extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED; 122 123#define SENSE_CODE(x) sense_code_ ## x 124 125int scsi_sense_to_errno(int key, int asc, int ascq); 126int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size); 127bool scsi_sense_buf_is_guest_recoverable(const uint8_t *sense, size_t sense_size); 128 129int scsi_convert_sense(uint8_t *in_buf, int in_len, 130 uint8_t *buf, int len, bool fixed); 131const char *scsi_command_name(uint8_t cmd); 132 133uint64_t scsi_cmd_lba(SCSICommand *cmd); 134uint32_t scsi_data_cdb_xfer(uint8_t *buf); 135uint32_t scsi_cdb_xfer(uint8_t *buf); 136int scsi_cdb_length(uint8_t *buf); 137 138/* Linux SG_IO interface. */ 139#ifdef CONFIG_LINUX 140#define SG_ERR_DRIVER_TIMEOUT 0x06 141#define SG_ERR_DRIVER_SENSE 0x08 142#endif 143 144int scsi_sense_from_errno(int errno_value, SCSISense *sense); 145int scsi_sense_from_host_status(uint8_t host_status, SCSISense *sense); 146 147#endif