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.
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/spinlock.h>
18 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
19 #include <linux/dma-direction.h>
20 #include <linux/vhost_iotlb.h>
22 #include <asm/barrier.h>
24 /* virtio_ring with information needed for host access. */
26 /* Everything is little endian */
29 /* Guest publishes used event idx (note: we always do). */
32 /* Can we get away with weak barriers? */
35 /* Last available index we saw (ie. where we're up to). */
38 /* Last index we used. */
41 /* How many descriptors we've completed since last need_notify(). */
44 /* The vring (note: it may contain user pointers!) */
47 /* IOTLB for this vring */
48 struct vhost_iotlb *iotlb;
50 /* spinlock to synchronize IOTLB accesses */
51 spinlock_t *iotlb_lock;
53 /* The function to call to notify the guest about added buffers */
54 void (*notify)(struct vringh *);
58 * struct vringh_config_ops - ops for creating a host vring from a virtio driver
59 * @find_vrhs: find the host vrings and instantiate them
60 * vdev: the virtio_device
61 * nhvrs: the number of host vrings to find
62 * hvrs: on success, includes new host vrings
63 * callbacks: array of driver callbacks, for each host vring
64 * include a NULL entry for vqs that do not need a callback
65 * Returns 0 on success or error status
66 * @del_vrhs: free the host vrings found by find_vrhs().
69 typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
70 struct vringh_config_ops {
71 int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs,
72 struct vringh *vrhs[], vrh_callback_t *callbacks[]);
73 void (*del_vrhs)(struct virtio_device *vdev);
76 /* The memory the vring can access, and what offset to apply. */
83 * struct vringh_iov - iovec mangler.
85 * Mangles iovec in place, and restores it.
86 * Remaining data is iov + i, of used - i elements.
90 size_t consumed; /* Within iov[i] */
91 unsigned i, used, max_num;
95 * struct vringh_iov - kvec mangler.
97 * Mangles kvec in place, and restores it.
98 * Remaining data is iov + i, of used - i elements.
102 size_t consumed; /* Within iov[i] */
103 unsigned i, used, max_num;
106 /* Flag on max_num to indicate we're kmalloced. */
107 #define VRINGH_IOV_ALLOCATED 0x8000000
109 /* Helpers for userspace vrings. */
110 int vringh_init_user(struct vringh *vrh, u64 features,
111 unsigned int num, bool weak_barriers,
112 vring_desc_t __user *desc,
113 vring_avail_t __user *avail,
114 vring_used_t __user *used);
116 static inline void vringh_iov_init(struct vringh_iov *iov,
117 struct iovec *iovec, unsigned num)
119 iov->used = iov->i = 0;
125 static inline void vringh_iov_reset(struct vringh_iov *iov)
127 iov->iov[iov->i].iov_len += iov->consumed;
128 iov->iov[iov->i].iov_base -= iov->consumed;
133 static inline void vringh_iov_cleanup(struct vringh_iov *iov)
135 if (iov->max_num & VRINGH_IOV_ALLOCATED)
137 iov->max_num = iov->used = iov->i = iov->consumed = 0;
141 /* Convert a descriptor into iovecs. */
142 int vringh_getdesc_user(struct vringh *vrh,
143 struct vringh_iov *riov,
144 struct vringh_iov *wiov,
145 bool (*getrange)(struct vringh *vrh,
146 u64 addr, struct vringh_range *r),
149 /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
150 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
152 /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
153 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
154 const void *src, size_t len);
156 /* Mark a descriptor as used. */
157 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
158 int vringh_complete_multi_user(struct vringh *vrh,
159 const struct vring_used_elem used[],
162 /* Pretend we've never seen descriptor (for easy error handling). */
163 void vringh_abandon_user(struct vringh *vrh, unsigned int num);
165 /* Do we need to fire the eventfd to notify the other side? */
166 int vringh_need_notify_user(struct vringh *vrh);
168 bool vringh_notify_enable_user(struct vringh *vrh);
169 void vringh_notify_disable_user(struct vringh *vrh);
171 /* Helpers for kernelspace vrings. */
172 int vringh_init_kern(struct vringh *vrh, u64 features,
173 unsigned int num, bool weak_barriers,
174 struct vring_desc *desc,
175 struct vring_avail *avail,
176 struct vring_used *used);
178 static inline void vringh_kiov_init(struct vringh_kiov *kiov,
179 struct kvec *kvec, unsigned num)
181 kiov->used = kiov->i = 0;
187 static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
189 kiov->iov[kiov->i].iov_len += kiov->consumed;
190 kiov->iov[kiov->i].iov_base -= kiov->consumed;
195 static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
197 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
199 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
203 static inline size_t vringh_kiov_length(struct vringh_kiov *kiov)
208 for (i = kiov->i; i < kiov->used; i++)
209 len += kiov->iov[i].iov_len;
214 void vringh_kiov_advance(struct vringh_kiov *kiov, size_t len);
216 int vringh_getdesc_kern(struct vringh *vrh,
217 struct vringh_kiov *riov,
218 struct vringh_kiov *wiov,
222 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
223 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
224 const void *src, size_t len);
225 void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
226 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
228 bool vringh_notify_enable_kern(struct vringh *vrh);
229 void vringh_notify_disable_kern(struct vringh *vrh);
231 int vringh_need_notify_kern(struct vringh *vrh);
233 /* Notify the guest about buffers added to the used ring */
234 static inline void vringh_notify(struct vringh *vrh)
240 static inline bool vringh_is_little_endian(const struct vringh *vrh)
242 return vrh->little_endian ||
243 virtio_legacy_is_little_endian();
246 static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
248 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
251 static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
253 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
256 static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
258 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
261 static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
263 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
266 static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
268 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
271 static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
273 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
276 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
278 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb,
279 spinlock_t *iotlb_lock);
281 int vringh_init_iotlb(struct vringh *vrh, u64 features,
282 unsigned int num, bool weak_barriers,
283 struct vring_desc *desc,
284 struct vring_avail *avail,
285 struct vring_used *used);
287 int vringh_getdesc_iotlb(struct vringh *vrh,
288 struct vringh_kiov *riov,
289 struct vringh_kiov *wiov,
293 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
294 struct vringh_kiov *riov,
295 void *dst, size_t len);
296 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
297 struct vringh_kiov *wiov,
298 const void *src, size_t len);
300 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num);
302 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len);
304 bool vringh_notify_enable_iotlb(struct vringh *vrh);
305 void vringh_notify_disable_iotlb(struct vringh *vrh);
307 int vringh_need_notify_iotlb(struct vringh *vrh);
309 #endif /* CONFIG_VHOST_IOTLB */
311 #endif /* _LINUX_VRINGH_H */