| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Virtio SCSI dataplane | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright Red Hat, Inc. 2014 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Authors: | 
					
						
							|  |  |  |  *   Fam Zheng <famz@redhat.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU GPL, version 2 or later. | 
					
						
							|  |  |  |  * See the COPYING file in the top-level directory. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:07 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | #include "hw/virtio/virtio-scsi.h"
 | 
					
						
							|  |  |  | #include "qemu/error-report.h"
 | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:18 +02:00
										 |  |  | #include "sysemu/block-backend.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | #include <hw/scsi/scsi.h>
 | 
					
						
							|  |  |  | #include <block/scsi.h>
 | 
					
						
							|  |  |  | #include <hw/virtio/virtio-bus.h>
 | 
					
						
							|  |  |  | #include "hw/virtio/virtio-access.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Context: QEMU global mutex held */ | 
					
						
							|  |  |  | void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); | 
					
						
							|  |  |  |     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | 
					
						
							|  |  |  |     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(!s->ctx); | 
					
						
							|  |  |  |     s->ctx = iothread_get_aio_context(vs->conf.iothread); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Don't try if transport does not support notifiers. */ | 
					
						
							|  |  |  |     if (!k->set_guest_notifiers || !k->set_host_notifier) { | 
					
						
							|  |  |  |         fprintf(stderr, "virtio-scsi: Failed to set iothread " | 
					
						
							|  |  |  |                    "(transport does not support notifiers)"); | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 12:16:27 +02:00
										 |  |  | static void virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev, | 
					
						
							|  |  |  |                                               VirtQueue *vq) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOSCSI *s = (VirtIOSCSI *)vdev; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(s->ctx && s->dataplane_started); | 
					
						
							|  |  |  |     virtio_scsi_handle_cmd_vq(s, vq); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev, | 
					
						
							|  |  |  |                                                VirtQueue *vq) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOSCSI *s = VIRTIO_SCSI(vdev); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(s->ctx && s->dataplane_started); | 
					
						
							|  |  |  |     virtio_scsi_handle_ctrl_vq(s, vq); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void virtio_scsi_data_plane_handle_event(VirtIODevice *vdev, | 
					
						
							|  |  |  |                                                 VirtQueue *vq) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOSCSI *s = VIRTIO_SCSI(vdev); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(s->ctx && s->dataplane_started); | 
					
						
							|  |  |  |     virtio_scsi_handle_event_vq(s, vq); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int virtio_scsi_vring_init(VirtIOSCSI *s, VirtQueue *vq, int n, | 
					
						
							|  |  |  |                                   void (*fn)(VirtIODevice *vdev, VirtQueue *vq)) | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); | 
					
						
							|  |  |  |     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:24 +02:00
										 |  |  |     int rc; | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Set up virtqueue notify */ | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:24 +02:00
										 |  |  |     rc = k->set_host_notifier(qbus->parent, n, true); | 
					
						
							|  |  |  |     if (rc != 0) { | 
					
						
							|  |  |  |         fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n", | 
					
						
							|  |  |  |                 rc); | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:26 +02:00
										 |  |  |         s->dataplane_fenced = true; | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |         return rc; | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-18 09:42:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 12:16:28 +02:00
										 |  |  |     virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, fn); | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  | void virtio_scsi_dataplane_notify(VirtIODevice *vdev, VirtIOSCSIReq *req) | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     if (virtio_should_notify(vdev, req->vq)) { | 
					
						
							|  |  |  |         event_notifier_set(virtio_queue_get_guest_notifier(req->vq)); | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:28 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  | /* assumes s->ctx held */ | 
					
						
							|  |  |  | static void virtio_scsi_clear_aio(VirtIOSCSI *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 12:16:28 +02:00
										 |  |  |     virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx, NULL); | 
					
						
							|  |  |  |     virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx, NULL); | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     for (i = 0; i < vs->conf.num_queues; i++) { | 
					
						
							| 
									
										
										
										
											2016-04-06 12:16:28 +02:00
										 |  |  |         virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL); | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | /* Context: QEMU global mutex held */ | 
					
						
							|  |  |  | void virtio_scsi_dataplane_start(VirtIOSCSI *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     int rc; | 
					
						
							|  |  |  |     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); | 
					
						
							|  |  |  |     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | 
					
						
							|  |  |  |     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->dataplane_started || | 
					
						
							|  |  |  |         s->dataplane_starting || | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:26 +02:00
										 |  |  |         s->dataplane_fenced || | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  |         s->ctx != iothread_get_aio_context(vs->conf.iothread)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s->dataplane_starting = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Set up guest notifier (irq) */ | 
					
						
							|  |  |  |     rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true); | 
					
						
							|  |  |  |     if (rc != 0) { | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:24 +02:00
										 |  |  |         fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), " | 
					
						
							|  |  |  |                 "ensure -enable-kvm is set\n", rc); | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  |         goto fail_guest_notifiers; | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     aio_context_acquire(s->ctx); | 
					
						
							| 
									
										
										
										
											2016-04-06 12:16:27 +02:00
										 |  |  |     rc = virtio_scsi_vring_init(s, vs->ctrl_vq, 0, | 
					
						
							|  |  |  |                                 virtio_scsi_data_plane_handle_ctrl); | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     if (rc) { | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  |         goto fail_vrings; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-06 12:16:27 +02:00
										 |  |  |     rc = virtio_scsi_vring_init(s, vs->event_vq, 1, | 
					
						
							|  |  |  |                                 virtio_scsi_data_plane_handle_event); | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     if (rc) { | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  |         goto fail_vrings; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  |     for (i = 0; i < vs->conf.num_queues; i++) { | 
					
						
							| 
									
										
										
										
											2016-04-06 12:16:27 +02:00
										 |  |  |         rc = virtio_scsi_vring_init(s, vs->cmd_vqs[i], i + 2, | 
					
						
							|  |  |  |                                     virtio_scsi_data_plane_handle_cmd); | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |         if (rc) { | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  |             goto fail_vrings; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s->dataplane_starting = false; | 
					
						
							|  |  |  |     s->dataplane_started = true; | 
					
						
							| 
									
										
										
										
											2014-10-31 17:38:04 +01:00
										 |  |  |     aio_context_release(s->ctx); | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | fail_vrings: | 
					
						
							|  |  |  |     virtio_scsi_clear_aio(s); | 
					
						
							|  |  |  |     aio_context_release(s->ctx); | 
					
						
							|  |  |  |     for (i = 0; i < vs->conf.num_queues + 2; i++) { | 
					
						
							|  |  |  |         k->set_host_notifier(qbus->parent, i, false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); | 
					
						
							|  |  |  | fail_guest_notifiers: | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     s->dataplane_fenced = true; | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:25 +02:00
										 |  |  |     s->dataplane_starting = false; | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     s->dataplane_started = true; | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Context: QEMU global mutex held */ | 
					
						
							|  |  |  | void virtio_scsi_dataplane_stop(VirtIOSCSI *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); | 
					
						
							|  |  |  |     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | 
					
						
							|  |  |  |     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     if (!s->dataplane_started || s->dataplane_stopping) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 15:15:26 +02:00
										 |  |  |     /* Better luck next time. */ | 
					
						
							|  |  |  |     if (s->dataplane_fenced) { | 
					
						
							|  |  |  |         s->dataplane_fenced = false; | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |         s->dataplane_started = false; | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     s->dataplane_stopping = true; | 
					
						
							|  |  |  |     assert(s->ctx == iothread_get_aio_context(vs->conf.iothread)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     aio_context_acquire(s->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-14 18:17:10 +01:00
										 |  |  |     virtio_scsi_clear_aio(s); | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:18 +02:00
										 |  |  |     blk_drain_all(); /* ensure there are no in-flight requests */ | 
					
						
							| 
									
										
										
										
											2014-09-23 15:49:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     aio_context_release(s->ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < vs->conf.num_queues + 2; i++) { | 
					
						
							|  |  |  |         k->set_host_notifier(qbus->parent, i, false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Clean up guest notifier (irq) */ | 
					
						
							|  |  |  |     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); | 
					
						
							|  |  |  |     s->dataplane_stopping = false; | 
					
						
							|  |  |  |     s->dataplane_started = false; | 
					
						
							|  |  |  | } |