1 /* $OpenBSD: virtioreg.h,v 1.4 2019/03/24 18:21:12 sf Exp $ */ 2 /* $NetBSD: virtioreg.h,v 1.1 2011/10/30 12:12:21 hannken Exp $ */ 3 4 /* 5 * Copyright (c) 2012 Stefan Fritsch. 6 * Copyright (c) 2010 Minoura Makoto. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT' 32 * Appendix A. 33 */ 34 /* An interface for efficient virtio implementation. 35 * 36 * This header is BSD licensed so anyone can use the definitions 37 * to implement compatible drivers/servers. 38 * 39 * Copyright 2007, 2009, IBM Corporation 40 * All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of IBM nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 */ 65 66 67 #ifndef _DEV_PV_VIRTIOREG_H_ 68 #define _DEV_PV_VIRTIOREG_H_ 69 70 #include <sys/types.h> 71 72 /* Virtio product id (subsystem) */ 73 #define PCI_PRODUCT_VIRTIO_NETWORK 1 74 #define PCI_PRODUCT_VIRTIO_BLOCK 2 75 #define PCI_PRODUCT_VIRTIO_CONSOLE 3 76 #define PCI_PRODUCT_VIRTIO_ENTROPY 4 77 #define PCI_PRODUCT_VIRTIO_BALLOON 5 78 #define PCI_PRODUCT_VIRTIO_IOMEM 6 79 #define PCI_PRODUCT_VIRTIO_RPMSG 7 80 #define PCI_PRODUCT_VIRTIO_SCSI 8 81 #define PCI_PRODUCT_VIRTIO_9P 9 82 #define PCI_PRODUCT_VIRTIO_MAC80211 10 83 #define PCI_PRODUCT_VIRTIO_VMMCI 65535 /* private id */ 84 85 /* device-independent feature bits */ 86 #define VIRTIO_F_NOTIFY_ON_EMPTY (1ULL<<24) 87 #define VIRTIO_F_RING_INDIRECT_DESC (1ULL<<28) 88 #define VIRTIO_F_RING_EVENT_IDX (1ULL<<29) 89 #define VIRTIO_F_BAD_FEATURE (1ULL<<30) 90 #define VIRTIO_F_VERSION_1 (1ULL<<32) 91 92 /* device status bits */ 93 #define VIRTIO_CONFIG_DEVICE_STATUS_RESET 0 94 #define VIRTIO_CONFIG_DEVICE_STATUS_ACK 1 95 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER 2 96 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK 4 97 #define VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK 8 98 #define VIRTIO_CONFIG_DEVICE_STATUS_DEVICE_NEEDS_RESET 64 99 #define VIRTIO_CONFIG_DEVICE_STATUS_FAILED 128 100 101 /* Virtqueue */ 102 /* This marks a buffer as continuing via the next field. */ 103 #define VRING_DESC_F_NEXT 1 104 /* This marks a buffer as write-only (otherwise read-only). */ 105 #define VRING_DESC_F_WRITE 2 106 /* This means the buffer contains a list of buffer descriptors. */ 107 #define VRING_DESC_F_INDIRECT 4 108 109 /* The Host uses this in used->flags to advise the Guest: don't kick me 110 * when you add a buffer. It's unreliable, so it's simply an 111 * optimization. Guest will still kick if it's out of buffers. */ 112 #define VRING_USED_F_NO_NOTIFY 1 113 /* The Guest uses this in avail->flags to advise the Host: don't 114 * interrupt me when you consume a buffer. It's unreliable, so it's 115 * simply an optimization. */ 116 #define VRING_AVAIL_F_NO_INTERRUPT 1 117 118 119 /* The standard layout for the ring is a continuous chunk of memory which 120 * looks like this. We assume num is a power of 2. 121 * 122 * struct vring { 123 * // The actual descriptors (16 bytes each) 124 * struct vring_desc desc[num]; 125 * 126 * // A ring of available descriptor heads with free-running index. 127 * __u16 avail_flags; 128 * __u16 avail_idx; 129 * __u16 available[num]; 130 * __u16 used_event_idx 131 * 132 * // Padding to the next align boundary. 133 * char pad[]; 134 * 135 * // A ring of used descriptor heads with free-running index. 136 * __u16 used_flags; 137 * __u16 used_idx; 138 * struct vring_used_elem used[num]; 139 * __u16 avail_event_idx; 140 * }; 141 * Note: for virtio PCI, align is 4096. 142 */ 143 144 /* Virtio ring descriptors: 16 bytes. 145 * These can chain together via "next". */ 146 struct vring_desc { 147 /* Address (guest-physical). */ 148 uint64_t addr; 149 /* Length. */ 150 uint32_t len; 151 /* The flags as indicated above. */ 152 uint16_t flags; 153 /* We chain unused descriptors via this, too */ 154 uint16_t next; 155 } __packed; 156 157 struct vring_avail { 158 uint16_t flags; 159 uint16_t idx; 160 uint16_t ring[0]; 161 } __packed; 162 163 /* u32 is used here for ids for padding reasons. */ 164 struct vring_used_elem { 165 /* Index of start of used descriptor chain. */ 166 uint32_t id; 167 /* Total length of the descriptor chain which was written to. */ 168 uint32_t len; 169 } __packed; 170 171 struct vring_used { 172 uint16_t flags; 173 uint16_t idx; 174 struct vring_used_elem ring[0]; 175 } __packed; 176 177 /* 178 * We publish the used event index at the end of the available ring, and vice 179 * versa. They are at the end for backwards compatibility. 180 */ 181 #define VQ_USED_EVENT(vq) (*(uint16_t*)(&(vq)->vq_avail->ring[(vq)->vq_num])) 182 #define VQ_AVAIL_EVENT(vq) (*(uint16_t*)(&(vq)->vq_used->ring[(vq)->vq_num])) 183 184 #define VIRTIO_PAGE_SIZE (4096) 185 186 #endif /* _DEV_PV_VIRTIOREG_H_ */ 187