scsi: Update sense code handling
PATCH 1/1 scsi: Update sense code handling
The SCSI spec has a quite detailed list of sense codes available. It even mandates the use of specific ones for some failure cases. The current implementation just has one type of 'generic' error which is actually a violation of the spec in certain cases. This patch introduces various predefined sense codes to have the sense code reporting more in line with the spec.
Signed-off-by Hannes Reinecke <[email protected]>
--- hw/scsi-bus.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ hw/scsi-disk.c | 109 +++++++++++++++++++++++++++-------------------------- hw/scsi-generic.c | 76 ++++++++++++++++++++++++++----------- hw/scsi.h | 38 ++++++++++++++++++ 4 files changed, 239 insertions(+), 76 deletions(-)
hw/scsi-bus.c +92 -0
@@ -388,6 +388,98 @@ int scsi_req_parse(SCSIRequest *req, uint8_t *buf)return 0 ; } +/* +* Predefined sense codes +*/ + +/* No sense data available */ +const struct SCSISense sense_code_NO_SENSE = { + .key =NO_SENSE , .asc =0x00 , .ascq =0x00 +}; + +/* LUN not ready, Manual intervention required */ +const struct SCSISense sense_code_LUN_NOT_READY = { + .key =NOT_READY , .asc =0x04 , .ascq =0x03 +}; + +/* LUN not ready, Medium not present */ +const struct SCSISense sense_code_NO_MEDIUM = { + .key =NOT_READY , .asc =0x3a , .ascq =0x00 +}; + +/* Hardware error, internal target failure */ +const struct SCSISense sense_code_TARGET_FAILURE = { + .key =HARDWARE_ERROR , .asc =0x44 , .ascq =0x00 +}; + +/* Illegal request, invalid command operation code */ +const struct SCSISense sense_code_INVALID_OPCODE = { + .key =ILLEGAL_REQUEST , .asc =0x20 , .ascq =0x00 +}; + +/* Illegal request, LBA out of range */ +const struct SCSISense sense_code_LBA_OUT_OF_RANGE = { + .key =ILLEGAL_REQUEST , .asc =0x21 , .ascq =0x00 +}; + +/* Illegal request, Invalid field in CDB */ +const struct SCSISense sense_code_INVALID_FIELD = { + .key =ILLEGAL_REQUEST , .asc =0x24 , .ascq =0x00 +}; + +/* Illegal request, LUN not supported */ +const struct SCSISense sense_code_LUN_NOT_SUPPORTED = { + .key =ILLEGAL_REQUEST , .asc =0x25 , .ascq =0x00 +}; + +/* Command aborted, I/O process terminated */ +const struct SCSISense sense_code_IO_ERROR = { + .key =ABORTED_COMMAND , .asc =0x00 , .ascq =0x06 +}; + +/* Command aborted, I_T Nexus loss occurred */ +const struct SCSISense sense_code_I_T_NEXUS_LOSS = { + .key =ABORTED_COMMAND , .asc =0x29 , .ascq =0x07 +}; + +/* Command aborted, Logical Unit failure */ +const struct SCSISense sense_code_LUN_FAILURE = { + .key =ABORTED_COMMAND , .asc =0x3e , .ascq =0x01 +}; + +/* +* scsi_build_sense +* +* Build a sense buffer +*/ +int scsi_build_sense (SCSISense sense,uint8_t *buf,int len,int fixed) +{ +if (len <8 ) +return 0 ; +if (fixed && len <14 ) +return 0 ; + +memset (buf,0 , len); +if (fixed) { +/* Return fixed format sense buffer */ + buf[0 ] =0xf0 ; + buf[2 ] = sense.key; + buf[7 ] =7 ; + buf[12 ] = sense.asc; + buf[13 ] = sense.ascq; + len =14 ; + }else { +/* Return descriptor format sense buffer */ + buf[0 ] =0x72 ; + buf[1 ] = sense.key; + buf[2 ] = sense.asc; + buf[3 ] = sense.ascq; + len =8 ; + } + +return len; +} +static const char *scsi_command_name (uint8_t cmd) {static const char *names[] = {
hw/scsi-disk.c +56 -53
241 lines not on this page · raw message
hw/scsi-generic.c +53 -23
143 lines not on this page · raw message
hw/scsi.h +38 -0
@@ -26,6 +26,12 @@ enum SCSIXferMode { SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */ }; +typedef struct SCSISense { +uint8_t key; +uint8_t asc; +uint8_t ascq; +}SCSISense ; + typedef struct SCSIRequest { SCSIBus *bus; SCSIDevice *dev;
@@ -97,6 +103,38 @@ static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)SCSIDevice *scsi_bus_legacy_add_drive (SCSIBus *bus,BlockDriverState *bdrv,int unit);int scsi_bus_legacy_handle_cmdline (SCSIBus *bus); +/* +* Predefined sense codes +*/ + +/* No sense data available */ +extern const struct SCSISense sense_code_NO_SENSE; +/* LUN not ready, Manual intervention required */ +extern const struct SCSISense sense_code_LUN_NOT_READY; +/* LUN not ready, Medium not present */ +extern const struct SCSISense sense_code_NO_MEDIUM; +/* Hardware error, internal target failure */ +extern const struct SCSISense sense_code_TARGET_FAILURE; +/* Illegal request, invalid command operation code */ +extern const struct SCSISense sense_code_INVALID_OPCODE; +/* Illegal request, LBA out of range */ +extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE; +/* Illegal request, Invalid field in CDB */ +extern const struct SCSISense sense_code_INVALID_FIELD; +/* Illegal request, LUN not supported */ +extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED; +/* Command aborted, I/O process terminated */ +extern const struct SCSISense sense_code_IO_ERROR; +/* Command aborted, I_T Nexus loss occurred */ +extern const struct SCSISense sense_code_I_T_NEXUS_LOSS; +/* Command aborted, Logical Unit failure */ +extern const struct SCSISense sense_code_LUN_FAILURE; + +#define SENSE_CODE (x) sense_code_ ## x + +int scsi_build_sense (SCSISense sense,uint8_t *buf,int len,int fixed); +int scsi_sense_valid (SCSISense sense); +SCSIRequest *scsi_req_alloc (size_t size,SCSIDevice *d,uint32_t tag,uint32_t lun);SCSIRequest *scsi_req_find (SCSIDevice *d,uint32_t tag);void scsi_req_free (SCSIRequest *req);
Signature · 1 line
1.6.0.2