virtio.h revision 238360
1228301Sgrehan/*- 2227652Sgrehan * This header is BSD licensed so anyone can use the definitions to implement 3227652Sgrehan * compatible drivers/servers. 4227652Sgrehan * 5228301Sgrehan * Redistribution and use in source and binary forms, with or without 6228301Sgrehan * modification, are permitted provided that the following conditions 7228301Sgrehan * are met: 8228301Sgrehan * 1. Redistributions of source code must retain the above copyright 9228301Sgrehan * notice, this list of conditions and the following disclaimer. 10228301Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11228301Sgrehan * notice, this list of conditions and the following disclaimer in the 12228301Sgrehan * documentation and/or other materials provided with the distribution. 13228301Sgrehan * 3. Neither the name of IBM nor the names of its contributors 14228301Sgrehan * may be used to endorse or promote products derived from this software 15228301Sgrehan * without specific prior written permission. 16228301Sgrehan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17228301Sgrehan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18228301Sgrehan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19228301Sgrehan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 20228301Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21228301Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22228301Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23228301Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24228301Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25228301Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26228301Sgrehan * SUCH DAMAGE. 27228301Sgrehan * 28227652Sgrehan * $FreeBSD: head/sys/dev/virtio/virtio.h 238360 2012-07-11 02:57:19Z grehan $ 29227652Sgrehan */ 30227652Sgrehan 31227652Sgrehan#ifndef _VIRTIO_H_ 32227652Sgrehan#define _VIRTIO_H_ 33227652Sgrehan 34227652Sgrehanstruct vq_alloc_info; 35227652Sgrehan 36227652Sgrehan/* VirtIO device IDs. */ 37227652Sgrehan#define VIRTIO_ID_NETWORK 0x01 38227652Sgrehan#define VIRTIO_ID_BLOCK 0x02 39227652Sgrehan#define VIRTIO_ID_CONSOLE 0x03 40227652Sgrehan#define VIRTIO_ID_ENTROPY 0x04 41227652Sgrehan#define VIRTIO_ID_BALLOON 0x05 42227652Sgrehan#define VIRTIO_ID_IOMEMORY 0x06 43227652Sgrehan#define VIRTIO_ID_9P 0x09 44227652Sgrehan 45227652Sgrehan/* Status byte for guest to report progress. */ 46227652Sgrehan#define VIRTIO_CONFIG_STATUS_RESET 0x00 47227652Sgrehan#define VIRTIO_CONFIG_STATUS_ACK 0x01 48227652Sgrehan#define VIRTIO_CONFIG_STATUS_DRIVER 0x02 49227652Sgrehan#define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04 50227652Sgrehan#define VIRTIO_CONFIG_STATUS_FAILED 0x80 51227652Sgrehan 52227652Sgrehan/* 53227652Sgrehan * Generate interrupt when the virtqueue ring is 54227652Sgrehan * completely used, even if we've suppressed them. 55227652Sgrehan */ 56227652Sgrehan#define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24) 57227652Sgrehan 58227652Sgrehan/* 59227652Sgrehan * The guest should never negotiate this feature; it 60227652Sgrehan * is used to detect faulty drivers. 61227652Sgrehan */ 62227652Sgrehan#define VIRTIO_F_BAD_FEATURE (1 << 30) 63227652Sgrehan 64227652Sgrehan/* 65227652Sgrehan * Some VirtIO feature bits (currently bits 28 through 31) are 66227652Sgrehan * reserved for the transport being used (eg. virtio_ring), the 67227652Sgrehan * rest are per-device feature bits. 68227652Sgrehan */ 69227652Sgrehan#define VIRTIO_TRANSPORT_F_START 28 70227652Sgrehan#define VIRTIO_TRANSPORT_F_END 32 71227652Sgrehan 72227652Sgrehan/* 73227652Sgrehan * Maximum number of virtqueues per device. 74227652Sgrehan */ 75227652Sgrehan#define VIRTIO_MAX_VIRTQUEUES 8 76227652Sgrehan 77227652Sgrehan/* 78227652Sgrehan * Each virtqueue indirect descriptor list must be physically contiguous. 79227652Sgrehan * To allow us to malloc(9) each list individually, limit the number 80227652Sgrehan * supported to what will fit in one page. With 4KB pages, this is a limit 81227652Sgrehan * of 256 descriptors. If there is ever a need for more, we can switch to 82227652Sgrehan * contigmalloc(9) for the larger allocations, similar to what 83227652Sgrehan * bus_dmamem_alloc(9) does. 84227652Sgrehan * 85227652Sgrehan * Note the sizeof(struct vring_desc) is 16 bytes. 86227652Sgrehan */ 87227652Sgrehan#define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16)) 88227652Sgrehan 89227652Sgrehan/* 90227652Sgrehan * VirtIO instance variables indices. 91227652Sgrehan */ 92227652Sgrehan#define VIRTIO_IVAR_DEVTYPE 1 93227652Sgrehan#define VIRTIO_IVAR_FEATURE_DESC 2 94238360Sgrehan#define VIRTIO_IVAR_VENDOR 3 95238360Sgrehan#define VIRTIO_IVAR_DEVICE 4 96238360Sgrehan#define VIRTIO_IVAR_SUBVENDOR 5 97238360Sgrehan#define VIRTIO_IVAR_SUBDEVICE 6 98227652Sgrehan 99227652Sgrehanstruct virtio_feature_desc { 100227652Sgrehan uint64_t vfd_val; 101227652Sgrehan char *vfd_str; 102227652Sgrehan}; 103227652Sgrehan 104227652Sgrehanconst char *virtio_device_name(uint16_t devid); 105227652Sgrehanvoid virtio_describe(device_t dev, const char *msg, 106227652Sgrehan uint64_t features, struct virtio_feature_desc *feature_desc); 107227652Sgrehan 108227652Sgrehan/* 109227652Sgrehan * VirtIO Bus Methods. 110227652Sgrehan */ 111238360Sgrehanvoid virtio_read_ivar(device_t dev, int ivar, uintptr_t *val); 112238360Sgrehanvoid virtio_write_ivar(device_t dev, int ivar, uintptr_t val); 113227652Sgrehanuint64_t virtio_negotiate_features(device_t dev, uint64_t child_features); 114227652Sgrehanint virtio_alloc_virtqueues(device_t dev, int flags, int nvqs, 115227652Sgrehan struct vq_alloc_info *info); 116227652Sgrehanint virtio_setup_intr(device_t dev, enum intr_type type); 117227652Sgrehanint virtio_with_feature(device_t dev, uint64_t feature); 118227652Sgrehanvoid virtio_stop(device_t dev); 119227652Sgrehanint virtio_reinit(device_t dev, uint64_t features); 120227652Sgrehanvoid virtio_reinit_complete(device_t dev); 121227652Sgrehan 122227652Sgrehan/* 123227652Sgrehan * Read/write a variable amount from the device specific (ie, network) 124227652Sgrehan * configuration region. This region is encoded in the same endian as 125227652Sgrehan * the guest. 126227652Sgrehan */ 127227652Sgrehanvoid virtio_read_device_config(device_t dev, bus_size_t offset, 128227652Sgrehan void *dst, int length); 129227652Sgrehanvoid virtio_write_device_config(device_t dev, bus_size_t offset, 130227652Sgrehan void *src, int length); 131227652Sgrehan 132227652Sgrehan/* Inlined device specific read/write functions for common lengths. */ 133227652Sgrehan#define VIRTIO_RDWR_DEVICE_CONFIG(size, type) \ 134227652Sgrehanstatic inline type \ 135227652Sgrehan__CONCAT(virtio_read_dev_config_,size)(device_t dev, \ 136227652Sgrehan bus_size_t offset) \ 137227652Sgrehan{ \ 138227652Sgrehan type val; \ 139227652Sgrehan virtio_read_device_config(dev, offset, &val, sizeof(type)); \ 140227652Sgrehan return (val); \ 141227652Sgrehan} \ 142227652Sgrehan \ 143227652Sgrehanstatic inline void \ 144227652Sgrehan__CONCAT(virtio_write_dev_config_,size)(device_t dev, \ 145227652Sgrehan bus_size_t offset, type val) \ 146227652Sgrehan{ \ 147227652Sgrehan virtio_write_device_config(dev, offset, &val, sizeof(type)); \ 148227652Sgrehan} 149227652Sgrehan 150227652SgrehanVIRTIO_RDWR_DEVICE_CONFIG(1, uint8_t); 151227652SgrehanVIRTIO_RDWR_DEVICE_CONFIG(2, uint16_t); 152227652SgrehanVIRTIO_RDWR_DEVICE_CONFIG(4, uint32_t); 153227652Sgrehan 154238360Sgrehan#define VIRTIO_READ_IVAR(name, ivar) \ 155238360Sgrehanstatic inline int \ 156238360Sgrehan__CONCAT(virtio_get_,name)(device_t dev) \ 157238360Sgrehan{ \ 158238360Sgrehan uintptr_t val; \ 159238360Sgrehan virtio_read_ivar(dev, ivar, &val); \ 160238360Sgrehan return ((int) val); \ 161238360Sgrehan} 162238360Sgrehan 163238360SgrehanVIRTIO_READ_IVAR(device_type, VIRTIO_IVAR_DEVTYPE); 164238360SgrehanVIRTIO_READ_IVAR(vendor, VIRTIO_IVAR_VENDOR); 165238360SgrehanVIRTIO_READ_IVAR(device, VIRTIO_IVAR_DEVICE); 166238360SgrehanVIRTIO_READ_IVAR(subvendor, VIRTIO_IVAR_SUBVENDOR); 167238360SgrehanVIRTIO_READ_IVAR(subdevice, VIRTIO_IVAR_SUBDEVICE); 168238360Sgrehan 169238360Sgrehan#define VIRTIO_WRITE_IVAR(name, ivar) \ 170238360Sgrehanstatic inline void \ 171238360Sgrehan__CONCAT(virtio_set_,name)(device_t dev, void *val) \ 172238360Sgrehan{ \ 173238360Sgrehan virtio_write_ivar(dev, ivar, (uintptr_t) val); \ 174238360Sgrehan} 175238360Sgrehan 176238360SgrehanVIRTIO_WRITE_IVAR(feature_desc, VIRTIO_IVAR_FEATURE_DESC); 177238360Sgrehan 178227652Sgrehan#endif /* _VIRTIO_H_ */ 179