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 #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 /* 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 */ 64 struct virtio_device; 65 typedef void vrh_callback_t(struct virtio_device *, struct vringh *); 66 struct 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); 70 }; 71 72 /* The memory the vring can access, and what offset to apply. */ 73 struct 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 */ 84 struct 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 */ 96 struct 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. */ 106 int vringh_init_user(struct vringh *vrh, u64 features, 107 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 112 static 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 121 static 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 129 static 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. */ 138 int 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). */ 146 ssize_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). */ 149 ssize_t vringh_iov_push_user(struct vringh_iov *wiov, 150 const void *src, size_t len); 151 152 /* Mark a descriptor as used. */ 153 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len); 154 int 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). */ 159 void vringh_abandon_user(struct vringh *vrh, unsigned int num); 160 161 /* Do we need to fire the eventfd to notify the other side? */ 162 int vringh_need_notify_user(struct vringh *vrh); 163 164 bool vringh_notify_enable_user(struct vringh *vrh); 165 void vringh_notify_disable_user(struct vringh *vrh); 166 167 /* Helpers for kernelspace vrings. */ 168 int vringh_init_kern(struct vringh *vrh, u64 features, 169 unsigned int num, bool weak_barriers, 170 struct vring_desc *desc, 171 struct vring_avail *avail, 172 struct vring_used *used); 173 174 static 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 183 static 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 191 static 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 199 int vringh_getdesc_kern(struct vringh *vrh, 200 struct vringh_kiov *riov, 201 struct vringh_kiov *wiov, 202 u16 *head, 203 gfp_t gfp); 204 205 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len); 206 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov, 207 const void *src, size_t len); 208 void vringh_abandon_kern(struct vringh *vrh, unsigned int num); 209 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len); 210 211 bool vringh_notify_enable_kern(struct vringh *vrh); 212 void vringh_notify_disable_kern(struct vringh *vrh); 213 214 int vringh_need_notify_kern(struct vringh *vrh); 215 216 /* Notify the guest about buffers added to the used ring */ 217 static inline void vringh_notify(struct vringh *vrh) 218 { 219 if (vrh->notify) 220 vrh->notify(vrh); 221 } 222 223 static inline bool vringh_is_little_endian(const struct vringh *vrh) 224 { 225 return vrh->little_endian || 226 virtio_legacy_is_little_endian(); 227 } 228 229 static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val) 230 { 231 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val); 232 } 233 234 static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val) 235 { 236 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val); 237 } 238 239 static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val) 240 { 241 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val); 242 } 243 244 static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val) 245 { 246 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val); 247 } 248 249 static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val) 250 { 251 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val); 252 } 253 254 static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val) 255 { 256 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val); 257 } 258 259 #if IS_REACHABLE(CONFIG_VHOST_IOTLB) 260 261 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb); 262 263 int 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 269 int vringh_getdesc_iotlb(struct vringh *vrh, 270 struct vringh_kiov *riov, 271 struct vringh_kiov *wiov, 272 u16 *head, 273 gfp_t gfp); 274 275 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh, 276 struct vringh_kiov *riov, 277 void *dst, size_t len); 278 ssize_t vringh_iov_push_iotlb(struct vringh *vrh, 279 struct vringh_kiov *wiov, 280 const void *src, size_t len); 281 282 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num); 283 284 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len); 285 286 bool vringh_notify_enable_iotlb(struct vringh *vrh); 287 void vringh_notify_disable_iotlb(struct vringh *vrh); 288 289 int vringh_need_notify_iotlb(struct vringh *vrh); 290 291 #endif /* CONFIG_VHOST_IOTLB */ 292 293 #endif /* _LINUX_VRINGH_H */ 294