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