/drivers/vhost/vhost.h

https://github.com/kernel-digger/linux · C Header · 206 lines · 158 code · 33 blank · 15 comment · 2 complexity · 03b4285261c9e64a39deca228e289820 MD5 · raw file

  1. #ifndef _VHOST_H
  2. #define _VHOST_H
  3. #include <linux/eventfd.h>
  4. #include <linux/vhost.h>
  5. #include <linux/mm.h>
  6. #include <linux/mutex.h>
  7. #include <linux/poll.h>
  8. #include <linux/file.h>
  9. #include <linux/uio.h>
  10. #include <linux/virtio_config.h>
  11. #include <linux/virtio_ring.h>
  12. #include <linux/atomic.h>
  13. struct vhost_work;
  14. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  15. struct vhost_work {
  16. struct list_head node;
  17. vhost_work_fn_t fn;
  18. wait_queue_head_t done;
  19. int flushing;
  20. unsigned queue_seq;
  21. unsigned done_seq;
  22. };
  23. /* Poll a file (eventfd or socket) */
  24. /* Note: there's nothing vhost specific about this structure. */
  25. struct vhost_poll {
  26. poll_table table;
  27. wait_queue_head_t *wqh;
  28. wait_queue_t wait;
  29. struct vhost_work work;
  30. unsigned long mask;
  31. struct vhost_dev *dev;
  32. };
  33. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
  34. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
  35. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  36. unsigned long mask, struct vhost_dev *dev);
  37. int vhost_poll_start(struct vhost_poll *poll, struct file *file);
  38. void vhost_poll_stop(struct vhost_poll *poll);
  39. void vhost_poll_flush(struct vhost_poll *poll);
  40. void vhost_poll_queue(struct vhost_poll *poll);
  41. void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
  42. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  43. struct vhost_log {
  44. u64 addr;
  45. u64 len;
  46. };
  47. /* The virtqueue structure describes a queue attached to a device. */
  48. struct vhost_virtqueue {
  49. struct vhost_dev *dev;
  50. /* The actual ring of buffers. */
  51. struct mutex mutex;
  52. unsigned int num;
  53. struct vring_desc __user *desc;
  54. struct vring_avail __user *avail;
  55. struct vring_used __user *used;
  56. struct file *kick;
  57. struct file *call;
  58. struct file *error;
  59. struct eventfd_ctx *call_ctx;
  60. struct eventfd_ctx *error_ctx;
  61. struct eventfd_ctx *log_ctx;
  62. struct vhost_poll poll;
  63. /* The routine to call when the Guest pings us, or timeout. */
  64. vhost_work_fn_t handle_kick;
  65. /* Last available index we saw. */
  66. u16 last_avail_idx;
  67. /* Caches available index value from user. */
  68. u16 avail_idx;
  69. /* Last index we used. */
  70. u16 last_used_idx;
  71. /* Used flags */
  72. u16 used_flags;
  73. /* Last used index value we have signalled on */
  74. u16 signalled_used;
  75. /* Last used index value we have signalled on */
  76. bool signalled_used_valid;
  77. /* Log writes to used structure. */
  78. bool log_used;
  79. u64 log_addr;
  80. struct iovec iov[UIO_MAXIOV];
  81. struct iovec *indirect;
  82. struct vring_used_elem *heads;
  83. /* Protected by virtqueue mutex. */
  84. struct vhost_memory *memory;
  85. void *private_data;
  86. u64 acked_features;
  87. /* Log write descriptors */
  88. void __user *log_base;
  89. struct vhost_log *log;
  90. };
  91. struct vhost_dev {
  92. struct vhost_memory *memory;
  93. struct mm_struct *mm;
  94. struct mutex mutex;
  95. struct vhost_virtqueue **vqs;
  96. int nvqs;
  97. struct file *log_file;
  98. struct eventfd_ctx *log_ctx;
  99. spinlock_t work_lock;
  100. struct list_head work_list;
  101. struct task_struct *worker;
  102. };
  103. void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs);
  104. long vhost_dev_set_owner(struct vhost_dev *dev);
  105. bool vhost_dev_has_owner(struct vhost_dev *dev);
  106. long vhost_dev_check_owner(struct vhost_dev *);
  107. struct vhost_memory *vhost_dev_reset_owner_prepare(void);
  108. void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_memory *);
  109. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  110. void vhost_dev_stop(struct vhost_dev *);
  111. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  112. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  113. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  114. int vhost_log_access_ok(struct vhost_dev *);
  115. int vhost_get_vq_desc(struct vhost_virtqueue *,
  116. struct iovec iov[], unsigned int iov_count,
  117. unsigned int *out_num, unsigned int *in_num,
  118. struct vhost_log *log, unsigned int *log_num);
  119. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  120. int vhost_init_used(struct vhost_virtqueue *);
  121. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  122. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  123. unsigned count);
  124. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  125. unsigned int id, int len);
  126. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  127. struct vring_used_elem *heads, unsigned count);
  128. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  129. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  130. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  131. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  132. unsigned int log_num, u64 len);
  133. #define vq_err(vq, fmt, ...) do { \
  134. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  135. if ((vq)->error_ctx) \
  136. eventfd_signal((vq)->error_ctx, 1);\
  137. } while (0)
  138. enum {
  139. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  140. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  141. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  142. (1ULL << VHOST_F_LOG_ALL),
  143. };
  144. static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
  145. {
  146. return vq->acked_features & (1ULL << bit);
  147. }
  148. /* Memory accessors */
  149. static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
  150. {
  151. return __virtio16_to_cpu(vhost_has_feature(vq, VIRTIO_F_VERSION_1), val);
  152. }
  153. static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
  154. {
  155. return __cpu_to_virtio16(vhost_has_feature(vq, VIRTIO_F_VERSION_1), val);
  156. }
  157. static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
  158. {
  159. return __virtio32_to_cpu(vhost_has_feature(vq, VIRTIO_F_VERSION_1), val);
  160. }
  161. static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
  162. {
  163. return __cpu_to_virtio32(vhost_has_feature(vq, VIRTIO_F_VERSION_1), val);
  164. }
  165. static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
  166. {
  167. return __virtio64_to_cpu(vhost_has_feature(vq, VIRTIO_F_VERSION_1), val);
  168. }
  169. static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
  170. {
  171. return __cpu_to_virtio64(vhost_has_feature(vq, VIRTIO_F_VERSION_1), val);
  172. }
  173. #endif