4 * Copyright IBM, Corp. 2010
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"
20 #include "hw/virtio/virtio-access.h"
23 static const struct V9fsTransport virtio_9p_transport
;
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
);
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
;
46 VirtQueueElement
*elem
;
48 while ((pdu
= pdu_alloc(s
))) {
51 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
56 if (elem
->in_num
== 0) {
58 "The guest sent a VirtFS request without space for "
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,
67 if (len
!= sizeof(out
)) {
68 virtio_error(vdev
, "The guest sent a malformed VirtFS request: "
69 "header size is %zd, should be 7", len
);
73 pdu_submit(pdu
, &out
);
79 virtqueue_detach_element(vq
, elem
, 0);
85 static uint64_t virtio_9p_get_features(VirtIODevice
*vdev
, uint64_t features
,
88 virtio_add_feature(&features
, VIRTIO_9P_MOUNT_TAG
);
92 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
95 struct virtio_9p_config
*cfg
;
96 V9fsVirtioState
*v
= VIRTIO_9P(vdev
);
97 V9fsState
*s
= &v
->state
;
100 cfg
= g_malloc0(sizeof(struct virtio_9p_config
) + len
);
101 virtio_stw_p(vdev
, &cfg
->tag_len
, len
);
102 /* We don't copy the terminating null to config space */
103 memcpy(cfg
->tag
, s
->tag
, len
);
104 memcpy(config
, cfg
, v
->config_size
);
108 static void virtio_9p_device_realize(DeviceState
*dev
, Error
**errp
)
110 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
111 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
112 V9fsState
*s
= &v
->state
;
114 if (v9fs_device_realize_common(s
, errp
)) {
118 v
->config_size
= sizeof(struct virtio_9p_config
) + strlen(s
->fsconf
.tag
);
119 virtio_init(vdev
, "virtio-9p", VIRTIO_ID_9P
, v
->config_size
);
120 v
->vq
= virtio_add_queue(vdev
, MAX_REQ
, handle_9p_output
);
121 v9fs_register_transport(s
, &virtio_9p_transport
);
127 static void virtio_9p_device_unrealize(DeviceState
*dev
, Error
**errp
)
129 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
130 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
131 V9fsState
*s
= &v
->state
;
133 virtio_cleanup(vdev
);
134 v9fs_device_unrealize_common(s
, errp
);
137 static void virtio_9p_reset(VirtIODevice
*vdev
)
139 V9fsVirtioState
*v
= (V9fsVirtioState
*)vdev
;
141 v9fs_reset(&v
->state
);
144 static ssize_t
virtio_pdu_vmarshal(V9fsPDU
*pdu
, size_t offset
,
145 const char *fmt
, va_list ap
)
147 V9fsState
*s
= pdu
->s
;
148 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
149 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
151 return v9fs_iov_vmarshal(elem
->in_sg
, elem
->in_num
, offset
, 1, fmt
, ap
);
154 static ssize_t
virtio_pdu_vunmarshal(V9fsPDU
*pdu
, size_t offset
,
155 const char *fmt
, va_list ap
)
157 V9fsState
*s
= pdu
->s
;
158 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
159 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
161 return v9fs_iov_vunmarshal(elem
->out_sg
, elem
->out_num
, offset
, 1, fmt
, ap
);
164 /* The size parameter is used by other transports. Do not drop it. */
165 static void virtio_init_in_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
166 unsigned int *pniov
, size_t size
)
168 V9fsState
*s
= pdu
->s
;
169 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
170 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
173 *pniov
= elem
->in_num
;
176 static void virtio_init_out_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
179 V9fsState
*s
= pdu
->s
;
180 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
181 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
183 *piov
= elem
->out_sg
;
184 *pniov
= elem
->out_num
;
187 static const struct V9fsTransport virtio_9p_transport
= {
188 .pdu_vmarshal
= virtio_pdu_vmarshal
,
189 .pdu_vunmarshal
= virtio_pdu_vunmarshal
,
190 .init_in_iov_from_pdu
= virtio_init_in_iov_from_pdu
,
191 .init_out_iov_from_pdu
= virtio_init_out_iov_from_pdu
,
192 .push_and_notify
= virtio_9p_push_and_notify
,
195 /* virtio-9p device */
197 static const VMStateDescription vmstate_virtio_9p
= {
199 .minimum_version_id
= 1,
201 .fields
= (VMStateField
[]) {
202 VMSTATE_VIRTIO_DEVICE
,
203 VMSTATE_END_OF_LIST()
207 static Property virtio_9p_properties
[] = {
208 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState
, state
.fsconf
.tag
),
209 DEFINE_PROP_STRING("fsdev", V9fsVirtioState
, state
.fsconf
.fsdev_id
),
210 DEFINE_PROP_END_OF_LIST(),
213 static void virtio_9p_class_init(ObjectClass
*klass
, void *data
)
215 DeviceClass
*dc
= DEVICE_CLASS(klass
);
216 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
218 dc
->props
= virtio_9p_properties
;
219 dc
->vmsd
= &vmstate_virtio_9p
;
220 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
221 vdc
->realize
= virtio_9p_device_realize
;
222 vdc
->unrealize
= virtio_9p_device_unrealize
;
223 vdc
->get_features
= virtio_9p_get_features
;
224 vdc
->get_config
= virtio_9p_get_config
;
225 vdc
->reset
= virtio_9p_reset
;
228 static const TypeInfo virtio_device_info
= {
229 .name
= TYPE_VIRTIO_9P
,
230 .parent
= TYPE_VIRTIO_DEVICE
,
231 .instance_size
= sizeof(V9fsVirtioState
),
232 .class_init
= virtio_9p_class_init
,
235 static void virtio_9p_register_types(void)
237 type_register_static(&virtio_device_info
);
240 type_init(virtio_9p_register_types
)