master
c 439 lines 12.5 KB
Raw
1 /*
2 * vhost-user-scsi host device
3 *
4 * Copyright (c) 2016 Nutanix Inc. All rights reserved.
5 *
6 * Author:
7 * Felipe Franciosi <felipe@nutanix.com>
8 *
9 * This work is largely based on the "vhost-scsi" implementation by:
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
11 * Nicholas Bellinger <nab@risingtidesystems.com>
12 *
13 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
14 * See the COPYING.LIB file in the top-level directory.
15 *
16 */
17
18 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "qemu/error-report.h"
21 #include "hw/core/fw-path-provider.h"
22 #include "hw/core/qdev.h"
23 #include "hw/core/qdev-properties.h"
24 #include "hw/core/qdev-properties-system.h"
25 #include "hw/virtio/vhost.h"
26 #include "hw/virtio/vhost-backend.h"
27 #include "hw/virtio/vhost-user-scsi.h"
28 #include "hw/virtio/virtio.h"
29 #include "chardev/char-fe.h"
30 #include "system/system.h"
31
32 /* Features supported by the host application */
33 static const int user_feature_bits[] = {
34 VIRTIO_F_NOTIFY_ON_EMPTY,
35 VIRTIO_RING_F_INDIRECT_DESC,
36 VIRTIO_RING_F_EVENT_IDX,
37 VIRTIO_SCSI_F_HOTPLUG,
38 VIRTIO_F_RING_RESET,
39 VIRTIO_F_IN_ORDER,
40 VIRTIO_F_NOTIFICATION_DATA,
41 VHOST_INVALID_FEATURE_BIT
42 };
43
44 static int vhost_user_scsi_start(VHostUserSCSI *s, Error **errp)
45 {
46 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
47 int ret;
48
49 ret = vhost_scsi_common_start(vsc, errp);
50 s->started_vu = !(ret < 0);
51
52 return ret;
53 }
54
55 static int vhost_user_scsi_stop(VHostUserSCSI *s)
56 {
57 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
58
59 if (!s->started_vu) {
60 return 0;
61 }
62 s->started_vu = false;
63
64 return vhost_scsi_common_stop(vsc);
65 }
66
67 static int vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status)
68 {
69 VHostUserSCSI *s = (VHostUserSCSI *)vdev;
70 DeviceState *dev = DEVICE(vdev);
71 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
72 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
73 bool should_start = virtio_device_should_start(vdev, status);
74 Error *local_err = NULL;
75 int ret;
76
77 if (!s->connected) {
78 return -1;
79 }
80
81 if (vhost_dev_is_started(&vsc->dev) == should_start) {
82 return 0;
83 }
84
85 if (should_start) {
86 ret = vhost_user_scsi_start(s, &local_err);
87 if (ret < 0) {
88 error_reportf_err(local_err,
89 "unable to start vhost-user-scsi: %s: ",
90 strerror(-ret));
91 qemu_chr_fe_disconnect(&vs->conf.chardev);
92 }
93 } else {
94 ret = vhost_user_scsi_stop(s);
95 if (ret) {
96 return ret;
97 }
98 }
99 return 0;
100 }
101
102 static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *vq)
103 {
104 VHostUserSCSI *s = (VHostUserSCSI *)vdev;
105 DeviceState *dev = DEVICE(vdev);
106 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
107 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
108
109 Error *local_err = NULL;
110 int i, ret;
111
112 if (!vdev->start_on_kick) {
113 return;
114 }
115
116 if (!s->connected) {
117 return;
118 }
119
120 if (vhost_dev_is_started(&vsc->dev)) {
121 return;
122 }
123
124 /*
125 * Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
126 * vhost here instead of waiting for .set_status().
127 */
128 ret = vhost_user_scsi_start(s, &local_err);
129 if (ret < 0) {
130 error_reportf_err(local_err, "vhost-user-scsi: vhost start failed: ");
131 qemu_chr_fe_disconnect(&vs->conf.chardev);
132 return;
133 }
134
135 /* Kick right away to begin processing requests already in vring */
136 for (i = 0; i < vsc->dev.nvqs; i++) {
137 VirtQueue *kick_vq = virtio_get_queue(vdev, i);
138
139 if (!virtio_queue_get_desc_addr(vdev, i)) {
140 continue;
141 }
142 event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
143 }
144 }
145
146 static int vhost_user_scsi_connect(DeviceState *dev, Error **errp)
147 {
148 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
149 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
150 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
151 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
152 int ret = 0;
153
154 if (s->connected) {
155 return 0;
156 }
157
158 vsc->dev.num_queues = vs->conf.num_queues;
159 vsc->dev.nvqs = VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
160 vsc->dev.vqs = s->vhost_vqs;
161 vsc->dev.vq_index = 0;
162
163 ret = vhost_dev_init(&vsc->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
164 errp);
165 if (ret < 0) {
166 return ret;
167 }
168
169 s->connected = true;
170
171 /* restore vhost state */
172 if (virtio_device_started(vdev, vdev->status)) {
173 ret = vhost_user_scsi_start(s, errp);
174 }
175
176 return ret;
177 }
178
179 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event);
180
181 static void vhost_user_scsi_disconnect(DeviceState *dev)
182 {
183 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
184 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
185 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
186 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
187
188 if (!s->connected) {
189 goto done;
190 }
191 s->connected = false;
192
193 vhost_user_scsi_stop(s);
194
195 vhost_dev_cleanup(&vsc->dev);
196
197 done:
198 /* Re-instate the event handler for new connections */
199 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL,
200 vhost_user_scsi_event, NULL, dev, NULL, true);
201 }
202
203 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event)
204 {
205 DeviceState *dev = opaque;
206 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
207 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
208 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
209 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
210 Error *local_err = NULL;
211
212 switch (event) {
213 case CHR_EVENT_OPENED:
214 if (vhost_user_scsi_connect(dev, &local_err) < 0) {
215 error_report_err(local_err);
216 qemu_chr_fe_disconnect(&vs->conf.chardev);
217 return;
218 }
219 break;
220 case CHR_EVENT_CLOSED:
221 /* defer close until later to avoid circular close */
222 vhost_user_async_close(dev, &vs->conf.chardev, &vsc->dev,
223 vhost_user_scsi_disconnect);
224 break;
225 case CHR_EVENT_BREAK:
226 case CHR_EVENT_MUX_IN:
227 case CHR_EVENT_MUX_OUT:
228 /* Ignore */
229 break;
230 }
231 }
232
233 static int vhost_user_scsi_realize_connect(VHostUserSCSI *s, Error **errp)
234 {
235 DeviceState *dev = DEVICE(s);
236 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
237 int ret;
238
239 s->connected = false;
240
241 ret = qemu_chr_fe_wait_connected(&vs->conf.chardev, errp);
242 if (ret < 0) {
243 return ret;
244 }
245
246 ret = vhost_user_scsi_connect(dev, errp);
247 if (ret < 0) {
248 qemu_chr_fe_disconnect(&vs->conf.chardev);
249 return ret;
250 }
251 assert(s->connected);
252
253 return 0;
254 }
255
256 static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
257 {
258 ERRP_GUARD();
259 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
260 VHostUserSCSI *s = VHOST_USER_SCSI(dev);
261 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
262 Error *err = NULL;
263 int ret;
264 int retries = VU_REALIZE_CONN_RETRIES;
265
266 if (!vs->conf.chardev.chr) {
267 error_setg(errp, "vhost-user-scsi: missing chardev");
268 return;
269 }
270
271 virtio_scsi_common_realize(dev, vhost_user_scsi_handle_output,
272 vhost_user_scsi_handle_output,
273 vhost_user_scsi_handle_output, &err);
274 if (err != NULL) {
275 error_propagate(errp, err);
276 return;
277 }
278
279 if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
280 goto free_virtio;
281 }
282
283 vsc->inflight = g_new0(struct vhost_inflight, 1);
284 s->vhost_vqs = g_new0(struct vhost_virtqueue,
285 VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues);
286
287 assert(!*errp);
288 do {
289 if (*errp) {
290 error_prepend(errp, "Reconnecting after error: ");
291 error_report_err(*errp);
292 *errp = NULL;
293 }
294 ret = vhost_user_scsi_realize_connect(s, errp);
295 } while (ret < 0 && retries--);
296
297 if (ret < 0) {
298 goto free_vhost;
299 }
300
301 /* we're fully initialized, now we can operate, so add the handler */
302 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL,
303 vhost_user_scsi_event, NULL, (void *)dev,
304 NULL, true);
305 /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
306 vsc->channel = 0;
307 vsc->lun = 0;
308 vsc->target = vs->conf.boot_tpgt;
309
310 return;
311
312 free_vhost:
313 g_free(s->vhost_vqs);
314 s->vhost_vqs = NULL;
315 g_free(vsc->inflight);
316 vsc->inflight = NULL;
317 vhost_user_cleanup(&s->vhost_user);
318
319 free_virtio:
320 virtio_scsi_common_unrealize(dev);
321 }
322
323 static void vhost_user_scsi_unrealize(DeviceState *dev)
324 {
325 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
326 VHostUserSCSI *s = VHOST_USER_SCSI(dev);
327 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
328 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
329
330 /* This will stop the vhost backend. */
331 vhost_user_scsi_set_status(vdev, 0);
332 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, NULL, NULL, NULL,
333 NULL, false);
334
335 vhost_dev_cleanup(&vsc->dev);
336 g_free(s->vhost_vqs);
337 s->vhost_vqs = NULL;
338
339 vhost_dev_free_inflight(vsc->inflight);
340 g_free(vsc->inflight);
341 vsc->inflight = NULL;
342
343 vhost_user_cleanup(&s->vhost_user);
344 virtio_scsi_common_unrealize(dev);
345 }
346
347 static const Property vhost_user_scsi_properties[] = {
348 DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev),
349 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
350 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
351 VIRTIO_SCSI_AUTO_NUM_QUEUES),
352 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
353 128),
354 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
355 0xFFFF),
356 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
357 DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features,
358 VIRTIO_SCSI_F_HOTPLUG,
359 true),
360 DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features,
361 VIRTIO_SCSI_F_CHANGE,
362 true),
363 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
364 VIRTIO_SCSI_F_T10_PI,
365 false),
366 };
367
368 static void vhost_user_scsi_reset(VirtIODevice *vdev)
369 {
370 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
371 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
372
373 vhost_dev_free_inflight(vsc->inflight);
374 }
375
376 static struct vhost_dev *vhost_user_scsi_get_vhost(VirtIODevice *vdev)
377 {
378 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
379 return &vsc->dev;
380 }
381
382 static const VMStateDescription vmstate_vhost_scsi = {
383 .name = "virtio-scsi",
384 .minimum_version_id = 1,
385 .version_id = 1,
386 .fields = (const VMStateField[]) {
387 VMSTATE_VIRTIO_DEVICE,
388 VMSTATE_END_OF_LIST()
389 },
390 };
391
392 static void vhost_user_scsi_class_init(ObjectClass *klass, const void *data)
393 {
394 DeviceClass *dc = DEVICE_CLASS(klass);
395 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
396 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
397
398 device_class_set_props(dc, vhost_user_scsi_properties);
399 dc->vmsd = &vmstate_vhost_scsi;
400 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
401 vdc->realize = vhost_user_scsi_realize;
402 vdc->unrealize = vhost_user_scsi_unrealize;
403 vdc->get_features = vhost_scsi_common_get_features;
404 vdc->set_config = vhost_scsi_common_set_config;
405 vdc->set_status = vhost_user_scsi_set_status;
406 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
407 vdc->reset = vhost_user_scsi_reset;
408 vdc->get_vhost = vhost_user_scsi_get_vhost;
409 }
410
411 static void vhost_user_scsi_instance_init(Object *obj)
412 {
413 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
414
415 vsc->feature_bits = user_feature_bits;
416
417 /* Add the bootindex property for this object */
418 device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
419 DEVICE(vsc));
420 }
421
422 static const TypeInfo vhost_user_scsi_info = {
423 .name = TYPE_VHOST_USER_SCSI,
424 .parent = TYPE_VHOST_SCSI_COMMON,
425 .instance_size = sizeof(VHostUserSCSI),
426 .class_init = vhost_user_scsi_class_init,
427 .instance_init = vhost_user_scsi_instance_init,
428 .interfaces = (const InterfaceInfo[]) {
429 { TYPE_FW_PATH_PROVIDER },
430 { }
431 },
432 };
433
434 static void virtio_register_types(void)
435 {
436 type_register_static(&vhost_user_scsi_info);
437 }
438
439 type_init(virtio_register_types)