]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/virtio-9p-device.c
virtio-serial: Wrap in vmstate
[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;
44
00588a0a 45 while ((pdu = pdu_alloc(s))) {
0192cc5d
WL
46 struct {
47 uint32_t size_le;
48 uint8_t id;
49 uint16_t tag_le;
50 } QEMU_PACKED out;
51b19ebe 51 VirtQueueElement *elem;
0192cc5d 52
51b19ebe
PB
53 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
54 if (!elem) {
00588a0a
WL
55 pdu_free(pdu);
56 break;
57 }
58
59 BUG_ON(elem->out_num == 0 || elem->in_num == 0);
0192cc5d
WL
60 QEMU_BUILD_BUG_ON(sizeof out != 7);
61
51b19ebe 62 v->elems[pdu->idx] = elem;
00588a0a 63 len = iov_to_buf(elem->out_sg, elem->out_num, 0,
0192cc5d
WL
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 }
0192cc5d
WL
75}
76
9d5b731d
JW
77static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
78 Error **errp)
f4f61d27 79{
0cd09c3a 80 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
f4f61d27
AK
81 return features;
82}
83
f4f61d27
AK
84static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
85{
e9a0152b 86 int len;
f4f61d27 87 struct virtio_9p_config *cfg;
00588a0a
WL
88 V9fsVirtioState *v = VIRTIO_9P(vdev);
89 V9fsState *s = &v->state;
f4f61d27 90
e9a0152b
AK
91 len = strlen(s->tag);
92 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
d64ccb91 93 virtio_stw_p(vdev, &cfg->tag_len, len);
e9a0152b
AK
94 /* We don't copy the terminating null to config space */
95 memcpy(cfg->tag, s->tag, len);
00588a0a 96 memcpy(config, cfg, v->config_size);
7267c094 97 g_free(cfg);
f4f61d27
AK
98}
99
4652f164
GK
100static void virtio_9p_save(QEMUFile *f, void *opaque)
101{
102 virtio_save(VIRTIO_DEVICE(opaque), f);
103}
104
105static int virtio_9p_load(QEMUFile *f, void *opaque, int version_id)
106{
107 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
108}
109
59be7522 110static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
0174fe73 111{
59be7522 112 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
00588a0a
WL
113 V9fsVirtioState *v = VIRTIO_9P(dev);
114 V9fsState *s = &v->state;
f4f61d27 115
2a0c56aa 116 if (v9fs_device_realize_common(s, errp)) {
92304bf3 117 goto out;
f4f61d27 118 }
e9a0152b 119
00588a0a
WL
120 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
121 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
122 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
123 register_savevm(dev, "virtio-9p", -1, 1, virtio_9p_save, virtio_9p_load, v);
2a0c56aa 124
92304bf3 125out:
2a0c56aa 126 return;
e7303c43
FK
127}
128
6cecf093
GK
129static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
130{
131 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
00588a0a
WL
132 V9fsVirtioState *v = VIRTIO_9P(dev);
133 V9fsState *s = &v->state;
6cecf093
GK
134
135 virtio_cleanup(vdev);
00588a0a 136 unregister_savevm(dev, "virtio-9p", v);
2a0c56aa 137 v9fs_device_unrealize_common(s, errp);
6cecf093
GK
138}
139
fe9fa96d
WL
140ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
141 const char *fmt, va_list ap)
142{
00588a0a
WL
143 V9fsState *s = pdu->s;
144 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 145 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a
WL
146
147 return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
fe9fa96d
WL
148}
149
150ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
151 const char *fmt, va_list ap)
152{
00588a0a
WL
153 V9fsState *s = pdu->s;
154 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 155 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a
WL
156
157 return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
fe9fa96d
WL
158}
159
592707af
WL
160void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
161 unsigned int *pniov, bool is_write)
162{
00588a0a
WL
163 V9fsState *s = pdu->s;
164 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 165 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a 166
592707af 167 if (is_write) {
00588a0a
WL
168 *piov = elem->out_sg;
169 *pniov = elem->out_num;
592707af 170 } else {
00588a0a
WL
171 *piov = elem->in_sg;
172 *pniov = elem->in_num;
592707af
WL
173 }
174}
175
e7303c43
FK
176/* virtio-9p device */
177
e7303c43 178static Property virtio_9p_properties[] = {
00588a0a
WL
179 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
180 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
e7303c43
FK
181 DEFINE_PROP_END_OF_LIST(),
182};
183
184static void virtio_9p_class_init(ObjectClass *klass, void *data)
185{
186 DeviceClass *dc = DEVICE_CLASS(klass);
187 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
59be7522 188
e7303c43 189 dc->props = virtio_9p_properties;
125ee0ed 190 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
59be7522 191 vdc->realize = virtio_9p_device_realize;
6cecf093 192 vdc->unrealize = virtio_9p_device_unrealize;
e7303c43
FK
193 vdc->get_features = virtio_9p_get_features;
194 vdc->get_config = virtio_9p_get_config;
195}
196
197static const TypeInfo virtio_device_info = {
198 .name = TYPE_VIRTIO_9P,
199 .parent = TYPE_VIRTIO_DEVICE,
00588a0a 200 .instance_size = sizeof(V9fsVirtioState),
e7303c43
FK
201 .class_init = virtio_9p_class_init,
202};
203
204static void virtio_9p_register_types(void)
205{
206 type_register_static(&virtio_device_info);
207}
208
209type_init(virtio_9p_register_types)