hw/arm/virt: fix max-cpus check
[qemu/ar7.git] / hw / 9pfs / virtio-9p-device.c
bloba38850ee89dad867cfe5b2e7dddde04bf65f4acb
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 "hw/i386/pc.h"
17 #include "qemu/sockets.h"
18 #include "virtio-9p.h"
19 #include "fsdev/qemu-fsdev.h"
20 #include "9p-xattr.h"
21 #include "coth.h"
22 #include "hw/virtio/virtio-access.h"
23 #include "qemu/iov.h"
25 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;
47 while ((pdu = pdu_alloc(s))) {
48 struct {
49 uint32_t size_le;
50 uint8_t id;
51 uint16_t tag_le;
52 } QEMU_PACKED out;
53 VirtQueueElement *elem;
55 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
56 if (!elem) {
57 pdu_free(pdu);
58 break;
61 BUG_ON(elem->out_num == 0 || elem->in_num == 0);
62 QEMU_BUILD_BUG_ON(sizeof out != 7);
64 v->elems[pdu->idx] = elem;
65 len = iov_to_buf(elem->out_sg, elem->out_num, 0,
66 &out, sizeof out);
67 BUG_ON(len != sizeof out);
69 pdu->size = le32_to_cpu(out.size_le);
71 pdu->id = out.id;
72 pdu->tag = le16_to_cpu(out.tag_le);
74 qemu_co_queue_init(&pdu->complete);
75 pdu_submit(pdu);
79 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
80 Error **errp)
82 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
83 return features;
86 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
88 int len;
89 struct virtio_9p_config *cfg;
90 V9fsVirtioState *v = VIRTIO_9P(vdev);
91 V9fsState *s = &v->state;
93 len = strlen(s->tag);
94 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
95 virtio_stw_p(vdev, &cfg->tag_len, len);
96 /* We don't copy the terminating null to config space */
97 memcpy(cfg->tag, s->tag, len);
98 memcpy(config, cfg, v->config_size);
99 g_free(cfg);
102 static void virtio_9p_save(QEMUFile *f, void *opaque)
104 virtio_save(VIRTIO_DEVICE(opaque), f);
107 static int virtio_9p_load(QEMUFile *f, void *opaque, int version_id)
109 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
112 static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
114 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
115 V9fsVirtioState *v = VIRTIO_9P(dev);
116 V9fsState *s = &v->state;
118 if (v9fs_device_realize_common(s, errp)) {
119 goto out;
122 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
123 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
124 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
125 register_savevm(dev, "virtio-9p", -1, 1, virtio_9p_save, virtio_9p_load, v);
127 out:
128 return;
131 static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
133 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
134 V9fsVirtioState *v = VIRTIO_9P(dev);
135 V9fsState *s = &v->state;
137 virtio_cleanup(vdev);
138 unregister_savevm(dev, "virtio-9p", v);
139 v9fs_device_unrealize_common(s, errp);
142 ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
143 const char *fmt, va_list ap)
145 V9fsState *s = pdu->s;
146 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
147 VirtQueueElement *elem = v->elems[pdu->idx];
149 return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
152 ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
153 const char *fmt, va_list ap)
155 V9fsState *s = pdu->s;
156 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
157 VirtQueueElement *elem = v->elems[pdu->idx];
159 return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
162 void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
163 unsigned int *pniov, bool is_write)
165 V9fsState *s = pdu->s;
166 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
167 VirtQueueElement *elem = v->elems[pdu->idx];
169 if (is_write) {
170 *piov = elem->out_sg;
171 *pniov = elem->out_num;
172 } else {
173 *piov = elem->in_sg;
174 *pniov = elem->in_num;
178 /* virtio-9p device */
180 static Property virtio_9p_properties[] = {
181 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
182 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
183 DEFINE_PROP_END_OF_LIST(),
186 static void virtio_9p_class_init(ObjectClass *klass, void *data)
188 DeviceClass *dc = DEVICE_CLASS(klass);
189 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
191 dc->props = virtio_9p_properties;
192 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
193 vdc->realize = virtio_9p_device_realize;
194 vdc->unrealize = virtio_9p_device_unrealize;
195 vdc->get_features = virtio_9p_get_features;
196 vdc->get_config = virtio_9p_get_config;
199 static const TypeInfo virtio_device_info = {
200 .name = TYPE_VIRTIO_9P,
201 .parent = TYPE_VIRTIO_DEVICE,
202 .instance_size = sizeof(V9fsVirtioState),
203 .class_init = virtio_9p_class_init,
206 static void virtio_9p_register_types(void)
208 type_register_static(&virtio_device_info);
211 type_init(virtio_9p_register_types)