]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio-bus.c
virtio-bus: Plug devices after features are negotiated
[mirror_qemu.git] / hw / virtio / virtio-bus.c
CommitLineData
ff8eca55
FK
1/*
2 * VirtioBus
3 *
4 * Copyright (C) 2012 : GreenSocs Ltd
5 * http://www.greensocs.com/ , email: info@greensocs.com
6 *
7 * Developed by :
8 * Frederic Konrad <fred.konrad@greensocs.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
9b8bfe21 25#include "qemu/osdep.h"
83c9f4ca 26#include "hw/hw.h"
ff8eca55 27#include "qemu/error-report.h"
83c9f4ca 28#include "hw/qdev.h"
0d09e41a
PB
29#include "hw/virtio/virtio-bus.h"
30#include "hw/virtio/virtio.h"
ff8eca55
FK
31
32/* #define DEBUG_VIRTIO_BUS */
33
34#ifdef DEBUG_VIRTIO_BUS
35#define DPRINTF(fmt, ...) \
36do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
37#else
38#define DPRINTF(fmt, ...) do { } while (0)
39#endif
40
5e96f5d2 41/* A VirtIODevice is being plugged */
e8398045 42void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
ff8eca55
FK
43{
44 DeviceState *qdev = DEVICE(vdev);
45 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
46 VirtioBusState *bus = VIRTIO_BUS(qbus);
47 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
6b8f1020
CH
48 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
49
ff8eca55
FK
50 DPRINTF("%s: plug device.\n", qbus->name);
51
d1b4259f
MC
52 if (klass->pre_plugged != NULL) {
53 klass->pre_plugged(qbus->parent, errp);
ff8eca55
FK
54 }
55
6b8f1020
CH
56 /* Get the features of the plugged device. */
57 assert(vdc->get_features != NULL);
9d5b731d
JW
58 vdev->host_features = vdc->get_features(vdev, vdev->host_features,
59 errp);
d1b4259f
MC
60
61 if (klass->device_plugged != NULL) {
62 klass->device_plugged(qbus->parent, errp);
11380b36 63 }
ff8eca55
FK
64}
65
66/* Reset the virtio_bus */
67void virtio_bus_reset(VirtioBusState *bus)
68{
06d3dff0
PB
69 VirtIODevice *vdev = virtio_bus_get_device(bus);
70
2c80ab15 71 DPRINTF("%s: reset device.\n", BUS(bus)->name);
06d3dff0
PB
72 if (vdev != NULL) {
73 virtio_reset(vdev);
ff8eca55
FK
74 }
75}
76
5e96f5d2
PB
77/* A VirtIODevice is being unplugged */
78void virtio_bus_device_unplugged(VirtIODevice *vdev)
ff8eca55 79{
5e96f5d2
PB
80 DeviceState *qdev = DEVICE(vdev);
81 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
82 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
06d3dff0 83
ff8eca55
FK
84 DPRINTF("%s: remove device.\n", qbus->name);
85
06d3dff0 86 if (vdev != NULL) {
5e96f5d2
PB
87 if (klass->device_unplugged != NULL) {
88 klass->device_unplugged(qbus->parent);
ff8eca55 89 }
ff8eca55
FK
90 }
91}
92
93/* Get the device id of the plugged device. */
94uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
95{
06d3dff0
PB
96 VirtIODevice *vdev = virtio_bus_get_device(bus);
97 assert(vdev != NULL);
98 return vdev->device_id;
ff8eca55
FK
99}
100
101/* Get the config_len field of the plugged device. */
102size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
103{
06d3dff0
PB
104 VirtIODevice *vdev = virtio_bus_get_device(bus);
105 assert(vdev != NULL);
106 return vdev->config_len;
ff8eca55
FK
107}
108
8e05db92
FK
109/* Get bad features of the plugged device. */
110uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
111{
06d3dff0 112 VirtIODevice *vdev = virtio_bus_get_device(bus);
8e05db92 113 VirtioDeviceClass *k;
06d3dff0
PB
114
115 assert(vdev != NULL);
116 k = VIRTIO_DEVICE_GET_CLASS(vdev);
8e05db92 117 if (k->bad_features != NULL) {
06d3dff0 118 return k->bad_features(vdev);
8e05db92
FK
119 } else {
120 return 0;
121 }
122}
123
124/* Get config of the plugged device. */
125void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
126{
06d3dff0 127 VirtIODevice *vdev = virtio_bus_get_device(bus);
8e05db92 128 VirtioDeviceClass *k;
06d3dff0
PB
129
130 assert(vdev != NULL);
131 k = VIRTIO_DEVICE_GET_CLASS(vdev);
8e05db92 132 if (k->get_config != NULL) {
06d3dff0 133 k->get_config(vdev, config);
8e05db92
FK
134 }
135}
136
5d448f9d
FK
137/* Set config of the plugged device. */
138void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
139{
06d3dff0 140 VirtIODevice *vdev = virtio_bus_get_device(bus);
5d448f9d 141 VirtioDeviceClass *k;
06d3dff0
PB
142
143 assert(vdev != NULL);
144 k = VIRTIO_DEVICE_GET_CLASS(vdev);
5d448f9d 145 if (k->set_config != NULL) {
06d3dff0 146 k->set_config(vdev, config);
5d448f9d
FK
147 }
148}
149
6798e245
CH
150/*
151 * This function handles both assigning the ioeventfd handler and
152 * registering it with the kernel.
153 * assign: register/deregister ioeventfd with the kernel
154 * set_handler: use the generic ioeventfd handler
155 */
156static int set_host_notifier_internal(DeviceState *proxy, VirtioBusState *bus,
157 int n, bool assign, bool set_handler)
158{
159 VirtIODevice *vdev = virtio_bus_get_device(bus);
160 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
161 VirtQueue *vq = virtio_get_queue(vdev, n);
162 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
163 int r = 0;
164
165 if (assign) {
166 r = event_notifier_init(notifier, 1);
167 if (r < 0) {
a8bba0ad
TH
168 error_report("%s: unable to init event notifier: %s (%d)",
169 __func__, strerror(-r), r);
6798e245
CH
170 return r;
171 }
172 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
173 r = k->ioeventfd_assign(proxy, notifier, n, assign);
174 if (r < 0) {
175 error_report("%s: unable to assign ioeventfd: %d", __func__, r);
176 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
177 event_notifier_cleanup(notifier);
178 return r;
179 }
180 } else {
6798e245 181 k->ioeventfd_assign(proxy, notifier, n, assign);
0830c96d 182 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
6798e245
CH
183 event_notifier_cleanup(notifier);
184 }
185 return r;
186}
187
188void virtio_bus_start_ioeventfd(VirtioBusState *bus)
189{
190 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
191 DeviceState *proxy = DEVICE(BUS(bus)->parent);
192 VirtIODevice *vdev;
193 int n, r;
194
195 if (!k->ioeventfd_started || k->ioeventfd_started(proxy)) {
196 return;
197 }
198 if (k->ioeventfd_disabled(proxy)) {
199 return;
200 }
201 vdev = virtio_bus_get_device(bus);
202 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
203 if (!virtio_queue_get_num(vdev, n)) {
204 continue;
205 }
206 r = set_host_notifier_internal(proxy, bus, n, true, true);
207 if (r < 0) {
208 goto assign_error;
209 }
210 }
211 k->ioeventfd_set_started(proxy, true, false);
212 return;
213
214assign_error:
215 while (--n >= 0) {
216 if (!virtio_queue_get_num(vdev, n)) {
217 continue;
218 }
219
220 r = set_host_notifier_internal(proxy, bus, n, false, false);
221 assert(r >= 0);
222 }
223 k->ioeventfd_set_started(proxy, false, true);
224 error_report("%s: failed. Fallback to userspace (slower).", __func__);
225}
226
227void virtio_bus_stop_ioeventfd(VirtioBusState *bus)
228{
229 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
230 DeviceState *proxy = DEVICE(BUS(bus)->parent);
231 VirtIODevice *vdev;
232 int n, r;
233
234 if (!k->ioeventfd_started || !k->ioeventfd_started(proxy)) {
235 return;
236 }
237 vdev = virtio_bus_get_device(bus);
238 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
239 if (!virtio_queue_get_num(vdev, n)) {
240 continue;
241 }
242 r = set_host_notifier_internal(proxy, bus, n, false, false);
243 assert(r >= 0);
244 }
245 k->ioeventfd_set_started(proxy, false, false);
246}
247
248/*
249 * This function switches from/to the generic ioeventfd handler.
250 * assign==false means 'use generic ioeventfd handler'.
251 */
252int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
253{
254 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
255 DeviceState *proxy = DEVICE(BUS(bus)->parent);
6798e245
CH
256
257 if (!k->ioeventfd_started) {
258 return -ENOSYS;
259 }
0830c96d 260 k->ioeventfd_set_disabled(proxy, assign);
6798e245
CH
261 if (assign) {
262 /*
263 * Stop using the generic ioeventfd, we are doing eventfd handling
264 * ourselves below
0830c96d
CH
265 *
266 * FIXME: We should just switch the handler and not deassign the
267 * ioeventfd.
268 * Otherwise, there's a window where we don't have an
269 * ioeventfd and we may end up with a notification where
270 * we don't expect one.
6798e245 271 */
0830c96d 272 virtio_bus_stop_ioeventfd(bus);
6798e245 273 }
0830c96d 274 return set_host_notifier_internal(proxy, bus, n, assign, false);
6798e245
CH
275}
276
6d46895b
FK
277static char *virtio_bus_get_dev_path(DeviceState *dev)
278{
279 BusState *bus = qdev_get_parent_bus(dev);
280 DeviceState *proxy = DEVICE(bus->parent);
281 return qdev_get_dev_path(proxy);
282}
283
bbfa18fc
AK
284static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
285{
286 return NULL;
287}
288
6d46895b
FK
289static void virtio_bus_class_init(ObjectClass *klass, void *data)
290{
291 BusClass *bus_class = BUS_CLASS(klass);
292 bus_class->get_dev_path = virtio_bus_get_dev_path;
bbfa18fc 293 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
6d46895b
FK
294}
295
ff8eca55
FK
296static const TypeInfo virtio_bus_info = {
297 .name = TYPE_VIRTIO_BUS,
298 .parent = TYPE_BUS,
299 .instance_size = sizeof(VirtioBusState),
300 .abstract = true,
301 .class_size = sizeof(VirtioBusClass),
6d46895b 302 .class_init = virtio_bus_class_init
ff8eca55
FK
303};
304
305static void virtio_register_types(void)
306{
307 type_register_static(&virtio_bus_info);
308}
309
310type_init(virtio_register_types)