Add Linux userfaultfd.h header
[qemu/ar7.git] / hw / scsi / virtio-scsi.c
blob76554011cbffb4c940514d9d98b09255c81ff51d
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 "standard-headers/linux/virtio_ids.h"
17 #include "hw/virtio/virtio-scsi.h"
18 #include "qemu/error-report.h"
19 #include "qemu/iov.h"
20 #include "sysemu/block-backend.h"
21 #include <hw/scsi/scsi.h>
22 #include <block/scsi.h>
23 #include <hw/virtio/virtio-bus.h>
24 #include "hw/virtio/virtio-access.h"
25 #include "migration/migration.h"
27 static inline int virtio_scsi_get_lun(uint8_t *lun)
29 return ((lun[2] << 8) | lun[3]) & 0x3FFF;
32 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun)
34 if (lun[0] != 1) {
35 return NULL;
37 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
38 return NULL;
40 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
43 VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq)
45 VirtIOSCSIReq *req;
46 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s;
47 const size_t zero_skip = offsetof(VirtIOSCSIReq, elem)
48 + sizeof(VirtQueueElement);
50 req = g_malloc(sizeof(*req) + vs->cdb_size);
51 req->vq = vq;
52 req->dev = s;
53 qemu_sglist_init(&req->qsgl, DEVICE(s), 8, &address_space_memory);
54 qemu_iovec_init(&req->resp_iov, 1);
55 memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip);
56 return req;
59 void virtio_scsi_free_req(VirtIOSCSIReq *req)
61 qemu_iovec_destroy(&req->resp_iov);
62 qemu_sglist_destroy(&req->qsgl);
63 g_free(req);
66 static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
68 VirtIOSCSI *s = req->dev;
69 VirtQueue *vq = req->vq;
70 VirtIODevice *vdev = VIRTIO_DEVICE(s);
72 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size);
73 if (req->vring) {
74 assert(req->vq == NULL);
75 virtio_scsi_vring_push_notify(req);
76 } else {
77 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size);
78 virtio_notify(vdev, vq);
81 if (req->sreq) {
82 req->sreq->hba_private = NULL;
83 scsi_req_unref(req->sreq);
85 virtio_scsi_free_req(req);
88 static void virtio_scsi_bad_req(void)
90 error_report("wrong size for virtio-scsi headers");
91 exit(1);
94 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov,
95 hwaddr *addr, int num, size_t skip)
97 QEMUSGList *qsgl = &req->qsgl;
98 size_t copied = 0;
100 while (num) {
101 if (skip >= iov->iov_len) {
102 skip -= iov->iov_len;
103 } else {
104 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip);
105 copied += iov->iov_len - skip;
106 skip = 0;
108 iov++;
109 addr++;
110 num--;
113 assert(skip == 0);
114 return copied;
117 static int virtio_scsi_parse_req(VirtIOSCSIReq *req,
118 unsigned req_size, unsigned resp_size)
120 VirtIODevice *vdev = (VirtIODevice *) req->dev;
121 size_t in_size, out_size;
123 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
124 &req->req, req_size) < req_size) {
125 return -EINVAL;
128 if (qemu_iovec_concat_iov(&req->resp_iov,
129 req->elem.in_sg, req->elem.in_num, 0,
130 resp_size) < resp_size) {
131 return -EINVAL;
134 req->resp_size = resp_size;
136 /* Old BIOSes left some padding by mistake after the req_size/resp_size.
137 * As a workaround, always consider the first buffer as the virtio-scsi
138 * request/response, making the payload start at the second element
139 * of the iovec.
141 * The actual length of the response header, stored in req->resp_size,
142 * does not change.
144 * TODO: always disable this workaround for virtio 1.0 devices.
146 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) {
147 if (req->elem.out_num) {
148 req_size = req->elem.out_sg[0].iov_len;
150 if (req->elem.in_num) {
151 resp_size = req->elem.in_sg[0].iov_len;
155 out_size = qemu_sgl_concat(req, req->elem.out_sg,
156 &req->elem.out_addr[0], req->elem.out_num,
157 req_size);
158 in_size = qemu_sgl_concat(req, req->elem.in_sg,
159 &req->elem.in_addr[0], req->elem.in_num,
160 resp_size);
162 if (out_size && in_size) {
163 return -ENOTSUP;
166 if (out_size) {
167 req->mode = SCSI_XFER_TO_DEV;
168 } else if (in_size) {
169 req->mode = SCSI_XFER_FROM_DEV;
172 return 0;
175 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
177 VirtIOSCSIReq *req = virtio_scsi_init_req(s, vq);
178 if (!virtqueue_pop(vq, &req->elem)) {
179 virtio_scsi_free_req(req);
180 return NULL;
182 return 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_queue_get_id(req->vq) - 2;
191 assert(n < vs->conf.num_queues);
192 qemu_put_be32s(f, &n);
193 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(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 VirtIOSCSIReq *req;
202 uint32_t n;
204 qemu_get_be32s(f, &n);
205 assert(n < vs->conf.num_queues);
206 req = virtio_scsi_init_req(s, vs->cmd_vqs[n]);
207 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
209 virtqueue_map(&req->elem);
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");
214 exit(1);
217 scsi_req_ref(sreq);
218 req->sreq = sreq;
219 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
220 assert(req->sreq->cmd.mode == req->mode);
222 return req;
225 typedef struct {
226 Notifier notifier;
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,
234 notifier);
236 if (--n->tmf_req->remaining == 0) {
237 virtio_scsi_complete_req(n->tmf_req);
239 g_free(n);
242 /* Return 0 if the request is ready to be completed and return to guest;
243 * -EINPROGRESS if the request is submitted and will be completed later, in the
244 * case of async cancellation. */
245 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
247 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun);
248 SCSIRequest *r, *next;
249 BusChild *kid;
250 int target;
251 int ret = 0;
253 if (s->dataplane_started) {
254 assert(blk_get_aio_context(d->conf.blk) == s->ctx);
256 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
257 req->resp.tmf.response = VIRTIO_SCSI_S_OK;
259 virtio_tswap32s(VIRTIO_DEVICE(s), &req->req.tmf.subtype);
260 switch (req->req.tmf.subtype) {
261 case VIRTIO_SCSI_T_TMF_ABORT_TASK:
262 case VIRTIO_SCSI_T_TMF_QUERY_TASK:
263 if (!d) {
264 goto fail;
266 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
267 goto incorrect_lun;
269 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
270 VirtIOSCSIReq *cmd_req = r->hba_private;
271 if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) {
272 break;
275 if (r) {
277 * Assert that the request has not been completed yet, we
278 * check for it in the loop above.
280 assert(r->hba_private);
281 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) {
282 /* "If the specified command is present in the task set, then
283 * return a service response set to FUNCTION SUCCEEDED".
285 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
286 } else {
287 VirtIOSCSICancelNotifier *notifier;
289 req->remaining = 1;
290 notifier = g_new(VirtIOSCSICancelNotifier, 1);
291 notifier->tmf_req = req;
292 notifier->notifier.notify = virtio_scsi_cancel_notify;
293 scsi_req_cancel_async(r, &notifier->notifier);
294 ret = -EINPROGRESS;
297 break;
299 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
300 if (!d) {
301 goto fail;
303 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
304 goto incorrect_lun;
306 s->resetting++;
307 qdev_reset_all(&d->qdev);
308 s->resetting--;
309 break;
311 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
312 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
313 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
314 if (!d) {
315 goto fail;
317 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
318 goto incorrect_lun;
321 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
322 * This way, if the bus starts calling back to the notifiers
323 * even before we finish the loop, virtio_scsi_cancel_notify
324 * will not complete the TMF too early.
326 req->remaining = 1;
327 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
328 if (r->hba_private) {
329 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) {
330 /* "If there is any command present in the task set, then
331 * return a service response set to FUNCTION SUCCEEDED".
333 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
334 break;
335 } else {
336 VirtIOSCSICancelNotifier *notifier;
338 req->remaining++;
339 notifier = g_new(VirtIOSCSICancelNotifier, 1);
340 notifier->notifier.notify = virtio_scsi_cancel_notify;
341 notifier->tmf_req = req;
342 scsi_req_cancel_async(r, &notifier->notifier);
346 if (--req->remaining > 0) {
347 ret = -EINPROGRESS;
349 break;
351 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
352 target = req->req.tmf.lun[1];
353 s->resetting++;
354 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
355 d = DO_UPCAST(SCSIDevice, qdev, kid->child);
356 if (d->channel == 0 && d->id == target) {
357 qdev_reset_all(&d->qdev);
360 s->resetting--;
361 break;
363 case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
364 default:
365 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
366 break;
369 return ret;
371 incorrect_lun:
372 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
373 return ret;
375 fail:
376 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
377 return ret;
380 void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
382 VirtIODevice *vdev = (VirtIODevice *)s;
383 uint32_t type;
384 int r = 0;
386 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
387 &type, sizeof(type)) < sizeof(type)) {
388 virtio_scsi_bad_req();
389 return;
392 virtio_tswap32s(vdev, &type);
393 if (type == VIRTIO_SCSI_T_TMF) {
394 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq),
395 sizeof(VirtIOSCSICtrlTMFResp)) < 0) {
396 virtio_scsi_bad_req();
397 } else {
398 r = virtio_scsi_do_tmf(s, req);
401 } else if (type == VIRTIO_SCSI_T_AN_QUERY ||
402 type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
403 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq),
404 sizeof(VirtIOSCSICtrlANResp)) < 0) {
405 virtio_scsi_bad_req();
406 } else {
407 req->resp.an.event_actual = 0;
408 req->resp.an.response = VIRTIO_SCSI_S_OK;
411 if (r == 0) {
412 virtio_scsi_complete_req(req);
413 } else {
414 assert(r == -EINPROGRESS);
418 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
420 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
421 VirtIOSCSIReq *req;
423 if (s->ctx && !s->dataplane_disabled) {
424 virtio_scsi_dataplane_start(s);
425 return;
427 while ((req = virtio_scsi_pop_req(s, vq))) {
428 virtio_scsi_handle_ctrl_req(s, req);
432 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req)
434 /* Sense data is not in req->resp and is copied separately
435 * in virtio_scsi_command_complete.
437 req->resp_size = sizeof(VirtIOSCSICmdResp);
438 virtio_scsi_complete_req(req);
441 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status,
442 size_t resid)
444 VirtIOSCSIReq *req = r->hba_private;
445 uint8_t sense[SCSI_SENSE_BUF_SIZE];
446 uint32_t sense_len;
447 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
449 if (r->io_canceled) {
450 return;
453 req->resp.cmd.response = VIRTIO_SCSI_S_OK;
454 req->resp.cmd.status = status;
455 if (req->resp.cmd.status == GOOD) {
456 req->resp.cmd.resid = virtio_tswap32(vdev, resid);
457 } else {
458 req->resp.cmd.resid = 0;
459 sense_len = scsi_req_get_sense(r, sense, sizeof(sense));
460 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd));
461 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
462 sense, sense_len);
463 req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
465 virtio_scsi_complete_cmd_req(req);
468 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd,
469 uint8_t *buf, void *hba_private)
471 VirtIOSCSIReq *req = hba_private;
473 if (cmd->len == 0) {
474 cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE);
475 memcpy(cmd->buf, buf, cmd->len);
478 /* Extract the direction and mode directly from the request, for
479 * host device passthrough.
481 cmd->xfer = req->qsgl.size;
482 cmd->mode = req->mode;
483 return 0;
486 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
488 VirtIOSCSIReq *req = r->hba_private;
490 return &req->qsgl;
493 static void virtio_scsi_request_cancelled(SCSIRequest *r)
495 VirtIOSCSIReq *req = r->hba_private;
497 if (!req) {
498 return;
500 if (req->dev->resetting) {
501 req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
502 } else {
503 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
505 virtio_scsi_complete_cmd_req(req);
508 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
510 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
511 virtio_scsi_complete_cmd_req(req);
514 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
516 VirtIOSCSICommon *vs = &s->parent_obj;
517 SCSIDevice *d;
518 int rc;
520 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
521 sizeof(VirtIOSCSICmdResp) + vs->sense_size);
522 if (rc < 0) {
523 if (rc == -ENOTSUP) {
524 virtio_scsi_fail_cmd_req(req);
525 } else {
526 virtio_scsi_bad_req();
528 return false;
531 d = virtio_scsi_device_find(s, req->req.cmd.lun);
532 if (!d) {
533 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
534 virtio_scsi_complete_cmd_req(req);
535 return false;
537 if (s->dataplane_started) {
538 assert(blk_get_aio_context(d->conf.blk) == s->ctx);
540 req->sreq = scsi_req_new(d, req->req.cmd.tag,
541 virtio_scsi_get_lun(req->req.cmd.lun),
542 req->req.cmd.cdb, req);
544 if (req->sreq->cmd.mode != SCSI_XFER_NONE
545 && (req->sreq->cmd.mode != req->mode ||
546 req->sreq->cmd.xfer > req->qsgl.size)) {
547 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN;
548 virtio_scsi_complete_cmd_req(req);
549 return false;
551 scsi_req_ref(req->sreq);
552 blk_io_plug(d->conf.blk);
553 return true;
556 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req)
558 SCSIRequest *sreq = req->sreq;
559 if (scsi_req_enqueue(sreq)) {
560 scsi_req_continue(sreq);
562 blk_io_unplug(sreq->dev->conf.blk);
563 scsi_req_unref(sreq);
566 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
568 /* use non-QOM casts in the data path */
569 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
570 VirtIOSCSIReq *req, *next;
571 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
573 if (s->ctx && !s->dataplane_disabled) {
574 virtio_scsi_dataplane_start(s);
575 return;
577 while ((req = virtio_scsi_pop_req(s, vq))) {
578 if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
579 QTAILQ_INSERT_TAIL(&reqs, req, next);
583 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
584 virtio_scsi_handle_cmd_req_submit(s, req);
588 static void virtio_scsi_get_config(VirtIODevice *vdev,
589 uint8_t *config)
591 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
592 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
594 virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
595 virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2);
596 virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors);
597 virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun);
598 virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
599 virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size);
600 virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size);
601 virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
602 virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
603 virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
606 static void virtio_scsi_set_config(VirtIODevice *vdev,
607 const uint8_t *config)
609 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
610 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
612 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 ||
613 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) {
614 error_report("bad data written to virtio-scsi configuration space");
615 exit(1);
618 vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size);
619 vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size);
622 static uint64_t virtio_scsi_get_features(VirtIODevice *vdev,
623 uint64_t requested_features,
624 Error **errp)
626 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
628 /* Firstly sync all virtio-scsi possible supported features */
629 requested_features |= s->host_features;
630 return requested_features;
633 static void virtio_scsi_reset(VirtIODevice *vdev)
635 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
636 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
638 if (s->ctx) {
639 virtio_scsi_dataplane_stop(s);
641 s->resetting++;
642 qbus_reset_all(&s->bus.qbus);
643 s->resetting--;
645 vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
646 vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
647 s->events_dropped = false;
650 /* The device does not have anything to save beyond the virtio data.
651 * Request data is saved with callbacks from SCSI devices.
653 static void virtio_scsi_save(QEMUFile *f, void *opaque)
655 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
656 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
658 if (s->dataplane_started) {
659 virtio_scsi_dataplane_stop(s);
661 virtio_save(vdev, f);
664 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id)
666 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
667 int ret;
669 ret = virtio_load(vdev, f, version_id);
670 if (ret) {
671 return ret;
673 return 0;
676 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev,
677 uint32_t event, uint32_t reason)
679 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
680 VirtIOSCSIReq *req;
681 VirtIOSCSIEvent *evt;
682 VirtIODevice *vdev = VIRTIO_DEVICE(s);
684 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
685 return;
688 if (s->dataplane_started) {
689 assert(s->ctx);
690 aio_context_acquire(s->ctx);
693 if (s->dataplane_started) {
694 req = virtio_scsi_pop_req_vring(s, s->event_vring);
695 } else {
696 req = virtio_scsi_pop_req(s, vs->event_vq);
698 if (!req) {
699 s->events_dropped = true;
700 goto out;
703 if (s->events_dropped) {
704 event |= VIRTIO_SCSI_T_EVENTS_MISSED;
705 s->events_dropped = false;
708 if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) {
709 virtio_scsi_bad_req();
712 evt = &req->resp.event;
713 memset(evt, 0, sizeof(VirtIOSCSIEvent));
714 evt->event = virtio_tswap32(vdev, event);
715 evt->reason = virtio_tswap32(vdev, reason);
716 if (!dev) {
717 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED);
718 } else {
719 evt->lun[0] = 1;
720 evt->lun[1] = dev->id;
722 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
723 if (dev->lun >= 256) {
724 evt->lun[2] = (dev->lun >> 8) | 0x40;
726 evt->lun[3] = dev->lun & 0xFF;
728 virtio_scsi_complete_req(req);
729 out:
730 if (s->dataplane_started) {
731 aio_context_release(s->ctx);
735 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
737 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
739 if (s->ctx && !s->dataplane_disabled) {
740 virtio_scsi_dataplane_start(s);
741 return;
743 if (s->events_dropped) {
744 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
748 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
750 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
751 VirtIODevice *vdev = VIRTIO_DEVICE(s);
753 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) &&
754 dev->type != TYPE_ROM) {
755 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE,
756 sense.asc | (sense.ascq << 8));
760 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
761 Error **errp)
763 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
764 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
765 SCSIDevice *sd = SCSI_DEVICE(dev);
767 if (s->ctx && !s->dataplane_disabled) {
768 if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
769 return;
771 blk_op_block_all(sd->conf.blk, s->blocker);
772 aio_context_acquire(s->ctx);
773 blk_set_aio_context(sd->conf.blk, s->ctx);
774 aio_context_release(s->ctx);
777 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
778 virtio_scsi_push_event(s, sd,
779 VIRTIO_SCSI_T_TRANSPORT_RESET,
780 VIRTIO_SCSI_EVT_RESET_RESCAN);
784 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
785 Error **errp)
787 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
788 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
789 SCSIDevice *sd = SCSI_DEVICE(dev);
791 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
792 virtio_scsi_push_event(s, sd,
793 VIRTIO_SCSI_T_TRANSPORT_RESET,
794 VIRTIO_SCSI_EVT_RESET_REMOVED);
797 if (s->ctx) {
798 blk_op_unblock_all(sd->conf.blk, s->blocker);
800 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
803 static struct SCSIBusInfo virtio_scsi_scsi_info = {
804 .tcq = true,
805 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
806 .max_target = VIRTIO_SCSI_MAX_TARGET,
807 .max_lun = VIRTIO_SCSI_MAX_LUN,
809 .complete = virtio_scsi_command_complete,
810 .cancel = virtio_scsi_request_cancelled,
811 .change = virtio_scsi_change,
812 .parse_cdb = virtio_scsi_parse_cdb,
813 .get_sg_list = virtio_scsi_get_sg_list,
814 .save_request = virtio_scsi_save_request,
815 .load_request = virtio_scsi_load_request,
818 void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
819 HandleOutput ctrl, HandleOutput evt,
820 HandleOutput cmd)
822 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
823 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
824 int i;
826 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI,
827 sizeof(VirtIOSCSIConfig));
829 if (s->conf.num_queues == 0 ||
830 s->conf.num_queues > VIRTIO_QUEUE_MAX - 2) {
831 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
832 "must be a positive integer less than %d.",
833 s->conf.num_queues, VIRTIO_QUEUE_MAX - 2);
834 virtio_cleanup(vdev);
835 return;
837 s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues);
838 s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
839 s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
841 s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
842 ctrl);
843 s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
844 evt);
845 for (i = 0; i < s->conf.num_queues; i++) {
846 s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
847 cmd);
850 if (s->conf.iothread) {
851 virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
855 /* Disable dataplane thread during live migration since it does not
856 * update the dirty memory bitmap yet.
858 static void virtio_scsi_migration_state_changed(Notifier *notifier, void *data)
860 VirtIOSCSI *s = container_of(notifier, VirtIOSCSI,
861 migration_state_notifier);
862 MigrationState *mig = data;
864 if (migration_in_setup(mig)) {
865 if (!s->dataplane_started) {
866 return;
868 virtio_scsi_dataplane_stop(s);
869 s->dataplane_disabled = true;
870 } else if (migration_has_finished(mig) ||
871 migration_has_failed(mig)) {
872 if (s->dataplane_started) {
873 return;
875 blk_drain_all(); /* complete in-flight non-dataplane requests */
876 s->dataplane_disabled = false;
880 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp)
882 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
883 VirtIOSCSI *s = VIRTIO_SCSI(dev);
884 static int virtio_scsi_id;
885 Error *err = NULL;
887 virtio_scsi_common_realize(dev, &err, virtio_scsi_handle_ctrl,
888 virtio_scsi_handle_event,
889 virtio_scsi_handle_cmd);
890 if (err != NULL) {
891 error_propagate(errp, err);
892 return;
895 scsi_bus_new(&s->bus, sizeof(s->bus), dev,
896 &virtio_scsi_scsi_info, vdev->bus_name);
897 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
898 qbus_set_hotplug_handler(BUS(&s->bus), dev, &error_abort);
900 if (!dev->hotplugged) {
901 scsi_bus_legacy_handle_cmdline(&s->bus, &err);
902 if (err != NULL) {
903 error_propagate(errp, err);
904 return;
908 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1,
909 virtio_scsi_save, virtio_scsi_load, s);
910 s->migration_state_notifier.notify = virtio_scsi_migration_state_changed;
911 add_migration_state_change_notifier(&s->migration_state_notifier);
913 error_setg(&s->blocker, "block device is in use by data plane");
916 static void virtio_scsi_instance_init(Object *obj)
918 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(obj);
920 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
921 (Object **)&vs->conf.iothread,
922 qdev_prop_allow_set_link_before_realize,
923 OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);
926 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
928 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
929 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
931 g_free(vs->cmd_vqs);
932 virtio_cleanup(vdev);
935 static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp)
937 VirtIOSCSI *s = VIRTIO_SCSI(dev);
939 error_free(s->blocker);
941 unregister_savevm(dev, "virtio-scsi", s);
942 remove_migration_state_change_notifier(&s->migration_state_notifier);
944 virtio_scsi_common_unrealize(dev, errp);
947 static Property virtio_scsi_properties[] = {
948 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI, parent_obj.conf.num_queues, 1),
949 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI, parent_obj.conf.max_sectors,
950 0xFFFF),
951 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI, parent_obj.conf.cmd_per_lun,
952 128),
953 DEFINE_PROP_BIT("hotplug", VirtIOSCSI, host_features,
954 VIRTIO_SCSI_F_HOTPLUG, true),
955 DEFINE_PROP_BIT("param_change", VirtIOSCSI, host_features,
956 VIRTIO_SCSI_F_CHANGE, true),
957 DEFINE_PROP_END_OF_LIST(),
960 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data)
962 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
963 DeviceClass *dc = DEVICE_CLASS(klass);
965 vdc->get_config = virtio_scsi_get_config;
966 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
969 static void virtio_scsi_class_init(ObjectClass *klass, void *data)
971 DeviceClass *dc = DEVICE_CLASS(klass);
972 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
973 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
975 dc->props = virtio_scsi_properties;
976 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
977 vdc->realize = virtio_scsi_device_realize;
978 vdc->unrealize = virtio_scsi_device_unrealize;
979 vdc->set_config = virtio_scsi_set_config;
980 vdc->get_features = virtio_scsi_get_features;
981 vdc->reset = virtio_scsi_reset;
982 hc->plug = virtio_scsi_hotplug;
983 hc->unplug = virtio_scsi_hotunplug;
986 static const TypeInfo virtio_scsi_common_info = {
987 .name = TYPE_VIRTIO_SCSI_COMMON,
988 .parent = TYPE_VIRTIO_DEVICE,
989 .instance_size = sizeof(VirtIOSCSICommon),
990 .abstract = true,
991 .class_init = virtio_scsi_common_class_init,
994 static const TypeInfo virtio_scsi_info = {
995 .name = TYPE_VIRTIO_SCSI,
996 .parent = TYPE_VIRTIO_SCSI_COMMON,
997 .instance_size = sizeof(VirtIOSCSI),
998 .instance_init = virtio_scsi_instance_init,
999 .class_init = virtio_scsi_class_init,
1000 .interfaces = (InterfaceInfo[]) {
1001 { TYPE_HOTPLUG_HANDLER },
1006 static void virtio_register_types(void)
1008 type_register_static(&virtio_scsi_common_info);
1009 type_register_static(&virtio_scsi_info);
1012 type_init(virtio_register_types)