]>
Commit | Line | Data |
---|---|---|
5e9be92d NB |
1 | /* |
2 | * vhost_scsi host device | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
8 | * | |
9 | * Changes for QEMU mainline + tcm_vhost kernel upstream: | |
10 | * Nicholas Bellinger <[email protected]> | |
11 | * | |
12 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
13 | * See the COPYING.LIB file in the top-level directory. | |
14 | * | |
15 | */ | |
16 | ||
a4ab4792 | 17 | #include "qemu/osdep.h" |
a9c94277 | 18 | #include <linux/vhost.h> |
5e9be92d | 19 | #include <sys/ioctl.h> |
a9c94277 | 20 | #include "qapi/error.h" |
d49b6836 | 21 | #include "qemu/error-report.h" |
5e9be92d NB |
22 | #include "qemu/queue.h" |
23 | #include "monitor/monitor.h" | |
24 | #include "migration/migration.h" | |
25 | #include "hw/virtio/vhost-scsi.h" | |
26 | #include "hw/virtio/vhost.h" | |
27 | #include "hw/virtio/virtio-scsi.h" | |
1c819449 | 28 | #include "hw/virtio/virtio-bus.h" |
7ce04255 | 29 | #include "hw/virtio/virtio-access.h" |
1956cf6f | 30 | #include "hw/fw-path-provider.h" |
f348b6d1 | 31 | #include "qemu/cutils.h" |
5e9be92d | 32 | |
2e6d46d7 NN |
33 | /* Features supported by host kernel. */ |
34 | static const int kernel_feature_bits[] = { | |
35 | VIRTIO_F_NOTIFY_ON_EMPTY, | |
36 | VIRTIO_RING_F_INDIRECT_DESC, | |
37 | VIRTIO_RING_F_EVENT_IDX, | |
38 | VIRTIO_SCSI_F_HOTPLUG, | |
39 | VHOST_INVALID_FEATURE_BIT | |
40 | }; | |
41 | ||
5e9be92d NB |
42 | static int vhost_scsi_set_endpoint(VHostSCSI *s) |
43 | { | |
44 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); | |
24d1eb33 | 45 | const VhostOps *vhost_ops = s->dev.vhost_ops; |
5e9be92d NB |
46 | struct vhost_scsi_target backend; |
47 | int ret; | |
48 | ||
49 | memset(&backend, 0, sizeof(backend)); | |
50 | pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); | |
21e70425 | 51 | ret = vhost_ops->vhost_scsi_set_endpoint(&s->dev, &backend); |
5e9be92d NB |
52 | if (ret < 0) { |
53 | return -errno; | |
54 | } | |
55 | return 0; | |
56 | } | |
57 | ||
58 | static void vhost_scsi_clear_endpoint(VHostSCSI *s) | |
59 | { | |
60 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); | |
61 | struct vhost_scsi_target backend; | |
24d1eb33 | 62 | const VhostOps *vhost_ops = s->dev.vhost_ops; |
5e9be92d NB |
63 | |
64 | memset(&backend, 0, sizeof(backend)); | |
65 | pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); | |
21e70425 | 66 | vhost_ops->vhost_scsi_clear_endpoint(&s->dev, &backend); |
5e9be92d NB |
67 | } |
68 | ||
69 | static int vhost_scsi_start(VHostSCSI *s) | |
70 | { | |
71 | int ret, abi_version, i; | |
72 | VirtIODevice *vdev = VIRTIO_DEVICE(s); | |
1c819449 FK |
73 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
74 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
24d1eb33 | 75 | const VhostOps *vhost_ops = s->dev.vhost_ops; |
5e9be92d | 76 | |
1c819449 | 77 | if (!k->set_guest_notifiers) { |
5e9be92d NB |
78 | error_report("binding does not support guest notifiers"); |
79 | return -ENOSYS; | |
80 | } | |
81 | ||
21e70425 | 82 | ret = vhost_ops->vhost_scsi_get_abi_version(&s->dev, &abi_version); |
5e9be92d NB |
83 | if (ret < 0) { |
84 | return -errno; | |
85 | } | |
86 | if (abi_version > VHOST_SCSI_ABI_VERSION) { | |
87 | error_report("vhost-scsi: The running tcm_vhost kernel abi_version:" | |
88 | " %d is greater than vhost_scsi userspace supports: %d, please" | |
f6a16175 | 89 | " upgrade your version of QEMU", abi_version, |
5e9be92d NB |
90 | VHOST_SCSI_ABI_VERSION); |
91 | return -ENOSYS; | |
92 | } | |
93 | ||
94 | ret = vhost_dev_enable_notifiers(&s->dev, vdev); | |
95 | if (ret < 0) { | |
96 | return ret; | |
97 | } | |
98 | ||
99 | s->dev.acked_features = vdev->guest_features; | |
100 | ret = vhost_dev_start(&s->dev, vdev); | |
101 | if (ret < 0) { | |
102 | error_report("Error start vhost dev"); | |
103 | goto err_notifiers; | |
104 | } | |
105 | ||
106 | ret = vhost_scsi_set_endpoint(s); | |
107 | if (ret < 0) { | |
108 | error_report("Error set vhost-scsi endpoint"); | |
109 | goto err_vhost_stop; | |
110 | } | |
111 | ||
1c819449 | 112 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); |
5e9be92d NB |
113 | if (ret < 0) { |
114 | error_report("Error binding guest notifier"); | |
115 | goto err_endpoint; | |
116 | } | |
117 | ||
118 | /* guest_notifier_mask/pending not used yet, so just unmask | |
119 | * everything here. virtio-pci will do the right thing by | |
120 | * enabling/disabling irqfd. | |
121 | */ | |
122 | for (i = 0; i < s->dev.nvqs; i++) { | |
fe2d1a81 | 123 | vhost_virtqueue_mask(&s->dev, vdev, s->dev.vq_index + i, false); |
5e9be92d NB |
124 | } |
125 | ||
126 | return ret; | |
127 | ||
128 | err_endpoint: | |
129 | vhost_scsi_clear_endpoint(s); | |
130 | err_vhost_stop: | |
131 | vhost_dev_stop(&s->dev, vdev); | |
132 | err_notifiers: | |
133 | vhost_dev_disable_notifiers(&s->dev, vdev); | |
134 | return ret; | |
135 | } | |
136 | ||
137 | static void vhost_scsi_stop(VHostSCSI *s) | |
138 | { | |
139 | VirtIODevice *vdev = VIRTIO_DEVICE(s); | |
1c819449 FK |
140 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
141 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
5e9be92d NB |
142 | int ret = 0; |
143 | ||
0e22a2d1 | 144 | if (k->set_guest_notifiers) { |
1c819449 | 145 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
5e9be92d | 146 | if (ret < 0) { |
f6a16175 | 147 | error_report("vhost guest notifier cleanup failed: %d", ret); |
5e9be92d NB |
148 | } |
149 | } | |
150 | assert(ret >= 0); | |
151 | ||
152 | vhost_scsi_clear_endpoint(s); | |
153 | vhost_dev_stop(&s->dev, vdev); | |
154 | vhost_dev_disable_notifiers(&s->dev, vdev); | |
155 | } | |
156 | ||
019a3edb | 157 | static uint64_t vhost_scsi_get_features(VirtIODevice *vdev, |
9d5b731d JW |
158 | uint64_t features, |
159 | Error **errp) | |
5e9be92d NB |
160 | { |
161 | VHostSCSI *s = VHOST_SCSI(vdev); | |
162 | ||
2e6d46d7 | 163 | return vhost_get_features(&s->dev, kernel_feature_bits, features); |
5e9be92d NB |
164 | } |
165 | ||
166 | static void vhost_scsi_set_config(VirtIODevice *vdev, | |
167 | const uint8_t *config) | |
168 | { | |
169 | VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; | |
170 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); | |
171 | ||
7ce04255 PB |
172 | if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size || |
173 | (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) { | |
5e9be92d NB |
174 | error_report("vhost-scsi does not support changing the sense data and CDB sizes"); |
175 | exit(1); | |
176 | } | |
177 | } | |
178 | ||
179 | static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val) | |
180 | { | |
181 | VHostSCSI *s = (VHostSCSI *)vdev; | |
182 | bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK); | |
183 | ||
184 | if (s->dev.started == start) { | |
185 | return; | |
186 | } | |
187 | ||
188 | if (start) { | |
189 | int ret; | |
190 | ||
191 | ret = vhost_scsi_start(s); | |
192 | if (ret < 0) { | |
f6a16175 | 193 | error_report("virtio-scsi: unable to start vhost: %s", |
5e9be92d NB |
194 | strerror(-ret)); |
195 | ||
196 | /* There is no userspace virtio-scsi fallback so exit */ | |
197 | exit(1); | |
198 | } | |
199 | } else { | |
200 | vhost_scsi_stop(s); | |
201 | } | |
202 | } | |
203 | ||
91d670fb ML |
204 | static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
205 | { | |
206 | } | |
207 | ||
71a6520b | 208 | static void vhost_scsi_realize(DeviceState *dev, Error **errp) |
5e9be92d | 209 | { |
71a6520b AF |
210 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); |
211 | VHostSCSI *s = VHOST_SCSI(dev); | |
212 | Error *err = NULL; | |
5e9be92d NB |
213 | int vhostfd = -1; |
214 | int ret; | |
215 | ||
216 | if (!vs->conf.wwpn) { | |
71a6520b AF |
217 | error_setg(errp, "vhost-scsi: missing wwpn"); |
218 | return; | |
5e9be92d NB |
219 | } |
220 | ||
221 | if (vs->conf.vhostfd) { | |
e43bfd9c | 222 | vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, errp); |
5e9be92d | 223 | if (vhostfd == -1) { |
e43bfd9c | 224 | error_prepend(errp, "vhost-scsi: unable to parse vhostfd: "); |
71a6520b | 225 | return; |
5e9be92d | 226 | } |
81647a65 NN |
227 | } else { |
228 | vhostfd = open("/dev/vhost-scsi", O_RDWR); | |
229 | if (vhostfd < 0) { | |
230 | error_setg(errp, "vhost-scsi: open vhost char device failed: %s", | |
231 | strerror(errno)); | |
232 | return; | |
233 | } | |
5e9be92d NB |
234 | } |
235 | ||
91d670fb ML |
236 | virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output, |
237 | vhost_dummy_handle_output, | |
238 | vhost_dummy_handle_output); | |
71a6520b AF |
239 | if (err != NULL) { |
240 | error_propagate(errp, err); | |
b19ca188 | 241 | close(vhostfd); |
71a6520b | 242 | return; |
5e9be92d NB |
243 | } |
244 | ||
5e9be92d NB |
245 | s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; |
246 | s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs); | |
247 | s->dev.vq_index = 0; | |
3a1655fc | 248 | s->dev.backend_features = 0; |
5e9be92d | 249 | |
81647a65 | 250 | ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd, |
69e87b32 | 251 | VHOST_BACKEND_TYPE_KERNEL, 0); |
5e9be92d | 252 | if (ret < 0) { |
71a6520b AF |
253 | error_setg(errp, "vhost-scsi: vhost initialization failed: %s", |
254 | strerror(-ret)); | |
255 | return; | |
5e9be92d | 256 | } |
5e9be92d | 257 | |
444c7e0d GA |
258 | /* At present, channel and lun both are 0 for bootable vhost-scsi disk */ |
259 | s->channel = 0; | |
260 | s->lun = 0; | |
261 | /* Note: we can also get the minimum tpgt from kernel */ | |
262 | s->target = vs->conf.boot_tpgt; | |
263 | ||
5e9be92d NB |
264 | error_setg(&s->migration_blocker, |
265 | "vhost-scsi does not support migration"); | |
266 | migrate_add_blocker(s->migration_blocker); | |
5e9be92d NB |
267 | } |
268 | ||
306ec6c3 | 269 | static void vhost_scsi_unrealize(DeviceState *dev, Error **errp) |
5e9be92d | 270 | { |
306ec6c3 AF |
271 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
272 | VHostSCSI *s = VHOST_SCSI(dev); | |
5e9be92d NB |
273 | |
274 | migrate_del_blocker(s->migration_blocker); | |
275 | error_free(s->migration_blocker); | |
276 | ||
277 | /* This will stop vhost backend. */ | |
278 | vhost_scsi_set_status(vdev, 0); | |
279 | ||
af103c93 | 280 | vhost_dev_cleanup(&s->dev); |
5e9be92d | 281 | g_free(s->dev.vqs); |
306ec6c3 AF |
282 | |
283 | virtio_scsi_common_unrealize(dev, errp); | |
5e9be92d NB |
284 | } |
285 | ||
1956cf6f GA |
286 | /* |
287 | * Implementation of an interface to adjust firmware path | |
288 | * for the bootindex property handling. | |
289 | */ | |
290 | static char *vhost_scsi_get_fw_dev_path(FWPathProvider *p, BusState *bus, | |
291 | DeviceState *dev) | |
292 | { | |
293 | VHostSCSI *s = VHOST_SCSI(dev); | |
294 | /* format: channel@channel/vhost-scsi@target,lun */ | |
f42bf6a2 | 295 | return g_strdup_printf("/channel@%x/%s@%x,%x", s->channel, |
1956cf6f GA |
296 | qdev_fw_name(dev), s->target, s->lun); |
297 | } | |
298 | ||
5e9be92d | 299 | static Property vhost_scsi_properties[] = { |
21549a46 SZ |
300 | DEFINE_PROP_STRING("vhostfd", VHostSCSI, parent_obj.conf.vhostfd), |
301 | DEFINE_PROP_STRING("wwpn", VHostSCSI, parent_obj.conf.wwpn), | |
302 | DEFINE_PROP_UINT32("boot_tpgt", VHostSCSI, parent_obj.conf.boot_tpgt, 0), | |
303 | DEFINE_PROP_UINT32("num_queues", VHostSCSI, parent_obj.conf.num_queues, 1), | |
304 | DEFINE_PROP_UINT32("max_sectors", VHostSCSI, parent_obj.conf.max_sectors, | |
305 | 0xFFFF), | |
306 | DEFINE_PROP_UINT32("cmd_per_lun", VHostSCSI, parent_obj.conf.cmd_per_lun, | |
307 | 128), | |
5e9be92d NB |
308 | DEFINE_PROP_END_OF_LIST(), |
309 | }; | |
310 | ||
311 | static void vhost_scsi_class_init(ObjectClass *klass, void *data) | |
312 | { | |
313 | DeviceClass *dc = DEVICE_CLASS(klass); | |
314 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
1956cf6f | 315 | FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass); |
71a6520b | 316 | |
5e9be92d | 317 | dc->props = vhost_scsi_properties; |
125ee0ed | 318 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
71a6520b | 319 | vdc->realize = vhost_scsi_realize; |
306ec6c3 | 320 | vdc->unrealize = vhost_scsi_unrealize; |
5e9be92d NB |
321 | vdc->get_features = vhost_scsi_get_features; |
322 | vdc->set_config = vhost_scsi_set_config; | |
323 | vdc->set_status = vhost_scsi_set_status; | |
1956cf6f | 324 | fwc->get_dev_path = vhost_scsi_get_fw_dev_path; |
5e9be92d NB |
325 | } |
326 | ||
d4433f32 GA |
327 | static void vhost_scsi_instance_init(Object *obj) |
328 | { | |
329 | VHostSCSI *dev = VHOST_SCSI(obj); | |
330 | ||
331 | device_add_bootindex_property(obj, &dev->bootindex, "bootindex", NULL, | |
332 | DEVICE(dev), NULL); | |
333 | } | |
334 | ||
5e9be92d NB |
335 | static const TypeInfo vhost_scsi_info = { |
336 | .name = TYPE_VHOST_SCSI, | |
337 | .parent = TYPE_VIRTIO_SCSI_COMMON, | |
338 | .instance_size = sizeof(VHostSCSI), | |
339 | .class_init = vhost_scsi_class_init, | |
d4433f32 | 340 | .instance_init = vhost_scsi_instance_init, |
1956cf6f GA |
341 | .interfaces = (InterfaceInfo[]) { |
342 | { TYPE_FW_PATH_PROVIDER }, | |
343 | { } | |
344 | }, | |
5e9be92d NB |
345 | }; |
346 | ||
347 | static void virtio_register_types(void) | |
348 | { | |
349 | type_register_static(&vhost_scsi_info); | |
350 | } | |
351 | ||
352 | type_init(virtio_register_types) |