Separate the initialization of the `sg_io_hdr' struct in two parts: one part that fills the struct with sane defaults, and another part that prepares it for an SG_IO request with DIRECT IO and a single buffer. The first part can also be reused later on by the code that uses scatter-gather lists.
Signed-off-by: Alex Pyrgiotis <apyr...@arrikto.com> Signed-off-by: Dimitris Aragiorgis <dim...@arrikto.com> diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c index f24f472..71c0110 100644 --- a/hw/scsi/scsi-generic.c +++ b/hw/scsi/scsi-generic.c @@ -58,6 +58,30 @@ typedef struct SCSIGenericReq { sg_io_hdr_t io_header; } SCSIGenericReq; +/* Fill an io header with sane defaults. */ +static void _init_io_header(SCSIGenericReq *r, int direction) +{ + r->io_header.interface_id = 'S'; + r->io_header.dxfer_direction = direction; + r->io_header.dxfer_len = r->buflen; + r->io_header.cmdp = r->req.cmd.buf; + r->io_header.cmd_len = r->req.cmd.len; + r->io_header.mx_sb_len = sizeof(r->req.sense); + r->io_header.sbp = r->req.sense; + r->io_header.timeout = MAX_UINT; + r->io_header.usr_ptr = r; +} + +/* Create an io_header for buf* operations. */ +static void scsi_buf_init_io_header(SCSIGenericReq *r, int direction) +{ + _init_io_header(r, direction); + + r->io_header.iovec_count = 0; + r->io_header.dxferp = r->buf; + r->io_header.flags |= SG_FLAG_DIRECT_IO; +} + static void scsi_generic_save_request(QEMUFile *f, SCSIRequest *req) { SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); @@ -155,17 +179,7 @@ static int execute_command(BlockBackend *blk, SCSIGenericReq *r, int direction, BlockCompletionFunc *complete) { - 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->req.cmd.buf; - r->io_header.cmd_len = r->req.cmd.len; - r->io_header.mx_sb_len = sizeof(r->req.sense); - r->io_header.sbp = r->req.sense; - r->io_header.timeout = MAX_UINT; - r->io_header.usr_ptr = r; - r->io_header.flags |= SG_FLAG_DIRECT_IO; + scsi_buf_init_io_header(r, direction); r->req.aiocb = blk_aio_ioctl(blk, SG_IO, &r->io_header, complete, r); if (r->req.aiocb == NULL) { -- 2.6.2