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_complete_req_bh(void *opaque
)
128 VirtIOSCSIReq
*req
= opaque
;
130 virtio_scsi_complete_req(req
);
134 * Called from virtio_scsi_do_one_tmf_bh() in main loop thread. The main loop
135 * thread cannot touch the virtqueue since that could race with an IOThread.
137 static void virtio_scsi_complete_req_from_main_loop(VirtIOSCSIReq
*req
)
139 VirtIOSCSI
*s
= req
->dev
;
141 if (!s
->ctx
|| s
->ctx
== qemu_get_aio_context()) {
142 /* No need to schedule a BH when there is no IOThread */
143 virtio_scsi_complete_req(req
);
145 /* Run request completion in the IOThread */
146 aio_wait_bh_oneshot(s
->ctx
, virtio_scsi_complete_req_bh
, req
);
150 static void virtio_scsi_bad_req(VirtIOSCSIReq
*req
)
152 virtio_error(VIRTIO_DEVICE(req
->dev
), "wrong size for virtio-scsi headers");
153 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
154 virtio_scsi_free_req(req
);
157 static size_t qemu_sgl_concat(VirtIOSCSIReq
*req
, struct iovec
*iov
,
158 hwaddr
*addr
, int num
, size_t skip
)
160 QEMUSGList
*qsgl
= &req
->qsgl
;
164 if (skip
>= iov
->iov_len
) {
165 skip
-= iov
->iov_len
;
167 qemu_sglist_add(qsgl
, *addr
+ skip
, iov
->iov_len
- skip
);
168 copied
+= iov
->iov_len
- skip
;
180 static int virtio_scsi_parse_req(VirtIOSCSIReq
*req
,
181 unsigned req_size
, unsigned resp_size
)
183 VirtIODevice
*vdev
= (VirtIODevice
*) req
->dev
;
184 size_t in_size
, out_size
;
186 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
187 &req
->req
, req_size
) < req_size
) {
191 if (qemu_iovec_concat_iov(&req
->resp_iov
,
192 req
->elem
.in_sg
, req
->elem
.in_num
, 0,
193 resp_size
) < resp_size
) {
197 req
->resp_size
= resp_size
;
199 /* Old BIOSes left some padding by mistake after the req_size/resp_size.
200 * As a workaround, always consider the first buffer as the virtio-scsi
201 * request/response, making the payload start at the second element
204 * The actual length of the response header, stored in req->resp_size,
207 * TODO: always disable this workaround for virtio 1.0 devices.
209 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_ANY_LAYOUT
)) {
210 if (req
->elem
.out_num
) {
211 req_size
= req
->elem
.out_sg
[0].iov_len
;
213 if (req
->elem
.in_num
) {
214 resp_size
= req
->elem
.in_sg
[0].iov_len
;
218 out_size
= qemu_sgl_concat(req
, req
->elem
.out_sg
,
219 &req
->elem
.out_addr
[0], req
->elem
.out_num
,
221 in_size
= qemu_sgl_concat(req
, req
->elem
.in_sg
,
222 &req
->elem
.in_addr
[0], req
->elem
.in_num
,
225 if (out_size
&& in_size
) {
230 req
->mode
= SCSI_XFER_TO_DEV
;
231 } else if (in_size
) {
232 req
->mode
= SCSI_XFER_FROM_DEV
;
238 static VirtIOSCSIReq
*virtio_scsi_pop_req(VirtIOSCSI
*s
, VirtQueue
*vq
)
240 VirtIOSCSICommon
*vs
= (VirtIOSCSICommon
*)s
;
243 req
= virtqueue_pop(vq
, sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
247 virtio_scsi_init_req(s
, vq
, req
);
251 static void virtio_scsi_save_request(QEMUFile
*f
, SCSIRequest
*sreq
)
253 VirtIOSCSIReq
*req
= sreq
->hba_private
;
254 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(req
->dev
);
255 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
256 uint32_t n
= virtio_get_queue_index(req
->vq
) - VIRTIO_SCSI_VQ_NUM_FIXED
;
258 assert(n
< vs
->conf
.num_queues
);
259 qemu_put_be32s(f
, &n
);
260 qemu_put_virtqueue_element(vdev
, f
, &req
->elem
);
263 static void *virtio_scsi_load_request(QEMUFile
*f
, SCSIRequest
*sreq
)
265 SCSIBus
*bus
= sreq
->bus
;
266 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
267 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
268 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
272 qemu_get_be32s(f
, &n
);
273 assert(n
< vs
->conf
.num_queues
);
274 req
= qemu_get_virtqueue_element(vdev
, f
,
275 sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
276 virtio_scsi_init_req(s
, vs
->cmd_vqs
[n
], req
);
278 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
279 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
) < 0) {
280 error_report("invalid SCSI request migration data");
286 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
) {
287 assert(req
->sreq
->cmd
.mode
== req
->mode
);
294 VirtIOSCSIReq
*tmf_req
;
295 } VirtIOSCSICancelNotifier
;
297 static void virtio_scsi_cancel_notify(Notifier
*notifier
, void *data
)
299 VirtIOSCSICancelNotifier
*n
= container_of(notifier
,
300 VirtIOSCSICancelNotifier
,
303 if (--n
->tmf_req
->remaining
== 0) {
304 VirtIOSCSIReq
*req
= n
->tmf_req
;
306 trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req
->req
.tmf
.lun
),
307 req
->req
.tmf
.tag
, req
->resp
.tmf
.response
);
308 virtio_scsi_complete_req(req
);
313 static inline void virtio_scsi_ctx_check(VirtIOSCSI
*s
, SCSIDevice
*d
)
315 if (s
->dataplane_started
&& d
&& blk_is_available(d
->conf
.blk
)) {
316 assert(blk_get_aio_context(d
->conf
.blk
) == s
->ctx
);
320 static void virtio_scsi_do_one_tmf_bh(VirtIOSCSIReq
*req
)
322 VirtIOSCSI
*s
= req
->dev
;
323 SCSIDevice
*d
= virtio_scsi_device_get(s
, req
->req
.tmf
.lun
);
327 switch (req
->req
.tmf
.subtype
) {
328 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
330 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
333 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
334 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
337 qatomic_inc(&s
->resetting
);
338 device_cold_reset(&d
->qdev
);
339 qatomic_dec(&s
->resetting
);
342 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
343 target
= req
->req
.tmf
.lun
[1];
344 qatomic_inc(&s
->resetting
);
347 QTAILQ_FOREACH_RCU(kid
, &s
->bus
.qbus
.children
, sibling
) {
348 SCSIDevice
*d1
= SCSI_DEVICE(kid
->child
);
349 if (d1
->channel
== 0 && d1
->id
== target
) {
350 device_cold_reset(&d1
->qdev
);
355 qatomic_dec(&s
->resetting
);
359 g_assert_not_reached();
363 object_unref(OBJECT(d
));
364 virtio_scsi_complete_req_from_main_loop(req
);
367 /* Some TMFs must be processed from the main loop thread */
368 static void virtio_scsi_do_tmf_bh(void *opaque
)
370 VirtIOSCSI
*s
= opaque
;
371 QTAILQ_HEAD(, VirtIOSCSIReq
) reqs
= QTAILQ_HEAD_INITIALIZER(reqs
);
377 WITH_QEMU_LOCK_GUARD(&s
->tmf_bh_lock
) {
378 QTAILQ_FOREACH_SAFE(req
, &s
->tmf_bh_list
, next
, tmp
) {
379 QTAILQ_REMOVE(&s
->tmf_bh_list
, req
, next
);
380 QTAILQ_INSERT_TAIL(&reqs
, req
, next
);
383 qemu_bh_delete(s
->tmf_bh
);
387 QTAILQ_FOREACH_SAFE(req
, &reqs
, next
, tmp
) {
388 QTAILQ_REMOVE(&reqs
, req
, next
);
389 virtio_scsi_do_one_tmf_bh(req
);
393 static void virtio_scsi_reset_tmf_bh(VirtIOSCSI
*s
)
400 /* Called after ioeventfd has been stopped, so tmf_bh_lock is not needed */
402 qemu_bh_delete(s
->tmf_bh
);
406 QTAILQ_FOREACH_SAFE(req
, &s
->tmf_bh_list
, next
, tmp
) {
407 QTAILQ_REMOVE(&s
->tmf_bh_list
, req
, next
);
409 /* SAM-6 6.3.2 Hard reset */
410 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_TARGET_FAILURE
;
411 virtio_scsi_complete_req(req
);
415 static void virtio_scsi_defer_tmf_to_bh(VirtIOSCSIReq
*req
)
417 VirtIOSCSI
*s
= req
->dev
;
419 WITH_QEMU_LOCK_GUARD(&s
->tmf_bh_lock
) {
420 QTAILQ_INSERT_TAIL(&s
->tmf_bh_list
, req
, next
);
423 s
->tmf_bh
= qemu_bh_new(virtio_scsi_do_tmf_bh
, s
);
424 qemu_bh_schedule(s
->tmf_bh
);
429 /* Return 0 if the request is ready to be completed and return to guest;
430 * -EINPROGRESS if the request is submitted and will be completed later, in the
431 * case of async cancellation. */
432 static int virtio_scsi_do_tmf(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
434 SCSIDevice
*d
= virtio_scsi_device_get(s
, req
->req
.tmf
.lun
);
435 SCSIRequest
*r
, *next
;
438 virtio_scsi_ctx_check(s
, d
);
439 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
440 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_OK
;
443 * req->req.tmf has the QEMU_PACKED attribute. Don't use virtio_tswap32s()
444 * to avoid compiler errors.
446 req
->req
.tmf
.subtype
=
447 virtio_tswap32(VIRTIO_DEVICE(s
), req
->req
.tmf
.subtype
);
449 trace_virtio_scsi_tmf_req(virtio_scsi_get_lun(req
->req
.tmf
.lun
),
450 req
->req
.tmf
.tag
, req
->req
.tmf
.subtype
);
452 switch (req
->req
.tmf
.subtype
) {
453 case VIRTIO_SCSI_T_TMF_ABORT_TASK
:
454 case VIRTIO_SCSI_T_TMF_QUERY_TASK
:
458 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
461 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
462 VirtIOSCSIReq
*cmd_req
= r
->hba_private
;
463 if (cmd_req
&& cmd_req
->req
.cmd
.tag
== req
->req
.tmf
.tag
) {
469 * Assert that the request has not been completed yet, we
470 * check for it in the loop above.
472 assert(r
->hba_private
);
473 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK
) {
474 /* "If the specified command is present in the task set, then
475 * return a service response set to FUNCTION SUCCEEDED".
477 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
479 VirtIOSCSICancelNotifier
*notifier
;
482 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
483 notifier
->tmf_req
= req
;
484 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
485 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
491 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
492 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
493 virtio_scsi_defer_tmf_to_bh(req
);
497 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET
:
498 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET
:
499 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
:
503 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
507 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
508 * This way, if the bus starts calling back to the notifiers
509 * even before we finish the loop, virtio_scsi_cancel_notify
510 * will not complete the TMF too early.
513 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
514 if (r
->hba_private
) {
515 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
) {
516 /* "If there is any command present in the task set, then
517 * return a service response set to FUNCTION SUCCEEDED".
519 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
522 VirtIOSCSICancelNotifier
*notifier
;
525 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
526 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
527 notifier
->tmf_req
= req
;
528 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
532 if (--req
->remaining
> 0) {
537 case VIRTIO_SCSI_T_TMF_CLEAR_ACA
:
539 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_REJECTED
;
543 object_unref(OBJECT(d
));
547 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
548 object_unref(OBJECT(d
));
552 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
553 object_unref(OBJECT(d
));
557 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
559 VirtIODevice
*vdev
= (VirtIODevice
*)s
;
563 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
564 &type
, sizeof(type
)) < sizeof(type
)) {
565 virtio_scsi_bad_req(req
);
569 virtio_tswap32s(vdev
, &type
);
570 if (type
== VIRTIO_SCSI_T_TMF
) {
571 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlTMFReq
),
572 sizeof(VirtIOSCSICtrlTMFResp
)) < 0) {
573 virtio_scsi_bad_req(req
);
576 r
= virtio_scsi_do_tmf(s
, req
);
579 } else if (type
== VIRTIO_SCSI_T_AN_QUERY
||
580 type
== VIRTIO_SCSI_T_AN_SUBSCRIBE
) {
581 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlANReq
),
582 sizeof(VirtIOSCSICtrlANResp
)) < 0) {
583 virtio_scsi_bad_req(req
);
586 req
->req
.an
.event_requested
=
587 virtio_tswap32(VIRTIO_DEVICE(s
), req
->req
.an
.event_requested
);
588 trace_virtio_scsi_an_req(virtio_scsi_get_lun(req
->req
.an
.lun
),
589 req
->req
.an
.event_requested
);
590 req
->resp
.an
.event_actual
= 0;
591 req
->resp
.an
.response
= VIRTIO_SCSI_S_OK
;
595 if (type
== VIRTIO_SCSI_T_TMF
)
596 trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req
->req
.tmf
.lun
),
598 req
->resp
.tmf
.response
);
599 else if (type
== VIRTIO_SCSI_T_AN_QUERY
||
600 type
== VIRTIO_SCSI_T_AN_SUBSCRIBE
)
601 trace_virtio_scsi_an_resp(virtio_scsi_get_lun(req
->req
.an
.lun
),
602 req
->resp
.an
.response
);
603 virtio_scsi_complete_req(req
);
605 assert(r
== -EINPROGRESS
);
609 static void virtio_scsi_handle_ctrl_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
613 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
614 virtio_scsi_handle_ctrl_req(s
, req
);
619 * If dataplane is configured but not yet started, do so now and return true on
622 * Dataplane is started by the core virtio code but virtqueue handler functions
623 * can also be invoked when a guest kicks before DRIVER_OK, so this helper
624 * function helps us deal with manually starting ioeventfd in that case.
626 static bool virtio_scsi_defer_to_dataplane(VirtIOSCSI
*s
)
628 if (!s
->ctx
|| s
->dataplane_started
) {
632 virtio_device_start_ioeventfd(&s
->parent_obj
.parent_obj
);
633 return !s
->dataplane_fenced
;
636 static void virtio_scsi_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
638 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
640 if (virtio_scsi_defer_to_dataplane(s
)) {
644 virtio_scsi_handle_ctrl_vq(s
, vq
);
647 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq
*req
)
649 trace_virtio_scsi_cmd_resp(virtio_scsi_get_lun(req
->req
.cmd
.lun
),
651 req
->resp
.cmd
.response
,
652 req
->resp
.cmd
.status
);
653 /* Sense data is not in req->resp and is copied separately
654 * in virtio_scsi_command_complete.
656 req
->resp_size
= sizeof(VirtIOSCSICmdResp
);
657 virtio_scsi_complete_req(req
);
660 static void virtio_scsi_command_failed(SCSIRequest
*r
)
662 VirtIOSCSIReq
*req
= r
->hba_private
;
664 if (r
->io_canceled
) {
668 req
->resp
.cmd
.status
= GOOD
;
669 switch (r
->host_status
) {
670 case SCSI_HOST_NO_LUN
:
671 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
674 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BUSY
;
676 case SCSI_HOST_TIME_OUT
:
677 case SCSI_HOST_ABORTED
:
678 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_ABORTED
;
680 case SCSI_HOST_BAD_RESPONSE
:
681 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
683 case SCSI_HOST_RESET
:
684 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_RESET
;
686 case SCSI_HOST_TRANSPORT_DISRUPTED
:
687 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_TRANSPORT_FAILURE
;
689 case SCSI_HOST_TARGET_FAILURE
:
690 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_TARGET_FAILURE
;
692 case SCSI_HOST_RESERVATION_ERROR
:
693 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_NEXUS_FAILURE
;
695 case SCSI_HOST_ALLOCATION_FAILURE
:
696 case SCSI_HOST_MEDIUM_ERROR
:
697 case SCSI_HOST_ERROR
:
699 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_FAILURE
;
702 virtio_scsi_complete_cmd_req(req
);
705 static void virtio_scsi_command_complete(SCSIRequest
*r
, size_t resid
)
707 VirtIOSCSIReq
*req
= r
->hba_private
;
708 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
710 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
712 if (r
->io_canceled
) {
716 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OK
;
717 req
->resp
.cmd
.status
= r
->status
;
718 if (req
->resp
.cmd
.status
== GOOD
) {
719 req
->resp
.cmd
.resid
= virtio_tswap32(vdev
, resid
);
721 req
->resp
.cmd
.resid
= 0;
722 sense_len
= scsi_req_get_sense(r
, sense
, sizeof(sense
));
723 sense_len
= MIN(sense_len
, req
->resp_iov
.size
- sizeof(req
->resp
.cmd
));
724 qemu_iovec_from_buf(&req
->resp_iov
, sizeof(req
->resp
.cmd
),
726 req
->resp
.cmd
.sense_len
= virtio_tswap32(vdev
, sense_len
);
728 virtio_scsi_complete_cmd_req(req
);
731 static int virtio_scsi_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
,
732 uint8_t *buf
, size_t buf_len
,
735 VirtIOSCSIReq
*req
= hba_private
;
738 cmd
->len
= MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE
, SCSI_CMD_BUF_SIZE
);
739 memcpy(cmd
->buf
, buf
, cmd
->len
);
742 /* Extract the direction and mode directly from the request, for
743 * host device passthrough.
745 cmd
->xfer
= req
->qsgl
.size
;
746 cmd
->mode
= req
->mode
;
750 static QEMUSGList
*virtio_scsi_get_sg_list(SCSIRequest
*r
)
752 VirtIOSCSIReq
*req
= r
->hba_private
;
757 static void virtio_scsi_request_cancelled(SCSIRequest
*r
)
759 VirtIOSCSIReq
*req
= r
->hba_private
;
764 if (qatomic_read(&req
->dev
->resetting
)) {
765 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_RESET
;
767 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_ABORTED
;
769 virtio_scsi_complete_cmd_req(req
);
772 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq
*req
)
774 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_FAILURE
;
775 virtio_scsi_complete_cmd_req(req
);
778 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
780 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
784 rc
= virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
785 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
);
787 if (rc
== -ENOTSUP
) {
788 virtio_scsi_fail_cmd_req(req
);
791 virtio_scsi_bad_req(req
);
795 trace_virtio_scsi_cmd_req(virtio_scsi_get_lun(req
->req
.cmd
.lun
),
796 req
->req
.cmd
.tag
, req
->req
.cmd
.cdb
[0]);
798 d
= virtio_scsi_device_get(s
, req
->req
.cmd
.lun
);
800 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
801 virtio_scsi_complete_cmd_req(req
);
804 virtio_scsi_ctx_check(s
, d
);
805 req
->sreq
= scsi_req_new(d
, req
->req
.cmd
.tag
,
806 virtio_scsi_get_lun(req
->req
.cmd
.lun
),
807 req
->req
.cmd
.cdb
, vs
->cdb_size
, req
);
809 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
810 && (req
->sreq
->cmd
.mode
!= req
->mode
||
811 req
->sreq
->cmd
.xfer
> req
->qsgl
.size
)) {
812 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OVERRUN
;
813 virtio_scsi_complete_cmd_req(req
);
814 object_unref(OBJECT(d
));
817 scsi_req_ref(req
->sreq
);
819 object_unref(OBJECT(d
));
823 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
825 SCSIRequest
*sreq
= req
->sreq
;
826 if (scsi_req_enqueue(sreq
)) {
827 scsi_req_continue(sreq
);
830 scsi_req_unref(sreq
);
833 static void virtio_scsi_handle_cmd_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
835 VirtIOSCSIReq
*req
, *next
;
837 bool suppress_notifications
= virtio_queue_get_notification(vq
);
839 QTAILQ_HEAD(, VirtIOSCSIReq
) reqs
= QTAILQ_HEAD_INITIALIZER(reqs
);
842 if (suppress_notifications
) {
843 virtio_queue_set_notification(vq
, 0);
846 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
847 ret
= virtio_scsi_handle_cmd_req_prepare(s
, req
);
849 QTAILQ_INSERT_TAIL(&reqs
, req
, next
);
850 } else if (ret
== -EINVAL
) {
851 /* The device is broken and shouldn't process any request */
852 while (!QTAILQ_EMPTY(&reqs
)) {
853 req
= QTAILQ_FIRST(&reqs
);
854 QTAILQ_REMOVE(&reqs
, req
, next
);
856 scsi_req_unref(req
->sreq
);
857 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
858 virtio_scsi_free_req(req
);
863 if (suppress_notifications
) {
864 virtio_queue_set_notification(vq
, 1);
866 } while (ret
!= -EINVAL
&& !virtio_queue_empty(vq
));
868 QTAILQ_FOREACH_SAFE(req
, &reqs
, next
, next
) {
869 virtio_scsi_handle_cmd_req_submit(s
, req
);
873 static void virtio_scsi_handle_cmd(VirtIODevice
*vdev
, VirtQueue
*vq
)
875 /* use non-QOM casts in the data path */
876 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
878 if (virtio_scsi_defer_to_dataplane(s
)) {
882 virtio_scsi_handle_cmd_vq(s
, vq
);
885 static void virtio_scsi_get_config(VirtIODevice
*vdev
,
888 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
889 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(vdev
);
891 virtio_stl_p(vdev
, &scsiconf
->num_queues
, s
->conf
.num_queues
);
892 virtio_stl_p(vdev
, &scsiconf
->seg_max
,
893 s
->conf
.seg_max_adjust
? s
->conf
.virtqueue_size
- 2 : 128 - 2);
894 virtio_stl_p(vdev
, &scsiconf
->max_sectors
, s
->conf
.max_sectors
);
895 virtio_stl_p(vdev
, &scsiconf
->cmd_per_lun
, s
->conf
.cmd_per_lun
);
896 virtio_stl_p(vdev
, &scsiconf
->event_info_size
, sizeof(VirtIOSCSIEvent
));
897 virtio_stl_p(vdev
, &scsiconf
->sense_size
, s
->sense_size
);
898 virtio_stl_p(vdev
, &scsiconf
->cdb_size
, s
->cdb_size
);
899 virtio_stw_p(vdev
, &scsiconf
->max_channel
, VIRTIO_SCSI_MAX_CHANNEL
);
900 virtio_stw_p(vdev
, &scsiconf
->max_target
, VIRTIO_SCSI_MAX_TARGET
);
901 virtio_stl_p(vdev
, &scsiconf
->max_lun
, VIRTIO_SCSI_MAX_LUN
);
904 static void virtio_scsi_set_config(VirtIODevice
*vdev
,
905 const uint8_t *config
)
907 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
908 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
910 if ((uint32_t) virtio_ldl_p(vdev
, &scsiconf
->sense_size
) >= 65536 ||
911 (uint32_t) virtio_ldl_p(vdev
, &scsiconf
->cdb_size
) >= 256) {
913 "bad data written to virtio-scsi configuration space");
917 vs
->sense_size
= virtio_ldl_p(vdev
, &scsiconf
->sense_size
);
918 vs
->cdb_size
= virtio_ldl_p(vdev
, &scsiconf
->cdb_size
);
921 static uint64_t virtio_scsi_get_features(VirtIODevice
*vdev
,
922 uint64_t requested_features
,
925 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
927 /* Firstly sync all virtio-scsi possible supported features */
928 requested_features
|= s
->host_features
;
929 return requested_features
;
932 static void virtio_scsi_reset(VirtIODevice
*vdev
)
934 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
935 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
937 assert(!s
->dataplane_started
);
939 virtio_scsi_reset_tmf_bh(s
);
941 qatomic_inc(&s
->resetting
);
942 bus_cold_reset(BUS(&s
->bus
));
943 qatomic_dec(&s
->resetting
);
945 vs
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
946 vs
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
947 s
->events_dropped
= false;
954 /* Used by messages specific to a device */
960 } VirtIOSCSIEventInfo
;
962 static void virtio_scsi_push_event(VirtIOSCSI
*s
,
963 const VirtIOSCSIEventInfo
*info
)
965 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
967 VirtIOSCSIEvent
*evt
;
968 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
969 uint32_t event
= info
->event
;
970 uint32_t reason
= info
->reason
;
972 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
976 req
= virtio_scsi_pop_req(s
, vs
->event_vq
);
978 s
->events_dropped
= true;
982 if (s
->events_dropped
) {
983 event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
984 s
->events_dropped
= false;
987 if (virtio_scsi_parse_req(req
, 0, sizeof(VirtIOSCSIEvent
))) {
988 virtio_scsi_bad_req(req
);
992 evt
= &req
->resp
.event
;
993 memset(evt
, 0, sizeof(VirtIOSCSIEvent
));
994 evt
->event
= virtio_tswap32(vdev
, event
);
995 evt
->reason
= virtio_tswap32(vdev
, reason
);
996 if (event
!= VIRTIO_SCSI_T_EVENTS_MISSED
) {
998 evt
->lun
[1] = info
->address
.id
;
1000 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
1001 if (info
->address
.lun
>= 256) {
1002 evt
->lun
[2] = (info
->address
.lun
>> 8) | 0x40;
1004 evt
->lun
[3] = info
->address
.lun
& 0xFF;
1006 trace_virtio_scsi_event(virtio_scsi_get_lun(evt
->lun
), event
, reason
);
1008 virtio_scsi_complete_req(req
);
1011 static void virtio_scsi_handle_event_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
1013 if (s
->events_dropped
) {
1014 VirtIOSCSIEventInfo info
= {
1015 .event
= VIRTIO_SCSI_T_NO_EVENT
,
1017 virtio_scsi_push_event(s
, &info
);
1021 static void virtio_scsi_handle_event(VirtIODevice
*vdev
, VirtQueue
*vq
)
1023 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
1025 if (virtio_scsi_defer_to_dataplane(s
)) {
1029 virtio_scsi_handle_event_vq(s
, vq
);
1032 static void virtio_scsi_change(SCSIBus
*bus
, SCSIDevice
*dev
, SCSISense sense
)
1034 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
1035 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1037 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_CHANGE
) &&
1038 dev
->type
!= TYPE_ROM
) {
1039 VirtIOSCSIEventInfo info
= {
1040 .event
= VIRTIO_SCSI_T_PARAM_CHANGE
,
1041 .reason
= sense
.asc
| (sense
.ascq
<< 8),
1048 virtio_scsi_push_event(s
, &info
);
1052 static void virtio_scsi_pre_hotplug(HotplugHandler
*hotplug_dev
,
1053 DeviceState
*dev
, Error
**errp
)
1055 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
1056 sd
->hba_supports_iothread
= true;
1059 static void virtio_scsi_hotplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
1062 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
1063 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
1064 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
1067 if (s
->ctx
&& !s
->dataplane_fenced
) {
1068 if (blk_op_is_blocked(sd
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
1071 ret
= blk_set_aio_context(sd
->conf
.blk
, s
->ctx
, errp
);
1077 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
1078 VirtIOSCSIEventInfo info
= {
1079 .event
= VIRTIO_SCSI_T_TRANSPORT_RESET
,
1080 .reason
= VIRTIO_SCSI_EVT_RESET_RESCAN
,
1087 virtio_scsi_push_event(s
, &info
);
1088 scsi_bus_set_ua(&s
->bus
, SENSE_CODE(REPORTED_LUNS_CHANGED
));
1092 static void virtio_scsi_hotunplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
1095 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
1096 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
1097 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
1098 VirtIOSCSIEventInfo info
= {
1099 .event
= VIRTIO_SCSI_T_TRANSPORT_RESET
,
1100 .reason
= VIRTIO_SCSI_EVT_RESET_REMOVED
,
1107 qdev_simple_device_unplug_cb(hotplug_dev
, dev
, errp
);
1110 /* If other users keep the BlockBackend in the iothread, that's ok */
1111 blk_set_aio_context(sd
->conf
.blk
, qemu_get_aio_context(), NULL
);
1114 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
1115 virtio_scsi_push_event(s
, &info
);
1116 scsi_bus_set_ua(&s
->bus
, SENSE_CODE(REPORTED_LUNS_CHANGED
));
1120 /* Suspend virtqueue ioeventfd processing during drain */
1121 static void virtio_scsi_drained_begin(SCSIBus
*bus
)
1123 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
1124 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1125 uint32_t total_queues
= VIRTIO_SCSI_VQ_NUM_FIXED
+
1126 s
->parent_obj
.conf
.num_queues
;
1129 * Drain is called when stopping dataplane but the host notifier has
1130 * already been detached. Detaching multiple times is a no-op if nothing
1131 * else is using the monitoring same file descriptor, but avoid it just in
1134 * Also, don't detach if dataplane has not even been started yet because
1135 * the host notifier isn't attached.
1137 if (s
->dataplane_stopping
|| !s
->dataplane_started
) {
1141 for (uint32_t i
= 0; i
< total_queues
; i
++) {
1142 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
1143 virtio_queue_aio_detach_host_notifier(vq
, s
->ctx
);
1147 /* Resume virtqueue ioeventfd processing after drain */
1148 static void virtio_scsi_drained_end(SCSIBus
*bus
)
1150 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
1151 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
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 if (vq
== vs
->event_vq
) {
1170 virtio_queue_aio_attach_host_notifier_no_poll(vq
, s
->ctx
);
1172 virtio_queue_aio_attach_host_notifier(vq
, s
->ctx
);
1177 static struct SCSIBusInfo virtio_scsi_scsi_info
= {
1179 .max_channel
= VIRTIO_SCSI_MAX_CHANNEL
,
1180 .max_target
= VIRTIO_SCSI_MAX_TARGET
,
1181 .max_lun
= VIRTIO_SCSI_MAX_LUN
,
1183 .complete
= virtio_scsi_command_complete
,
1184 .fail
= virtio_scsi_command_failed
,
1185 .cancel
= virtio_scsi_request_cancelled
,
1186 .change
= virtio_scsi_change
,
1187 .parse_cdb
= virtio_scsi_parse_cdb
,
1188 .get_sg_list
= virtio_scsi_get_sg_list
,
1189 .save_request
= virtio_scsi_save_request
,
1190 .load_request
= virtio_scsi_load_request
,
1191 .drained_begin
= virtio_scsi_drained_begin
,
1192 .drained_end
= virtio_scsi_drained_end
,
1195 void virtio_scsi_common_realize(DeviceState
*dev
,
1196 VirtIOHandleOutput ctrl
,
1197 VirtIOHandleOutput evt
,
1198 VirtIOHandleOutput cmd
,
1201 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1202 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(dev
);
1205 virtio_init(vdev
, VIRTIO_ID_SCSI
, sizeof(VirtIOSCSIConfig
));
1207 if (s
->conf
.num_queues
== VIRTIO_SCSI_AUTO_NUM_QUEUES
) {
1208 s
->conf
.num_queues
= 1;
1210 if (s
->conf
.num_queues
== 0 ||
1211 s
->conf
.num_queues
> VIRTIO_QUEUE_MAX
- VIRTIO_SCSI_VQ_NUM_FIXED
) {
1212 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
1213 "must be a positive integer less than %d.",
1215 VIRTIO_QUEUE_MAX
- VIRTIO_SCSI_VQ_NUM_FIXED
);
1216 virtio_cleanup(vdev
);
1219 if (s
->conf
.virtqueue_size
<= 2) {
1220 error_setg(errp
, "invalid virtqueue_size property (= %" PRIu32
"), "
1221 "must be > 2", s
->conf
.virtqueue_size
);
1224 s
->cmd_vqs
= g_new0(VirtQueue
*, s
->conf
.num_queues
);
1225 s
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
1226 s
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
1228 s
->ctrl_vq
= virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, ctrl
);
1229 s
->event_vq
= virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, evt
);
1230 for (i
= 0; i
< s
->conf
.num_queues
; i
++) {
1231 s
->cmd_vqs
[i
] = virtio_add_queue(vdev
, s
->conf
.virtqueue_size
, cmd
);
1235 static void virtio_scsi_device_realize(DeviceState
*dev
, Error
**errp
)
1237 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1238 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
1241 QTAILQ_INIT(&s
->tmf_bh_list
);
1242 qemu_mutex_init(&s
->tmf_bh_lock
);
1244 virtio_scsi_common_realize(dev
,
1245 virtio_scsi_handle_ctrl
,
1246 virtio_scsi_handle_event
,
1247 virtio_scsi_handle_cmd
,
1250 error_propagate(errp
, err
);
1254 scsi_bus_init_named(&s
->bus
, sizeof(s
->bus
), dev
,
1255 &virtio_scsi_scsi_info
, vdev
->bus_name
);
1256 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
1257 qbus_set_hotplug_handler(BUS(&s
->bus
), OBJECT(dev
));
1259 virtio_scsi_dataplane_setup(s
, errp
);
1262 void virtio_scsi_common_unrealize(DeviceState
*dev
)
1264 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1265 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
1268 virtio_delete_queue(vs
->ctrl_vq
);
1269 virtio_delete_queue(vs
->event_vq
);
1270 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
1271 virtio_delete_queue(vs
->cmd_vqs
[i
]);
1273 g_free(vs
->cmd_vqs
);
1274 virtio_cleanup(vdev
);
1277 static void virtio_scsi_device_unrealize(DeviceState
*dev
)
1279 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
1281 virtio_scsi_reset_tmf_bh(s
);
1283 qbus_set_hotplug_handler(BUS(&s
->bus
), NULL
);
1284 virtio_scsi_common_unrealize(dev
);
1285 qemu_mutex_destroy(&s
->tmf_bh_lock
);
1288 static Property virtio_scsi_properties
[] = {
1289 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI
, parent_obj
.conf
.num_queues
,
1290 VIRTIO_SCSI_AUTO_NUM_QUEUES
),
1291 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSI
,
1292 parent_obj
.conf
.virtqueue_size
, 256),
1293 DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSI
,
1294 parent_obj
.conf
.seg_max_adjust
, true),
1295 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI
, parent_obj
.conf
.max_sectors
,
1297 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI
, parent_obj
.conf
.cmd_per_lun
,
1299 DEFINE_PROP_BIT("hotplug", VirtIOSCSI
, host_features
,
1300 VIRTIO_SCSI_F_HOTPLUG
, true),
1301 DEFINE_PROP_BIT("param_change", VirtIOSCSI
, host_features
,
1302 VIRTIO_SCSI_F_CHANGE
, true),
1303 DEFINE_PROP_LINK("iothread", VirtIOSCSI
, parent_obj
.conf
.iothread
,
1304 TYPE_IOTHREAD
, IOThread
*),
1305 DEFINE_PROP_END_OF_LIST(),
1308 static const VMStateDescription vmstate_virtio_scsi
= {
1309 .name
= "virtio-scsi",
1310 .minimum_version_id
= 1,
1312 .fields
= (const VMStateField
[]) {
1313 VMSTATE_VIRTIO_DEVICE
,
1314 VMSTATE_END_OF_LIST()
1318 static void virtio_scsi_common_class_init(ObjectClass
*klass
, void *data
)
1320 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1321 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1323 vdc
->get_config
= virtio_scsi_get_config
;
1324 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1327 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
1329 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1330 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1331 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1333 device_class_set_props(dc
, virtio_scsi_properties
);
1334 dc
->vmsd
= &vmstate_virtio_scsi
;
1335 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1336 vdc
->realize
= virtio_scsi_device_realize
;
1337 vdc
->unrealize
= virtio_scsi_device_unrealize
;
1338 vdc
->set_config
= virtio_scsi_set_config
;
1339 vdc
->get_features
= virtio_scsi_get_features
;
1340 vdc
->reset
= virtio_scsi_reset
;
1341 vdc
->start_ioeventfd
= virtio_scsi_dataplane_start
;
1342 vdc
->stop_ioeventfd
= virtio_scsi_dataplane_stop
;
1343 hc
->pre_plug
= virtio_scsi_pre_hotplug
;
1344 hc
->plug
= virtio_scsi_hotplug
;
1345 hc
->unplug
= virtio_scsi_hotunplug
;
1348 static const TypeInfo virtio_scsi_common_info
= {
1349 .name
= TYPE_VIRTIO_SCSI_COMMON
,
1350 .parent
= TYPE_VIRTIO_DEVICE
,
1351 .instance_size
= sizeof(VirtIOSCSICommon
),
1353 .class_init
= virtio_scsi_common_class_init
,
1356 static const TypeInfo virtio_scsi_info
= {
1357 .name
= TYPE_VIRTIO_SCSI
,
1358 .parent
= TYPE_VIRTIO_SCSI_COMMON
,
1359 .instance_size
= sizeof(VirtIOSCSI
),
1360 .class_init
= virtio_scsi_class_init
,
1361 .interfaces
= (InterfaceInfo
[]) {
1362 { TYPE_HOTPLUG_HANDLER
},
1367 static void virtio_register_types(void)
1369 type_register_static(&virtio_scsi_common_info
);
1370 type_register_static(&virtio_scsi_info
);
1373 type_init(virtio_register_types
)