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 (iov_size(elem
->in_sg
, elem
->in_num
) < 7) {
58 "The guest sent a VirtFS request without space for "
63 len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0, &out
, 7);
65 virtio_error(vdev
, "The guest sent a malformed VirtFS request: "
66 "header size is %zd, should be 7", len
);
70 v
->elems
[pdu
->idx
] = elem
;
72 pdu_submit(pdu
, &out
);
78 virtqueue_detach_element(vq
, elem
, 0);
84 static uint64_t virtio_9p_get_features(VirtIODevice
*vdev
, uint64_t features
,
87 virtio_add_feature(&features
, VIRTIO_9P_MOUNT_TAG
);
91 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
94 struct virtio_9p_config
*cfg
;
95 V9fsVirtioState
*v
= VIRTIO_9P(vdev
);
96 V9fsState
*s
= &v
->state
;
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
);
107 static void virtio_9p_device_realize(DeviceState
*dev
, Error
**errp
)
109 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
110 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
111 V9fsState
*s
= &v
->state
;
113 if (v9fs_device_realize_common(s
, errp
)) {
117 v
->config_size
= sizeof(struct virtio_9p_config
) + strlen(s
->fsconf
.tag
);
118 virtio_init(vdev
, "virtio-9p", VIRTIO_ID_9P
, v
->config_size
);
119 v
->vq
= virtio_add_queue(vdev
, MAX_REQ
, handle_9p_output
);
120 v9fs_register_transport(s
, &virtio_9p_transport
);
126 static void virtio_9p_device_unrealize(DeviceState
*dev
, Error
**errp
)
128 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
129 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
130 V9fsState
*s
= &v
->state
;
132 virtio_cleanup(vdev
);
133 v9fs_device_unrealize_common(s
, errp
);
136 static void virtio_9p_reset(VirtIODevice
*vdev
)
138 V9fsVirtioState
*v
= (V9fsVirtioState
*)vdev
;
140 v9fs_reset(&v
->state
);
143 static ssize_t
virtio_pdu_vmarshal(V9fsPDU
*pdu
, size_t offset
,
144 const char *fmt
, va_list ap
)
146 V9fsState
*s
= pdu
->s
;
147 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
148 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
151 ret
= v9fs_iov_vmarshal(elem
->in_sg
, elem
->in_num
, offset
, 1, fmt
, ap
);
153 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
155 virtio_error(vdev
, "Failed to encode VirtFS reply type %d",
161 static ssize_t
virtio_pdu_vunmarshal(V9fsPDU
*pdu
, size_t offset
,
162 const char *fmt
, va_list ap
)
164 V9fsState
*s
= pdu
->s
;
165 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
166 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
169 ret
= v9fs_iov_vunmarshal(elem
->out_sg
, elem
->out_num
, offset
, 1, fmt
, ap
);
171 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
173 virtio_error(vdev
, "Failed to decode VirtFS request type %d", pdu
->id
);
178 static void virtio_init_in_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
179 unsigned int *pniov
, size_t size
)
181 V9fsState
*s
= pdu
->s
;
182 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
183 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
184 size_t buf_size
= iov_size(elem
->in_sg
, elem
->in_num
);
186 if (buf_size
< size
) {
187 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
190 "VirtFS reply type %d needs %zu bytes, buffer has %zu",
191 pdu
->id
+ 1, size
, buf_size
);
195 *pniov
= elem
->in_num
;
198 static void virtio_init_out_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
199 unsigned int *pniov
, size_t size
)
201 V9fsState
*s
= pdu
->s
;
202 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
203 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
204 size_t buf_size
= iov_size(elem
->out_sg
, elem
->out_num
);
206 if (buf_size
< size
) {
207 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
210 "VirtFS request type %d needs %zu bytes, buffer has %zu",
211 pdu
->id
, size
, buf_size
);
214 *piov
= elem
->out_sg
;
215 *pniov
= elem
->out_num
;
218 static const struct V9fsTransport virtio_9p_transport
= {
219 .pdu_vmarshal
= virtio_pdu_vmarshal
,
220 .pdu_vunmarshal
= virtio_pdu_vunmarshal
,
221 .init_in_iov_from_pdu
= virtio_init_in_iov_from_pdu
,
222 .init_out_iov_from_pdu
= virtio_init_out_iov_from_pdu
,
223 .push_and_notify
= virtio_9p_push_and_notify
,
226 /* virtio-9p device */
228 static const VMStateDescription vmstate_virtio_9p
= {
230 .minimum_version_id
= 1,
232 .fields
= (VMStateField
[]) {
233 VMSTATE_VIRTIO_DEVICE
,
234 VMSTATE_END_OF_LIST()
238 static Property virtio_9p_properties
[] = {
239 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState
, state
.fsconf
.tag
),
240 DEFINE_PROP_STRING("fsdev", V9fsVirtioState
, state
.fsconf
.fsdev_id
),
241 DEFINE_PROP_END_OF_LIST(),
244 static void virtio_9p_class_init(ObjectClass
*klass
, void *data
)
246 DeviceClass
*dc
= DEVICE_CLASS(klass
);
247 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
249 dc
->props
= virtio_9p_properties
;
250 dc
->vmsd
= &vmstate_virtio_9p
;
251 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
252 vdc
->realize
= virtio_9p_device_realize
;
253 vdc
->unrealize
= virtio_9p_device_unrealize
;
254 vdc
->get_features
= virtio_9p_get_features
;
255 vdc
->get_config
= virtio_9p_get_config
;
256 vdc
->reset
= virtio_9p_reset
;
259 static const TypeInfo virtio_device_info
= {
260 .name
= TYPE_VIRTIO_9P
,
261 .parent
= TYPE_VIRTIO_DEVICE
,
262 .instance_size
= sizeof(V9fsVirtioState
),
263 .class_init
= virtio_9p_class_init
,
266 static void virtio_9p_register_types(void)
268 type_register_static(&virtio_device_info
);
271 type_init(virtio_9p_register_types
)