vringh.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Linux host-side vring helpers; for when the kernel needs to access
  3. * someone else's vring.
  4. *
  5. * Copyright IBM Corporation, 2013.
  6. * Parts taken from drivers/vhost/vhost.c Copyright 2009 Red Hat, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. * Written by: Rusty Russell <rusty@rustcorp.com.au>
  23. */
  24. #ifndef _LINUX_VRINGH_H
  25. #define _LINUX_VRINGH_H
  26. #include <uapi/linux/virtio_ring.h>
  27. #include <linux/uio.h>
  28. #include <linux/slab.h>
  29. #include <asm/barrier.h>
  30. /* virtio_ring with information needed for host access. */
  31. struct vringh {
  32. /* Guest publishes used event idx (note: we always do). */
  33. bool event_indices;
  34. /* Can we get away with weak barriers? */
  35. bool weak_barriers;
  36. /* Last available index we saw (ie. where we're up to). */
  37. u16 last_avail_idx;
  38. /* Last index we used. */
  39. u16 last_used_idx;
  40. /* How many descriptors we've completed since last need_notify(). */
  41. u32 completed;
  42. /* The vring (note: it may contain user pointers!) */
  43. struct vring vring;
  44. /* The function to call to notify the guest about added buffers */
  45. void (*notify)(struct vringh *);
  46. };
  47. /**
  48. * struct vringh_config_ops - ops for creating a host vring from a virtio driver
  49. * @find_vrhs: find the host vrings and instantiate them
  50. * vdev: the virtio_device
  51. * nhvrs: the number of host vrings to find
  52. * hvrs: on success, includes new host vrings
  53. * callbacks: array of driver callbacks, for each host vring
  54. * include a NULL entry for vqs that do not need a callback
  55. * Returns 0 on success or error status
  56. * @del_vrhs: free the host vrings found by find_vrhs().
  57. */
  58. struct virtio_device;
  59. typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
  60. struct vringh_config_ops {
  61. int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs,
  62. struct vringh *vrhs[], vrh_callback_t *callbacks[]);
  63. void (*del_vrhs)(struct virtio_device *vdev);
  64. };
  65. /* The memory the vring can access, and what offset to apply. */
  66. struct vringh_range {
  67. u64 start, end_incl;
  68. u64 offset;
  69. };
  70. /**
  71. * struct vringh_iov - iovec mangler.
  72. *
  73. * Mangles iovec in place, and restores it.
  74. * Remaining data is iov + i, of used - i elements.
  75. */
  76. struct vringh_iov {
  77. struct iovec *iov;
  78. size_t consumed; /* Within iov[i] */
  79. unsigned i, used, max_num;
  80. };
  81. /**
  82. * struct vringh_iov - kvec mangler.
  83. *
  84. * Mangles kvec in place, and restores it.
  85. * Remaining data is iov + i, of used - i elements.
  86. */
  87. struct vringh_kiov {
  88. struct kvec *iov;
  89. size_t consumed; /* Within iov[i] */
  90. unsigned i, used, max_num;
  91. };
  92. /* Flag on max_num to indicate we're kmalloced. */
  93. #define VRINGH_IOV_ALLOCATED 0x8000000
  94. /* Helpers for userspace vrings. */
  95. int vringh_init_user(struct vringh *vrh, u32 features,
  96. unsigned int num, bool weak_barriers,
  97. struct vring_desc __user *desc,
  98. struct vring_avail __user *avail,
  99. struct vring_used __user *used);
  100. static inline void vringh_iov_init(struct vringh_iov *iov,
  101. struct iovec *iovec, unsigned num)
  102. {
  103. iov->used = iov->i = 0;
  104. iov->consumed = 0;
  105. iov->max_num = num;
  106. iov->iov = iovec;
  107. }
  108. static inline void vringh_iov_reset(struct vringh_iov *iov)
  109. {
  110. iov->iov[iov->i].iov_len += iov->consumed;
  111. iov->iov[iov->i].iov_base -= iov->consumed;
  112. iov->consumed = 0;
  113. iov->i = 0;
  114. }
  115. static inline void vringh_iov_cleanup(struct vringh_iov *iov)
  116. {
  117. if (iov->max_num & VRINGH_IOV_ALLOCATED)
  118. kfree(iov->iov);
  119. iov->max_num = iov->used = iov->i = iov->consumed = 0;
  120. iov->iov = NULL;
  121. }
  122. /* Convert a descriptor into iovecs. */
  123. int vringh_getdesc_user(struct vringh *vrh,
  124. struct vringh_iov *riov,
  125. struct vringh_iov *wiov,
  126. bool (*getrange)(struct vringh *vrh,
  127. u64 addr, struct vringh_range *r),
  128. u16 *head);
  129. /* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
  130. ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
  131. /* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
  132. ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
  133. const void *src, size_t len);
  134. /* Mark a descriptor as used. */
  135. int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
  136. int vringh_complete_multi_user(struct vringh *vrh,
  137. const struct vring_used_elem used[],
  138. unsigned num_used);
  139. /* Pretend we've never seen descriptor (for easy error handling). */
  140. void vringh_abandon_user(struct vringh *vrh, unsigned int num);
  141. /* Do we need to fire the eventfd to notify the other side? */
  142. int vringh_need_notify_user(struct vringh *vrh);
  143. bool vringh_notify_enable_user(struct vringh *vrh);
  144. void vringh_notify_disable_user(struct vringh *vrh);
  145. /* Helpers for kernelspace vrings. */
  146. int vringh_init_kern(struct vringh *vrh, u32 features,
  147. unsigned int num, bool weak_barriers,
  148. struct vring_desc *desc,
  149. struct vring_avail *avail,
  150. struct vring_used *used);
  151. static inline void vringh_kiov_init(struct vringh_kiov *kiov,
  152. struct kvec *kvec, unsigned num)
  153. {
  154. kiov->used = kiov->i = 0;
  155. kiov->consumed = 0;
  156. kiov->max_num = num;
  157. kiov->iov = kvec;
  158. }
  159. static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
  160. {
  161. kiov->iov[kiov->i].iov_len += kiov->consumed;
  162. kiov->iov[kiov->i].iov_base -= kiov->consumed;
  163. kiov->consumed = 0;
  164. kiov->i = 0;
  165. }
  166. static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
  167. {
  168. if (kiov->max_num & VRINGH_IOV_ALLOCATED)
  169. kfree(kiov->iov);
  170. kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
  171. kiov->iov = NULL;
  172. }
  173. int vringh_getdesc_kern(struct vringh *vrh,
  174. struct vringh_kiov *riov,
  175. struct vringh_kiov *wiov,
  176. u16 *head,
  177. gfp_t gfp);
  178. ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
  179. ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
  180. const void *src, size_t len);
  181. void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
  182. int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
  183. bool vringh_notify_enable_kern(struct vringh *vrh);
  184. void vringh_notify_disable_kern(struct vringh *vrh);
  185. int vringh_need_notify_kern(struct vringh *vrh);
  186. /* Notify the guest about buffers added to the used ring */
  187. static inline void vringh_notify(struct vringh *vrh)
  188. {
  189. if (vrh->notify)
  190. vrh->notify(vrh);
  191. }
  192. #endif /* _LINUX_VRINGH_H */