1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#ifndef _LINUX_VRINGH_H
25#define _LINUX_VRINGH_H
26#include <uapi/linux/virtio_ring.h>
27#include <linux/virtio_byteorder.h>
28#include <linux/uio.h>
29#include <linux/slab.h>
30#include <asm/barrier.h>
31
32
33struct vringh {
34
35 bool little_endian;
36
37
38 bool event_indices;
39
40
41 bool weak_barriers;
42
43
44 u16 last_avail_idx;
45
46
47 u16 last_used_idx;
48
49
50 u32 completed;
51
52
53 struct vring vring;
54
55
56 void (*notify)(struct vringh *);
57};
58
59
60
61
62
63
64
65
66
67
68
69
70struct virtio_device;
71typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
72struct vringh_config_ops {
73 int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs,
74 struct vringh *vrhs[], vrh_callback_t *callbacks[]);
75 void (*del_vrhs)(struct virtio_device *vdev);
76};
77
78
79struct vringh_range {
80 u64 start, end_incl;
81 u64 offset;
82};
83
84
85
86
87
88
89
90struct vringh_iov {
91 struct iovec *iov;
92 size_t consumed;
93 unsigned i, used, max_num;
94};
95
96
97
98
99
100
101
102struct vringh_kiov {
103 struct kvec *iov;
104 size_t consumed;
105 unsigned i, used, max_num;
106};
107
108
109#define VRINGH_IOV_ALLOCATED 0x8000000
110
111
112int vringh_init_user(struct vringh *vrh, u64 features,
113 unsigned int num, bool weak_barriers,
114 struct vring_desc __user *desc,
115 struct vring_avail __user *avail,
116 struct vring_used __user *used);
117
118static inline void vringh_iov_init(struct vringh_iov *iov,
119 struct iovec *iovec, unsigned num)
120{
121 iov->used = iov->i = 0;
122 iov->consumed = 0;
123 iov->max_num = num;
124 iov->iov = iovec;
125}
126
127static inline void vringh_iov_reset(struct vringh_iov *iov)
128{
129 iov->iov[iov->i].iov_len += iov->consumed;
130 iov->iov[iov->i].iov_base -= iov->consumed;
131 iov->consumed = 0;
132 iov->i = 0;
133}
134
135static inline void vringh_iov_cleanup(struct vringh_iov *iov)
136{
137 if (iov->max_num & VRINGH_IOV_ALLOCATED)
138 kfree(iov->iov);
139 iov->max_num = iov->used = iov->i = iov->consumed = 0;
140 iov->iov = NULL;
141}
142
143
144int vringh_getdesc_user(struct vringh *vrh,
145 struct vringh_iov *riov,
146 struct vringh_iov *wiov,
147 bool (*getrange)(struct vringh *vrh,
148 u64 addr, struct vringh_range *r),
149 u16 *head);
150
151
152ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
153
154
155ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
156 const void *src, size_t len);
157
158
159int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
160int vringh_complete_multi_user(struct vringh *vrh,
161 const struct vring_used_elem used[],
162 unsigned num_used);
163
164
165void vringh_abandon_user(struct vringh *vrh, unsigned int num);
166
167
168int vringh_need_notify_user(struct vringh *vrh);
169
170bool vringh_notify_enable_user(struct vringh *vrh);
171void vringh_notify_disable_user(struct vringh *vrh);
172
173
174int vringh_init_kern(struct vringh *vrh, u64 features,
175 unsigned int num, bool weak_barriers,
176 struct vring_desc *desc,
177 struct vring_avail *avail,
178 struct vring_used *used);
179
180static inline void vringh_kiov_init(struct vringh_kiov *kiov,
181 struct kvec *kvec, unsigned num)
182{
183 kiov->used = kiov->i = 0;
184 kiov->consumed = 0;
185 kiov->max_num = num;
186 kiov->iov = kvec;
187}
188
189static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
190{
191 kiov->iov[kiov->i].iov_len += kiov->consumed;
192 kiov->iov[kiov->i].iov_base -= kiov->consumed;
193 kiov->consumed = 0;
194 kiov->i = 0;
195}
196
197static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
198{
199 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
200 kfree(kiov->iov);
201 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
202 kiov->iov = NULL;
203}
204
205int vringh_getdesc_kern(struct vringh *vrh,
206 struct vringh_kiov *riov,
207 struct vringh_kiov *wiov,
208 u16 *head,
209 gfp_t gfp);
210
211ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
212ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
213 const void *src, size_t len);
214void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
215int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
216
217bool vringh_notify_enable_kern(struct vringh *vrh);
218void vringh_notify_disable_kern(struct vringh *vrh);
219
220int vringh_need_notify_kern(struct vringh *vrh);
221
222
223static inline void vringh_notify(struct vringh *vrh)
224{
225 if (vrh->notify)
226 vrh->notify(vrh);
227}
228
229static inline bool vringh_is_little_endian(const struct vringh *vrh)
230{
231 return vrh->little_endian ||
232 virtio_legacy_is_little_endian();
233}
234
235static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
236{
237 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
238}
239
240static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
241{
242 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
243}
244
245static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
246{
247 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
248}
249
250static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
251{
252 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
253}
254
255static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
256{
257 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
258}
259
260static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
261{
262 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
263}
264#endif
265