blob: 9e2763d7c15912e0a77f7ca2e90e2ae836347c08 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Rusty Russellf87d0fb2013-03-20 13:50:14 +10302/*
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 *
Rusty Russellf87d0fb2013-03-20 13:50:14 +10309 * 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>
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +020014#include <linux/virtio_byteorder.h>
Rusty Russellf87d0fb2013-03-20 13:50:14 +103015#include <linux/uio.h>
16#include <linux/slab.h>
Michael S. Tsirkin33023632020-04-01 12:46:22 -040017#if IS_REACHABLE(CONFIG_VHOST_IOTLB)
Jason Wang9ad9c492020-03-26 22:01:20 +080018#include <linux/dma-direction.h>
19#include <linux/vhost_iotlb.h>
Michael S. Tsirkin33023632020-04-01 12:46:22 -040020#endif
Rusty Russellf87d0fb2013-03-20 13:50:14 +103021#include <asm/barrier.h>
22
23/* virtio_ring with information needed for host access. */
24struct vringh {
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +020025 /* Everything is little endian */
26 bool little_endian;
27
Rusty Russellf87d0fb2013-03-20 13:50:14 +103028 /* 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;
Sjur Brændeland3beee862013-03-20 13:51:24 +103045
Jason Wang9ad9c492020-03-26 22:01:20 +080046 /* IOTLB for this vring */
47 struct vhost_iotlb *iotlb;
48
Sjur Brændeland3beee862013-03-20 13:51:24 +103049 /* The function to call to notify the guest about added buffers */
50 void (*notify)(struct vringh *);
51};
52
53/**
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().
63 */
64struct virtio_device;
65typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
66struct 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);
Rusty Russellf87d0fb2013-03-20 13:50:14 +103070};
71
72/* The memory the vring can access, and what offset to apply. */
73struct vringh_range {
74 u64 start, end_incl;
75 u64 offset;
76};
77
78/**
79 * struct vringh_iov - iovec mangler.
80 *
81 * Mangles iovec in place, and restores it.
82 * Remaining data is iov + i, of used - i elements.
83 */
84struct vringh_iov {
85 struct iovec *iov;
86 size_t consumed; /* Within iov[i] */
87 unsigned i, used, max_num;
88};
89
90/**
91 * struct vringh_iov - kvec mangler.
92 *
93 * Mangles kvec in place, and restores it.
94 * Remaining data is iov + i, of used - i elements.
95 */
96struct vringh_kiov {
97 struct kvec *iov;
98 size_t consumed; /* Within iov[i] */
99 unsigned i, used, max_num;
100};
101
102/* Flag on max_num to indicate we're kmalloced. */
103#define VRINGH_IOV_ALLOCATED 0x8000000
104
105/* Helpers for userspace vrings. */
Michael S. Tsirkinb97a8a92014-12-12 00:36:06 +0200106int vringh_init_user(struct vringh *vrh, u64 features,
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030107 unsigned int num, bool weak_barriers,
108 struct vring_desc __user *desc,
109 struct vring_avail __user *avail,
110 struct vring_used __user *used);
111
112static inline void vringh_iov_init(struct vringh_iov *iov,
113 struct iovec *iovec, unsigned num)
114{
115 iov->used = iov->i = 0;
116 iov->consumed = 0;
117 iov->max_num = num;
118 iov->iov = iovec;
119}
120
121static inline void vringh_iov_reset(struct vringh_iov *iov)
122{
123 iov->iov[iov->i].iov_len += iov->consumed;
124 iov->iov[iov->i].iov_base -= iov->consumed;
125 iov->consumed = 0;
126 iov->i = 0;
127}
128
129static inline void vringh_iov_cleanup(struct vringh_iov *iov)
130{
131 if (iov->max_num & VRINGH_IOV_ALLOCATED)
132 kfree(iov->iov);
133 iov->max_num = iov->used = iov->i = iov->consumed = 0;
134 iov->iov = NULL;
135}
136
137/* Convert a descriptor into iovecs. */
138int 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),
143 u16 *head);
144
145/* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
146ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
147
148/* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
149ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
150 const void *src, size_t len);
151
152/* Mark a descriptor as used. */
153int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
154int vringh_complete_multi_user(struct vringh *vrh,
155 const struct vring_used_elem used[],
156 unsigned num_used);
157
158/* Pretend we've never seen descriptor (for easy error handling). */
159void vringh_abandon_user(struct vringh *vrh, unsigned int num);
160
161/* Do we need to fire the eventfd to notify the other side? */
162int vringh_need_notify_user(struct vringh *vrh);
163
164bool vringh_notify_enable_user(struct vringh *vrh);
165void vringh_notify_disable_user(struct vringh *vrh);
166
167/* Helpers for kernelspace vrings. */
Michael S. Tsirkinb97a8a92014-12-12 00:36:06 +0200168int vringh_init_kern(struct vringh *vrh, u64 features,
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030169 unsigned int num, bool weak_barriers,
170 struct vring_desc *desc,
171 struct vring_avail *avail,
172 struct vring_used *used);
173
174static inline void vringh_kiov_init(struct vringh_kiov *kiov,
175 struct kvec *kvec, unsigned num)
176{
177 kiov->used = kiov->i = 0;
178 kiov->consumed = 0;
179 kiov->max_num = num;
180 kiov->iov = kvec;
181}
182
183static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
184{
185 kiov->iov[kiov->i].iov_len += kiov->consumed;
186 kiov->iov[kiov->i].iov_base -= kiov->consumed;
187 kiov->consumed = 0;
188 kiov->i = 0;
189}
190
191static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
192{
193 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
194 kfree(kiov->iov);
195 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
196 kiov->iov = NULL;
197}
198
199int vringh_getdesc_kern(struct vringh *vrh,
200 struct vringh_kiov *riov,
201 struct vringh_kiov *wiov,
202 u16 *head,
203 gfp_t gfp);
204
205ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
206ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
207 const void *src, size_t len);
208void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
209int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
210
211bool vringh_notify_enable_kern(struct vringh *vrh);
212void vringh_notify_disable_kern(struct vringh *vrh);
213
214int vringh_need_notify_kern(struct vringh *vrh);
215
Sjur Brændeland3beee862013-03-20 13:51:24 +1030216/* Notify the guest about buffers added to the used ring */
217static inline void vringh_notify(struct vringh *vrh)
218{
219 if (vrh->notify)
220 vrh->notify(vrh);
221}
222
Greg Kurz5da7b162015-04-24 14:24:58 +0200223static inline bool vringh_is_little_endian(const struct vringh *vrh)
224{
Greg Kurz7d824102015-04-24 14:26:24 +0200225 return vrh->little_endian ||
226 virtio_legacy_is_little_endian();
Greg Kurz5da7b162015-04-24 14:24:58 +0200227}
228
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200229static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
230{
Greg Kurz5da7b162015-04-24 14:24:58 +0200231 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200232}
233
234static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
235{
Greg Kurz5da7b162015-04-24 14:24:58 +0200236 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200237}
238
239static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
240{
Greg Kurz5da7b162015-04-24 14:24:58 +0200241 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200242}
243
244static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
245{
Greg Kurz5da7b162015-04-24 14:24:58 +0200246 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200247}
248
249static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
250{
Greg Kurz5da7b162015-04-24 14:24:58 +0200251 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200252}
253
254static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
255{
Greg Kurz5da7b162015-04-24 14:24:58 +0200256 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200257}
Jason Wang9ad9c492020-03-26 22:01:20 +0800258
Michael S. Tsirkin33023632020-04-01 12:46:22 -0400259#if IS_REACHABLE(CONFIG_VHOST_IOTLB)
260
Jason Wang9ad9c492020-03-26 22:01:20 +0800261void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb);
262
263int 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);
268
269int vringh_getdesc_iotlb(struct vringh *vrh,
270 struct vringh_kiov *riov,
271 struct vringh_kiov *wiov,
272 u16 *head,
273 gfp_t gfp);
274
275ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
276 struct vringh_kiov *riov,
277 void *dst, size_t len);
278ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
279 struct vringh_kiov *wiov,
280 const void *src, size_t len);
281
282void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num);
283
284int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len);
285
286bool vringh_notify_enable_iotlb(struct vringh *vrh);
287void vringh_notify_disable_iotlb(struct vringh *vrh);
288
289int vringh_need_notify_iotlb(struct vringh *vrh);
290
Michael S. Tsirkin33023632020-04-01 12:46:22 -0400291#endif /* CONFIG_VHOST_IOTLB */
292
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030293#endif /* _LINUX_VRINGH_H */