| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1 | #ifndef _LINUX_VIRTIO_RING_H | 
|  | 2 | #define _LINUX_VIRTIO_RING_H | 
|  | 3 | /* An interface for efficient virtio implementation, currently for use by KVM | 
|  | 4 | * and lguest, but hopefully others soon.  Do NOT change this since it will | 
|  | 5 | * break existing servers and clients. | 
|  | 6 | * | 
|  | 7 | * This header is BSD licensed so anyone can use the definitions to implement | 
|  | 8 | * compatible drivers/servers. | 
|  | 9 | * | 
|  | 10 | * Copyright Rusty Russell IBM Corporation 2007. */ | 
|  | 11 | #include <linux/types.h> | 
|  | 12 |  | 
|  | 13 | /* This marks a buffer as continuing via the next field. */ | 
|  | 14 | #define VRING_DESC_F_NEXT	1 | 
|  | 15 | /* This marks a buffer as write-only (otherwise read-only). */ | 
|  | 16 | #define VRING_DESC_F_WRITE	2 | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 17 | /* This means the buffer contains a list of buffer descriptors. */ | 
|  | 18 | #define VRING_DESC_F_INDIRECT	4 | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 19 |  | 
| Rusty Russell | 426e3e0 | 2008-02-04 23:49:59 -0500 | [diff] [blame] | 20 | /* The Host uses this in used->flags to advise the Guest: don't kick me when | 
|  | 21 | * you add a buffer.  It's unreliable, so it's simply an optimization.  Guest | 
|  | 22 | * will still kick if it's out of buffers. */ | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 23 | #define VRING_USED_F_NO_NOTIFY	1 | 
| Rusty Russell | 426e3e0 | 2008-02-04 23:49:59 -0500 | [diff] [blame] | 24 | /* The Guest uses this in avail->flags to advise the Host: don't interrupt me | 
|  | 25 | * when you consume a buffer.  It's unreliable, so it's simply an | 
|  | 26 | * optimization.  */ | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 27 | #define VRING_AVAIL_F_NO_INTERRUPT	1 | 
|  | 28 |  | 
| Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 29 | /* We support indirect buffer descriptors */ | 
|  | 30 | #define VIRTIO_RING_F_INDIRECT_DESC	28 | 
|  | 31 |  | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 32 | /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */ | 
|  | 33 | struct vring_desc | 
|  | 34 | { | 
|  | 35 | /* Address (guest-physical). */ | 
|  | 36 | __u64 addr; | 
|  | 37 | /* Length. */ | 
|  | 38 | __u32 len; | 
|  | 39 | /* The flags as indicated above. */ | 
|  | 40 | __u16 flags; | 
|  | 41 | /* We chain unused descriptors via this, too */ | 
|  | 42 | __u16 next; | 
|  | 43 | }; | 
|  | 44 |  | 
|  | 45 | struct vring_avail | 
|  | 46 | { | 
|  | 47 | __u16 flags; | 
|  | 48 | __u16 idx; | 
|  | 49 | __u16 ring[]; | 
|  | 50 | }; | 
|  | 51 |  | 
|  | 52 | /* u32 is used here for ids for padding reasons. */ | 
|  | 53 | struct vring_used_elem | 
|  | 54 | { | 
|  | 55 | /* Index of start of used descriptor chain. */ | 
|  | 56 | __u32 id; | 
|  | 57 | /* Total length of the descriptor chain which was used (written to) */ | 
|  | 58 | __u32 len; | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | struct vring_used | 
|  | 62 | { | 
|  | 63 | __u16 flags; | 
|  | 64 | __u16 idx; | 
|  | 65 | struct vring_used_elem ring[]; | 
|  | 66 | }; | 
|  | 67 |  | 
|  | 68 | struct vring { | 
|  | 69 | unsigned int num; | 
|  | 70 |  | 
|  | 71 | struct vring_desc *desc; | 
|  | 72 |  | 
|  | 73 | struct vring_avail *avail; | 
|  | 74 |  | 
|  | 75 | struct vring_used *used; | 
|  | 76 | }; | 
|  | 77 |  | 
|  | 78 | /* The standard layout for the ring is a continuous chunk of memory which looks | 
| Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 79 | * like this.  We assume num is a power of 2. | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 80 | * | 
|  | 81 | * struct vring | 
|  | 82 | * { | 
|  | 83 | *	// The actual descriptors (16 bytes each) | 
|  | 84 | *	struct vring_desc desc[num]; | 
|  | 85 | * | 
|  | 86 | *	// A ring of available descriptor heads with free-running index. | 
|  | 87 | *	__u16 avail_flags; | 
|  | 88 | *	__u16 avail_idx; | 
|  | 89 | *	__u16 available[num]; | 
|  | 90 | * | 
| Rusty Russell | 5f0d1d7 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 91 | *	// Padding to the next align boundary. | 
| Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 92 | *	char pad[]; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 93 | * | 
|  | 94 | *	// A ring of used descriptor heads with free-running index. | 
|  | 95 | *	__u16 used_flags; | 
|  | 96 | *	__u16 used_idx; | 
|  | 97 | *	struct vring_used_elem used[num]; | 
|  | 98 | * }; | 
|  | 99 | */ | 
| Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 100 | static inline void vring_init(struct vring *vr, unsigned int num, void *p, | 
| Rusty Russell | 5f0d1d7 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 101 | unsigned long align) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 102 | { | 
|  | 103 | vr->num = num; | 
|  | 104 | vr->desc = p; | 
| Anthony Liguori | 44332f7 | 2007-11-07 16:31:52 +1100 | [diff] [blame] | 105 | vr->avail = p + num*sizeof(struct vring_desc); | 
| Rusty Russell | 5f0d1d7 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 106 | vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1) | 
|  | 107 | & ~(align - 1)); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Rusty Russell | 5f0d1d7 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 110 | static inline unsigned vring_size(unsigned int num, unsigned long align) | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 111 | { | 
| Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 112 | return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num) | 
| Rusty Russell | 5f0d1d7 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 113 | + align - 1) & ~(align - 1)) | 
| Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 114 | + sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num; | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 | #ifdef __KERNEL__ | 
|  | 118 | #include <linux/irqreturn.h> | 
|  | 119 | struct virtio_device; | 
|  | 120 | struct virtqueue; | 
|  | 121 |  | 
|  | 122 | struct virtqueue *vring_new_virtqueue(unsigned int num, | 
| Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 123 | unsigned int vring_align, | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 124 | struct virtio_device *vdev, | 
|  | 125 | void *pages, | 
|  | 126 | void (*notify)(struct virtqueue *vq), | 
| Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 127 | void (*callback)(struct virtqueue *vq), | 
|  | 128 | const char *name); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 129 | void vring_del_virtqueue(struct virtqueue *vq); | 
| Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 130 | /* Filter out transport-specific feature bits. */ | 
|  | 131 | void vring_transport_features(struct virtio_device *vdev); | 
| Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 132 |  | 
|  | 133 | irqreturn_t vring_interrupt(int irq, void *_vq); | 
|  | 134 | #endif /* __KERNEL__ */ | 
|  | 135 | #endif /* _LINUX_VIRTIO_RING_H */ |