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