1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "hw/hw.h"
27#include "qemu/error-report.h"
28#include "hw/qdev.h"
29#include "hw/virtio/virtio-bus.h"
30#include "hw/virtio/virtio.h"
31
32
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
41
42void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
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);
48 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
49
50 DPRINTF("%s: plug device.\n", qbus->name);
51
52 if (klass->device_plugged != NULL) {
53 klass->device_plugged(qbus->parent, errp);
54 }
55
56
57 assert(vdc->get_features != NULL);
58 vdev->host_features = vdc->get_features(vdev, vdev->host_features,
59 errp);
60 if (klass->post_plugged != NULL) {
61 klass->post_plugged(qbus->parent, errp);
62 }
63}
64
65
66void virtio_bus_reset(VirtioBusState *bus)
67{
68 VirtIODevice *vdev = virtio_bus_get_device(bus);
69
70 DPRINTF("%s: reset device.\n", BUS(bus)->name);
71 if (vdev != NULL) {
72 virtio_reset(vdev);
73 }
74}
75
76
77void virtio_bus_device_unplugged(VirtIODevice *vdev)
78{
79 DeviceState *qdev = DEVICE(vdev);
80 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
81 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
82
83 DPRINTF("%s: remove device.\n", qbus->name);
84
85 if (vdev != NULL) {
86 if (klass->device_unplugged != NULL) {
87 klass->device_unplugged(qbus->parent);
88 }
89 }
90}
91
92
93uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
94{
95 VirtIODevice *vdev = virtio_bus_get_device(bus);
96 assert(vdev != NULL);
97 return vdev->device_id;
98}
99
100
101size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
102{
103 VirtIODevice *vdev = virtio_bus_get_device(bus);
104 assert(vdev != NULL);
105 return vdev->config_len;
106}
107
108
109uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
110{
111 VirtIODevice *vdev = virtio_bus_get_device(bus);
112 VirtioDeviceClass *k;
113
114 assert(vdev != NULL);
115 k = VIRTIO_DEVICE_GET_CLASS(vdev);
116 if (k->bad_features != NULL) {
117 return k->bad_features(vdev);
118 } else {
119 return 0;
120 }
121}
122
123
124void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
125{
126 VirtIODevice *vdev = virtio_bus_get_device(bus);
127 VirtioDeviceClass *k;
128
129 assert(vdev != NULL);
130 k = VIRTIO_DEVICE_GET_CLASS(vdev);
131 if (k->get_config != NULL) {
132 k->get_config(vdev, config);
133 }
134}
135
136
137void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
138{
139 VirtIODevice *vdev = virtio_bus_get_device(bus);
140 VirtioDeviceClass *k;
141
142 assert(vdev != NULL);
143 k = VIRTIO_DEVICE_GET_CLASS(vdev);
144 if (k->set_config != NULL) {
145 k->set_config(vdev, config);
146 }
147}
148
149static char *virtio_bus_get_dev_path(DeviceState *dev)
150{
151 BusState *bus = qdev_get_parent_bus(dev);
152 DeviceState *proxy = DEVICE(bus->parent);
153 return qdev_get_dev_path(proxy);
154}
155
156static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
157{
158 return NULL;
159}
160
161static void virtio_bus_class_init(ObjectClass *klass, void *data)
162{
163 BusClass *bus_class = BUS_CLASS(klass);
164 bus_class->get_dev_path = virtio_bus_get_dev_path;
165 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
166}
167
168static const TypeInfo virtio_bus_info = {
169 .name = TYPE_VIRTIO_BUS,
170 .parent = TYPE_BUS,
171 .instance_size = sizeof(VirtioBusState),
172 .abstract = true,
173 .class_size = sizeof(VirtioBusClass),
174 .class_init = virtio_bus_class_init
175};
176
177static void virtio_register_types(void)
178{
179 type_register_static(&virtio_bus_info);
180}
181
182type_init(virtio_register_types)
183