hw: Convert from BlockDriverState to BlockBackend, mostly
[qemu-kvm.git] / hw / scsi / virtio-scsi.c
bloba1725b8f48902f99553154932aebff85fba70861
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 "hw/virtio/virtio-scsi.h"
17 #include "qemu/error-report.h"
18 #include "qemu/iov.h"
19 #include "sysemu/block-backend.h"
20 #include <hw/scsi/scsi.h>
21 #include <block/scsi.h>
22 #include <hw/virtio/virtio-bus.h>
23 #include "hw/virtio/virtio-access.h"
24 #include "migration/migration.h"
26 static inline int virtio_scsi_get_lun(uint8_t *lun)
28 return ((lun[2] << 8) | lun[3]) & 0x3FFF;
31 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun)
33 if (lun[0] != 1) {
34 return NULL;
36 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
37 return NULL;
39 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
42 VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq)
44 VirtIOSCSIReq *req;
45 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s;
46 const size_t zero_skip = offsetof(VirtIOSCSIReq, elem)
47 + sizeof(VirtQueueElement);
49 req = g_slice_alloc(sizeof(*req) + vs->cdb_size);
50 req->vq = vq;
51 req->dev = s;
52 qemu_sglist_init(&req->qsgl, DEVICE(s), 8, &address_space_memory);
53 qemu_iovec_init(&req->resp_iov, 1);
54 memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip);
55 return req;
58 void virtio_scsi_free_req(VirtIOSCSIReq *req)
60 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)req->dev;
62 qemu_iovec_destroy(&req->resp_iov);
63 qemu_sglist_destroy(&req->qsgl);
64 g_slice_free1(sizeof(*req) + vs->cdb_size, req);
67 static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
69 VirtIOSCSI *s = req->dev;
70 VirtQueue *vq = req->vq;
71 VirtIODevice *vdev = VIRTIO_DEVICE(s);
73 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size);
74 if (req->vring) {
75 assert(req->vq == NULL);
76 virtio_scsi_vring_push_notify(req);
77 } else {
78 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size);
79 virtio_notify(vdev, vq);
82 if (req->sreq) {
83 req->sreq->hba_private = NULL;
84 scsi_req_unref(req->sreq);
86 virtio_scsi_free_req(req);
89 static void virtio_scsi_bad_req(void)
91 error_report("wrong size for virtio-scsi headers");
92 exit(1);
95 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov,
96 hwaddr *addr, int num, size_t skip)
98 QEMUSGList *qsgl = &req->qsgl;
99 size_t copied = 0;
101 while (num) {
102 if (skip >= iov->iov_len) {
103 skip -= iov->iov_len;
104 } else {
105 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip);
106 copied += iov->iov_len - skip;
107 skip = 0;
109 iov++;
110 addr++;
111 num--;
114 assert(skip == 0);
115 return copied;
118 static int virtio_scsi_parse_req(VirtIOSCSIReq *req,
119 unsigned req_size, unsigned resp_size)
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;
133 req->resp_size = resp_size;
135 out_size = qemu_sgl_concat(req, req->elem.out_sg,
136 &req->elem.out_addr[0], req->elem.out_num,
137 req_size);
138 in_size = qemu_sgl_concat(req, req->elem.in_sg,
139 &req->elem.in_addr[0], req->elem.in_num,
140 resp_size);
142 if (out_size && in_size) {
143 return -ENOTSUP;
146 if (out_size) {
147 req->mode = SCSI_XFER_TO_DEV;
148 } else if (in_size) {
149 req->mode = SCSI_XFER_FROM_DEV;
152 return 0;
155 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
157 VirtIOSCSIReq *req = virtio_scsi_init_req(s, vq);
158 if (!virtqueue_pop(vq, &req->elem)) {
159 virtio_scsi_free_req(req);
160 return NULL;
162 return req;
165 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq)
167 VirtIOSCSIReq *req = sreq->hba_private;
168 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev);
169 uint32_t n = virtio_queue_get_id(req->vq) - 2;
171 assert(n < vs->conf.num_queues);
172 qemu_put_be32s(f, &n);
173 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
176 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
178 SCSIBus *bus = sreq->bus;
179 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
180 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
181 VirtIOSCSIReq *req;
182 uint32_t n;
184 qemu_get_be32s(f, &n);
185 assert(n < vs->conf.num_queues);
186 req = virtio_scsi_init_req(s, vs->cmd_vqs[n]);
187 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
188 /* TODO: add a way for SCSIBusInfo's load_request to fail,
189 * and fail migration instead of asserting here.
190 * When we do, we might be able to re-enable NDEBUG below.
192 #ifdef NDEBUG
193 #error building with NDEBUG is not supported
194 #endif
195 assert(req->elem.in_num <= ARRAY_SIZE(req->elem.in_sg));
196 assert(req->elem.out_num <= ARRAY_SIZE(req->elem.out_sg));
198 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
199 sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) {
200 error_report("invalid SCSI request migration data");
201 exit(1);
204 scsi_req_ref(sreq);
205 req->sreq = sreq;
206 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
207 assert(req->sreq->cmd.mode == req->mode);
209 return req;
212 typedef struct {
213 Notifier notifier;
214 VirtIOSCSIReq *tmf_req;
215 } VirtIOSCSICancelNotifier;
217 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data)
219 VirtIOSCSICancelNotifier *n = container_of(notifier,
220 VirtIOSCSICancelNotifier,
221 notifier);
223 if (--n->tmf_req->remaining == 0) {
224 virtio_scsi_complete_req(n->tmf_req);
226 g_slice_free(VirtIOSCSICancelNotifier, n);
229 /* Return 0 if the request is ready to be completed and return to guest;
230 * -EINPROGRESS if the request is submitted and will be completed later, in the
231 * case of async cancellation. */
232 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
234 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun);
235 SCSIRequest *r, *next;
236 BusChild *kid;
237 int target;
238 int ret = 0;
240 if (s->dataplane_started && blk_get_aio_context(d->conf.blk) != s->ctx) {
241 aio_context_acquire(s->ctx);
242 blk_set_aio_context(d->conf.blk, s->ctx);
243 aio_context_release(s->ctx);
245 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
246 req->resp.tmf.response = VIRTIO_SCSI_S_OK;
248 virtio_tswap32s(VIRTIO_DEVICE(s), &req->req.tmf.subtype);
249 switch (req->req.tmf.subtype) {
250 case VIRTIO_SCSI_T_TMF_ABORT_TASK:
251 case VIRTIO_SCSI_T_TMF_QUERY_TASK:
252 if (!d) {
253 goto fail;
255 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
256 goto incorrect_lun;
258 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
259 VirtIOSCSIReq *cmd_req = r->hba_private;
260 if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) {
261 break;
264 if (r) {
266 * Assert that the request has not been completed yet, we
267 * check for it in the loop above.
269 assert(r->hba_private);
270 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) {
271 /* "If the specified command is present in the task set, then
272 * return a service response set to FUNCTION SUCCEEDED".
274 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
275 } else {
276 VirtIOSCSICancelNotifier *notifier;
278 req->remaining = 1;
279 notifier = g_slice_new(VirtIOSCSICancelNotifier);
280 notifier->tmf_req = req;
281 notifier->notifier.notify = virtio_scsi_cancel_notify;
282 scsi_req_cancel_async(r, &notifier->notifier);
283 ret = -EINPROGRESS;
286 break;
288 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
289 if (!d) {
290 goto fail;
292 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
293 goto incorrect_lun;
295 s->resetting++;
296 qdev_reset_all(&d->qdev);
297 s->resetting--;
298 break;
300 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
301 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
302 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
303 if (!d) {
304 goto fail;
306 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
307 goto incorrect_lun;
310 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
311 * This way, if the bus starts calling back to the notifiers
312 * even before we finish the loop, virtio_scsi_cancel_notify
313 * will not complete the TMF too early.
315 req->remaining = 1;
316 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
317 if (r->hba_private) {
318 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) {
319 /* "If there is any command present in the task set, then
320 * return a service response set to FUNCTION SUCCEEDED".
322 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
323 break;
324 } else {
325 VirtIOSCSICancelNotifier *notifier;
327 req->remaining++;
328 notifier = g_slice_new(VirtIOSCSICancelNotifier);
329 notifier->notifier.notify = virtio_scsi_cancel_notify;
330 notifier->tmf_req = req;
331 scsi_req_cancel_async(r, &notifier->notifier);
335 if (--req->remaining > 0) {
336 ret = -EINPROGRESS;
338 break;
340 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
341 target = req->req.tmf.lun[1];
342 s->resetting++;
343 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
344 d = DO_UPCAST(SCSIDevice, qdev, kid->child);
345 if (d->channel == 0 && d->id == target) {
346 qdev_reset_all(&d->qdev);
349 s->resetting--;
350 break;
352 case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
353 default:
354 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
355 break;
358 return ret;
360 incorrect_lun:
361 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
362 return ret;
364 fail:
365 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
366 return ret;
369 void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
371 VirtIODevice *vdev = (VirtIODevice *)s;
372 int type;
373 int r = 0;
375 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
376 &type, sizeof(type)) < sizeof(type)) {
377 virtio_scsi_bad_req();
378 return;
381 virtio_tswap32s(vdev, &req->req.tmf.type);
382 if (req->req.tmf.type == VIRTIO_SCSI_T_TMF) {
383 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq),
384 sizeof(VirtIOSCSICtrlTMFResp)) < 0) {
385 virtio_scsi_bad_req();
386 } else {
387 r = virtio_scsi_do_tmf(s, req);
390 } else if (req->req.tmf.type == VIRTIO_SCSI_T_AN_QUERY ||
391 req->req.tmf.type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
392 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq),
393 sizeof(VirtIOSCSICtrlANResp)) < 0) {
394 virtio_scsi_bad_req();
395 } else {
396 req->resp.an.event_actual = 0;
397 req->resp.an.response = VIRTIO_SCSI_S_OK;
400 if (r == 0) {
401 virtio_scsi_complete_req(req);
402 } else {
403 assert(r == -EINPROGRESS);
407 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
409 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
410 VirtIOSCSIReq *req;
412 if (s->ctx && !s->dataplane_disabled) {
413 virtio_scsi_dataplane_start(s);
414 return;
416 while ((req = virtio_scsi_pop_req(s, vq))) {
417 virtio_scsi_handle_ctrl_req(s, req);
421 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req)
423 /* Sense data is not in req->resp and is copied separately
424 * in virtio_scsi_command_complete.
426 req->resp_size = sizeof(VirtIOSCSICmdResp);
427 virtio_scsi_complete_req(req);
430 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status,
431 size_t resid)
433 VirtIOSCSIReq *req = r->hba_private;
434 uint8_t sense[SCSI_SENSE_BUF_SIZE];
435 uint32_t sense_len;
436 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
438 if (r->io_canceled) {
439 return;
442 req->resp.cmd.response = VIRTIO_SCSI_S_OK;
443 req->resp.cmd.status = status;
444 if (req->resp.cmd.status == GOOD) {
445 req->resp.cmd.resid = virtio_tswap32(vdev, resid);
446 } else {
447 req->resp.cmd.resid = 0;
448 sense_len = scsi_req_get_sense(r, sense, sizeof(sense));
449 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd));
450 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
451 &req->resp, sense_len);
452 req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
454 virtio_scsi_complete_cmd_req(req);
457 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd,
458 uint8_t *buf, void *hba_private)
460 VirtIOSCSIReq *req = hba_private;
462 if (cmd->len == 0) {
463 cmd->len = MIN(VIRTIO_SCSI_CDB_SIZE, SCSI_CMD_BUF_SIZE);
464 memcpy(cmd->buf, buf, cmd->len);
467 /* Extract the direction and mode directly from the request, for
468 * host device passthrough.
470 cmd->xfer = req->qsgl.size;
471 cmd->mode = req->mode;
472 return 0;
475 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
477 VirtIOSCSIReq *req = r->hba_private;
479 return &req->qsgl;
482 static void virtio_scsi_request_cancelled(SCSIRequest *r)
484 VirtIOSCSIReq *req = r->hba_private;
486 if (!req) {
487 return;
489 if (req->dev->resetting) {
490 req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
491 } else {
492 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
494 virtio_scsi_complete_cmd_req(req);
497 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
499 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
500 virtio_scsi_complete_cmd_req(req);
503 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
505 VirtIOSCSICommon *vs = &s->parent_obj;
506 SCSIDevice *d;
507 int rc;
509 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
510 sizeof(VirtIOSCSICmdResp) + vs->sense_size);
511 if (rc < 0) {
512 if (rc == -ENOTSUP) {
513 virtio_scsi_fail_cmd_req(req);
514 } else {
515 virtio_scsi_bad_req();
517 return false;
520 d = virtio_scsi_device_find(s, req->req.cmd.lun);
521 if (!d) {
522 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
523 virtio_scsi_complete_cmd_req(req);
524 return false;
526 if (s->dataplane_started && blk_get_aio_context(d->conf.blk) != s->ctx) {
527 aio_context_acquire(s->ctx);
528 blk_set_aio_context(d->conf.blk, s->ctx);
529 aio_context_release(s->ctx);
531 req->sreq = scsi_req_new(d, req->req.cmd.tag,
532 virtio_scsi_get_lun(req->req.cmd.lun),
533 req->req.cdb, req);
535 if (req->sreq->cmd.mode != SCSI_XFER_NONE
536 && (req->sreq->cmd.mode != req->mode ||
537 req->sreq->cmd.xfer > req->qsgl.size)) {
538 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN;
539 virtio_scsi_complete_cmd_req(req);
540 return false;
542 scsi_req_ref(req->sreq);
543 blk_io_plug(d->conf.blk);
544 return true;
547 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req)
549 SCSIRequest *sreq = req->sreq;
550 if (scsi_req_enqueue(sreq)) {
551 scsi_req_continue(sreq);
553 blk_io_unplug(sreq->dev->conf.blk);
554 scsi_req_unref(sreq);
557 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
559 /* use non-QOM casts in the data path */
560 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
561 VirtIOSCSIReq *req, *next;
562 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
564 if (s->ctx && !s->dataplane_disabled) {
565 virtio_scsi_dataplane_start(s);
566 return;
568 while ((req = virtio_scsi_pop_req(s, vq))) {
569 if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
570 QTAILQ_INSERT_TAIL(&reqs, req, next);
574 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
575 virtio_scsi_handle_cmd_req_submit(s, req);
579 static void virtio_scsi_get_config(VirtIODevice *vdev,
580 uint8_t *config)
582 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
583 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
585 virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
586 virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2);
587 virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors);
588 virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun);
589 virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
590 virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size);
591 virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size);
592 virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
593 virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
594 virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
597 static void virtio_scsi_set_config(VirtIODevice *vdev,
598 const uint8_t *config)
600 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
601 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
603 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 ||
604 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) {
605 error_report("bad data written to virtio-scsi configuration space");
606 exit(1);
609 vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size);
610 vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size);
613 static uint32_t virtio_scsi_get_features(VirtIODevice *vdev,
614 uint32_t requested_features)
616 return requested_features;
619 static void virtio_scsi_reset(VirtIODevice *vdev)
621 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
622 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
624 if (s->ctx) {
625 virtio_scsi_dataplane_stop(s);
627 s->resetting++;
628 qbus_reset_all(&s->bus.qbus);
629 s->resetting--;
631 vs->sense_size = VIRTIO_SCSI_SENSE_SIZE;
632 vs->cdb_size = VIRTIO_SCSI_CDB_SIZE;
633 s->events_dropped = false;
636 /* The device does not have anything to save beyond the virtio data.
637 * Request data is saved with callbacks from SCSI devices.
639 static void virtio_scsi_save(QEMUFile *f, void *opaque)
641 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
642 virtio_save(vdev, f);
645 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id)
647 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
648 int ret;
650 ret = virtio_load(vdev, f, version_id);
651 if (ret) {
652 return ret;
654 return 0;
657 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev,
658 uint32_t event, uint32_t reason)
660 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
661 VirtIOSCSIReq *req;
662 VirtIOSCSIEvent *evt;
663 VirtIODevice *vdev = VIRTIO_DEVICE(s);
665 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
666 return;
669 if (s->dataplane_started) {
670 assert(s->ctx);
671 aio_context_acquire(s->ctx);
674 if (s->dataplane_started) {
675 req = virtio_scsi_pop_req_vring(s, s->event_vring);
676 } else {
677 req = virtio_scsi_pop_req(s, vs->event_vq);
679 if (!req) {
680 s->events_dropped = true;
681 goto out;
684 if (s->events_dropped) {
685 event |= VIRTIO_SCSI_T_EVENTS_MISSED;
686 s->events_dropped = false;
689 if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) {
690 virtio_scsi_bad_req();
693 evt = &req->resp.event;
694 memset(evt, 0, sizeof(VirtIOSCSIEvent));
695 evt->event = virtio_tswap32(vdev, event);
696 evt->reason = virtio_tswap32(vdev, reason);
697 if (!dev) {
698 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED);
699 } else {
700 evt->lun[0] = 1;
701 evt->lun[1] = dev->id;
703 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
704 if (dev->lun >= 256) {
705 evt->lun[2] = (dev->lun >> 8) | 0x40;
707 evt->lun[3] = dev->lun & 0xFF;
709 virtio_scsi_complete_req(req);
710 out:
711 if (s->dataplane_started) {
712 aio_context_release(s->ctx);
716 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
718 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
720 if (s->ctx && !s->dataplane_disabled) {
721 virtio_scsi_dataplane_start(s);
722 return;
724 if (s->events_dropped) {
725 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
729 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
731 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
732 VirtIODevice *vdev = VIRTIO_DEVICE(s);
734 if (((vdev->guest_features >> VIRTIO_SCSI_F_CHANGE) & 1) &&
735 dev->type != TYPE_ROM) {
736 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE,
737 sense.asc | (sense.ascq << 8));
741 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
742 Error **errp)
744 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
746 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) {
747 virtio_scsi_push_event(VIRTIO_SCSI(hotplug_dev), SCSI_DEVICE(dev),
748 VIRTIO_SCSI_T_TRANSPORT_RESET,
749 VIRTIO_SCSI_EVT_RESET_RESCAN);
753 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
754 Error **errp)
756 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
758 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) {
759 virtio_scsi_push_event(VIRTIO_SCSI(hotplug_dev), SCSI_DEVICE(dev),
760 VIRTIO_SCSI_T_TRANSPORT_RESET,
761 VIRTIO_SCSI_EVT_RESET_REMOVED);
763 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
766 static struct SCSIBusInfo virtio_scsi_scsi_info = {
767 .tcq = true,
768 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
769 .max_target = VIRTIO_SCSI_MAX_TARGET,
770 .max_lun = VIRTIO_SCSI_MAX_LUN,
772 .complete = virtio_scsi_command_complete,
773 .cancel = virtio_scsi_request_cancelled,
774 .change = virtio_scsi_change,
775 .parse_cdb = virtio_scsi_parse_cdb,
776 .get_sg_list = virtio_scsi_get_sg_list,
777 .save_request = virtio_scsi_save_request,
778 .load_request = virtio_scsi_load_request,
781 void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
782 HandleOutput ctrl, HandleOutput evt,
783 HandleOutput cmd)
785 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
786 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
787 int i;
789 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI,
790 sizeof(VirtIOSCSIConfig));
792 if (s->conf.num_queues <= 0 || s->conf.num_queues > VIRTIO_PCI_QUEUE_MAX) {
793 error_setg(errp, "Invalid number of queues (= %" PRId32 "), "
794 "must be a positive integer less than %d.",
795 s->conf.num_queues, VIRTIO_PCI_QUEUE_MAX);
796 return;
798 s->cmd_vqs = g_malloc0(s->conf.num_queues * sizeof(VirtQueue *));
799 s->sense_size = VIRTIO_SCSI_SENSE_SIZE;
800 s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
802 s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
803 ctrl);
804 s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
805 evt);
806 for (i = 0; i < s->conf.num_queues; i++) {
807 s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
808 cmd);
811 if (s->conf.iothread) {
812 virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
816 /* Disable dataplane thread during live migration since it does not
817 * update the dirty memory bitmap yet.
819 static void virtio_scsi_migration_state_changed(Notifier *notifier, void *data)
821 VirtIOSCSI *s = container_of(notifier, VirtIOSCSI,
822 migration_state_notifier);
823 MigrationState *mig = data;
825 if (migration_in_setup(mig)) {
826 if (!s->dataplane_started) {
827 return;
829 virtio_scsi_dataplane_stop(s);
830 s->dataplane_disabled = true;
831 } else if (migration_has_finished(mig) ||
832 migration_has_failed(mig)) {
833 if (s->dataplane_started) {
834 return;
836 blk_drain_all(); /* complete in-flight non-dataplane requests */
837 s->dataplane_disabled = false;
841 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp)
843 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
844 VirtIOSCSI *s = VIRTIO_SCSI(dev);
845 static int virtio_scsi_id;
846 Error *err = NULL;
848 virtio_scsi_common_realize(dev, &err, virtio_scsi_handle_ctrl,
849 virtio_scsi_handle_event,
850 virtio_scsi_handle_cmd);
851 if (err != NULL) {
852 error_propagate(errp, err);
853 return;
856 scsi_bus_new(&s->bus, sizeof(s->bus), dev,
857 &virtio_scsi_scsi_info, vdev->bus_name);
858 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
859 qbus_set_hotplug_handler(BUS(&s->bus), dev, &error_abort);
861 if (!dev->hotplugged) {
862 scsi_bus_legacy_handle_cmdline(&s->bus, &err);
863 if (err != NULL) {
864 error_propagate(errp, err);
865 return;
869 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1,
870 virtio_scsi_save, virtio_scsi_load, s);
871 s->migration_state_notifier.notify = virtio_scsi_migration_state_changed;
872 add_migration_state_change_notifier(&s->migration_state_notifier);
875 static void virtio_scsi_instance_init(Object *obj)
877 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(obj);
879 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
880 (Object **)&vs->conf.iothread,
881 qdev_prop_allow_set_link_before_realize,
882 OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);
885 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
887 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
888 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
890 g_free(vs->cmd_vqs);
891 virtio_cleanup(vdev);
894 static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp)
896 VirtIOSCSI *s = VIRTIO_SCSI(dev);
898 unregister_savevm(dev, "virtio-scsi", s);
899 remove_migration_state_change_notifier(&s->migration_state_notifier);
901 virtio_scsi_common_unrealize(dev, errp);
904 static Property virtio_scsi_properties[] = {
905 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSI, parent_obj.conf),
906 DEFINE_PROP_END_OF_LIST(),
909 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data)
911 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
912 DeviceClass *dc = DEVICE_CLASS(klass);
914 vdc->get_config = virtio_scsi_get_config;
915 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
918 static void virtio_scsi_class_init(ObjectClass *klass, void *data)
920 DeviceClass *dc = DEVICE_CLASS(klass);
921 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
922 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
924 dc->props = virtio_scsi_properties;
925 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
926 vdc->realize = virtio_scsi_device_realize;
927 vdc->unrealize = virtio_scsi_device_unrealize;
928 vdc->set_config = virtio_scsi_set_config;
929 vdc->get_features = virtio_scsi_get_features;
930 vdc->reset = virtio_scsi_reset;
931 hc->plug = virtio_scsi_hotplug;
932 hc->unplug = virtio_scsi_hotunplug;
935 static const TypeInfo virtio_scsi_common_info = {
936 .name = TYPE_VIRTIO_SCSI_COMMON,
937 .parent = TYPE_VIRTIO_DEVICE,
938 .instance_size = sizeof(VirtIOSCSICommon),
939 .abstract = true,
940 .class_init = virtio_scsi_common_class_init,
943 static const TypeInfo virtio_scsi_info = {
944 .name = TYPE_VIRTIO_SCSI,
945 .parent = TYPE_VIRTIO_SCSI_COMMON,
946 .instance_size = sizeof(VirtIOSCSI),
947 .instance_init = virtio_scsi_instance_init,
948 .class_init = virtio_scsi_class_init,
949 .interfaces = (InterfaceInfo[]) {
950 { TYPE_HOTPLUG_HANDLER },
955 static void virtio_register_types(void)
957 type_register_static(&virtio_scsi_common_info);
958 type_register_static(&virtio_scsi_info);
961 type_init(virtio_register_types)