These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / hw / scsi / virtio-scsi-dataplane.c
1 /*
2  * Virtio SCSI dataplane
3  *
4  * Copyright Red Hat, Inc. 2014
5  *
6  * Authors:
7  *   Fam Zheng <famz@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  *
12  */
13
14 #include "qemu/osdep.h"
15 #include "hw/virtio/virtio-scsi.h"
16 #include "qemu/error-report.h"
17 #include "sysemu/block-backend.h"
18 #include <hw/scsi/scsi.h>
19 #include <block/scsi.h>
20 #include <hw/virtio/virtio-bus.h>
21 #include "hw/virtio/virtio-access.h"
22
23 /* Context: QEMU global mutex held */
24 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread)
25 {
26     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
27     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
28     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
29
30     assert(!s->ctx);
31     s->ctx = iothread_get_aio_context(vs->conf.iothread);
32
33     /* Don't try if transport does not support notifiers. */
34     if (!k->set_guest_notifiers || !k->set_host_notifier) {
35         fprintf(stderr, "virtio-scsi: Failed to set iothread "
36                    "(transport does not support notifiers)");
37         exit(1);
38     }
39 }
40
41 static void virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev,
42                                               VirtQueue *vq)
43 {
44     VirtIOSCSI *s = (VirtIOSCSI *)vdev;
45
46     assert(s->ctx && s->dataplane_started);
47     virtio_scsi_handle_cmd_vq(s, vq);
48 }
49
50 static void virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev,
51                                                VirtQueue *vq)
52 {
53     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
54
55     assert(s->ctx && s->dataplane_started);
56     virtio_scsi_handle_ctrl_vq(s, vq);
57 }
58
59 static void virtio_scsi_data_plane_handle_event(VirtIODevice *vdev,
60                                                 VirtQueue *vq)
61 {
62     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
63
64     assert(s->ctx && s->dataplane_started);
65     virtio_scsi_handle_event_vq(s, vq);
66 }
67
68 static int virtio_scsi_vring_init(VirtIOSCSI *s, VirtQueue *vq, int n,
69                                   void (*fn)(VirtIODevice *vdev, VirtQueue *vq))
70 {
71     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
72     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
73     int rc;
74
75     /* Set up virtqueue notify */
76     rc = k->set_host_notifier(qbus->parent, n, true);
77     if (rc != 0) {
78         fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
79                 rc);
80         s->dataplane_fenced = true;
81         return rc;
82     }
83
84     virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, fn);
85     return 0;
86 }
87
88 void virtio_scsi_dataplane_notify(VirtIODevice *vdev, VirtIOSCSIReq *req)
89 {
90     if (virtio_should_notify(vdev, req->vq)) {
91         event_notifier_set(virtio_queue_get_guest_notifier(req->vq));
92     }
93 }
94
95 /* assumes s->ctx held */
96 static void virtio_scsi_clear_aio(VirtIOSCSI *s)
97 {
98     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
99     int i;
100
101     virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx, NULL);
102     virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx, NULL);
103     for (i = 0; i < vs->conf.num_queues; i++) {
104         virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL);
105     }
106 }
107
108 /* Context: QEMU global mutex held */
109 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
110 {
111     int i;
112     int rc;
113     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
114     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
115     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
116
117     if (s->dataplane_started ||
118         s->dataplane_starting ||
119         s->dataplane_fenced ||
120         s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
121         return;
122     }
123
124     s->dataplane_starting = true;
125
126     /* Set up guest notifier (irq) */
127     rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
128     if (rc != 0) {
129         fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
130                 "ensure -enable-kvm is set\n", rc);
131         goto fail_guest_notifiers;
132     }
133
134     aio_context_acquire(s->ctx);
135     rc = virtio_scsi_vring_init(s, vs->ctrl_vq, 0,
136                                 virtio_scsi_data_plane_handle_ctrl);
137     if (rc) {
138         goto fail_vrings;
139     }
140     rc = virtio_scsi_vring_init(s, vs->event_vq, 1,
141                                 virtio_scsi_data_plane_handle_event);
142     if (rc) {
143         goto fail_vrings;
144     }
145     for (i = 0; i < vs->conf.num_queues; i++) {
146         rc = virtio_scsi_vring_init(s, vs->cmd_vqs[i], i + 2,
147                                     virtio_scsi_data_plane_handle_cmd);
148         if (rc) {
149             goto fail_vrings;
150         }
151     }
152
153     s->dataplane_starting = false;
154     s->dataplane_started = true;
155     aio_context_release(s->ctx);
156     return;
157
158 fail_vrings:
159     virtio_scsi_clear_aio(s);
160     aio_context_release(s->ctx);
161     for (i = 0; i < vs->conf.num_queues + 2; i++) {
162         k->set_host_notifier(qbus->parent, i, false);
163     }
164     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
165 fail_guest_notifiers:
166     s->dataplane_fenced = true;
167     s->dataplane_starting = false;
168     s->dataplane_started = true;
169 }
170
171 /* Context: QEMU global mutex held */
172 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
173 {
174     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
175     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
176     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
177     int i;
178
179     if (!s->dataplane_started || s->dataplane_stopping) {
180         return;
181     }
182
183     /* Better luck next time. */
184     if (s->dataplane_fenced) {
185         s->dataplane_fenced = false;
186         s->dataplane_started = false;
187         return;
188     }
189     s->dataplane_stopping = true;
190     assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
191
192     aio_context_acquire(s->ctx);
193
194     virtio_scsi_clear_aio(s);
195
196     blk_drain_all(); /* ensure there are no in-flight requests */
197
198     aio_context_release(s->ctx);
199
200     for (i = 0; i < vs->conf.num_queues + 2; i++) {
201         k->set_host_notifier(qbus->parent, i, false);
202     }
203
204     /* Clean up guest notifier (irq) */
205     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
206     s->dataplane_stopping = false;
207     s->dataplane_started = false;
208 }