xref: /openbmc/qemu/hw/scsi/vhost-user-scsi.c (revision d5938f29fea29581725426f203a74da746ca03e7)
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 "qom/object.h"
22 #include "hw/fw-path-provider.h"
23 #include "hw/qdev-core.h"
24 #include "hw/qdev-properties.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 "hw/virtio/virtio-access.h"
30 #include "chardev/char-fe.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     VHOST_INVALID_FEATURE_BIT
39 };
40 
41 static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status)
42 {
43     VHostUserSCSI *s = (VHostUserSCSI *)vdev;
44     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
45     bool start = (status & VIRTIO_CONFIG_S_DRIVER_OK) && vdev->vm_running;
46 
47     if (vsc->dev.started == start) {
48         return;
49     }
50 
51     if (start) {
52         int ret;
53 
54         ret = vhost_scsi_common_start(vsc);
55         if (ret < 0) {
56             error_report("unable to start vhost-user-scsi: %s", strerror(-ret));
57             exit(1);
58         }
59     } else {
60         vhost_scsi_common_stop(vsc);
61     }
62 }
63 
64 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
65 {
66 }
67 
68 static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
69 {
70     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
71     VHostUserSCSI *s = VHOST_USER_SCSI(dev);
72     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
73     struct vhost_virtqueue *vqs = NULL;
74     Error *err = NULL;
75     int ret;
76 
77     if (!vs->conf.chardev.chr) {
78         error_setg(errp, "vhost-user-scsi: missing chardev");
79         return;
80     }
81 
82     virtio_scsi_common_realize(dev, vhost_dummy_handle_output,
83                                vhost_dummy_handle_output,
84                                vhost_dummy_handle_output, &err);
85     if (err != NULL) {
86         error_propagate(errp, err);
87         return;
88     }
89 
90     if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
91         goto free_virtio;
92     }
93 
94     vsc->dev.nvqs = 2 + vs->conf.num_queues;
95     vsc->dev.vqs = g_new(struct vhost_virtqueue, vsc->dev.nvqs);
96     vsc->dev.vq_index = 0;
97     vsc->dev.backend_features = 0;
98     vqs = vsc->dev.vqs;
99 
100     ret = vhost_dev_init(&vsc->dev, &s->vhost_user,
101                          VHOST_BACKEND_TYPE_USER, 0);
102     if (ret < 0) {
103         error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s",
104                    strerror(-ret));
105         goto free_vhost;
106     }
107 
108     /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
109     vsc->channel = 0;
110     vsc->lun = 0;
111     vsc->target = vs->conf.boot_tpgt;
112 
113     return;
114 
115 free_vhost:
116     vhost_user_cleanup(&s->vhost_user);
117     g_free(vqs);
118 free_virtio:
119     virtio_scsi_common_unrealize(dev);
120 }
121 
122 static void vhost_user_scsi_unrealize(DeviceState *dev, Error **errp)
123 {
124     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
125     VHostUserSCSI *s = VHOST_USER_SCSI(dev);
126     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
127     struct vhost_virtqueue *vqs = vsc->dev.vqs;
128 
129     /* This will stop the vhost backend. */
130     vhost_user_scsi_set_status(vdev, 0);
131 
132     vhost_dev_cleanup(&vsc->dev);
133     g_free(vqs);
134 
135     virtio_scsi_common_unrealize(dev);
136     vhost_user_cleanup(&s->vhost_user);
137 }
138 
139 static Property vhost_user_scsi_properties[] = {
140     DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev),
141     DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
142     DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues, 1),
143     DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
144                        128),
145     DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
146                        0xFFFF),
147     DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
148     DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features,
149                                                   VIRTIO_SCSI_F_HOTPLUG,
150                                                   true),
151     DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features,
152                                                        VIRTIO_SCSI_F_CHANGE,
153                                                        true),
154     DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
155                                                  VIRTIO_SCSI_F_T10_PI,
156                                                  false),
157     DEFINE_PROP_END_OF_LIST(),
158 };
159 
160 static const VMStateDescription vmstate_vhost_scsi = {
161     .name = "virtio-scsi",
162     .minimum_version_id = 1,
163     .version_id = 1,
164     .fields = (VMStateField[]) {
165         VMSTATE_VIRTIO_DEVICE,
166         VMSTATE_END_OF_LIST()
167     },
168 };
169 
170 static void vhost_user_scsi_class_init(ObjectClass *klass, void *data)
171 {
172     DeviceClass *dc = DEVICE_CLASS(klass);
173     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
174     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
175 
176     dc->props = vhost_user_scsi_properties;
177     dc->vmsd = &vmstate_vhost_scsi;
178     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
179     vdc->realize = vhost_user_scsi_realize;
180     vdc->unrealize = vhost_user_scsi_unrealize;
181     vdc->get_features = vhost_scsi_common_get_features;
182     vdc->set_config = vhost_scsi_common_set_config;
183     vdc->set_status = vhost_user_scsi_set_status;
184     fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
185 }
186 
187 static void vhost_user_scsi_instance_init(Object *obj)
188 {
189     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
190 
191     vsc->feature_bits = user_feature_bits;
192 
193     /* Add the bootindex property for this object */
194     device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
195                                   DEVICE(vsc), NULL);
196 }
197 
198 static const TypeInfo vhost_user_scsi_info = {
199     .name = TYPE_VHOST_USER_SCSI,
200     .parent = TYPE_VHOST_SCSI_COMMON,
201     .instance_size = sizeof(VHostUserSCSI),
202     .class_init = vhost_user_scsi_class_init,
203     .instance_init = vhost_user_scsi_instance_init,
204     .interfaces = (InterfaceInfo[]) {
205         { TYPE_FW_PATH_PROVIDER },
206         { }
207     },
208 };
209 
210 static void virtio_register_types(void)
211 {
212     type_register_static(&vhost_user_scsi_info);
213 }
214 
215 type_init(virtio_register_types)
216