1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "qemu/osdep.h"
15#include "hw/virtio/virtio.h"
16#include "qemu/sockets.h"
17#include "virtio-9p.h"
18#include "fsdev/qemu-fsdev.h"
19#include "coth.h"
20#include "hw/virtio/virtio-access.h"
21#include "qemu/iov.h"
22
23void virtio_9p_push_and_notify(V9fsPDU *pdu)
24{
25 V9fsState *s = pdu->s;
26 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
27 VirtQueueElement *elem = v->elems[pdu->idx];
28
29
30 virtqueue_push(v->vq, elem, pdu->size);
31 g_free(elem);
32 v->elems[pdu->idx] = NULL;
33
34
35 virtio_notify(VIRTIO_DEVICE(v), v->vq);
36}
37
38static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
39{
40 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
41 V9fsState *s = &v->state;
42 V9fsPDU *pdu;
43 ssize_t len;
44
45 while ((pdu = pdu_alloc(s))) {
46 struct {
47 uint32_t size_le;
48 uint8_t id;
49 uint16_t tag_le;
50 } QEMU_PACKED out;
51 VirtQueueElement *elem;
52
53 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
54 if (!elem) {
55 pdu_free(pdu);
56 break;
57 }
58
59 BUG_ON(elem->out_num == 0 || elem->in_num == 0);
60 QEMU_BUILD_BUG_ON(sizeof out != 7);
61
62 v->elems[pdu->idx] = elem;
63 len = iov_to_buf(elem->out_sg, elem->out_num, 0,
64 &out, sizeof out);
65 BUG_ON(len != sizeof out);
66
67 pdu->size = le32_to_cpu(out.size_le);
68
69 pdu->id = out.id;
70 pdu->tag = le16_to_cpu(out.tag_le);
71
72 qemu_co_queue_init(&pdu->complete);
73 pdu_submit(pdu);
74 }
75}
76
77static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
78 Error **errp)
79{
80 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
81 return features;
82}
83
84static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
85{
86 int len;
87 struct virtio_9p_config *cfg;
88 V9fsVirtioState *v = VIRTIO_9P(vdev);
89 V9fsState *s = &v->state;
90
91 len = strlen(s->tag);
92 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
93 virtio_stw_p(vdev, &cfg->tag_len, len);
94
95 memcpy(cfg->tag, s->tag, len);
96 memcpy(config, cfg, v->config_size);
97 g_free(cfg);
98}
99
100static int virtio_9p_load(QEMUFile *f, void *opaque, size_t size)
101{
102 return virtio_load(VIRTIO_DEVICE(opaque), f, 1);
103}
104
105static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
106{
107 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
108 V9fsVirtioState *v = VIRTIO_9P(dev);
109 V9fsState *s = &v->state;
110
111 if (v9fs_device_realize_common(s, errp)) {
112 goto out;
113 }
114
115 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
116 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
117 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
118
119out:
120 return;
121}
122
123static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
124{
125 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
126 V9fsVirtioState *v = VIRTIO_9P(dev);
127 V9fsState *s = &v->state;
128
129 virtio_cleanup(vdev);
130 v9fs_device_unrealize_common(s, errp);
131}
132
133ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
134 const char *fmt, va_list ap)
135{
136 V9fsState *s = pdu->s;
137 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
138 VirtQueueElement *elem = v->elems[pdu->idx];
139
140 return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
141}
142
143ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
144 const char *fmt, va_list ap)
145{
146 V9fsState *s = pdu->s;
147 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
148 VirtQueueElement *elem = v->elems[pdu->idx];
149
150 return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
151}
152
153void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
154 unsigned int *pniov, bool is_write)
155{
156 V9fsState *s = pdu->s;
157 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
158 VirtQueueElement *elem = v->elems[pdu->idx];
159
160 if (is_write) {
161 *piov = elem->out_sg;
162 *pniov = elem->out_num;
163 } else {
164 *piov = elem->in_sg;
165 *pniov = elem->in_num;
166 }
167}
168
169
170
171VMSTATE_VIRTIO_DEVICE(9p, 1, virtio_9p_load, virtio_vmstate_save);
172
173static Property virtio_9p_properties[] = {
174 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
175 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
176 DEFINE_PROP_END_OF_LIST(),
177};
178
179static void virtio_9p_class_init(ObjectClass *klass, void *data)
180{
181 DeviceClass *dc = DEVICE_CLASS(klass);
182 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
183
184 dc->props = virtio_9p_properties;
185 dc->vmsd = &vmstate_virtio_9p;
186 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
187 vdc->realize = virtio_9p_device_realize;
188 vdc->unrealize = virtio_9p_device_unrealize;
189 vdc->get_features = virtio_9p_get_features;
190 vdc->get_config = virtio_9p_get_config;
191}
192
193static const TypeInfo virtio_device_info = {
194 .name = TYPE_VIRTIO_9P,
195 .parent = TYPE_VIRTIO_DEVICE,
196 .instance_size = sizeof(V9fsVirtioState),
197 .class_init = virtio_9p_class_init,
198};
199
200static void virtio_9p_register_types(void)
201{
202 type_register_static(&virtio_device_info);
203}
204
205type_init(virtio_9p_register_types)
206