switch (buf[0] >> 5) {
case 0:
return buf[4];
- break;
case 1:
case 2:
return lduw_be_p(&buf[7]);
- break;
case 4:
return ldl_be_p(&buf[10]) & 0xffffffffULL;
- break;
case 5:
return ldl_be_p(&buf[6]) & 0xffffffffULL;
- break;
default:
return -1;
}
return cdb_len;
}
+SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len)
+{
+ bool fixed_in;
+ SCSISense sense;
+
+ assert(in_len > 0);
+ fixed_in = (in_buf[0] & 2) == 0;
+ if (fixed_in) {
+ if (in_len < 14) {
+ return SENSE_CODE(IO_ERROR);
+ }
+ sense.key = in_buf[2];
+ sense.asc = in_buf[12];
+ sense.ascq = in_buf[13];
+ } else {
+ if (in_len < 4) {
+ return SENSE_CODE(IO_ERROR);
+ }
+ sense.key = in_buf[1];
+ sense.asc = in_buf[2];
+ sense.ascq = in_buf[3];
+ }
+
+ return sense;
+}
+
+int scsi_build_sense_buf(uint8_t *out_buf, size_t size, SCSISense sense,
+ bool fixed_sense)
+{
+ int len;
+ uint8_t buf[SCSI_SENSE_LEN] = { 0 };
+
+ if (fixed_sense) {
+ buf[0] = 0x70;
+ buf[2] = sense.key;
+ buf[7] = 10;
+ buf[12] = sense.asc;
+ buf[13] = sense.ascq;
+ len = 18;
+ } else {
+ buf[0] = 0x72;
+ buf[1] = sense.key;
+ buf[2] = sense.asc;
+ buf[3] = sense.ascq;
+ len = 8;
+ }
+ len = MIN(len, size);
+ memcpy(out_buf, buf, len);
+ return len;
+}
+
int scsi_build_sense(uint8_t *buf, SCSISense sense)
{
- memset(buf, 0, 18);
- buf[0] = 0x70;
- buf[2] = sense.key;
- buf[7] = 10;
- buf[12] = sense.asc;
- buf[13] = sense.ascq;
- return 18;
+ return scsi_build_sense_buf(buf, SCSI_SENSE_LEN, sense, true);
}
/*
.key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
};
+/* Illegal request, Invalid value in parameter list */
+const struct SCSISense sense_code_INVALID_PARAM_VALUE = {
+ .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x01
+};
+
/* Illegal request, Parameter list length error */
const struct SCSISense sense_code_INVALID_PARAM_LEN = {
.key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
.key = ABORTED_COMMAND, .asc = 0x08, .ascq = 0x00
};
+/* Command aborted, LUN does not respond to selection */
+const struct SCSISense sense_code_LUN_NOT_RESPONDING = {
+ .key = ABORTED_COMMAND, .asc = 0x05, .ascq = 0x00
+};
+
+/* Command aborted, Command Timeout during processing */
+const struct SCSISense sense_code_COMMAND_TIMEOUT = {
+ .key = ABORTED_COMMAND, .asc = 0x2e, .ascq = 0x02
+};
+
+/* Command aborted, Commands cleared by device server */
+const struct SCSISense sense_code_COMMAND_ABORTED = {
+ .key = ABORTED_COMMAND, .asc = 0x2f, .ascq = 0x02
+};
+
+/* Medium Error, Unrecovered read error */
+const struct SCSISense sense_code_READ_ERROR = {
+ .key = MEDIUM_ERROR, .asc = 0x11, .ascq = 0x00
+};
+
+/* Not ready, Cause not reportable */
+const struct SCSISense sense_code_NOT_READY = {
+ .key = NOT_READY, .asc = 0x04, .ascq = 0x00
+};
+
/* Unit attention, Capacity data has changed */
const struct SCSISense sense_code_CAPACITY_CHANGED = {
.key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
int scsi_convert_sense(uint8_t *in_buf, int in_len,
uint8_t *buf, int len, bool fixed)
{
- bool fixed_in;
SCSISense sense;
- if (!fixed && len < 8) {
- return 0;
- }
+ bool fixed_in;
if (in_len == 0) {
- sense.key = NO_SENSE;
- sense.asc = 0;
- sense.ascq = 0;
- } else {
- fixed_in = (in_buf[0] & 2) == 0;
+ return scsi_build_sense_buf(buf, len, SENSE_CODE(NO_SENSE), fixed);
+ }
- if (fixed == fixed_in) {
- memcpy(buf, in_buf, MIN(len, in_len));
- return MIN(len, in_len);
- }
+ fixed_in = (in_buf[0] & 2) == 0;
+ if (fixed == fixed_in) {
+ memcpy(buf, in_buf, MIN(len, in_len));
+ return MIN(len, in_len);
+ } else {
+ sense = scsi_parse_sense_buf(in_buf, in_len);
+ return scsi_build_sense_buf(buf, len, sense, fixed);
+ }
+}
- if (fixed_in) {
- sense.key = in_buf[2];
- sense.asc = in_buf[12];
- sense.ascq = in_buf[13];
- } else {
- sense.key = in_buf[1];
- sense.asc = in_buf[2];
- sense.ascq = in_buf[3];
- }
+static bool scsi_sense_is_guest_recoverable(int key, int asc, int ascq)
+{
+ switch (key) {
+ case NO_SENSE:
+ case RECOVERED_ERROR:
+ case UNIT_ATTENTION:
+ case ABORTED_COMMAND:
+ return true;
+ case NOT_READY:
+ case ILLEGAL_REQUEST:
+ case DATA_PROTECT:
+ /* Parse ASCQ */
+ break;
+ default:
+ return false;
}
- memset(buf, 0, len);
- if (fixed) {
- /* Return fixed format sense buffer */
- buf[0] = 0x70;
- buf[2] = sense.key;
- buf[7] = 10;
- buf[12] = sense.asc;
- buf[13] = sense.ascq;
- return MIN(len, SCSI_SENSE_LEN);
- } else {
- /* Return descriptor format sense buffer */
- buf[0] = 0x72;
- buf[1] = sense.key;
- buf[2] = sense.asc;
- buf[3] = sense.ascq;
- return 8;
+ switch ((asc << 8) | ascq) {
+ case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
+ case 0x2000: /* INVALID OPERATION CODE */
+ case 0x2400: /* INVALID FIELD IN CDB */
+ case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
+ case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
+
+ case 0x2104: /* UNALIGNED WRITE COMMAND */
+ case 0x2105: /* WRITE BOUNDARY VIOLATION */
+ case 0x2106: /* ATTEMPT TO READ INVALID DATA */
+ case 0x550e: /* INSUFFICIENT ZONE RESOURCES */
+
+ case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
+ case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
+ return true;
+ default:
+ return false;
}
}
int scsi_sense_to_errno(int key, int asc, int ascq)
{
switch (key) {
- case 0x00: /* NO SENSE */
- case 0x01: /* RECOVERED ERROR */
- case 0x06: /* UNIT ATTENTION */
- /* These sense keys are not errors */
- return 0;
- case 0x0b: /* COMMAND ABORTED */
+ case NO_SENSE:
+ case RECOVERED_ERROR:
+ case UNIT_ATTENTION:
+ return EAGAIN;
+ case ABORTED_COMMAND: /* COMMAND ABORTED */
return ECANCELED;
- case 0x02: /* NOT READY */
- case 0x05: /* ILLEGAL REQUEST */
- case 0x07: /* DATA PROTECTION */
+ case NOT_READY:
+ case ILLEGAL_REQUEST:
+ case DATA_PROTECT:
/* Parse ASCQ */
break;
default:
case 0x2700: /* WRITE PROTECTED */
return EACCES;
case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
- return EAGAIN;
+ return EINPROGRESS;
case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
return ENOTCONN;
default:
}
}
-int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size)
+int scsi_sense_buf_to_errno(const uint8_t *in_buf, size_t in_len)
{
- int key, asc, ascq;
- if (sense_size < 1) {
+ SCSISense sense;
+ if (in_len < 1) {
return EIO;
}
- switch (sense[0]) {
- case 0x70: /* Fixed format sense data. */
- if (sense_size < 14) {
- return EIO;
- }
- key = sense[2] & 0xF;
- asc = sense[12];
- ascq = sense[13];
- break;
- case 0x72: /* Descriptor format sense data. */
- if (sense_size < 4) {
- return EIO;
- }
- key = sense[1] & 0xF;
- asc = sense[2];
- ascq = sense[3];
- break;
- default:
- return EIO;
- break;
+
+ sense = scsi_parse_sense_buf(in_buf, in_len);
+ return scsi_sense_to_errno(sense.key, sense.asc, sense.ascq);
+}
+
+bool scsi_sense_buf_is_guest_recoverable(const uint8_t *in_buf, size_t in_len)
+{
+ SCSISense sense;
+ if (in_len < 1) {
+ return false;
}
- return scsi_sense_to_errno(key, asc, ascq);
+
+ sense = scsi_parse_sense_buf(in_buf, in_len);
+ return scsi_sense_is_guest_recoverable(sense.key, sense.asc, sense.ascq);
}
const char *scsi_command_name(uint8_t cmd)
return names[cmd];
}
+int scsi_sense_from_errno(int errno_value, SCSISense *sense)
+{
+ switch (errno_value) {
+ case 0:
+ return GOOD;
+ case EDOM:
+ return TASK_SET_FULL;
#ifdef CONFIG_LINUX
-int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
- SCSISense *sense)
+ /* These errno mapping are specific to Linux. For more information:
+ * - scsi_check_sense and scsi_decide_disposition in drivers/scsi/scsi_error.c
+ * - scsi_result_to_blk_status in drivers/scsi/scsi_lib.c
+ * - blk_errors[] in block/blk-core.c
+ */
+ case EBADE:
+ return RESERVATION_CONFLICT;
+ case ENODATA:
+ *sense = SENSE_CODE(READ_ERROR);
+ return CHECK_CONDITION;
+ case EREMOTEIO:
+ *sense = SENSE_CODE(TARGET_FAILURE);
+ return CHECK_CONDITION;
+#endif
+ case ENOMEDIUM:
+ *sense = SENSE_CODE(NO_MEDIUM);
+ return CHECK_CONDITION;
+ case ENOMEM:
+ *sense = SENSE_CODE(TARGET_FAILURE);
+ return CHECK_CONDITION;
+ case EINVAL:
+ *sense = SENSE_CODE(INVALID_FIELD);
+ return CHECK_CONDITION;
+ case ENOSPC:
+ *sense = SENSE_CODE(SPACE_ALLOC_FAILED);
+ return CHECK_CONDITION;
+ default:
+ *sense = SENSE_CODE(IO_ERROR);
+ return CHECK_CONDITION;
+ }
+}
+
+int scsi_sense_from_host_status(uint8_t host_status,
+ SCSISense *sense)
{
- if (errno_value != 0) {
- switch (errno_value) {
- case EDOM:
- return TASK_SET_FULL;
- case ENOMEM:
- *sense = SENSE_CODE(TARGET_FAILURE);
- return CHECK_CONDITION;
- default:
- *sense = SENSE_CODE(IO_ERROR);
- return CHECK_CONDITION;
- }
- } else {
- if (io_hdr->host_status == SG_ERR_DID_NO_CONNECT ||
- io_hdr->host_status == SG_ERR_DID_BUS_BUSY ||
- io_hdr->host_status == SG_ERR_DID_TIME_OUT ||
- (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT)) {
- return BUSY;
- } else if (io_hdr->host_status) {
- *sense = SENSE_CODE(I_T_NEXUS_LOSS);
- return CHECK_CONDITION;
- } else if (io_hdr->status) {
- return io_hdr->status;
- } else if (io_hdr->driver_status & SG_ERR_DRIVER_SENSE) {
- return CHECK_CONDITION;
- } else {
- return GOOD;
- }
+ switch (host_status) {
+ case SCSI_HOST_NO_LUN:
+ *sense = SENSE_CODE(LUN_NOT_RESPONDING);
+ return CHECK_CONDITION;
+ case SCSI_HOST_BUSY:
+ return BUSY;
+ case SCSI_HOST_TIME_OUT:
+ *sense = SENSE_CODE(COMMAND_TIMEOUT);
+ return CHECK_CONDITION;
+ case SCSI_HOST_BAD_RESPONSE:
+ *sense = SENSE_CODE(LUN_COMM_FAILURE);
+ return CHECK_CONDITION;
+ case SCSI_HOST_ABORTED:
+ *sense = SENSE_CODE(COMMAND_ABORTED);
+ return CHECK_CONDITION;
+ case SCSI_HOST_RESET:
+ *sense = SENSE_CODE(RESET);
+ return CHECK_CONDITION;
+ case SCSI_HOST_TRANSPORT_DISRUPTED:
+ *sense = SENSE_CODE(I_T_NEXUS_LOSS);
+ return CHECK_CONDITION;
+ case SCSI_HOST_TARGET_FAILURE:
+ *sense = SENSE_CODE(TARGET_FAILURE);
+ return CHECK_CONDITION;
+ case SCSI_HOST_RESERVATION_ERROR:
+ return RESERVATION_CONFLICT;
+ case SCSI_HOST_ALLOCATION_FAILURE:
+ *sense = SENSE_CODE(SPACE_ALLOC_FAILED);
+ return CHECK_CONDITION;
+ case SCSI_HOST_MEDIUM_ERROR:
+ *sense = SENSE_CODE(READ_ERROR);
+ return CHECK_CONDITION;
}
+ return GOOD;
}
-#endif