1 /*
   2  * Copyright (c) 2010 Minoura Makoto.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  * 1. Redistributions of source code must retain the above copyright
   8  *    notice, this list of conditions and the following disclaimer.
   9  * 2. Redistributions in binary form must reproduce the above copyright
  10  *    notice, this list of conditions and the following disclaimer in the
  11  *    documentation and/or other materials provided with the distribution.
  12  *
  13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23  */
  24 
  25 /*
  26  * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT'
  27  * Appendix A.
  28  */
  29 
  30 /*
  31  * An interface for efficient virtio implementation.
  32  *
  33  * This header is BSD licensed so anyone can use the definitions
  34  * to implement compatible drivers/servers.
  35  *
  36  * Copyright 2007, 2009, IBM Corporation
  37  * All rights reserved.
  38  *
  39  * Redistribution and use in source and binary forms, with or without
  40  * modification, are permitted provided that the following conditions
  41  * are met:
  42  * 1. Redistributions of source code must retain the above copyright
  43  *    notice, this list of conditions and the following disclaimer.
  44  * 2. Redistributions in binary form must reproduce the above copyright
  45  *    notice, this list of conditions and the following disclaimer in the
  46  *    documentation and/or other materials provided with the distribution.
  47  * 3. Neither the name of IBM nor the names of its contributors
  48  *    may be used to endorse or promote products derived from this software
  49  *    without specific prior written permission.
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * ``AS IS'' ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
  54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  60  * SUCH DAMAGE.
  61  */
  62 
  63 /*
  64  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
  65  */
  66 
  67 #ifndef __VIRTIOREG_H__
  68 #define __VIRTIOREG_H__
  69 
  70 #include <sys/types.h>
  71 
  72 #define PCI_VENDOR_QUMRANET 0x1af4
  73 #define PCI_DEV_VIRTIO_MIN 0x1000
  74 #define PCI_DEV_VIRTIO_MAX 0x103f
  75 #define VIRTIO_PCI_ABI_VERSION 0
  76 
  77 /* Virtio product id (subsystem) */
  78 #define PCI_PRODUCT_VIRTIO_NETWORK      1
  79 #define PCI_PRODUCT_VIRTIO_BLOCK        2
  80 #define PCI_PRODUCT_VIRTIO_CONSOLE      3
  81 #define PCI_PRODUCT_VIRTIO_ENTROPY      4
  82 #define PCI_PRODUCT_VIRTIO_BALLOON      5
  83 #define PCI_PRODUCT_VIRTIO_9P           9
  84 
  85 /* Virtio header */
  86 #define VIRTIO_CONFIG_DEVICE_FEATURES           0 /* 32bit */
  87 #define VIRTIO_CONFIG_GUEST_FEATURES            4 /* 32bit */
  88 
  89 #define VIRTIO_F_NOTIFY_ON_EMPTY                (1<<24)
  90 #define VIRTIO_F_RING_INDIRECT_DESC             (1<<28)
  91 #define VIRTIO_F_BAD_FEATURE                    (1<<30)
  92 
  93 #define VIRTIO_CONFIG_QUEUE_ADDRESS             8 /* 32bit */
  94 #define VIRTIO_CONFIG_QUEUE_SIZE                12 /* 16bit */
  95 #define VIRTIO_CONFIG_QUEUE_SELECT              14 /* 16bit */
  96 #define VIRTIO_CONFIG_QUEUE_NOTIFY              16 /* 16bit */
  97 #define VIRTIO_CONFIG_DEVICE_STATUS             18 /* 8bit */
  98 
  99 #define VIRTIO_CONFIG_DEVICE_STATUS_RESET       0
 100 #define VIRTIO_CONFIG_DEVICE_STATUS_ACK         1
 101 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER      2
 102 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK   4
 103 #define VIRTIO_CONFIG_DEVICE_STATUS_FAILED      128
 104 
 105 #define VIRTIO_CONFIG_ISR_STATUS                19 /* 8bit */
 106 #define VIRTIO_CONFIG_ISR_CONFIG_CHANGE         2
 107 
 108 #define VIRTIO_CONFIG_CONFIG_VECTOR             20 /* 16bit, optional */
 109 #define VIRTIO_CONFIG_QUEUE_VECTOR              22
 110 
 111 #define VIRTIO_CONFIG_DEVICE_CONFIG_NOMSIX      20
 112 #define VIRTIO_CONFIG_DEVICE_CONFIG_MSIX        24
 113 
 114 #define VIRTIO_MSI_NO_VECTOR 0xffff
 115 
 116 /* Virtqueue */
 117 /* This marks a buffer as continuing via the next field. */
 118 #define VRING_DESC_F_NEXT       1
 119 /*
 120  * This marks a buffer as write-only, from the devices's perspective.
 121  * (otherwise read-only).
 122  */
 123 #define VRING_DESC_F_WRITE      2
 124 /* This means the buffer contains a list of buffer descriptors. */
 125 #define VRING_DESC_F_INDIRECT   4
 126 
 127 /*
 128  * The Host uses this in used->flags to advise the Guest: don't kick me
 129  * when you add a buffer.  It's unreliable, so it's simply an
 130  * optimization.  Guest will still kick if it's out of buffers.
 131  */
 132 #define VRING_USED_F_NO_NOTIFY  1
 133 /*
 134  *  The Guest uses this in avail->flags to advise the Host: don't
 135  * interrupt me when you consume a buffer.  It's unreliable, so it's
 136  * simply an optimization.
 137  */
 138 #define VRING_AVAIL_F_NO_INTERRUPT      1
 139 
 140 /*
 141  * Virtio ring descriptors: 16 bytes.
 142  * These can chain together via "next".
 143  */
 144 struct vring_desc {
 145         /* Address (guest-physical). */
 146         uint64_t addr;
 147         /* Length. */
 148         uint32_t len;
 149         /* The flags as indicated above. */
 150         uint16_t flags;
 151         /* We chain unused descriptors via this, too */
 152         uint16_t next;
 153 } __attribute__((packed));
 154 
 155 struct vring_avail {
 156         uint16_t flags;
 157         uint16_t idx;
 158         uint16_t ring[];
 159 } __attribute__((packed));
 160 
 161 /* u32 is used here for ids for padding reasons. */
 162 struct vring_used_elem {
 163         /* Index of start of used descriptor chain. */
 164         uint32_t id;
 165         /* Total length of the descriptor chain which was written to. */
 166         uint32_t len;
 167 } __attribute__((packed));
 168 
 169 struct vring_used {
 170         uint16_t flags;
 171         uint16_t idx;
 172         struct vring_used_elem ring[];
 173 } __attribute__((packed));
 174 
 175 
 176 /* Got nothing to do with the system page size, just a confusing name. */
 177 #define VIRTIO_PAGE_SIZE        (4096)
 178 
 179 #endif /* __VIRTIOREG_H__ */