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 "qemu/error-report.h"
22 #include "sysemu/block-backend.h"
23 #include "hw/scsi/scsi.h"
24 #include "block/scsi.h"
25 #include "hw/virtio/virtio-bus.h"
26 #include "hw/virtio/virtio-access.h"
28 static inline int virtio_scsi_get_lun(uint8_t *lun
)
30 return ((lun
[2] << 8) | lun
[3]) & 0x3FFF;
33 static inline SCSIDevice
*virtio_scsi_device_find(VirtIOSCSI
*s
, uint8_t *lun
)
38 if (lun
[2] != 0 && !(lun
[2] >= 0x40 && lun
[2] < 0x80)) {
41 return scsi_device_find(&s
->bus
, 0, lun
[1], virtio_scsi_get_lun(lun
));
44 void virtio_scsi_init_req(VirtIOSCSI
*s
, VirtQueue
*vq
, VirtIOSCSIReq
*req
)
46 const size_t zero_skip
=
47 offsetof(VirtIOSCSIReq
, resp_iov
) + sizeof(req
->resp_iov
);
51 qemu_sglist_init(&req
->qsgl
, DEVICE(s
), 8, &address_space_memory
);
52 qemu_iovec_init(&req
->resp_iov
, 1);
53 memset((uint8_t *)req
+ zero_skip
, 0, sizeof(*req
) - zero_skip
);
56 void virtio_scsi_free_req(VirtIOSCSIReq
*req
)
58 qemu_iovec_destroy(&req
->resp_iov
);
59 qemu_sglist_destroy(&req
->qsgl
);
63 static void virtio_scsi_complete_req(VirtIOSCSIReq
*req
)
65 VirtIOSCSI
*s
= req
->dev
;
66 VirtQueue
*vq
= req
->vq
;
67 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
69 qemu_iovec_from_buf(&req
->resp_iov
, 0, &req
->resp
, req
->resp_size
);
70 virtqueue_push(vq
, &req
->elem
, req
->qsgl
.size
+ req
->resp_iov
.size
);
71 if (s
->dataplane_started
&& !s
->dataplane_fenced
) {
72 virtio_notify_irqfd(vdev
, vq
);
74 virtio_notify(vdev
, vq
);
78 req
->sreq
->hba_private
= NULL
;
79 scsi_req_unref(req
->sreq
);
81 virtio_scsi_free_req(req
);
84 static void virtio_scsi_bad_req(VirtIOSCSIReq
*req
)
86 virtio_error(VIRTIO_DEVICE(req
->dev
), "wrong size for virtio-scsi headers");
87 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
88 virtio_scsi_free_req(req
);
91 static size_t qemu_sgl_concat(VirtIOSCSIReq
*req
, struct iovec
*iov
,
92 hwaddr
*addr
, int num
, size_t skip
)
94 QEMUSGList
*qsgl
= &req
->qsgl
;
98 if (skip
>= iov
->iov_len
) {
101 qemu_sglist_add(qsgl
, *addr
+ skip
, iov
->iov_len
- skip
);
102 copied
+= iov
->iov_len
- skip
;
114 static int virtio_scsi_parse_req(VirtIOSCSIReq
*req
,
115 unsigned req_size
, unsigned resp_size
)
117 VirtIODevice
*vdev
= (VirtIODevice
*) req
->dev
;
118 size_t in_size
, out_size
;
120 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
121 &req
->req
, req_size
) < req_size
) {
125 if (qemu_iovec_concat_iov(&req
->resp_iov
,
126 req
->elem
.in_sg
, req
->elem
.in_num
, 0,
127 resp_size
) < resp_size
) {
131 req
->resp_size
= resp_size
;
133 /* Old BIOSes left some padding by mistake after the req_size/resp_size.
134 * As a workaround, always consider the first buffer as the virtio-scsi
135 * request/response, making the payload start at the second element
138 * The actual length of the response header, stored in req->resp_size,
141 * TODO: always disable this workaround for virtio 1.0 devices.
143 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_ANY_LAYOUT
)) {
144 if (req
->elem
.out_num
) {
145 req_size
= req
->elem
.out_sg
[0].iov_len
;
147 if (req
->elem
.in_num
) {
148 resp_size
= req
->elem
.in_sg
[0].iov_len
;
152 out_size
= qemu_sgl_concat(req
, req
->elem
.out_sg
,
153 &req
->elem
.out_addr
[0], req
->elem
.out_num
,
155 in_size
= qemu_sgl_concat(req
, req
->elem
.in_sg
,
156 &req
->elem
.in_addr
[0], req
->elem
.in_num
,
159 if (out_size
&& in_size
) {
164 req
->mode
= SCSI_XFER_TO_DEV
;
165 } else if (in_size
) {
166 req
->mode
= SCSI_XFER_FROM_DEV
;
172 static VirtIOSCSIReq
*virtio_scsi_pop_req(VirtIOSCSI
*s
, VirtQueue
*vq
)
174 VirtIOSCSICommon
*vs
= (VirtIOSCSICommon
*)s
;
177 req
= virtqueue_pop(vq
, sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
181 virtio_scsi_init_req(s
, vq
, req
);
185 static void virtio_scsi_save_request(QEMUFile
*f
, SCSIRequest
*sreq
)
187 VirtIOSCSIReq
*req
= sreq
->hba_private
;
188 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(req
->dev
);
189 uint32_t n
= virtio_get_queue_index(req
->vq
) - 2;
191 assert(n
< vs
->conf
.num_queues
);
192 qemu_put_be32s(f
, &n
);
193 qemu_put_virtqueue_element(f
, &req
->elem
);
196 static void *virtio_scsi_load_request(QEMUFile
*f
, SCSIRequest
*sreq
)
198 SCSIBus
*bus
= sreq
->bus
;
199 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
200 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
201 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
205 qemu_get_be32s(f
, &n
);
206 assert(n
< vs
->conf
.num_queues
);
207 req
= qemu_get_virtqueue_element(vdev
, f
,
208 sizeof(VirtIOSCSIReq
) + vs
->cdb_size
);
209 virtio_scsi_init_req(s
, vs
->cmd_vqs
[n
], req
);
211 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
212 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
) < 0) {
213 error_report("invalid SCSI request migration data");
219 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
) {
220 assert(req
->sreq
->cmd
.mode
== req
->mode
);
227 VirtIOSCSIReq
*tmf_req
;
228 } VirtIOSCSICancelNotifier
;
230 static void virtio_scsi_cancel_notify(Notifier
*notifier
, void *data
)
232 VirtIOSCSICancelNotifier
*n
= container_of(notifier
,
233 VirtIOSCSICancelNotifier
,
236 if (--n
->tmf_req
->remaining
== 0) {
237 virtio_scsi_complete_req(n
->tmf_req
);
242 static inline void virtio_scsi_ctx_check(VirtIOSCSI
*s
, SCSIDevice
*d
)
244 if (s
->dataplane_started
&& d
&& blk_is_available(d
->conf
.blk
)) {
245 assert(blk_get_aio_context(d
->conf
.blk
) == s
->ctx
);
249 /* Return 0 if the request is ready to be completed and return to guest;
250 * -EINPROGRESS if the request is submitted and will be completed later, in the
251 * case of async cancellation. */
252 static int virtio_scsi_do_tmf(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
254 SCSIDevice
*d
= virtio_scsi_device_find(s
, req
->req
.tmf
.lun
);
255 SCSIRequest
*r
, *next
;
260 virtio_scsi_ctx_check(s
, d
);
261 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
262 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_OK
;
264 virtio_tswap32s(VIRTIO_DEVICE(s
), &req
->req
.tmf
.subtype
);
265 switch (req
->req
.tmf
.subtype
) {
266 case VIRTIO_SCSI_T_TMF_ABORT_TASK
:
267 case VIRTIO_SCSI_T_TMF_QUERY_TASK
:
271 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
274 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
275 VirtIOSCSIReq
*cmd_req
= r
->hba_private
;
276 if (cmd_req
&& cmd_req
->req
.cmd
.tag
== req
->req
.tmf
.tag
) {
282 * Assert that the request has not been completed yet, we
283 * check for it in the loop above.
285 assert(r
->hba_private
);
286 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK
) {
287 /* "If the specified command is present in the task set, then
288 * return a service response set to FUNCTION SUCCEEDED".
290 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
292 VirtIOSCSICancelNotifier
*notifier
;
295 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
296 notifier
->tmf_req
= req
;
297 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
298 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
304 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
308 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
312 qdev_reset_all(&d
->qdev
);
316 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET
:
317 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET
:
318 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
:
322 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
326 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
327 * This way, if the bus starts calling back to the notifiers
328 * even before we finish the loop, virtio_scsi_cancel_notify
329 * will not complete the TMF too early.
332 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
333 if (r
->hba_private
) {
334 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
) {
335 /* "If there is any command present in the task set, then
336 * return a service response set to FUNCTION SUCCEEDED".
338 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
341 VirtIOSCSICancelNotifier
*notifier
;
344 notifier
= g_new(VirtIOSCSICancelNotifier
, 1);
345 notifier
->notifier
.notify
= virtio_scsi_cancel_notify
;
346 notifier
->tmf_req
= req
;
347 scsi_req_cancel_async(r
, ¬ifier
->notifier
);
351 if (--req
->remaining
> 0) {
356 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
357 target
= req
->req
.tmf
.lun
[1];
359 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
360 d
= SCSI_DEVICE(kid
->child
);
361 if (d
->channel
== 0 && d
->id
== target
) {
362 qdev_reset_all(&d
->qdev
);
368 case VIRTIO_SCSI_T_TMF_CLEAR_ACA
:
370 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_REJECTED
;
377 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
381 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
385 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
387 VirtIODevice
*vdev
= (VirtIODevice
*)s
;
391 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
392 &type
, sizeof(type
)) < sizeof(type
)) {
393 virtio_scsi_bad_req(req
);
397 virtio_tswap32s(vdev
, &type
);
398 if (type
== VIRTIO_SCSI_T_TMF
) {
399 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlTMFReq
),
400 sizeof(VirtIOSCSICtrlTMFResp
)) < 0) {
401 virtio_scsi_bad_req(req
);
404 r
= virtio_scsi_do_tmf(s
, req
);
407 } else if (type
== VIRTIO_SCSI_T_AN_QUERY
||
408 type
== VIRTIO_SCSI_T_AN_SUBSCRIBE
) {
409 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlANReq
),
410 sizeof(VirtIOSCSICtrlANResp
)) < 0) {
411 virtio_scsi_bad_req(req
);
414 req
->resp
.an
.event_actual
= 0;
415 req
->resp
.an
.response
= VIRTIO_SCSI_S_OK
;
419 virtio_scsi_complete_req(req
);
421 assert(r
== -EINPROGRESS
);
425 static inline void virtio_scsi_acquire(VirtIOSCSI
*s
)
428 aio_context_acquire(s
->ctx
);
432 static inline void virtio_scsi_release(VirtIOSCSI
*s
)
435 aio_context_release(s
->ctx
);
439 bool virtio_scsi_handle_ctrl_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
442 bool progress
= false;
444 virtio_scsi_acquire(s
);
445 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
447 virtio_scsi_handle_ctrl_req(s
, req
);
449 virtio_scsi_release(s
);
453 static void virtio_scsi_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
455 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
458 virtio_device_start_ioeventfd(vdev
);
459 if (!s
->dataplane_fenced
) {
463 virtio_scsi_handle_ctrl_vq(s
, vq
);
466 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq
*req
)
468 /* Sense data is not in req->resp and is copied separately
469 * in virtio_scsi_command_complete.
471 req
->resp_size
= sizeof(VirtIOSCSICmdResp
);
472 virtio_scsi_complete_req(req
);
475 static void virtio_scsi_command_complete(SCSIRequest
*r
, uint32_t status
,
478 VirtIOSCSIReq
*req
= r
->hba_private
;
479 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
481 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
483 if (r
->io_canceled
) {
487 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OK
;
488 req
->resp
.cmd
.status
= status
;
489 if (req
->resp
.cmd
.status
== GOOD
) {
490 req
->resp
.cmd
.resid
= virtio_tswap32(vdev
, resid
);
492 req
->resp
.cmd
.resid
= 0;
493 sense_len
= scsi_req_get_sense(r
, sense
, sizeof(sense
));
494 sense_len
= MIN(sense_len
, req
->resp_iov
.size
- sizeof(req
->resp
.cmd
));
495 qemu_iovec_from_buf(&req
->resp_iov
, sizeof(req
->resp
.cmd
),
497 req
->resp
.cmd
.sense_len
= virtio_tswap32(vdev
, sense_len
);
499 virtio_scsi_complete_cmd_req(req
);
502 static int virtio_scsi_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
,
503 uint8_t *buf
, void *hba_private
)
505 VirtIOSCSIReq
*req
= hba_private
;
508 cmd
->len
= MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE
, SCSI_CMD_BUF_SIZE
);
509 memcpy(cmd
->buf
, buf
, cmd
->len
);
512 /* Extract the direction and mode directly from the request, for
513 * host device passthrough.
515 cmd
->xfer
= req
->qsgl
.size
;
516 cmd
->mode
= req
->mode
;
520 static QEMUSGList
*virtio_scsi_get_sg_list(SCSIRequest
*r
)
522 VirtIOSCSIReq
*req
= r
->hba_private
;
527 static void virtio_scsi_request_cancelled(SCSIRequest
*r
)
529 VirtIOSCSIReq
*req
= r
->hba_private
;
534 if (req
->dev
->resetting
) {
535 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_RESET
;
537 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_ABORTED
;
539 virtio_scsi_complete_cmd_req(req
);
542 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq
*req
)
544 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_FAILURE
;
545 virtio_scsi_complete_cmd_req(req
);
548 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
550 VirtIOSCSICommon
*vs
= &s
->parent_obj
;
554 rc
= virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
555 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
);
557 if (rc
== -ENOTSUP
) {
558 virtio_scsi_fail_cmd_req(req
);
561 virtio_scsi_bad_req(req
);
566 d
= virtio_scsi_device_find(s
, req
->req
.cmd
.lun
);
568 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
569 virtio_scsi_complete_cmd_req(req
);
572 virtio_scsi_ctx_check(s
, d
);
573 req
->sreq
= scsi_req_new(d
, req
->req
.cmd
.tag
,
574 virtio_scsi_get_lun(req
->req
.cmd
.lun
),
575 req
->req
.cmd
.cdb
, req
);
577 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
578 && (req
->sreq
->cmd
.mode
!= req
->mode
||
579 req
->sreq
->cmd
.xfer
> req
->qsgl
.size
)) {
580 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OVERRUN
;
581 virtio_scsi_complete_cmd_req(req
);
584 scsi_req_ref(req
->sreq
);
585 blk_io_plug(d
->conf
.blk
);
589 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
591 SCSIRequest
*sreq
= req
->sreq
;
592 if (scsi_req_enqueue(sreq
)) {
593 scsi_req_continue(sreq
);
595 blk_io_unplug(sreq
->dev
->conf
.blk
);
596 scsi_req_unref(sreq
);
599 bool virtio_scsi_handle_cmd_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
601 VirtIOSCSIReq
*req
, *next
;
603 bool progress
= false;
605 QTAILQ_HEAD(, VirtIOSCSIReq
) reqs
= QTAILQ_HEAD_INITIALIZER(reqs
);
607 virtio_scsi_acquire(s
);
609 virtio_queue_set_notification(vq
, 0);
611 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
613 ret
= virtio_scsi_handle_cmd_req_prepare(s
, req
);
615 QTAILQ_INSERT_TAIL(&reqs
, req
, next
);
616 } else if (ret
== -EINVAL
) {
617 /* The device is broken and shouldn't process any request */
618 while (!QTAILQ_EMPTY(&reqs
)) {
619 req
= QTAILQ_FIRST(&reqs
);
620 QTAILQ_REMOVE(&reqs
, req
, next
);
621 blk_io_unplug(req
->sreq
->dev
->conf
.blk
);
622 scsi_req_unref(req
->sreq
);
623 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
624 virtio_scsi_free_req(req
);
629 virtio_queue_set_notification(vq
, 1);
630 } while (ret
!= -EINVAL
&& !virtio_queue_empty(vq
));
632 QTAILQ_FOREACH_SAFE(req
, &reqs
, next
, next
) {
633 virtio_scsi_handle_cmd_req_submit(s
, req
);
635 virtio_scsi_release(s
);
639 static void virtio_scsi_handle_cmd(VirtIODevice
*vdev
, VirtQueue
*vq
)
641 /* use non-QOM casts in the data path */
642 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
645 virtio_device_start_ioeventfd(vdev
);
646 if (!s
->dataplane_fenced
) {
650 virtio_scsi_handle_cmd_vq(s
, vq
);
653 static void virtio_scsi_get_config(VirtIODevice
*vdev
,
656 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
657 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(vdev
);
659 virtio_stl_p(vdev
, &scsiconf
->num_queues
, s
->conf
.num_queues
);
660 virtio_stl_p(vdev
, &scsiconf
->seg_max
, 128 - 2);
661 virtio_stl_p(vdev
, &scsiconf
->max_sectors
, s
->conf
.max_sectors
);
662 virtio_stl_p(vdev
, &scsiconf
->cmd_per_lun
, s
->conf
.cmd_per_lun
);
663 virtio_stl_p(vdev
, &scsiconf
->event_info_size
, sizeof(VirtIOSCSIEvent
));
664 virtio_stl_p(vdev
, &scsiconf
->sense_size
, s
->sense_size
);
665 virtio_stl_p(vdev
, &scsiconf
->cdb_size
, s
->cdb_size
);
666 virtio_stw_p(vdev
, &scsiconf
->max_channel
, VIRTIO_SCSI_MAX_CHANNEL
);
667 virtio_stw_p(vdev
, &scsiconf
->max_target
, VIRTIO_SCSI_MAX_TARGET
);
668 virtio_stl_p(vdev
, &scsiconf
->max_lun
, VIRTIO_SCSI_MAX_LUN
);
671 static void virtio_scsi_set_config(VirtIODevice
*vdev
,
672 const uint8_t *config
)
674 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
675 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
677 if ((uint32_t) virtio_ldl_p(vdev
, &scsiconf
->sense_size
) >= 65536 ||
678 (uint32_t) virtio_ldl_p(vdev
, &scsiconf
->cdb_size
) >= 256) {
680 "bad data written to virtio-scsi configuration space");
684 vs
->sense_size
= virtio_ldl_p(vdev
, &scsiconf
->sense_size
);
685 vs
->cdb_size
= virtio_ldl_p(vdev
, &scsiconf
->cdb_size
);
688 static uint64_t virtio_scsi_get_features(VirtIODevice
*vdev
,
689 uint64_t requested_features
,
692 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
694 /* Firstly sync all virtio-scsi possible supported features */
695 requested_features
|= s
->host_features
;
696 return requested_features
;
699 static void virtio_scsi_reset(VirtIODevice
*vdev
)
701 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
702 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
704 assert(!s
->dataplane_started
);
706 qbus_reset_all(&s
->bus
.qbus
);
709 vs
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
710 vs
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
711 s
->events_dropped
= false;
714 void virtio_scsi_push_event(VirtIOSCSI
*s
, SCSIDevice
*dev
,
715 uint32_t event
, uint32_t reason
)
717 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
719 VirtIOSCSIEvent
*evt
;
720 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
722 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
726 virtio_scsi_acquire(s
);
728 req
= virtio_scsi_pop_req(s
, vs
->event_vq
);
730 s
->events_dropped
= true;
734 if (s
->events_dropped
) {
735 event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
736 s
->events_dropped
= false;
739 if (virtio_scsi_parse_req(req
, 0, sizeof(VirtIOSCSIEvent
))) {
740 virtio_scsi_bad_req(req
);
744 evt
= &req
->resp
.event
;
745 memset(evt
, 0, sizeof(VirtIOSCSIEvent
));
746 evt
->event
= virtio_tswap32(vdev
, event
);
747 evt
->reason
= virtio_tswap32(vdev
, reason
);
749 assert(event
== VIRTIO_SCSI_T_EVENTS_MISSED
);
752 evt
->lun
[1] = dev
->id
;
754 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
755 if (dev
->lun
>= 256) {
756 evt
->lun
[2] = (dev
->lun
>> 8) | 0x40;
758 evt
->lun
[3] = dev
->lun
& 0xFF;
760 virtio_scsi_complete_req(req
);
762 virtio_scsi_release(s
);
765 bool virtio_scsi_handle_event_vq(VirtIOSCSI
*s
, VirtQueue
*vq
)
767 virtio_scsi_acquire(s
);
768 if (s
->events_dropped
) {
769 virtio_scsi_push_event(s
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
770 virtio_scsi_release(s
);
773 virtio_scsi_release(s
);
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_handle_event_vq(s
, vq
);
790 static void virtio_scsi_change(SCSIBus
*bus
, SCSIDevice
*dev
, SCSISense sense
)
792 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
793 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
795 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_CHANGE
) &&
796 dev
->type
!= TYPE_ROM
) {
797 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_PARAM_CHANGE
,
798 sense
.asc
| (sense
.ascq
<< 8));
802 static void virtio_scsi_hotplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
805 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
806 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
807 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
809 if (s
->ctx
&& !s
->dataplane_fenced
) {
810 if (blk_op_is_blocked(sd
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
813 virtio_scsi_acquire(s
);
814 blk_set_aio_context(sd
->conf
.blk
, s
->ctx
);
815 virtio_scsi_release(s
);
819 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
820 virtio_scsi_push_event(s
, sd
,
821 VIRTIO_SCSI_T_TRANSPORT_RESET
,
822 VIRTIO_SCSI_EVT_RESET_RESCAN
);
826 static void virtio_scsi_hotunplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
829 VirtIODevice
*vdev
= VIRTIO_DEVICE(hotplug_dev
);
830 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
831 SCSIDevice
*sd
= SCSI_DEVICE(dev
);
833 if (virtio_vdev_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
)) {
834 virtio_scsi_push_event(s
, sd
,
835 VIRTIO_SCSI_T_TRANSPORT_RESET
,
836 VIRTIO_SCSI_EVT_RESET_REMOVED
);
839 qdev_simple_device_unplug_cb(hotplug_dev
, dev
, errp
);
842 static struct SCSIBusInfo virtio_scsi_scsi_info
= {
844 .max_channel
= VIRTIO_SCSI_MAX_CHANNEL
,
845 .max_target
= VIRTIO_SCSI_MAX_TARGET
,
846 .max_lun
= VIRTIO_SCSI_MAX_LUN
,
848 .complete
= virtio_scsi_command_complete
,
849 .cancel
= virtio_scsi_request_cancelled
,
850 .change
= virtio_scsi_change
,
851 .parse_cdb
= virtio_scsi_parse_cdb
,
852 .get_sg_list
= virtio_scsi_get_sg_list
,
853 .save_request
= virtio_scsi_save_request
,
854 .load_request
= virtio_scsi_load_request
,
857 void virtio_scsi_common_realize(DeviceState
*dev
, Error
**errp
,
858 VirtIOHandleOutput ctrl
,
859 VirtIOHandleOutput evt
,
860 VirtIOHandleOutput cmd
)
862 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
863 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(dev
);
866 virtio_init(vdev
, "virtio-scsi", VIRTIO_ID_SCSI
,
867 sizeof(VirtIOSCSIConfig
));
869 if (s
->conf
.num_queues
== 0 ||
870 s
->conf
.num_queues
> VIRTIO_QUEUE_MAX
- 2) {
871 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
872 "must be a positive integer less than %d.",
873 s
->conf
.num_queues
, VIRTIO_QUEUE_MAX
- 2);
874 virtio_cleanup(vdev
);
877 s
->cmd_vqs
= g_new0(VirtQueue
*, s
->conf
.num_queues
);
878 s
->sense_size
= VIRTIO_SCSI_SENSE_DEFAULT_SIZE
;
879 s
->cdb_size
= VIRTIO_SCSI_CDB_DEFAULT_SIZE
;
881 s
->ctrl_vq
= virtio_add_queue(vdev
, VIRTIO_SCSI_VQ_SIZE
, ctrl
);
882 s
->event_vq
= virtio_add_queue(vdev
, VIRTIO_SCSI_VQ_SIZE
, evt
);
883 for (i
= 0; i
< s
->conf
.num_queues
; i
++) {
884 s
->cmd_vqs
[i
] = virtio_add_queue(vdev
, VIRTIO_SCSI_VQ_SIZE
, cmd
);
888 static void virtio_scsi_device_realize(DeviceState
*dev
, Error
**errp
)
890 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
891 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
894 virtio_scsi_common_realize(dev
, &err
, virtio_scsi_handle_ctrl
,
895 virtio_scsi_handle_event
,
896 virtio_scsi_handle_cmd
);
898 error_propagate(errp
, err
);
902 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), dev
,
903 &virtio_scsi_scsi_info
, vdev
->bus_name
);
904 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
905 qbus_set_hotplug_handler(BUS(&s
->bus
), dev
, &error_abort
);
907 if (!dev
->hotplugged
) {
908 scsi_bus_legacy_handle_cmdline(&s
->bus
, &err
);
910 error_propagate(errp
, err
);
915 virtio_scsi_dataplane_setup(s
, errp
);
918 static void virtio_scsi_instance_init(Object
*obj
)
920 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(obj
);
922 object_property_add_link(obj
, "iothread", TYPE_IOTHREAD
,
923 (Object
**)&vs
->conf
.iothread
,
924 qdev_prop_allow_set_link_before_realize
,
925 OBJ_PROP_LINK_UNREF_ON_RELEASE
, &error_abort
);
928 void virtio_scsi_common_unrealize(DeviceState
*dev
, Error
**errp
)
930 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
931 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
934 virtio_cleanup(vdev
);
937 static void virtio_scsi_device_unrealize(DeviceState
*dev
, Error
**errp
)
939 virtio_scsi_common_unrealize(dev
, errp
);
942 static Property virtio_scsi_properties
[] = {
943 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI
, parent_obj
.conf
.num_queues
, 1),
944 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI
, parent_obj
.conf
.max_sectors
,
946 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI
, parent_obj
.conf
.cmd_per_lun
,
948 DEFINE_PROP_BIT("hotplug", VirtIOSCSI
, host_features
,
949 VIRTIO_SCSI_F_HOTPLUG
, true),
950 DEFINE_PROP_BIT("param_change", VirtIOSCSI
, host_features
,
951 VIRTIO_SCSI_F_CHANGE
, true),
952 DEFINE_PROP_END_OF_LIST(),
955 static const VMStateDescription vmstate_virtio_scsi
= {
956 .name
= "virtio-scsi",
957 .minimum_version_id
= 1,
959 .fields
= (VMStateField
[]) {
960 VMSTATE_VIRTIO_DEVICE
,
961 VMSTATE_END_OF_LIST()
965 static void virtio_scsi_common_class_init(ObjectClass
*klass
, void *data
)
967 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
968 DeviceClass
*dc
= DEVICE_CLASS(klass
);
970 vdc
->get_config
= virtio_scsi_get_config
;
971 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
974 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
976 DeviceClass
*dc
= DEVICE_CLASS(klass
);
977 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
978 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
980 dc
->props
= virtio_scsi_properties
;
981 dc
->vmsd
= &vmstate_virtio_scsi
;
982 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
983 vdc
->realize
= virtio_scsi_device_realize
;
984 vdc
->unrealize
= virtio_scsi_device_unrealize
;
985 vdc
->set_config
= virtio_scsi_set_config
;
986 vdc
->get_features
= virtio_scsi_get_features
;
987 vdc
->reset
= virtio_scsi_reset
;
988 vdc
->start_ioeventfd
= virtio_scsi_dataplane_start
;
989 vdc
->stop_ioeventfd
= virtio_scsi_dataplane_stop
;
990 hc
->plug
= virtio_scsi_hotplug
;
991 hc
->unplug
= virtio_scsi_hotunplug
;
994 static const TypeInfo virtio_scsi_common_info
= {
995 .name
= TYPE_VIRTIO_SCSI_COMMON
,
996 .parent
= TYPE_VIRTIO_DEVICE
,
997 .instance_size
= sizeof(VirtIOSCSICommon
),
999 .class_init
= virtio_scsi_common_class_init
,
1002 static const TypeInfo virtio_scsi_info
= {
1003 .name
= TYPE_VIRTIO_SCSI
,
1004 .parent
= TYPE_VIRTIO_SCSI_COMMON
,
1005 .instance_size
= sizeof(VirtIOSCSI
),
1006 .instance_init
= virtio_scsi_instance_init
,
1007 .class_init
= virtio_scsi_class_init
,
1008 .interfaces
= (InterfaceInfo
[]) {
1009 { TYPE_HOTPLUG_HANDLER
},
1014 static void virtio_register_types(void)
1016 type_register_static(&virtio_scsi_common_info
);
1017 type_register_static(&virtio_scsi_info
);
1020 type_init(virtio_register_types
)