Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2 | #ifndef _VHOST_H |
| 3 | #define _VHOST_H |
| 4 | |
| 5 | #include <linux/eventfd.h> |
| 6 | #include <linux/vhost.h> |
| 7 | #include <linux/mm.h> |
| 8 | #include <linux/mutex.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 9 | #include <linux/poll.h> |
| 10 | #include <linux/file.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 11 | #include <linux/uio.h> |
| 12 | #include <linux/virtio_config.h> |
| 13 | #include <linux/virtio_ring.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 14 | #include <linux/atomic.h> |
Jason Wang | 7f46603 | 2019-05-24 04:12:18 -0400 | [diff] [blame] | 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/mmu_notifier.h> |
| 17 | #include <asm/cacheflush.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 18 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 19 | struct vhost_work; |
| 20 | typedef void (*vhost_work_fn_t)(struct vhost_work *work); |
| 21 | |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 22 | #define VHOST_WORK_QUEUED 1 |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 23 | struct vhost_work { |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 24 | struct llist_node node; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 25 | vhost_work_fn_t fn; |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 26 | unsigned long flags; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 29 | /* Poll a file (eventfd or socket) */ |
| 30 | /* Note: there's nothing vhost specific about this structure. */ |
| 31 | struct vhost_poll { |
| 32 | poll_table table; |
| 33 | wait_queue_head_t *wqh; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 34 | wait_queue_entry_t wait; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 35 | struct vhost_work work; |
Al Viro | 58e3b60 | 2017-07-03 23:50:40 -0400 | [diff] [blame] | 36 | __poll_t mask; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 37 | struct vhost_dev *dev; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Stefan Hajnoczi | 163049a | 2012-07-21 06:55:37 +0000 | [diff] [blame] | 40 | void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn); |
| 41 | void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work); |
Jason Wang | 526d3e7 | 2016-03-04 06:24:51 -0500 | [diff] [blame] | 42 | bool vhost_has_work(struct vhost_dev *dev); |
Stefan Hajnoczi | 163049a | 2012-07-21 06:55:37 +0000 | [diff] [blame] | 43 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 44 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
Al Viro | 58e3b60 | 2017-07-03 23:50:40 -0400 | [diff] [blame] | 45 | __poll_t mask, struct vhost_dev *dev); |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 46 | int vhost_poll_start(struct vhost_poll *poll, struct file *file); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 47 | void vhost_poll_stop(struct vhost_poll *poll); |
| 48 | void vhost_poll_flush(struct vhost_poll *poll); |
| 49 | void vhost_poll_queue(struct vhost_poll *poll); |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 50 | void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work); |
Sonny Rao | 26b3660 | 2018-03-14 10:05:06 -0700 | [diff] [blame] | 51 | long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 52 | |
| 53 | struct vhost_log { |
| 54 | u64 addr; |
| 55 | u64 len; |
| 56 | }; |
| 57 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 58 | #define START(node) ((node)->start) |
| 59 | #define LAST(node) ((node)->last) |
| 60 | |
| 61 | struct vhost_umem_node { |
| 62 | struct rb_node rb; |
| 63 | struct list_head link; |
| 64 | __u64 start; |
| 65 | __u64 last; |
| 66 | __u64 size; |
| 67 | __u64 userspace_addr; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 68 | __u32 perm; |
| 69 | __u32 flags_padding; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 70 | __u64 __subtree_last; |
| 71 | }; |
| 72 | |
| 73 | struct vhost_umem { |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 74 | struct rb_root_cached umem_tree; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 75 | struct list_head umem_list; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 76 | int numem; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 77 | }; |
| 78 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 79 | enum vhost_uaddr_type { |
| 80 | VHOST_ADDR_DESC = 0, |
| 81 | VHOST_ADDR_AVAIL = 1, |
| 82 | VHOST_ADDR_USED = 2, |
| 83 | VHOST_NUM_ADDRS = 3, |
| 84 | }; |
| 85 | |
Jason Wang | 7f46603 | 2019-05-24 04:12:18 -0400 | [diff] [blame] | 86 | struct vhost_map { |
| 87 | int npages; |
| 88 | void *addr; |
| 89 | struct page **pages; |
| 90 | }; |
| 91 | |
| 92 | struct vhost_uaddr { |
| 93 | unsigned long uaddr; |
| 94 | size_t size; |
| 95 | bool write; |
| 96 | }; |
| 97 | |
Michael S. Tsirkin | 0b4a709 | 2019-06-06 17:17:36 -0400 | [diff] [blame] | 98 | #if defined(CONFIG_MMU_NOTIFIER) && ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 0 |
Michael S. Tsirkin | 73f628e | 2019-07-26 07:49:29 -0400 | [diff] [blame^] | 99 | #define VHOST_ARCH_CAN_ACCEL_UACCESS 0 |
Michael S. Tsirkin | 0b4a709 | 2019-06-06 17:17:36 -0400 | [diff] [blame] | 100 | #else |
| 101 | #define VHOST_ARCH_CAN_ACCEL_UACCESS 0 |
| 102 | #endif |
Jason Wang | 7f46603 | 2019-05-24 04:12:18 -0400 | [diff] [blame] | 103 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 104 | /* The virtqueue structure describes a queue attached to a device. */ |
| 105 | struct vhost_virtqueue { |
| 106 | struct vhost_dev *dev; |
| 107 | |
| 108 | /* The actual ring of buffers. */ |
| 109 | struct mutex mutex; |
| 110 | unsigned int num; |
| 111 | struct vring_desc __user *desc; |
| 112 | struct vring_avail __user *avail; |
| 113 | struct vring_used __user *used; |
Jason Wang | 7f46603 | 2019-05-24 04:12:18 -0400 | [diff] [blame] | 114 | |
| 115 | #if VHOST_ARCH_CAN_ACCEL_UACCESS |
| 116 | /* Read by memory accessors, modified by meta data |
| 117 | * prefetching, MMU notifier and vring ioctl(). |
| 118 | * Synchonrized through mmu_lock (writers) and RCU (writers |
| 119 | * and readers). |
| 120 | */ |
| 121 | struct vhost_map __rcu *maps[VHOST_NUM_ADDRS]; |
| 122 | /* Read by MMU notifier, modified by vring ioctl(), |
| 123 | * synchronized through MMU notifier |
| 124 | * registering/unregistering. |
| 125 | */ |
| 126 | struct vhost_uaddr uaddrs[VHOST_NUM_ADDRS]; |
| 127 | #endif |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 128 | const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS]; |
Jason Wang | 7f46603 | 2019-05-24 04:12:18 -0400 | [diff] [blame] | 129 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 130 | struct file *kick; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 131 | struct eventfd_ctx *call_ctx; |
| 132 | struct eventfd_ctx *error_ctx; |
| 133 | struct eventfd_ctx *log_ctx; |
| 134 | |
| 135 | struct vhost_poll poll; |
| 136 | |
| 137 | /* The routine to call when the Guest pings us, or timeout. */ |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 138 | vhost_work_fn_t handle_kick; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 139 | |
| 140 | /* Last available index we saw. */ |
| 141 | u16 last_avail_idx; |
| 142 | |
| 143 | /* Caches available index value from user. */ |
| 144 | u16 avail_idx; |
| 145 | |
| 146 | /* Last index we used. */ |
| 147 | u16 last_used_idx; |
| 148 | |
| 149 | /* Used flags */ |
| 150 | u16 used_flags; |
| 151 | |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 152 | /* Last used index value we have signalled on */ |
| 153 | u16 signalled_used; |
| 154 | |
| 155 | /* Last used index value we have signalled on */ |
| 156 | bool signalled_used_valid; |
| 157 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 158 | /* Log writes to used structure. */ |
| 159 | bool log_used; |
| 160 | u64 log_addr; |
| 161 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 162 | struct iovec iov[UIO_MAXIOV]; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 163 | struct iovec iotlb_iov[64]; |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 164 | struct iovec *indirect; |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 165 | struct vring_used_elem *heads; |
Asias He | 22fa90c | 2013-05-07 14:54:36 +0800 | [diff] [blame] | 166 | /* Protected by virtqueue mutex. */ |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 167 | struct vhost_umem *umem; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 168 | struct vhost_umem *iotlb; |
Asias He | 22fa90c | 2013-05-07 14:54:36 +0800 | [diff] [blame] | 169 | void *private_data; |
Michael S. Tsirkin | bd82752 | 2014-10-24 14:08:18 +0300 | [diff] [blame] | 170 | u64 acked_features; |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 171 | u64 acked_backend_features; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 172 | /* Log write descriptors */ |
| 173 | void __user *log_base; |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 174 | struct vhost_log *log; |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 175 | |
| 176 | /* Ring endianness. Defaults to legacy native endianness. |
| 177 | * Set to true when starting a modern virtio device. */ |
| 178 | bool is_le; |
| 179 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY |
| 180 | /* Ring endianness requested by userspace for cross-endian support. */ |
| 181 | bool user_be; |
| 182 | #endif |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 183 | u32 busyloop_timeout; |
Jason Wang | 7f46603 | 2019-05-24 04:12:18 -0400 | [diff] [blame] | 184 | spinlock_t mmu_lock; |
| 185 | int invalidate_count; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 188 | struct vhost_msg_node { |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 189 | union { |
| 190 | struct vhost_msg msg; |
| 191 | struct vhost_msg_v2 msg_v2; |
| 192 | }; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 193 | struct vhost_virtqueue *vq; |
| 194 | struct list_head node; |
| 195 | }; |
| 196 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 197 | struct vhost_dev { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 198 | struct mm_struct *mm; |
Jason Wang | 7f46603 | 2019-05-24 04:12:18 -0400 | [diff] [blame] | 199 | #ifdef CONFIG_MMU_NOTIFIER |
| 200 | struct mmu_notifier mmu_notifier; |
| 201 | #endif |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 202 | struct mutex mutex; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 203 | struct vhost_virtqueue **vqs; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 204 | int nvqs; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 205 | struct eventfd_ctx *log_ctx; |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 206 | struct llist_head work_list; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 207 | struct task_struct *worker; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 208 | struct vhost_umem *umem; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 209 | struct vhost_umem *iotlb; |
| 210 | spinlock_t iotlb_lock; |
| 211 | struct list_head read_list; |
| 212 | struct list_head pending_list; |
| 213 | wait_queue_head_t wait; |
Jason Wang | b46a0bf | 2019-01-28 15:05:05 +0800 | [diff] [blame] | 214 | int iov_limit; |
Jason Wang | e82b9b0 | 2019-05-17 00:29:49 -0400 | [diff] [blame] | 215 | int weight; |
| 216 | int byte_weight; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Jason Wang | e82b9b0 | 2019-05-17 00:29:49 -0400 | [diff] [blame] | 219 | bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len); |
Jason Wang | b46a0bf | 2019-01-28 15:05:05 +0800 | [diff] [blame] | 220 | void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, |
Jason Wang | e82b9b0 | 2019-05-17 00:29:49 -0400 | [diff] [blame] | 221 | int nvqs, int iov_limit, int weight, int byte_weight); |
Asias He | 54db63c | 2013-05-06 11:15:59 +0800 | [diff] [blame] | 222 | long vhost_dev_set_owner(struct vhost_dev *dev); |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 223 | bool vhost_dev_has_owner(struct vhost_dev *dev); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 224 | long vhost_dev_check_owner(struct vhost_dev *); |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 225 | struct vhost_umem *vhost_dev_reset_owner_prepare(void); |
| 226 | void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *); |
夷则(Caspar) | f6f93f7 | 2017-12-25 00:08:58 +0800 | [diff] [blame] | 227 | void vhost_dev_cleanup(struct vhost_dev *); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 228 | void vhost_dev_stop(struct vhost_dev *); |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 229 | long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp); |
Sonny Rao | 26b3660 | 2018-03-14 10:05:06 -0700 | [diff] [blame] | 230 | long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 231 | bool vhost_vq_access_ok(struct vhost_virtqueue *vq); |
| 232 | bool vhost_log_access_ok(struct vhost_dev *); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 233 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 234 | int vhost_get_vq_desc(struct vhost_virtqueue *, |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 235 | struct iovec iov[], unsigned int iov_count, |
| 236 | unsigned int *out_num, unsigned int *in_num, |
| 237 | struct vhost_log *log, unsigned int *log_num); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 238 | void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 239 | |
Greg Kurz | 80f7d03 | 2016-02-16 15:59:44 +0100 | [diff] [blame] | 240 | int vhost_vq_init_access(struct vhost_virtqueue *); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 241 | int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 242 | int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, |
| 243 | unsigned count); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 244 | void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 245 | unsigned int id, int len); |
| 246 | void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, |
| 247 | struct vring_used_elem *heads, unsigned count); |
| 248 | void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 249 | void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); |
Jason Wang | d4a6060 | 2016-03-04 06:24:52 -0500 | [diff] [blame] | 250 | bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 251 | bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 252 | |
| 253 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 254 | unsigned int log_num, u64 len, |
| 255 | struct iovec *iov, int count); |
Jason Wang | 9b5e830 | 2019-05-24 04:12:15 -0400 | [diff] [blame] | 256 | int vq_meta_prefetch(struct vhost_virtqueue *vq); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 257 | |
| 258 | struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); |
| 259 | void vhost_enqueue_msg(struct vhost_dev *dev, |
| 260 | struct list_head *head, |
| 261 | struct vhost_msg_node *node); |
| 262 | struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, |
| 263 | struct list_head *head); |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 264 | __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 265 | poll_table *wait); |
| 266 | ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, |
| 267 | int noblock); |
| 268 | ssize_t vhost_chr_write_iter(struct vhost_dev *dev, |
| 269 | struct iov_iter *from); |
| 270 | int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 271 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 272 | #define vq_err(vq, fmt, ...) do { \ |
| 273 | pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ |
| 274 | if ((vq)->error_ctx) \ |
| 275 | eventfd_signal((vq)->error_ctx, 1);\ |
| 276 | } while (0) |
| 277 | |
| 278 | enum { |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 279 | VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | |
| 280 | (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | |
| 281 | (1ULL << VIRTIO_RING_F_EVENT_IDX) | |
Michael S. Tsirkin | 4e9fa50 | 2015-09-09 22:24:56 +0300 | [diff] [blame] | 282 | (1ULL << VHOST_F_LOG_ALL) | |
| 283 | (1ULL << VIRTIO_F_ANY_LAYOUT) | |
| 284 | (1ULL << VIRTIO_F_VERSION_1) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 285 | }; |
| 286 | |
Michael S. Tsirkin | bd82752 | 2014-10-24 14:08:18 +0300 | [diff] [blame] | 287 | static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 288 | { |
Michael S. Tsirkin | bd82752 | 2014-10-24 14:08:18 +0300 | [diff] [blame] | 289 | return vq->acked_features & (1ULL << bit); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 290 | } |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 291 | |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 292 | static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit) |
| 293 | { |
| 294 | return vq->acked_backend_features & (1ULL << bit); |
| 295 | } |
| 296 | |
Michael S. Tsirkin | e407f39 | 2015-10-27 11:37:39 +0200 | [diff] [blame] | 297 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 298 | static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) |
| 299 | { |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 300 | return vq->is_le; |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 301 | } |
Michael S. Tsirkin | e407f39 | 2015-10-27 11:37:39 +0200 | [diff] [blame] | 302 | #else |
| 303 | static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) |
| 304 | { |
| 305 | return virtio_legacy_is_little_endian() || vq->is_le; |
| 306 | } |
| 307 | #endif |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 308 | |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 309 | /* Memory accessors */ |
| 310 | static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val) |
| 311 | { |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 312 | return __virtio16_to_cpu(vhost_is_little_endian(vq), val); |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val) |
| 316 | { |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 317 | return __cpu_to_virtio16(vhost_is_little_endian(vq), val); |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val) |
| 321 | { |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 322 | return __virtio32_to_cpu(vhost_is_little_endian(vq), val); |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val) |
| 326 | { |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 327 | return __cpu_to_virtio32(vhost_is_little_endian(vq), val); |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val) |
| 331 | { |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 332 | return __virtio64_to_cpu(vhost_is_little_endian(vq), val); |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val) |
| 336 | { |
Greg Kurz | ab27c07 | 2015-04-24 14:25:12 +0200 | [diff] [blame] | 337 | return __cpu_to_virtio64(vhost_is_little_endian(vq), val); |
Michael S. Tsirkin | e05fd12 | 2014-10-24 11:48:17 +0300 | [diff] [blame] | 338 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 339 | #endif |