From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Lhr1l-0002jO-Ah for qemu-devel@nongnu.org; Thu, 12 Mar 2009 15:57:13 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Lhr1k-0002iY-DA for qemu-devel@nongnu.org; Thu, 12 Mar 2009 15:57:12 -0400 Received: from [199.232.76.173] (port=59022 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Lhr1k-0002iO-9e for qemu-devel@nongnu.org; Thu, 12 Mar 2009 15:57:12 -0400 Received: from savannah.gnu.org ([199.232.41.3]:49569 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Lhr1j-00037M-RN for qemu-devel@nongnu.org; Thu, 12 Mar 2009 15:57:11 -0400 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Lhr1h-000307-Nn for qemu-devel@nongnu.org; Thu, 12 Mar 2009 19:57:09 +0000 Received: from aliguori by cvs.savannah.gnu.org with local (Exim 4.69) (envelope-from ) id 1Lhr1h-000302-Di for qemu-devel@nongnu.org; Thu, 12 Mar 2009 19:57:09 +0000 MIME-Version: 1.0 Errors-To: aliguori Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anthony Liguori Message-Id: Date: Thu, 12 Mar 2009 19:57:09 +0000 Subject: [Qemu-devel] [6822] Add specialized block driver scsi generic API (Avi Kivity) Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 6822 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6822 Author: aliguori Date: 2009-03-12 19:57:08 +0000 (Thu, 12 Mar 2009) Log Message: ----------- 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 Modified Paths: -------------- trunk/block.c trunk/block.h trunk/hw/scsi-generic.c Modified: trunk/block.c =================================================================== --- trunk/block.c 2009-03-11 20:16:16 UTC (rev 6821) +++ trunk/block.c 2009-03-12 19:57:08 UTC (rev 6822) @@ -1675,3 +1675,25 @@ 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); +} Modified: trunk/block.h =================================================================== --- trunk/block.h 2009-03-11 20:16:16 UTC (rev 6821) +++ trunk/block.h 2009-03-12 19:57:08 UTC (rev 6822) @@ -101,6 +101,14 @@ 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); Modified: trunk/hw/scsi-generic.c =================================================================== --- trunk/hw/scsi-generic.c 2009-03-11 20:16:16 UTC (rev 6821) +++ trunk/hw/scsi-generic.c 2009-03-12 19:57:08 UTC (rev 6822) @@ -201,6 +201,7 @@ SCSIRequest *r, int direction, BlockDriverCompletionFunc *complete) { + int ret; r->io_header.interface_id = 'S'; r->io_header.dxfer_direction = direction; @@ -214,25 +215,27 @@ 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 @@ 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 @@ 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];