2 * vhost-user-scsi host device
4 * Copyright (c) 2016 Nutanix Inc. All rights reserved.
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
,
39 VHOST_INVALID_FEATURE_BIT
42 static int vhost_user_scsi_start(VHostUserSCSI
*s
, Error
**errp
)
44 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
47 ret
= vhost_scsi_common_start(vsc
, errp
);
48 s
->started_vu
= !(ret
< 0);
53 static void vhost_user_scsi_stop(VHostUserSCSI
*s
)
55 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
60 s
->started_vu
= false;
62 vhost_scsi_common_stop(vsc
);
65 static void vhost_user_scsi_set_status(VirtIODevice
*vdev
, uint8_t status
)
67 VHostUserSCSI
*s
= (VHostUserSCSI
*)vdev
;
68 DeviceState
*dev
= DEVICE(vdev
);
69 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
70 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
71 bool should_start
= virtio_device_should_start(vdev
, status
);
72 Error
*local_err
= NULL
;
79 if (vhost_dev_is_started(&vsc
->dev
) == should_start
) {
84 ret
= vhost_user_scsi_start(s
, &local_err
);
86 error_reportf_err(local_err
, "unable to start vhost-user-scsi: %s",
88 qemu_chr_fe_disconnect(&vs
->conf
.chardev
);
91 vhost_user_scsi_stop(s
);
95 static void vhost_user_scsi_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
97 VHostUserSCSI
*s
= (VHostUserSCSI
*)vdev
;
98 DeviceState
*dev
= DEVICE(vdev
);
99 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
100 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
102 Error
*local_err
= NULL
;
105 if (!vdev
->start_on_kick
) {
113 if (vhost_dev_is_started(&vsc
->dev
)) {
118 * Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
119 * vhost here instead of waiting for .set_status().
121 ret
= vhost_user_scsi_start(s
, &local_err
);
123 error_reportf_err(local_err
, "vhost-user-scsi: vhost start failed: ");
124 qemu_chr_fe_disconnect(&vs
->conf
.chardev
);
128 /* Kick right away to begin processing requests already in vring */
129 for (i
= 0; i
< vsc
->dev
.nvqs
; i
++) {
130 VirtQueue
*kick_vq
= virtio_get_queue(vdev
, i
);
132 if (!virtio_queue_get_desc_addr(vdev
, i
)) {
135 event_notifier_set(virtio_queue_get_host_notifier(kick_vq
));
139 static int vhost_user_scsi_connect(DeviceState
*dev
, Error
**errp
)
141 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
142 VHostUserSCSI
*s
= VHOST_USER_SCSI(vdev
);
143 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
144 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
151 vsc
->dev
.num_queues
= vs
->conf
.num_queues
;
152 vsc
->dev
.nvqs
= VIRTIO_SCSI_VQ_NUM_FIXED
+ vs
->conf
.num_queues
;
153 vsc
->dev
.vqs
= s
->vhost_vqs
;
154 vsc
->dev
.vq_index
= 0;
155 vsc
->dev
.backend_features
= 0;
157 ret
= vhost_dev_init(&vsc
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0,
165 /* restore vhost state */
166 if (virtio_device_started(vdev
, vdev
->status
)) {
167 ret
= vhost_user_scsi_start(s
, errp
);
173 static void vhost_user_scsi_event(void *opaque
, QEMUChrEvent event
);
175 static void vhost_user_scsi_disconnect(DeviceState
*dev
)
177 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
178 VHostUserSCSI
*s
= VHOST_USER_SCSI(vdev
);
179 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
180 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
185 s
->connected
= false;
187 vhost_user_scsi_stop(s
);
189 vhost_dev_cleanup(&vsc
->dev
);
191 /* Re-instate the event handler for new connections */
192 qemu_chr_fe_set_handlers(&vs
->conf
.chardev
, NULL
, NULL
,
193 vhost_user_scsi_event
, NULL
, dev
, NULL
, true);
196 static void vhost_user_scsi_event(void *opaque
, QEMUChrEvent event
)
198 DeviceState
*dev
= opaque
;
199 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
200 VHostUserSCSI
*s
= VHOST_USER_SCSI(vdev
);
201 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
202 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
203 Error
*local_err
= NULL
;
206 case CHR_EVENT_OPENED
:
207 if (vhost_user_scsi_connect(dev
, &local_err
) < 0) {
208 error_report_err(local_err
);
209 qemu_chr_fe_disconnect(&vs
->conf
.chardev
);
213 case CHR_EVENT_CLOSED
:
214 /* defer close until later to avoid circular close */
215 vhost_user_async_close(dev
, &vs
->conf
.chardev
, &vsc
->dev
,
216 vhost_user_scsi_disconnect
,
217 vhost_user_scsi_event
);
219 case CHR_EVENT_BREAK
:
220 case CHR_EVENT_MUX_IN
:
221 case CHR_EVENT_MUX_OUT
:
227 static int vhost_user_scsi_realize_connect(VHostUserSCSI
*s
, Error
**errp
)
229 DeviceState
*dev
= DEVICE(s
);
230 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
233 s
->connected
= false;
235 ret
= qemu_chr_fe_wait_connected(&vs
->conf
.chardev
, errp
);
240 ret
= vhost_user_scsi_connect(dev
, errp
);
242 qemu_chr_fe_disconnect(&vs
->conf
.chardev
);
245 assert(s
->connected
);
250 static void vhost_user_scsi_realize(DeviceState
*dev
, Error
**errp
)
253 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
254 VHostUserSCSI
*s
= VHOST_USER_SCSI(dev
);
255 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
258 int retries
= VU_REALIZE_CONN_RETRIES
;
260 if (!vs
->conf
.chardev
.chr
) {
261 error_setg(errp
, "vhost-user-scsi: missing chardev");
265 virtio_scsi_common_realize(dev
, vhost_user_scsi_handle_output
,
266 vhost_user_scsi_handle_output
,
267 vhost_user_scsi_handle_output
, &err
);
269 error_propagate(errp
, err
);
273 if (!vhost_user_init(&s
->vhost_user
, &vs
->conf
.chardev
, errp
)) {
277 vsc
->inflight
= g_new0(struct vhost_inflight
, 1);
278 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
,
279 VIRTIO_SCSI_VQ_NUM_FIXED
+ vs
->conf
.num_queues
);
284 error_prepend(errp
, "Reconnecting after error: ");
285 error_report_err(*errp
);
288 ret
= vhost_user_scsi_realize_connect(s
, errp
);
289 } while (ret
< 0 && retries
--);
295 /* we're fully initialized, now we can operate, so add the handler */
296 qemu_chr_fe_set_handlers(&vs
->conf
.chardev
, NULL
, NULL
,
297 vhost_user_scsi_event
, NULL
, (void *)dev
,
299 /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
302 vsc
->target
= vs
->conf
.boot_tpgt
;
307 g_free(s
->vhost_vqs
);
309 g_free(vsc
->inflight
);
310 vsc
->inflight
= NULL
;
311 vhost_user_cleanup(&s
->vhost_user
);
314 virtio_scsi_common_unrealize(dev
);
317 static void vhost_user_scsi_unrealize(DeviceState
*dev
)
319 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
320 VHostUserSCSI
*s
= VHOST_USER_SCSI(dev
);
321 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
322 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
324 /* This will stop the vhost backend. */
325 vhost_user_scsi_set_status(vdev
, 0);
326 qemu_chr_fe_set_handlers(&vs
->conf
.chardev
, NULL
, NULL
, NULL
, NULL
, NULL
,
329 vhost_dev_cleanup(&vsc
->dev
);
330 g_free(s
->vhost_vqs
);
333 vhost_dev_free_inflight(vsc
->inflight
);
334 g_free(vsc
->inflight
);
335 vsc
->inflight
= NULL
;
337 vhost_user_cleanup(&s
->vhost_user
);
338 virtio_scsi_common_unrealize(dev
);
341 static Property vhost_user_scsi_properties
[] = {
342 DEFINE_PROP_CHR("chardev", VirtIOSCSICommon
, conf
.chardev
),
343 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon
, conf
.boot_tpgt
, 0),
344 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon
, conf
.num_queues
,
345 VIRTIO_SCSI_AUTO_NUM_QUEUES
),
346 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon
, conf
.virtqueue_size
,
348 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon
, conf
.max_sectors
,
350 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon
, conf
.cmd_per_lun
, 128),
351 DEFINE_PROP_BIT64("hotplug", VHostSCSICommon
, host_features
,
352 VIRTIO_SCSI_F_HOTPLUG
,
354 DEFINE_PROP_BIT64("param_change", VHostSCSICommon
, host_features
,
355 VIRTIO_SCSI_F_CHANGE
,
357 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon
, host_features
,
358 VIRTIO_SCSI_F_T10_PI
,
360 DEFINE_PROP_END_OF_LIST(),
363 static void vhost_user_scsi_reset(VirtIODevice
*vdev
)
365 VHostUserSCSI
*s
= VHOST_USER_SCSI(vdev
);
366 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(s
);
368 vhost_dev_free_inflight(vsc
->inflight
);
371 static struct vhost_dev
*vhost_user_scsi_get_vhost(VirtIODevice
*vdev
)
373 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(vdev
);
377 static const VMStateDescription vmstate_vhost_scsi
= {
378 .name
= "virtio-scsi",
379 .minimum_version_id
= 1,
381 .fields
= (VMStateField
[]) {
382 VMSTATE_VIRTIO_DEVICE
,
383 VMSTATE_END_OF_LIST()
387 static void vhost_user_scsi_class_init(ObjectClass
*klass
, void *data
)
389 DeviceClass
*dc
= DEVICE_CLASS(klass
);
390 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
391 FWPathProviderClass
*fwc
= FW_PATH_PROVIDER_CLASS(klass
);
393 device_class_set_props(dc
, vhost_user_scsi_properties
);
394 dc
->vmsd
= &vmstate_vhost_scsi
;
395 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
396 vdc
->realize
= vhost_user_scsi_realize
;
397 vdc
->unrealize
= vhost_user_scsi_unrealize
;
398 vdc
->get_features
= vhost_scsi_common_get_features
;
399 vdc
->set_config
= vhost_scsi_common_set_config
;
400 vdc
->set_status
= vhost_user_scsi_set_status
;
401 fwc
->get_dev_path
= vhost_scsi_common_get_fw_dev_path
;
402 vdc
->reset
= vhost_user_scsi_reset
;
403 vdc
->get_vhost
= vhost_user_scsi_get_vhost
;
406 static void vhost_user_scsi_instance_init(Object
*obj
)
408 VHostSCSICommon
*vsc
= VHOST_SCSI_COMMON(obj
);
410 vsc
->feature_bits
= user_feature_bits
;
412 /* Add the bootindex property for this object */
413 device_add_bootindex_property(obj
, &vsc
->bootindex
, "bootindex", NULL
,
417 static const TypeInfo vhost_user_scsi_info
= {
418 .name
= TYPE_VHOST_USER_SCSI
,
419 .parent
= TYPE_VHOST_SCSI_COMMON
,
420 .instance_size
= sizeof(VHostUserSCSI
),
421 .class_init
= vhost_user_scsi_class_init
,
422 .instance_init
= vhost_user_scsi_instance_init
,
423 .interfaces
= (InterfaceInfo
[]) {
424 { TYPE_FW_PATH_PROVIDER
},
429 static void virtio_register_types(void)
431 type_register_static(&vhost_user_scsi_info
);
434 type_init(virtio_register_types
)