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 <[email protected]> 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 #include <linux/dma-direction.h> 18 #include <linux/vhost_iotlb.h> 19 #include <asm/barrier.h> 20 21 /* virtio_ring with information needed for host access. */ 22 struct vringh { 23 /* Everything is little endian */ 24 bool little_endian; 25 26 /* Guest publishes used event idx (note: we always do). */ 27 bool event_indices; 28 29 /* Can we get away with weak barriers? */ 30 bool weak_barriers; 31 32 /* Last available index we saw (ie. where we're up to). */ 33 u16 last_avail_idx; 34 35 /* Last index we used. */ 36 u16 last_used_idx; 37 38 /* How many descriptors we've completed since last need_notify(). */ 39 u32 completed; 40 41 /* The vring (note: it may contain user pointers!) */ 42 struct vring vring; 43 44 /* IOTLB for this vring */ 45 struct vhost_iotlb *iotlb; 46 47 /* The function to call to notify the guest about added buffers */ 48 void (*notify)(struct vringh *); 49 }; 50 51 /** 52 * struct vringh_config_ops - ops for creating a host vring from a virtio driver 53 * @find_vrhs: find the host vrings and instantiate them 54 * vdev: the virtio_device 55 * nhvrs: the number of host vrings to find 56 * hvrs: on success, includes new host vrings 57 * callbacks: array of driver callbacks, for each host vring 58 * include a NULL entry for vqs that do not need a callback 59 * Returns 0 on success or error status 60 * @del_vrhs: free the host vrings found by find_vrhs(). 61 */ 62 struct virtio_device; 63 typedef void vrh_callback_t(struct virtio_device *, struct vringh *); 64 struct vringh_config_ops { 65 int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs, 66 struct vringh *vrhs[], vrh_callback_t *callbacks[]); 67 void (*del_vrhs)(struct virtio_device *vdev); 68 }; 69 70 /* The memory the vring can access, and what offset to apply. */ 71 struct vringh_range { 72 u64 start, end_incl; 73 u64 offset; 74 }; 75 76 /** 77 * struct vringh_iov - iovec mangler. 78 * 79 * Mangles iovec in place, and restores it. 80 * Remaining data is iov + i, of used - i elements. 81 */ 82 struct vringh_iov { 83 struct iovec *iov; 84 size_t consumed; /* Within iov[i] */ 85 unsigned i, used, max_num; 86 }; 87 88 /** 89 * struct vringh_iov - kvec mangler. 90 * 91 * Mangles kvec in place, and restores it. 92 * Remaining data is iov + i, of used - i elements. 93 */ 94 struct vringh_kiov { 95 struct kvec *iov; 96 size_t consumed; /* Within iov[i] */ 97 unsigned i, used, max_num; 98 }; 99 100 /* Flag on max_num to indicate we're kmalloced. */ 101 #define VRINGH_IOV_ALLOCATED 0x8000000 102 103 /* Helpers for userspace vrings. */ 104 int vringh_init_user(struct vringh *vrh, u64 features, 105 unsigned int num, bool weak_barriers, 106 struct vring_desc __user *desc, 107 struct vring_avail __user *avail, 108 struct vring_used __user *used); 109 110 static inline void vringh_iov_init(struct vringh_iov *iov, 111 struct iovec *iovec, unsigned num) 112 { 113 iov->used = iov->i = 0; 114 iov->consumed = 0; 115 iov->max_num = num; 116 iov->iov = iovec; 117 } 118 119 static inline void vringh_iov_reset(struct vringh_iov *iov) 120 { 121 iov->iov[iov->i].iov_len += iov->consumed; 122 iov->iov[iov->i].iov_base -= iov->consumed; 123 iov->consumed = 0; 124 iov->i = 0; 125 } 126 127 static inline void vringh_iov_cleanup(struct vringh_iov *iov) 128 { 129 if (iov->max_num & VRINGH_IOV_ALLOCATED) 130 kfree(iov->iov); 131 iov->max_num = iov->used = iov->i = iov->consumed = 0; 132 iov->iov = NULL; 133 } 134 135 /* Convert a descriptor into iovecs. */ 136 int vringh_getdesc_user(struct vringh *vrh, 137 struct vringh_iov *riov, 138 struct vringh_iov *wiov, 139 bool (*getrange)(struct vringh *vrh, 140 u64 addr, struct vringh_range *r), 141 u16 *head); 142 143 /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */ 144 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len); 145 146 /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */ 147 ssize_t vringh_iov_push_user(struct vringh_iov *wiov, 148 const void *src, size_t len); 149 150 /* Mark a descriptor as used. */ 151 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len); 152 int vringh_complete_multi_user(struct vringh *vrh, 153 const struct vring_used_elem used[], 154 unsigned num_used); 155 156 /* Pretend we've never seen descriptor (for easy error handling). */ 157 void vringh_abandon_user(struct vringh *vrh, unsigned int num); 158 159 /* Do we need to fire the eventfd to notify the other side? */ 160 int vringh_need_notify_user(struct vringh *vrh); 161 162 bool vringh_notify_enable_user(struct vringh *vrh); 163 void vringh_notify_disable_user(struct vringh *vrh); 164 165 /* Helpers for kernelspace vrings. */ 166 int vringh_init_kern(struct vringh *vrh, u64 features, 167 unsigned int num, bool weak_barriers, 168 struct vring_desc *desc, 169 struct vring_avail *avail, 170 struct vring_used *used); 171 172 static inline void vringh_kiov_init(struct vringh_kiov *kiov, 173 struct kvec *kvec, unsigned num) 174 { 175 kiov->used = kiov->i = 0; 176 kiov->consumed = 0; 177 kiov->max_num = num; 178 kiov->iov = kvec; 179 } 180 181 static inline void vringh_kiov_reset(struct vringh_kiov *kiov) 182 { 183 kiov->iov[kiov->i].iov_len += kiov->consumed; 184 kiov->iov[kiov->i].iov_base -= kiov->consumed; 185 kiov->consumed = 0; 186 kiov->i = 0; 187 } 188 189 static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov) 190 { 191 if (kiov->max_num & VRINGH_IOV_ALLOCATED) 192 kfree(kiov->iov); 193 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0; 194 kiov->iov = NULL; 195 } 196 197 int vringh_getdesc_kern(struct vringh *vrh, 198 struct vringh_kiov *riov, 199 struct vringh_kiov *wiov, 200 u16 *head, 201 gfp_t gfp); 202 203 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len); 204 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov, 205 const void *src, size_t len); 206 void vringh_abandon_kern(struct vringh *vrh, unsigned int num); 207 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len); 208 209 bool vringh_notify_enable_kern(struct vringh *vrh); 210 void vringh_notify_disable_kern(struct vringh *vrh); 211 212 int vringh_need_notify_kern(struct vringh *vrh); 213 214 /* Notify the guest about buffers added to the used ring */ 215 static inline void vringh_notify(struct vringh *vrh) 216 { 217 if (vrh->notify) 218 vrh->notify(vrh); 219 } 220 221 static inline bool vringh_is_little_endian(const struct vringh *vrh) 222 { 223 return vrh->little_endian || 224 virtio_legacy_is_little_endian(); 225 } 226 227 static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val) 228 { 229 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val); 230 } 231 232 static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val) 233 { 234 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val); 235 } 236 237 static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val) 238 { 239 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val); 240 } 241 242 static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val) 243 { 244 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val); 245 } 246 247 static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val) 248 { 249 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val); 250 } 251 252 static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val) 253 { 254 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val); 255 } 256 257 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb); 258 259 int vringh_init_iotlb(struct vringh *vrh, u64 features, 260 unsigned int num, bool weak_barriers, 261 struct vring_desc *desc, 262 struct vring_avail *avail, 263 struct vring_used *used); 264 265 int vringh_getdesc_iotlb(struct vringh *vrh, 266 struct vringh_kiov *riov, 267 struct vringh_kiov *wiov, 268 u16 *head, 269 gfp_t gfp); 270 271 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh, 272 struct vringh_kiov *riov, 273 void *dst, size_t len); 274 ssize_t vringh_iov_push_iotlb(struct vringh *vrh, 275 struct vringh_kiov *wiov, 276 const void *src, size_t len); 277 278 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num); 279 280 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len); 281 282 bool vringh_notify_enable_iotlb(struct vringh *vrh); 283 void vringh_notify_disable_iotlb(struct vringh *vrh); 284 285 int vringh_need_notify_iotlb(struct vringh *vrh); 286 287 #endif /* _LINUX_VRINGH_H */ 288