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 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
18 #include <linux/dma-direction.h>
19 #include <linux/vhost_iotlb.h>
21 #include <asm/barrier.h>
23 /* virtio_ring with information needed for host access. */
25 /* Everything is little endian */
28 /* Guest publishes used event idx (note: we always do). */
31 /* Can we get away with weak barriers? */
34 /* Last available index we saw (ie. where we're up to). */
37 /* Last index we used. */
40 /* How many descriptors we've completed since last need_notify(). */
43 /* The vring (note: it may contain user pointers!) */
46 /* IOTLB for this vring */
47 struct vhost_iotlb *iotlb;
49 /* spinlock to synchronize IOTLB accesses */
50 spinlock_t *iotlb_lock;
52 /* The function to call to notify the guest about added buffers */
53 void (*notify)(struct vringh *);
57 * struct vringh_config_ops - ops for creating a host vring from a virtio driver
58 * @find_vrhs: find the host vrings and instantiate them
59 * vdev: the virtio_device
60 * nhvrs: the number of host vrings to find
61 * hvrs: on success, includes new host vrings
62 * callbacks: array of driver callbacks, for each host vring
63 * include a NULL entry for vqs that do not need a callback
64 * Returns 0 on success or error status
65 * @del_vrhs: free the host vrings found by find_vrhs().
68 typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
69 struct vringh_config_ops {
70 int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs,
71 struct vringh *vrhs[], vrh_callback_t *callbacks[]);
72 void (*del_vrhs)(struct virtio_device *vdev);
75 /* The memory the vring can access, and what offset to apply. */
82 * struct vringh_iov - iovec mangler.
84 * Mangles iovec in place, and restores it.
85 * Remaining data is iov + i, of used - i elements.
89 size_t consumed; /* Within iov[i] */
90 unsigned i, used, max_num;
94 * struct vringh_iov - kvec mangler.
96 * Mangles kvec in place, and restores it.
97 * Remaining data is iov + i, of used - i elements.
101 size_t consumed; /* Within iov[i] */
102 unsigned i, used, max_num;
105 /* Flag on max_num to indicate we're kmalloced. */
106 #define VRINGH_IOV_ALLOCATED 0x8000000
108 /* Helpers for userspace vrings. */
109 int vringh_init_user(struct vringh *vrh, u64 features,
110 unsigned int num, bool weak_barriers,
111 vring_desc_t __user *desc,
112 vring_avail_t __user *avail,
113 vring_used_t __user *used);
115 static inline void vringh_iov_init(struct vringh_iov *iov,
116 struct iovec *iovec, unsigned num)
118 iov->used = iov->i = 0;
124 static inline void vringh_iov_reset(struct vringh_iov *iov)
126 iov->iov[iov->i].iov_len += iov->consumed;
127 iov->iov[iov->i].iov_base -= iov->consumed;
132 static inline void vringh_iov_cleanup(struct vringh_iov *iov)
134 if (iov->max_num & VRINGH_IOV_ALLOCATED)
136 iov->max_num = iov->used = iov->i = iov->consumed = 0;
140 /* Convert a descriptor into iovecs. */
141 int vringh_getdesc_user(struct vringh *vrh,
142 struct vringh_iov *riov,
143 struct vringh_iov *wiov,
144 bool (*getrange)(struct vringh *vrh,
145 u64 addr, struct vringh_range *r),
148 /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
149 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
151 /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
152 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
153 const void *src, size_t len);
155 /* Mark a descriptor as used. */
156 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
157 int vringh_complete_multi_user(struct vringh *vrh,
158 const struct vring_used_elem used[],
161 /* Pretend we've never seen descriptor (for easy error handling). */
162 void vringh_abandon_user(struct vringh *vrh, unsigned int num);
164 /* Do we need to fire the eventfd to notify the other side? */
165 int vringh_need_notify_user(struct vringh *vrh);
167 bool vringh_notify_enable_user(struct vringh *vrh);
168 void vringh_notify_disable_user(struct vringh *vrh);
170 /* Helpers for kernelspace vrings. */
171 int vringh_init_kern(struct vringh *vrh, u64 features,
172 unsigned int num, bool weak_barriers,
173 struct vring_desc *desc,
174 struct vring_avail *avail,
175 struct vring_used *used);
177 static inline void vringh_kiov_init(struct vringh_kiov *kiov,
178 struct kvec *kvec, unsigned num)
180 kiov->used = kiov->i = 0;
186 static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
188 kiov->iov[kiov->i].iov_len += kiov->consumed;
189 kiov->iov[kiov->i].iov_base -= kiov->consumed;
194 static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
196 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
198 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
202 static inline size_t vringh_kiov_length(struct vringh_kiov *kiov)
207 for (i = kiov->i; i < kiov->used; i++)
208 len += kiov->iov[i].iov_len;
213 void vringh_kiov_advance(struct vringh_kiov *kiov, size_t len);
215 int vringh_getdesc_kern(struct vringh *vrh,
216 struct vringh_kiov *riov,
217 struct vringh_kiov *wiov,
221 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
222 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
223 const void *src, size_t len);
224 void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
225 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
227 bool vringh_notify_enable_kern(struct vringh *vrh);
228 void vringh_notify_disable_kern(struct vringh *vrh);
230 int vringh_need_notify_kern(struct vringh *vrh);
232 /* Notify the guest about buffers added to the used ring */
233 static inline void vringh_notify(struct vringh *vrh)
239 static inline bool vringh_is_little_endian(const struct vringh *vrh)
241 return vrh->little_endian ||
242 virtio_legacy_is_little_endian();
245 static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
247 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
250 static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
252 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
255 static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
257 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
260 static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
262 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
265 static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
267 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
270 static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
272 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
275 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
277 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb,
278 spinlock_t *iotlb_lock);
280 int vringh_init_iotlb(struct vringh *vrh, u64 features,
281 unsigned int num, bool weak_barriers,
282 struct vring_desc *desc,
283 struct vring_avail *avail,
284 struct vring_used *used);
286 int vringh_getdesc_iotlb(struct vringh *vrh,
287 struct vringh_kiov *riov,
288 struct vringh_kiov *wiov,
292 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
293 struct vringh_kiov *riov,
294 void *dst, size_t len);
295 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
296 struct vringh_kiov *wiov,
297 const void *src, size_t len);
299 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num);
301 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len);
303 bool vringh_notify_enable_iotlb(struct vringh *vrh);
304 void vringh_notify_disable_iotlb(struct vringh *vrh);
306 int vringh_need_notify_iotlb(struct vringh *vrh);
308 #endif /* CONFIG_VHOST_IOTLB */
310 #endif /* _LINUX_VRINGH_H */