]>
Commit | Line | Data |
---|---|---|
f4f61d27 AK |
1 | /* |
2 | * Virtio 9p backend | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
9b8bfe21 | 14 | #include "qemu/osdep.h" |
0d09e41a PB |
15 | #include "hw/virtio/virtio.h" |
16 | #include "hw/i386/pc.h" | |
1de7afc9 | 17 | #include "qemu/sockets.h" |
f4f61d27 AK |
18 | #include "virtio-9p.h" |
19 | #include "fsdev/qemu-fsdev.h" | |
267ae092 | 20 | #include "9p-xattr.h" |
fe52840c | 21 | #include "coth.h" |
d64ccb91 | 22 | #include "hw/virtio/virtio-access.h" |
0192cc5d | 23 | #include "qemu/iov.h" |
f4f61d27 | 24 | |
0d3716b4 WL |
25 | void virtio_9p_push_and_notify(V9fsPDU *pdu) |
26 | { | |
27 | V9fsState *s = pdu->s; | |
00588a0a WL |
28 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); |
29 | VirtQueueElement *elem = &v->elems[pdu->idx]; | |
0d3716b4 WL |
30 | |
31 | /* push onto queue and notify */ | |
00588a0a | 32 | virtqueue_push(v->vq, elem, pdu->size); |
0d3716b4 WL |
33 | |
34 | /* FIXME: we should batch these completions */ | |
00588a0a | 35 | virtio_notify(VIRTIO_DEVICE(v), v->vq); |
0d3716b4 WL |
36 | } |
37 | ||
0192cc5d WL |
38 | static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) |
39 | { | |
00588a0a WL |
40 | V9fsVirtioState *v = (V9fsVirtioState *)vdev; |
41 | V9fsState *s = &v->state; | |
0192cc5d WL |
42 | V9fsPDU *pdu; |
43 | ssize_t len; | |
44 | ||
00588a0a | 45 | while ((pdu = pdu_alloc(s))) { |
0192cc5d WL |
46 | struct { |
47 | uint32_t size_le; | |
48 | uint8_t id; | |
49 | uint16_t tag_le; | |
50 | } QEMU_PACKED out; | |
00588a0a | 51 | VirtQueueElement *elem = &v->elems[pdu->idx]; |
0192cc5d | 52 | |
00588a0a WL |
53 | len = virtqueue_pop(vq, elem); |
54 | if (!len) { | |
55 | pdu_free(pdu); | |
56 | break; | |
57 | } | |
58 | ||
59 | BUG_ON(elem->out_num == 0 || elem->in_num == 0); | |
0192cc5d WL |
60 | QEMU_BUILD_BUG_ON(sizeof out != 7); |
61 | ||
00588a0a | 62 | len = iov_to_buf(elem->out_sg, elem->out_num, 0, |
0192cc5d WL |
63 | &out, sizeof out); |
64 | BUG_ON(len != sizeof out); | |
65 | ||
66 | pdu->size = le32_to_cpu(out.size_le); | |
67 | ||
68 | pdu->id = out.id; | |
69 | pdu->tag = le16_to_cpu(out.tag_le); | |
70 | ||
71 | qemu_co_queue_init(&pdu->complete); | |
72 | pdu_submit(pdu); | |
73 | } | |
0192cc5d WL |
74 | } |
75 | ||
9d5b731d JW |
76 | static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, |
77 | Error **errp) | |
f4f61d27 | 78 | { |
0cd09c3a | 79 | virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); |
f4f61d27 AK |
80 | return features; |
81 | } | |
82 | ||
f4f61d27 AK |
83 | static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) |
84 | { | |
e9a0152b | 85 | int len; |
f4f61d27 | 86 | struct virtio_9p_config *cfg; |
00588a0a WL |
87 | V9fsVirtioState *v = VIRTIO_9P(vdev); |
88 | V9fsState *s = &v->state; | |
f4f61d27 | 89 | |
e9a0152b AK |
90 | len = strlen(s->tag); |
91 | cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); | |
d64ccb91 | 92 | virtio_stw_p(vdev, &cfg->tag_len, len); |
e9a0152b AK |
93 | /* We don't copy the terminating null to config space */ |
94 | memcpy(cfg->tag, s->tag, len); | |
00588a0a | 95 | memcpy(config, cfg, v->config_size); |
7267c094 | 96 | g_free(cfg); |
f4f61d27 AK |
97 | } |
98 | ||
4652f164 GK |
99 | static void virtio_9p_save(QEMUFile *f, void *opaque) |
100 | { | |
101 | virtio_save(VIRTIO_DEVICE(opaque), f); | |
102 | } | |
103 | ||
104 | static int virtio_9p_load(QEMUFile *f, void *opaque, int version_id) | |
105 | { | |
106 | return virtio_load(VIRTIO_DEVICE(opaque), f, version_id); | |
107 | } | |
108 | ||
59be7522 | 109 | static void virtio_9p_device_realize(DeviceState *dev, Error **errp) |
0174fe73 | 110 | { |
59be7522 | 111 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
00588a0a WL |
112 | V9fsVirtioState *v = VIRTIO_9P(dev); |
113 | V9fsState *s = &v->state; | |
f4f61d27 | 114 | |
2a0c56aa | 115 | if (v9fs_device_realize_common(s, errp)) { |
92304bf3 | 116 | goto out; |
f4f61d27 | 117 | } |
e9a0152b | 118 | |
00588a0a WL |
119 | v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag); |
120 | virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size); | |
121 | v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); | |
122 | register_savevm(dev, "virtio-9p", -1, 1, virtio_9p_save, virtio_9p_load, v); | |
2a0c56aa | 123 | |
92304bf3 | 124 | out: |
2a0c56aa | 125 | return; |
e7303c43 FK |
126 | } |
127 | ||
6cecf093 GK |
128 | static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) |
129 | { | |
130 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
00588a0a WL |
131 | V9fsVirtioState *v = VIRTIO_9P(dev); |
132 | V9fsState *s = &v->state; | |
6cecf093 GK |
133 | |
134 | virtio_cleanup(vdev); | |
00588a0a | 135 | unregister_savevm(dev, "virtio-9p", v); |
2a0c56aa | 136 | v9fs_device_unrealize_common(s, errp); |
6cecf093 GK |
137 | } |
138 | ||
fe9fa96d WL |
139 | ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset, |
140 | const char *fmt, va_list ap) | |
141 | { | |
00588a0a WL |
142 | V9fsState *s = pdu->s; |
143 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); | |
144 | VirtQueueElement *elem = &v->elems[pdu->idx]; | |
145 | ||
146 | return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap); | |
fe9fa96d WL |
147 | } |
148 | ||
149 | ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset, | |
150 | const char *fmt, va_list ap) | |
151 | { | |
00588a0a WL |
152 | V9fsState *s = pdu->s; |
153 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); | |
154 | VirtQueueElement *elem = &v->elems[pdu->idx]; | |
155 | ||
156 | return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap); | |
fe9fa96d WL |
157 | } |
158 | ||
592707af WL |
159 | void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, |
160 | unsigned int *pniov, bool is_write) | |
161 | { | |
00588a0a WL |
162 | V9fsState *s = pdu->s; |
163 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); | |
164 | VirtQueueElement *elem = &v->elems[pdu->idx]; | |
165 | ||
592707af | 166 | if (is_write) { |
00588a0a WL |
167 | *piov = elem->out_sg; |
168 | *pniov = elem->out_num; | |
592707af | 169 | } else { |
00588a0a WL |
170 | *piov = elem->in_sg; |
171 | *pniov = elem->in_num; | |
592707af WL |
172 | } |
173 | } | |
174 | ||
e7303c43 FK |
175 | /* virtio-9p device */ |
176 | ||
e7303c43 | 177 | static Property virtio_9p_properties[] = { |
00588a0a WL |
178 | DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag), |
179 | DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id), | |
e7303c43 FK |
180 | DEFINE_PROP_END_OF_LIST(), |
181 | }; | |
182 | ||
183 | static void virtio_9p_class_init(ObjectClass *klass, void *data) | |
184 | { | |
185 | DeviceClass *dc = DEVICE_CLASS(klass); | |
186 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
59be7522 | 187 | |
e7303c43 | 188 | dc->props = virtio_9p_properties; |
125ee0ed | 189 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
59be7522 | 190 | vdc->realize = virtio_9p_device_realize; |
6cecf093 | 191 | vdc->unrealize = virtio_9p_device_unrealize; |
e7303c43 FK |
192 | vdc->get_features = virtio_9p_get_features; |
193 | vdc->get_config = virtio_9p_get_config; | |
194 | } | |
195 | ||
196 | static const TypeInfo virtio_device_info = { | |
197 | .name = TYPE_VIRTIO_9P, | |
198 | .parent = TYPE_VIRTIO_DEVICE, | |
00588a0a | 199 | .instance_size = sizeof(V9fsVirtioState), |
e7303c43 FK |
200 | .class_init = virtio_9p_class_init, |
201 | }; | |
202 | ||
203 | static void virtio_9p_register_types(void) | |
204 | { | |
205 | type_register_static(&virtio_device_info); | |
206 | } | |
207 | ||
208 | type_init(virtio_9p_register_types) |