Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / scsi / virtio-scsi.c
blob6637cfeaf517dc928be348fd8c1a91d29046da86
1 /*
2 * Virtio SCSI HBA
4 * Copyright IBM, Corp. 2010
5 * Copyright Red Hat, Inc. 2011
7 * Authors:
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"
23 #include "qemu/iov.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"
32 #include "trace.h"
34 typedef struct VirtIOSCSIReq {
36 * Note:
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;
42 VirtIOSCSI *dev;
43 VirtQueue *vq;
44 QEMUSGList qsgl;
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 */
51 int remaining;
53 SCSIRequest *sreq;
54 size_t resp_size;
55 enum SCSIXferMode mode;
56 union {
57 VirtIOSCSICmdResp cmd;
58 VirtIOSCSICtrlTMFResp tmf;
59 VirtIOSCSICtrlANResp an;
60 VirtIOSCSIEvent event;
61 } resp;
62 union {
63 VirtIOSCSICmdReq cmd;
64 VirtIOSCSICtrlTMFReq tmf;
65 VirtIOSCSICtrlANReq an;
66 } req;
67 } VirtIOSCSIReq;
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)
76 if (lun[0] != 1) {
77 return NULL;
79 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
80 return NULL;
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);
91 req->vq = vq;
92 req->dev = s;
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);
102 g_free(req);
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);
115 } else {
116 virtio_notify(vdev, vq);
119 if (req->sreq) {
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);
144 } else {
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;
161 size_t copied = 0;
163 while (num) {
164 if (skip >= iov->iov_len) {
165 skip -= iov->iov_len;
166 } else {
167 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip);
168 copied += iov->iov_len - skip;
169 skip = 0;
171 iov++;
172 addr++;
173 num--;
176 assert(skip == 0);
177 return copied;
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) {
188 return -EINVAL;
191 if (qemu_iovec_concat_iov(&req->resp_iov,
192 req->elem.in_sg, req->elem.in_num, 0,
193 resp_size) < resp_size) {
194 return -EINVAL;
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
202 * of the iovec.
204 * The actual length of the response header, stored in req->resp_size,
205 * does not change.
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,
220 req_size);
221 in_size = qemu_sgl_concat(req, req->elem.in_sg,
222 &req->elem.in_addr[0], req->elem.in_num,
223 resp_size);
225 if (out_size && in_size) {
226 return -ENOTSUP;
229 if (out_size) {
230 req->mode = SCSI_XFER_TO_DEV;
231 } else if (in_size) {
232 req->mode = SCSI_XFER_FROM_DEV;
235 return 0;
238 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
240 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s;
241 VirtIOSCSIReq *req;
243 req = virtqueue_pop(vq, sizeof(VirtIOSCSIReq) + vs->cdb_size);
244 if (!req) {
245 return NULL;
247 virtio_scsi_init_req(s, vq, req);
248 return 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);
269 VirtIOSCSIReq *req;
270 uint32_t n;
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");
281 exit(1);
284 scsi_req_ref(sreq);
285 req->sreq = sreq;
286 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
287 assert(req->sreq->cmd.mode == req->mode);
289 return req;
292 typedef struct {
293 Notifier notifier;
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,
301 notifier);
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);
310 g_free(n);
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);
324 BusChild *kid;
325 int target;
327 switch (req->req.tmf.subtype) {
328 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
329 if (!d) {
330 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
331 goto out;
333 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
334 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
335 goto out;
337 qatomic_inc(&s->resetting);
338 device_cold_reset(&d->qdev);
339 qatomic_dec(&s->resetting);
340 break;
342 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
343 target = req->req.tmf.lun[1];
344 qatomic_inc(&s->resetting);
346 rcu_read_lock();
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);
353 rcu_read_unlock();
355 qatomic_dec(&s->resetting);
356 break;
358 default:
359 g_assert_not_reached();
362 out:
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);
372 VirtIOSCSIReq *req;
373 VirtIOSCSIReq *tmp;
375 GLOBAL_STATE_CODE();
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);
384 s->tmf_bh = NULL;
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)
395 VirtIOSCSIReq *req;
396 VirtIOSCSIReq *tmp;
398 GLOBAL_STATE_CODE();
400 /* Called after ioeventfd has been stopped, so tmf_bh_lock is not needed */
401 if (s->tmf_bh) {
402 qemu_bh_delete(s->tmf_bh);
403 s->tmf_bh = NULL;
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);
422 if (!s->tmf_bh) {
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;
436 int ret = 0;
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:
455 if (!d) {
456 goto fail;
458 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
459 goto incorrect_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) {
464 break;
467 if (r) {
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;
478 } else {
479 VirtIOSCSICancelNotifier *notifier;
481 req->remaining = 1;
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, &notifier->notifier);
486 ret = -EINPROGRESS;
489 break;
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);
494 ret = -EINPROGRESS;
495 break;
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:
500 if (!d) {
501 goto fail;
503 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
504 goto incorrect_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.
512 req->remaining = 1;
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;
520 break;
521 } else {
522 VirtIOSCSICancelNotifier *notifier;
524 req->remaining++;
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, &notifier->notifier);
532 if (--req->remaining > 0) {
533 ret = -EINPROGRESS;
535 break;
537 case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
538 default:
539 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
540 break;
543 object_unref(OBJECT(d));
544 return ret;
546 incorrect_lun:
547 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
548 object_unref(OBJECT(d));
549 return ret;
551 fail:
552 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
553 object_unref(OBJECT(d));
554 return ret;
557 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
559 VirtIODevice *vdev = (VirtIODevice *)s;
560 uint32_t type;
561 int r = 0;
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);
566 return;
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);
574 return;
575 } else {
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);
584 return;
585 } else {
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;
594 if (r == 0) {
595 if (type == VIRTIO_SCSI_T_TMF)
596 trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req->req.tmf.lun),
597 req->req.tmf.tag,
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);
604 } else {
605 assert(r == -EINPROGRESS);
609 static void virtio_scsi_handle_ctrl_vq(VirtIOSCSI *s, VirtQueue *vq)
611 VirtIOSCSIReq *req;
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
620 * success.
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) {
629 return false;
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)) {
641 return;
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),
650 req->req.cmd.tag,
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) {
665 return;
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;
672 break;
673 case SCSI_HOST_BUSY:
674 req->resp.cmd.response = VIRTIO_SCSI_S_BUSY;
675 break;
676 case SCSI_HOST_TIME_OUT:
677 case SCSI_HOST_ABORTED:
678 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
679 break;
680 case SCSI_HOST_BAD_RESPONSE:
681 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
682 break;
683 case SCSI_HOST_RESET:
684 req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
685 break;
686 case SCSI_HOST_TRANSPORT_DISRUPTED:
687 req->resp.cmd.response = VIRTIO_SCSI_S_TRANSPORT_FAILURE;
688 break;
689 case SCSI_HOST_TARGET_FAILURE:
690 req->resp.cmd.response = VIRTIO_SCSI_S_TARGET_FAILURE;
691 break;
692 case SCSI_HOST_RESERVATION_ERROR:
693 req->resp.cmd.response = VIRTIO_SCSI_S_NEXUS_FAILURE;
694 break;
695 case SCSI_HOST_ALLOCATION_FAILURE:
696 case SCSI_HOST_MEDIUM_ERROR:
697 case SCSI_HOST_ERROR:
698 default:
699 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
700 break;
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];
709 uint32_t sense_len;
710 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
712 if (r->io_canceled) {
713 return;
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);
720 } else {
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),
725 sense, sense_len);
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,
733 void *hba_private)
735 VirtIOSCSIReq *req = hba_private;
737 if (cmd->len == 0) {
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;
747 return 0;
750 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
752 VirtIOSCSIReq *req = r->hba_private;
754 return &req->qsgl;
757 static void virtio_scsi_request_cancelled(SCSIRequest *r)
759 VirtIOSCSIReq *req = r->hba_private;
761 if (!req) {
762 return;
764 if (qatomic_read(&req->dev->resetting)) {
765 req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
766 } else {
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);
781 SCSIDevice *d;
782 int rc;
784 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
785 sizeof(VirtIOSCSICmdResp) + vs->sense_size);
786 if (rc < 0) {
787 if (rc == -ENOTSUP) {
788 virtio_scsi_fail_cmd_req(req);
789 return -ENOTSUP;
790 } else {
791 virtio_scsi_bad_req(req);
792 return -EINVAL;
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);
799 if (!d) {
800 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
801 virtio_scsi_complete_cmd_req(req);
802 return -ENOENT;
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));
815 return -ENOBUFS;
817 scsi_req_ref(req->sreq);
818 defer_call_begin();
819 object_unref(OBJECT(d));
820 return 0;
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);
829 defer_call_end();
830 scsi_req_unref(sreq);
833 static void virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq)
835 VirtIOSCSIReq *req, *next;
836 int ret = 0;
837 bool suppress_notifications = virtio_queue_get_notification(vq);
839 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
841 do {
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);
848 if (!ret) {
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);
855 defer_call_end();
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)) {
879 return;
882 virtio_scsi_handle_cmd_vq(s, vq);
885 static void virtio_scsi_get_config(VirtIODevice *vdev,
886 uint8_t *config)
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) {
912 virtio_error(vdev,
913 "bad data written to virtio-scsi configuration space");
914 return;
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,
923 Error **errp)
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;
950 typedef struct {
951 uint32_t event;
952 uint32_t reason;
953 union {
954 /* Used by messages specific to a device */
955 struct {
956 uint32_t id;
957 uint32_t lun;
958 } address;
960 } VirtIOSCSIEventInfo;
962 static void virtio_scsi_push_event(VirtIOSCSI *s,
963 const VirtIOSCSIEventInfo *info)
965 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
966 VirtIOSCSIReq *req;
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)) {
973 return;
976 req = virtio_scsi_pop_req(s, vs->event_vq);
977 if (!req) {
978 s->events_dropped = true;
979 return;
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);
989 return;
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) {
997 evt->lun[0] = 1;
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)) {
1026 return;
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),
1042 .address = {
1043 .id = dev->id,
1044 .lun = dev->lun,
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,
1060 Error **errp)
1062 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
1063 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1064 SCSIDevice *sd = SCSI_DEVICE(dev);
1065 int ret;
1067 if (s->ctx && !s->dataplane_fenced) {
1068 if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
1069 return;
1071 ret = blk_set_aio_context(sd->conf.blk, s->ctx, errp);
1072 if (ret < 0) {
1073 return;
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,
1081 .address = {
1082 .id = sd->id,
1083 .lun = sd->lun,
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,
1093 Error **errp)
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,
1101 .address = {
1102 .id = sd->id,
1103 .lun = sd->lun,
1107 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
1109 if (s->ctx) {
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
1132 * case.
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) {
1138 return;
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
1161 * ready.
1163 if (s->dataplane_stopping || !s->dataplane_started) {
1164 return;
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);
1171 } else {
1172 virtio_queue_aio_attach_host_notifier(vq, s->ctx);
1177 static struct SCSIBusInfo virtio_scsi_scsi_info = {
1178 .tcq = true,
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,
1199 Error **errp)
1201 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1202 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
1203 int i;
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.",
1214 s->conf.num_queues,
1215 VIRTIO_QUEUE_MAX - VIRTIO_SCSI_VQ_NUM_FIXED);
1216 virtio_cleanup(vdev);
1217 return;
1219 if (s->conf.virtqueue_size <= 2) {
1220 error_setg(errp, "invalid virtqueue_size property (= %" PRIu32 "), "
1221 "must be > 2", s->conf.virtqueue_size);
1222 return;
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);
1239 Error *err = NULL;
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,
1248 &err);
1249 if (err != NULL) {
1250 error_propagate(errp, err);
1251 return;
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);
1266 int i;
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,
1296 0xFFFF),
1297 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI, parent_obj.conf.cmd_per_lun,
1298 128),
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,
1311 .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),
1352 .abstract = true,
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)