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