xref: /openbmc/qemu/hw/scsi/vhost-scsi-common.c (revision 9d195efd)
1 /*
2  * vhost-scsi-common
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 "qemu/error-report.h"
20 #include "qemu/module.h"
21 #include "hw/virtio/vhost.h"
22 #include "hw/virtio/vhost-scsi-common.h"
23 #include "hw/virtio/virtio-scsi.h"
24 #include "hw/virtio/virtio-bus.h"
25 #include "hw/virtio/virtio-access.h"
26 #include "hw/fw-path-provider.h"
27 
28 int vhost_scsi_common_start(VHostSCSICommon *vsc)
29 {
30     int ret, i;
31     VirtIODevice *vdev = VIRTIO_DEVICE(vsc);
32     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
33     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
34 
35     VirtIOSCSICommon *vs = (VirtIOSCSICommon *)vsc;
36 
37     if (!k->set_guest_notifiers) {
38         error_report("binding does not support guest notifiers");
39         return -ENOSYS;
40     }
41 
42     ret = vhost_dev_enable_notifiers(&vsc->dev, vdev);
43     if (ret < 0) {
44         return ret;
45     }
46 
47     ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, true);
48     if (ret < 0) {
49         error_report("Error binding guest notifier");
50         goto err_host_notifiers;
51     }
52 
53     vsc->dev.acked_features = vdev->guest_features;
54 
55     assert(vsc->inflight == NULL);
56     vsc->inflight = g_new0(struct vhost_inflight, 1);
57     ret = vhost_dev_get_inflight(&vsc->dev,
58                                  vs->conf.virtqueue_size,
59                                  vsc->inflight);
60     if (ret < 0) {
61         error_report("Error get inflight: %d", -ret);
62         goto err_guest_notifiers;
63     }
64 
65     ret = vhost_dev_set_inflight(&vsc->dev, vsc->inflight);
66     if (ret < 0) {
67         error_report("Error set inflight: %d", -ret);
68         goto err_guest_notifiers;
69     }
70 
71     ret = vhost_dev_start(&vsc->dev, vdev, true);
72     if (ret < 0) {
73         error_report("Error start vhost dev");
74         goto err_guest_notifiers;
75     }
76 
77     /* guest_notifier_mask/pending not used yet, so just unmask
78      * everything here.  virtio-pci will do the right thing by
79      * enabling/disabling irqfd.
80      */
81     for (i = 0; i < vsc->dev.nvqs; i++) {
82         vhost_virtqueue_mask(&vsc->dev, vdev, vsc->dev.vq_index + i, false);
83     }
84 
85     return ret;
86 
87 err_guest_notifiers:
88     g_free(vsc->inflight);
89     vsc->inflight = NULL;
90 
91     k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
92 err_host_notifiers:
93     vhost_dev_disable_notifiers(&vsc->dev, vdev);
94     return ret;
95 }
96 
97 void vhost_scsi_common_stop(VHostSCSICommon *vsc)
98 {
99     VirtIODevice *vdev = VIRTIO_DEVICE(vsc);
100     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
101     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
102     int ret = 0;
103 
104     vhost_dev_stop(&vsc->dev, vdev, true);
105 
106     if (k->set_guest_notifiers) {
107         ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
108         if (ret < 0) {
109                 error_report("vhost guest notifier cleanup failed: %d", ret);
110         }
111     }
112     assert(ret >= 0);
113 
114     if (vsc->inflight) {
115         vhost_dev_free_inflight(vsc->inflight);
116         g_free(vsc->inflight);
117         vsc->inflight = NULL;
118     }
119 
120     vhost_dev_disable_notifiers(&vsc->dev, vdev);
121 }
122 
123 uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t features,
124                                         Error **errp)
125 {
126     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
127 
128     /* Turn on predefined features supported by this device */
129     features |= vsc->host_features;
130 
131     return vhost_get_features(&vsc->dev, vsc->feature_bits, features);
132 }
133 
134 void vhost_scsi_common_set_config(VirtIODevice *vdev, const uint8_t *config)
135 {
136     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
137     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
138 
139     if ((uint32_t)virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size ||
140         (uint32_t)virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) {
141         error_report("vhost-scsi does not support changing the sense data and "
142                      "CDB sizes");
143         exit(1);
144     }
145 }
146 
147 /*
148  * Implementation of an interface to adjust firmware path
149  * for the bootindex property handling.
150  */
151 char *vhost_scsi_common_get_fw_dev_path(FWPathProvider *p, BusState *bus,
152                                         DeviceState *dev)
153 {
154     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
155     /* format: /channel@channel/vhost-scsi@target,lun */
156     return g_strdup_printf("/channel@%x/%s@%x,%x", vsc->channel,
157                            qdev_fw_name(dev), vsc->target, vsc->lun);
158 }
159 
160 static const TypeInfo vhost_scsi_common_info = {
161     .name = TYPE_VHOST_SCSI_COMMON,
162     .parent = TYPE_VIRTIO_SCSI_COMMON,
163     .instance_size = sizeof(VHostSCSICommon),
164     .abstract = true,
165 };
166 
167 static void virtio_register_types(void)
168 {
169     type_register_static(&vhost_scsi_common_info);
170 }
171 
172 type_init(virtio_register_types)
173