From c9b6609b69facad0cc5425d4fa7934c33d7f2e91 Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Mon, 16 Nov 2020 19:31:13 +0100 Subject: scsi: make io_timeout configurable The current code sets an infinite timeout on SG_IO requests, causing the guest to stall if the host experiences a frame loss. This patch adds an 'io_timeout' parameter for SCSIDevice to make the SG_IO timeout configurable, and also shortens the default timeout to 30 seconds to avoid infinite stalls. Signed-off-by: Hannes Reinecke Message-Id: <20201116183114.55703-3-hare@suse.de> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 6 ++++-- hw/scsi/scsi-generic.c | 17 +++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'hw') diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index ed52fcd..c4016d1 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -2624,7 +2624,7 @@ static int get_device_type(SCSIDiskState *s) cmd[4] = sizeof(buf); ret = scsi_SG_IO_FROM_DEV(s->qdev.conf.blk, cmd, sizeof(cmd), - buf, sizeof(buf)); + buf, sizeof(buf), s->qdev.io_timeout); if (ret < 0) { return -1; } @@ -2785,7 +2785,7 @@ static BlockAIOCB *scsi_block_do_sgio(SCSIBlockReq *req, /* The rest is as in scsi-generic.c. */ io_header->mx_sb_len = sizeof(r->req.sense); io_header->sbp = r->req.sense; - io_header->timeout = UINT_MAX; + io_header->timeout = s->qdev.io_timeout * 1000; io_header->usr_ptr = r; io_header->flags |= SG_FLAG_DIRECT_IO; @@ -3103,6 +3103,8 @@ static Property scsi_block_properties[] = { DEFAULT_MAX_IO_SIZE), DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, -1), + DEFINE_PROP_UINT32("io_timeout", SCSIDiskState, qdev.io_timeout, + DEFAULT_IO_TIMEOUT), DEFINE_PROP_END_OF_LIST(), }; diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c index ab22014..3dd3ccd 100644 --- a/hw/scsi/scsi-generic.c +++ b/hw/scsi/scsi-generic.c @@ -115,6 +115,8 @@ static int execute_command(BlockBackend *blk, SCSIGenericReq *r, int direction, BlockCompletionFunc *complete) { + SCSIDevice *s = r->req.dev; + r->io_header.interface_id = 'S'; r->io_header.dxfer_direction = direction; r->io_header.dxferp = r->buf; @@ -123,7 +125,7 @@ static int execute_command(BlockBackend *blk, 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.timeout = s->io_timeout * 1000; r->io_header.usr_ptr = r; r->io_header.flags |= SG_FLAG_DIRECT_IO; @@ -506,7 +508,7 @@ static int read_naa_id(const uint8_t *p, uint64_t *p_wwn) } int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size, - uint8_t *buf, uint8_t buf_size) + uint8_t *buf, uint8_t buf_size, uint32_t timeout) { sg_io_hdr_t io_header; uint8_t sensebuf[8]; @@ -521,7 +523,7 @@ int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size, io_header.cmd_len = cmd_size; io_header.mx_sb_len = sizeof(sensebuf); io_header.sbp = sensebuf; - io_header.timeout = 6000; /* XXX */ + io_header.timeout = timeout * 1000; ret = blk_ioctl(blk, SG_IO, &io_header); if (ret < 0 || io_header.driver_status || io_header.host_status) { @@ -551,7 +553,7 @@ static void scsi_generic_set_vpd_bl_emulation(SCSIDevice *s) cmd[4] = sizeof(buf); ret = scsi_SG_IO_FROM_DEV(s->conf.blk, cmd, sizeof(cmd), - buf, sizeof(buf)); + buf, sizeof(buf), s->io_timeout); if (ret < 0) { /* * Do not assume anything if we can't retrieve the @@ -587,7 +589,7 @@ static void scsi_generic_read_device_identification(SCSIDevice *s) cmd[4] = sizeof(buf); ret = scsi_SG_IO_FROM_DEV(s->conf.blk, cmd, sizeof(cmd), - buf, sizeof(buf)); + buf, sizeof(buf), s->io_timeout); if (ret < 0) { return; } @@ -638,7 +640,7 @@ static int get_stream_blocksize(BlockBackend *blk) cmd[0] = MODE_SENSE; cmd[4] = sizeof(buf); - ret = scsi_SG_IO_FROM_DEV(blk, cmd, sizeof(cmd), buf, sizeof(buf)); + ret = scsi_SG_IO_FROM_DEV(blk, cmd, sizeof(cmd), buf, sizeof(buf), 6); if (ret < 0) { return -1; } @@ -728,6 +730,7 @@ static void scsi_generic_realize(SCSIDevice *s, Error **errp) /* Only used by scsi-block, but initialize it nevertheless to be clean. */ s->default_scsi_version = -1; + s->io_timeout = DEFAULT_IO_TIMEOUT; scsi_generic_read_device_inquiry(s); } @@ -751,6 +754,8 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, static Property scsi_generic_properties[] = { DEFINE_PROP_DRIVE("drive", SCSIDevice, conf.blk), DEFINE_PROP_BOOL("share-rw", SCSIDevice, conf.share_rw, false), + DEFINE_PROP_UINT32("io_timeout", SCSIDevice, io_timeout, + DEFAULT_IO_TIMEOUT), DEFINE_PROP_END_OF_LIST(), }; -- cgit v1.1 From b2d50a3343d939a603df4436ccc41b4cf8223f88 Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Mon, 16 Nov 2020 19:31:14 +0100 Subject: scsi: add tracing for SG_IO commands Add tracepoints for SG_IO commands to allow for debugging of SG_IO commands. Signed-off-by: Hannes Reinecke Message-Id: <20201116183114.55703-4-hare@suse.de> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 3 ++- hw/scsi/scsi-generic.c | 8 +++++++- hw/scsi/trace-events | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) (limited to 'hw') diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index c4016d1..a2716b2 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -2788,7 +2788,8 @@ static BlockAIOCB *scsi_block_do_sgio(SCSIBlockReq *req, io_header->timeout = s->qdev.io_timeout * 1000; io_header->usr_ptr = r; io_header->flags |= SG_FLAG_DIRECT_IO; - + trace_scsi_disk_aio_sgio_command(r->req.tag, req->cdb[0], lba, + nb_logical_blocks, io_header->timeout); aiocb = blk_aio_ioctl(s->qdev.conf.blk, SG_IO, io_header, cb, opaque); assert(aiocb != NULL); return aiocb; diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c index 3dd3ccd..176a729 100644 --- a/hw/scsi/scsi-generic.c +++ b/hw/scsi/scsi-generic.c @@ -129,6 +129,8 @@ static int execute_command(BlockBackend *blk, r->io_header.usr_ptr = r; r->io_header.flags |= SG_FLAG_DIRECT_IO; + trace_scsi_generic_aio_sgio_command(r->req.tag, r->req.cmd.buf[0], + r->io_header.timeout); r->req.aiocb = blk_aio_ioctl(blk, SG_IO, &r->io_header, complete, r); if (r->req.aiocb == NULL) { return -EIO; @@ -525,8 +527,12 @@ int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size, io_header.sbp = sensebuf; io_header.timeout = timeout * 1000; + trace_scsi_generic_ioctl_sgio_command(cmd[0], io_header.timeout); ret = blk_ioctl(blk, SG_IO, &io_header); - if (ret < 0 || io_header.driver_status || io_header.host_status) { + if (ret < 0 || io_header.status || + io_header.driver_status || io_header.host_status) { + trace_scsi_generic_ioctl_sgio_done(cmd[0], ret, io_header.status, + io_header.host_status); return -1; } return 0; diff --git a/hw/scsi/trace-events b/hw/scsi/trace-events index 0e0aa98..9788661 100644 --- a/hw/scsi/trace-events +++ b/hw/scsi/trace-events @@ -331,6 +331,7 @@ scsi_disk_emulate_command_UNKNOWN(int cmd, const char *name) "Unknown SCSI comma scsi_disk_dma_command_READ(uint64_t lba, uint32_t len) "Read (sector %" PRId64 ", count %u)" scsi_disk_dma_command_WRITE(const char *cmd, uint64_t lba, int len) "Write %s(sector %" PRId64 ", count %u)" scsi_disk_new_request(uint32_t lun, uint32_t tag, const char *line) "Command: lun=%d tag=0x%x data=%s" +scsi_disk_aio_sgio_command(uint32_t tag, uint8_t cmd, uint64_t lba, int len, uint32_t timeout) "disk aio sgio: tag=0x%x cmd=0x%x (sector %" PRId64 ", count %d) timeout=%u" # scsi-generic.c scsi_generic_command_complete_noio(void *req, uint32_t tag, int statuc) "Command complete %p tag=0x%x status=%d" @@ -342,3 +343,6 @@ scsi_generic_write_data(uint32_t tag) "scsi_write_data tag=0x%x" scsi_generic_send_command(const char *line) "Command: data=%s" scsi_generic_realize_type(int type) "device type %d" scsi_generic_realize_blocksize(int blocksize) "block size %d" +scsi_generic_aio_sgio_command(uint32_t tag, uint8_t cmd, uint32_t timeout) "generic aio sgio: tag=0x%x cmd=0x%x timeout=%u" +scsi_generic_ioctl_sgio_command(uint8_t cmd, uint32_t timeout) "generic ioctl sgio: cmd=0x%x timeout=%u" +scsi_generic_ioctl_sgio_done(uint8_t cmd, int ret, uint8_t status, uint8_t host_status) "generic ioctl sgio: cmd=0x%x ret=%d status=0x%x host_status=0x%x" -- cgit v1.1 From 166854f7cd91d7cff23298180585209ea9d501d4 Mon Sep 17 00:00:00 2001 From: Zihao Chang Date: Tue, 3 Nov 2020 14:12:40 +0800 Subject: scsi: allow user to set werror as report 'enospc' is the default for -drive, but qemu allows user to set drive option werror. If werror of scsi-generic is set to 'report' by user, qemu will not allow vm to start. This patch allow user to set werror as 'report' for scsi-generic. Signed-off-by: Zihao Chang Reviewed-by: Fam Zheng Message-Id: <20201103061240.1364-1-changzihao1@huawei.com> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-generic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'hw') diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c index 176a729..cf7e11c 100644 --- a/hw/scsi/scsi-generic.c +++ b/hw/scsi/scsi-generic.c @@ -673,7 +673,8 @@ static void scsi_generic_realize(SCSIDevice *s, Error **errp) return; } - if (blk_get_on_error(s->conf.blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC) { + if (blk_get_on_error(s->conf.blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC && + blk_get_on_error(s->conf.blk, 0) != BLOCKDEV_ON_ERROR_REPORT) { error_setg(errp, "Device doesn't support drive option werror"); return; } -- cgit v1.1 From 6f1a5c37db5a6fc7c5c44b3e45cee6e33df31e9d Mon Sep 17 00:00:00 2001 From: Maxim Levitsky Date: Thu, 17 Dec 2020 17:00:38 +0200 Subject: virtio-scsi: don't process IO on fenced dataplane If virtio_scsi_dataplane_start fails, there is a small window when it drops the aio lock (in aio_wait_bh_oneshot) and the dataplane's AIO handler can still run during that window. This is done after the dataplane was marked as fenced, thus we use this flag to avoid it doing any IO. Signed-off-by: Maxim Levitsky Message-Id: <20201217150040.906961-2-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- hw/scsi/virtio-scsi-dataplane.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'hw') diff --git a/hw/scsi/virtio-scsi-dataplane.c b/hw/scsi/virtio-scsi-dataplane.c index 2c83a0a..4ad8793 100644 --- a/hw/scsi/virtio-scsi-dataplane.c +++ b/hw/scsi/virtio-scsi-dataplane.c @@ -52,12 +52,14 @@ void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp) static bool virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev, VirtQueue *vq) { - bool progress; + bool progress = false; VirtIOSCSI *s = VIRTIO_SCSI(vdev); virtio_scsi_acquire(s); - assert(s->ctx && s->dataplane_started); - progress = virtio_scsi_handle_cmd_vq(s, vq); + if (!s->dataplane_fenced) { + assert(s->ctx && s->dataplane_started); + progress = virtio_scsi_handle_cmd_vq(s, vq); + } virtio_scsi_release(s); return progress; } @@ -65,12 +67,14 @@ static bool virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev, static bool virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) { - bool progress; + bool progress = false; VirtIOSCSI *s = VIRTIO_SCSI(vdev); virtio_scsi_acquire(s); - assert(s->ctx && s->dataplane_started); - progress = virtio_scsi_handle_ctrl_vq(s, vq); + if (!s->dataplane_fenced) { + assert(s->ctx && s->dataplane_started); + progress = virtio_scsi_handle_ctrl_vq(s, vq); + } virtio_scsi_release(s); return progress; } @@ -78,12 +82,14 @@ static bool virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev, static bool virtio_scsi_data_plane_handle_event(VirtIODevice *vdev, VirtQueue *vq) { - bool progress; + bool progress = false; VirtIOSCSI *s = VIRTIO_SCSI(vdev); virtio_scsi_acquire(s); - assert(s->ctx && s->dataplane_started); - progress = virtio_scsi_handle_event_vq(s, vq); + if (!s->dataplane_fenced) { + assert(s->ctx && s->dataplane_started); + progress = virtio_scsi_handle_event_vq(s, vq); + } virtio_scsi_release(s); return progress; } -- cgit v1.1 From f95f61c2c9618fae7d8ea4c1d63e7416884bad52 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 24 Feb 2021 13:14:07 +0100 Subject: scsi-disk: move scsi_handle_rw_error earlier Remove the forward declaration. Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 168 ++++++++++++++++++++++++++-------------------------- 1 file changed, 83 insertions(+), 85 deletions(-) (limited to 'hw') diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index a2716b2..18ab777 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -111,8 +111,6 @@ struct SCSIDiskState { uint16_t rotation_rate; }; -static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed); - static void scsi_free_request(SCSIRequest *req) { SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); @@ -182,6 +180,89 @@ static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req) qemu_iovec_init_external(&r->qiov, &r->iov, 1); } +/* + * scsi_handle_rw_error has two return values. False means that the error + * must be ignored, true means that the error has been processed and the + * caller should not do anything else for this request. Note that + * scsi_handle_rw_error always manages its reference counts, independent + * of the return value. + */ +static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed) +{ + bool is_read = (r->req.cmd.mode == SCSI_XFER_FROM_DEV); + SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); + SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); + BlockErrorAction action = blk_get_error_action(s->qdev.conf.blk, + is_read, error); + + if (action == BLOCK_ERROR_ACTION_REPORT) { + if (acct_failed) { + block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); + } + switch (error) { + case 0: + /* A passthrough command has run and has produced sense data; check + * whether the error has to be handled by the guest or should rather + * pause the host. + */ + assert(r->status && *r->status); + if (scsi_sense_buf_is_guest_recoverable(r->req.sense, sizeof(r->req.sense))) { + /* These errors are handled by guest. */ + sdc->update_sense(&r->req); + scsi_req_complete(&r->req, *r->status); + return true; + } + error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense)); + break; +#ifdef CONFIG_LINUX + /* These errno mapping are specific to Linux. For more information: + * - scsi_decide_disposition in drivers/scsi/scsi_error.c + * - scsi_result_to_blk_status in drivers/scsi/scsi_lib.c + * - blk_errors[] in block/blk-core.c + */ + case EBADE: + /* DID_NEXUS_FAILURE -> BLK_STS_NEXUS. */ + scsi_req_complete(&r->req, RESERVATION_CONFLICT); + break; + case ENODATA: + /* DID_MEDIUM_ERROR -> BLK_STS_MEDIUM. */ + scsi_check_condition(r, SENSE_CODE(READ_ERROR)); + break; + case EREMOTEIO: + /* DID_TARGET_FAILURE -> BLK_STS_TARGET. */ + scsi_req_complete(&r->req, HARDWARE_ERROR); + break; +#endif + case ENOMEDIUM: + scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); + break; + case ENOMEM: + scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE)); + break; + case EINVAL: + scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); + break; + case ENOSPC: + scsi_check_condition(r, SENSE_CODE(SPACE_ALLOC_FAILED)); + break; + default: + scsi_check_condition(r, SENSE_CODE(IO_ERROR)); + break; + } + } + + blk_error_action(s->qdev.conf.blk, action, is_read, error); + if (action == BLOCK_ERROR_ACTION_IGNORE) { + scsi_req_complete(&r->req, 0); + return true; + } + + if (action == BLOCK_ERROR_ACTION_STOP) { + scsi_req_retry(&r->req); + } + return true; +} + static bool scsi_disk_req_check_error(SCSIDiskReq *r, int ret, bool acct_failed) { if (r->req.io_canceled) { @@ -428,89 +509,6 @@ static void scsi_read_data(SCSIRequest *req) } } -/* - * scsi_handle_rw_error has two return values. False means that the error - * must be ignored, true means that the error has been processed and the - * caller should not do anything else for this request. Note that - * scsi_handle_rw_error always manages its reference counts, independent - * of the return value. - */ -static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed) -{ - bool is_read = (r->req.cmd.mode == SCSI_XFER_FROM_DEV); - SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); - SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); - BlockErrorAction action = blk_get_error_action(s->qdev.conf.blk, - is_read, error); - - if (action == BLOCK_ERROR_ACTION_REPORT) { - if (acct_failed) { - block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); - } - switch (error) { - case 0: - /* A passthrough command has run and has produced sense data; check - * whether the error has to be handled by the guest or should rather - * pause the host. - */ - assert(r->status && *r->status); - if (scsi_sense_buf_is_guest_recoverable(r->req.sense, sizeof(r->req.sense))) { - /* These errors are handled by guest. */ - sdc->update_sense(&r->req); - scsi_req_complete(&r->req, *r->status); - return true; - } - error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense)); - break; -#ifdef CONFIG_LINUX - /* These errno mapping are specific to Linux. For more information: - * - scsi_decide_disposition in drivers/scsi/scsi_error.c - * - scsi_result_to_blk_status in drivers/scsi/scsi_lib.c - * - blk_errors[] in block/blk-core.c - */ - case EBADE: - /* DID_NEXUS_FAILURE -> BLK_STS_NEXUS. */ - scsi_req_complete(&r->req, RESERVATION_CONFLICT); - break; - case ENODATA: - /* DID_MEDIUM_ERROR -> BLK_STS_MEDIUM. */ - scsi_check_condition(r, SENSE_CODE(READ_ERROR)); - break; - case EREMOTEIO: - /* DID_TARGET_FAILURE -> BLK_STS_TARGET. */ - scsi_req_complete(&r->req, HARDWARE_ERROR); - break; -#endif - case ENOMEDIUM: - scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); - break; - case ENOMEM: - scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE)); - break; - case EINVAL: - scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); - break; - case ENOSPC: - scsi_check_condition(r, SENSE_CODE(SPACE_ALLOC_FAILED)); - break; - default: - scsi_check_condition(r, SENSE_CODE(IO_ERROR)); - break; - } - } - - blk_error_action(s->qdev.conf.blk, action, is_read, error); - if (action == BLOCK_ERROR_ACTION_IGNORE) { - scsi_req_complete(&r->req, 0); - return true; - } - - if (action == BLOCK_ERROR_ACTION_STOP) { - scsi_req_retry(&r->req); - } - return true; -} - static void scsi_write_complete_noio(SCSIDiskReq *r, int ret) { uint32_t n; -- cgit v1.1 From 424740def9a42da88550410de9a41ef07cc4a010 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 24 Feb 2021 13:16:32 +0100 Subject: scsi-disk: do not complete requests early for rerror/werror=ignore When requested to ignore errors, just do nothing and let the request complete normally. This means that the request will be accounted correctly. This is what commit 40dce4ee61 ("scsi-disk: fix rerror/werror=ignore", 2018-10-19) was supposed to do: Fixes: 40dce4ee61 ("scsi-disk: fix rerror/werror=ignore", 2018-10-19) Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'hw') diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 18ab777..36aa872 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -253,8 +253,7 @@ static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed) blk_error_action(s->qdev.conf.blk, action, is_read, error); if (action == BLOCK_ERROR_ACTION_IGNORE) { - scsi_req_complete(&r->req, 0); - return true; + return false; } if (action == BLOCK_ERROR_ACTION_STOP) { -- cgit v1.1 From d7a84021db8eeddcd5d24ab591a1434763caff6c Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 24 Feb 2021 16:30:09 +0100 Subject: scsi: introduce scsi_sense_from_errno() The new function is an extension of the switch statement in scsi-disk.c which also includes the errno cases only found in sg_io_sense_from_errno. This allows us to consolidate the errno handling. Extracted from a patch by Hannes Reinecke . Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 45 ++++++++------------------------------------- 1 file changed, 8 insertions(+), 37 deletions(-) (limited to 'hw') diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 36aa872..9c6099f 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -194,13 +194,13 @@ static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed) SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); BlockErrorAction action = blk_get_error_action(s->qdev.conf.blk, is_read, error); + SCSISense sense; if (action == BLOCK_ERROR_ACTION_REPORT) { if (acct_failed) { block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); } - switch (error) { - case 0: + if (error == 0) { /* A passthrough command has run and has produced sense data; check * whether the error has to be handled by the guest or should rather * pause the host. @@ -213,41 +213,12 @@ static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed) return true; } error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense)); - break; -#ifdef CONFIG_LINUX - /* These errno mapping are specific to Linux. For more information: - * - scsi_decide_disposition in drivers/scsi/scsi_error.c - * - scsi_result_to_blk_status in drivers/scsi/scsi_lib.c - * - blk_errors[] in block/blk-core.c - */ - case EBADE: - /* DID_NEXUS_FAILURE -> BLK_STS_NEXUS. */ - scsi_req_complete(&r->req, RESERVATION_CONFLICT); - break; - case ENODATA: - /* DID_MEDIUM_ERROR -> BLK_STS_MEDIUM. */ - scsi_check_condition(r, SENSE_CODE(READ_ERROR)); - break; - case EREMOTEIO: - /* DID_TARGET_FAILURE -> BLK_STS_TARGET. */ - scsi_req_complete(&r->req, HARDWARE_ERROR); - break; -#endif - case ENOMEDIUM: - scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); - break; - case ENOMEM: - scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE)); - break; - case EINVAL: - scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); - break; - case ENOSPC: - scsi_check_condition(r, SENSE_CODE(SPACE_ALLOC_FAILED)); - break; - default: - scsi_check_condition(r, SENSE_CODE(IO_ERROR)); - break; + } else { + int status = scsi_sense_from_errno(error, &sense); + if (status == CHECK_CONDITION) { + scsi_req_build_sense(&r->req, sense); + } + scsi_req_complete(&r->req, status); } } -- cgit v1.1 From f63c68bc0f514694a958b2e84a204b7792d28b17 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 24 Feb 2021 18:59:36 +0100 Subject: scsi-disk: pass SCSI status to scsi_handle_rw_error Instead of fishing it from *r->status, just pass the SCSI status as a positive value of the second parameter and an errno as a negative value. Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'hw') diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 9c6099f..548a529 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -187,34 +187,48 @@ static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req) * scsi_handle_rw_error always manages its reference counts, independent * of the return value. */ -static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed) +static bool scsi_handle_rw_error(SCSIDiskReq *r, int ret, bool acct_failed) { bool is_read = (r->req.cmd.mode == SCSI_XFER_FROM_DEV); SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); - BlockErrorAction action = blk_get_error_action(s->qdev.conf.blk, - is_read, error); - SCSISense sense; + SCSISense sense = SENSE_CODE(NO_SENSE); + int error = 0; + bool req_has_sense = false; + BlockErrorAction action; + int status; + if (ret < 0) { + status = scsi_sense_from_errno(-ret, &sense); + error = -ret; + } else { + /* A passthrough command has completed with nonzero status. */ + status = ret; + if (status == CHECK_CONDITION) { + req_has_sense = true; + error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense)); + } else { + error = EINVAL; + } + } + + action = blk_get_error_action(s->qdev.conf.blk, is_read, error); if (action == BLOCK_ERROR_ACTION_REPORT) { if (acct_failed) { block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); } - if (error == 0) { + if (req_has_sense) { /* A passthrough command has run and has produced sense data; check * whether the error has to be handled by the guest or should rather * pause the host. */ - assert(r->status && *r->status); if (scsi_sense_buf_is_guest_recoverable(r->req.sense, sizeof(r->req.sense))) { /* These errors are handled by guest. */ sdc->update_sense(&r->req); - scsi_req_complete(&r->req, *r->status); + scsi_req_complete(&r->req, status); return true; } - error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense)); } else { - int status = scsi_sense_from_errno(error, &sense); if (status == CHECK_CONDITION) { scsi_req_build_sense(&r->req, sense); } @@ -240,8 +254,10 @@ static bool scsi_disk_req_check_error(SCSIDiskReq *r, int ret, bool acct_failed) return true; } - if (ret < 0 || (r->status && *r->status)) { - return scsi_handle_rw_error(r, -ret, acct_failed); + if (ret < 0) { + return scsi_handle_rw_error(r, ret, acct_failed); + } else if (r->status && *r->status) { + return scsi_handle_rw_error(r, *r->status, acct_failed); } return false; -- cgit v1.1 From 782a78c9e994c2be23467262f50e885a0eb0d9fc Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 24 Feb 2021 12:57:44 +0100 Subject: scsi-disk: pass guest recoverable errors through even for rerror=stop Right now, recoverable sense values are only passed directly to the guest only for rerror=report. However, when rerror/werror are 'stop' we still don't want the host to be involved on every UNIT ATTENTION (especially considered that the QMP event will not have enough information to act on the report). Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'hw') diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 548a529..a5a58d7 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -212,39 +212,44 @@ static bool scsi_handle_rw_error(SCSIDiskReq *r, int ret, bool acct_failed) } } - action = blk_get_error_action(s->qdev.conf.blk, is_read, error); - if (action == BLOCK_ERROR_ACTION_REPORT) { + /* + * Check whether the error has to be handled by the guest or should + * rather follow the rerror=/werror= settings. Guest-handled errors + * are usually retried immediately, so do not post them to QMP and + * do not account them as failed I/O. + */ + if (req_has_sense && + scsi_sense_buf_is_guest_recoverable(r->req.sense, sizeof(r->req.sense))) { + action = BLOCK_ERROR_ACTION_REPORT; + acct_failed = false; + } else { + action = blk_get_error_action(s->qdev.conf.blk, is_read, error); + blk_error_action(s->qdev.conf.blk, action, is_read, error); + } + + switch (action) { + case BLOCK_ERROR_ACTION_REPORT: if (acct_failed) { block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); } if (req_has_sense) { - /* A passthrough command has run and has produced sense data; check - * whether the error has to be handled by the guest or should rather - * pause the host. - */ - if (scsi_sense_buf_is_guest_recoverable(r->req.sense, sizeof(r->req.sense))) { - /* These errors are handled by guest. */ - sdc->update_sense(&r->req); - scsi_req_complete(&r->req, status); - return true; - } - } else { - if (status == CHECK_CONDITION) { - scsi_req_build_sense(&r->req, sense); - } - scsi_req_complete(&r->req, status); + sdc->update_sense(&r->req); + } else if (status == CHECK_CONDITION) { + scsi_req_build_sense(&r->req, sense); } - } + scsi_req_complete(&r->req, status); + return true; - blk_error_action(s->qdev.conf.blk, action, is_read, error); - if (action == BLOCK_ERROR_ACTION_IGNORE) { + case BLOCK_ERROR_ACTION_IGNORE: return false; - } - if (action == BLOCK_ERROR_ACTION_STOP) { + case BLOCK_ERROR_ACTION_STOP: scsi_req_retry(&r->req); + return true; + + default: + g_assert_not_reached(); } - return true; } static bool scsi_disk_req_check_error(SCSIDiskReq *r, int ret, bool acct_failed) -- cgit v1.1 From 17ea26c2d80a695b4d3af9ae2eaa438095029773 Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Mon, 16 Nov 2020 19:40:36 +0100 Subject: scsi: drop 'result' argument from command_complete callback The command complete callback has a SCSIRequest as the first argument, and the status field of that structure is identical to the 'status' argument. So drop the argument from the callback. Signed-off-by: Hannes Reinecke Message-Id: <20201116184041.60465-3-hare@suse.de> Signed-off-by: Paolo Bonzini --- hw/scsi/esp-pci.c | 5 ++--- hw/scsi/esp.c | 7 +++---- hw/scsi/lsi53c895a.c | 6 +++--- hw/scsi/megasas.c | 6 ++---- hw/scsi/mptsas.c | 5 +++-- hw/scsi/scsi-bus.c | 2 +- hw/scsi/spapr_vscsi.c | 12 ++++++------ hw/scsi/virtio-scsi.c | 5 ++--- hw/scsi/vmw_pvscsi.c | 4 ++-- hw/usb/dev-storage.c | 6 +++--- hw/usb/dev-uas.c | 7 +++---- 11 files changed, 30 insertions(+), 35 deletions(-) (limited to 'hw') diff --git a/hw/scsi/esp-pci.c b/hw/scsi/esp-pci.c index 2ce96dc..4d7c2ca 100644 --- a/hw/scsi/esp-pci.c +++ b/hw/scsi/esp-pci.c @@ -329,13 +329,12 @@ static const VMStateDescription vmstate_esp_pci_scsi = { } }; -static void esp_pci_command_complete(SCSIRequest *req, uint32_t status, - size_t resid) +static void esp_pci_command_complete(SCSIRequest *req, size_t resid) { ESPState *s = req->hba_private; PCIESPState *pci = container_of(s, PCIESPState, esp); - esp_command_complete(req, status, resid); + esp_command_complete(req, resid); pci->dma_regs[DMA_WBC] = 0; pci->dma_regs[DMA_STAT] |= DMA_STAT_DONE; } diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c index b84e0fe..93d9c9c 100644 --- a/hw/scsi/esp.c +++ b/hw/scsi/esp.c @@ -485,8 +485,7 @@ static void esp_report_command_complete(ESPState *s, uint32_t status) } } -void esp_command_complete(SCSIRequest *req, uint32_t status, - size_t resid) +void esp_command_complete(SCSIRequest *req, size_t resid) { ESPState *s = req->hba_private; @@ -495,11 +494,11 @@ void esp_command_complete(SCSIRequest *req, uint32_t status, * interrupt has been handled. */ trace_esp_command_complete_deferred(); - s->deferred_status = status; + s->deferred_status = req->status; s->deferred_complete = true; return; } - esp_report_command_complete(s, status); + esp_report_command_complete(s, req->status); } void esp_transfer_data(SCSIRequest *req, uint32_t len) diff --git a/hw/scsi/lsi53c895a.c b/hw/scsi/lsi53c895a.c index 7d13c7d..a4e58580 100644 --- a/hw/scsi/lsi53c895a.c +++ b/hw/scsi/lsi53c895a.c @@ -787,14 +787,14 @@ static int lsi_queue_req(LSIState *s, SCSIRequest *req, uint32_t len) } /* Callback to indicate that the SCSI layer has completed a command. */ -static void lsi_command_complete(SCSIRequest *req, uint32_t status, size_t resid) +static void lsi_command_complete(SCSIRequest *req, size_t resid) { LSIState *s = LSI53C895A(req->bus->qbus.parent); int out; out = (s->sstat1 & PHASE_MASK) == PHASE_DO; - trace_lsi_command_complete(status); - s->status = status; + trace_lsi_command_complete(req->status); + s->status = req->status; s->command_complete = 2; if (s->waiting && s->dbc != 0) { /* Raise phase mismatch for short transfers. */ diff --git a/hw/scsi/megasas.c b/hw/scsi/megasas.c index 5bfc92f..8f2389d 100644 --- a/hw/scsi/megasas.c +++ b/hw/scsi/megasas.c @@ -1852,13 +1852,12 @@ static void megasas_xfer_complete(SCSIRequest *req, uint32_t len) } } -static void megasas_command_complete(SCSIRequest *req, uint32_t status, - size_t resid) +static void megasas_command_complete(SCSIRequest *req, size_t resid) { MegasasCmd *cmd = req->hba_private; uint8_t cmd_status = MFI_STAT_OK; - trace_megasas_command_complete(cmd->index, status, resid); + trace_megasas_command_complete(cmd->index, req->status, resid); if (req->io_canceled) { return; @@ -1873,7 +1872,6 @@ static void megasas_command_complete(SCSIRequest *req, uint32_t status, return; } } else { - req->status = status; trace_megasas_scsi_complete(cmd->index, req->status, cmd->iov_size, req->cmd.xfer); if (req->status != GOOD) { diff --git a/hw/scsi/mptsas.c b/hw/scsi/mptsas.c index f866165..7416e78 100644 --- a/hw/scsi/mptsas.c +++ b/hw/scsi/mptsas.c @@ -1133,7 +1133,7 @@ static QEMUSGList *mptsas_get_sg_list(SCSIRequest *sreq) } static void mptsas_command_complete(SCSIRequest *sreq, - uint32_t status, size_t resid) + size_t resid) { MPTSASRequest *req = sreq->hba_private; MPTSASState *s = req->dev; @@ -1143,7 +1143,8 @@ static void mptsas_command_complete(SCSIRequest *sreq, hwaddr sense_buffer_addr = req->dev->sense_buffer_high_addr | req->scsi_io.SenseBufferLowAddr; - trace_mptsas_command_complete(s, req->scsi_io.MsgContext, status, resid); + trace_mptsas_command_complete(s, req->scsi_io.MsgContext, + sreq->status, resid); sense_len = scsi_req_get_sense(sreq, sense_buf, SCSI_SENSE_BUF_SIZE); if (sense_len > 0) { diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index c349fb7..dc4141e 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -1483,7 +1483,7 @@ void scsi_req_complete(SCSIRequest *req, int status) scsi_req_ref(req); scsi_req_dequeue(req); - req->bus->info->complete(req, req->status, req->resid); + req->bus->info->complete(req, req->resid); /* Cancelled requests might end up being completed instead of cancelled */ notifier_list_notify(&req->cancel_notifiers, req); diff --git a/hw/scsi/spapr_vscsi.c b/hw/scsi/spapr_vscsi.c index 4aa0224..ca5c13c 100644 --- a/hw/scsi/spapr_vscsi.c +++ b/hw/scsi/spapr_vscsi.c @@ -551,19 +551,19 @@ static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len) } /* Callback to indicate that the SCSI layer has completed a transfer. */ -static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid) +static void vscsi_command_complete(SCSIRequest *sreq, size_t resid) { VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent); vscsi_req *req = sreq->hba_private; int32_t res_in = 0, res_out = 0; - trace_spapr_vscsi_command_complete(sreq->tag, status, req); + trace_spapr_vscsi_command_complete(sreq->tag, sreq->status, req); if (req == NULL) { fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag); return; } - if (status == CHECK_CONDITION) { + if (sreq->status == CHECK_CONDITION) { req->senselen = scsi_req_get_sense(req->sreq, req->sense, sizeof(req->sense)); trace_spapr_vscsi_command_complete_sense_data1(req->senselen, @@ -574,8 +574,8 @@ static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t re req->sense[12], req->sense[13], req->sense[14], req->sense[15]); } - trace_spapr_vscsi_command_complete_status(status); - if (status == 0) { + trace_spapr_vscsi_command_complete_status(sreq->status); + if (sreq->status == 0) { /* We handle overflows, not underflows for normal commands, * but hopefully nobody cares */ @@ -585,7 +585,7 @@ static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t re res_in = req->data_len; } } - vscsi_send_rsp(s, req, status, res_in, res_out); + vscsi_send_rsp(s, req, sreq->status, res_in, res_out); vscsi_put_req(req); } diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 99ff261..358c0e7 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -500,8 +500,7 @@ static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req) virtio_scsi_complete_req(req); } -static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status, - size_t resid) +static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid) { VirtIOSCSIReq *req = r->hba_private; uint8_t sense[SCSI_SENSE_BUF_SIZE]; @@ -513,7 +512,7 @@ static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status, } req->resp.cmd.response = VIRTIO_SCSI_S_OK; - req->resp.cmd.status = status; + req->resp.cmd.status = r->status; if (req->resp.cmd.status == GOOD) { req->resp.cmd.resid = virtio_tswap32(vdev, resid); } else { diff --git a/hw/scsi/vmw_pvscsi.c b/hw/scsi/vmw_pvscsi.c index a63d25d..0da378e 100644 --- a/hw/scsi/vmw_pvscsi.c +++ b/hw/scsi/vmw_pvscsi.c @@ -511,7 +511,7 @@ pvscsi_write_sense(PVSCSIRequest *r, uint8_t *sense, int len) } static void -pvscsi_command_complete(SCSIRequest *req, uint32_t status, size_t resid) +pvscsi_command_complete(SCSIRequest *req, size_t resid) { PVSCSIRequest *pvscsi_req = req->hba_private; PVSCSIState *s; @@ -528,7 +528,7 @@ pvscsi_command_complete(SCSIRequest *req, uint32_t status, size_t resid) pvscsi_req->cmp.hostStatus = BTSTAT_DATARUN; } - pvscsi_req->cmp.scsiStatus = status; + pvscsi_req->cmp.scsiStatus = req->status; if (pvscsi_req->cmp.scsiStatus == CHECK_CONDITION) { uint8_t sense[SCSI_SENSE_BUF_SIZE]; int sense_len = diff --git a/hw/usb/dev-storage.c b/hw/usb/dev-storage.c index c49e8b8..a5f76fc 100644 --- a/hw/usb/dev-storage.c +++ b/hw/usb/dev-storage.c @@ -277,17 +277,17 @@ static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len) } } -static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, size_t resid) +static void usb_msd_command_complete(SCSIRequest *req, size_t resid) { MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); USBPacket *p = s->packet; - trace_usb_msd_cmd_complete(status, req->tag); + trace_usb_msd_cmd_complete(req->status, req->tag); s->csw.sig = cpu_to_le32(0x53425355); s->csw.tag = cpu_to_le32(req->tag); s->csw.residue = cpu_to_le32(s->data_len); - s->csw.status = status != 0; + s->csw.status = req->status != 0; if (s->packet) { if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) { diff --git a/hw/usb/dev-uas.c b/hw/usb/dev-uas.c index a51402b..d2bd85d 100644 --- a/hw/usb/dev-uas.c +++ b/hw/usb/dev-uas.c @@ -598,17 +598,16 @@ static void usb_uas_scsi_transfer_data(SCSIRequest *r, uint32_t len) } } -static void usb_uas_scsi_command_complete(SCSIRequest *r, - uint32_t status, size_t resid) +static void usb_uas_scsi_command_complete(SCSIRequest *r, size_t resid) { UASRequest *req = r->hba_private; - trace_usb_uas_scsi_complete(req->uas->dev.addr, req->tag, status, resid); + trace_usb_uas_scsi_complete(req->uas->dev.addr, req->tag, r->status, resid); req->complete = true; if (req->data) { usb_uas_complete_data_packet(req); } - usb_uas_queue_sense(req, status); + usb_uas_queue_sense(req, r->status); scsi_req_unref(req->req); } -- cgit v1.1