scsi_command_complete() sends invalid values
PATCH 1/1 scsi_command_complete() sends invalid values
Hi,
it seems there are some inconsistencies between hw/lsi53c895a.c, hw/scsi-disk.c and what linux driver is waiting (I found this by tracing the linux driver).
The sense value sent by qemu/hw/scsi-disk.c in:
static void scsi_command_complete(SCSIRequest *r, int sense)
{
SCSIDeviceState *s = r->dev;
uint32_t tag;
DPRINTF("Command complete tag=0x%x sense=%d\n", r->tag, sense);
s->sense = sense;
tag = r->tag;
scsi_remove_request(r);
s->completion(s->opaque, SCSI_REASON_DONE, tag, sense);
}is decoded by linux/drivers/scsi/sym53c8xx_2/sym_hipd.c, sym_sir_bad_scsi_status(), in s_status, and must be one of these values:
drivers/scsi/sym53c8xx_2/sym_defs.h
#define S_GOOD SAM_STAT_GOOD #define S_CHECK_COND SAM_STAT_CHECK_CONDITION #define S_COND_MET SAM_STAT_CONDITION_MET #define S_BUSY SAM_STAT_BUSY #define S_INT SAM_STAT_INTERMEDIATE #define S_INT_COND_MET SAM_STAT_INTERMEDIATE_CONDITION_MET #define S_CONFLICT SAM_STAT_RESERVATION_CONFLICT #define S_TERMINATED SAM_STAT_COMMAND_TERMINATED #define S_QUEUE_FULL SAM_STAT_TASK_SET_FULL #define S_ILLEGAL (0xff)
The attached patch is a try to correct this.
It corrects also hw/scsi-generic.c, allowing to use commands like "mt eod" or "mt fsf 4" on a SCSI stream device (tape).
Regards,
Laurent
Signature · 3 lines
----------------- [email protected] ------------------ "Programmers who subconsciously view themselves as artists will enjoy what they do and will do it better." D. Knuth
--- hw/scsi-disk.c | 28 ++++++++++++++-------------- hw/scsi-generic.c | 8 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-)
hw/scsi-generic.c +4 -4
@@ -158,15 +158,15 @@ static void scsi_command_complete(void * s->driver_status = r->io_header.driver_status;if (ret !=0 ) - sense =HARDWARE_ERROR ; + sense =CHECK_CONDITION ;else {if (s->driver_status &SG_ERR_DRIVER_TIMEOUT ) { - sense =HARDWARE_ERROR ; + sense =CHECK_CONDITION ;BADF ("Driver Timeout\n" ); }else if ((s->driver_status &SG_ERR_DRIVER_SENSE ) ==0 ) - sense =NO_SENSE ; + sense =GOOD ;else - sense = s->sensebuf[2 ]; + sense =CHECK_CONDITION ; }DPRINTF ("Command complete 0x%p tag=0x%x sense=%d\n" , r, r->tag, sense);
hw/scsi-disk.c +14 -14
@@ -29,10 +29,10 @@ do { fprintf(stderr, "scsi-disk: " fmt ,#include "block.h" #include "scsi-disk.h" -#define SENSE_NO_SENSE 0 -#define SENSE_NOT_READY 2 -#define SENSE_HARDWARE_ERROR 4 -#define SENSE_ILLEGAL_REQUEST 5 +#define STATUS_GOOD 0 +#define STATUS_CHECK_CONDITION 1 +#define STATUS_BUSY 4 +#define STATUS_ILLEGAL (0xff)#define SCSI_DMA_BUF_SIZE 131072
@@ -157,7 +157,7 @@ static void scsi_read_complete(void * opif (ret) {DPRINTF ("IO error\n" ); -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION );return ; }DPRINTF ("Data ready tag=0x%x len=%d\n" , r->tag, r->buf_len);
@@ -176,7 +176,7 @@ static void scsi_read_data(SCSIDevice *dif (!r) {BADF ("Bad read tag 0x%x\n" , tag);/* ??? This is the wrong error. */ -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION );return ; }if (r->sector_count == (uint32_t )-1 ) {
@@ -187,7 +187,7 @@ static void scsi_read_data(SCSIDevice *d }DPRINTF ("Read sector_count=%d\n" , r->sector_count);if (r->sector_count ==0 ) { -scsi_command_complete (r,SENSE_NO_SENSE ); +scsi_command_complete (r,STATUS_GOOD );return ; }
@@ -199,7 +199,7 @@ static void scsi_read_data(SCSIDevice *d r->aiocb =bdrv_aio_read (s->bdrv, r->sector, r->dma_buf, n, scsi_read_complete, r);if (r->aiocb ==NULL ) -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ); r->sector += n; r->sector_count -= n; }
@@ -217,7 +217,7 @@ static void scsi_write_complete(void * o r->aiocb =NULL ;if (r->sector_count ==0 ) { -scsi_command_complete (r,SENSE_NO_SENSE ); +scsi_command_complete (r,STATUS_GOOD ); }else { len = r->sector_count *512 ;if (len >SCSI_DMA_BUF_SIZE ) {
@@ -241,7 +241,7 @@ static int scsi_write_data(SCSIDevice *d r =scsi_find_request (s, tag);if (!r) {BADF ("Bad write tag 0x%x\n" , tag); -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION );return 1 ; }if (r->aiocb)
@@ -251,7 +251,7 @@ static int scsi_write_data(SCSIDevice *d r->aiocb =bdrv_aio_write (s->bdrv, r->sector, r->dma_buf, n, scsi_write_complete, r);if (r->aiocb ==NULL ) -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ); r->sector += n; r->sector_count -= n; }else {
@@ -670,7 +670,7 @@ static int32_t scsi_send_command(SCSIDev outbuf[7 ] =0 ; r->buf_len =8 ; }else { -scsi_command_complete (r,SENSE_NOT_READY ); +scsi_command_complete (r,STATUS_BUSY );return 0 ; }break ;
@@ -757,11 +757,11 @@ static int32_t scsi_send_command(SCSIDevdefault :DPRINTF ("Unknown SCSI command (%2.2x)\n" , buf[0 ]); fail: -scsi_command_complete (r,SENSE_ILLEGAL_REQUEST ); +scsi_command_complete (r,STATUS_ILLEGAL );return 0 ; }if (r->sector_count ==0 && r->buf_len ==0 ) { -scsi_command_complete (r,SENSE_NO_SENSE ); +scsi_command_complete (r,STATUS_GOOD ); } len = r->sector_count *512 + r->buf_len;if (is_write) {
Quoted 6 lines · Laurent Vivier
Hi,
it seems there are some inconsistencies between
hw/lsi53c895a.c,hw/scsi-disk.cand what linux driver is waiting (I found this by tracing the linux driver).
Have you also tested the Windows driver with your changes?
Regards,
Anthony Liguori
Quoted 9 lines · Anthony Liguori
Quoted 6 lines · Laurent Vivier
Hi,
it seems there are some inconsistencies between
hw/lsi53c895a.c,hw/scsi-disk.cand what linux driver is waiting (I found this by tracing the linux driver).Have you also tested the Windows driver with your changes?
It was not, and now it is, it reveals another problem in scsi-disk.c:
we can't format disk from windows.
When windows formats a disk, it sends a "VERIFY" (0x2f) command to the disk, but scsi-disk.c doesn't emulate it. Previously, this command was ignored because scsi_command_complete() didn't send the good status, but with my patch the error is detected by the driver and windows guesses it is not able to format the disk.
I'm able to format a disk with windows by modifying scsi-disk.c to simply ignore "VERIFY" (which was the original behavior).
Moreover, I have modified my patch to store the sense code.
Paul, am I wrong ?
Find attached an updated patch (tested with windows XP and linux).
Regards,
Laurent
Signature · 3 lines
----------------- [email protected] ------------------ "La perfection est atteinte non quand il ne reste rien à ajouter mais quand il ne reste rien à enlever." Saint Exupéry
--- hw/scsi-disk.c | 31 +++++++++++++++++++------------ hw/scsi-generic.c | 8 ++++---- 2 files changed, 23 insertions(+), 16 deletions(-)
hw/scsi-generic.c +7 -7
@@ -154,25 +154,25 @@ static void scsi_command_complete(void *SCSIRequest *r = (SCSIRequest *)opaque;SCSIDeviceState *s = r->dev;uint32_t tag; -int sense; +int status; s->driver_status = r->io_header.driver_status;if (ret !=0 ) - sense =HARDWARE_ERROR ; + status =CHECK_CONDITION ;else {if (s->driver_status &SG_ERR_DRIVER_TIMEOUT ) { - sense =HARDWARE_ERROR ; + status =CHECK_CONDITION ;BADF ("Driver Timeout\n" ); }else if ((s->driver_status &SG_ERR_DRIVER_SENSE ) ==0 ) - sense =NO_SENSE ; + status =GOOD ;else - sense = s->sensebuf[2 ]; + status =CHECK_CONDITION ; } -DPRINTF ("Command complete 0x%p tag=0x%x sense=%d\n" , r, r->tag, sense); +DPRINTF ("Command complete 0x%p tag=0x%x status=%d\n" , r, r->tag, status); tag = r->tag;scsi_remove_request (r); - s->completion (s->opaque,SCSI_REASON_DONE , tag, sense); + s->completion (s->opaque,SCSI_REASON_DONE , tag, status); }/* Cancel a pending data transfer. */
hw/scsi-disk.c +19 -12
@@ -34,6 +34,9 @@ do { fprintf(stderr, "scsi-disk: " fmt ,#define SENSE_HARDWARE_ERROR 4#define SENSE_ILLEGAL_REQUEST 5 +#define STATUS_GOOD 0 +#define STATUS_CHECK_CONDITION 1 +#define SCSI_DMA_BUF_SIZE 131072 typedef struct SCSIRequest {
@@ -124,7 +127,7 @@ static SCSIRequest *scsi_find_request(SC }/* Helper function for command completion. */ -static void scsi_command_complete (SCSIRequest *r,int sense) +static void scsi_command_complete (SCSIRequest *r,int status,int sense) {SCSIDeviceState *s = r->dev;uint32_t tag;
@@ -132,7 +135,7 @@ static void scsi_command_complete(SCSIRe s->sense = sense; tag = r->tag;scsi_remove_request (r); - s->completion (s->opaque,SCSI_REASON_DONE , tag, sense); + s->completion (s->opaque,SCSI_REASON_DONE , tag, status); }/* Cancel a pending data transfer. */
@@ -157,7 +160,7 @@ static void scsi_read_complete(void * opif (ret) {DPRINTF ("IO error\n" ); -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ,SENSE_HARDWARE_ERROR );return ; }DPRINTF ("Data ready tag=0x%x len=%d\n" , r->tag, r->buf_len);
@@ -176,7 +179,7 @@ static void scsi_read_data(SCSIDevice *dif (!r) {BADF ("Bad read tag 0x%x\n" , tag);/* ??? This is the wrong error. */ -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ,SENSE_HARDWARE_ERROR );return ; }if (r->sector_count == (uint32_t )-1 ) {
@@ -187,7 +190,7 @@ static void scsi_read_data(SCSIDevice *d }DPRINTF ("Read sector_count=%d\n" , r->sector_count);if (r->sector_count ==0 ) { -scsi_command_complete (r,SENSE_NO_SENSE ); +scsi_command_complete (r,STATUS_GOOD ,SENSE_NO_SENSE );return ; }
@@ -199,7 +202,7 @@ static void scsi_read_data(SCSIDevice *d r->aiocb =bdrv_aio_read (s->bdrv, r->sector, r->dma_buf, n, scsi_read_complete, r);if (r->aiocb ==NULL ) -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ,SENSE_HARDWARE_ERROR ); r->sector += n; r->sector_count -= n; }
@@ -217,7 +220,7 @@ static void scsi_write_complete(void * o r->aiocb =NULL ;if (r->sector_count ==0 ) { -scsi_command_complete (r,SENSE_NO_SENSE ); +scsi_command_complete (r,STATUS_GOOD ,SENSE_NO_SENSE ); }else { len = r->sector_count *512 ;if (len >SCSI_DMA_BUF_SIZE ) {
@@ -241,7 +244,7 @@ static int scsi_write_data(SCSIDevice *d r =scsi_find_request (s, tag);if (!r) {BADF ("Bad write tag 0x%x\n" , tag); -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ,SENSE_HARDWARE_ERROR );return 1 ; }if (r->aiocb)
@@ -251,7 +254,8 @@ static int scsi_write_data(SCSIDevice *d r->aiocb =bdrv_aio_write (s->bdrv, r->sector, r->dma_buf, n, scsi_write_complete, r);if (r->aiocb ==NULL ) -scsi_command_complete (r,SENSE_HARDWARE_ERROR ); +scsi_command_complete (r,STATUS_CHECK_CONDITION , +SENSE_HARDWARE_ERROR ); r->sector += n; r->sector_count -= n; }else {
@@ -670,7 +674,7 @@ static int32_t scsi_send_command(SCSIDev outbuf[7 ] =0 ; r->buf_len =8 ; }else { -scsi_command_complete (r,SENSE_NOT_READY ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ,SENSE_NOT_READY );return 0 ; }break ;
@@ -754,14 +758,17 @@ static int32_t scsi_send_command(SCSIDev outbuf[3 ] =8 ; r->buf_len =16 ;break ; +case 0x2f : +DPRINTF ("Verify (sector %d, count %d)\n" , lba, len); +break ;default :DPRINTF ("Unknown SCSI command (%2.2x)\n" , buf[0 ]); fail: -scsi_command_complete (r,SENSE_ILLEGAL_REQUEST ); +scsi_command_complete (r,STATUS_CHECK_CONDITION ,SENSE_ILLEGAL_REQUEST );return 0 ; }if (r->sector_count ==0 && r->buf_len ==0 ) { -scsi_command_complete (r,SENSE_NO_SENSE ); +scsi_command_complete (r,STATUS_GOOD ,SENSE_NO_SENSE ); } len = r->sector_count *512 + r->buf_len;if (is_write) {