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