1228301Sgrehan/*- 2228301Sgrehan * Copyright Rusty Russell IBM Corporation 2007. 3227652Sgrehan * 4228301Sgrehan * This header is BSD licensed so anyone can use the definitions to implement 5228301Sgrehan * compatible drivers/servers. 6228301Sgrehan * 7228301Sgrehan * Redistribution and use in source and binary forms, with or without 8228301Sgrehan * modification, are permitted provided that the following conditions 9228301Sgrehan * are met: 10228301Sgrehan * 1. Redistributions of source code must retain the above copyright 11228301Sgrehan * notice, this list of conditions and the following disclaimer. 12228301Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 13228301Sgrehan * notice, this list of conditions and the following disclaimer in the 14228301Sgrehan * documentation and/or other materials provided with the distribution. 15228301Sgrehan * 3. Neither the name of IBM nor the names of its contributors 16228301Sgrehan * may be used to endorse or promote products derived from this software 17228301Sgrehan * without specific prior written permission. 18228301Sgrehan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19228301Sgrehan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20228301Sgrehan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21228301Sgrehan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 22228301Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23228301Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24228301Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25228301Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26228301Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27228301Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28228301Sgrehan * SUCH DAMAGE. 29228301Sgrehan * 30228301Sgrehan * $FreeBSD$ 31227652Sgrehan */ 32227652Sgrehan 33227652Sgrehan#ifndef VIRTIO_RING_H 34227652Sgrehan#define VIRTIO_RING_H 35227652Sgrehan 36227652Sgrehan/* This marks a buffer as continuing via the next field. */ 37227652Sgrehan#define VRING_DESC_F_NEXT 1 38227652Sgrehan/* This marks a buffer as write-only (otherwise read-only). */ 39227652Sgrehan#define VRING_DESC_F_WRITE 2 40227652Sgrehan/* This means the buffer contains a list of buffer descriptors. */ 41227652Sgrehan#define VRING_DESC_F_INDIRECT 4 42227652Sgrehan 43227652Sgrehan/* The Host uses this in used->flags to advise the Guest: don't kick me 44227652Sgrehan * when you add a buffer. It's unreliable, so it's simply an 45227652Sgrehan * optimization. Guest will still kick if it's out of buffers. */ 46227652Sgrehan#define VRING_USED_F_NO_NOTIFY 1 47227652Sgrehan/* The Guest uses this in avail->flags to advise the Host: don't 48227652Sgrehan * interrupt me when you consume a buffer. It's unreliable, so it's 49227652Sgrehan * simply an optimization. */ 50227652Sgrehan#define VRING_AVAIL_F_NO_INTERRUPT 1 51227652Sgrehan 52227652Sgrehan/* VirtIO ring descriptors: 16 bytes. 53227652Sgrehan * These can chain together via "next". */ 54227652Sgrehanstruct vring_desc { 55227652Sgrehan /* Address (guest-physical). */ 56227652Sgrehan uint64_t addr; 57227652Sgrehan /* Length. */ 58227652Sgrehan uint32_t len; 59227652Sgrehan /* The flags as indicated above. */ 60227652Sgrehan uint16_t flags; 61227652Sgrehan /* We chain unused descriptors via this, too. */ 62227652Sgrehan uint16_t next; 63227652Sgrehan}; 64227652Sgrehan 65227652Sgrehanstruct vring_avail { 66227652Sgrehan uint16_t flags; 67227652Sgrehan uint16_t idx; 68227652Sgrehan uint16_t ring[0]; 69227652Sgrehan}; 70227652Sgrehan 71227652Sgrehan/* uint32_t is used here for ids for padding reasons. */ 72227652Sgrehanstruct vring_used_elem { 73227652Sgrehan /* Index of start of used descriptor chain. */ 74227652Sgrehan uint32_t id; 75227652Sgrehan /* Total length of the descriptor chain which was written to. */ 76227652Sgrehan uint32_t len; 77227652Sgrehan}; 78227652Sgrehan 79227652Sgrehanstruct vring_used { 80227652Sgrehan uint16_t flags; 81227652Sgrehan uint16_t idx; 82227652Sgrehan struct vring_used_elem ring[0]; 83227652Sgrehan}; 84227652Sgrehan 85227652Sgrehanstruct vring { 86227652Sgrehan unsigned int num; 87227652Sgrehan 88227652Sgrehan struct vring_desc *desc; 89227652Sgrehan struct vring_avail *avail; 90227652Sgrehan struct vring_used *used; 91227652Sgrehan}; 92227652Sgrehan 93227652Sgrehan/* The standard layout for the ring is a continuous chunk of memory which 94227652Sgrehan * looks like this. We assume num is a power of 2. 95227652Sgrehan * 96227652Sgrehan * struct vring { 97227652Sgrehan * // The actual descriptors (16 bytes each) 98227652Sgrehan * struct vring_desc desc[num]; 99227652Sgrehan * 100227652Sgrehan * // A ring of available descriptor heads with free-running index. 101227652Sgrehan * __u16 avail_flags; 102227652Sgrehan * __u16 avail_idx; 103227652Sgrehan * __u16 available[num]; 104234270Sgrehan * __u16 used_event_idx; 105227652Sgrehan * 106227652Sgrehan * // Padding to the next align boundary. 107227652Sgrehan * char pad[]; 108227652Sgrehan * 109227652Sgrehan * // A ring of used descriptor heads with free-running index. 110227652Sgrehan * __u16 used_flags; 111227652Sgrehan * __u16 used_idx; 112227652Sgrehan * struct vring_used_elem used[num]; 113234270Sgrehan * __u16 avail_event_idx; 114227652Sgrehan * }; 115227652Sgrehan * 116227652Sgrehan * NOTE: for VirtIO PCI, align is 4096. 117227652Sgrehan */ 118227652Sgrehan 119234270Sgrehan/* 120234270Sgrehan * We publish the used event index at the end of the available ring, and vice 121234270Sgrehan * versa. They are at the end for backwards compatibility. 122234270Sgrehan */ 123238360Sgrehan#define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) 124238360Sgrehan#define vring_avail_event(vr) (*(uint16_t *)&(vr)->used->ring[(vr)->num]) 125234270Sgrehan 126227652Sgrehanstatic inline int 127227652Sgrehanvring_size(unsigned int num, unsigned long align) 128227652Sgrehan{ 129227652Sgrehan int size; 130227652Sgrehan 131227652Sgrehan size = num * sizeof(struct vring_desc); 132238360Sgrehan size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) + 133238360Sgrehan sizeof(uint16_t); 134227652Sgrehan size = (size + align - 1) & ~(align - 1); 135227652Sgrehan size += sizeof(struct vring_used) + 136238360Sgrehan (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t); 137227652Sgrehan return (size); 138227652Sgrehan} 139227652Sgrehan 140227652Sgrehanstatic inline void 141227652Sgrehanvring_init(struct vring *vr, unsigned int num, uint8_t *p, 142227652Sgrehan unsigned long align) 143227652Sgrehan{ 144227652Sgrehan vr->num = num; 145227652Sgrehan vr->desc = (struct vring_desc *) p; 146227652Sgrehan vr->avail = (struct vring_avail *) (p + 147227652Sgrehan num * sizeof(struct vring_desc)); 148227652Sgrehan vr->used = (void *) 149227652Sgrehan (((unsigned long) &vr->avail->ring[num] + align-1) & ~(align-1)); 150227652Sgrehan} 151234270Sgrehan 152234270Sgrehan/* 153234270Sgrehan * The following is used with VIRTIO_RING_F_EVENT_IDX. 154234270Sgrehan * 155234270Sgrehan * Assuming a given event_idx value from the other size, if we have 156234270Sgrehan * just incremented index from old to new_idx, should we trigger an 157234270Sgrehan * event? 158234270Sgrehan */ 159234270Sgrehanstatic inline int 160234270Sgrehanvring_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old) 161234270Sgrehan{ 162234270Sgrehan 163234270Sgrehan return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old); 164234270Sgrehan} 165227652Sgrehan#endif /* VIRTIO_RING_H */ 166