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 void virtio_9p_push_and_notify(V9fsPDU
*pdu
)
25 V9fsState
*s
= pdu
->s
;
26 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
27 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
29 /* push onto queue and notify */
30 virtqueue_push(v
->vq
, elem
, pdu
->size
);
32 v
->elems
[pdu
->idx
] = NULL
;
34 /* FIXME: we should batch these completions */
35 virtio_notify(VIRTIO_DEVICE(v
), v
->vq
);
38 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
40 V9fsVirtioState
*v
= (V9fsVirtioState
*)vdev
;
41 V9fsState
*s
= &v
->state
;
44 VirtQueueElement
*elem
;
46 while ((pdu
= pdu_alloc(s
))) {
49 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
54 if (iov_size(elem
->in_sg
, elem
->in_num
) < 7) {
56 "The guest sent a VirtFS request without space for "
61 len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0, &out
, 7);
63 virtio_error(vdev
, "The guest sent a malformed VirtFS request: "
64 "header size is %zd, should be 7", len
);
68 v
->elems
[pdu
->idx
] = elem
;
70 pdu_submit(pdu
, &out
);
76 virtqueue_detach_element(vq
, elem
, 0);
82 static uint64_t virtio_9p_get_features(VirtIODevice
*vdev
, uint64_t features
,
85 virtio_add_feature(&features
, VIRTIO_9P_MOUNT_TAG
);
89 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
92 struct virtio_9p_config
*cfg
;
93 V9fsVirtioState
*v
= VIRTIO_9P(vdev
);
94 V9fsState
*s
= &v
->state
;
97 cfg
= g_malloc0(sizeof(struct virtio_9p_config
) + len
);
98 virtio_stw_p(vdev
, &cfg
->tag_len
, len
);
99 /* We don't copy the terminating null to config space */
100 memcpy(cfg
->tag
, s
->tag
, len
);
101 memcpy(config
, cfg
, v
->config_size
);
105 static void virtio_9p_reset(VirtIODevice
*vdev
)
107 V9fsVirtioState
*v
= (V9fsVirtioState
*)vdev
;
109 v9fs_reset(&v
->state
);
112 static ssize_t
virtio_pdu_vmarshal(V9fsPDU
*pdu
, size_t offset
,
113 const char *fmt
, va_list ap
)
115 V9fsState
*s
= pdu
->s
;
116 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
117 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
120 ret
= v9fs_iov_vmarshal(elem
->in_sg
, elem
->in_num
, offset
, 1, fmt
, ap
);
122 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
124 virtio_error(vdev
, "Failed to encode VirtFS reply type %d",
130 static ssize_t
virtio_pdu_vunmarshal(V9fsPDU
*pdu
, size_t offset
,
131 const char *fmt
, va_list ap
)
133 V9fsState
*s
= pdu
->s
;
134 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
135 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
138 ret
= v9fs_iov_vunmarshal(elem
->out_sg
, elem
->out_num
, offset
, 1, fmt
, ap
);
140 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
142 virtio_error(vdev
, "Failed to decode VirtFS request type %d", pdu
->id
);
147 static void virtio_init_in_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
148 unsigned int *pniov
, size_t size
)
150 V9fsState
*s
= pdu
->s
;
151 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
152 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
153 size_t buf_size
= iov_size(elem
->in_sg
, elem
->in_num
);
155 if (buf_size
< size
) {
156 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
159 "VirtFS reply type %d needs %zu bytes, buffer has %zu",
160 pdu
->id
+ 1, size
, buf_size
);
164 *pniov
= elem
->in_num
;
167 static void virtio_init_out_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
168 unsigned int *pniov
, size_t size
)
170 V9fsState
*s
= pdu
->s
;
171 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
172 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
173 size_t buf_size
= iov_size(elem
->out_sg
, elem
->out_num
);
175 if (buf_size
< size
) {
176 VirtIODevice
*vdev
= VIRTIO_DEVICE(v
);
179 "VirtFS request type %d needs %zu bytes, buffer has %zu",
180 pdu
->id
, size
, buf_size
);
183 *piov
= elem
->out_sg
;
184 *pniov
= elem
->out_num
;
187 static const 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 static void virtio_9p_device_realize(DeviceState
*dev
, Error
**errp
)
197 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
198 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
199 V9fsState
*s
= &v
->state
;
201 if (v9fs_device_realize_common(s
, &virtio_9p_transport
, errp
)) {
205 v
->config_size
= sizeof(struct virtio_9p_config
) + strlen(s
->fsconf
.tag
);
206 virtio_init(vdev
, "virtio-9p", VIRTIO_ID_9P
, v
->config_size
);
207 v
->vq
= virtio_add_queue(vdev
, MAX_REQ
, handle_9p_output
);
210 static void virtio_9p_device_unrealize(DeviceState
*dev
, Error
**errp
)
212 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
213 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
214 V9fsState
*s
= &v
->state
;
216 virtio_cleanup(vdev
);
217 v9fs_device_unrealize_common(s
, errp
);
220 /* virtio-9p device */
222 static const VMStateDescription vmstate_virtio_9p
= {
224 .minimum_version_id
= 1,
226 .fields
= (VMStateField
[]) {
227 VMSTATE_VIRTIO_DEVICE
,
228 VMSTATE_END_OF_LIST()
232 static Property virtio_9p_properties
[] = {
233 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState
, state
.fsconf
.tag
),
234 DEFINE_PROP_STRING("fsdev", V9fsVirtioState
, state
.fsconf
.fsdev_id
),
235 DEFINE_PROP_END_OF_LIST(),
238 static void virtio_9p_class_init(ObjectClass
*klass
, void *data
)
240 DeviceClass
*dc
= DEVICE_CLASS(klass
);
241 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
243 dc
->props
= virtio_9p_properties
;
244 dc
->vmsd
= &vmstate_virtio_9p
;
245 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
246 vdc
->realize
= virtio_9p_device_realize
;
247 vdc
->unrealize
= virtio_9p_device_unrealize
;
248 vdc
->get_features
= virtio_9p_get_features
;
249 vdc
->get_config
= virtio_9p_get_config
;
250 vdc
->reset
= virtio_9p_reset
;
253 static const TypeInfo virtio_device_info
= {
254 .name
= TYPE_VIRTIO_9P
,
255 .parent
= TYPE_VIRTIO_DEVICE
,
256 .instance_size
= sizeof(V9fsVirtioState
),
257 .class_init
= virtio_9p_class_init
,
260 static void virtio_9p_register_types(void)
262 type_register_static(&virtio_device_info
);
265 type_init(virtio_9p_register_types
)