]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/virtio-9p-device.c
virtio: cleanup VMSTATE_VIRTIO_DEVICE
[mirror_qemu.git] / hw / 9pfs / virtio-9p-device.c
CommitLineData
f4f61d27
AK
1/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
9b8bfe21 14#include "qemu/osdep.h"
0d09e41a 15#include "hw/virtio/virtio.h"
1de7afc9 16#include "qemu/sockets.h"
f4f61d27
AK
17#include "virtio-9p.h"
18#include "fsdev/qemu-fsdev.h"
fe52840c 19#include "coth.h"
d64ccb91 20#include "hw/virtio/virtio-access.h"
0192cc5d 21#include "qemu/iov.h"
f4f61d27 22
0d3716b4
WL
23void virtio_9p_push_and_notify(V9fsPDU *pdu)
24{
25 V9fsState *s = pdu->s;
00588a0a 26 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 27 VirtQueueElement *elem = v->elems[pdu->idx];
0d3716b4
WL
28
29 /* push onto queue and notify */
00588a0a 30 virtqueue_push(v->vq, elem, pdu->size);
51b19ebe
PB
31 g_free(elem);
32 v->elems[pdu->idx] = NULL;
0d3716b4
WL
33
34 /* FIXME: we should batch these completions */
00588a0a 35 virtio_notify(VIRTIO_DEVICE(v), v->vq);
0d3716b4
WL
36}
37
0192cc5d
WL
38static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
39{
00588a0a
WL
40 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
41 V9fsState *s = &v->state;
0192cc5d
WL
42 V9fsPDU *pdu;
43 ssize_t len;
d3d74d6f 44 VirtQueueElement *elem;
0192cc5d 45
00588a0a 46 while ((pdu = pdu_alloc(s))) {
0192cc5d
WL
47 struct {
48 uint32_t size_le;
49 uint8_t id;
50 uint16_t tag_le;
51 } QEMU_PACKED out;
0192cc5d 52
51b19ebe
PB
53 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
54 if (!elem) {
d3d74d6f 55 goto out_free_pdu;
00588a0a
WL
56 }
57
d3d74d6f
GK
58 if (elem->in_num == 0) {
59 virtio_error(vdev,
60 "The guest sent a VirtFS request without space for "
61 "the reply");
62 goto out_free_req;
63 }
e8582891 64 QEMU_BUILD_BUG_ON(sizeof(out) != 7);
0192cc5d 65
51b19ebe 66 v->elems[pdu->idx] = elem;
00588a0a 67 len = iov_to_buf(elem->out_sg, elem->out_num, 0,
e8582891 68 &out, sizeof(out));
d3d74d6f
GK
69 if (len != sizeof(out)) {
70 virtio_error(vdev, "The guest sent a malformed VirtFS request: "
71 "header size is %zd, should be 7", len);
72 goto out_free_req;
73 }
0192cc5d
WL
74
75 pdu->size = le32_to_cpu(out.size_le);
76
77 pdu->id = out.id;
78 pdu->tag = le16_to_cpu(out.tag_le);
79
80 qemu_co_queue_init(&pdu->complete);
81 pdu_submit(pdu);
82 }
d3d74d6f
GK
83
84 return;
85
86out_free_req:
87 virtqueue_detach_element(vq, elem, 0);
88 g_free(elem);
89out_free_pdu:
90 pdu_free(pdu);
0192cc5d
WL
91}
92
9d5b731d
JW
93static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
94 Error **errp)
f4f61d27 95{
0cd09c3a 96 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
f4f61d27
AK
97 return features;
98}
99
f4f61d27
AK
100static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
101{
e9a0152b 102 int len;
f4f61d27 103 struct virtio_9p_config *cfg;
00588a0a
WL
104 V9fsVirtioState *v = VIRTIO_9P(vdev);
105 V9fsState *s = &v->state;
f4f61d27 106
e9a0152b
AK
107 len = strlen(s->tag);
108 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
d64ccb91 109 virtio_stw_p(vdev, &cfg->tag_len, len);
e9a0152b
AK
110 /* We don't copy the terminating null to config space */
111 memcpy(cfg->tag, s->tag, len);
00588a0a 112 memcpy(config, cfg, v->config_size);
7267c094 113 g_free(cfg);
f4f61d27
AK
114}
115
59be7522 116static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
0174fe73 117{
59be7522 118 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
00588a0a
WL
119 V9fsVirtioState *v = VIRTIO_9P(dev);
120 V9fsState *s = &v->state;
f4f61d27 121
2a0c56aa 122 if (v9fs_device_realize_common(s, errp)) {
92304bf3 123 goto out;
f4f61d27 124 }
e9a0152b 125
00588a0a
WL
126 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
127 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
128 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
2a0c56aa 129
92304bf3 130out:
2a0c56aa 131 return;
e7303c43
FK
132}
133
6cecf093
GK
134static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
135{
136 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
00588a0a
WL
137 V9fsVirtioState *v = VIRTIO_9P(dev);
138 V9fsState *s = &v->state;
6cecf093
GK
139
140 virtio_cleanup(vdev);
2a0c56aa 141 v9fs_device_unrealize_common(s, errp);
6cecf093
GK
142}
143
fe9fa96d
WL
144ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
145 const char *fmt, va_list ap)
146{
00588a0a
WL
147 V9fsState *s = pdu->s;
148 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 149 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a
WL
150
151 return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
fe9fa96d
WL
152}
153
154ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
155 const char *fmt, va_list ap)
156{
00588a0a
WL
157 V9fsState *s = pdu->s;
158 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 159 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a
WL
160
161 return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
fe9fa96d
WL
162}
163
592707af
WL
164void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
165 unsigned int *pniov, bool is_write)
166{
00588a0a
WL
167 V9fsState *s = pdu->s;
168 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 169 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a 170
592707af 171 if (is_write) {
00588a0a
WL
172 *piov = elem->out_sg;
173 *pniov = elem->out_num;
592707af 174 } else {
00588a0a
WL
175 *piov = elem->in_sg;
176 *pniov = elem->in_num;
592707af
WL
177 }
178}
179
e7303c43
FK
180/* virtio-9p device */
181
dcaf8dda
HP
182static const VMStateDescription vmstate_virtio_9p = {
183 .name = "virtio-9p",
184 .minimum_version_id = 1,
185 .version_id = 1,
186 .fields = (VMStateField[]) {
187 VMSTATE_VIRTIO_DEVICE,
188 VMSTATE_END_OF_LIST()
189 },
190};
18e0e5b2 191
e7303c43 192static Property virtio_9p_properties[] = {
00588a0a
WL
193 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
194 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
e7303c43
FK
195 DEFINE_PROP_END_OF_LIST(),
196};
197
198static void virtio_9p_class_init(ObjectClass *klass, void *data)
199{
200 DeviceClass *dc = DEVICE_CLASS(klass);
201 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
59be7522 202
e7303c43 203 dc->props = virtio_9p_properties;
18e0e5b2 204 dc->vmsd = &vmstate_virtio_9p;
125ee0ed 205 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
59be7522 206 vdc->realize = virtio_9p_device_realize;
6cecf093 207 vdc->unrealize = virtio_9p_device_unrealize;
e7303c43
FK
208 vdc->get_features = virtio_9p_get_features;
209 vdc->get_config = virtio_9p_get_config;
210}
211
212static const TypeInfo virtio_device_info = {
213 .name = TYPE_VIRTIO_9P,
214 .parent = TYPE_VIRTIO_DEVICE,
00588a0a 215 .instance_size = sizeof(V9fsVirtioState),
e7303c43
FK
216 .class_init = virtio_9p_class_init,
217};
218
219static void virtio_9p_register_types(void)
220{
221 type_register_static(&virtio_device_info);
222}
223
224type_init(virtio_9p_register_types)