scsi: create common SCSIRequest structure.

Rename the SCSIRequest structs in scsi-disk.c and scsi-generic.c to
SCSIDiskReq and SCSIGenericReq.  Create a SCSIRequest struct and move
the common elements over.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Gerd Hoffmann 2009-11-26 15:33:48 +01:00 committed by Anthony Liguori
parent 1e37607b5e
commit 4c41d2ef5f
3 changed files with 114 additions and 107 deletions

View File

@ -46,10 +46,8 @@ do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
typedef struct SCSIDiskState SCSIDiskState;
typedef struct SCSIRequest {
SCSIBus *bus;
SCSIDiskState *dev;
uint32_t tag;
typedef struct SCSIDiskReq {
SCSIRequest req;
/* ??? We should probably keep track of whether the data transfer is
a read or a write. Currently we rely on the host getting it right. */
/* Both sector and sector_count are in terms of qemu 512 byte blocks. */
@ -57,16 +55,15 @@ typedef struct SCSIRequest {
uint32_t sector_count;
struct iovec iov;
QEMUIOVector qiov;
BlockDriverAIOCB *aiocb;
struct SCSIRequest *next;
struct SCSIDiskReq *next;
uint32_t status;
} SCSIRequest;
} SCSIDiskReq;
struct SCSIDiskState
{
SCSIDevice qdev;
DriveInfo *dinfo;
SCSIRequest *requests;
SCSIDiskReq *requests;
/* The qemu block layer uses a fixed 512 byte sector size.
This is the number of 512 byte blocks in a single scsi sector. */
int cluster_size;
@ -77,26 +74,26 @@ struct SCSIDiskState
};
/* Global pool of SCSIRequest structures. */
static SCSIRequest *free_requests = NULL;
static SCSIDiskReq *free_requests = NULL;
static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag)
static SCSIDiskReq *scsi_new_request(SCSIDevice *d, uint32_t tag)
{
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
SCSIRequest *r;
SCSIDiskReq *r;
if (free_requests) {
r = free_requests;
free_requests = r->next;
} else {
r = qemu_malloc(sizeof(SCSIRequest));
r = qemu_malloc(sizeof(SCSIDiskReq));
r->iov.iov_base = qemu_memalign(512, SCSI_DMA_BUF_SIZE);
}
r->bus = scsi_bus_from_device(d);
r->dev = s;
r->tag = tag;
r->req.bus = scsi_bus_from_device(d);
r->req.dev = d;
r->req.tag = tag;
r->sector_count = 0;
r->iov.iov_len = 0;
r->aiocb = NULL;
r->req.aiocb = NULL;
r->status = 0;
r->next = s->requests;
@ -104,10 +101,10 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag)
return r;
}
static void scsi_remove_request(SCSIRequest *r)
static void scsi_remove_request(SCSIDiskReq *r)
{
SCSIRequest *last;
SCSIDiskState *s = r->dev;
SCSIDiskReq *last;
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
if (s->requests == r) {
s->requests = r->next;
@ -125,64 +122,65 @@ static void scsi_remove_request(SCSIRequest *r)
free_requests = r;
}
static SCSIRequest *scsi_find_request(SCSIDiskState *s, uint32_t tag)
static SCSIDiskReq *scsi_find_request(SCSIDiskState *s, uint32_t tag)
{
SCSIRequest *r;
SCSIDiskReq *r;
r = s->requests;
while (r && r->tag != tag)
while (r && r->req.tag != tag)
r = r->next;
return r;
}
/* Helper function for command completion. */
static void scsi_command_complete(SCSIRequest *r, int status, int sense)
static void scsi_command_complete(SCSIDiskReq *r, int status, int sense)
{
SCSIDiskState *s = r->dev;
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
uint32_t tag;
DPRINTF("Command complete tag=0x%x status=%d sense=%d\n", r->tag, status, sense);
DPRINTF("Command complete tag=0x%x status=%d sense=%d\n",
r->req.tag, status, sense);
s->sense = sense;
tag = r->tag;
tag = r->req.tag;
scsi_remove_request(r);
r->bus->complete(r->bus, SCSI_REASON_DONE, tag, status);
r->req.bus->complete(r->req.bus, SCSI_REASON_DONE, tag, status);
}
/* Cancel a pending data transfer. */
static void scsi_cancel_io(SCSIDevice *d, uint32_t tag)
{
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
SCSIRequest *r;
SCSIDiskReq *r;
DPRINTF("Cancel tag=0x%x\n", tag);
r = scsi_find_request(s, tag);
if (r) {
if (r->aiocb)
bdrv_aio_cancel(r->aiocb);
r->aiocb = NULL;
if (r->req.aiocb)
bdrv_aio_cancel(r->req.aiocb);
r->req.aiocb = NULL;
scsi_remove_request(r);
}
}
static void scsi_read_complete(void * opaque, int ret)
{
SCSIRequest *r = (SCSIRequest *)opaque;
SCSIDiskReq *r = (SCSIDiskReq *)opaque;
if (ret) {
DPRINTF("IO error\n");
r->bus->complete(r->bus, SCSI_REASON_DATA, r->tag, 0);
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, 0);
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_NO_SENSE);
return;
}
DPRINTF("Data ready tag=0x%x len=%" PRId64 "\n", r->tag, r->iov.iov_len);
DPRINTF("Data ready tag=0x%x len=%" PRId64 "\n", r->req.tag, r->iov.iov_len);
r->bus->complete(r->bus, SCSI_REASON_DATA, r->tag, r->iov.iov_len);
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, r->iov.iov_len);
}
/* Read more data from scsi device into buffer. */
static void scsi_read_data(SCSIDevice *d, uint32_t tag)
{
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
SCSIRequest *r;
SCSIDiskReq *r;
uint32_t n;
r = scsi_find_request(s, tag);
@ -195,7 +193,7 @@ static void scsi_read_data(SCSIDevice *d, uint32_t tag)
if (r->sector_count == (uint32_t)-1) {
DPRINTF("Read buf_len=%" PRId64 "\n", r->iov.iov_len);
r->sector_count = 0;
r->bus->complete(r->bus, SCSI_REASON_DATA, r->tag, r->iov.iov_len);
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, r->iov.iov_len);
return;
}
DPRINTF("Read sector_count=%d\n", r->sector_count);
@ -210,17 +208,18 @@ static void scsi_read_data(SCSIDevice *d, uint32_t tag)
r->iov.iov_len = n * 512;
qemu_iovec_init_external(&r->qiov, &r->iov, 1);
r->aiocb = bdrv_aio_readv(s->dinfo->bdrv, r->sector, &r->qiov, n,
r->req.aiocb = bdrv_aio_readv(s->dinfo->bdrv, r->sector, &r->qiov, n,
scsi_read_complete, r);
if (r->aiocb == NULL)
if (r->req.aiocb == NULL)
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_HARDWARE_ERROR);
r->sector += n;
r->sector_count -= n;
}
static int scsi_handle_write_error(SCSIRequest *r, int error)
static int scsi_handle_write_error(SCSIDiskReq *r, int error)
{
BlockInterfaceErrorAction action = drive_get_onerror(r->dev->dinfo->bdrv);
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
BlockInterfaceErrorAction action = drive_get_onerror(s->dinfo->bdrv);
if (action == BLOCK_ERR_IGNORE)
return 0;
@ -239,11 +238,11 @@ static int scsi_handle_write_error(SCSIRequest *r, int error)
static void scsi_write_complete(void * opaque, int ret)
{
SCSIRequest *r = (SCSIRequest *)opaque;
SCSIDiskReq *r = (SCSIDiskReq *)opaque;
uint32_t len;
uint32_t n;
r->aiocb = NULL;
r->req.aiocb = NULL;
if (ret) {
if (scsi_handle_write_error(r, -ret))
@ -261,22 +260,22 @@ static void scsi_write_complete(void * opaque, int ret)
len = SCSI_DMA_BUF_SIZE;
}
r->iov.iov_len = len;
DPRINTF("Write complete tag=0x%x more=%d\n", r->tag, len);
r->bus->complete(r->bus, SCSI_REASON_DATA, r->tag, len);
DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, len);
}
}
static void scsi_write_request(SCSIRequest *r)
static void scsi_write_request(SCSIDiskReq *r)
{
SCSIDiskState *s = r->dev;
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
uint32_t n;
n = r->iov.iov_len / 512;
if (n) {
qemu_iovec_init_external(&r->qiov, &r->iov, 1);
r->aiocb = bdrv_aio_writev(s->dinfo->bdrv, r->sector, &r->qiov, n,
r->req.aiocb = bdrv_aio_writev(s->dinfo->bdrv, r->sector, &r->qiov, n,
scsi_write_complete, r);
if (r->aiocb == NULL)
if (r->req.aiocb == NULL)
scsi_command_complete(r, STATUS_CHECK_CONDITION,
SENSE_HARDWARE_ERROR);
} else {
@ -290,7 +289,7 @@ static void scsi_write_request(SCSIRequest *r)
static int scsi_write_data(SCSIDevice *d, uint32_t tag)
{
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
SCSIRequest *r;
SCSIDiskReq *r;
DPRINTF("Write data tag=0x%x\n", tag);
r = scsi_find_request(s, tag);
@ -300,7 +299,7 @@ static int scsi_write_data(SCSIDevice *d, uint32_t tag)
return 1;
}
if (r->aiocb)
if (r->req.aiocb)
BADF("Data transfer already in progress\n");
scsi_write_request(r);
@ -311,7 +310,7 @@ static int scsi_write_data(SCSIDevice *d, uint32_t tag)
static void scsi_dma_restart_bh(void *opaque)
{
SCSIDiskState *s = opaque;
SCSIRequest *r = s->requests;
SCSIDiskReq *r = s->requests;
qemu_bh_delete(s->bh);
s->bh = NULL;
@ -342,7 +341,7 @@ static void scsi_dma_restart_cb(void *opaque, int running, int reason)
static uint8_t *scsi_get_buf(SCSIDevice *d, uint32_t tag)
{
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
SCSIRequest *r;
SCSIDiskReq *r;
r = scsi_find_request(s, tag);
if (!r) {
@ -368,7 +367,7 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
int is_write;
uint8_t command;
uint8_t *outbuf;
SCSIRequest *r;
SCSIDiskReq *r;
command = buf[0];
r = scsi_find_request(s, tag);
@ -606,7 +605,7 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
outbuf[3] = 2; /* Format 2 */
outbuf[4] = len - 5; /* Additional Length = (Len - 1) - 4 */
/* Sync data transfer and TCQ. */
outbuf[7] = 0x10 | (r->bus->tcq ? 0x02 : 0);
outbuf[7] = 0x10 | (r->req.bus->tcq ? 0x02 : 0);
r->iov.iov_len = len;
break;
case 0x16:

View File

@ -54,24 +54,21 @@ do { fprintf(stderr, "scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
typedef struct SCSIGenericState SCSIGenericState;
typedef struct SCSIRequest {
BlockDriverAIOCB *aiocb;
struct SCSIRequest *next;
SCSIBus *bus;
SCSIGenericState *dev;
uint32_t tag;
typedef struct SCSIGenericReq {
SCSIRequest req;
struct SCSIGenericReq *next;
uint8_t cmd[SCSI_CMD_BUF_SIZE];
int cmdlen;
uint8_t *buf;
int buflen;
int len;
sg_io_hdr_t io_header;
} SCSIRequest;
} SCSIGenericReq;
struct SCSIGenericState
{
SCSIDevice qdev;
SCSIRequest *requests;
SCSIGenericReq *requests;
DriveInfo *dinfo;
int type;
int blocksize;
@ -81,30 +78,30 @@ struct SCSIGenericState
uint8_t senselen;
};
/* Global pool of SCSIRequest structures. */
static SCSIRequest *free_requests = NULL;
/* Global pool of SCSIGenericReq structures. */
static SCSIGenericReq *free_requests = NULL;
static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag)
static SCSIGenericReq *scsi_new_request(SCSIDevice *d, uint32_t tag)
{
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
SCSIRequest *r;
SCSIGenericReq *r;
if (free_requests) {
r = free_requests;
free_requests = r->next;
} else {
r = qemu_malloc(sizeof(SCSIRequest));
r = qemu_malloc(sizeof(SCSIGenericReq));
r->buf = NULL;
r->buflen = 0;
}
r->bus = scsi_bus_from_device(d);
r->dev = s;
r->tag = tag;
r->req.bus = scsi_bus_from_device(d);
r->req.dev = d;
r->req.tag = tag;
memset(r->cmd, 0, sizeof(r->cmd));
memset(&r->io_header, 0, sizeof(r->io_header));
r->cmdlen = 0;
r->len = 0;
r->aiocb = NULL;
r->req.aiocb = NULL;
/* link */
@ -113,10 +110,10 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag)
return r;
}
static void scsi_remove_request(SCSIRequest *r)
static void scsi_remove_request(SCSIGenericReq *r)
{
SCSIRequest *last;
SCSIGenericState *s = r->dev;
SCSIGenericReq *last;
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, r->req.dev);
if (s->requests == r) {
s->requests = r->next;
@ -134,12 +131,12 @@ static void scsi_remove_request(SCSIRequest *r)
free_requests = r;
}
static SCSIRequest *scsi_find_request(SCSIGenericState *s, uint32_t tag)
static SCSIGenericReq *scsi_find_request(SCSIGenericState *s, uint32_t tag)
{
SCSIRequest *r;
SCSIGenericReq *r;
r = s->requests;
while (r && r->tag != tag)
while (r && r->req.tag != tag)
r = r->next;
return r;
@ -148,8 +145,8 @@ static SCSIRequest *scsi_find_request(SCSIGenericState *s, uint32_t tag)
/* Helper function for command completion. */
static void scsi_command_complete(void *opaque, int ret)
{
SCSIRequest *r = (SCSIRequest *)opaque;
SCSIGenericState *s = r->dev;
SCSIGenericReq *r = (SCSIGenericReq *)opaque;
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, r->req.dev);
uint32_t tag;
int status;
@ -171,10 +168,10 @@ static void scsi_command_complete(void *opaque, int ret)
status = GOOD << 1;
}
DPRINTF("Command complete 0x%p tag=0x%x status=%d\n",
r, r->tag, status);
tag = r->tag;
r, r->req.tag, status);
tag = r->req.tag;
scsi_remove_request(r);
r->bus->complete(r->bus, SCSI_REASON_DONE, tag, status);
r->req.bus->complete(r->req.bus, SCSI_REASON_DONE, tag, status);
}
/* Cancel a pending data transfer. */
@ -182,35 +179,37 @@ static void scsi_cancel_io(SCSIDevice *d, uint32_t tag)
{
DPRINTF("scsi_cancel_io 0x%x\n", tag);
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
SCSIRequest *r;
SCSIGenericReq *r;
DPRINTF("Cancel tag=0x%x\n", tag);
r = scsi_find_request(s, tag);
if (r) {
if (r->aiocb)
bdrv_aio_cancel(r->aiocb);
r->aiocb = NULL;
if (r->req.aiocb)
bdrv_aio_cancel(r->req.aiocb);
r->req.aiocb = NULL;
scsi_remove_request(r);
}
}
static int execute_command(BlockDriverState *bdrv,
SCSIRequest *r, int direction,
SCSIGenericReq *r, int direction,
BlockDriverCompletionFunc *complete)
{
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, r->req.dev);
r->io_header.interface_id = 'S';
r->io_header.dxfer_direction = direction;
r->io_header.dxferp = r->buf;
r->io_header.dxfer_len = r->buflen;
r->io_header.cmdp = r->cmd;
r->io_header.cmd_len = r->cmdlen;
r->io_header.mx_sb_len = sizeof(r->dev->sensebuf);
r->io_header.sbp = r->dev->sensebuf;
r->io_header.mx_sb_len = sizeof(s->sensebuf);
r->io_header.sbp = s->sensebuf;
r->io_header.timeout = MAX_UINT;
r->io_header.usr_ptr = r;
r->io_header.flags |= SG_FLAG_DIRECT_IO;
r->aiocb = bdrv_aio_ioctl(bdrv, SG_IO, &r->io_header, complete, r);
if (r->aiocb == NULL) {
r->req.aiocb = bdrv_aio_ioctl(bdrv, SG_IO, &r->io_header, complete, r);
if (r->req.aiocb == NULL) {
BADF("execute_command: read failed !\n");
return -1;
}
@ -220,7 +219,7 @@ static int execute_command(BlockDriverState *bdrv,
static void scsi_read_complete(void * opaque, int ret)
{
SCSIRequest *r = (SCSIRequest *)opaque;
SCSIGenericReq *r = (SCSIGenericReq *)opaque;
int len;
if (ret) {
@ -229,10 +228,10 @@ static void scsi_read_complete(void * opaque, int ret)
return;
}
len = r->io_header.dxfer_len - r->io_header.resid;
DPRINTF("Data ready tag=0x%x len=%d\n", r->tag, len);
DPRINTF("Data ready tag=0x%x len=%d\n", r->req.tag, len);
r->len = -1;
r->bus->complete(r->bus, SCSI_REASON_DATA, r->tag, len);
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, len);
if (len == 0)
scsi_command_complete(r, 0);
}
@ -241,7 +240,7 @@ static void scsi_read_complete(void * opaque, int ret)
static void scsi_read_data(SCSIDevice *d, uint32_t tag)
{
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
SCSIRequest *r;
SCSIGenericReq *r;
int ret;
DPRINTF("scsi_read_data 0x%x\n", tag);
@ -266,11 +265,11 @@ static void scsi_read_data(SCSIDevice *d, uint32_t tag)
r->io_header.status = 0;
r->io_header.dxfer_len = s->senselen;
r->len = -1;
DPRINTF("Data ready tag=0x%x len=%d\n", r->tag, s->senselen);
DPRINTF("Data ready tag=0x%x len=%d\n", r->req.tag, s->senselen);
DPRINTF("Sense: %d %d %d %d %d %d %d %d\n",
r->buf[0], r->buf[1], r->buf[2], r->buf[3],
r->buf[4], r->buf[5], r->buf[6], r->buf[7]);
r->bus->complete(r->bus, SCSI_REASON_DATA, r->tag, s->senselen);
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, s->senselen);
return;
}
@ -283,7 +282,8 @@ static void scsi_read_data(SCSIDevice *d, uint32_t tag)
static void scsi_write_complete(void * opaque, int ret)
{
SCSIRequest *r = (SCSIRequest *)opaque;
SCSIGenericReq *r = (SCSIGenericReq *)opaque;
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, r->req.dev);
DPRINTF("scsi_write_complete() ret = %d\n", ret);
if (ret) {
@ -293,9 +293,9 @@ static void scsi_write_complete(void * opaque, int ret)
}
if (r->cmd[0] == MODE_SELECT && r->cmd[4] == 12 &&
r->dev->type == TYPE_TAPE) {
r->dev->blocksize = (r->buf[9] << 16) | (r->buf[10] << 8) | r->buf[11];
DPRINTF("block size %d\n", r->dev->blocksize);
s->type == TYPE_TAPE) {
s->blocksize = (r->buf[9] << 16) | (r->buf[10] << 8) | r->buf[11];
DPRINTF("block size %d\n", s->blocksize);
}
scsi_command_complete(r, ret);
@ -306,7 +306,7 @@ static void scsi_write_complete(void * opaque, int ret)
static int scsi_write_data(SCSIDevice *d, uint32_t tag)
{
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
SCSIRequest *r;
SCSIGenericReq *r;
int ret;
DPRINTF("scsi_write_data 0x%x\n", tag);
@ -320,7 +320,7 @@ static int scsi_write_data(SCSIDevice *d, uint32_t tag)
if (r->len == 0) {
r->len = r->buflen;
r->bus->complete(r->bus, SCSI_REASON_DATA, r->tag, r->len);
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, r->len);
return 0;
}
@ -337,7 +337,7 @@ static int scsi_write_data(SCSIDevice *d, uint32_t tag)
static uint8_t *scsi_get_buf(SCSIDevice *d, uint32_t tag)
{
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
SCSIRequest *r;
SCSIGenericReq *r;
r = scsi_find_request(s, tag);
if (!r) {
BADF("Bad buffer tag 0x%x\n", tag);
@ -512,7 +512,7 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
uint32_t len=0;
int cmdlen=0;
SCSIRequest *r;
SCSIGenericReq *r;
SCSIBus *bus;
int ret;
@ -653,7 +653,7 @@ static int get_stream_blocksize(BlockDriverState *bdrv)
static void scsi_destroy(SCSIDevice *d)
{
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
SCSIRequest *r, *n;
SCSIGenericReq *r, *n;
r = s->requests;
while (r) {

View File

@ -2,6 +2,7 @@
#define QEMU_HW_SCSI_H
#include "qdev.h"
#include "block.h"
/* scsi-disk.c */
enum scsi_reason {
@ -15,6 +16,13 @@ typedef struct SCSIDeviceInfo SCSIDeviceInfo;
typedef void (*scsi_completionfn)(SCSIBus *bus, int reason, uint32_t tag,
uint32_t arg);
typedef struct SCSIRequest {
SCSIBus *bus;
SCSIDevice *dev;
uint32_t tag;
BlockDriverAIOCB *aiocb;
} SCSIRequest;
struct SCSIDevice
{
DeviceState qdev;