1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Linux host-side vring helpers; for when the kernel needs to access
4 * someone else's vring.
6 * Copyright IBM Corporation, 2013.
7 * Parts taken from drivers/vhost/vhost.c Copyright 2009 Red Hat, Inc.
9 * Written by: Rusty Russell <rusty@rustcorp.com.au>
11 #ifndef _LINUX_VRINGH_H
12 #define _LINUX_VRINGH_H
13 #include <uapi/linux/virtio_ring.h>
14 #include <linux/virtio_byteorder.h>
15 #include <linux/uio.h>
16 #include <linux/slab.h>
17 #include <linux/dma-direction.h>
18 #include <linux/vhost_iotlb.h>
19 #include <asm/barrier.h>
21 /* virtio_ring with information needed for host access. */
23 /* Everything is little endian */
26 /* Guest publishes used event idx (note: we always do). */
29 /* Can we get away with weak barriers? */
32 /* Last available index we saw (ie. where we're up to). */
35 /* Last index we used. */
38 /* How many descriptors we've completed since last need_notify(). */
41 /* The vring (note: it may contain user pointers!) */
44 /* IOTLB for this vring */
45 struct vhost_iotlb *iotlb;
47 /* The function to call to notify the guest about added buffers */
48 void (*notify)(struct vringh *);
52 * struct vringh_config_ops - ops for creating a host vring from a virtio driver
53 * @find_vrhs: find the host vrings and instantiate them
54 * vdev: the virtio_device
55 * nhvrs: the number of host vrings to find
56 * hvrs: on success, includes new host vrings
57 * callbacks: array of driver callbacks, for each host vring
58 * include a NULL entry for vqs that do not need a callback
59 * Returns 0 on success or error status
60 * @del_vrhs: free the host vrings found by find_vrhs().
63 typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
64 struct vringh_config_ops {
65 int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs,
66 struct vringh *vrhs[], vrh_callback_t *callbacks[]);
67 void (*del_vrhs)(struct virtio_device *vdev);
70 /* The memory the vring can access, and what offset to apply. */
77 * struct vringh_iov - iovec mangler.
79 * Mangles iovec in place, and restores it.
80 * Remaining data is iov + i, of used - i elements.
84 size_t consumed; /* Within iov[i] */
85 unsigned i, used, max_num;
89 * struct vringh_iov - kvec mangler.
91 * Mangles kvec in place, and restores it.
92 * Remaining data is iov + i, of used - i elements.
96 size_t consumed; /* Within iov[i] */
97 unsigned i, used, max_num;
100 /* Flag on max_num to indicate we're kmalloced. */
101 #define VRINGH_IOV_ALLOCATED 0x8000000
103 /* Helpers for userspace vrings. */
104 int vringh_init_user(struct vringh *vrh, u64 features,
105 unsigned int num, bool weak_barriers,
106 struct vring_desc __user *desc,
107 struct vring_avail __user *avail,
108 struct vring_used __user *used);
110 static inline void vringh_iov_init(struct vringh_iov *iov,
111 struct iovec *iovec, unsigned num)
113 iov->used = iov->i = 0;
119 static inline void vringh_iov_reset(struct vringh_iov *iov)
121 iov->iov[iov->i].iov_len += iov->consumed;
122 iov->iov[iov->i].iov_base -= iov->consumed;
127 static inline void vringh_iov_cleanup(struct vringh_iov *iov)
129 if (iov->max_num & VRINGH_IOV_ALLOCATED)
131 iov->max_num = iov->used = iov->i = iov->consumed = 0;
135 /* Convert a descriptor into iovecs. */
136 int vringh_getdesc_user(struct vringh *vrh,
137 struct vringh_iov *riov,
138 struct vringh_iov *wiov,
139 bool (*getrange)(struct vringh *vrh,
140 u64 addr, struct vringh_range *r),
143 /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
144 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
146 /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
147 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
148 const void *src, size_t len);
150 /* Mark a descriptor as used. */
151 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
152 int vringh_complete_multi_user(struct vringh *vrh,
153 const struct vring_used_elem used[],
156 /* Pretend we've never seen descriptor (for easy error handling). */
157 void vringh_abandon_user(struct vringh *vrh, unsigned int num);
159 /* Do we need to fire the eventfd to notify the other side? */
160 int vringh_need_notify_user(struct vringh *vrh);
162 bool vringh_notify_enable_user(struct vringh *vrh);
163 void vringh_notify_disable_user(struct vringh *vrh);
165 /* Helpers for kernelspace vrings. */
166 int vringh_init_kern(struct vringh *vrh, u64 features,
167 unsigned int num, bool weak_barriers,
168 struct vring_desc *desc,
169 struct vring_avail *avail,
170 struct vring_used *used);
172 static inline void vringh_kiov_init(struct vringh_kiov *kiov,
173 struct kvec *kvec, unsigned num)
175 kiov->used = kiov->i = 0;
181 static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
183 kiov->iov[kiov->i].iov_len += kiov->consumed;
184 kiov->iov[kiov->i].iov_base -= kiov->consumed;
189 static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
191 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
193 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
197 int vringh_getdesc_kern(struct vringh *vrh,
198 struct vringh_kiov *riov,
199 struct vringh_kiov *wiov,
203 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
204 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
205 const void *src, size_t len);
206 void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
207 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
209 bool vringh_notify_enable_kern(struct vringh *vrh);
210 void vringh_notify_disable_kern(struct vringh *vrh);
212 int vringh_need_notify_kern(struct vringh *vrh);
214 /* Notify the guest about buffers added to the used ring */
215 static inline void vringh_notify(struct vringh *vrh)
221 static inline bool vringh_is_little_endian(const struct vringh *vrh)
223 return vrh->little_endian ||
224 virtio_legacy_is_little_endian();
227 static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
229 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
232 static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
234 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
237 static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
239 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
242 static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
244 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
247 static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
249 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
252 static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
254 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
257 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb);
259 int vringh_init_iotlb(struct vringh *vrh, u64 features,
260 unsigned int num, bool weak_barriers,
261 struct vring_desc *desc,
262 struct vring_avail *avail,
263 struct vring_used *used);
265 int vringh_getdesc_iotlb(struct vringh *vrh,
266 struct vringh_kiov *riov,
267 struct vringh_kiov *wiov,
271 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
272 struct vringh_kiov *riov,
273 void *dst, size_t len);
274 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
275 struct vringh_kiov *wiov,
276 const void *src, size_t len);
278 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num);
280 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len);
282 bool vringh_notify_enable_iotlb(struct vringh *vrh);
283 void vringh_notify_disable_iotlb(struct vringh *vrh);
285 int vringh_need_notify_iotlb(struct vringh *vrh);
287 #endif /* _LINUX_VRINGH_H */