Commit 7d78066926b68afe28a1948c64618ee085d9ab02

Authored by aliguori
1 parent 943984c7

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 <avi@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6822 c046a42c-6fe2-441c-8c8c-71466251a162
... ... @@ -1675,3 +1675,25 @@ int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1675 1675 return drv->bdrv_ioctl(bs, req, buf);
1676 1676 return -ENOTSUP;
1677 1677 }
  1678 +
  1679 +int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count)
  1680 +{
  1681 + return bdrv_pwrite(bs, -1, buf, count);
  1682 +}
  1683 +
  1684 +int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count)
  1685 +{
  1686 + return bdrv_pread(bs, -1, buf, count);
  1687 +}
  1688 +
  1689 +BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
  1690 + BlockDriverCompletionFunc *cb, void *opaque)
  1691 +{
  1692 + return bdrv_aio_read(bs, 0, buf, -(int64_t)count, cb, opaque);
  1693 +}
  1694 +
  1695 +BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
  1696 + BlockDriverCompletionFunc *cb, void *opaque)
  1697 +{
  1698 + return bdrv_aio_write(bs, 0, buf, -(int64_t)count, cb, opaque);
  1699 +}
... ...
... ... @@ -101,6 +101,14 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
101 101 BlockDriverCompletionFunc *cb, void *opaque);
102 102 void bdrv_aio_cancel(BlockDriverAIOCB *acb);
103 103  
  104 +/* sg packet commands */
  105 +int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count);
  106 +int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count);
  107 +BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
  108 + BlockDriverCompletionFunc *cb, void *opaque);
  109 +BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
  110 + BlockDriverCompletionFunc *cb, void *opaque);
  111 +
104 112 /* Ensure contents are flushed to disk. */
105 113 void bdrv_flush(BlockDriverState *bs);
106 114 void bdrv_flush_all(void);
... ...
hw/scsi-generic.c
... ... @@ -201,6 +201,7 @@ static int execute_command(BlockDriverState *bdrv,
201 201 SCSIRequest *r, int direction,
202 202 BlockDriverCompletionFunc *complete)
203 203 {
  204 + int ret;
204 205  
205 206 r->io_header.interface_id = 'S';
206 207 r->io_header.dxfer_direction = direction;
... ... @@ -214,25 +215,27 @@ static int execute_command(BlockDriverState *bdrv,
214 215 r->io_header.usr_ptr = r;
215 216 r->io_header.flags |= SG_FLAG_DIRECT_IO;
216 217  
217   - if (bdrv_pwrite(bdrv, -1, &r->io_header, sizeof(r->io_header)) == -1) {
  218 + ret = bdrv_sg_send_command(bdrv, &r->io_header, sizeof(r->io_header));
  219 + if (ret < 0) {
218 220 BADF("execute_command: write failed ! (%d)\n", errno);
219 221 return -1;
220 222 }
221 223 if (complete == NULL) {
222 224 int ret;
223 225 r->aiocb = NULL;
224   - while ((ret = bdrv_pread(bdrv, -1, &r->io_header,
225   - sizeof(r->io_header))) == -1 &&
226   - errno == EINTR);
227   - if (ret == -1) {
  226 + while ((ret = bdrv_sg_recv_response(bdrv, &r->io_header,
  227 + sizeof(r->io_header))) < 0 &&
  228 + ret == -EINTR)
  229 + ;
  230 + if (ret < 0) {
228 231 BADF("execute_command: read failed !\n");
229 232 return -1;
230 233 }
231 234 return 0;
232 235 }
233 236  
234   - r->aiocb = bdrv_aio_read(bdrv, 0, (uint8_t*)&r->io_header,
235   - -(int64_t)sizeof(r->io_header), complete, r);
  237 + r->aiocb = bdrv_sg_aio_read(bdrv, (uint8_t*)&r->io_header,
  238 + sizeof(r->io_header), complete, r);
236 239 if (r->aiocb == NULL) {
237 240 BADF("execute_command: read failed !\n");
238 241 return -1;
... ... @@ -634,14 +637,15 @@ static int get_blocksize(BlockDriverState *bdrv)
634 637 io_header.sbp = sensebuf;
635 638 io_header.timeout = 6000; /* XXX */
636 639  
637   - ret = bdrv_pwrite(bdrv, -1, &io_header, sizeof(io_header));
638   - if (ret == -1)
  640 + ret = bdrv_sg_send_command(bdrv, &io_header, sizeof(io_header));
  641 + if (ret < 0)
639 642 return -1;
640 643  
641   - while ((ret = bdrv_pread(bdrv, -1, &io_header, sizeof(io_header))) == -1 &&
642   - errno == EINTR);
  644 + while ((ret = bdrv_sg_recv_response(bdrv, &io_header, sizeof(io_header))) < 0 &&
  645 + ret == -EINTR)
  646 + ;
643 647  
644   - if (ret == -1)
  648 + if (ret < 0)
645 649 return -1;
646 650  
647 651 return (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
... ... @@ -671,14 +675,15 @@ static int get_stream_blocksize(BlockDriverState *bdrv)
671 675 io_header.sbp = sensebuf;
672 676 io_header.timeout = 6000; /* XXX */
673 677  
674   - ret = bdrv_pwrite(bdrv, -1, &io_header, sizeof(io_header));
675   - if (ret == -1)
  678 + ret = bdrv_sg_send_command(bdrv, &io_header, sizeof(io_header));
  679 + if (ret < 0)
676 680 return -1;
677 681  
678   - while ((ret = bdrv_pread(bdrv, -1, &io_header, sizeof(io_header))) == -1 &&
679   - errno == EINTR);
  682 + while ((ret = bdrv_sg_recv_response(bdrv, &io_header, sizeof(io_header))) < 0 &&
  683 + ret == -EINTR)
  684 + ;
680 685  
681   - if (ret == -1)
  686 + if (ret < 0)
682 687 return -1;
683 688  
684 689 return (buf[9] << 16) | (buf[10] << 8) | buf[11];
... ...