June 2025 | ||||||
Mo | Tu | We | Th | Fr | Sa | Su |
26 | 27 | 28 | 29 | 30 | 31 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 1 | 2 | 3 | 4 | 5 | 6 |
001: #ifndef _LINUX_VIRTIO_RING_H 002: #define _LINUX_VIRTIO_RING_H 003: /* An interface for efficient virtio implementation, currently for use by KVM 004: * and lguest, but hopefully others soon. Do NOT change this since it will 005: * break existing servers and clients. 006: * 007: * This header is BSD licensed so anyone can use the definitions to implement 008: * compatible drivers/servers. 009: * 010: * Redistribution and use in source and binary forms, with or without 011: * modification, are permitted provided that the following conditions 012: * are met: 013: * 1. Redistributions of source code must retain the above copyright 014: * notice, this list of conditions and the following disclaimer. 015: * 2. Redistributions in binary form must reproduce the above copyright 016: * notice, this list of conditions and the following disclaimer in the 017: * documentation and/or other materials provided with the distribution. 018: * 3. Neither the name of IBM nor the names of its contributors 019: * may be used to endorse or promote products derived from this software 020: * without specific prior written permission. 021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 022: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 023: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 024: * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 025: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 027: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 028: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 029: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 030: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 031: * SUCH DAMAGE. 032: * 033: * Copyright Rusty Russell IBM Corporation 2007. */ 034: #include <linux/types.h> 035: 036: /* This marks a buffer as continuing via the next field. */ 037: #define VRING_DESC_F_NEXT 1 038: /* This marks a buffer as write-only (otherwise read-only). */ 039: #define VRING_DESC_F_WRITE 2 040: /* This means the buffer contains a list of buffer descriptors. */ 041: #define VRING_DESC_F_INDIRECT 4 042: 043: /* The Host uses this in used->flags to advise the Guest: don't kick me when 044: * you add a buffer. It's unreliable, so it's simply an optimization. Guest 045: * will still kick if it's out of buffers. */ 046: #define VRING_USED_F_NO_NOTIFY 1 047: /* The Guest uses this in avail->flags to advise the Host: don't interrupt me 048: * when you consume a buffer. It's unreliable, so it's simply an 049: * optimization. */ 050: #define VRING_AVAIL_F_NO_INTERRUPT 1 051: 052: /* We support indirect buffer descriptors */ 053: #define VIRTIO_RING_F_INDIRECT_DESC 28 054: 055: /* The Guest publishes the used index for which it expects an interrupt 056: * at the end of the avail ring. Host should ignore the avail->flags field. */ 057: /* The Host publishes the avail index for which it expects a kick 058: * at the end of the used ring. Guest should ignore the used->flags field. */ 059: #define VIRTIO_RING_F_EVENT_IDX 29 060: 061: /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */ 062: struct vring_desc { 063: /* Address (guest-physical). */ 064: __u64 addr; 065: /* Length. */ 066: __u32 len; 067: /* The flags as indicated above. */ 068: __u16 flags; 069: /* We chain unused descriptors via this, too */ 070: __u16 next; 071: }; 072: 073: struct vring_avail { 074: __u16 flags; 075: __u16 idx; 076: __u16 ring[]; 077: }; 078: 079: /* u32 is used here for ids for padding reasons. */ 080: struct vring_used_elem { 081: /* Index of start of used descriptor chain. */ 082: __u32 id; 083: /* Total length of the descriptor chain which was used (written to) */ 084: __u32 len; 085: }; 086: 087: struct vring_used { 088: __u16 flags; 089: __u16 idx; 090: struct vring_used_elem ring[]; 091: }; 092: 093: struct vring { 094: unsigned int num; 095: 096: struct vring_desc *desc; 097: 098: struct vring_avail *avail; 099: 100: struct vring_used *used; 101: }; 102: 103: /* The standard layout for the ring is a continuous chunk of memory which looks 104: * like this. We assume num is a power of 2. 105: * 106: * struct vring 107: * { 108: * // The actual descriptors (16 bytes each) 109: * struct vring_desc desc[num]; 110: * 111: * // A ring of available descriptor heads with free-running index. 112: * __u16 avail_flags; 113: * __u16 avail_idx; 114: * __u16 available[num]; 115: * __u16 used_event_idx; 116: * 117: * // Padding to the next align boundary. 118: * char pad[]; 119: * 120: * // A ring of used descriptor heads with free-running index. 121: * __u16 used_flags; 122: * __u16 used_idx; 123: * struct vring_used_elem used[num]; 124: * __u16 avail_event_idx; 125: * }; 126: */ 127: /* We publish the used event index at the end of the available ring, and vice 128: * versa. They are at the end for backwards compatibility. */ 129: #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) 130: #define vring_avail_event(vr) (*(__u16 *)&(vr)->used->ring[(vr)->num]) 131: 132: static __inline__ void vring_init(struct vring *vr, unsigned int num, void *p, 133: unsigned long align) 134: { 135: vr->num = num; 136: vr->desc = p; 137: vr->avail = p + num*sizeof(struct vring_desc); 138: vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + sizeof(__u16) 139: + align-1) & ~(align - 1)); 140: } 141: 142: static __inline__ unsigned vring_size(unsigned int num, unsigned long align) 143: { 144: return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (3 + num) 145: + align - 1) & ~(align - 1)) 146: + sizeof(__u16) * 3 + sizeof(struct vring_used_elem) * num; 147: } 148: 149: /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */ 150: /* Assuming a given event_idx value from the other size, if 151: * we have just incremented index from old to new_idx, 152: * should we trigger an event? */ 153: static __inline__ int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old) 154: { 155: /* Note: Xen has similar logic for notification hold-off 156: * in include/xen/interface/io/ring.h with req_event and req_prod 157: * corresponding to event_idx + 1 and new_idx respectively. 158: * Note also that req_event and req_prod in Xen start at 1, 159: * event indexes in virtio start at 0. */ 160: return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old); 161: } 162: 163: #endif /* _LINUX_VIRTIO_RING_H */ 164: