Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / scsi / vhost-user-scsi.c
blob55e4be5b343ed03b597c40c4f71764023c7d612a
1 /*
2 * vhost-user-scsi host device
4 * Copyright (c) 2016 Nutanix Inc. All rights reserved.
6 * Author:
7 * Felipe Franciosi <felipe@nutanix.com>
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>
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.
18 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "qemu/error-report.h"
21 #include "hw/fw-path-provider.h"
22 #include "hw/qdev-core.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/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 "sysemu/sysemu.h"
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
44 static int vhost_user_scsi_start(VHostUserSCSI *s, Error **errp)
46 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
47 int ret;
49 ret = vhost_scsi_common_start(vsc, errp);
50 s->started_vu = !(ret < 0);
52 return ret;
55 static void vhost_user_scsi_stop(VHostUserSCSI *s)
57 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
59 if (!s->started_vu) {
60 return;
62 s->started_vu = false;
64 vhost_scsi_common_stop(vsc);
67 static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status)
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;
77 if (!s->connected) {
78 return;
81 if (vhost_dev_is_started(&vsc->dev) == should_start) {
82 return;
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);
93 } else {
94 vhost_user_scsi_stop(s);
98 static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *vq)
100 VHostUserSCSI *s = (VHostUserSCSI *)vdev;
101 DeviceState *dev = DEVICE(vdev);
102 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
103 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
105 Error *local_err = NULL;
106 int i, ret;
108 if (!vdev->start_on_kick) {
109 return;
112 if (!s->connected) {
113 return;
116 if (vhost_dev_is_started(&vsc->dev)) {
117 return;
121 * Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
122 * vhost here instead of waiting for .set_status().
124 ret = vhost_user_scsi_start(s, &local_err);
125 if (ret < 0) {
126 error_reportf_err(local_err, "vhost-user-scsi: vhost start failed: ");
127 qemu_chr_fe_disconnect(&vs->conf.chardev);
128 return;
131 /* Kick right away to begin processing requests already in vring */
132 for (i = 0; i < vsc->dev.nvqs; i++) {
133 VirtQueue *kick_vq = virtio_get_queue(vdev, i);
135 if (!virtio_queue_get_desc_addr(vdev, i)) {
136 continue;
138 event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
142 static int vhost_user_scsi_connect(DeviceState *dev, Error **errp)
144 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
145 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
146 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
147 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
148 int ret = 0;
150 if (s->connected) {
151 return 0;
154 vsc->dev.num_queues = vs->conf.num_queues;
155 vsc->dev.nvqs = VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
156 vsc->dev.vqs = s->vhost_vqs;
157 vsc->dev.vq_index = 0;
158 vsc->dev.backend_features = 0;
160 ret = vhost_dev_init(&vsc->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
161 errp);
162 if (ret < 0) {
163 return ret;
166 s->connected = true;
168 /* restore vhost state */
169 if (virtio_device_started(vdev, vdev->status)) {
170 ret = vhost_user_scsi_start(s, errp);
173 return ret;
176 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event);
178 static void vhost_user_scsi_disconnect(DeviceState *dev)
180 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
181 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
182 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
183 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
185 if (!s->connected) {
186 goto done;
188 s->connected = false;
190 vhost_user_scsi_stop(s);
192 vhost_dev_cleanup(&vsc->dev);
194 done:
195 /* Re-instate the event handler for new connections */
196 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL,
197 vhost_user_scsi_event, NULL, dev, NULL, true);
200 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event)
202 DeviceState *dev = opaque;
203 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
204 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
205 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
206 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
207 Error *local_err = NULL;
209 switch (event) {
210 case CHR_EVENT_OPENED:
211 if (vhost_user_scsi_connect(dev, &local_err) < 0) {
212 error_report_err(local_err);
213 qemu_chr_fe_disconnect(&vs->conf.chardev);
214 return;
216 break;
217 case CHR_EVENT_CLOSED:
218 /* defer close until later to avoid circular close */
219 vhost_user_async_close(dev, &vs->conf.chardev, &vsc->dev,
220 vhost_user_scsi_disconnect);
221 break;
222 case CHR_EVENT_BREAK:
223 case CHR_EVENT_MUX_IN:
224 case CHR_EVENT_MUX_OUT:
225 /* Ignore */
226 break;
230 static int vhost_user_scsi_realize_connect(VHostUserSCSI *s, Error **errp)
232 DeviceState *dev = DEVICE(s);
233 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
234 int ret;
236 s->connected = false;
238 ret = qemu_chr_fe_wait_connected(&vs->conf.chardev, errp);
239 if (ret < 0) {
240 return ret;
243 ret = vhost_user_scsi_connect(dev, errp);
244 if (ret < 0) {
245 qemu_chr_fe_disconnect(&vs->conf.chardev);
246 return ret;
248 assert(s->connected);
250 return 0;
253 static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
255 ERRP_GUARD();
256 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
257 VHostUserSCSI *s = VHOST_USER_SCSI(dev);
258 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
259 Error *err = NULL;
260 int ret;
261 int retries = VU_REALIZE_CONN_RETRIES;
263 if (!vs->conf.chardev.chr) {
264 error_setg(errp, "vhost-user-scsi: missing chardev");
265 return;
268 virtio_scsi_common_realize(dev, vhost_user_scsi_handle_output,
269 vhost_user_scsi_handle_output,
270 vhost_user_scsi_handle_output, &err);
271 if (err != NULL) {
272 error_propagate(errp, err);
273 return;
276 if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
277 goto free_virtio;
280 vsc->inflight = g_new0(struct vhost_inflight, 1);
281 s->vhost_vqs = g_new0(struct vhost_virtqueue,
282 VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues);
284 assert(!*errp);
285 do {
286 if (*errp) {
287 error_prepend(errp, "Reconnecting after error: ");
288 error_report_err(*errp);
289 *errp = NULL;
291 ret = vhost_user_scsi_realize_connect(s, errp);
292 } while (ret < 0 && retries--);
294 if (ret < 0) {
295 goto free_vhost;
298 /* we're fully initialized, now we can operate, so add the handler */
299 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL,
300 vhost_user_scsi_event, NULL, (void *)dev,
301 NULL, true);
302 /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
303 vsc->channel = 0;
304 vsc->lun = 0;
305 vsc->target = vs->conf.boot_tpgt;
307 return;
309 free_vhost:
310 g_free(s->vhost_vqs);
311 s->vhost_vqs = NULL;
312 g_free(vsc->inflight);
313 vsc->inflight = NULL;
314 vhost_user_cleanup(&s->vhost_user);
316 free_virtio:
317 virtio_scsi_common_unrealize(dev);
320 static void vhost_user_scsi_unrealize(DeviceState *dev)
322 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
323 VHostUserSCSI *s = VHOST_USER_SCSI(dev);
324 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
325 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
327 /* This will stop the vhost backend. */
328 vhost_user_scsi_set_status(vdev, 0);
329 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, NULL, NULL, NULL,
330 NULL, false);
332 vhost_dev_cleanup(&vsc->dev);
333 g_free(s->vhost_vqs);
334 s->vhost_vqs = NULL;
336 vhost_dev_free_inflight(vsc->inflight);
337 g_free(vsc->inflight);
338 vsc->inflight = NULL;
340 vhost_user_cleanup(&s->vhost_user);
341 virtio_scsi_common_unrealize(dev);
344 static Property vhost_user_scsi_properties[] = {
345 DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev),
346 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
347 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
348 VIRTIO_SCSI_AUTO_NUM_QUEUES),
349 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
350 128),
351 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
352 0xFFFF),
353 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
354 DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features,
355 VIRTIO_SCSI_F_HOTPLUG,
356 true),
357 DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features,
358 VIRTIO_SCSI_F_CHANGE,
359 true),
360 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
361 VIRTIO_SCSI_F_T10_PI,
362 false),
363 DEFINE_PROP_END_OF_LIST(),
366 static void vhost_user_scsi_reset(VirtIODevice *vdev)
368 VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
369 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
371 vhost_dev_free_inflight(vsc->inflight);
374 static struct vhost_dev *vhost_user_scsi_get_vhost(VirtIODevice *vdev)
376 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
377 return &vsc->dev;
380 static const VMStateDescription vmstate_vhost_scsi = {
381 .name = "virtio-scsi",
382 .minimum_version_id = 1,
383 .version_id = 1,
384 .fields = (const VMStateField[]) {
385 VMSTATE_VIRTIO_DEVICE,
386 VMSTATE_END_OF_LIST()
390 static void vhost_user_scsi_class_init(ObjectClass *klass, void *data)
392 DeviceClass *dc = DEVICE_CLASS(klass);
393 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
394 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
396 device_class_set_props(dc, vhost_user_scsi_properties);
397 dc->vmsd = &vmstate_vhost_scsi;
398 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
399 vdc->realize = vhost_user_scsi_realize;
400 vdc->unrealize = vhost_user_scsi_unrealize;
401 vdc->get_features = vhost_scsi_common_get_features;
402 vdc->set_config = vhost_scsi_common_set_config;
403 vdc->set_status = vhost_user_scsi_set_status;
404 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
405 vdc->reset = vhost_user_scsi_reset;
406 vdc->get_vhost = vhost_user_scsi_get_vhost;
409 static void vhost_user_scsi_instance_init(Object *obj)
411 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
413 vsc->feature_bits = user_feature_bits;
415 /* Add the bootindex property for this object */
416 device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
417 DEVICE(vsc));
420 static const TypeInfo vhost_user_scsi_info = {
421 .name = TYPE_VHOST_USER_SCSI,
422 .parent = TYPE_VHOST_SCSI_COMMON,
423 .instance_size = sizeof(VHostUserSCSI),
424 .class_init = vhost_user_scsi_class_init,
425 .instance_init = vhost_user_scsi_instance_init,
426 .interfaces = (InterfaceInfo[]) {
427 { TYPE_FW_PATH_PROVIDER },
432 static void virtio_register_types(void)
434 type_register_static(&vhost_user_scsi_info);
437 type_init(virtio_register_types)