1 /* 2 * This header is BSD licensed so anyone can use the definitions to implement 3 * compatible drivers/servers. 4 * 5 * $FreeBSD$ 6 */ 7 8 #ifndef _VIRTIO_H_ 9 #define _VIRTIO_H_ 10 11 #include <sys/types.h> 12 13 struct vq_alloc_info; 14 15 /* VirtIO device IDs. */ 16 #define VIRTIO_ID_NETWORK 0x01 17 #define VIRTIO_ID_BLOCK 0x02 18 #define VIRTIO_ID_CONSOLE 0x03 19 #define VIRTIO_ID_ENTROPY 0x04 20 #define VIRTIO_ID_BALLOON 0x05 21 #define VIRTIO_ID_IOMEMORY 0x06 22 #define VIRTIO_ID_9P 0x09 23 24 /* Status byte for guest to report progress. */ 25 #define VIRTIO_CONFIG_STATUS_RESET 0x00 26 #define VIRTIO_CONFIG_STATUS_ACK 0x01 27 #define VIRTIO_CONFIG_STATUS_DRIVER 0x02 28 #define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04 29 #define VIRTIO_CONFIG_STATUS_FAILED 0x80 30 31 /* 32 * Generate interrupt when the virtqueue ring is 33 * completely used, even if we've suppressed them. 34 */ 35 #define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24) 36 37 /* 38 * The guest should never negotiate this feature; it 39 * is used to detect faulty drivers. 40 */ 41 #define VIRTIO_F_BAD_FEATURE (1 << 30) 42 43 /* 44 * Some VirtIO feature bits (currently bits 28 through 31) are 45 * reserved for the transport being used (eg. virtio_ring), the 46 * rest are per-device feature bits. 47 */ 48 #define VIRTIO_TRANSPORT_F_START 28 49 #define VIRTIO_TRANSPORT_F_END 32 50 51 /* 52 * Maximum number of virtqueues per device. 53 */ 54 #define VIRTIO_MAX_VIRTQUEUES 8 55 56 /* 57 * Each virtqueue indirect descriptor list must be physically contiguous. 58 * To allow us to malloc(9) each list individually, limit the number 59 * supported to what will fit in one page. With 4KB pages, this is a limit 60 * of 256 descriptors. If there is ever a need for more, we can switch to 61 * contigmalloc(9) for the larger allocations, similar to what 62 * bus_dmamem_alloc(9) does. 63 * 64 * Note the sizeof(struct vring_desc) is 16 bytes. 65 */ 66 #define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16)) 67 68 /* 69 * VirtIO instance variables indices. 70 */ 71 #define VIRTIO_IVAR_DEVTYPE 1 72 #define VIRTIO_IVAR_FEATURE_DESC 2 73 74 struct virtio_feature_desc { 75 uint64_t vfd_val; 76 char *vfd_str; 77 }; 78 79 const char *virtio_device_name(uint16_t devid); 80 int virtio_get_device_type(device_t dev); 81 void virtio_set_feature_desc(device_t dev, 82 struct virtio_feature_desc *feature_desc); 83 void virtio_describe(device_t dev, const char *msg, 84 uint64_t features, struct virtio_feature_desc *feature_desc); 85 86 /* 87 * VirtIO Bus Methods. 88 */ 89 uint64_t virtio_negotiate_features(device_t dev, uint64_t child_features); 90 int virtio_alloc_virtqueues(device_t dev, int flags, int nvqs, 91 struct vq_alloc_info *info); 92 int virtio_setup_intr(device_t dev, enum intr_type type); 93 int virtio_with_feature(device_t dev, uint64_t feature); 94 void virtio_stop(device_t dev); 95 int virtio_reinit(device_t dev, uint64_t features); 96 void virtio_reinit_complete(device_t dev); 97 98 /* 99 * Read/write a variable amount from the device specific (ie, network) 100 * configuration region. This region is encoded in the same endian as 101 * the guest. 102 */ 103 void virtio_read_device_config(device_t dev, bus_size_t offset, 104 void *dst, int length); 105 void virtio_write_device_config(device_t dev, bus_size_t offset, 106 void *src, int length); 107 108 /* Inlined device specific read/write functions for common lengths. */ 109 #define VIRTIO_RDWR_DEVICE_CONFIG(size, type) \ 110 static inline type \ 111 __CONCAT(virtio_read_dev_config_,size)(device_t dev, \ 112 bus_size_t offset) \ 113 { \ 114 type val; \ 115 virtio_read_device_config(dev, offset, &val, sizeof(type)); \ 116 return (val); \ 117 } \ 118 \ 119 static inline void \ 120 __CONCAT(virtio_write_dev_config_,size)(device_t dev, \ 121 bus_size_t offset, type val) \ 122 { \ 123 virtio_write_device_config(dev, offset, &val, sizeof(type)); \ 124 } 125 126 VIRTIO_RDWR_DEVICE_CONFIG(1, uint8_t); 127 VIRTIO_RDWR_DEVICE_CONFIG(2, uint16_t); 128 VIRTIO_RDWR_DEVICE_CONFIG(4, uint32_t); 129 130 #endif /* _VIRTIO_H_ */ 131