#ifndef SCSI_UTILS_H
#define SCSI_UTILS_H
#ifdef CONFIG_LINUX
#include <scsi/sg.h>
#endif
#define SCSI_CMD_BUF_SIZE 16
#define SCSI_SENSE_LEN 18
#define SCSI_SENSE_LEN_SCANNER 32
#define SCSI_INQUIRY_LEN 36
enum SCSIXferMode {
SCSI_XFER_NONE,
SCSI_XFER_FROM_DEV,
SCSI_XFER_TO_DEV,
};
enum SCSIHostStatus {
SCSI_HOST_OK,
SCSI_HOST_NO_LUN,
SCSI_HOST_BUSY,
SCSI_HOST_TIME_OUT,
SCSI_HOST_BAD_RESPONSE,
SCSI_HOST_ABORTED,
SCSI_HOST_ERROR = 0x07,
SCSI_HOST_RESET = 0x08,
SCSI_HOST_TRANSPORT_DISRUPTED = 0xe,
SCSI_HOST_TARGET_FAILURE = 0x10,
SCSI_HOST_RESERVATION_ERROR = 0x11,
SCSI_HOST_ALLOCATION_FAILURE = 0x12,
SCSI_HOST_MEDIUM_ERROR = 0x13,
};
typedef struct SCSICommand {
uint8_t buf[SCSI_CMD_BUF_SIZE];
int len;
size_t xfer;
uint64_t lba;
enum SCSIXferMode mode;
} SCSICommand;
typedef struct SCSISense {
uint8_t key;
uint8_t asc;
uint8_t ascq;
} SCSISense;
int scsi_build_sense(uint8_t *buf, SCSISense sense);
SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len);
int scsi_build_sense_buf(uint8_t *buf, size_t max_size, SCSISense sense,
bool fixed_sense);
* Predefined sense codes
*/
extern const struct SCSISense sense_code_NO_SENSE;
extern const struct SCSISense sense_code_LUN_NOT_READY;
extern const struct SCSISense sense_code_NO_MEDIUM;
extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
extern const struct SCSISense sense_code_TARGET_FAILURE;
extern const struct SCSISense sense_code_INVALID_OPCODE;
extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
extern const struct SCSISense sense_code_INVALID_FIELD;
extern const struct SCSISense sense_code_INVALID_PARAM;
extern const struct SCSISense sense_code_INVALID_PARAM_VALUE;
extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
extern const struct SCSISense sense_code_INVALID_TAG;
extern const struct SCSISense sense_code_IO_ERROR;
extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
extern const struct SCSISense sense_code_LUN_FAILURE;
extern const struct SCSISense sense_code_LUN_COMM_FAILURE;
extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
extern const struct SCSISense sense_code_READ_ERROR;
extern const struct SCSISense sense_code_NOT_READY;
extern const struct SCSISense sense_code_CAPACITY_CHANGED;
extern const struct SCSISense sense_code_SCSI_BUS_RESET;
extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
extern const struct SCSISense sense_code_RESET;
extern const struct SCSISense sense_code_MEDIUM_CHANGED;
extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
extern const struct SCSISense sense_code_WRITE_PROTECTED;
extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
#define SENSE_CODE(x) sense_code_ ## x
int scsi_sense_to_errno(int key, int asc, int ascq);
int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size);
bool scsi_sense_buf_is_guest_recoverable(const uint8_t *sense, size_t sense_size);
int scsi_convert_sense(uint8_t *in_buf, int in_len,
uint8_t *buf, int len, bool fixed);
const char *scsi_command_name(uint8_t cmd);
uint64_t scsi_cmd_lba(SCSICommand *cmd);
uint32_t scsi_data_cdb_xfer(uint8_t *buf);
uint32_t scsi_cdb_xfer(uint8_t *buf);
int scsi_cdb_length(uint8_t *buf);
#ifdef CONFIG_LINUX
#define SG_ERR_DRIVER_TIMEOUT 0x06
#define SG_ERR_DRIVER_SENSE 0x08
#endif
int scsi_sense_from_errno(int errno_value, SCSISense *sense);
int scsi_sense_from_host_status(uint8_t host_status, SCSISense *sense);
#endif