virtio_ring.h revision 227652
1227652Sgrehan/* 2227652Sgrehan * This header is BSD licensed so anyone can use the definitions 3227652Sgrehan * to implement compatible drivers/servers. 4227652Sgrehan * 5227652Sgrehan * Copyright Rusty Russell IBM Corporation 2007. 6227652Sgrehan */ 7227652Sgrehan/* $FreeBSD: head/sys/dev/virtio/virtio_ring.h 227652 2011-11-18 05:43:43Z grehan $ */ 8227652Sgrehan 9227652Sgrehan#ifndef VIRTIO_RING_H 10227652Sgrehan#define VIRTIO_RING_H 11227652Sgrehan 12227652Sgrehan#include <sys/types.h> 13227652Sgrehan 14227652Sgrehan/* This marks a buffer as continuing via the next field. */ 15227652Sgrehan#define VRING_DESC_F_NEXT 1 16227652Sgrehan/* This marks a buffer as write-only (otherwise read-only). */ 17227652Sgrehan#define VRING_DESC_F_WRITE 2 18227652Sgrehan/* This means the buffer contains a list of buffer descriptors. */ 19227652Sgrehan#define VRING_DESC_F_INDIRECT 4 20227652Sgrehan 21227652Sgrehan/* The Host uses this in used->flags to advise the Guest: don't kick me 22227652Sgrehan * when you add a buffer. It's unreliable, so it's simply an 23227652Sgrehan * optimization. Guest will still kick if it's out of buffers. */ 24227652Sgrehan#define VRING_USED_F_NO_NOTIFY 1 25227652Sgrehan/* The Guest uses this in avail->flags to advise the Host: don't 26227652Sgrehan * interrupt me when you consume a buffer. It's unreliable, so it's 27227652Sgrehan * simply an optimization. */ 28227652Sgrehan#define VRING_AVAIL_F_NO_INTERRUPT 1 29227652Sgrehan 30227652Sgrehan/* VirtIO ring descriptors: 16 bytes. 31227652Sgrehan * These can chain together via "next". */ 32227652Sgrehanstruct vring_desc { 33227652Sgrehan /* Address (guest-physical). */ 34227652Sgrehan uint64_t addr; 35227652Sgrehan /* Length. */ 36227652Sgrehan uint32_t len; 37227652Sgrehan /* The flags as indicated above. */ 38227652Sgrehan uint16_t flags; 39227652Sgrehan /* We chain unused descriptors via this, too. */ 40227652Sgrehan uint16_t next; 41227652Sgrehan}; 42227652Sgrehan 43227652Sgrehanstruct vring_avail { 44227652Sgrehan uint16_t flags; 45227652Sgrehan uint16_t idx; 46227652Sgrehan uint16_t ring[0]; 47227652Sgrehan}; 48227652Sgrehan 49227652Sgrehan/* uint32_t is used here for ids for padding reasons. */ 50227652Sgrehanstruct vring_used_elem { 51227652Sgrehan /* Index of start of used descriptor chain. */ 52227652Sgrehan uint32_t id; 53227652Sgrehan /* Total length of the descriptor chain which was written to. */ 54227652Sgrehan uint32_t len; 55227652Sgrehan}; 56227652Sgrehan 57227652Sgrehanstruct vring_used { 58227652Sgrehan uint16_t flags; 59227652Sgrehan uint16_t idx; 60227652Sgrehan struct vring_used_elem ring[0]; 61227652Sgrehan}; 62227652Sgrehan 63227652Sgrehanstruct vring { 64227652Sgrehan unsigned int num; 65227652Sgrehan 66227652Sgrehan struct vring_desc *desc; 67227652Sgrehan struct vring_avail *avail; 68227652Sgrehan struct vring_used *used; 69227652Sgrehan}; 70227652Sgrehan 71227652Sgrehan/* The standard layout for the ring is a continuous chunk of memory which 72227652Sgrehan * looks like this. We assume num is a power of 2. 73227652Sgrehan * 74227652Sgrehan * struct vring { 75227652Sgrehan * // The actual descriptors (16 bytes each) 76227652Sgrehan * struct vring_desc desc[num]; 77227652Sgrehan * 78227652Sgrehan * // A ring of available descriptor heads with free-running index. 79227652Sgrehan * __u16 avail_flags; 80227652Sgrehan * __u16 avail_idx; 81227652Sgrehan * __u16 available[num]; 82227652Sgrehan * 83227652Sgrehan * // Padding to the next align boundary. 84227652Sgrehan * char pad[]; 85227652Sgrehan * 86227652Sgrehan * // A ring of used descriptor heads with free-running index. 87227652Sgrehan * __u16 used_flags; 88227652Sgrehan * __u16 used_idx; 89227652Sgrehan * struct vring_used_elem used[num]; 90227652Sgrehan * }; 91227652Sgrehan * 92227652Sgrehan * NOTE: for VirtIO PCI, align is 4096. 93227652Sgrehan */ 94227652Sgrehan 95227652Sgrehanstatic inline int 96227652Sgrehanvring_size(unsigned int num, unsigned long align) 97227652Sgrehan{ 98227652Sgrehan int size; 99227652Sgrehan 100227652Sgrehan size = num * sizeof(struct vring_desc); 101227652Sgrehan size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)); 102227652Sgrehan size = (size + align - 1) & ~(align - 1); 103227652Sgrehan size += sizeof(struct vring_used) + 104227652Sgrehan (num * sizeof(struct vring_used_elem)); 105227652Sgrehan return (size); 106227652Sgrehan} 107227652Sgrehan 108227652Sgrehanstatic inline void 109227652Sgrehanvring_init(struct vring *vr, unsigned int num, uint8_t *p, 110227652Sgrehan unsigned long align) 111227652Sgrehan{ 112227652Sgrehan vr->num = num; 113227652Sgrehan vr->desc = (struct vring_desc *) p; 114227652Sgrehan vr->avail = (struct vring_avail *) (p + 115227652Sgrehan num * sizeof(struct vring_desc)); 116227652Sgrehan vr->used = (void *) 117227652Sgrehan (((unsigned long) &vr->avail->ring[num] + align-1) & ~(align-1)); 118227652Sgrehan} 119227652Sgrehan#endif /* VIRTIO_RING_H */ 120