4 * Copyright IBM, Corp. 2010
5 * Copyright Red Hat, Inc. 2011
8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "standard-headers/linux/virtio_ids.h"
19 #include "hw/virtio/virtio-scsi.h"
20 #include "migration/qemu-file-types.h"
21 #include "qemu/error-report.h"
23 #include "qemu/module.h"
24 #include "sysemu/block-backend.h"
25 #include "hw/qdev-properties.h"
26 #include "hw/scsi/scsi.h"
27 #include "scsi/constants.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/virtio/virtio-access.h"
31 static inline int virtio_scsi_get_lun(uint8_t *lun
)
33 return ((lun
[2] << 8) | lun
[3]) & 0x3FFF;
36 static inline SCSIDevice
*virtio_scsi_device_find(VirtIOSCSI
*s
, uint8_t *lun
)
41 if (lun
[2] != 0 && !(lun
[2] >= 0x40 && lun
[2] < 0x80)) {
44 return scsi_device_find(&s
->bus
, 0, lun
[1], virtio_scsi_get_lun(lun
));
47 void virtio_scsi_init_req(VirtIOSCSI
*s
, VirtQueue
*vq
, VirtIOSCSIReq
*req
)
49 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
50 const size_t zero_skip
=
51 offsetof(VirtIOSCSIReq
, resp_iov
) + sizeof(req
->resp_iov
);
55 qemu_sglist_init(&req
->qsgl
, DEVICE(s
), 8, vdev
->dma_as
);
56 qemu_iovec_init(&req
->resp_iov
, 1);
57 memset((uint8_t *)req
+ zero_skip
, 0, sizeof(*req
) - zero_skip
);
60 void virtio_scsi_free_req(VirtIOSCSIReq
*req
)
62 qemu_iovec_destroy(&req
->resp_iov
);
63 qemu_sglist_destroy(&req
->qsgl
);
67 static void virtio_scsi_complete_req(VirtIOSCSIReq
*req
)
69 VirtIOSCSI
*s
= req
->dev
;
70 VirtQueue
*vq
= req
->vq
;
71 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
73 qemu_iovec_from_buf(&req
->resp_iov
, 0, &req
->resp
, req
->resp_size
);
74 virtqueue_push(vq
, &req
->elem
, req
->qsgl
.size
+ req
->resp_iov
.size
);
75 if (s
->dataplane_started
&& !s
->dataplane_fenced
) {
76 virtio_notify_irqfd(vdev
, vq
);
78 virtio_notify(vdev
, vq
);
82 req
->sreq
->hba_private
= NULL
;
83 scsi_req_unref(req
->sreq
);
85 virtio_scsi_free_req(req
);
88 static void virtio_scsi_bad_req(VirtIOSCSIReq
*req
)
90 virtio_error(VIRTIO_DEVICE(req
->dev
), "wrong size for virtio-scsi headers");
91 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
92 virtio_scsi_free_req(req
);
95 static size_t qemu_sgl_concat(VirtIOSCSIReq
*req
, struct iovec
*iov
,
96 hwaddr
*addr
, int num
, size_t skip
)
98 QEMUSGList
*qsgl
= &req
->qsgl
;
102 if (skip
>= iov
->iov_len
) {
103 skip
-= iov
->iov_len
;
105 qemu_sglist_add(qsgl
, *addr
+ skip
, iov
->iov_len
- skip
);
106 copied
+= iov
->iov_len
- skip
;
118 static int virtio_scsi_parse_req(VirtIOSCSIReq
*req
,
119 unsigned req_size
, unsigned resp_size
)
121 VirtIODevice
*vdev
= (VirtIODevice
*) req
->dev
;
122 size_t in_size
, out_size
;
124 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
125 &req
->req
, req_size
) < req_size
) {
129 if (qemu_iovec_concat_iov(&req
->resp_iov
,
130 req
->elem
.in_sg
, req
->elem
.in_num
, 0,
131 resp_size
) < resp_size
) {
135 req
->resp_size
= resp_size
;
137 /* Old BIOSes left some padding by mistake after the req_size/resp_size.
138 * As a workaround, always consider the first buffer as the virtio-scsi
139 * request/response, making the payload start at the second element
142 * The actual length of the response header, stored in req->resp_size,
145 * TODO: always disable this workaround for virtio 1.0 devices.
147 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_ANY_LAYOUT
)) {
148 if (req
->elem
.out_num
) {
149 req_size
= req
->elem
.out_sg
[0].iov_len
;
151 if (req
->elem
.in_num
) {
152 resp_size
= req
->elem
.in_sg
[0].iov_len
;
156 out_size
= qemu_sgl_concat(req
, req
->elem
.out_sg
,
157 &req
->elem
.out_addr
[0], req
->elem
.out_num
,
159 in_size
= qemu_sgl_concat(req
, req
->elem
.in_sg
,
160 &req
->elem
.in_addr
[0], req
->elem
.in_num
,
163 if (out_size
&& in_size
) {
168 req
->mode
= SCSI_XFER_TO_DEV
;
169 } else if (in_size
) {
170 req
->mode
= SCSI_XFER_FROM_DEV
;
176 static VirtIOSCSIReq
*virtio_scsi_pop_req(VirtIOSCSI
*s
, VirtQueue
*vq
)
178 VirtIOSCSICommon
*vs
= (VirtIOSCSICommon
*)s
;
181 req
= virtqueue_pop(vq
, sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
185 virtio_scsi_init_req(s
, vq
, req
);
189 static void virtio_scsi_save_request(QEMUFile
*f
, SCSIRequest
*sreq
)
191 VirtIOSCSIReq
*req
= sreq
->hba_private
;
192 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(req
->dev
);
193 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
194 uint32_t n
= virtio_get_queue_index(req
->vq
) - VIRTIO_SCSI_VQ_NUM_FIXED
;
196 assert(n
< vs
->conf
.num_queues
);
197 qemu_put_be32s(f
, &n
);
198 qemu_put_virtqueue_element(vdev
, f
, &req
->elem
);
201 static void *virtio_scsi_load_request(QEMUFile
*f
, SCSIRequest
*sreq
)
203 SCSIBus
*bus
= sreq
->bus
;
204 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
205 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
206 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
210 qemu_get_be32s(f
, &n
);
211 assert(n
< vs
->conf
.num_queues
);
212 req
= qemu_get_virtqueue_element(vdev
, f
,
213 sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
214 virtio_scsi_init_req(s
, vs
->cmd_vqs
[n
], req
);
216 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
217 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
) < 0) {
218 error_report("invalid SCSI request migration data");
224 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
) {
225 assert(req
->sreq
->cmd
.mode
== req
->mode
);
232 VirtIOSCSIReq
*tmf_req
;
233 } VirtIOSCSICancelNotifier
;
235 static void virtio_scsi_cancel_notify(Notifier
*notifier
, void *data
)
237 VirtIOSCSICancelNotifier
*n
= container_of(notifier
,
238 VirtIOSCSICancelNotifier
,
241 if (--n
->tmf_req
->remaining
== 0) {
242 virtio_scsi_complete_req(n
->tmf_req
);
247 static inline void virtio_scsi_ctx_check(VirtIOSCSI
*s
, SCSIDevice
*d
)
249 if (s
->dataplane_started
&& d
&& blk_is_available(d
->conf
.blk
)) {
250 assert(blk_get_aio_context(d
->conf
.blk
) == s
->ctx
);
254 /* Return 0 if the request is ready to be completed and return to guest;
255 * -EINPROGRESS if the request is submitted and will be completed later, in the
256 * case of async cancellation. */
257 static int virtio_scsi_do_tmf(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
259 SCSIDevice
*d
= virtio_scsi_device_find(s
, req
->req
.tmf
.lun
);
260 SCSIRequest
*r
, *next
;
265 virtio_scsi_ctx_check(s
, d
);
266 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
267 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_OK
;
270 * req->req.tmf has the QEMU_PACKED attribute. Don't use virtio_tswap32s()
271 * to avoid compiler errors.
273 req
->req
.tmf
.subtype
=
274 virtio_tswap32(VIRTIO_DEVICE(s
), req
->req
.tmf
.subtype
);
276 switch (req
->req
.tmf
.subtype
) {
277 case VIRTIO_SCSI_T_TMF_ABORT_TASK
:
278 case VIRTIO_SCSI_T_TMF_QUERY_TASK
:
282 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
285 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
286 VirtIOSCSIReq
*cmd_req
= r
->hba_private
;
287 if (cmd_req
&& cmd_req
->req
.cmd
.tag
== req
->req
.tmf
.tag
) {
293 * Assert that the request has not been completed yet, we
294 * check for it in the loop above.
296 assert(r
->hba_private
);
297 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK
) {
298 /* "If the specified command is present in the task set, then
299 * return a service response set to FUNCTION SUCCEEDED".
301 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
303 VirtIOSCSICancelNotifier
*notifier
;
306 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
307 notifier
->tmf_req
= req
;
308 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
309 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
315 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
319 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
323 qdev_reset_all(&d
->qdev
);
327 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET
:
328 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET
:
329 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
:
333 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
337 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
338 * This way, if the bus starts calling back to the notifiers
339 * even before we finish the loop, virtio_scsi_cancel_notify
340 * will not complete the TMF too early.
343 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
344 if (r
->hba_private
) {
345 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
) {
346 /* "If there is any command present in the task set, then
347 * return a service response set to FUNCTION SUCCEEDED".
349 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
352 VirtIOSCSICancelNotifier
*notifier
;
355 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
356 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
357 notifier
->tmf_req
= req
;
358 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
362 if (--req
->remaining
> 0) {
367 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
368 target
= req
->req
.tmf
.lun
[1];
372 QTAILQ_FOREACH_RCU(kid
, &s
->bus
.qbus
.children
, sibling
) {
373 d
= SCSI_DEVICE(kid
->child
);
374 if (d
->channel
== 0 && d
->id
== target
) {
375 qdev_reset_all(&d
->qdev
);
383 case VIRTIO_SCSI_T_TMF_CLEAR_ACA
:
385 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_REJECTED
;
392 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
396 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
400 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
402 VirtIODevice
*vdev
= (VirtIODevice
*)s
;
406 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
407 &type
, sizeof(type
)) < sizeof(type
)) {
408 virtio_scsi_bad_req(req
);
412 virtio_tswap32s(vdev
, &type
);
413 if (type
== VIRTIO_SCSI_T_TMF
) {
414 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlTMFReq
),
415 sizeof(VirtIOSCSICtrlTMFResp
)) < 0) {
416 virtio_scsi_bad_req(req
);
419 r
= virtio_scsi_do_tmf(s
, req
);
422 } else if (type
== VIRTIO_SCSI_T_AN_QUERY
||
423 type
== VIRTIO_SCSI_T_AN_SUBSCRIBE
) {
424 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlANReq
),
425 sizeof(VirtIOSCSICtrlANResp
)) < 0) {
426 virtio_scsi_bad_req(req
);
429 req
->resp
.an
.event_actual
= 0;
430 req
->resp
.an
.response
= VIRTIO_SCSI_S_OK
;
434 virtio_scsi_complete_req(req
);
436 assert(r
== -EINPROGRESS
);
440 bool virtio_scsi_handle_ctrl_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
443 bool progress
= false;
445 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
447 virtio_scsi_handle_ctrl_req(s
, req
);
452 static void virtio_scsi_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
454 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
457 virtio_device_start_ioeventfd(vdev
);
458 if (!s
->dataplane_fenced
) {
462 virtio_scsi_acquire(s
);
463 virtio_scsi_handle_ctrl_vq(s
, vq
);
464 virtio_scsi_release(s
);
467 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq
*req
)
469 /* Sense data is not in req->resp and is copied separately
470 * in virtio_scsi_command_complete.
472 req
->resp_size
= sizeof(VirtIOSCSICmdResp
);
473 virtio_scsi_complete_req(req
);
476 static void virtio_scsi_command_complete(SCSIRequest
*r
, uint32_t status
,
479 VirtIOSCSIReq
*req
= r
->hba_private
;
480 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
482 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
484 if (r
->io_canceled
) {
488 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OK
;
489 req
->resp
.cmd
.status
= status
;
490 if (req
->resp
.cmd
.status
== GOOD
) {
491 req
->resp
.cmd
.resid
= virtio_tswap32(vdev
, resid
);
493 req
->resp
.cmd
.resid
= 0;
494 sense_len
= scsi_req_get_sense(r
, sense
, sizeof(sense
));
495 sense_len
= MIN(sense_len
, req
->resp_iov
.size
- sizeof(req
->resp
.cmd
));
496 qemu_iovec_from_buf(&req
->resp_iov
, sizeof(req
->resp
.cmd
),
498 req
->resp
.cmd
.sense_len
= virtio_tswap32(vdev
, sense_len
);
500 virtio_scsi_complete_cmd_req(req
);
503 static int virtio_scsi_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
,
504 uint8_t *buf
, void *hba_private
)
506 VirtIOSCSIReq
*req
= hba_private
;
509 cmd
->len
= MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE
, SCSI_CMD_BUF_SIZE
);
510 memcpy(cmd
->buf
, buf
, cmd
->len
);
513 /* Extract the direction and mode directly from the request, for
514 * host device passthrough.
516 cmd
->xfer
= req
->qsgl
.size
;
517 cmd
->mode
= req
->mode
;
521 static QEMUSGList
*virtio_scsi_get_sg_list(SCSIRequest
*r
)
523 VirtIOSCSIReq
*req
= r
->hba_private
;
528 static void virtio_scsi_request_cancelled(SCSIRequest
*r
)
530 VirtIOSCSIReq
*req
= r
->hba_private
;
535 if (req
->dev
->resetting
) {
536 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_RESET
;
538 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_ABORTED
;
540 virtio_scsi_complete_cmd_req(req
);
543 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq
*req
)
545 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_FAILURE
;
546 virtio_scsi_complete_cmd_req(req
);
549 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
551 VirtIOSCSICommon
*vs
= &s
->parent_obj
;
555 rc
= virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
556 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
);
558 if (rc
== -ENOTSUP
) {
559 virtio_scsi_fail_cmd_req(req
);
562 virtio_scsi_bad_req(req
);
567 d
= virtio_scsi_device_find(s
, req
->req
.cmd
.lun
);
569 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
570 virtio_scsi_complete_cmd_req(req
);
573 virtio_scsi_ctx_check(s
, d
);
574 req
->sreq
= scsi_req_new(d
, req
->req
.cmd
.tag
,
575 virtio_scsi_get_lun(req
->req
.cmd
.lun
),
576 req
->req
.cmd
.cdb
, req
);
578 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
579 && (req
->sreq
->cmd
.mode
!= req
->mode
||
580 req
->sreq
->cmd
.xfer
> req
->qsgl
.size
)) {
581 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OVERRUN
;
582 virtio_scsi_complete_cmd_req(req
);
585 scsi_req_ref(req
->sreq
);
586 blk_io_plug(d
->conf
.blk
);
590 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
592 SCSIRequest
*sreq
= req
->sreq
;
593 if (scsi_req_enqueue(sreq
)) {
594 scsi_req_continue(sreq
);
596 blk_io_unplug(sreq
->dev
->conf
.blk
);
597 scsi_req_unref(sreq
);
600 bool virtio_scsi_handle_cmd_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
602 VirtIOSCSIReq
*req
, *next
;
604 bool suppress_notifications
= virtio_queue_get_notification(vq
);
605 bool progress
= false;
607 QTAILQ_HEAD(, VirtIOSCSIReq
) reqs
= QTAILQ_HEAD_INITIALIZER(reqs
);
610 if (suppress_notifications
) {
611 virtio_queue_set_notification(vq
, 0);
614 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
616 ret
= virtio_scsi_handle_cmd_req_prepare(s
, req
);
618 QTAILQ_INSERT_TAIL(&reqs
, req
, next
);
619 } else if (ret
== -EINVAL
) {
620 /* The device is broken and shouldn't process any request */
621 while (!QTAILQ_EMPTY(&reqs
)) {
622 req
= QTAILQ_FIRST(&reqs
);
623 QTAILQ_REMOVE(&reqs
, req
, next
);
624 blk_io_unplug(req
->sreq
->dev
->conf
.blk
);
625 scsi_req_unref(req
->sreq
);
626 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
627 virtio_scsi_free_req(req
);
632 if (suppress_notifications
) {
633 virtio_queue_set_notification(vq
, 1);
635 } while (ret
!= -EINVAL
&& !virtio_queue_empty(vq
));
637 QTAILQ_FOREACH_SAFE(req
, &reqs
, next
, next
) {
638 virtio_scsi_handle_cmd_req_submit(s
, req
);
643 static void virtio_scsi_handle_cmd(VirtIODevice
*vdev
, VirtQueue
*vq
)
645 /* use non-QOM casts in the data path */
646 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
649 virtio_device_start_ioeventfd(vdev
);
650 if (!s
->dataplane_fenced
) {
654 virtio_scsi_acquire(s
);
655 virtio_scsi_handle_cmd_vq(s
, vq
);
656 virtio_scsi_release(s
);
659 static void virtio_scsi_get_config(VirtIODevice
*vdev
,
662 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
663 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(vdev
);
665 virtio_stl_p(vdev
, &scsiconf
->num_queues
, s
->conf
.num_queues
);
666 virtio_stl_p(vdev
, &scsiconf
->seg_max
,
667 s
->conf
.seg_max_adjust ? s
->conf
.virtqueue_size
- 2 : 128 - 2);
668 virtio_stl_p(vdev
, &scsiconf
->max_sectors
, s
->conf
.max_sectors
);
669 virtio_stl_p(vdev
, &scsiconf
->cmd_per_lun
, s
->conf
.cmd_per_lun
);
670 virtio_stl_p(vdev
, &scsiconf
->event_info_size
, sizeof(VirtIOSCSIEvent
));
671 virtio_stl_p(vdev
, &scsiconf
->sense_size
, s
->sense_size
);
672 virtio_stl_p(vdev
, &scsiconf
->cdb_size
, s
->cdb_size
);
673 virtio_stw_p(vdev
, &scsiconf
->max_channel
, VIRTIO_SCSI_MAX_CHANNEL
);
674 virtio_stw_p(vdev
, &scsiconf
->max_target
, VIRTIO_SCSI_MAX_TARGET
);
675 virtio_stl_p(vdev
, &scsiconf
->max_lun
, VIRTIO_SCSI_MAX_LUN
);
678 static void virtio_scsi_set_config(VirtIODevice
*vdev
,
679 const uint8_t *config
)
681 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
682 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
684 if ((uint32_t) virtio_ldl_p(vdev
, &scsiconf
->sense_size
) >= 65536 ||
685 (uint32_t) virtio_ldl_p(vdev
, &scsiconf
->cdb_size
) >= 256) {
687 "bad data written to virtio-scsi configuration space");
691 vs
->sense_size
= virtio_ldl_p(vdev
, &scsiconf
->sense_size
);
692 vs
->cdb_size
= virtio_ldl_p(vdev
, &scsiconf
->cdb_size
);
695 static uint64_t virtio_scsi_get_features(VirtIODevice
*vdev
,
696 uint64_t requested_features
,
699 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
701 /* Firstly sync all virtio-scsi possible supported features */
702 requested_features
|= s
->host_features
;
703 return requested_features
;
706 static void virtio_scsi_reset(VirtIODevice
*vdev
)
708 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
709 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
711 assert(!s
->dataplane_started
);
713 qbus_reset_all(BUS(&s
->bus
));
716 vs
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
717 vs
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
718 s
->events_dropped
= false;
721 void virtio_scsi_push_event(VirtIOSCSI
*s
, SCSIDevice
*dev
,
722 uint32_t event
, uint32_t reason
)
724 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
726 VirtIOSCSIEvent
*evt
;
727 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
729 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
733 req
= virtio_scsi_pop_req(s
, vs
->event_vq
);
735 s
->events_dropped
= true;
739 if (s
->events_dropped
) {
740 event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
741 s
->events_dropped
= false;
744 if (virtio_scsi_parse_req(req
, 0, sizeof(VirtIOSCSIEvent
))) {
745 virtio_scsi_bad_req(req
);
749 evt
= &req
->resp
.event
;
750 memset(evt
, 0, sizeof(VirtIOSCSIEvent
));
751 evt
->event
= virtio_tswap32(vdev
, event
);
752 evt
->reason
= virtio_tswap32(vdev
, reason
);
754 assert(event
== VIRTIO_SCSI_T_EVENTS_MISSED
);
757 evt
->lun
[1] = dev
->id
;
759 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
760 if (dev
->lun
>= 256) {
761 evt
->lun
[2] = (dev
->lun
>> 8) | 0x40;
763 evt
->lun
[3] = dev
->lun
& 0xFF;
765 virtio_scsi_complete_req(req
);
768 bool virtio_scsi_handle_event_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
770 if (s
->events_dropped
) {
771 virtio_scsi_push_event(s
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
777 static void virtio_scsi_handle_event(VirtIODevice
*vdev
, VirtQueue
*vq
)
779 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
782 virtio_device_start_ioeventfd(vdev
);
783 if (!s
->dataplane_fenced
) {
787 virtio_scsi_acquire(s
);
788 virtio_scsi_handle_event_vq(s
, vq
);
789 virtio_scsi_release(s
);
792 static void virtio_scsi_change(SCSIBus
*bus
, SCSIDevice
*dev
, SCSISense sense
)
794 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
795 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
797 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_CHANGE
) &&
798 dev
->type
!= TYPE_ROM
) {
799 virtio_scsi_acquire(s
);
800 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_PARAM_CHANGE
,
801 sense
.asc
| (sense
.ascq
<< 8));
802 virtio_scsi_release(s
);
806 static void virtio_scsi_pre_hotplug(HotplugHandler
*hotplug_dev
,
807 DeviceState
*dev
, Error
**errp
)
809 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
810 sd
->hba_supports_iothread
= true;
813 static void virtio_scsi_hotplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
816 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
817 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
818 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
821 if (s
->ctx
&& !s
->dataplane_fenced
) {
822 if (blk_op_is_blocked(sd
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
825 virtio_scsi_acquire(s
);
826 ret
= blk_set_aio_context(sd
->conf
.blk
, s
->ctx
, errp
);
827 virtio_scsi_release(s
);
833 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
834 virtio_scsi_acquire(s
);
835 virtio_scsi_push_event(s
, sd
,
836 VIRTIO_SCSI_T_TRANSPORT_RESET
,
837 VIRTIO_SCSI_EVT_RESET_RESCAN
);
838 virtio_scsi_release(s
);
842 static void virtio_scsi_hotunplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
845 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
846 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
847 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
848 AioContext
*ctx
= s
->ctx ?
: qemu_get_aio_context();
850 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
851 virtio_scsi_acquire(s
);
852 virtio_scsi_push_event(s
, sd
,
853 VIRTIO_SCSI_T_TRANSPORT_RESET
,
854 VIRTIO_SCSI_EVT_RESET_REMOVED
);
855 virtio_scsi_release(s
);
858 aio_disable_external(ctx
);
859 qdev_simple_device_unplug_cb(hotplug_dev
, dev
, errp
);
860 aio_enable_external(ctx
);
863 virtio_scsi_acquire(s
);
864 /* If other users keep the BlockBackend in the iothread, that's ok */
865 blk_set_aio_context(sd
->conf
.blk
, qemu_get_aio_context(), NULL
);
866 virtio_scsi_release(s
);
870 static struct SCSIBusInfo virtio_scsi_scsi_info
= {
872 .max_channel
= VIRTIO_SCSI_MAX_CHANNEL
,
873 .max_target
= VIRTIO_SCSI_MAX_TARGET
,
874 .max_lun
= VIRTIO_SCSI_MAX_LUN
,
876 .complete
= virtio_scsi_command_complete
,
877 .cancel
= virtio_scsi_request_cancelled
,
878 .change
= virtio_scsi_change
,
879 .parse_cdb
= virtio_scsi_parse_cdb
,
880 .get_sg_list
= virtio_scsi_get_sg_list
,
881 .save_request
= virtio_scsi_save_request
,
882 .load_request
= virtio_scsi_load_request
,
885 void virtio_scsi_common_realize(DeviceState
*dev
,
886 VirtIOHandleOutput ctrl
,
887 VirtIOHandleOutput evt
,
888 VirtIOHandleOutput cmd
,
891 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
892 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(dev
);
895 virtio_init(vdev
, "virtio-scsi", VIRTIO_ID_SCSI
,
896 sizeof(VirtIOSCSIConfig
));
898 if (s
->conf
.num_queues
== VIRTIO_SCSI_AUTO_NUM_QUEUES
) {
899 s
->conf
.num_queues
= 1;
901 if (s
->conf
.num_queues
== 0 ||
902 s
->conf
.num_queues
> VIRTIO_QUEUE_MAX
- VIRTIO_SCSI_VQ_NUM_FIXED
) {
903 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
904 "must be a positive integer less than %d.",
906 VIRTIO_QUEUE_MAX
- VIRTIO_SCSI_VQ_NUM_FIXED
);
907 virtio_cleanup(vdev
);
910 if (s
->conf
.virtqueue_size
<= 2) {
911 error_setg(errp
, "invalid virtqueue_size property (= %" PRIu32
"), "
912 "must be > 2", s
->conf
.virtqueue_size
);
915 s
->cmd_vqs
= g_new0(VirtQueue
*, s
->conf
.num_queues
);
916 s
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
917 s
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
919 s
->ctrl_vq
= virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, ctrl
);
920 s
->event_vq
= virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, evt
);
921 for (i
= 0; i
< s
->conf
.num_queues
; i
++) {
922 s
->cmd_vqs
[i
] = virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, cmd
);
926 static void virtio_scsi_device_realize(DeviceState
*dev
, Error
**errp
)
928 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
929 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
932 virtio_scsi_common_realize(dev
,
933 virtio_scsi_handle_ctrl
,
934 virtio_scsi_handle_event
,
935 virtio_scsi_handle_cmd
,
938 error_propagate(errp
, err
);
942 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), dev
,
943 &virtio_scsi_scsi_info
, vdev
->bus_name
);
944 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
945 qbus_set_hotplug_handler(BUS(&s
->bus
), OBJECT(dev
));
947 virtio_scsi_dataplane_setup(s
, errp
);
950 void virtio_scsi_common_unrealize(DeviceState
*dev
)
952 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
953 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
956 virtio_delete_queue(vs
->ctrl_vq
);
957 virtio_delete_queue(vs
->event_vq
);
958 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
959 virtio_delete_queue(vs
->cmd_vqs
[i
]);
962 virtio_cleanup(vdev
);
965 static void virtio_scsi_device_unrealize(DeviceState
*dev
)
967 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
969 qbus_set_hotplug_handler(BUS(&s
->bus
), NULL
);
970 virtio_scsi_common_unrealize(dev
);
973 static Property virtio_scsi_properties
[] = {
974 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI
, parent_obj
.conf
.num_queues
,
975 VIRTIO_SCSI_AUTO_NUM_QUEUES
),
976 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSI
,
977 parent_obj
.conf
.virtqueue_size
, 256),
978 DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSI
,
979 parent_obj
.conf
.seg_max_adjust
, true),
980 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI
, parent_obj
.conf
.max_sectors
,
982 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI
, parent_obj
.conf
.cmd_per_lun
,
984 DEFINE_PROP_BIT("hotplug", VirtIOSCSI
, host_features
,
985 VIRTIO_SCSI_F_HOTPLUG
, true),
986 DEFINE_PROP_BIT("param_change", VirtIOSCSI
, host_features
,
987 VIRTIO_SCSI_F_CHANGE
, true),
988 DEFINE_PROP_LINK("iothread", VirtIOSCSI
, parent_obj
.conf
.iothread
,
989 TYPE_IOTHREAD
, IOThread
*),
990 DEFINE_PROP_END_OF_LIST(),
993 static const VMStateDescription vmstate_virtio_scsi
= {
994 .name
= "virtio-scsi",
995 .minimum_version_id
= 1,
997 .fields
= (VMStateField
[]) {
998 VMSTATE_VIRTIO_DEVICE
,
999 VMSTATE_END_OF_LIST()
1003 static void virtio_scsi_common_class_init(ObjectClass
*klass
, void *data
)
1005 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1006 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1008 vdc
->get_config
= virtio_scsi_get_config
;
1009 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1012 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
1014 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1015 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1016 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1018 device_class_set_props(dc
, virtio_scsi_properties
);
1019 dc
->vmsd
= &vmstate_virtio_scsi
;
1020 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1021 vdc
->realize
= virtio_scsi_device_realize
;
1022 vdc
->unrealize
= virtio_scsi_device_unrealize
;
1023 vdc
->set_config
= virtio_scsi_set_config
;
1024 vdc
->get_features
= virtio_scsi_get_features
;
1025 vdc
->reset
= virtio_scsi_reset
;
1026 vdc
->start_ioeventfd
= virtio_scsi_dataplane_start
;
1027 vdc
->stop_ioeventfd
= virtio_scsi_dataplane_stop
;
1028 hc
->pre_plug
= virtio_scsi_pre_hotplug
;
1029 hc
->plug
= virtio_scsi_hotplug
;
1030 hc
->unplug
= virtio_scsi_hotunplug
;
1033 static const TypeInfo virtio_scsi_common_info
= {
1034 .name
= TYPE_VIRTIO_SCSI_COMMON
,
1035 .parent
= TYPE_VIRTIO_DEVICE
,
1036 .instance_size
= sizeof(VirtIOSCSICommon
),
1038 .class_init
= virtio_scsi_common_class_init
,
1041 static const TypeInfo virtio_scsi_info
= {
1042 .name
= TYPE_VIRTIO_SCSI
,
1043 .parent
= TYPE_VIRTIO_SCSI_COMMON
,
1044 .instance_size
= sizeof(VirtIOSCSI
),
1045 .class_init
= virtio_scsi_class_init
,
1046 .interfaces
= (InterfaceInfo
[]) {
1047 { TYPE_HOTPLUG_HANDLER
},
1052 static void virtio_register_types(void)
1054 type_register_static(&virtio_scsi_common_info
);
1055 type_register_static(&virtio_scsi_info
);
1058 type_init(virtio_register_types
)