From 7d78066926b68afe28a1948c64618ee085d9ab02 Mon Sep 17 00:00:00 2001 From: aliguori Date: Thu, 12 Mar 2009 19:57:08 +0000 Subject: [PATCH] Add specialized block driver scsi generic API (Avi Kivity) When a scsi device is backed by a scsi generic device instead of an ordinary host block device, the block API is abused in a couple of annoying ways: - nb_sectors is negative, and specifies a byte count instead of a sector count - offset is ignored, since scsi-generic is essentially a packet protocol This overloading makes hacking the block layer difficult. Remove it by introducing a new explicit API for scsi-generic devices. The new API is still backed by the old implementation, but at least the users are insulated. Signed-off-by: Avi Kivity Signed-off-by: Anthony Liguori git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6822 c046a42c-6fe2-441c-8c8c-71466251a162 --- block.c | 22 ++++++++++++++++++++++ block.h | 8 ++++++++ hw/scsi-generic.c | 39 ++++++++++++++++++++++----------------- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/block.c b/block.c index 1ef4ae5f3e..39b27b2702 100644 --- a/block.c +++ b/block.c @@ -1675,3 +1675,25 @@ int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) return drv->bdrv_ioctl(bs, req, buf); return -ENOTSUP; } + +int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count) +{ + return bdrv_pwrite(bs, -1, buf, count); +} + +int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count) +{ + return bdrv_pread(bs, -1, buf, count); +} + +BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count, + BlockDriverCompletionFunc *cb, void *opaque) +{ + return bdrv_aio_read(bs, 0, buf, -(int64_t)count, cb, opaque); +} + +BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count, + BlockDriverCompletionFunc *cb, void *opaque) +{ + return bdrv_aio_write(bs, 0, buf, -(int64_t)count, cb, opaque); +} diff --git a/block.h b/block.h index f6b14ee8ea..b0c63ac588 100644 --- a/block.h +++ b/block.h @@ -101,6 +101,14 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, BlockDriverCompletionFunc *cb, void *opaque); void bdrv_aio_cancel(BlockDriverAIOCB *acb); +/* sg packet commands */ +int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count); +int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count); +BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count, + BlockDriverCompletionFunc *cb, void *opaque); +BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count, + BlockDriverCompletionFunc *cb, void *opaque); + /* Ensure contents are flushed to disk. */ void bdrv_flush(BlockDriverState *bs); void bdrv_flush_all(void); diff --git a/hw/scsi-generic.c b/hw/scsi-generic.c index 5bf160ab70..53e8951270 100644 --- a/hw/scsi-generic.c +++ b/hw/scsi-generic.c @@ -201,6 +201,7 @@ static int execute_command(BlockDriverState *bdrv, SCSIRequest *r, int direction, BlockDriverCompletionFunc *complete) { + int ret; r->io_header.interface_id = 'S'; r->io_header.dxfer_direction = direction; @@ -214,25 +215,27 @@ static int execute_command(BlockDriverState *bdrv, r->io_header.usr_ptr = r; r->io_header.flags |= SG_FLAG_DIRECT_IO; - if (bdrv_pwrite(bdrv, -1, &r->io_header, sizeof(r->io_header)) == -1) { + ret = bdrv_sg_send_command(bdrv, &r->io_header, sizeof(r->io_header)); + if (ret < 0) { BADF("execute_command: write failed ! (%d)\n", errno); return -1; } if (complete == NULL) { int ret; r->aiocb = NULL; - while ((ret = bdrv_pread(bdrv, -1, &r->io_header, - sizeof(r->io_header))) == -1 && - errno == EINTR); - if (ret == -1) { + while ((ret = bdrv_sg_recv_response(bdrv, &r->io_header, + sizeof(r->io_header))) < 0 && + ret == -EINTR) + ; + if (ret < 0) { BADF("execute_command: read failed !\n"); return -1; } return 0; } - r->aiocb = bdrv_aio_read(bdrv, 0, (uint8_t*)&r->io_header, - -(int64_t)sizeof(r->io_header), complete, r); + r->aiocb = bdrv_sg_aio_read(bdrv, (uint8_t*)&r->io_header, + sizeof(r->io_header), complete, r); if (r->aiocb == NULL) { BADF("execute_command: read failed !\n"); return -1; @@ -634,14 +637,15 @@ static int get_blocksize(BlockDriverState *bdrv) io_header.sbp = sensebuf; io_header.timeout = 6000; /* XXX */ - ret = bdrv_pwrite(bdrv, -1, &io_header, sizeof(io_header)); - if (ret == -1) + ret = bdrv_sg_send_command(bdrv, &io_header, sizeof(io_header)); + if (ret < 0) return -1; - while ((ret = bdrv_pread(bdrv, -1, &io_header, sizeof(io_header))) == -1 && - errno == EINTR); + while ((ret = bdrv_sg_recv_response(bdrv, &io_header, sizeof(io_header))) < 0 && + ret == -EINTR) + ; - if (ret == -1) + if (ret < 0) return -1; return (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]; @@ -671,14 +675,15 @@ static int get_stream_blocksize(BlockDriverState *bdrv) io_header.sbp = sensebuf; io_header.timeout = 6000; /* XXX */ - ret = bdrv_pwrite(bdrv, -1, &io_header, sizeof(io_header)); - if (ret == -1) + ret = bdrv_sg_send_command(bdrv, &io_header, sizeof(io_header)); + if (ret < 0) return -1; - while ((ret = bdrv_pread(bdrv, -1, &io_header, sizeof(io_header))) == -1 && - errno == EINTR); + while ((ret = bdrv_sg_recv_response(bdrv, &io_header, sizeof(io_header))) < 0 && + ret == -EINTR) + ; - if (ret == -1) + if (ret < 0) return -1; return (buf[9] << 16) | (buf[10] << 8) | buf[11]; -- 2.11.4.GIT