Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 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" |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 21 | #include "hw/fw-path-provider.h" |
| 22 | #include "hw/qdev-core.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 23 | #include "hw/qdev-properties.h" |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 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" |
Markus Armbruster | 2f780b6 | 2019-08-12 07:23:58 +0200 | [diff] [blame] | 30 | #include "sysemu/sysemu.h" |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 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 | |
Raphael Norwitz | f047243 | 2019-10-29 17:38:03 -0400 | [diff] [blame] | 41 | enum VhostUserProtocolFeature { |
| 42 | VHOST_USER_PROTOCOL_F_RESET_DEVICE = 13, |
| 43 | }; |
| 44 | |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 45 | static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status) |
| 46 | { |
| 47 | VHostUserSCSI *s = (VHostUserSCSI *)vdev; |
| 48 | VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); |
| 49 | bool start = (status & VIRTIO_CONFIG_S_DRIVER_OK) && vdev->vm_running; |
| 50 | |
| 51 | if (vsc->dev.started == start) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if (start) { |
| 56 | int ret; |
| 57 | |
| 58 | ret = vhost_scsi_common_start(vsc); |
| 59 | if (ret < 0) { |
| 60 | error_report("unable to start vhost-user-scsi: %s", strerror(-ret)); |
| 61 | exit(1); |
| 62 | } |
| 63 | } else { |
| 64 | vhost_scsi_common_stop(vsc); |
| 65 | } |
| 66 | } |
| 67 | |
Raphael Norwitz | f047243 | 2019-10-29 17:38:03 -0400 | [diff] [blame] | 68 | static void vhost_user_scsi_reset(VirtIODevice *vdev) |
| 69 | { |
| 70 | VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev); |
| 71 | struct vhost_dev *dev = &vsc->dev; |
| 72 | |
| 73 | /* |
| 74 | * Historically, reset was not implemented so only reset devices |
| 75 | * that are expecting it. |
| 76 | */ |
| 77 | if (!virtio_has_feature(dev->protocol_features, |
| 78 | VHOST_USER_PROTOCOL_F_RESET_DEVICE)) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (dev->vhost_ops->vhost_reset_device) { |
| 83 | dev->vhost_ops->vhost_reset_device(dev); |
| 84 | } |
| 85 | } |
| 86 | |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 87 | static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | static void vhost_user_scsi_realize(DeviceState *dev, Error **errp) |
| 92 | { |
| 93 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); |
| 94 | VHostUserSCSI *s = VHOST_USER_SCSI(dev); |
| 95 | VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); |
Jie Wang | 386cff4 | 2019-04-30 15:15:00 +0800 | [diff] [blame] | 96 | struct vhost_virtqueue *vqs = NULL; |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 97 | Error *err = NULL; |
| 98 | int ret; |
| 99 | |
| 100 | if (!vs->conf.chardev.chr) { |
| 101 | error_setg(errp, "vhost-user-scsi: missing chardev"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | virtio_scsi_common_realize(dev, vhost_dummy_handle_output, |
| 106 | vhost_dummy_handle_output, |
| 107 | vhost_dummy_handle_output, &err); |
| 108 | if (err != NULL) { |
| 109 | error_propagate(errp, err); |
| 110 | return; |
| 111 | } |
| 112 | |
Marc-André Lureau | 0b99f22 | 2019-03-08 15:04:45 +0100 | [diff] [blame] | 113 | if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) { |
Xie Yongji | 68fa7ca | 2019-07-17 08:46:06 +0800 | [diff] [blame] | 114 | goto free_virtio; |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 115 | } |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 116 | |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 117 | vsc->dev.nvqs = 2 + vs->conf.num_queues; |
Raphael Norwitz | 5d4c1ed | 2019-06-11 17:35:17 -0700 | [diff] [blame] | 118 | vsc->dev.vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs); |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 119 | vsc->dev.vq_index = 0; |
| 120 | vsc->dev.backend_features = 0; |
Jie Wang | 386cff4 | 2019-04-30 15:15:00 +0800 | [diff] [blame] | 121 | vqs = vsc->dev.vqs; |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 122 | |
Marc-André Lureau | 0b99f22 | 2019-03-08 15:04:45 +0100 | [diff] [blame] | 123 | ret = vhost_dev_init(&vsc->dev, &s->vhost_user, |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 124 | VHOST_BACKEND_TYPE_USER, 0); |
| 125 | if (ret < 0) { |
| 126 | error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s", |
| 127 | strerror(-ret)); |
Xie Yongji | 68fa7ca | 2019-07-17 08:46:06 +0800 | [diff] [blame] | 128 | goto free_vhost; |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Channel and lun both are 0 for bootable vhost-user-scsi disk */ |
| 132 | vsc->channel = 0; |
| 133 | vsc->lun = 0; |
| 134 | vsc->target = vs->conf.boot_tpgt; |
Xie Yongji | 68fa7ca | 2019-07-17 08:46:06 +0800 | [diff] [blame] | 135 | |
| 136 | return; |
| 137 | |
| 138 | free_vhost: |
| 139 | vhost_user_cleanup(&s->vhost_user); |
| 140 | g_free(vqs); |
| 141 | free_virtio: |
| 142 | virtio_scsi_common_unrealize(dev); |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 145 | static void vhost_user_scsi_unrealize(DeviceState *dev) |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 146 | { |
| 147 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 148 | VHostUserSCSI *s = VHOST_USER_SCSI(dev); |
| 149 | VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); |
Jian Wang | a5390d9 | 2018-12-22 18:27:28 +0800 | [diff] [blame] | 150 | struct vhost_virtqueue *vqs = vsc->dev.vqs; |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 151 | |
| 152 | /* This will stop the vhost backend. */ |
| 153 | vhost_user_scsi_set_status(vdev, 0); |
| 154 | |
| 155 | vhost_dev_cleanup(&vsc->dev); |
Jian Wang | a5390d9 | 2018-12-22 18:27:28 +0800 | [diff] [blame] | 156 | g_free(vqs); |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 157 | |
Paolo Bonzini | 12e1dc4 | 2019-07-17 11:46:50 +0200 | [diff] [blame] | 158 | virtio_scsi_common_unrealize(dev); |
Marc-André Lureau | 0b99f22 | 2019-03-08 15:04:45 +0100 | [diff] [blame] | 159 | vhost_user_cleanup(&s->vhost_user); |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 162 | static Property vhost_user_scsi_properties[] = { |
| 163 | DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev), |
| 164 | DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0), |
| 165 | DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues, 1), |
Dariusz Stojaczyk | 9200361 | 2017-11-14 17:28:36 +0100 | [diff] [blame] | 166 | DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size, |
| 167 | 128), |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 168 | DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors, |
| 169 | 0xFFFF), |
| 170 | DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128), |
Greg Edwards | eb5757f | 2018-08-08 13:52:33 -0600 | [diff] [blame] | 171 | DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features, |
| 172 | VIRTIO_SCSI_F_HOTPLUG, |
| 173 | true), |
| 174 | DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features, |
| 175 | VIRTIO_SCSI_F_CHANGE, |
| 176 | true), |
Greg Edwards | f287fdd | 2018-08-08 13:52:35 -0600 | [diff] [blame] | 177 | DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features, |
| 178 | VIRTIO_SCSI_F_T10_PI, |
| 179 | false), |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 180 | DEFINE_PROP_END_OF_LIST(), |
| 181 | }; |
| 182 | |
| 183 | static const VMStateDescription vmstate_vhost_scsi = { |
| 184 | .name = "virtio-scsi", |
| 185 | .minimum_version_id = 1, |
| 186 | .version_id = 1, |
| 187 | .fields = (VMStateField[]) { |
| 188 | VMSTATE_VIRTIO_DEVICE, |
| 189 | VMSTATE_END_OF_LIST() |
| 190 | }, |
| 191 | }; |
| 192 | |
| 193 | static void vhost_user_scsi_class_init(ObjectClass *klass, void *data) |
| 194 | { |
| 195 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 196 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
| 197 | FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass); |
| 198 | |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 199 | device_class_set_props(dc, vhost_user_scsi_properties); |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 200 | dc->vmsd = &vmstate_vhost_scsi; |
| 201 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
| 202 | vdc->realize = vhost_user_scsi_realize; |
| 203 | vdc->unrealize = vhost_user_scsi_unrealize; |
Greg Edwards | b1110d8 | 2018-08-08 13:52:34 -0600 | [diff] [blame] | 204 | vdc->get_features = vhost_scsi_common_get_features; |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 205 | vdc->set_config = vhost_scsi_common_set_config; |
| 206 | vdc->set_status = vhost_user_scsi_set_status; |
Raphael Norwitz | f047243 | 2019-10-29 17:38:03 -0400 | [diff] [blame] | 207 | vdc->reset = vhost_user_scsi_reset; |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 208 | fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path; |
| 209 | } |
| 210 | |
| 211 | static void vhost_user_scsi_instance_init(Object *obj) |
| 212 | { |
| 213 | VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj); |
| 214 | |
| 215 | vsc->feature_bits = user_feature_bits; |
| 216 | |
| 217 | /* Add the bootindex property for this object */ |
| 218 | device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL, |
Markus Armbruster | 40c2281 | 2020-05-05 17:29:23 +0200 | [diff] [blame] | 219 | DEVICE(vsc)); |
Felipe Franciosi | f12c1eb | 2017-03-02 10:25:52 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static const TypeInfo vhost_user_scsi_info = { |
| 223 | .name = TYPE_VHOST_USER_SCSI, |
| 224 | .parent = TYPE_VHOST_SCSI_COMMON, |
| 225 | .instance_size = sizeof(VHostUserSCSI), |
| 226 | .class_init = vhost_user_scsi_class_init, |
| 227 | .instance_init = vhost_user_scsi_instance_init, |
| 228 | .interfaces = (InterfaceInfo[]) { |
| 229 | { TYPE_FW_PATH_PROVIDER }, |
| 230 | { } |
| 231 | }, |
| 232 | }; |
| 233 | |
| 234 | static void virtio_register_types(void) |
| 235 | { |
| 236 | type_register_static(&vhost_user_scsi_info); |
| 237 | } |
| 238 | |
| 239 | type_init(virtio_register_types) |