]>
Commit | Line | Data |
---|---|---|
91cb1c9b FZ |
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 | ||
9b8bfe21 | 14 | #include "qemu/osdep.h" |
ad07cd69 | 15 | #include "qapi/error.h" |
91cb1c9b FZ |
16 | #include "hw/virtio/virtio-scsi.h" |
17 | #include "qemu/error-report.h" | |
4be74634 | 18 | #include "sysemu/block-backend.h" |
a9c94277 | 19 | #include "hw/scsi/scsi.h" |
08e2c9f1 | 20 | #include "scsi/constants.h" |
a9c94277 | 21 | #include "hw/virtio/virtio-bus.h" |
91cb1c9b | 22 | |
0b2675c4 | 23 | /* Context: BQL held */ |
ad07cd69 | 24 | void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp) |
91cb1c9b | 25 | { |
91cb1c9b | 26 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); |
ad07cd69 PB |
27 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
28 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
29 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
91cb1c9b | 30 | |
ad07cd69 PB |
31 | if (vs->conf.iothread) { |
32 | if (!k->set_guest_notifiers || !k->ioeventfd_assign) { | |
33 | error_setg(errp, | |
34 | "device is incompatible with iothread " | |
35 | "(transport does not support notifiers)"); | |
36 | return; | |
37 | } | |
38 | if (!virtio_device_ioeventfd_enabled(vdev)) { | |
39 | error_setg(errp, "ioeventfd is required for iothread"); | |
40 | return; | |
41 | } | |
42 | s->ctx = iothread_get_aio_context(vs->conf.iothread); | |
43 | } else { | |
44 | if (!virtio_device_ioeventfd_enabled(vdev)) { | |
45 | return; | |
46 | } | |
47 | s->ctx = qemu_get_aio_context(); | |
91cb1c9b FZ |
48 | } |
49 | } | |
50 | ||
61fc57bf | 51 | static int virtio_scsi_set_host_notifier(VirtIOSCSI *s, VirtQueue *vq, int n) |
91cb1c9b FZ |
52 | { |
53 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); | |
6d2c8316 | 54 | int rc; |
91cb1c9b FZ |
55 | |
56 | /* Set up virtqueue notify */ | |
b1f0a33d | 57 | rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true); |
6d2c8316 CH |
58 | if (rc != 0) { |
59 | fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n", | |
60 | rc); | |
4adea804 | 61 | s->dataplane_fenced = true; |
e24a47c5 | 62 | return rc; |
91cb1c9b | 63 | } |
196d4fc5 | 64 | |
e24a47c5 | 65 | return 0; |
91cb1c9b FZ |
66 | } |
67 | ||
184b9623 SH |
68 | /* Context: BH in IOThread */ |
69 | static void virtio_scsi_dataplane_stop_bh(void *opaque) | |
361dcc79 | 70 | { |
184b9623 | 71 | VirtIOSCSI *s = opaque; |
361dcc79 | 72 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); |
bd58ab40 | 73 | EventNotifier *host_notifier; |
361dcc79 CH |
74 | int i; |
75 | ||
db608fb7 | 76 | virtio_queue_aio_detach_host_notifier(vs->ctrl_vq, s->ctx); |
bd58ab40 SH |
77 | host_notifier = virtio_queue_get_host_notifier(vs->ctrl_vq); |
78 | ||
79 | /* | |
80 | * Test and clear notifier after disabling event, in case poll callback | |
81 | * didn't have time to run. | |
82 | */ | |
83 | virtio_queue_host_notifier_read(host_notifier); | |
84 | ||
db608fb7 | 85 | virtio_queue_aio_detach_host_notifier(vs->event_vq, s->ctx); |
bd58ab40 SH |
86 | host_notifier = virtio_queue_get_host_notifier(vs->event_vq); |
87 | virtio_queue_host_notifier_read(host_notifier); | |
88 | ||
e24a47c5 | 89 | for (i = 0; i < vs->conf.num_queues; i++) { |
db608fb7 | 90 | virtio_queue_aio_detach_host_notifier(vs->cmd_vqs[i], s->ctx); |
bd58ab40 SH |
91 | host_notifier = virtio_queue_get_host_notifier(vs->cmd_vqs[i]); |
92 | virtio_queue_host_notifier_read(host_notifier); | |
361dcc79 CH |
93 | } |
94 | } | |
95 | ||
0b2675c4 | 96 | /* Context: BQL held */ |
ad07cd69 | 97 | int virtio_scsi_dataplane_start(VirtIODevice *vdev) |
91cb1c9b FZ |
98 | { |
99 | int i; | |
100 | int rc; | |
dec2bb14 | 101 | int vq_init_count = 0; |
ad07cd69 | 102 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
91cb1c9b | 103 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
ad07cd69 PB |
104 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); |
105 | VirtIOSCSI *s = VIRTIO_SCSI(vdev); | |
91cb1c9b FZ |
106 | |
107 | if (s->dataplane_started || | |
108 | s->dataplane_starting || | |
ad07cd69 PB |
109 | s->dataplane_fenced) { |
110 | return 0; | |
91cb1c9b FZ |
111 | } |
112 | ||
113 | s->dataplane_starting = true; | |
114 | ||
115 | /* Set up guest notifier (irq) */ | |
116 | rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true); | |
117 | if (rc != 0) { | |
a1d30f28 TH |
118 | error_report("virtio-scsi: Failed to set guest notifiers (%d), " |
119 | "ensure -accel kvm is set.", rc); | |
361dcc79 | 120 | goto fail_guest_notifiers; |
91cb1c9b FZ |
121 | } |
122 | ||
9cf4fd87 GK |
123 | /* |
124 | * Batch all the host notifiers in a single transaction to avoid | |
125 | * quadratic time complexity in address_space_update_ioeventfds(). | |
126 | */ | |
c4f5dcc4 GK |
127 | memory_region_transaction_begin(); |
128 | ||
61fc57bf GK |
129 | rc = virtio_scsi_set_host_notifier(s, vs->ctrl_vq, 0); |
130 | if (rc != 0) { | |
131 | goto fail_host_notifiers; | |
361dcc79 | 132 | } |
dec2bb14 ML |
133 | |
134 | vq_init_count++; | |
61fc57bf GK |
135 | rc = virtio_scsi_set_host_notifier(s, vs->event_vq, 1); |
136 | if (rc != 0) { | |
137 | goto fail_host_notifiers; | |
361dcc79 | 138 | } |
dec2bb14 ML |
139 | |
140 | vq_init_count++; | |
61fc57bf | 141 | |
91cb1c9b | 142 | for (i = 0; i < vs->conf.num_queues; i++) { |
61fc57bf | 143 | rc = virtio_scsi_set_host_notifier(s, vs->cmd_vqs[i], i + 2); |
e24a47c5 | 144 | if (rc) { |
61fc57bf | 145 | goto fail_host_notifiers; |
361dcc79 | 146 | } |
dec2bb14 | 147 | vq_init_count++; |
91cb1c9b FZ |
148 | } |
149 | ||
c4f5dcc4 GK |
150 | memory_region_transaction_commit(); |
151 | ||
9a4b6a63 SH |
152 | s->dataplane_starting = false; |
153 | s->dataplane_started = true; | |
765ca516 | 154 | smp_wmb(); /* paired with aio_notify_accept() */ |
9a4b6a63 | 155 | |
766aa2de | 156 | if (s->bus.drain_count == 0) { |
766aa2de SH |
157 | virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx); |
158 | virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq, s->ctx); | |
61fc57bf | 159 | |
766aa2de SH |
160 | for (i = 0; i < vs->conf.num_queues; i++) { |
161 | virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx); | |
162 | } | |
61fc57bf | 163 | } |
ad07cd69 | 164 | return 0; |
361dcc79 | 165 | |
61fc57bf | 166 | fail_host_notifiers: |
dec2bb14 | 167 | for (i = 0; i < vq_init_count; i++) { |
21a4d962 | 168 | virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); |
c4f5dcc4 GK |
169 | } |
170 | ||
9cf4fd87 GK |
171 | /* |
172 | * The transaction expects the ioeventfds to be open when it | |
173 | * commits. Do it now, before the cleanup loop. | |
174 | */ | |
c4f5dcc4 GK |
175 | memory_region_transaction_commit(); |
176 | ||
177 | for (i = 0; i < vq_init_count; i++) { | |
76143618 | 178 | virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); |
361dcc79 CH |
179 | } |
180 | k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); | |
181 | fail_guest_notifiers: | |
e24a47c5 | 182 | s->dataplane_fenced = true; |
361dcc79 | 183 | s->dataplane_starting = false; |
e24a47c5 | 184 | s->dataplane_started = true; |
ad07cd69 | 185 | return -ENOSYS; |
91cb1c9b FZ |
186 | } |
187 | ||
0b2675c4 | 188 | /* Context: BQL held */ |
ad07cd69 | 189 | void virtio_scsi_dataplane_stop(VirtIODevice *vdev) |
91cb1c9b | 190 | { |
ad07cd69 | 191 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
91cb1c9b | 192 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
ad07cd69 PB |
193 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); |
194 | VirtIOSCSI *s = VIRTIO_SCSI(vdev); | |
21a4d962 | 195 | int i; |
91cb1c9b | 196 | |
e24a47c5 PB |
197 | if (!s->dataplane_started || s->dataplane_stopping) { |
198 | return; | |
199 | } | |
200 | ||
4adea804 CH |
201 | /* Better luck next time. */ |
202 | if (s->dataplane_fenced) { | |
203 | s->dataplane_fenced = false; | |
e24a47c5 | 204 | s->dataplane_started = false; |
91cb1c9b FZ |
205 | return; |
206 | } | |
207 | s->dataplane_stopping = true; | |
91cb1c9b | 208 | |
766aa2de SH |
209 | if (s->bus.drain_count == 0) { |
210 | aio_wait_bh_oneshot(s->ctx, virtio_scsi_dataplane_stop_bh, s); | |
211 | } | |
91cb1c9b | 212 | |
4be74634 | 213 | blk_drain_all(); /* ensure there are no in-flight requests */ |
91cb1c9b | 214 | |
9cf4fd87 GK |
215 | /* |
216 | * Batch all the host notifiers in a single transaction to avoid | |
217 | * quadratic time complexity in address_space_update_ioeventfds(). | |
218 | */ | |
c4f5dcc4 GK |
219 | memory_region_transaction_begin(); |
220 | ||
91cb1c9b | 221 | for (i = 0; i < vs->conf.num_queues + 2; i++) { |
21a4d962 | 222 | virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); |
c4f5dcc4 GK |
223 | } |
224 | ||
9cf4fd87 GK |
225 | /* |
226 | * The transaction expects the ioeventfds to be open when it | |
227 | * commits. Do it now, before the cleanup loop. | |
228 | */ | |
c4f5dcc4 GK |
229 | memory_region_transaction_commit(); |
230 | ||
231 | for (i = 0; i < vs->conf.num_queues + 2; i++) { | |
76143618 | 232 | virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); |
91cb1c9b FZ |
233 | } |
234 | ||
235 | /* Clean up guest notifier (irq) */ | |
236 | k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); | |
237 | s->dataplane_stopping = false; | |
238 | s->dataplane_started = false; | |
239 | } |