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