2 * Linux host-side vring helpers; for when the kernel needs to access
3 * someone else's vring.
5 * Copyright IBM Corporation, 2013.
6 * Parts taken from drivers/vhost/vhost.c Copyright 2009 Red Hat, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Written by: Rusty Russell <rusty@rustcorp.com.au>
24 #ifndef _LINUX_VRINGH_H
25 #define _LINUX_VRINGH_H
26 #include <uapi/linux/virtio_ring.h>
27 #include <linux/uio.h>
28 #include <linux/slab.h>
29 #include <asm/barrier.h>
31 /* virtio_ring with information needed for host access. */
33 /* Guest publishes used event idx (note: we always do). */
36 /* Can we get away with weak barriers? */
39 /* Last available index we saw (ie. where we're up to). */
42 /* Last index we used. */
45 /* How many descriptors we've completed since last need_notify(). */
48 /* The vring (note: it may contain user pointers!) */
52 /* The memory the vring can access, and what offset to apply. */
59 * struct vringh_iov - iovec mangler.
61 * Mangles iovec in place, and restores it.
62 * Remaining data is iov + i, of used - i elements.
66 size_t consumed; /* Within iov[i] */
67 unsigned i, used, max_num;
71 * struct vringh_iov - kvec mangler.
73 * Mangles kvec in place, and restores it.
74 * Remaining data is iov + i, of used - i elements.
78 size_t consumed; /* Within iov[i] */
79 unsigned i, used, max_num;
82 /* Flag on max_num to indicate we're kmalloced. */
83 #define VRINGH_IOV_ALLOCATED 0x8000000
85 /* Helpers for userspace vrings. */
86 int vringh_init_user(struct vringh *vrh, u32 features,
87 unsigned int num, bool weak_barriers,
88 struct vring_desc __user *desc,
89 struct vring_avail __user *avail,
90 struct vring_used __user *used);
92 static inline void vringh_iov_init(struct vringh_iov *iov,
93 struct iovec *iovec, unsigned num)
95 iov->used = iov->i = 0;
101 static inline void vringh_iov_reset(struct vringh_iov *iov)
103 iov->iov[iov->i].iov_len += iov->consumed;
104 iov->iov[iov->i].iov_base -= iov->consumed;
109 static inline void vringh_iov_cleanup(struct vringh_iov *iov)
111 if (iov->max_num & VRINGH_IOV_ALLOCATED)
113 iov->max_num = iov->used = iov->i = iov->consumed = 0;
117 /* Convert a descriptor into iovecs. */
118 int vringh_getdesc_user(struct vringh *vrh,
119 struct vringh_iov *riov,
120 struct vringh_iov *wiov,
121 bool (*getrange)(struct vringh *vrh,
122 u64 addr, struct vringh_range *r),
125 /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
126 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
128 /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
129 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
130 const void *src, size_t len);
132 /* Mark a descriptor as used. */
133 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
134 int vringh_complete_multi_user(struct vringh *vrh,
135 const struct vring_used_elem used[],
138 /* Pretend we've never seen descriptor (for easy error handling). */
139 void vringh_abandon_user(struct vringh *vrh, unsigned int num);
141 /* Do we need to fire the eventfd to notify the other side? */
142 int vringh_need_notify_user(struct vringh *vrh);
144 bool vringh_notify_enable_user(struct vringh *vrh);
145 void vringh_notify_disable_user(struct vringh *vrh);
147 /* Helpers for kernelspace vrings. */
148 int vringh_init_kern(struct vringh *vrh, u32 features,
149 unsigned int num, bool weak_barriers,
150 struct vring_desc *desc,
151 struct vring_avail *avail,
152 struct vring_used *used);
154 static inline void vringh_kiov_init(struct vringh_kiov *kiov,
155 struct kvec *kvec, unsigned num)
157 kiov->used = kiov->i = 0;
163 static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
165 kiov->iov[kiov->i].iov_len += kiov->consumed;
166 kiov->iov[kiov->i].iov_base -= kiov->consumed;
171 static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
173 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
175 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
179 int vringh_getdesc_kern(struct vringh *vrh,
180 struct vringh_kiov *riov,
181 struct vringh_kiov *wiov,
185 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
186 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
187 const void *src, size_t len);
188 void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
189 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
191 bool vringh_notify_enable_kern(struct vringh *vrh);
192 void vringh_notify_disable_kern(struct vringh *vrh);
194 int vringh_need_notify_kern(struct vringh *vrh);
196 #endif /* _LINUX_VRINGH_H */