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/defer-call.h"
22 #include "qemu/error-report.h"
24 #include "qemu/module.h"
25 #include "sysemu/block-backend.h"
26 #include "sysemu/dma.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/scsi/scsi.h"
29 #include "scsi/constants.h"
30 #include "hw/virtio/virtio-bus.h"
31 #include "hw/virtio/virtio-access.h"
34 typedef struct VirtIOSCSIReq
{
37 * - fields up to resp_iov are initialized by virtio_scsi_init_req;
38 * - fields starting at vring are zeroed by virtio_scsi_init_req.
40 VirtQueueElement elem
;
45 QEMUIOVector resp_iov
;
47 /* Used for two-stage request submission and TMFs deferred to BH */
48 QTAILQ_ENTRY(VirtIOSCSIReq
) next
;
50 /* Used for cancellation of request during TMFs */
55 enum SCSIXferMode mode
;
57 VirtIOSCSICmdResp cmd
;
58 VirtIOSCSICtrlTMFResp tmf
;
59 VirtIOSCSICtrlANResp an
;
60 VirtIOSCSIEvent event
;
64 VirtIOSCSICtrlTMFReq tmf
;
65 VirtIOSCSICtrlANReq an
;
69 static inline int virtio_scsi_get_lun(uint8_t *lun
)
71 return ((lun
[2] << 8) | lun
[3]) & 0x3FFF;
74 static inline SCSIDevice
*virtio_scsi_device_get(VirtIOSCSI
*s
, uint8_t *lun
)
79 if (lun
[2] != 0 && !(lun
[2] >= 0x40 && lun
[2] < 0x80)) {
82 return scsi_device_get(&s
->bus
, 0, lun
[1], virtio_scsi_get_lun(lun
));
85 static void virtio_scsi_init_req(VirtIOSCSI
*s
, VirtQueue
*vq
, VirtIOSCSIReq
*req
)
87 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
88 const size_t zero_skip
=
89 offsetof(VirtIOSCSIReq
, resp_iov
) + sizeof(req
->resp_iov
);
93 qemu_sglist_init(&req
->qsgl
, DEVICE(s
), 8, vdev
->dma_as
);
94 qemu_iovec_init(&req
->resp_iov
, 1);
95 memset((uint8_t *)req
+ zero_skip
, 0, sizeof(*req
) - zero_skip
);
98 static void virtio_scsi_free_req(VirtIOSCSIReq
*req
)
100 qemu_iovec_destroy(&req
->resp_iov
);
101 qemu_sglist_destroy(&req
->qsgl
);
105 static void virtio_scsi_complete_req(VirtIOSCSIReq
*req
)
107 VirtIOSCSI
*s
= req
->dev
;
108 VirtQueue
*vq
= req
->vq
;
109 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
111 qemu_iovec_from_buf(&req
->resp_iov
, 0, &req
->resp
, req
->resp_size
);
112 virtqueue_push(vq
, &req
->elem
, req
->qsgl
.size
+ req
->resp_iov
.size
);
113 if (s
->dataplane_started
&& !s
->dataplane_fenced
) {
114 virtio_notify_irqfd(vdev
, vq
);
116 virtio_notify(vdev
, vq
);
120 req
->sreq
->hba_private
= NULL
;
121 scsi_req_unref(req
->sreq
);
123 virtio_scsi_free_req(req
);
126 static void virtio_scsi_bad_req(VirtIOSCSIReq
*req
)
128 virtio_error(VIRTIO_DEVICE(req
->dev
), "wrong size for virtio-scsi headers");
129 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
130 virtio_scsi_free_req(req
);
133 static size_t qemu_sgl_concat(VirtIOSCSIReq
*req
, struct iovec
*iov
,
134 hwaddr
*addr
, int num
, size_t skip
)
136 QEMUSGList
*qsgl
= &req
->qsgl
;
140 if (skip
>= iov
->iov_len
) {
141 skip
-= iov
->iov_len
;
143 qemu_sglist_add(qsgl
, *addr
+ skip
, iov
->iov_len
- skip
);
144 copied
+= iov
->iov_len
- skip
;
156 static int virtio_scsi_parse_req(VirtIOSCSIReq
*req
,
157 unsigned req_size
, unsigned resp_size
)
159 VirtIODevice
*vdev
= (VirtIODevice
*) req
->dev
;
160 size_t in_size
, out_size
;
162 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
163 &req
->req
, req_size
) < req_size
) {
167 if (qemu_iovec_concat_iov(&req
->resp_iov
,
168 req
->elem
.in_sg
, req
->elem
.in_num
, 0,
169 resp_size
) < resp_size
) {
173 req
->resp_size
= resp_size
;
175 /* Old BIOSes left some padding by mistake after the req_size/resp_size.
176 * As a workaround, always consider the first buffer as the virtio-scsi
177 * request/response, making the payload start at the second element
180 * The actual length of the response header, stored in req->resp_size,
183 * TODO: always disable this workaround for virtio 1.0 devices.
185 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_ANY_LAYOUT
)) {
186 if (req
->elem
.out_num
) {
187 req_size
= req
->elem
.out_sg
[0].iov_len
;
189 if (req
->elem
.in_num
) {
190 resp_size
= req
->elem
.in_sg
[0].iov_len
;
194 out_size
= qemu_sgl_concat(req
, req
->elem
.out_sg
,
195 &req
->elem
.out_addr
[0], req
->elem
.out_num
,
197 in_size
= qemu_sgl_concat(req
, req
->elem
.in_sg
,
198 &req
->elem
.in_addr
[0], req
->elem
.in_num
,
201 if (out_size
&& in_size
) {
206 req
->mode
= SCSI_XFER_TO_DEV
;
207 } else if (in_size
) {
208 req
->mode
= SCSI_XFER_FROM_DEV
;
214 static VirtIOSCSIReq
*virtio_scsi_pop_req(VirtIOSCSI
*s
, VirtQueue
*vq
)
216 VirtIOSCSICommon
*vs
= (VirtIOSCSICommon
*)s
;
219 req
= virtqueue_pop(vq
, sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
223 virtio_scsi_init_req(s
, vq
, req
);
227 static void virtio_scsi_save_request(QEMUFile
*f
, SCSIRequest
*sreq
)
229 VirtIOSCSIReq
*req
= sreq
->hba_private
;
230 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(req
->dev
);
231 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
232 uint32_t n
= virtio_get_queue_index(req
->vq
) - VIRTIO_SCSI_VQ_NUM_FIXED
;
234 assert(n
< vs
->conf
.num_queues
);
235 qemu_put_be32s(f
, &n
);
236 qemu_put_virtqueue_element(vdev
, f
, &req
->elem
);
239 static void *virtio_scsi_load_request(QEMUFile
*f
, SCSIRequest
*sreq
)
241 SCSIBus
*bus
= sreq
->bus
;
242 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
243 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
244 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
248 qemu_get_be32s(f
, &n
);
249 assert(n
< vs
->conf
.num_queues
);
250 req
= qemu_get_virtqueue_element(vdev
, f
,
251 sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
252 virtio_scsi_init_req(s
, vs
->cmd_vqs
[n
], req
);
254 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
255 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
) < 0) {
256 error_report("invalid SCSI request migration data");
262 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
) {
263 assert(req
->sreq
->cmd
.mode
== req
->mode
);
270 VirtIOSCSIReq
*tmf_req
;
271 } VirtIOSCSICancelNotifier
;
273 static void virtio_scsi_cancel_notify(Notifier
*notifier
, void *data
)
275 VirtIOSCSICancelNotifier
*n
= container_of(notifier
,
276 VirtIOSCSICancelNotifier
,
279 if (--n
->tmf_req
->remaining
== 0) {
280 VirtIOSCSIReq
*req
= n
->tmf_req
;
282 trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req
->req
.tmf
.lun
),
283 req
->req
.tmf
.tag
, req
->resp
.tmf
.response
);
284 virtio_scsi_complete_req(req
);
289 static inline void virtio_scsi_ctx_check(VirtIOSCSI
*s
, SCSIDevice
*d
)
291 if (s
->dataplane_started
&& d
&& blk_is_available(d
->conf
.blk
)) {
292 assert(blk_get_aio_context(d
->conf
.blk
) == s
->ctx
);
296 static void virtio_scsi_do_one_tmf_bh(VirtIOSCSIReq
*req
)
298 VirtIOSCSI
*s
= req
->dev
;
299 SCSIDevice
*d
= virtio_scsi_device_get(s
, req
->req
.tmf
.lun
);
303 switch (req
->req
.tmf
.subtype
) {
304 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
306 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
309 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
310 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
313 qatomic_inc(&s
->resetting
);
314 device_cold_reset(&d
->qdev
);
315 qatomic_dec(&s
->resetting
);
318 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
319 target
= req
->req
.tmf
.lun
[1];
320 qatomic_inc(&s
->resetting
);
323 QTAILQ_FOREACH_RCU(kid
, &s
->bus
.qbus
.children
, sibling
) {
324 SCSIDevice
*d1
= SCSI_DEVICE(kid
->child
);
325 if (d1
->channel
== 0 && d1
->id
== target
) {
326 device_cold_reset(&d1
->qdev
);
331 qatomic_dec(&s
->resetting
);
335 g_assert_not_reached();
340 object_unref(OBJECT(d
));
342 virtio_scsi_acquire(s
);
343 virtio_scsi_complete_req(req
);
344 virtio_scsi_release(s
);
347 /* Some TMFs must be processed from the main loop thread */
348 static void virtio_scsi_do_tmf_bh(void *opaque
)
350 VirtIOSCSI
*s
= opaque
;
351 QTAILQ_HEAD(, VirtIOSCSIReq
) reqs
= QTAILQ_HEAD_INITIALIZER(reqs
);
357 virtio_scsi_acquire(s
);
359 QTAILQ_FOREACH_SAFE(req
, &s
->tmf_bh_list
, next
, tmp
) {
360 QTAILQ_REMOVE(&s
->tmf_bh_list
, req
, next
);
361 QTAILQ_INSERT_TAIL(&reqs
, req
, next
);
364 qemu_bh_delete(s
->tmf_bh
);
367 virtio_scsi_release(s
);
369 QTAILQ_FOREACH_SAFE(req
, &reqs
, next
, tmp
) {
370 QTAILQ_REMOVE(&reqs
, req
, next
);
371 virtio_scsi_do_one_tmf_bh(req
);
375 static void virtio_scsi_reset_tmf_bh(VirtIOSCSI
*s
)
382 virtio_scsi_acquire(s
);
385 qemu_bh_delete(s
->tmf_bh
);
389 QTAILQ_FOREACH_SAFE(req
, &s
->tmf_bh_list
, next
, tmp
) {
390 QTAILQ_REMOVE(&s
->tmf_bh_list
, req
, next
);
392 /* SAM-6 6.3.2 Hard reset */
393 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_TARGET_FAILURE
;
394 virtio_scsi_complete_req(req
);
397 virtio_scsi_release(s
);
400 static void virtio_scsi_defer_tmf_to_bh(VirtIOSCSIReq
*req
)
402 VirtIOSCSI
*s
= req
->dev
;
404 QTAILQ_INSERT_TAIL(&s
->tmf_bh_list
, req
, next
);
407 s
->tmf_bh
= qemu_bh_new(virtio_scsi_do_tmf_bh
, s
);
408 qemu_bh_schedule(s
->tmf_bh
);
412 /* Return 0 if the request is ready to be completed and return to guest;
413 * -EINPROGRESS if the request is submitted and will be completed later, in the
414 * case of async cancellation. */
415 static int virtio_scsi_do_tmf(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
417 SCSIDevice
*d
= virtio_scsi_device_get(s
, req
->req
.tmf
.lun
);
418 SCSIRequest
*r
, *next
;
421 virtio_scsi_ctx_check(s
, d
);
422 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
423 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_OK
;
426 * req->req.tmf has the QEMU_PACKED attribute. Don't use virtio_tswap32s()
427 * to avoid compiler errors.
429 req
->req
.tmf
.subtype
=
430 virtio_tswap32(VIRTIO_DEVICE(s
), req
->req
.tmf
.subtype
);
432 trace_virtio_scsi_tmf_req(virtio_scsi_get_lun(req
->req
.tmf
.lun
),
433 req
->req
.tmf
.tag
, req
->req
.tmf
.subtype
);
435 switch (req
->req
.tmf
.subtype
) {
436 case VIRTIO_SCSI_T_TMF_ABORT_TASK
:
437 case VIRTIO_SCSI_T_TMF_QUERY_TASK
:
441 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
444 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
445 VirtIOSCSIReq
*cmd_req
= r
->hba_private
;
446 if (cmd_req
&& cmd_req
->req
.cmd
.tag
== req
->req
.tmf
.tag
) {
452 * Assert that the request has not been completed yet, we
453 * check for it in the loop above.
455 assert(r
->hba_private
);
456 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK
) {
457 /* "If the specified command is present in the task set, then
458 * return a service response set to FUNCTION SUCCEEDED".
460 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
462 VirtIOSCSICancelNotifier
*notifier
;
465 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
466 notifier
->tmf_req
= req
;
467 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
468 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
474 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
475 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
476 virtio_scsi_defer_tmf_to_bh(req
);
480 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET
:
481 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET
:
482 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
:
486 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
490 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
491 * This way, if the bus starts calling back to the notifiers
492 * even before we finish the loop, virtio_scsi_cancel_notify
493 * will not complete the TMF too early.
496 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
497 if (r
->hba_private
) {
498 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
) {
499 /* "If there is any command present in the task set, then
500 * return a service response set to FUNCTION SUCCEEDED".
502 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
505 VirtIOSCSICancelNotifier
*notifier
;
508 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
509 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
510 notifier
->tmf_req
= req
;
511 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
515 if (--req
->remaining
> 0) {
520 case VIRTIO_SCSI_T_TMF_CLEAR_ACA
:
522 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_REJECTED
;
526 object_unref(OBJECT(d
));
530 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
531 object_unref(OBJECT(d
));
535 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
536 object_unref(OBJECT(d
));
540 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
542 VirtIODevice
*vdev
= (VirtIODevice
*)s
;
546 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
547 &type
, sizeof(type
)) < sizeof(type
)) {
548 virtio_scsi_bad_req(req
);
552 virtio_tswap32s(vdev
, &type
);
553 if (type
== VIRTIO_SCSI_T_TMF
) {
554 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlTMFReq
),
555 sizeof(VirtIOSCSICtrlTMFResp
)) < 0) {
556 virtio_scsi_bad_req(req
);
559 r
= virtio_scsi_do_tmf(s
, req
);
562 } else if (type
== VIRTIO_SCSI_T_AN_QUERY
||
563 type
== VIRTIO_SCSI_T_AN_SUBSCRIBE
) {
564 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlANReq
),
565 sizeof(VirtIOSCSICtrlANResp
)) < 0) {
566 virtio_scsi_bad_req(req
);
569 req
->req
.an
.event_requested
=
570 virtio_tswap32(VIRTIO_DEVICE(s
), req
->req
.an
.event_requested
);
571 trace_virtio_scsi_an_req(virtio_scsi_get_lun(req
->req
.an
.lun
),
572 req
->req
.an
.event_requested
);
573 req
->resp
.an
.event_actual
= 0;
574 req
->resp
.an
.response
= VIRTIO_SCSI_S_OK
;
578 if (type
== VIRTIO_SCSI_T_TMF
)
579 trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req
->req
.tmf
.lun
),
581 req
->resp
.tmf
.response
);
582 else if (type
== VIRTIO_SCSI_T_AN_QUERY
||
583 type
== VIRTIO_SCSI_T_AN_SUBSCRIBE
)
584 trace_virtio_scsi_an_resp(virtio_scsi_get_lun(req
->req
.an
.lun
),
585 req
->resp
.an
.response
);
586 virtio_scsi_complete_req(req
);
588 assert(r
== -EINPROGRESS
);
592 static void virtio_scsi_handle_ctrl_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
596 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
597 virtio_scsi_handle_ctrl_req(s
, req
);
602 * If dataplane is configured but not yet started, do so now and return true on
605 * Dataplane is started by the core virtio code but virtqueue handler functions
606 * can also be invoked when a guest kicks before DRIVER_OK, so this helper
607 * function helps us deal with manually starting ioeventfd in that case.
609 static bool virtio_scsi_defer_to_dataplane(VirtIOSCSI
*s
)
611 if (!s
->ctx
|| s
->dataplane_started
) {
615 virtio_device_start_ioeventfd(&s
->parent_obj
.parent_obj
);
616 return !s
->dataplane_fenced
;
619 static void virtio_scsi_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
621 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
623 if (virtio_scsi_defer_to_dataplane(s
)) {
627 virtio_scsi_acquire(s
);
628 virtio_scsi_handle_ctrl_vq(s
, vq
);
629 virtio_scsi_release(s
);
632 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq
*req
)
634 trace_virtio_scsi_cmd_resp(virtio_scsi_get_lun(req
->req
.cmd
.lun
),
636 req
->resp
.cmd
.response
,
637 req
->resp
.cmd
.status
);
638 /* Sense data is not in req->resp and is copied separately
639 * in virtio_scsi_command_complete.
641 req
->resp_size
= sizeof(VirtIOSCSICmdResp
);
642 virtio_scsi_complete_req(req
);
645 static void virtio_scsi_command_failed(SCSIRequest
*r
)
647 VirtIOSCSIReq
*req
= r
->hba_private
;
649 if (r
->io_canceled
) {
653 req
->resp
.cmd
.status
= GOOD
;
654 switch (r
->host_status
) {
655 case SCSI_HOST_NO_LUN
:
656 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
659 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BUSY
;
661 case SCSI_HOST_TIME_OUT
:
662 case SCSI_HOST_ABORTED
:
663 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_ABORTED
;
665 case SCSI_HOST_BAD_RESPONSE
:
666 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
668 case SCSI_HOST_RESET
:
669 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_RESET
;
671 case SCSI_HOST_TRANSPORT_DISRUPTED
:
672 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_TRANSPORT_FAILURE
;
674 case SCSI_HOST_TARGET_FAILURE
:
675 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_TARGET_FAILURE
;
677 case SCSI_HOST_RESERVATION_ERROR
:
678 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_NEXUS_FAILURE
;
680 case SCSI_HOST_ALLOCATION_FAILURE
:
681 case SCSI_HOST_MEDIUM_ERROR
:
682 case SCSI_HOST_ERROR
:
684 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_FAILURE
;
687 virtio_scsi_complete_cmd_req(req
);
690 static void virtio_scsi_command_complete(SCSIRequest
*r
, size_t resid
)
692 VirtIOSCSIReq
*req
= r
->hba_private
;
693 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
695 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
697 if (r
->io_canceled
) {
701 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OK
;
702 req
->resp
.cmd
.status
= r
->status
;
703 if (req
->resp
.cmd
.status
== GOOD
) {
704 req
->resp
.cmd
.resid
= virtio_tswap32(vdev
, resid
);
706 req
->resp
.cmd
.resid
= 0;
707 sense_len
= scsi_req_get_sense(r
, sense
, sizeof(sense
));
708 sense_len
= MIN(sense_len
, req
->resp_iov
.size
- sizeof(req
->resp
.cmd
));
709 qemu_iovec_from_buf(&req
->resp_iov
, sizeof(req
->resp
.cmd
),
711 req
->resp
.cmd
.sense_len
= virtio_tswap32(vdev
, sense_len
);
713 virtio_scsi_complete_cmd_req(req
);
716 static int virtio_scsi_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
,
717 uint8_t *buf
, size_t buf_len
,
720 VirtIOSCSIReq
*req
= hba_private
;
723 cmd
->len
= MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE
, SCSI_CMD_BUF_SIZE
);
724 memcpy(cmd
->buf
, buf
, cmd
->len
);
727 /* Extract the direction and mode directly from the request, for
728 * host device passthrough.
730 cmd
->xfer
= req
->qsgl
.size
;
731 cmd
->mode
= req
->mode
;
735 static QEMUSGList
*virtio_scsi_get_sg_list(SCSIRequest
*r
)
737 VirtIOSCSIReq
*req
= r
->hba_private
;
742 static void virtio_scsi_request_cancelled(SCSIRequest
*r
)
744 VirtIOSCSIReq
*req
= r
->hba_private
;
749 if (qatomic_read(&req
->dev
->resetting
)) {
750 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_RESET
;
752 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_ABORTED
;
754 virtio_scsi_complete_cmd_req(req
);
757 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq
*req
)
759 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_FAILURE
;
760 virtio_scsi_complete_cmd_req(req
);
763 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
765 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
769 rc
= virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
770 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
);
772 if (rc
== -ENOTSUP
) {
773 virtio_scsi_fail_cmd_req(req
);
776 virtio_scsi_bad_req(req
);
780 trace_virtio_scsi_cmd_req(virtio_scsi_get_lun(req
->req
.cmd
.lun
),
781 req
->req
.cmd
.tag
, req
->req
.cmd
.cdb
[0]);
783 d
= virtio_scsi_device_get(s
, req
->req
.cmd
.lun
);
785 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
786 virtio_scsi_complete_cmd_req(req
);
789 virtio_scsi_ctx_check(s
, d
);
790 req
->sreq
= scsi_req_new(d
, req
->req
.cmd
.tag
,
791 virtio_scsi_get_lun(req
->req
.cmd
.lun
),
792 req
->req
.cmd
.cdb
, vs
->cdb_size
, req
);
794 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
795 && (req
->sreq
->cmd
.mode
!= req
->mode
||
796 req
->sreq
->cmd
.xfer
> req
->qsgl
.size
)) {
797 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OVERRUN
;
798 virtio_scsi_complete_cmd_req(req
);
799 object_unref(OBJECT(d
));
802 scsi_req_ref(req
->sreq
);
804 object_unref(OBJECT(d
));
808 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
810 SCSIRequest
*sreq
= req
->sreq
;
811 if (scsi_req_enqueue(sreq
)) {
812 scsi_req_continue(sreq
);
815 scsi_req_unref(sreq
);
818 static void virtio_scsi_handle_cmd_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
820 VirtIOSCSIReq
*req
, *next
;
822 bool suppress_notifications
= virtio_queue_get_notification(vq
);
824 QTAILQ_HEAD(, VirtIOSCSIReq
) reqs
= QTAILQ_HEAD_INITIALIZER(reqs
);
827 if (suppress_notifications
) {
828 virtio_queue_set_notification(vq
, 0);
831 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
832 ret
= virtio_scsi_handle_cmd_req_prepare(s
, req
);
834 QTAILQ_INSERT_TAIL(&reqs
, req
, next
);
835 } else if (ret
== -EINVAL
) {
836 /* The device is broken and shouldn't process any request */
837 while (!QTAILQ_EMPTY(&reqs
)) {
838 req
= QTAILQ_FIRST(&reqs
);
839 QTAILQ_REMOVE(&reqs
, req
, next
);
841 scsi_req_unref(req
->sreq
);
842 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
843 virtio_scsi_free_req(req
);
848 if (suppress_notifications
) {
849 virtio_queue_set_notification(vq
, 1);
851 } while (ret
!= -EINVAL
&& !virtio_queue_empty(vq
));
853 QTAILQ_FOREACH_SAFE(req
, &reqs
, next
, next
) {
854 virtio_scsi_handle_cmd_req_submit(s
, req
);
858 static void virtio_scsi_handle_cmd(VirtIODevice
*vdev
, VirtQueue
*vq
)
860 /* use non-QOM casts in the data path */
861 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
863 if (virtio_scsi_defer_to_dataplane(s
)) {
867 virtio_scsi_acquire(s
);
868 virtio_scsi_handle_cmd_vq(s
, vq
);
869 virtio_scsi_release(s
);
872 static void virtio_scsi_get_config(VirtIODevice
*vdev
,
875 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
876 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(vdev
);
878 virtio_stl_p(vdev
, &scsiconf
->num_queues
, s
->conf
.num_queues
);
879 virtio_stl_p(vdev
, &scsiconf
->seg_max
,
880 s
->conf
.seg_max_adjust
? s
->conf
.virtqueue_size
- 2 : 128 - 2);
881 virtio_stl_p(vdev
, &scsiconf
->max_sectors
, s
->conf
.max_sectors
);
882 virtio_stl_p(vdev
, &scsiconf
->cmd_per_lun
, s
->conf
.cmd_per_lun
);
883 virtio_stl_p(vdev
, &scsiconf
->event_info_size
, sizeof(VirtIOSCSIEvent
));
884 virtio_stl_p(vdev
, &scsiconf
->sense_size
, s
->sense_size
);
885 virtio_stl_p(vdev
, &scsiconf
->cdb_size
, s
->cdb_size
);
886 virtio_stw_p(vdev
, &scsiconf
->max_channel
, VIRTIO_SCSI_MAX_CHANNEL
);
887 virtio_stw_p(vdev
, &scsiconf
->max_target
, VIRTIO_SCSI_MAX_TARGET
);
888 virtio_stl_p(vdev
, &scsiconf
->max_lun
, VIRTIO_SCSI_MAX_LUN
);
891 static void virtio_scsi_set_config(VirtIODevice
*vdev
,
892 const uint8_t *config
)
894 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
895 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
897 if ((uint32_t) virtio_ldl_p(vdev
, &scsiconf
->sense_size
) >= 65536 ||
898 (uint32_t) virtio_ldl_p(vdev
, &scsiconf
->cdb_size
) >= 256) {
900 "bad data written to virtio-scsi configuration space");
904 vs
->sense_size
= virtio_ldl_p(vdev
, &scsiconf
->sense_size
);
905 vs
->cdb_size
= virtio_ldl_p(vdev
, &scsiconf
->cdb_size
);
908 static uint64_t virtio_scsi_get_features(VirtIODevice
*vdev
,
909 uint64_t requested_features
,
912 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
914 /* Firstly sync all virtio-scsi possible supported features */
915 requested_features
|= s
->host_features
;
916 return requested_features
;
919 static void virtio_scsi_reset(VirtIODevice
*vdev
)
921 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
922 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
924 assert(!s
->dataplane_started
);
926 virtio_scsi_reset_tmf_bh(s
);
928 qatomic_inc(&s
->resetting
);
929 bus_cold_reset(BUS(&s
->bus
));
930 qatomic_dec(&s
->resetting
);
932 vs
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
933 vs
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
934 s
->events_dropped
= false;
941 /* Used by messages specific to a device */
947 } VirtIOSCSIEventInfo
;
949 static void virtio_scsi_push_event(VirtIOSCSI
*s
,
950 const VirtIOSCSIEventInfo
*info
)
952 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
954 VirtIOSCSIEvent
*evt
;
955 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
956 uint32_t event
= info
->event
;
957 uint32_t reason
= info
->reason
;
959 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
963 req
= virtio_scsi_pop_req(s
, vs
->event_vq
);
965 s
->events_dropped
= true;
969 if (s
->events_dropped
) {
970 event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
971 s
->events_dropped
= false;
974 if (virtio_scsi_parse_req(req
, 0, sizeof(VirtIOSCSIEvent
))) {
975 virtio_scsi_bad_req(req
);
979 evt
= &req
->resp
.event
;
980 memset(evt
, 0, sizeof(VirtIOSCSIEvent
));
981 evt
->event
= virtio_tswap32(vdev
, event
);
982 evt
->reason
= virtio_tswap32(vdev
, reason
);
983 if (event
!= VIRTIO_SCSI_T_EVENTS_MISSED
) {
985 evt
->lun
[1] = info
->address
.id
;
987 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
988 if (info
->address
.lun
>= 256) {
989 evt
->lun
[2] = (info
->address
.lun
>> 8) | 0x40;
991 evt
->lun
[3] = info
->address
.lun
& 0xFF;
993 trace_virtio_scsi_event(virtio_scsi_get_lun(evt
->lun
), event
, reason
);
995 virtio_scsi_complete_req(req
);
998 static void virtio_scsi_handle_event_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
1000 if (s
->events_dropped
) {
1001 VirtIOSCSIEventInfo info
= {
1002 .event
= VIRTIO_SCSI_T_NO_EVENT
,
1004 virtio_scsi_push_event(s
, &info
);
1008 static void virtio_scsi_handle_event(VirtIODevice
*vdev
, VirtQueue
*vq
)
1010 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
1012 if (virtio_scsi_defer_to_dataplane(s
)) {
1016 virtio_scsi_acquire(s
);
1017 virtio_scsi_handle_event_vq(s
, vq
);
1018 virtio_scsi_release(s
);
1021 static void virtio_scsi_change(SCSIBus
*bus
, SCSIDevice
*dev
, SCSISense sense
)
1023 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
1024 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1026 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_CHANGE
) &&
1027 dev
->type
!= TYPE_ROM
) {
1028 VirtIOSCSIEventInfo info
= {
1029 .event
= VIRTIO_SCSI_T_PARAM_CHANGE
,
1030 .reason
= sense
.asc
| (sense
.ascq
<< 8),
1037 virtio_scsi_acquire(s
);
1038 virtio_scsi_push_event(s
, &info
);
1039 virtio_scsi_release(s
);
1043 static void virtio_scsi_pre_hotplug(HotplugHandler
*hotplug_dev
,
1044 DeviceState
*dev
, Error
**errp
)
1046 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
1047 sd
->hba_supports_iothread
= true;
1050 static void virtio_scsi_hotplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
1053 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
1054 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
1055 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
1056 AioContext
*old_context
;
1059 if (s
->ctx
&& !s
->dataplane_fenced
) {
1060 if (blk_op_is_blocked(sd
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
1063 old_context
= blk_get_aio_context(sd
->conf
.blk
);
1064 aio_context_acquire(old_context
);
1065 ret
= blk_set_aio_context(sd
->conf
.blk
, s
->ctx
, errp
);
1066 aio_context_release(old_context
);
1072 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
1073 VirtIOSCSIEventInfo info
= {
1074 .event
= VIRTIO_SCSI_T_TRANSPORT_RESET
,
1075 .reason
= VIRTIO_SCSI_EVT_RESET_RESCAN
,
1082 virtio_scsi_acquire(s
);
1083 virtio_scsi_push_event(s
, &info
);
1084 scsi_bus_set_ua(&s
->bus
, SENSE_CODE(REPORTED_LUNS_CHANGED
));
1085 virtio_scsi_release(s
);
1089 static void virtio_scsi_hotunplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
1092 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
1093 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
1094 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
1095 VirtIOSCSIEventInfo info
= {
1096 .event
= VIRTIO_SCSI_T_TRANSPORT_RESET
,
1097 .reason
= VIRTIO_SCSI_EVT_RESET_REMOVED
,
1104 qdev_simple_device_unplug_cb(hotplug_dev
, dev
, errp
);
1107 virtio_scsi_acquire(s
);
1108 /* If other users keep the BlockBackend in the iothread, that's ok */
1109 blk_set_aio_context(sd
->conf
.blk
, qemu_get_aio_context(), NULL
);
1110 virtio_scsi_release(s
);
1113 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
1114 virtio_scsi_acquire(s
);
1115 virtio_scsi_push_event(s
, &info
);
1116 scsi_bus_set_ua(&s
->bus
, SENSE_CODE(REPORTED_LUNS_CHANGED
));
1117 virtio_scsi_release(s
);
1121 /* Suspend virtqueue ioeventfd processing during drain */
1122 static void virtio_scsi_drained_begin(SCSIBus
*bus
)
1124 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
1125 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1126 uint32_t total_queues
= VIRTIO_SCSI_VQ_NUM_FIXED
+
1127 s
->parent_obj
.conf
.num_queues
;
1130 * Drain is called when stopping dataplane but the host notifier has
1131 * already been detached. Detaching multiple times is a no-op if nothing
1132 * else is using the monitoring same file descriptor, but avoid it just in
1135 * Also, don't detach if dataplane has not even been started yet because
1136 * the host notifier isn't attached.
1138 if (s
->dataplane_stopping
|| !s
->dataplane_started
) {
1142 for (uint32_t i
= 0; i
< total_queues
; i
++) {
1143 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
1144 virtio_queue_aio_detach_host_notifier(vq
, s
->ctx
);
1148 /* Resume virtqueue ioeventfd processing after drain */
1149 static void virtio_scsi_drained_end(SCSIBus
*bus
)
1151 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
1152 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1153 uint32_t total_queues
= VIRTIO_SCSI_VQ_NUM_FIXED
+
1154 s
->parent_obj
.conf
.num_queues
;
1157 * Drain is called when stopping dataplane. Keep the host notifier detached
1158 * so it's not left dangling after dataplane is stopped.
1160 * Also, don't attach if dataplane has not even been started yet. We're not
1163 if (s
->dataplane_stopping
|| !s
->dataplane_started
) {
1167 for (uint32_t i
= 0; i
< total_queues
; i
++) {
1168 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
1169 virtio_queue_aio_attach_host_notifier(vq
, s
->ctx
);
1173 static struct SCSIBusInfo virtio_scsi_scsi_info
= {
1175 .max_channel
= VIRTIO_SCSI_MAX_CHANNEL
,
1176 .max_target
= VIRTIO_SCSI_MAX_TARGET
,
1177 .max_lun
= VIRTIO_SCSI_MAX_LUN
,
1179 .complete
= virtio_scsi_command_complete
,
1180 .fail
= virtio_scsi_command_failed
,
1181 .cancel
= virtio_scsi_request_cancelled
,
1182 .change
= virtio_scsi_change
,
1183 .parse_cdb
= virtio_scsi_parse_cdb
,
1184 .get_sg_list
= virtio_scsi_get_sg_list
,
1185 .save_request
= virtio_scsi_save_request
,
1186 .load_request
= virtio_scsi_load_request
,
1187 .drained_begin
= virtio_scsi_drained_begin
,
1188 .drained_end
= virtio_scsi_drained_end
,
1191 void virtio_scsi_common_realize(DeviceState
*dev
,
1192 VirtIOHandleOutput ctrl
,
1193 VirtIOHandleOutput evt
,
1194 VirtIOHandleOutput cmd
,
1197 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1198 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(dev
);
1201 virtio_init(vdev
, VIRTIO_ID_SCSI
, sizeof(VirtIOSCSIConfig
));
1203 if (s
->conf
.num_queues
== VIRTIO_SCSI_AUTO_NUM_QUEUES
) {
1204 s
->conf
.num_queues
= 1;
1206 if (s
->conf
.num_queues
== 0 ||
1207 s
->conf
.num_queues
> VIRTIO_QUEUE_MAX
- VIRTIO_SCSI_VQ_NUM_FIXED
) {
1208 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
1209 "must be a positive integer less than %d.",
1211 VIRTIO_QUEUE_MAX
- VIRTIO_SCSI_VQ_NUM_FIXED
);
1212 virtio_cleanup(vdev
);
1215 if (s
->conf
.virtqueue_size
<= 2) {
1216 error_setg(errp
, "invalid virtqueue_size property (= %" PRIu32
"), "
1217 "must be > 2", s
->conf
.virtqueue_size
);
1220 s
->cmd_vqs
= g_new0(VirtQueue
*, s
->conf
.num_queues
);
1221 s
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
1222 s
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
1224 s
->ctrl_vq
= virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, ctrl
);
1225 s
->event_vq
= virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, evt
);
1226 for (i
= 0; i
< s
->conf
.num_queues
; i
++) {
1227 s
->cmd_vqs
[i
] = virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, cmd
);
1231 static void virtio_scsi_device_realize(DeviceState
*dev
, Error
**errp
)
1233 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1234 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
1237 QTAILQ_INIT(&s
->tmf_bh_list
);
1239 virtio_scsi_common_realize(dev
,
1240 virtio_scsi_handle_ctrl
,
1241 virtio_scsi_handle_event
,
1242 virtio_scsi_handle_cmd
,
1245 error_propagate(errp
, err
);
1249 scsi_bus_init_named(&s
->bus
, sizeof(s
->bus
), dev
,
1250 &virtio_scsi_scsi_info
, vdev
->bus_name
);
1251 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
1252 qbus_set_hotplug_handler(BUS(&s
->bus
), OBJECT(dev
));
1254 virtio_scsi_dataplane_setup(s
, errp
);
1257 void virtio_scsi_common_unrealize(DeviceState
*dev
)
1259 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1260 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
1263 virtio_delete_queue(vs
->ctrl_vq
);
1264 virtio_delete_queue(vs
->event_vq
);
1265 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
1266 virtio_delete_queue(vs
->cmd_vqs
[i
]);
1268 g_free(vs
->cmd_vqs
);
1269 virtio_cleanup(vdev
);
1272 static void virtio_scsi_device_unrealize(DeviceState
*dev
)
1274 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
1276 virtio_scsi_reset_tmf_bh(s
);
1278 qbus_set_hotplug_handler(BUS(&s
->bus
), NULL
);
1279 virtio_scsi_common_unrealize(dev
);
1282 static Property virtio_scsi_properties
[] = {
1283 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI
, parent_obj
.conf
.num_queues
,
1284 VIRTIO_SCSI_AUTO_NUM_QUEUES
),
1285 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSI
,
1286 parent_obj
.conf
.virtqueue_size
, 256),
1287 DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSI
,
1288 parent_obj
.conf
.seg_max_adjust
, true),
1289 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI
, parent_obj
.conf
.max_sectors
,
1291 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI
, parent_obj
.conf
.cmd_per_lun
,
1293 DEFINE_PROP_BIT("hotplug", VirtIOSCSI
, host_features
,
1294 VIRTIO_SCSI_F_HOTPLUG
, true),
1295 DEFINE_PROP_BIT("param_change", VirtIOSCSI
, host_features
,
1296 VIRTIO_SCSI_F_CHANGE
, true),
1297 DEFINE_PROP_LINK("iothread", VirtIOSCSI
, parent_obj
.conf
.iothread
,
1298 TYPE_IOTHREAD
, IOThread
*),
1299 DEFINE_PROP_END_OF_LIST(),
1302 static const VMStateDescription vmstate_virtio_scsi
= {
1303 .name
= "virtio-scsi",
1304 .minimum_version_id
= 1,
1306 .fields
= (VMStateField
[]) {
1307 VMSTATE_VIRTIO_DEVICE
,
1308 VMSTATE_END_OF_LIST()
1312 static void virtio_scsi_common_class_init(ObjectClass
*klass
, void *data
)
1314 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1315 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1317 vdc
->get_config
= virtio_scsi_get_config
;
1318 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1321 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
1323 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1324 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1325 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1327 device_class_set_props(dc
, virtio_scsi_properties
);
1328 dc
->vmsd
= &vmstate_virtio_scsi
;
1329 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1330 vdc
->realize
= virtio_scsi_device_realize
;
1331 vdc
->unrealize
= virtio_scsi_device_unrealize
;
1332 vdc
->set_config
= virtio_scsi_set_config
;
1333 vdc
->get_features
= virtio_scsi_get_features
;
1334 vdc
->reset
= virtio_scsi_reset
;
1335 vdc
->start_ioeventfd
= virtio_scsi_dataplane_start
;
1336 vdc
->stop_ioeventfd
= virtio_scsi_dataplane_stop
;
1337 hc
->pre_plug
= virtio_scsi_pre_hotplug
;
1338 hc
->plug
= virtio_scsi_hotplug
;
1339 hc
->unplug
= virtio_scsi_hotunplug
;
1342 static const TypeInfo virtio_scsi_common_info
= {
1343 .name
= TYPE_VIRTIO_SCSI_COMMON
,
1344 .parent
= TYPE_VIRTIO_DEVICE
,
1345 .instance_size
= sizeof(VirtIOSCSICommon
),
1347 .class_init
= virtio_scsi_common_class_init
,
1350 static const TypeInfo virtio_scsi_info
= {
1351 .name
= TYPE_VIRTIO_SCSI
,
1352 .parent
= TYPE_VIRTIO_SCSI_COMMON
,
1353 .instance_size
= sizeof(VirtIOSCSI
),
1354 .class_init
= virtio_scsi_class_init
,
1355 .interfaces
= (InterfaceInfo
[]) {
1356 { TYPE_HOTPLUG_HANDLER
},
1361 static void virtio_register_types(void)
1363 type_register_static(&virtio_scsi_common_info
);
1364 type_register_static(&virtio_scsi_info
);
1367 type_init(virtio_register_types
)