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 /* The function to call to notify the guest about added buffers */
50 void (*notify)(struct vringh *);
54 * struct vringh_config_ops - ops for creating a host vring from a virtio driver
55 * @find_vrhs: find the host vrings and instantiate them
56 * vdev: the virtio_device
57 * nhvrs: the number of host vrings to find
58 * hvrs: on success, includes new host vrings
59 * callbacks: array of driver callbacks, for each host vring
60 * include a NULL entry for vqs that do not need a callback
61 * Returns 0 on success or error status
62 * @del_vrhs: free the host vrings found by find_vrhs().
65 typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
66 struct vringh_config_ops {
67 int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs,
68 struct vringh *vrhs[], vrh_callback_t *callbacks[]);
69 void (*del_vrhs)(struct virtio_device *vdev);
72 /* The memory the vring can access, and what offset to apply. */
79 * struct vringh_iov - iovec mangler.
81 * Mangles iovec in place, and restores it.
82 * Remaining data is iov + i, of used - i elements.
86 size_t consumed; /* Within iov[i] */
87 unsigned i, used, max_num;
91 * struct vringh_iov - kvec mangler.
93 * Mangles kvec in place, and restores it.
94 * Remaining data is iov + i, of used - i elements.
98 size_t consumed; /* Within iov[i] */
99 unsigned i, used, max_num;
102 /* Flag on max_num to indicate we're kmalloced. */
103 #define VRINGH_IOV_ALLOCATED 0x8000000
105 /* Helpers for userspace vrings. */
106 int vringh_init_user(struct vringh *vrh, u64 features,
107 unsigned int num, bool weak_barriers,
108 vring_desc_t __user *desc,
109 vring_avail_t __user *avail,
110 vring_used_t __user *used);
112 static inline void vringh_iov_init(struct vringh_iov *iov,
113 struct iovec *iovec, unsigned num)
115 iov->used = iov->i = 0;
121 static inline void vringh_iov_reset(struct vringh_iov *iov)
123 iov->iov[iov->i].iov_len += iov->consumed;
124 iov->iov[iov->i].iov_base -= iov->consumed;
129 static inline void vringh_iov_cleanup(struct vringh_iov *iov)
131 if (iov->max_num & VRINGH_IOV_ALLOCATED)
133 iov->max_num = iov->used = iov->i = iov->consumed = 0;
137 /* Convert a descriptor into iovecs. */
138 int vringh_getdesc_user(struct vringh *vrh,
139 struct vringh_iov *riov,
140 struct vringh_iov *wiov,
141 bool (*getrange)(struct vringh *vrh,
142 u64 addr, struct vringh_range *r),
145 /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
146 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
148 /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
149 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
150 const void *src, size_t len);
152 /* Mark a descriptor as used. */
153 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
154 int vringh_complete_multi_user(struct vringh *vrh,
155 const struct vring_used_elem used[],
158 /* Pretend we've never seen descriptor (for easy error handling). */
159 void vringh_abandon_user(struct vringh *vrh, unsigned int num);
161 /* Do we need to fire the eventfd to notify the other side? */
162 int vringh_need_notify_user(struct vringh *vrh);
164 bool vringh_notify_enable_user(struct vringh *vrh);
165 void vringh_notify_disable_user(struct vringh *vrh);
167 /* Helpers for kernelspace vrings. */
168 int vringh_init_kern(struct vringh *vrh, u64 features,
169 unsigned int num, bool weak_barriers,
170 struct vring_desc *desc,
171 struct vring_avail *avail,
172 struct vring_used *used);
174 static inline void vringh_kiov_init(struct vringh_kiov *kiov,
175 struct kvec *kvec, unsigned num)
177 kiov->used = kiov->i = 0;
183 static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
185 kiov->iov[kiov->i].iov_len += kiov->consumed;
186 kiov->iov[kiov->i].iov_base -= kiov->consumed;
191 static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
193 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
195 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
199 int vringh_getdesc_kern(struct vringh *vrh,
200 struct vringh_kiov *riov,
201 struct vringh_kiov *wiov,
205 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
206 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
207 const void *src, size_t len);
208 void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
209 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
211 bool vringh_notify_enable_kern(struct vringh *vrh);
212 void vringh_notify_disable_kern(struct vringh *vrh);
214 int vringh_need_notify_kern(struct vringh *vrh);
216 /* Notify the guest about buffers added to the used ring */
217 static inline void vringh_notify(struct vringh *vrh)
223 static inline bool vringh_is_little_endian(const struct vringh *vrh)
225 return vrh->little_endian ||
226 virtio_legacy_is_little_endian();
229 static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
231 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
234 static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
236 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
239 static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
241 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
244 static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
246 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
249 static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
251 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
254 static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
256 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
259 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
261 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb);
263 int vringh_init_iotlb(struct vringh *vrh, u64 features,
264 unsigned int num, bool weak_barriers,
265 struct vring_desc *desc,
266 struct vring_avail *avail,
267 struct vring_used *used);
269 int vringh_getdesc_iotlb(struct vringh *vrh,
270 struct vringh_kiov *riov,
271 struct vringh_kiov *wiov,
275 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
276 struct vringh_kiov *riov,
277 void *dst, size_t len);
278 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
279 struct vringh_kiov *wiov,
280 const void *src, size_t len);
282 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num);
284 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len);
286 bool vringh_notify_enable_iotlb(struct vringh *vrh);
287 void vringh_notify_disable_iotlb(struct vringh *vrh);
289 int vringh_need_notify_iotlb(struct vringh *vrh);
291 #endif /* CONFIG_VHOST_IOTLB */
293 #endif /* _LINUX_VRINGH_H */