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 #define VMSTATE_VIRTIO_DEVICE_USE_NEW
16 #include "qemu/osdep.h"
17 #include "hw/virtio/virtio.h"
18 #include "qemu/sockets.h"
19 #include "virtio-9p.h"
20 #include "fsdev/qemu-fsdev.h"
22 #include "hw/virtio/virtio-access.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
);
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
))) {
55 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
60 if (elem
->in_num
== 0) {
62 "The guest sent a VirtFS request without space for "
66 QEMU_BUILD_BUG_ON(sizeof(out
) != 7);
68 v
->elems
[pdu
->idx
] = elem
;
69 len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
71 if (len
!= sizeof(out
)) {
72 virtio_error(vdev
, "The guest sent a malformed VirtFS request: "
73 "header size is %zd, should be 7", len
);
77 pdu
->size
= le32_to_cpu(out
.size_le
);
80 pdu
->tag
= le16_to_cpu(out
.tag_le
);
82 qemu_co_queue_init(&pdu
->complete
);
89 virtqueue_detach_element(vq
, elem
, 0);
95 static uint64_t virtio_9p_get_features(VirtIODevice
*vdev
, uint64_t features
,
98 virtio_add_feature(&features
, VIRTIO_9P_MOUNT_TAG
);
102 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
105 struct virtio_9p_config
*cfg
;
106 V9fsVirtioState
*v
= VIRTIO_9P(vdev
);
107 V9fsState
*s
= &v
->state
;
109 len
= strlen(s
->tag
);
110 cfg
= g_malloc0(sizeof(struct virtio_9p_config
) + len
);
111 virtio_stw_p(vdev
, &cfg
->tag_len
, len
);
112 /* We don't copy the terminating null to config space */
113 memcpy(cfg
->tag
, s
->tag
, len
);
114 memcpy(config
, cfg
, v
->config_size
);
118 static void virtio_9p_device_realize(DeviceState
*dev
, Error
**errp
)
120 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
121 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
122 V9fsState
*s
= &v
->state
;
124 if (v9fs_device_realize_common(s
, errp
)) {
128 v
->config_size
= sizeof(struct virtio_9p_config
) + strlen(s
->fsconf
.tag
);
129 virtio_init(vdev
, "virtio-9p", VIRTIO_ID_9P
, v
->config_size
);
130 v
->vq
= virtio_add_queue(vdev
, MAX_REQ
, handle_9p_output
);
136 static void virtio_9p_device_unrealize(DeviceState
*dev
, Error
**errp
)
138 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
139 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
140 V9fsState
*s
= &v
->state
;
142 virtio_cleanup(vdev
);
143 v9fs_device_unrealize_common(s
, errp
);
146 ssize_t
virtio_pdu_vmarshal(V9fsPDU
*pdu
, size_t offset
,
147 const char *fmt
, va_list ap
)
149 V9fsState
*s
= pdu
->s
;
150 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
151 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
153 return v9fs_iov_vmarshal(elem
->in_sg
, elem
->in_num
, offset
, 1, fmt
, ap
);
156 ssize_t
virtio_pdu_vunmarshal(V9fsPDU
*pdu
, size_t offset
,
157 const char *fmt
, va_list ap
)
159 V9fsState
*s
= pdu
->s
;
160 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
161 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
163 return v9fs_iov_vunmarshal(elem
->out_sg
, elem
->out_num
, offset
, 1, fmt
, ap
);
166 void virtio_init_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
167 unsigned int *pniov
, bool is_write
)
169 V9fsState
*s
= pdu
->s
;
170 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
171 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
174 *piov
= elem
->out_sg
;
175 *pniov
= elem
->out_num
;
178 *pniov
= elem
->in_num
;
182 /* virtio-9p device */
184 static const VMStateDescription vmstate_virtio_9p
= {
186 .minimum_version_id
= 1,
188 .fields
= (VMStateField
[]) {
189 VMSTATE_VIRTIO_DEVICE
,
190 VMSTATE_END_OF_LIST()
194 static Property virtio_9p_properties
[] = {
195 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState
, state
.fsconf
.tag
),
196 DEFINE_PROP_STRING("fsdev", V9fsVirtioState
, state
.fsconf
.fsdev_id
),
197 DEFINE_PROP_END_OF_LIST(),
200 static void virtio_9p_class_init(ObjectClass
*klass
, void *data
)
202 DeviceClass
*dc
= DEVICE_CLASS(klass
);
203 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
205 dc
->props
= virtio_9p_properties
;
206 dc
->vmsd
= &vmstate_virtio_9p
;
207 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
208 vdc
->realize
= virtio_9p_device_realize
;
209 vdc
->unrealize
= virtio_9p_device_unrealize
;
210 vdc
->get_features
= virtio_9p_get_features
;
211 vdc
->get_config
= virtio_9p_get_config
;
214 static const TypeInfo virtio_device_info
= {
215 .name
= TYPE_VIRTIO_9P
,
216 .parent
= TYPE_VIRTIO_DEVICE
,
217 .instance_size
= sizeof(V9fsVirtioState
),
218 .class_init
= virtio_9p_class_init
,
221 static void virtio_9p_register_types(void)
223 type_register_static(&virtio_device_info
);
226 type_init(virtio_9p_register_types
)