Correct SCSI error reporting (Laurent Vivier)

git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5455 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
blueswir1 2008-10-11 09:33:03 +00:00
parent 71fb234866
commit 22864256db
1 changed files with 27 additions and 15 deletions

View File

@ -34,6 +34,9 @@ do { fprintf(stderr, "scsi-disk: " fmt , ##args); } while (0)
#define SENSE_HARDWARE_ERROR 4 #define SENSE_HARDWARE_ERROR 4
#define SENSE_ILLEGAL_REQUEST 5 #define SENSE_ILLEGAL_REQUEST 5
#define STATUS_GOOD 0
#define STATUS_CHECK_CONDITION 2
#define SCSI_DMA_BUF_SIZE 131072 #define SCSI_DMA_BUF_SIZE 131072
typedef struct SCSIRequest { typedef struct SCSIRequest {
@ -124,15 +127,15 @@ static SCSIRequest *scsi_find_request(SCSIDeviceState *s, uint32_t tag)
} }
/* Helper function for command completion. */ /* 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; SCSIDeviceState *s = r->dev;
uint32_t tag; uint32_t tag;
DPRINTF("Command complete tag=0x%x sense=%d\n", r->tag, sense); DPRINTF("Command complete tag=0x%x status=%d sense=%d\n", r->tag, status, sense);
s->sense = sense; s->sense = sense;
tag = r->tag; tag = r->tag;
scsi_remove_request(r); 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. */ /* Cancel a pending data transfer. */
@ -157,7 +160,8 @@ static void scsi_read_complete(void * opaque, int ret)
if (ret) { if (ret) {
DPRINTF("IO error\n"); DPRINTF("IO error\n");
scsi_command_complete(r, SENSE_HARDWARE_ERROR); s->completion(s->opaque, SCSI_REASON_DATA, r->tag, 0);
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_NO_SENSE);
return; return;
} }
DPRINTF("Data ready tag=0x%x len=%d\n", r->tag, r->buf_len); DPRINTF("Data ready tag=0x%x len=%d\n", r->tag, r->buf_len);
@ -176,7 +180,7 @@ static void scsi_read_data(SCSIDevice *d, uint32_t tag)
if (!r) { if (!r) {
BADF("Bad read tag 0x%x\n", tag); BADF("Bad read tag 0x%x\n", tag);
/* ??? This is the wrong error. */ /* ??? This is the wrong error. */
scsi_command_complete(r, SENSE_HARDWARE_ERROR); scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_HARDWARE_ERROR);
return; return;
} }
if (r->sector_count == (uint32_t)-1) { if (r->sector_count == (uint32_t)-1) {
@ -187,7 +191,7 @@ static void scsi_read_data(SCSIDevice *d, uint32_t tag)
} }
DPRINTF("Read sector_count=%d\n", r->sector_count); DPRINTF("Read sector_count=%d\n", r->sector_count);
if (r->sector_count == 0) { if (r->sector_count == 0) {
scsi_command_complete(r, SENSE_NO_SENSE); scsi_command_complete(r, STATUS_GOOD, SENSE_NO_SENSE);
return; return;
} }
@ -199,7 +203,7 @@ static void scsi_read_data(SCSIDevice *d, uint32_t tag)
r->aiocb = bdrv_aio_read(s->bdrv, r->sector, r->dma_buf, n, r->aiocb = bdrv_aio_read(s->bdrv, r->sector, r->dma_buf, n,
scsi_read_complete, r); scsi_read_complete, r);
if (r->aiocb == NULL) 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 += n;
r->sector_count -= n; r->sector_count -= n;
} }
@ -217,7 +221,7 @@ static void scsi_write_complete(void * opaque, int ret)
r->aiocb = NULL; r->aiocb = NULL;
if (r->sector_count == 0) { if (r->sector_count == 0) {
scsi_command_complete(r, SENSE_NO_SENSE); scsi_command_complete(r, STATUS_GOOD, SENSE_NO_SENSE);
} else { } else {
len = r->sector_count * 512; len = r->sector_count * 512;
if (len > SCSI_DMA_BUF_SIZE) { if (len > SCSI_DMA_BUF_SIZE) {
@ -241,7 +245,7 @@ static int scsi_write_data(SCSIDevice *d, uint32_t tag)
r = scsi_find_request(s, tag); r = scsi_find_request(s, tag);
if (!r) { if (!r) {
BADF("Bad write tag 0x%x\n", tag); 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; return 1;
} }
if (r->aiocb) if (r->aiocb)
@ -251,7 +255,8 @@ static int scsi_write_data(SCSIDevice *d, uint32_t tag)
r->aiocb = bdrv_aio_write(s->bdrv, r->sector, r->dma_buf, n, r->aiocb = bdrv_aio_write(s->bdrv, r->sector, r->dma_buf, n,
scsi_write_complete, r); scsi_write_complete, r);
if (r->aiocb == NULL) 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 += n;
r->sector_count -= n; r->sector_count -= n;
} else { } else {
@ -344,7 +349,8 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
if (lun || buf[1] >> 5) { if (lun || buf[1] >> 5) {
/* Only LUN 0 supported. */ /* Only LUN 0 supported. */
DPRINTF("Unimplemented LUN %d\n", lun ? lun : buf[1] >> 5); DPRINTF("Unimplemented LUN %d\n", lun ? lun : buf[1] >> 5);
goto fail; if (command != 0x03 && command != 0x12) /* REQUEST SENSE and INQUIRY */
goto fail;
} }
switch (command) { switch (command) {
case 0x0: case 0x0:
@ -487,7 +493,10 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
} }
} }
memset(outbuf, 0, 36); memset(outbuf, 0, 36);
if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
if (lun || buf[1] >> 5) {
outbuf[0] = 0x7f; /* LUN not supported */
} else if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
outbuf[0] = 5; outbuf[0] = 5;
outbuf[1] = 0x80; outbuf[1] = 0x80;
memcpy(&outbuf[16], "QEMU CD-ROM ", 16); memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
@ -670,7 +679,7 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
outbuf[7] = 0; outbuf[7] = 0;
r->buf_len = 8; r->buf_len = 8;
} else { } else {
scsi_command_complete(r, SENSE_NOT_READY); scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_NOT_READY);
return 0; return 0;
} }
break; break;
@ -754,14 +763,17 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
outbuf[3] = 8; outbuf[3] = 8;
r->buf_len = 16; r->buf_len = 16;
break; break;
case 0x2f:
DPRINTF("Verify (sector %d, count %d)\n", lba, len);
break;
default: default:
DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]); DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
fail: fail:
scsi_command_complete(r, SENSE_ILLEGAL_REQUEST); scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_ILLEGAL_REQUEST);
return 0; return 0;
} }
if (r->sector_count == 0 && r->buf_len == 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; len = r->sector_count * 512 + r->buf_len;
if (is_write) { if (is_write) {