s390-virtio: Rename machine class name to use MACHINE_TYPE_NAME
[qemu/ar7.git] / hw / scsi / virtio-scsi.c
blob1c33f146a7d676de6159b974322fa9fb949f521e
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_slice_alloc(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 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)req->dev;
63 qemu_iovec_destroy(&req->resp_iov);
64 qemu_sglist_destroy(&req->qsgl);
65 g_slice_free1(sizeof(*req) + vs->cdb_size, req);
68 static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
70 VirtIOSCSI *s = req->dev;
71 VirtQueue *vq = req->vq;
72 VirtIODevice *vdev = VIRTIO_DEVICE(s);
74 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size);
75 if (req->vring) {
76 assert(req->vq == NULL);
77 virtio_scsi_vring_push_notify(req);
78 } else {
79 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size);
80 virtio_notify(vdev, vq);
83 if (req->sreq) {
84 req->sreq->hba_private = NULL;
85 scsi_req_unref(req->sreq);
87 virtio_scsi_free_req(req);
90 static void virtio_scsi_bad_req(void)
92 error_report("wrong size for virtio-scsi headers");
93 exit(1);
96 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov,
97 hwaddr *addr, int num, size_t skip)
99 QEMUSGList *qsgl = &req->qsgl;
100 size_t copied = 0;
102 while (num) {
103 if (skip >= iov->iov_len) {
104 skip -= iov->iov_len;
105 } else {
106 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip);
107 copied += iov->iov_len - skip;
108 skip = 0;
110 iov++;
111 addr++;
112 num--;
115 assert(skip == 0);
116 return copied;
119 static int virtio_scsi_parse_req(VirtIOSCSIReq *req,
120 unsigned req_size, unsigned resp_size)
122 VirtIODevice *vdev = (VirtIODevice *) req->dev;
123 size_t in_size, out_size;
125 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
126 &req->req, req_size) < req_size) {
127 return -EINVAL;
130 if (qemu_iovec_concat_iov(&req->resp_iov,
131 req->elem.in_sg, req->elem.in_num, 0,
132 resp_size) < resp_size) {
133 return -EINVAL;
136 req->resp_size = resp_size;
138 /* Old BIOSes left some padding by mistake after the req_size/resp_size.
139 * As a workaround, always consider the first buffer as the virtio-scsi
140 * request/response, making the payload start at the second element
141 * of the iovec.
143 * The actual length of the response header, stored in req->resp_size,
144 * does not change.
146 * TODO: always disable this workaround for virtio 1.0 devices.
148 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) {
149 if (req->elem.out_num) {
150 req_size = req->elem.out_sg[0].iov_len;
152 if (req->elem.in_num) {
153 resp_size = req->elem.in_sg[0].iov_len;
157 out_size = qemu_sgl_concat(req, req->elem.out_sg,
158 &req->elem.out_addr[0], req->elem.out_num,
159 req_size);
160 in_size = qemu_sgl_concat(req, req->elem.in_sg,
161 &req->elem.in_addr[0], req->elem.in_num,
162 resp_size);
164 if (out_size && in_size) {
165 return -ENOTSUP;
168 if (out_size) {
169 req->mode = SCSI_XFER_TO_DEV;
170 } else if (in_size) {
171 req->mode = SCSI_XFER_FROM_DEV;
174 return 0;
177 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
179 VirtIOSCSIReq *req = virtio_scsi_init_req(s, vq);
180 if (!virtqueue_pop(vq, &req->elem)) {
181 virtio_scsi_free_req(req);
182 return NULL;
184 return req;
187 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq)
189 VirtIOSCSIReq *req = sreq->hba_private;
190 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev);
191 uint32_t n = virtio_queue_get_id(req->vq) - 2;
193 assert(n < vs->conf.num_queues);
194 qemu_put_be32s(f, &n);
195 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
198 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
200 SCSIBus *bus = sreq->bus;
201 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
202 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
203 VirtIOSCSIReq *req;
204 uint32_t n;
206 qemu_get_be32s(f, &n);
207 assert(n < vs->conf.num_queues);
208 req = virtio_scsi_init_req(s, vs->cmd_vqs[n]);
209 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
210 /* TODO: add a way for SCSIBusInfo's load_request to fail,
211 * and fail migration instead of asserting here.
212 * When we do, we might be able to re-enable NDEBUG below.
214 #ifdef NDEBUG
215 #error building with NDEBUG is not supported
216 #endif
217 assert(req->elem.in_num <= ARRAY_SIZE(req->elem.in_sg));
218 assert(req->elem.out_num <= ARRAY_SIZE(req->elem.out_sg));
220 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
221 req->elem.in_num, 1);
222 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
223 req->elem.out_num, 0);
225 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
226 sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) {
227 error_report("invalid SCSI request migration data");
228 exit(1);
231 scsi_req_ref(sreq);
232 req->sreq = sreq;
233 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
234 assert(req->sreq->cmd.mode == req->mode);
236 return req;
239 typedef struct {
240 Notifier notifier;
241 VirtIOSCSIReq *tmf_req;
242 } VirtIOSCSICancelNotifier;
244 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data)
246 VirtIOSCSICancelNotifier *n = container_of(notifier,
247 VirtIOSCSICancelNotifier,
248 notifier);
250 if (--n->tmf_req->remaining == 0) {
251 virtio_scsi_complete_req(n->tmf_req);
253 g_slice_free(VirtIOSCSICancelNotifier, n);
256 /* Return 0 if the request is ready to be completed and return to guest;
257 * -EINPROGRESS if the request is submitted and will be completed later, in the
258 * case of async cancellation. */
259 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
261 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun);
262 SCSIRequest *r, *next;
263 BusChild *kid;
264 int target;
265 int ret = 0;
267 if (s->dataplane_started) {
268 assert(blk_get_aio_context(d->conf.blk) == s->ctx);
270 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
271 req->resp.tmf.response = VIRTIO_SCSI_S_OK;
273 virtio_tswap32s(VIRTIO_DEVICE(s), &req->req.tmf.subtype);
274 switch (req->req.tmf.subtype) {
275 case VIRTIO_SCSI_T_TMF_ABORT_TASK:
276 case VIRTIO_SCSI_T_TMF_QUERY_TASK:
277 if (!d) {
278 goto fail;
280 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
281 goto incorrect_lun;
283 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
284 VirtIOSCSIReq *cmd_req = r->hba_private;
285 if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) {
286 break;
289 if (r) {
291 * Assert that the request has not been completed yet, we
292 * check for it in the loop above.
294 assert(r->hba_private);
295 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) {
296 /* "If the specified command is present in the task set, then
297 * return a service response set to FUNCTION SUCCEEDED".
299 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
300 } else {
301 VirtIOSCSICancelNotifier *notifier;
303 req->remaining = 1;
304 notifier = g_slice_new(VirtIOSCSICancelNotifier);
305 notifier->tmf_req = req;
306 notifier->notifier.notify = virtio_scsi_cancel_notify;
307 scsi_req_cancel_async(r, &notifier->notifier);
308 ret = -EINPROGRESS;
311 break;
313 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
314 if (!d) {
315 goto fail;
317 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
318 goto incorrect_lun;
320 s->resetting++;
321 qdev_reset_all(&d->qdev);
322 s->resetting--;
323 break;
325 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
326 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
327 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
328 if (!d) {
329 goto fail;
331 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
332 goto incorrect_lun;
335 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
336 * This way, if the bus starts calling back to the notifiers
337 * even before we finish the loop, virtio_scsi_cancel_notify
338 * will not complete the TMF too early.
340 req->remaining = 1;
341 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
342 if (r->hba_private) {
343 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) {
344 /* "If there is any command present in the task set, then
345 * return a service response set to FUNCTION SUCCEEDED".
347 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
348 break;
349 } else {
350 VirtIOSCSICancelNotifier *notifier;
352 req->remaining++;
353 notifier = g_slice_new(VirtIOSCSICancelNotifier);
354 notifier->notifier.notify = virtio_scsi_cancel_notify;
355 notifier->tmf_req = req;
356 scsi_req_cancel_async(r, &notifier->notifier);
360 if (--req->remaining > 0) {
361 ret = -EINPROGRESS;
363 break;
365 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
366 target = req->req.tmf.lun[1];
367 s->resetting++;
368 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
369 d = DO_UPCAST(SCSIDevice, qdev, kid->child);
370 if (d->channel == 0 && d->id == target) {
371 qdev_reset_all(&d->qdev);
374 s->resetting--;
375 break;
377 case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
378 default:
379 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
380 break;
383 return ret;
385 incorrect_lun:
386 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
387 return ret;
389 fail:
390 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
391 return ret;
394 void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
396 VirtIODevice *vdev = (VirtIODevice *)s;
397 uint32_t type;
398 int r = 0;
400 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
401 &type, sizeof(type)) < sizeof(type)) {
402 virtio_scsi_bad_req();
403 return;
406 virtio_tswap32s(vdev, &type);
407 if (type == VIRTIO_SCSI_T_TMF) {
408 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq),
409 sizeof(VirtIOSCSICtrlTMFResp)) < 0) {
410 virtio_scsi_bad_req();
411 } else {
412 r = virtio_scsi_do_tmf(s, req);
415 } else if (type == VIRTIO_SCSI_T_AN_QUERY ||
416 type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
417 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq),
418 sizeof(VirtIOSCSICtrlANResp)) < 0) {
419 virtio_scsi_bad_req();
420 } else {
421 req->resp.an.event_actual = 0;
422 req->resp.an.response = VIRTIO_SCSI_S_OK;
425 if (r == 0) {
426 virtio_scsi_complete_req(req);
427 } else {
428 assert(r == -EINPROGRESS);
432 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
434 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
435 VirtIOSCSIReq *req;
437 if (s->ctx && !s->dataplane_disabled) {
438 virtio_scsi_dataplane_start(s);
439 return;
441 while ((req = virtio_scsi_pop_req(s, vq))) {
442 virtio_scsi_handle_ctrl_req(s, req);
446 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req)
448 /* Sense data is not in req->resp and is copied separately
449 * in virtio_scsi_command_complete.
451 req->resp_size = sizeof(VirtIOSCSICmdResp);
452 virtio_scsi_complete_req(req);
455 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status,
456 size_t resid)
458 VirtIOSCSIReq *req = r->hba_private;
459 uint8_t sense[SCSI_SENSE_BUF_SIZE];
460 uint32_t sense_len;
461 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
463 if (r->io_canceled) {
464 return;
467 req->resp.cmd.response = VIRTIO_SCSI_S_OK;
468 req->resp.cmd.status = status;
469 if (req->resp.cmd.status == GOOD) {
470 req->resp.cmd.resid = virtio_tswap32(vdev, resid);
471 } else {
472 req->resp.cmd.resid = 0;
473 sense_len = scsi_req_get_sense(r, sense, sizeof(sense));
474 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd));
475 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
476 sense, sense_len);
477 req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
479 virtio_scsi_complete_cmd_req(req);
482 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd,
483 uint8_t *buf, void *hba_private)
485 VirtIOSCSIReq *req = hba_private;
487 if (cmd->len == 0) {
488 cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE);
489 memcpy(cmd->buf, buf, cmd->len);
492 /* Extract the direction and mode directly from the request, for
493 * host device passthrough.
495 cmd->xfer = req->qsgl.size;
496 cmd->mode = req->mode;
497 return 0;
500 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
502 VirtIOSCSIReq *req = r->hba_private;
504 return &req->qsgl;
507 static void virtio_scsi_request_cancelled(SCSIRequest *r)
509 VirtIOSCSIReq *req = r->hba_private;
511 if (!req) {
512 return;
514 if (req->dev->resetting) {
515 req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
516 } else {
517 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
519 virtio_scsi_complete_cmd_req(req);
522 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
524 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
525 virtio_scsi_complete_cmd_req(req);
528 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
530 VirtIOSCSICommon *vs = &s->parent_obj;
531 SCSIDevice *d;
532 int rc;
534 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
535 sizeof(VirtIOSCSICmdResp) + vs->sense_size);
536 if (rc < 0) {
537 if (rc == -ENOTSUP) {
538 virtio_scsi_fail_cmd_req(req);
539 } else {
540 virtio_scsi_bad_req();
542 return false;
545 d = virtio_scsi_device_find(s, req->req.cmd.lun);
546 if (!d) {
547 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
548 virtio_scsi_complete_cmd_req(req);
549 return false;
551 if (s->dataplane_started) {
552 assert(blk_get_aio_context(d->conf.blk) == s->ctx);
554 req->sreq = scsi_req_new(d, req->req.cmd.tag,
555 virtio_scsi_get_lun(req->req.cmd.lun),
556 req->req.cmd.cdb, req);
558 if (req->sreq->cmd.mode != SCSI_XFER_NONE
559 && (req->sreq->cmd.mode != req->mode ||
560 req->sreq->cmd.xfer > req->qsgl.size)) {
561 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN;
562 virtio_scsi_complete_cmd_req(req);
563 return false;
565 scsi_req_ref(req->sreq);
566 blk_io_plug(d->conf.blk);
567 return true;
570 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req)
572 SCSIRequest *sreq = req->sreq;
573 if (scsi_req_enqueue(sreq)) {
574 scsi_req_continue(sreq);
576 blk_io_unplug(sreq->dev->conf.blk);
577 scsi_req_unref(sreq);
580 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
582 /* use non-QOM casts in the data path */
583 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
584 VirtIOSCSIReq *req, *next;
585 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
587 if (s->ctx && !s->dataplane_disabled) {
588 virtio_scsi_dataplane_start(s);
589 return;
591 while ((req = virtio_scsi_pop_req(s, vq))) {
592 if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
593 QTAILQ_INSERT_TAIL(&reqs, req, next);
597 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
598 virtio_scsi_handle_cmd_req_submit(s, req);
602 static void virtio_scsi_get_config(VirtIODevice *vdev,
603 uint8_t *config)
605 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
606 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
608 virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
609 virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2);
610 virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors);
611 virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun);
612 virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
613 virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size);
614 virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size);
615 virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
616 virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
617 virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
620 static void virtio_scsi_set_config(VirtIODevice *vdev,
621 const uint8_t *config)
623 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
624 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
626 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 ||
627 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) {
628 error_report("bad data written to virtio-scsi configuration space");
629 exit(1);
632 vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size);
633 vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size);
636 static uint64_t virtio_scsi_get_features(VirtIODevice *vdev,
637 uint64_t requested_features,
638 Error **errp)
640 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
642 /* Firstly sync all virtio-scsi possible supported features */
643 requested_features |= s->host_features;
644 return requested_features;
647 static void virtio_scsi_reset(VirtIODevice *vdev)
649 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
650 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
652 if (s->ctx) {
653 virtio_scsi_dataplane_stop(s);
655 s->resetting++;
656 qbus_reset_all(&s->bus.qbus);
657 s->resetting--;
659 vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
660 vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
661 s->events_dropped = false;
664 /* The device does not have anything to save beyond the virtio data.
665 * Request data is saved with callbacks from SCSI devices.
667 static void virtio_scsi_save(QEMUFile *f, void *opaque)
669 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
670 virtio_save(vdev, f);
673 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id)
675 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
676 int ret;
678 ret = virtio_load(vdev, f, version_id);
679 if (ret) {
680 return ret;
682 return 0;
685 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev,
686 uint32_t event, uint32_t reason)
688 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
689 VirtIOSCSIReq *req;
690 VirtIOSCSIEvent *evt;
691 VirtIODevice *vdev = VIRTIO_DEVICE(s);
693 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
694 return;
697 if (s->dataplane_started) {
698 assert(s->ctx);
699 aio_context_acquire(s->ctx);
702 if (s->dataplane_started) {
703 req = virtio_scsi_pop_req_vring(s, s->event_vring);
704 } else {
705 req = virtio_scsi_pop_req(s, vs->event_vq);
707 if (!req) {
708 s->events_dropped = true;
709 goto out;
712 if (s->events_dropped) {
713 event |= VIRTIO_SCSI_T_EVENTS_MISSED;
714 s->events_dropped = false;
717 if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) {
718 virtio_scsi_bad_req();
721 evt = &req->resp.event;
722 memset(evt, 0, sizeof(VirtIOSCSIEvent));
723 evt->event = virtio_tswap32(vdev, event);
724 evt->reason = virtio_tswap32(vdev, reason);
725 if (!dev) {
726 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED);
727 } else {
728 evt->lun[0] = 1;
729 evt->lun[1] = dev->id;
731 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
732 if (dev->lun >= 256) {
733 evt->lun[2] = (dev->lun >> 8) | 0x40;
735 evt->lun[3] = dev->lun & 0xFF;
737 virtio_scsi_complete_req(req);
738 out:
739 if (s->dataplane_started) {
740 aio_context_release(s->ctx);
744 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
746 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
748 if (s->ctx && !s->dataplane_disabled) {
749 virtio_scsi_dataplane_start(s);
750 return;
752 if (s->events_dropped) {
753 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
757 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
759 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
760 VirtIODevice *vdev = VIRTIO_DEVICE(s);
762 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) &&
763 dev->type != TYPE_ROM) {
764 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE,
765 sense.asc | (sense.ascq << 8));
769 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
770 Error **errp)
772 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
773 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
774 SCSIDevice *sd = SCSI_DEVICE(dev);
776 if (s->ctx && !s->dataplane_disabled) {
777 if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
778 return;
780 blk_op_block_all(sd->conf.blk, s->blocker);
781 aio_context_acquire(s->ctx);
782 blk_set_aio_context(sd->conf.blk, s->ctx);
783 aio_context_release(s->ctx);
786 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
787 virtio_scsi_push_event(s, sd,
788 VIRTIO_SCSI_T_TRANSPORT_RESET,
789 VIRTIO_SCSI_EVT_RESET_RESCAN);
793 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
794 Error **errp)
796 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
797 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
798 SCSIDevice *sd = SCSI_DEVICE(dev);
800 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
801 virtio_scsi_push_event(s, sd,
802 VIRTIO_SCSI_T_TRANSPORT_RESET,
803 VIRTIO_SCSI_EVT_RESET_REMOVED);
806 if (s->ctx) {
807 blk_op_unblock_all(sd->conf.blk, s->blocker);
809 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
812 static struct SCSIBusInfo virtio_scsi_scsi_info = {
813 .tcq = true,
814 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
815 .max_target = VIRTIO_SCSI_MAX_TARGET,
816 .max_lun = VIRTIO_SCSI_MAX_LUN,
818 .complete = virtio_scsi_command_complete,
819 .cancel = virtio_scsi_request_cancelled,
820 .change = virtio_scsi_change,
821 .parse_cdb = virtio_scsi_parse_cdb,
822 .get_sg_list = virtio_scsi_get_sg_list,
823 .save_request = virtio_scsi_save_request,
824 .load_request = virtio_scsi_load_request,
827 void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
828 HandleOutput ctrl, HandleOutput evt,
829 HandleOutput cmd)
831 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
832 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
833 int i;
835 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI,
836 sizeof(VirtIOSCSIConfig));
838 if (s->conf.num_queues == 0 ||
839 s->conf.num_queues > VIRTIO_QUEUE_MAX - 2) {
840 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
841 "must be a positive integer less than %d.",
842 s->conf.num_queues, VIRTIO_QUEUE_MAX - 2);
843 virtio_cleanup(vdev);
844 return;
846 s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues);
847 s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
848 s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
850 s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
851 ctrl);
852 s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
853 evt);
854 for (i = 0; i < s->conf.num_queues; i++) {
855 s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
856 cmd);
859 if (s->conf.iothread) {
860 virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
864 /* Disable dataplane thread during live migration since it does not
865 * update the dirty memory bitmap yet.
867 static void virtio_scsi_migration_state_changed(Notifier *notifier, void *data)
869 VirtIOSCSI *s = container_of(notifier, VirtIOSCSI,
870 migration_state_notifier);
871 MigrationState *mig = data;
873 if (migration_in_setup(mig)) {
874 if (!s->dataplane_started) {
875 return;
877 virtio_scsi_dataplane_stop(s);
878 s->dataplane_disabled = true;
879 } else if (migration_has_finished(mig) ||
880 migration_has_failed(mig)) {
881 if (s->dataplane_started) {
882 return;
884 blk_drain_all(); /* complete in-flight non-dataplane requests */
885 s->dataplane_disabled = false;
889 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp)
891 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
892 VirtIOSCSI *s = VIRTIO_SCSI(dev);
893 static int virtio_scsi_id;
894 Error *err = NULL;
896 virtio_scsi_common_realize(dev, &err, virtio_scsi_handle_ctrl,
897 virtio_scsi_handle_event,
898 virtio_scsi_handle_cmd);
899 if (err != NULL) {
900 error_propagate(errp, err);
901 return;
904 scsi_bus_new(&s->bus, sizeof(s->bus), dev,
905 &virtio_scsi_scsi_info, vdev->bus_name);
906 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
907 qbus_set_hotplug_handler(BUS(&s->bus), dev, &error_abort);
909 if (!dev->hotplugged) {
910 scsi_bus_legacy_handle_cmdline(&s->bus, &err);
911 if (err != NULL) {
912 error_propagate(errp, err);
913 return;
917 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1,
918 virtio_scsi_save, virtio_scsi_load, s);
919 s->migration_state_notifier.notify = virtio_scsi_migration_state_changed;
920 add_migration_state_change_notifier(&s->migration_state_notifier);
922 error_setg(&s->blocker, "block device is in use by data plane");
925 static void virtio_scsi_instance_init(Object *obj)
927 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(obj);
929 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
930 (Object **)&vs->conf.iothread,
931 qdev_prop_allow_set_link_before_realize,
932 OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);
935 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
937 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
938 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
940 g_free(vs->cmd_vqs);
941 virtio_cleanup(vdev);
944 static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp)
946 VirtIOSCSI *s = VIRTIO_SCSI(dev);
948 error_free(s->blocker);
950 unregister_savevm(dev, "virtio-scsi", s);
951 remove_migration_state_change_notifier(&s->migration_state_notifier);
953 virtio_scsi_common_unrealize(dev, errp);
956 static Property virtio_scsi_properties[] = {
957 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI, parent_obj.conf.num_queues, 1),
958 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI, parent_obj.conf.max_sectors,
959 0xFFFF),
960 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI, parent_obj.conf.cmd_per_lun,
961 128),
962 DEFINE_PROP_BIT("hotplug", VirtIOSCSI, host_features,
963 VIRTIO_SCSI_F_HOTPLUG, true),
964 DEFINE_PROP_BIT("param_change", VirtIOSCSI, host_features,
965 VIRTIO_SCSI_F_CHANGE, true),
966 DEFINE_PROP_END_OF_LIST(),
969 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data)
971 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
972 DeviceClass *dc = DEVICE_CLASS(klass);
974 vdc->get_config = virtio_scsi_get_config;
975 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
978 static void virtio_scsi_class_init(ObjectClass *klass, void *data)
980 DeviceClass *dc = DEVICE_CLASS(klass);
981 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
982 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
984 dc->props = virtio_scsi_properties;
985 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
986 vdc->realize = virtio_scsi_device_realize;
987 vdc->unrealize = virtio_scsi_device_unrealize;
988 vdc->set_config = virtio_scsi_set_config;
989 vdc->get_features = virtio_scsi_get_features;
990 vdc->reset = virtio_scsi_reset;
991 hc->plug = virtio_scsi_hotplug;
992 hc->unplug = virtio_scsi_hotunplug;
995 static const TypeInfo virtio_scsi_common_info = {
996 .name = TYPE_VIRTIO_SCSI_COMMON,
997 .parent = TYPE_VIRTIO_DEVICE,
998 .instance_size = sizeof(VirtIOSCSICommon),
999 .abstract = true,
1000 .class_init = virtio_scsi_common_class_init,
1003 static const TypeInfo virtio_scsi_info = {
1004 .name = TYPE_VIRTIO_SCSI,
1005 .parent = TYPE_VIRTIO_SCSI_COMMON,
1006 .instance_size = sizeof(VirtIOSCSI),
1007 .instance_init = virtio_scsi_instance_init,
1008 .class_init = virtio_scsi_class_init,
1009 .interfaces = (InterfaceInfo[]) {
1010 { TYPE_HOTPLUG_HANDLER },
1015 static void virtio_register_types(void)
1017 type_register_static(&virtio_scsi_common_info);
1018 type_register_static(&virtio_scsi_info);
1021 type_init(virtio_register_types)