1 /* $OpenBSD: virtiovar.h,v 1.14 2019/05/26 15:22:31 sf Exp $ */ 2 /* $NetBSD: virtiovar.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_VIRTIOVAR_H_ 68 #define _DEV_PV_VIRTIOVAR_H_ 69 70 #include <sys/param.h> 71 #include <sys/queue.h> 72 #include <sys/device.h> 73 #include <sys/mutex.h> 74 #include <machine/bus.h> 75 76 #include <dev/pv/virtioreg.h> 77 78 #ifndef VIRTIO_DEBUG 79 #define VIRTIO_DEBUG 0 80 #endif 81 82 /* flags for config(8) */ 83 #define VIRTIO_CF_NO_INDIRECT 1 84 #define VIRTIO_CF_NO_EVENT_IDX 2 85 #define VIRTIO_CF_PREFER_VERSION_1 4 86 #define VIRTIO_CF_NO_VERSION_1 8 87 88 struct vq_entry { 89 SLIST_ENTRY(vq_entry) qe_list; /* free list */ 90 uint16_t qe_index; /* index in vq_desc array */ 91 92 /* The following are used only in the `head' entry */ 93 int16_t qe_next; /* next enq slot */ 94 int16_t qe_indirect; /* 1 if using indirect */ 95 int16_t qe_vr_index; /* index in sc_reqs array */ 96 struct vring_desc *qe_desc_base; /* pointer to vd array */ 97 }; 98 99 struct virtqueue { 100 struct virtio_softc *vq_owner; 101 unsigned int vq_num; /* queue size (# of entries) */ 102 unsigned int vq_mask; /* (1 << vq_num - 1) */ 103 int vq_index; /* queue number (0, 1, ...) */ 104 105 /* vring pointers (KVA) */ 106 struct vring_desc *vq_desc; 107 struct vring_avail *vq_avail; 108 struct vring_used *vq_used; 109 void *vq_indirect; 110 111 /* virtqueue allocation info */ 112 void *vq_vaddr; 113 int vq_availoffset; 114 int vq_usedoffset; 115 int vq_indirectoffset; 116 bus_dma_segment_t vq_segs[1]; 117 unsigned int vq_bytesize; 118 bus_dmamap_t vq_dmamap; 119 120 int vq_maxnsegs; 121 122 /* free entry management */ 123 struct vq_entry *vq_entries; 124 SLIST_HEAD(, vq_entry) vq_freelist; 125 struct mutex *vq_freelist_lock; 126 127 /* enqueue/dequeue status */ 128 uint16_t vq_avail_idx; 129 uint16_t vq_used_idx; 130 int vq_queued; 131 struct mutex *vq_aring_lock; 132 struct mutex *vq_uring_lock; 133 134 /* interrupt handler */ 135 int (*vq_done)(struct virtqueue*); 136 /* 1.x only: offset for notify address calculation */ 137 uint32_t vq_notify_off; 138 }; 139 140 struct virtio_feature_name { 141 uint32_t bit; 142 const char *name; 143 }; 144 145 struct virtio_ops { 146 void (*kick)(struct virtio_softc *, uint16_t); 147 uint8_t (*read_dev_cfg_1)(struct virtio_softc *, int); 148 uint16_t (*read_dev_cfg_2)(struct virtio_softc *, int); 149 uint32_t (*read_dev_cfg_4)(struct virtio_softc *, int); 150 uint64_t (*read_dev_cfg_8)(struct virtio_softc *, int); 151 void (*write_dev_cfg_1)(struct virtio_softc *, int, uint8_t); 152 void (*write_dev_cfg_2)(struct virtio_softc *, int, uint16_t); 153 void (*write_dev_cfg_4)(struct virtio_softc *, int, uint32_t); 154 void (*write_dev_cfg_8)(struct virtio_softc *, int, uint64_t); 155 uint16_t (*read_queue_size)(struct virtio_softc *, uint16_t); 156 void (*setup_queue)(struct virtio_softc *, struct virtqueue *, uint64_t); 157 void (*set_status)(struct virtio_softc *, int); 158 int (*neg_features)(struct virtio_softc *, const struct virtio_feature_name *); 159 int (*poll_intr)(void *); 160 }; 161 162 #define VIRTIO_CHILD_ERROR ((void*)1) 163 164 struct virtio_softc { 165 struct device sc_dev; 166 bus_dma_tag_t sc_dmat; /* set by transport */ 167 struct virtio_ops *sc_ops; /* set by transport */ 168 169 int sc_ipl; /* set by child */ 170 171 uint64_t sc_driver_features; 172 uint64_t sc_active_features; 173 int sc_indirect; 174 int sc_version_1; 175 176 int sc_nvqs; /* set by child */ 177 struct virtqueue *sc_vqs; /* set by child */ 178 179 int sc_childdevid; /* set by transport */ 180 struct device *sc_child; /* set by child, 181 * VIRTIO_CHILD_ERROR on error 182 */ 183 int (*sc_config_change)(struct virtio_softc*); 184 /* set by child */ 185 }; 186 187 /* public interface */ 188 #define virtio_read_device_config_1(sc, o) (sc)->sc_ops->read_dev_cfg_1(sc, o) 189 #define virtio_read_device_config_2(sc, o) (sc)->sc_ops->read_dev_cfg_2(sc, o) 190 #define virtio_read_device_config_4(sc, o) (sc)->sc_ops->read_dev_cfg_4(sc, o) 191 #define virtio_read_device_config_8(sc, o) (sc)->sc_ops->read_dev_cfg_8(sc, o) 192 #define virtio_write_device_config_1(sc, o, v) (sc)->sc_ops->write_dev_cfg_1(sc, o, v) 193 #define virtio_write_device_config_2(sc, o, v) (sc)->sc_ops->write_dev_cfg_2(sc, o, v) 194 #define virtio_write_device_config_4(sc, o, v) (sc)->sc_ops->write_dev_cfg_4(sc, o, v) 195 #define virtio_write_device_config_8(sc, o, v) (sc)->sc_ops->write_dev_cfg_8(sc, o, v) 196 #define virtio_read_queue_size(sc, i) (sc)->sc_ops->read_queue_size(sc, i) 197 #define virtio_setup_queue(sc, i, v) (sc)->sc_ops->setup_queue(sc, i, v) 198 #define virtio_negotiate_features(sc, n) (sc)->sc_ops->neg_features(sc, n) 199 #define virtio_poll_intr(sc) (sc)->sc_ops->poll_intr(sc) 200 201 /* only for transport drivers */ 202 #define virtio_set_status(sc, i) (sc)->sc_ops->set_status(sc, i) 203 #define virtio_device_reset(sc) virtio_set_status((sc), 0) 204 205 static inline int 206 virtio_has_feature(struct virtio_softc *sc, unsigned int fbit) 207 { 208 if (sc->sc_active_features & fbit) 209 return 1; 210 return 0; 211 } 212 213 int virtio_alloc_vq(struct virtio_softc*, struct virtqueue*, int, int, int, 214 const char*); 215 int virtio_free_vq(struct virtio_softc*, struct virtqueue*); 216 void virtio_reset(struct virtio_softc *); 217 void virtio_reinit_start(struct virtio_softc *); 218 void virtio_reinit_end(struct virtio_softc *); 219 220 int virtio_enqueue_prep(struct virtqueue*, int*); 221 int virtio_enqueue_reserve(struct virtqueue*, int, int); 222 int virtio_enqueue(struct virtqueue*, int, bus_dmamap_t, int); 223 int virtio_enqueue_p(struct virtqueue*, int, bus_dmamap_t, bus_addr_t, 224 bus_size_t, int); 225 void virtio_enqueue_commit(struct virtio_softc*, struct virtqueue*, int, int); 226 #define virtio_notify(sc,vq) virtio_enqueue_commit(sc, vq, -1, 1) 227 228 int virtio_enqueue_abort(struct virtqueue*, int); 229 void virtio_enqueue_trim(struct virtqueue*, int, int); 230 231 int virtio_dequeue(struct virtio_softc*, struct virtqueue*, int *, int *); 232 int virtio_dequeue_commit(struct virtqueue*, int); 233 234 int virtio_intr(void *arg); 235 int virtio_check_vqs(struct virtio_softc *); 236 void virtio_stop_vq_intr(struct virtio_softc *, struct virtqueue *); 237 int virtio_start_vq_intr(struct virtio_softc *, struct virtqueue *); 238 239 const char *virtio_device_string(int); 240 #if VIRTIO_DEBUG 241 void virtio_log_features(uint64_t, uint64_t, const struct virtio_feature_name *); 242 void virtio_vq_dump(struct virtqueue *vq); 243 #endif 244 int virtio_nused(struct virtqueue *vq); 245 int virtio_postpone_intr(struct virtqueue *vq, uint16_t nslots); 246 int virtio_postpone_intr_smart(struct virtqueue *vq); 247 int virtio_postpone_intr_far(struct virtqueue *vq); 248 249 #endif /* _DEV_PV_VIRTIOVAR_H_ */ 250