vringh: implement vringh_kiov_advance()
[linux-2.6-microblaze.git] / include / linux / vringh.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Linux host-side vring helpers; for when the kernel needs to access
4  * someone else's vring.
5  *
6  * Copyright IBM Corporation, 2013.
7  * Parts taken from drivers/vhost/vhost.c Copyright 2009 Red Hat, Inc.
8  *
9  * Written by: Rusty Russell <rusty@rustcorp.com.au>
10  */
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>
20 #endif
21 #include <asm/barrier.h>
22
23 /* virtio_ring with information needed for host access. */
24 struct vringh {
25         /* Everything is little endian */
26         bool little_endian;
27
28         /* Guest publishes used event idx (note: we always do). */
29         bool event_indices;
30
31         /* Can we get away with weak barriers? */
32         bool weak_barriers;
33
34         /* Last available index we saw (ie. where we're up to). */
35         u16 last_avail_idx;
36
37         /* Last index we used. */
38         u16 last_used_idx;
39
40         /* How many descriptors we've completed since last need_notify(). */
41         u32 completed;
42
43         /* The vring (note: it may contain user pointers!) */
44         struct vring vring;
45
46         /* IOTLB for this vring */
47         struct vhost_iotlb *iotlb;
48
49         /* spinlock to synchronize IOTLB accesses */
50         spinlock_t *iotlb_lock;
51
52         /* The function to call to notify the guest about added buffers */
53         void (*notify)(struct vringh *);
54 };
55
56 /**
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().
66  */
67 struct virtio_device;
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);
73 };
74
75 /* The memory the vring can access, and what offset to apply. */
76 struct vringh_range {
77         u64 start, end_incl;
78         u64 offset;
79 };
80
81 /**
82  * struct vringh_iov - iovec mangler.
83  *
84  * Mangles iovec in place, and restores it.
85  * Remaining data is iov + i, of used - i elements.
86  */
87 struct vringh_iov {
88         struct iovec *iov;
89         size_t consumed; /* Within iov[i] */
90         unsigned i, used, max_num;
91 };
92
93 /**
94  * struct vringh_iov - kvec mangler.
95  *
96  * Mangles kvec in place, and restores it.
97  * Remaining data is iov + i, of used - i elements.
98  */
99 struct vringh_kiov {
100         struct kvec *iov;
101         size_t consumed; /* Within iov[i] */
102         unsigned i, used, max_num;
103 };
104
105 /* Flag on max_num to indicate we're kmalloced. */
106 #define VRINGH_IOV_ALLOCATED 0x8000000
107
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);
114
115 static inline void vringh_iov_init(struct vringh_iov *iov,
116                                    struct iovec *iovec, unsigned num)
117 {
118         iov->used = iov->i = 0;
119         iov->consumed = 0;
120         iov->max_num = num;
121         iov->iov = iovec;
122 }
123
124 static inline void vringh_iov_reset(struct vringh_iov *iov)
125 {
126         iov->iov[iov->i].iov_len += iov->consumed;
127         iov->iov[iov->i].iov_base -= iov->consumed;
128         iov->consumed = 0;
129         iov->i = 0;
130 }
131
132 static inline void vringh_iov_cleanup(struct vringh_iov *iov)
133 {
134         if (iov->max_num & VRINGH_IOV_ALLOCATED)
135                 kfree(iov->iov);
136         iov->max_num = iov->used = iov->i = iov->consumed = 0;
137         iov->iov = NULL;
138 }
139
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),
146                         u16 *head);
147
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);
150
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);
154
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[],
159                                unsigned num_used);
160
161 /* Pretend we've never seen descriptor (for easy error handling). */
162 void vringh_abandon_user(struct vringh *vrh, unsigned int num);
163
164 /* Do we need to fire the eventfd to notify the other side? */
165 int vringh_need_notify_user(struct vringh *vrh);
166
167 bool vringh_notify_enable_user(struct vringh *vrh);
168 void vringh_notify_disable_user(struct vringh *vrh);
169
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);
176
177 static inline void vringh_kiov_init(struct vringh_kiov *kiov,
178                                     struct kvec *kvec, unsigned num)
179 {
180         kiov->used = kiov->i = 0;
181         kiov->consumed = 0;
182         kiov->max_num = num;
183         kiov->iov = kvec;
184 }
185
186 static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
187 {
188         kiov->iov[kiov->i].iov_len += kiov->consumed;
189         kiov->iov[kiov->i].iov_base -= kiov->consumed;
190         kiov->consumed = 0;
191         kiov->i = 0;
192 }
193
194 static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
195 {
196         if (kiov->max_num & VRINGH_IOV_ALLOCATED)
197                 kfree(kiov->iov);
198         kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
199         kiov->iov = NULL;
200 }
201
202 void vringh_kiov_advance(struct vringh_kiov *kiov, size_t len);
203
204 int vringh_getdesc_kern(struct vringh *vrh,
205                         struct vringh_kiov *riov,
206                         struct vringh_kiov *wiov,
207                         u16 *head,
208                         gfp_t gfp);
209
210 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
211 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
212                              const void *src, size_t len);
213 void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
214 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
215
216 bool vringh_notify_enable_kern(struct vringh *vrh);
217 void vringh_notify_disable_kern(struct vringh *vrh);
218
219 int vringh_need_notify_kern(struct vringh *vrh);
220
221 /* Notify the guest about buffers added to the used ring */
222 static inline void vringh_notify(struct vringh *vrh)
223 {
224         if (vrh->notify)
225                 vrh->notify(vrh);
226 }
227
228 static inline bool vringh_is_little_endian(const struct vringh *vrh)
229 {
230         return vrh->little_endian ||
231                 virtio_legacy_is_little_endian();
232 }
233
234 static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
235 {
236         return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
237 }
238
239 static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
240 {
241         return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
242 }
243
244 static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
245 {
246         return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
247 }
248
249 static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
250 {
251         return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
252 }
253
254 static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
255 {
256         return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
257 }
258
259 static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
260 {
261         return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
262 }
263
264 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
265
266 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb,
267                       spinlock_t *iotlb_lock);
268
269 int vringh_init_iotlb(struct vringh *vrh, u64 features,
270                       unsigned int num, bool weak_barriers,
271                       struct vring_desc *desc,
272                       struct vring_avail *avail,
273                       struct vring_used *used);
274
275 int vringh_getdesc_iotlb(struct vringh *vrh,
276                          struct vringh_kiov *riov,
277                          struct vringh_kiov *wiov,
278                          u16 *head,
279                          gfp_t gfp);
280
281 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
282                               struct vringh_kiov *riov,
283                               void *dst, size_t len);
284 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
285                               struct vringh_kiov *wiov,
286                               const void *src, size_t len);
287
288 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num);
289
290 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len);
291
292 bool vringh_notify_enable_iotlb(struct vringh *vrh);
293 void vringh_notify_disable_iotlb(struct vringh *vrh);
294
295 int vringh_need_notify_iotlb(struct vringh *vrh);
296
297 #endif /* CONFIG_VHOST_IOTLB */
298
299 #endif /* _LINUX_VRINGH_H */