vringh: host-side implementation of virtio rings.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / include / linux / vringh.h
CommitLineData
f87d0fbb
RR
1/*
2 * Linux host-side vring helpers; for when the kernel needs to access
3 * someone else's vring.
4 *
5 * Copyright IBM Corporation, 2013.
6 * Parts taken from drivers/vhost/vhost.c Copyright 2009 Red Hat, Inc.
7 *
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.
12 *
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.
17 *
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.
21 *
22 * Written by: Rusty Russell <rusty@rustcorp.com.au>
23 */
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>
30
31/* virtio_ring with information needed for host access. */
32struct vringh {
33 /* Guest publishes used event idx (note: we always do). */
34 bool event_indices;
35
36 /* Can we get away with weak barriers? */
37 bool weak_barriers;
38
39 /* Last available index we saw (ie. where we're up to). */
40 u16 last_avail_idx;
41
42 /* Last index we used. */
43 u16 last_used_idx;
44
45 /* How many descriptors we've completed since last need_notify(). */
46 u32 completed;
47
48 /* The vring (note: it may contain user pointers!) */
49 struct vring vring;
50};
51
52/* The memory the vring can access, and what offset to apply. */
53struct vringh_range {
54 u64 start, end_incl;
55 u64 offset;
56};
57
58/**
59 * struct vringh_iov - iovec mangler.
60 *
61 * Mangles iovec in place, and restores it.
62 * Remaining data is iov + i, of used - i elements.
63 */
64struct vringh_iov {
65 struct iovec *iov;
66 size_t consumed; /* Within iov[i] */
67 unsigned i, used, max_num;
68};
69
70/**
71 * struct vringh_iov - kvec mangler.
72 *
73 * Mangles kvec in place, and restores it.
74 * Remaining data is iov + i, of used - i elements.
75 */
76struct vringh_kiov {
77 struct kvec *iov;
78 size_t consumed; /* Within iov[i] */
79 unsigned i, used, max_num;
80};
81
82/* Flag on max_num to indicate we're kmalloced. */
83#define VRINGH_IOV_ALLOCATED 0x8000000
84
85/* Helpers for userspace vrings. */
86int 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);
91
92static inline void vringh_iov_init(struct vringh_iov *iov,
93 struct iovec *iovec, unsigned num)
94{
95 iov->used = iov->i = 0;
96 iov->consumed = 0;
97 iov->max_num = num;
98 iov->iov = iovec;
99}
100
101static inline void vringh_iov_reset(struct vringh_iov *iov)
102{
103 iov->iov[iov->i].iov_len += iov->consumed;
104 iov->iov[iov->i].iov_base -= iov->consumed;
105 iov->consumed = 0;
106 iov->i = 0;
107}
108
109static inline void vringh_iov_cleanup(struct vringh_iov *iov)
110{
111 if (iov->max_num & VRINGH_IOV_ALLOCATED)
112 kfree(iov->iov);
113 iov->max_num = iov->used = iov->i = iov->consumed = 0;
114 iov->iov = NULL;
115}
116
117/* Convert a descriptor into iovecs. */
118int 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),
123 u16 *head);
124
125/* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
126ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
127
128/* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
129ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
130 const void *src, size_t len);
131
132/* Mark a descriptor as used. */
133int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
134int vringh_complete_multi_user(struct vringh *vrh,
135 const struct vring_used_elem used[],
136 unsigned num_used);
137
138/* Pretend we've never seen descriptor (for easy error handling). */
139void vringh_abandon_user(struct vringh *vrh, unsigned int num);
140
141/* Do we need to fire the eventfd to notify the other side? */
142int vringh_need_notify_user(struct vringh *vrh);
143
144bool vringh_notify_enable_user(struct vringh *vrh);
145void vringh_notify_disable_user(struct vringh *vrh);
146
147/* Helpers for kernelspace vrings. */
148int 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);
153
154static inline void vringh_kiov_init(struct vringh_kiov *kiov,
155 struct kvec *kvec, unsigned num)
156{
157 kiov->used = kiov->i = 0;
158 kiov->consumed = 0;
159 kiov->max_num = num;
160 kiov->iov = kvec;
161}
162
163static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
164{
165 kiov->iov[kiov->i].iov_len += kiov->consumed;
166 kiov->iov[kiov->i].iov_base -= kiov->consumed;
167 kiov->consumed = 0;
168 kiov->i = 0;
169}
170
171static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
172{
173 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
174 kfree(kiov->iov);
175 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
176 kiov->iov = NULL;
177}
178
179int vringh_getdesc_kern(struct vringh *vrh,
180 struct vringh_kiov *riov,
181 struct vringh_kiov *wiov,
182 u16 *head,
183 gfp_t gfp);
184
185ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
186ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
187 const void *src, size_t len);
188void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
189int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
190
191bool vringh_notify_enable_kern(struct vringh *vrh);
192void vringh_notify_disable_kern(struct vringh *vrh);
193
194int vringh_need_notify_kern(struct vringh *vrh);
195
196#endif /* _LINUX_VRINGH_H */