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 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
;
45 while ((pdu
= pdu_alloc(s
))) {
51 VirtQueueElement
*elem
;
53 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
59 BUG_ON(elem
->out_num
== 0 || elem
->in_num
== 0);
60 QEMU_BUILD_BUG_ON(sizeof out
!= 7);
62 v
->elems
[pdu
->idx
] = elem
;
63 len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
65 BUG_ON(len
!= sizeof out
);
67 pdu
->size
= le32_to_cpu(out
.size_le
);
70 pdu
->tag
= le16_to_cpu(out
.tag_le
);
72 qemu_co_queue_init(&pdu
->complete
);
77 static uint64_t virtio_9p_get_features(VirtIODevice
*vdev
, uint64_t features
,
80 virtio_add_feature(&features
, VIRTIO_9P_MOUNT_TAG
);
84 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
87 struct virtio_9p_config
*cfg
;
88 V9fsVirtioState
*v
= VIRTIO_9P(vdev
);
89 V9fsState
*s
= &v
->state
;
92 cfg
= g_malloc0(sizeof(struct virtio_9p_config
) + len
);
93 virtio_stw_p(vdev
, &cfg
->tag_len
, len
);
94 /* We don't copy the terminating null to config space */
95 memcpy(cfg
->tag
, s
->tag
, len
);
96 memcpy(config
, cfg
, v
->config_size
);
100 static void virtio_9p_save(QEMUFile
*f
, void *opaque
)
102 virtio_save(VIRTIO_DEVICE(opaque
), f
);
105 static int virtio_9p_load(QEMUFile
*f
, void *opaque
, int version_id
)
107 return virtio_load(VIRTIO_DEVICE(opaque
), f
, version_id
);
110 static void virtio_9p_device_realize(DeviceState
*dev
, Error
**errp
)
112 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
113 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
114 V9fsState
*s
= &v
->state
;
116 if (v9fs_device_realize_common(s
, errp
)) {
120 v
->config_size
= sizeof(struct virtio_9p_config
) + strlen(s
->fsconf
.tag
);
121 virtio_init(vdev
, "virtio-9p", VIRTIO_ID_9P
, v
->config_size
);
122 v
->vq
= virtio_add_queue(vdev
, MAX_REQ
, handle_9p_output
);
123 register_savevm(dev
, "virtio-9p", -1, 1, virtio_9p_save
, virtio_9p_load
, v
);
129 static void virtio_9p_device_unrealize(DeviceState
*dev
, Error
**errp
)
131 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
132 V9fsVirtioState
*v
= VIRTIO_9P(dev
);
133 V9fsState
*s
= &v
->state
;
135 virtio_cleanup(vdev
);
136 unregister_savevm(dev
, "virtio-9p", v
);
137 v9fs_device_unrealize_common(s
, errp
);
140 ssize_t
virtio_pdu_vmarshal(V9fsPDU
*pdu
, size_t offset
,
141 const char *fmt
, va_list ap
)
143 V9fsState
*s
= pdu
->s
;
144 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
145 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
147 return v9fs_iov_vmarshal(elem
->in_sg
, elem
->in_num
, offset
, 1, fmt
, ap
);
150 ssize_t
virtio_pdu_vunmarshal(V9fsPDU
*pdu
, size_t offset
,
151 const char *fmt
, va_list ap
)
153 V9fsState
*s
= pdu
->s
;
154 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
155 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
157 return v9fs_iov_vunmarshal(elem
->out_sg
, elem
->out_num
, offset
, 1, fmt
, ap
);
160 void virtio_init_iov_from_pdu(V9fsPDU
*pdu
, struct iovec
**piov
,
161 unsigned int *pniov
, bool is_write
)
163 V9fsState
*s
= pdu
->s
;
164 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
165 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
168 *piov
= elem
->out_sg
;
169 *pniov
= elem
->out_num
;
172 *pniov
= elem
->in_num
;
176 /* virtio-9p device */
178 static Property virtio_9p_properties
[] = {
179 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState
, state
.fsconf
.tag
),
180 DEFINE_PROP_STRING("fsdev", V9fsVirtioState
, state
.fsconf
.fsdev_id
),
181 DEFINE_PROP_END_OF_LIST(),
184 static void virtio_9p_class_init(ObjectClass
*klass
, void *data
)
186 DeviceClass
*dc
= DEVICE_CLASS(klass
);
187 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
189 dc
->props
= virtio_9p_properties
;
190 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
191 vdc
->realize
= virtio_9p_device_realize
;
192 vdc
->unrealize
= virtio_9p_device_unrealize
;
193 vdc
->get_features
= virtio_9p_get_features
;
194 vdc
->get_config
= virtio_9p_get_config
;
197 static const TypeInfo virtio_device_info
= {
198 .name
= TYPE_VIRTIO_9P
,
199 .parent
= TYPE_VIRTIO_DEVICE
,
200 .instance_size
= sizeof(V9fsVirtioState
),
201 .class_init
= virtio_9p_class_init
,
204 static void virtio_9p_register_types(void)
206 type_register_static(&virtio_device_info
);
209 type_init(virtio_9p_register_types
)