]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - include/linux/vringh.h
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 #include <asm/barrier.h>
19 /* virtio_ring with information needed for host access. */
21 /* Everything is little endian */
24 /* Guest publishes used event idx (note: we always do). */
27 /* Can we get away with weak barriers? */
30 /* Last available index we saw (ie. where we're up to). */
33 /* Last index we used. */
36 /* How many descriptors we've completed since last need_notify(). */
39 /* The vring (note: it may contain user pointers!) */
42 /* The function to call to notify the guest about added buffers */
43 void (*notify
)(struct vringh
*);
47 * struct vringh_config_ops - ops for creating a host vring from a virtio driver
48 * @find_vrhs: find the host vrings and instantiate them
49 * vdev: the virtio_device
50 * nhvrs: the number of host vrings to find
51 * hvrs: on success, includes new host vrings
52 * callbacks: array of driver callbacks, for each host vring
53 * include a NULL entry for vqs that do not need a callback
54 * Returns 0 on success or error status
55 * @del_vrhs: free the host vrings found by find_vrhs().
58 typedef void vrh_callback_t(struct virtio_device
*, struct vringh
*);
59 struct vringh_config_ops
{
60 int (*find_vrhs
)(struct virtio_device
*vdev
, unsigned nhvrs
,
61 struct vringh
*vrhs
[], vrh_callback_t
*callbacks
[]);
62 void (*del_vrhs
)(struct virtio_device
*vdev
);
65 /* The memory the vring can access, and what offset to apply. */
72 * struct vringh_iov - iovec mangler.
74 * Mangles iovec in place, and restores it.
75 * Remaining data is iov + i, of used - i elements.
79 size_t consumed
; /* Within iov[i] */
80 unsigned i
, used
, max_num
;
84 * struct vringh_iov - kvec mangler.
86 * Mangles kvec in place, and restores it.
87 * Remaining data is iov + i, of used - i elements.
91 size_t consumed
; /* Within iov[i] */
92 unsigned i
, used
, max_num
;
95 /* Flag on max_num to indicate we're kmalloced. */
96 #define VRINGH_IOV_ALLOCATED 0x8000000
98 /* Helpers for userspace vrings. */
99 int vringh_init_user(struct vringh
*vrh
, u64 features
,
100 unsigned int num
, bool weak_barriers
,
101 struct vring_desc __user
*desc
,
102 struct vring_avail __user
*avail
,
103 struct vring_used __user
*used
);
105 static inline void vringh_iov_init(struct vringh_iov
*iov
,
106 struct iovec
*iovec
, unsigned num
)
108 iov
->used
= iov
->i
= 0;
114 static inline void vringh_iov_reset(struct vringh_iov
*iov
)
116 iov
->iov
[iov
->i
].iov_len
+= iov
->consumed
;
117 iov
->iov
[iov
->i
].iov_base
-= iov
->consumed
;
122 static inline void vringh_iov_cleanup(struct vringh_iov
*iov
)
124 if (iov
->max_num
& VRINGH_IOV_ALLOCATED
)
126 iov
->max_num
= iov
->used
= iov
->i
= iov
->consumed
= 0;
130 /* Convert a descriptor into iovecs. */
131 int vringh_getdesc_user(struct vringh
*vrh
,
132 struct vringh_iov
*riov
,
133 struct vringh_iov
*wiov
,
134 bool (*getrange
)(struct vringh
*vrh
,
135 u64 addr
, struct vringh_range
*r
),
138 /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
139 ssize_t
vringh_iov_pull_user(struct vringh_iov
*riov
, void *dst
, size_t len
);
141 /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
142 ssize_t
vringh_iov_push_user(struct vringh_iov
*wiov
,
143 const void *src
, size_t len
);
145 /* Mark a descriptor as used. */
146 int vringh_complete_user(struct vringh
*vrh
, u16 head
, u32 len
);
147 int vringh_complete_multi_user(struct vringh
*vrh
,
148 const struct vring_used_elem used
[],
151 /* Pretend we've never seen descriptor (for easy error handling). */
152 void vringh_abandon_user(struct vringh
*vrh
, unsigned int num
);
154 /* Do we need to fire the eventfd to notify the other side? */
155 int vringh_need_notify_user(struct vringh
*vrh
);
157 bool vringh_notify_enable_user(struct vringh
*vrh
);
158 void vringh_notify_disable_user(struct vringh
*vrh
);
160 /* Helpers for kernelspace vrings. */
161 int vringh_init_kern(struct vringh
*vrh
, u64 features
,
162 unsigned int num
, bool weak_barriers
,
163 struct vring_desc
*desc
,
164 struct vring_avail
*avail
,
165 struct vring_used
*used
);
167 static inline void vringh_kiov_init(struct vringh_kiov
*kiov
,
168 struct kvec
*kvec
, unsigned num
)
170 kiov
->used
= kiov
->i
= 0;
176 static inline void vringh_kiov_reset(struct vringh_kiov
*kiov
)
178 kiov
->iov
[kiov
->i
].iov_len
+= kiov
->consumed
;
179 kiov
->iov
[kiov
->i
].iov_base
-= kiov
->consumed
;
184 static inline void vringh_kiov_cleanup(struct vringh_kiov
*kiov
)
186 if (kiov
->max_num
& VRINGH_IOV_ALLOCATED
)
188 kiov
->max_num
= kiov
->used
= kiov
->i
= kiov
->consumed
= 0;
192 int vringh_getdesc_kern(struct vringh
*vrh
,
193 struct vringh_kiov
*riov
,
194 struct vringh_kiov
*wiov
,
198 ssize_t
vringh_iov_pull_kern(struct vringh_kiov
*riov
, void *dst
, size_t len
);
199 ssize_t
vringh_iov_push_kern(struct vringh_kiov
*wiov
,
200 const void *src
, size_t len
);
201 void vringh_abandon_kern(struct vringh
*vrh
, unsigned int num
);
202 int vringh_complete_kern(struct vringh
*vrh
, u16 head
, u32 len
);
204 bool vringh_notify_enable_kern(struct vringh
*vrh
);
205 void vringh_notify_disable_kern(struct vringh
*vrh
);
207 int vringh_need_notify_kern(struct vringh
*vrh
);
209 /* Notify the guest about buffers added to the used ring */
210 static inline void vringh_notify(struct vringh
*vrh
)
216 static inline bool vringh_is_little_endian(const struct vringh
*vrh
)
218 return vrh
->little_endian
||
219 virtio_legacy_is_little_endian();
222 static inline u16
vringh16_to_cpu(const struct vringh
*vrh
, __virtio16 val
)
224 return __virtio16_to_cpu(vringh_is_little_endian(vrh
), val
);
227 static inline __virtio16
cpu_to_vringh16(const struct vringh
*vrh
, u16 val
)
229 return __cpu_to_virtio16(vringh_is_little_endian(vrh
), val
);
232 static inline u32
vringh32_to_cpu(const struct vringh
*vrh
, __virtio32 val
)
234 return __virtio32_to_cpu(vringh_is_little_endian(vrh
), val
);
237 static inline __virtio32
cpu_to_vringh32(const struct vringh
*vrh
, u32 val
)
239 return __cpu_to_virtio32(vringh_is_little_endian(vrh
), val
);
242 static inline u64
vringh64_to_cpu(const struct vringh
*vrh
, __virtio64 val
)
244 return __virtio64_to_cpu(vringh_is_little_endian(vrh
), val
);
247 static inline __virtio64
cpu_to_vringh64(const struct vringh
*vrh
, u64 val
)
249 return __cpu_to_virtio64(vringh_is_little_endian(vrh
), val
);
251 #endif /* _LINUX_VRINGH_H */