meson: convert hw/nvram
[qemu/ar7.git] / hw / 9pfs / virtio-9p-device.c
blob36f3aa9352f28cef3f84e9906c614ceb580017bf
1 /*
2 * Virtio 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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/qdev-properties.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "qemu/iov.h"
23 #include "qemu/module.h"
25 static void virtio_9p_push_and_notify(V9fsPDU *pdu)
27 V9fsState *s = pdu->s;
28 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
29 VirtQueueElement *elem = v->elems[pdu->idx];
31 /* push onto queue and notify */
32 virtqueue_push(v->vq, elem, pdu->size);
33 g_free(elem);
34 v->elems[pdu->idx] = NULL;
36 /* FIXME: we should batch these completions */
37 virtio_notify(VIRTIO_DEVICE(v), v->vq);
40 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
42 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
43 V9fsState *s = &v->state;
44 V9fsPDU *pdu;
45 ssize_t len;
46 VirtQueueElement *elem;
48 while ((pdu = pdu_alloc(s))) {
49 P9MsgHeader out;
51 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
52 if (!elem) {
53 goto out_free_pdu;
56 if (iov_size(elem->in_sg, elem->in_num) < 7) {
57 virtio_error(vdev,
58 "The guest sent a VirtFS request without space for "
59 "the reply");
60 goto out_free_req;
63 len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7);
64 if (len != 7) {
65 virtio_error(vdev, "The guest sent a malformed VirtFS request: "
66 "header size is %zd, should be 7", len);
67 goto out_free_req;
70 v->elems[pdu->idx] = elem;
72 pdu_submit(pdu, &out);
75 return;
77 out_free_req:
78 virtqueue_detach_element(vq, elem, 0);
79 g_free(elem);
80 out_free_pdu:
81 pdu_free(pdu);
84 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
85 Error **errp)
87 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
88 return features;
91 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
93 int len;
94 struct virtio_9p_config *cfg;
95 V9fsVirtioState *v = VIRTIO_9P(vdev);
96 V9fsState *s = &v->state;
98 len = strlen(s->tag);
99 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
100 virtio_stw_p(vdev, &cfg->tag_len, len);
101 /* We don't copy the terminating null to config space */
102 memcpy(cfg->tag, s->tag, len);
103 memcpy(config, cfg, v->config_size);
104 g_free(cfg);
107 static void virtio_9p_reset(VirtIODevice *vdev)
109 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
111 v9fs_reset(&v->state);
114 static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
115 const char *fmt, va_list ap)
117 V9fsState *s = pdu->s;
118 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
119 VirtQueueElement *elem = v->elems[pdu->idx];
120 ssize_t ret;
122 ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
123 if (ret < 0) {
124 VirtIODevice *vdev = VIRTIO_DEVICE(v);
126 virtio_error(vdev, "Failed to encode VirtFS reply type %d",
127 pdu->id + 1);
129 return ret;
132 static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
133 const char *fmt, va_list ap)
135 V9fsState *s = pdu->s;
136 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
137 VirtQueueElement *elem = v->elems[pdu->idx];
138 ssize_t ret;
140 ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
141 if (ret < 0) {
142 VirtIODevice *vdev = VIRTIO_DEVICE(v);
144 virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id);
146 return ret;
149 static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
150 unsigned int *pniov, size_t size)
152 V9fsState *s = pdu->s;
153 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
154 VirtQueueElement *elem = v->elems[pdu->idx];
155 size_t buf_size = iov_size(elem->in_sg, elem->in_num);
157 if (buf_size < size) {
158 VirtIODevice *vdev = VIRTIO_DEVICE(v);
160 virtio_error(vdev,
161 "VirtFS reply type %d needs %zu bytes, buffer has %zu",
162 pdu->id + 1, size, buf_size);
165 *piov = elem->in_sg;
166 *pniov = elem->in_num;
169 static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
170 unsigned int *pniov, size_t size)
172 V9fsState *s = pdu->s;
173 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
174 VirtQueueElement *elem = v->elems[pdu->idx];
175 size_t buf_size = iov_size(elem->out_sg, elem->out_num);
177 if (buf_size < size) {
178 VirtIODevice *vdev = VIRTIO_DEVICE(v);
180 virtio_error(vdev,
181 "VirtFS request type %d needs %zu bytes, buffer has %zu",
182 pdu->id, size, buf_size);
185 *piov = elem->out_sg;
186 *pniov = elem->out_num;
189 static const V9fsTransport virtio_9p_transport = {
190 .pdu_vmarshal = virtio_pdu_vmarshal,
191 .pdu_vunmarshal = virtio_pdu_vunmarshal,
192 .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
193 .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
194 .push_and_notify = virtio_9p_push_and_notify,
197 static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
199 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
200 V9fsVirtioState *v = VIRTIO_9P(dev);
201 V9fsState *s = &v->state;
203 if (v9fs_device_realize_common(s, &virtio_9p_transport, errp)) {
204 return;
207 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
208 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
209 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
212 static void virtio_9p_device_unrealize(DeviceState *dev)
214 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
215 V9fsVirtioState *v = VIRTIO_9P(dev);
216 V9fsState *s = &v->state;
218 virtio_delete_queue(v->vq);
219 virtio_cleanup(vdev);
220 v9fs_device_unrealize_common(s);
223 /* virtio-9p device */
225 static const VMStateDescription vmstate_virtio_9p = {
226 .name = "virtio-9p",
227 .minimum_version_id = 1,
228 .version_id = 1,
229 .fields = (VMStateField[]) {
230 VMSTATE_VIRTIO_DEVICE,
231 VMSTATE_END_OF_LIST()
235 static Property virtio_9p_properties[] = {
236 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
237 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
238 DEFINE_PROP_END_OF_LIST(),
241 static void virtio_9p_class_init(ObjectClass *klass, void *data)
243 DeviceClass *dc = DEVICE_CLASS(klass);
244 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
246 device_class_set_props(dc, virtio_9p_properties);
247 dc->vmsd = &vmstate_virtio_9p;
248 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
249 vdc->realize = virtio_9p_device_realize;
250 vdc->unrealize = virtio_9p_device_unrealize;
251 vdc->get_features = virtio_9p_get_features;
252 vdc->get_config = virtio_9p_get_config;
253 vdc->reset = virtio_9p_reset;
256 static const TypeInfo virtio_device_info = {
257 .name = TYPE_VIRTIO_9P,
258 .parent = TYPE_VIRTIO_DEVICE,
259 .instance_size = sizeof(V9fsVirtioState),
260 .class_init = virtio_9p_class_init,
263 static void virtio_9p_register_types(void)
265 type_register_static(&virtio_device_info);
268 type_init(virtio_9p_register_types)