1bf21cd93STycho Nightingale /*- 24c87aefeSPatrick Mooney * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 34c87aefeSPatrick Mooney * 4bf21cd93STycho Nightingale * Copyright (c) 2013 Chris Torek <torek @ torek net> 5bf21cd93STycho Nightingale * All rights reserved. 6bf21cd93STycho Nightingale * 7bf21cd93STycho Nightingale * Redistribution and use in source and binary forms, with or without 8bf21cd93STycho Nightingale * modification, are permitted provided that the following conditions 9bf21cd93STycho Nightingale * are met: 10bf21cd93STycho Nightingale * 1. Redistributions of source code must retain the above copyright 11bf21cd93STycho Nightingale * notice, this list of conditions and the following disclaimer. 12bf21cd93STycho Nightingale * 2. Redistributions in binary form must reproduce the above copyright 13bf21cd93STycho Nightingale * notice, this list of conditions and the following disclaimer in the 14bf21cd93STycho Nightingale * documentation and/or other materials provided with the distribution. 15bf21cd93STycho Nightingale * 16bf21cd93STycho Nightingale * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17bf21cd93STycho Nightingale * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18bf21cd93STycho Nightingale * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19bf21cd93STycho Nightingale * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20bf21cd93STycho Nightingale * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21bf21cd93STycho Nightingale * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22bf21cd93STycho Nightingale * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23bf21cd93STycho Nightingale * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24bf21cd93STycho Nightingale * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25bf21cd93STycho Nightingale * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26bf21cd93STycho Nightingale * SUCH DAMAGE. 27bf21cd93STycho Nightingale * 284c87aefeSPatrick Mooney * $FreeBSD$ 29bf21cd93STycho Nightingale */ 30bf21cd93STycho Nightingale 312b948146SAndy Fiddaman #ifndef _BHYVE_VIRTIO_H_ 322b948146SAndy Fiddaman #define _BHYVE_VIRTIO_H_ 33bf21cd93STycho Nightingale 344c87aefeSPatrick Mooney #include <pthread_np.h> 3584659b24SMichael Zeller #include <machine/atomic.h> 364c87aefeSPatrick Mooney 372b948146SAndy Fiddaman #include <dev/virtio/virtio.h> 382b948146SAndy Fiddaman #ifdef __FreeBSD__ 392b948146SAndy Fiddaman #include <dev/virtio/virtio_ring.h> 402b948146SAndy Fiddaman #include <dev/virtio/pci/virtio_pci_var.h> 412b948146SAndy Fiddaman #endif 422b948146SAndy Fiddaman 43bf21cd93STycho Nightingale /* 44bf21cd93STycho Nightingale * These are derived from several virtio specifications. 45bf21cd93STycho Nightingale * 46bf21cd93STycho Nightingale * Some useful links: 47bf21cd93STycho Nightingale * https://github.com/rustyrussell/virtio-spec 48bf21cd93STycho Nightingale * http://people.redhat.com/pbonzini/virtio-spec.pdf 49bf21cd93STycho Nightingale */ 50bf21cd93STycho Nightingale 51bf21cd93STycho Nightingale /* 52bf21cd93STycho Nightingale * A virtual device has zero or more "virtual queues" (virtqueue). 53bf21cd93STycho Nightingale * Each virtqueue uses at least two 4096-byte pages, laid out thus: 54bf21cd93STycho Nightingale * 55bf21cd93STycho Nightingale * +-----------------------------------------------+ 56bf21cd93STycho Nightingale * | "desc": <N> descriptors, 16 bytes each | 57bf21cd93STycho Nightingale * | ----------------------------------------- | 58bf21cd93STycho Nightingale * | "avail": 2 uint16; <N> uint16; 1 uint16 | 59bf21cd93STycho Nightingale * | ----------------------------------------- | 60bf21cd93STycho Nightingale * | pad to 4k boundary | 61bf21cd93STycho Nightingale * +-----------------------------------------------+ 62bf21cd93STycho Nightingale * | "used": 2 x uint16; <N> elems; 1 uint16 | 63bf21cd93STycho Nightingale * | ----------------------------------------- | 64bf21cd93STycho Nightingale * | pad to 4k boundary | 65bf21cd93STycho Nightingale * +-----------------------------------------------+ 66bf21cd93STycho Nightingale * 67bf21cd93STycho Nightingale * The number <N> that appears here is always a power of two and is 68bf21cd93STycho Nightingale * limited to no more than 32768 (as it must fit in a 16-bit field). 69bf21cd93STycho Nightingale * If <N> is sufficiently large, the above will occupy more than 70bf21cd93STycho Nightingale * two pages. In any case, all pages must be physically contiguous 71bf21cd93STycho Nightingale * within the guest's physical address space. 72bf21cd93STycho Nightingale * 73bf21cd93STycho Nightingale * The <N> 16-byte "desc" descriptors consist of a 64-bit guest 74bf21cd93STycho Nightingale * physical address <addr>, a 32-bit length <len>, a 16-bit 75bf21cd93STycho Nightingale * <flags>, and a 16-bit <next> field (all in guest byte order). 76bf21cd93STycho Nightingale * 77bf21cd93STycho Nightingale * There are three flags that may be set : 78bf21cd93STycho Nightingale * NEXT descriptor is chained, so use its "next" field 79bf21cd93STycho Nightingale * WRITE descriptor is for host to write into guest RAM 80bf21cd93STycho Nightingale * (else host is to read from guest RAM) 81bf21cd93STycho Nightingale * INDIRECT descriptor address field is (guest physical) 82bf21cd93STycho Nightingale * address of a linear array of descriptors 83bf21cd93STycho Nightingale * 84bf21cd93STycho Nightingale * Unless INDIRECT is set, <len> is the number of bytes that may 85bf21cd93STycho Nightingale * be read/written from guest physical address <addr>. If 86bf21cd93STycho Nightingale * INDIRECT is set, WRITE is ignored and <len> provides the length 87bf21cd93STycho Nightingale * of the indirect descriptors (and <len> must be a multiple of 88bf21cd93STycho Nightingale * 16). Note that NEXT may still be set in the main descriptor 89bf21cd93STycho Nightingale * pointing to the indirect, and should be set in each indirect 90bf21cd93STycho Nightingale * descriptor that uses the next descriptor (these should generally 91bf21cd93STycho Nightingale * be numbered sequentially). However, INDIRECT must not be set 92bf21cd93STycho Nightingale * in the indirect descriptors. Upon reaching an indirect descriptor 93bf21cd93STycho Nightingale * without a NEXT bit, control returns to the direct descriptors. 94bf21cd93STycho Nightingale * 95bf21cd93STycho Nightingale * Except inside an indirect, each <next> value must be in the 96bf21cd93STycho Nightingale * range [0 .. N) (i.e., the half-open interval). (Inside an 97bf21cd93STycho Nightingale * indirect, each <next> must be in the range [0 .. <len>/16).) 98bf21cd93STycho Nightingale * 99bf21cd93STycho Nightingale * The "avail" data structures reside in the same pages as the 100bf21cd93STycho Nightingale * "desc" structures since both together are used by the device to 101bf21cd93STycho Nightingale * pass information to the hypervisor's virtual driver. These 102bf21cd93STycho Nightingale * begin with a 16-bit <flags> field and 16-bit index <idx>, then 103bf21cd93STycho Nightingale * have <N> 16-bit <ring> values, followed by one final 16-bit 104bf21cd93STycho Nightingale * field <used_event>. The <N> <ring> entries are simply indices 105bf21cd93STycho Nightingale * indices into the descriptor ring (and thus must meet the same 106bf21cd93STycho Nightingale * constraints as each <next> value). However, <idx> is counted 107bf21cd93STycho Nightingale * up from 0 (initially) and simply wraps around after 65535; it 108bf21cd93STycho Nightingale * is taken mod <N> to find the next available entry. 109bf21cd93STycho Nightingale * 110bf21cd93STycho Nightingale * The "used" ring occupies a separate page or pages, and contains 111bf21cd93STycho Nightingale * values written from the virtual driver back to the guest OS. 112bf21cd93STycho Nightingale * This begins with a 16-bit <flags> and 16-bit <idx>, then there 113bf21cd93STycho Nightingale * are <N> "vring_used" elements, followed by a 16-bit <avail_event>. 114bf21cd93STycho Nightingale * The <N> "vring_used" elements consist of a 32-bit <id> and a 115bf21cd93STycho Nightingale * 32-bit <len> (vu_tlen below). The <id> is simply the index of 116bf21cd93STycho Nightingale * the head of a descriptor chain the guest made available 117bf21cd93STycho Nightingale * earlier, and the <len> is the number of bytes actually written, 118bf21cd93STycho Nightingale * e.g., in the case of a network driver that provided a large 119bf21cd93STycho Nightingale * receive buffer but received only a small amount of data. 120bf21cd93STycho Nightingale * 121bf21cd93STycho Nightingale * The two event fields, <used_event> and <avail_event>, in the 122bf21cd93STycho Nightingale * avail and used rings (respectively -- note the reversal!), are 123bf21cd93STycho Nightingale * always provided, but are used only if the virtual device 124bf21cd93STycho Nightingale * negotiates the VIRTIO_RING_F_EVENT_IDX feature during feature 125bf21cd93STycho Nightingale * negotiation. Similarly, both rings provide a flag -- 126bf21cd93STycho Nightingale * VRING_AVAIL_F_NO_INTERRUPT and VRING_USED_F_NO_NOTIFY -- in 127bf21cd93STycho Nightingale * their <flags> field, indicating that the guest does not need an 128bf21cd93STycho Nightingale * interrupt, or that the hypervisor driver does not need a 129bf21cd93STycho Nightingale * notify, when descriptors are added to the corresponding ring. 130bf21cd93STycho Nightingale * (These are provided only for interrupt optimization and need 131bf21cd93STycho Nightingale * not be implemented.) 132bf21cd93STycho Nightingale */ 133bf21cd93STycho Nightingale #define VRING_ALIGN 4096 134bf21cd93STycho Nightingale 135bf21cd93STycho Nightingale /* 136bf21cd93STycho Nightingale * The address of any given virtual queue is determined by a single 137bf21cd93STycho Nightingale * Page Frame Number register. The guest writes the PFN into the 138bf21cd93STycho Nightingale * PCI config space. However, a device that has two or more 139bf21cd93STycho Nightingale * virtqueues can have a different PFN, and size, for each queue. 140bf21cd93STycho Nightingale * The number of queues is determinable via the PCI config space 141bf21cd93STycho Nightingale * VTCFG_R_QSEL register. Writes to QSEL select the queue: 0 means 142bf21cd93STycho Nightingale * queue #0, 1 means queue#1, etc. Once a queue is selected, the 143bf21cd93STycho Nightingale * remaining PFN and QNUM registers refer to that queue. 144bf21cd93STycho Nightingale * 145bf21cd93STycho Nightingale * QNUM is a read-only register containing a nonzero power of two 146bf21cd93STycho Nightingale * that indicates the (hypervisor's) queue size. Or, if reading it 147bf21cd93STycho Nightingale * produces zero, the hypervisor does not have a corresponding 148bf21cd93STycho Nightingale * queue. (The number of possible queues depends on the virtual 149bf21cd93STycho Nightingale * device. The block device has just one; the network device 150bf21cd93STycho Nightingale * provides either two -- 0 = receive, 1 = transmit -- or three, 151bf21cd93STycho Nightingale * with 2 = control.) 152bf21cd93STycho Nightingale * 153bf21cd93STycho Nightingale * PFN is a read/write register giving the physical page address of 154bf21cd93STycho Nightingale * the virtqueue in guest memory (the guest must allocate enough space 155bf21cd93STycho Nightingale * based on the hypervisor's provided QNUM). 156bf21cd93STycho Nightingale * 157bf21cd93STycho Nightingale * QNOTIFY is effectively write-only: when the guest writes a queue 158bf21cd93STycho Nightingale * number to the register, the hypervisor should scan the specified 159bf21cd93STycho Nightingale * virtqueue. (Reading QNOTIFY currently always gets 0). 160bf21cd93STycho Nightingale */ 161bf21cd93STycho Nightingale 162bf21cd93STycho Nightingale /* 163bf21cd93STycho Nightingale * PFN register shift amount 164bf21cd93STycho Nightingale */ 165bf21cd93STycho Nightingale #define VRING_PFN 12 166bf21cd93STycho Nightingale 167bf21cd93STycho Nightingale /* 168bf21cd93STycho Nightingale * PCI vendor/device IDs 169bf21cd93STycho Nightingale */ 170bf21cd93STycho Nightingale #define VIRTIO_VENDOR 0x1AF4 171bf21cd93STycho Nightingale #define VIRTIO_DEV_NET 0x1000 172bf21cd93STycho Nightingale #define VIRTIO_DEV_BLOCK 0x1001 1734c87aefeSPatrick Mooney #define VIRTIO_DEV_CONSOLE 0x1003 174d7b72f7bSAndy Fiddaman #define VIRTIO_DEV_SCSI 0x1004 1754c87aefeSPatrick Mooney #define VIRTIO_DEV_RANDOM 0x1005 1762b948146SAndy Fiddaman #define VIRTIO_DEV_9P 0x1009 177b0de25cbSAndy Fiddaman #define VIRTIO_DEV_INPUT 0x1052 178b0de25cbSAndy Fiddaman 179b0de25cbSAndy Fiddaman /* 180b0de25cbSAndy Fiddaman * PCI revision IDs 181b0de25cbSAndy Fiddaman */ 182b0de25cbSAndy Fiddaman #define VIRTIO_REV_INPUT 1 183b0de25cbSAndy Fiddaman 184b0de25cbSAndy Fiddaman /* 185b0de25cbSAndy Fiddaman * PCI subvendor IDs 186b0de25cbSAndy Fiddaman */ 187b0de25cbSAndy Fiddaman #define VIRTIO_SUBVEN_INPUT 0x108E 188b0de25cbSAndy Fiddaman 189b0de25cbSAndy Fiddaman /* 190b0de25cbSAndy Fiddaman * PCI subdevice IDs 191b0de25cbSAndy Fiddaman */ 192b0de25cbSAndy Fiddaman #define VIRTIO_SUBDEV_INPUT 0x1100 193bf21cd93STycho Nightingale 194bf21cd93STycho Nightingale /* From section 2.3, "Virtqueue Configuration", of the virtio specification */ 1952b948146SAndy Fiddaman static inline int 1962b948146SAndy Fiddaman vring_size_aligned(u_int qsz) 197bf21cd93STycho Nightingale { 1982b948146SAndy Fiddaman return (roundup2(vring_size(qsz, VRING_ALIGN), VRING_ALIGN)); 199bf21cd93STycho Nightingale } 200bf21cd93STycho Nightingale 201bf21cd93STycho Nightingale struct vmctx; 202bf21cd93STycho Nightingale struct pci_devinst; 203bf21cd93STycho Nightingale struct vqueue_info; 204bf21cd93STycho Nightingale 205bf21cd93STycho Nightingale /* 206bf21cd93STycho Nightingale * A virtual device, with some number (possibly 0) of virtual 207bf21cd93STycho Nightingale * queues and some size (possibly 0) of configuration-space 208bf21cd93STycho Nightingale * registers private to the device. The virtio_softc should come 209bf21cd93STycho Nightingale * at the front of each "derived class", so that a pointer to the 210bf21cd93STycho Nightingale * virtio_softc is also a pointer to the more specific, derived- 211bf21cd93STycho Nightingale * from-virtio driver's softc. 212bf21cd93STycho Nightingale * 213bf21cd93STycho Nightingale * Note: inside each hypervisor virtio driver, changes to these 214bf21cd93STycho Nightingale * data structures must be locked against other threads, if any. 215bf21cd93STycho Nightingale * Except for PCI config space register read/write, we assume each 216bf21cd93STycho Nightingale * driver does the required locking, but we need a pointer to the 217bf21cd93STycho Nightingale * lock (if there is one) for PCI config space read/write ops. 218bf21cd93STycho Nightingale * 219bf21cd93STycho Nightingale * When the guest reads or writes the device's config space, the 220bf21cd93STycho Nightingale * generic layer checks for operations on the special registers 221bf21cd93STycho Nightingale * described above. If the offset of the register(s) being read 222bf21cd93STycho Nightingale * or written is past the CFG area (CFG0 or CFG1), the request is 223bf21cd93STycho Nightingale * passed on to the virtual device, after subtracting off the 224bf21cd93STycho Nightingale * generic-layer size. (So, drivers can just use the offset as 225bf21cd93STycho Nightingale * an offset into "struct config", for instance.) 226bf21cd93STycho Nightingale * 227bf21cd93STycho Nightingale * (The virtio layer also makes sure that the read or write is to/ 228bf21cd93STycho Nightingale * from a "good" config offset, hence vc_cfgsize, and on BAR #0. 229bf21cd93STycho Nightingale * However, the driver must verify the read or write size and offset 230bf21cd93STycho Nightingale * and that no one is writing a readonly register.) 231bf21cd93STycho Nightingale * 232bf21cd93STycho Nightingale * The BROKED flag ("this thing done gone and broked") is for future 233bf21cd93STycho Nightingale * use. 234bf21cd93STycho Nightingale */ 235bf21cd93STycho Nightingale #define VIRTIO_USE_MSIX 0x01 236bf21cd93STycho Nightingale #define VIRTIO_EVENT_IDX 0x02 /* use the event-index values */ 237bf21cd93STycho Nightingale #define VIRTIO_BROKED 0x08 /* ??? */ 238bf21cd93STycho Nightingale 239bf21cd93STycho Nightingale struct virtio_softc { 240bf21cd93STycho Nightingale struct virtio_consts *vs_vc; /* constants (see below) */ 241bf21cd93STycho Nightingale int vs_flags; /* VIRTIO_* flags from above */ 242bf21cd93STycho Nightingale pthread_mutex_t *vs_mtx; /* POSIX mutex, if any */ 243bf21cd93STycho Nightingale struct pci_devinst *vs_pi; /* PCI device instance */ 244bf21cd93STycho Nightingale uint32_t vs_negotiated_caps; /* negotiated capabilities */ 245bf21cd93STycho Nightingale struct vqueue_info *vs_queues; /* one per vc_nvq */ 246bf21cd93STycho Nightingale int vs_curq; /* current queue */ 247bf21cd93STycho Nightingale uint8_t vs_status; /* value from last status write */ 248bf21cd93STycho Nightingale uint8_t vs_isr; /* ISR flags, if not MSI-X */ 249bf21cd93STycho Nightingale uint16_t vs_msix_cfg_idx; /* MSI-X vector for config event */ 250bf21cd93STycho Nightingale }; 251bf21cd93STycho Nightingale 252bf21cd93STycho Nightingale #define VS_LOCK(vs) \ 253bf21cd93STycho Nightingale do { \ 254bf21cd93STycho Nightingale if (vs->vs_mtx) \ 255bf21cd93STycho Nightingale pthread_mutex_lock(vs->vs_mtx); \ 256bf21cd93STycho Nightingale } while (0) 257bf21cd93STycho Nightingale 258bf21cd93STycho Nightingale #define VS_UNLOCK(vs) \ 259bf21cd93STycho Nightingale do { \ 260bf21cd93STycho Nightingale if (vs->vs_mtx) \ 261bf21cd93STycho Nightingale pthread_mutex_unlock(vs->vs_mtx); \ 262bf21cd93STycho Nightingale } while (0) 263bf21cd93STycho Nightingale 264bf21cd93STycho Nightingale struct virtio_consts { 265bf21cd93STycho Nightingale const char *vc_name; /* name of driver (for diagnostics) */ 266bf21cd93STycho Nightingale int vc_nvq; /* number of virtual queues */ 267bf21cd93STycho Nightingale size_t vc_cfgsize; /* size of dev-specific config regs */ 268bf21cd93STycho Nightingale void (*vc_reset)(void *); /* called on virtual device reset */ 269bf21cd93STycho Nightingale void (*vc_qnotify)(void *, struct vqueue_info *); 270bf21cd93STycho Nightingale /* called on QNOTIFY if no VQ notify */ 271bf21cd93STycho Nightingale int (*vc_cfgread)(void *, int, int, uint32_t *); 272bf21cd93STycho Nightingale /* called to read config regs */ 273bf21cd93STycho Nightingale int (*vc_cfgwrite)(void *, int, int, uint32_t); 274bf21cd93STycho Nightingale /* called to write config regs */ 2754c87aefeSPatrick Mooney void (*vc_apply_features)(void *, uint64_t); 2764c87aefeSPatrick Mooney /* called to apply negotiated features */ 277bf21cd93STycho Nightingale uint64_t vc_hv_caps; /* hypervisor-provided capabilities */ 278bf21cd93STycho Nightingale }; 279bf21cd93STycho Nightingale 280bf21cd93STycho Nightingale /* 281bf21cd93STycho Nightingale * Data structure allocated (statically) per virtual queue. 282bf21cd93STycho Nightingale * 283bf21cd93STycho Nightingale * Drivers may change vq_qsize after a reset. When the guest OS 284bf21cd93STycho Nightingale * requests a device reset, the hypervisor first calls 285bf21cd93STycho Nightingale * vs->vs_vc->vc_reset(); then the data structure below is 286bf21cd93STycho Nightingale * reinitialized (for each virtqueue: vs->vs_vc->vc_nvq). 287bf21cd93STycho Nightingale * 288bf21cd93STycho Nightingale * The remaining fields should only be fussed-with by the generic 289bf21cd93STycho Nightingale * code. 290bf21cd93STycho Nightingale * 291bf21cd93STycho Nightingale * Note: the addresses of vq_desc, vq_avail, and vq_used are all 292bf21cd93STycho Nightingale * computable from each other, but it's a lot simpler if we just 293bf21cd93STycho Nightingale * keep a pointer to each one. The event indices are similarly 294bf21cd93STycho Nightingale * (but more easily) computable, and this time we'll compute them: 295bf21cd93STycho Nightingale * they're just XX_ring[N]. 296bf21cd93STycho Nightingale */ 297bf21cd93STycho Nightingale #define VQ_ALLOC 0x01 /* set once we have a pfn */ 298bf21cd93STycho Nightingale #define VQ_BROKED 0x02 /* ??? */ 299bf21cd93STycho Nightingale struct vqueue_info { 300bf21cd93STycho Nightingale uint16_t vq_qsize; /* size of this queue (a power of 2) */ 301bf21cd93STycho Nightingale void (*vq_notify)(void *, struct vqueue_info *); 302bf21cd93STycho Nightingale /* called instead of vc_notify, if not NULL */ 303bf21cd93STycho Nightingale 304bf21cd93STycho Nightingale struct virtio_softc *vq_vs; /* backpointer to softc */ 305bf21cd93STycho Nightingale uint16_t vq_num; /* we're the num'th queue in the softc */ 306bf21cd93STycho Nightingale 307bf21cd93STycho Nightingale uint16_t vq_flags; /* flags (see above) */ 3082b948146SAndy Fiddaman uint16_t vq_last_avail; /* a recent value of vq_avail->idx */ 309154972afSPatrick Mooney uint16_t vq_next_used; /* index of the next used slot to be filled */ 3102b948146SAndy Fiddaman uint16_t vq_save_used; /* saved vq_used->idx; see vq_endchains */ 311bf21cd93STycho Nightingale uint16_t vq_msix_idx; /* MSI-X index, or VIRTIO_MSI_NO_VECTOR */ 312bf21cd93STycho Nightingale 313bf21cd93STycho Nightingale uint32_t vq_pfn; /* PFN of virt queue (not shifted!) */ 314bf21cd93STycho Nightingale 31559d65d31SAndy Fiddaman struct vring_desc *vq_desc; /* descriptor array */ 31659d65d31SAndy Fiddaman struct vring_avail *vq_avail; /* the "avail" ring */ 31759d65d31SAndy Fiddaman struct vring_used *vq_used; /* the "used" ring */ 318bf21cd93STycho Nightingale 319bf21cd93STycho Nightingale }; 320bf21cd93STycho Nightingale /* as noted above, these are sort of backwards, name-wise */ 321bf21cd93STycho Nightingale #define VQ_AVAIL_EVENT_IDX(vq) \ 32259d65d31SAndy Fiddaman (*(uint16_t *)&(vq)->vq_used->ring[(vq)->vq_qsize]) 323bf21cd93STycho Nightingale #define VQ_USED_EVENT_IDX(vq) \ 3242b948146SAndy Fiddaman ((vq)->vq_avail->ring[(vq)->vq_qsize]) 325bf21cd93STycho Nightingale 326bf21cd93STycho Nightingale /* 327bf21cd93STycho Nightingale * Is this ring ready for I/O? 328bf21cd93STycho Nightingale */ 329bf21cd93STycho Nightingale static inline int 330bf21cd93STycho Nightingale vq_ring_ready(struct vqueue_info *vq) 331bf21cd93STycho Nightingale { 332bf21cd93STycho Nightingale 333bf21cd93STycho Nightingale return (vq->vq_flags & VQ_ALLOC); 334bf21cd93STycho Nightingale } 335bf21cd93STycho Nightingale 336bf21cd93STycho Nightingale /* 337bf21cd93STycho Nightingale * Are there "available" descriptors? (This does not count 338bf21cd93STycho Nightingale * how many, just returns True if there are some.) 339bf21cd93STycho Nightingale */ 340bf21cd93STycho Nightingale static inline int 341bf21cd93STycho Nightingale vq_has_descs(struct vqueue_info *vq) 342bf21cd93STycho Nightingale { 343bf21cd93STycho Nightingale 344bf21cd93STycho Nightingale return (vq_ring_ready(vq) && vq->vq_last_avail != 3452b948146SAndy Fiddaman vq->vq_avail->idx); 346bf21cd93STycho Nightingale } 347bf21cd93STycho Nightingale 348bf21cd93STycho Nightingale /* 349b0de25cbSAndy Fiddaman * Deliver an interrupt to the guest for a specific MSI-X queue or 350b0de25cbSAndy Fiddaman * event. 351bf21cd93STycho Nightingale */ 352bf21cd93STycho Nightingale static inline void 353b0de25cbSAndy Fiddaman vi_interrupt(struct virtio_softc *vs, uint8_t isr, uint16_t msix_idx) 354bf21cd93STycho Nightingale { 355bf21cd93STycho Nightingale 356bf21cd93STycho Nightingale if (pci_msix_enabled(vs->vs_pi)) 357b0de25cbSAndy Fiddaman pci_generate_msix(vs->vs_pi, msix_idx); 358bf21cd93STycho Nightingale else { 3594c87aefeSPatrick Mooney #ifndef __FreeBSD__ 3604c87aefeSPatrick Mooney boolean_t unlock = B_FALSE; 3614c87aefeSPatrick Mooney 3624c87aefeSPatrick Mooney if (vs->vs_mtx && !pthread_mutex_isowned_np(vs->vs_mtx)) { 3634c87aefeSPatrick Mooney unlock = B_TRUE; 3644c87aefeSPatrick Mooney pthread_mutex_lock(vs->vs_mtx); 3654c87aefeSPatrick Mooney } 3664c87aefeSPatrick Mooney #else 367bf21cd93STycho Nightingale VS_LOCK(vs); 3684c87aefeSPatrick Mooney #endif 369b0de25cbSAndy Fiddaman vs->vs_isr |= isr; 370bf21cd93STycho Nightingale pci_generate_msi(vs->vs_pi, 0); 371bf21cd93STycho Nightingale pci_lintr_assert(vs->vs_pi); 3724c87aefeSPatrick Mooney #ifndef __FreeBSD__ 3734c87aefeSPatrick Mooney if (unlock) 3744c87aefeSPatrick Mooney pthread_mutex_unlock(vs->vs_mtx); 3754c87aefeSPatrick Mooney #else 376bf21cd93STycho Nightingale VS_UNLOCK(vs); 3774c87aefeSPatrick Mooney #endif 378bf21cd93STycho Nightingale } 379bf21cd93STycho Nightingale } 380bf21cd93STycho Nightingale 381b0de25cbSAndy Fiddaman /* 382b0de25cbSAndy Fiddaman * Deliver an interrupt to the guest on the given virtual queue (if 383b0de25cbSAndy Fiddaman * possible, or a generic MSI interrupt if not using MSI-X). 384b0de25cbSAndy Fiddaman */ 385b0de25cbSAndy Fiddaman static inline void 386b0de25cbSAndy Fiddaman vq_interrupt(struct virtio_softc *vs, struct vqueue_info *vq) 387b0de25cbSAndy Fiddaman { 388b0de25cbSAndy Fiddaman 389b0de25cbSAndy Fiddaman vi_interrupt(vs, VIRTIO_PCI_ISR_INTR, vq->vq_msix_idx); 390b0de25cbSAndy Fiddaman } 391b0de25cbSAndy Fiddaman 39284659b24SMichael Zeller static inline void 39384659b24SMichael Zeller vq_kick_enable(struct vqueue_info *vq) 39484659b24SMichael Zeller { 39584659b24SMichael Zeller 3962b948146SAndy Fiddaman vq->vq_used->flags &= ~VRING_USED_F_NO_NOTIFY; 39784659b24SMichael Zeller /* 3982b948146SAndy Fiddaman * Full memory barrier to make sure the store to vq_used->flags 3992b948146SAndy Fiddaman * happens before the load from vq_avail->idx, which results from a 4002b948146SAndy Fiddaman * subsequent call to vq_has_descs(). 40184659b24SMichael Zeller */ 40284659b24SMichael Zeller atomic_thread_fence_seq_cst(); 40384659b24SMichael Zeller } 40484659b24SMichael Zeller 40584659b24SMichael Zeller static inline void 40684659b24SMichael Zeller vq_kick_disable(struct vqueue_info *vq) 40784659b24SMichael Zeller { 40884659b24SMichael Zeller 4092b948146SAndy Fiddaman vq->vq_used->flags |= VRING_USED_F_NO_NOTIFY; 41084659b24SMichael Zeller } 41184659b24SMichael Zeller 412bf21cd93STycho Nightingale struct iovec; 413b0de25cbSAndy Fiddaman 414b0de25cbSAndy Fiddaman /* 415b0de25cbSAndy Fiddaman * Request description returned by vq_getchain. 416b0de25cbSAndy Fiddaman * 417b0de25cbSAndy Fiddaman * Writable iovecs start at iov[req.readable]. 418b0de25cbSAndy Fiddaman */ 419b0de25cbSAndy Fiddaman struct vi_req { 420b0de25cbSAndy Fiddaman int readable; /* num of readable iovecs */ 421b0de25cbSAndy Fiddaman int writable; /* num of writable iovecs */ 422b0de25cbSAndy Fiddaman unsigned int idx; /* ring index */ 423b0de25cbSAndy Fiddaman }; 424b0de25cbSAndy Fiddaman 425bf21cd93STycho Nightingale void vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc, 426bf21cd93STycho Nightingale void *dev_softc, struct pci_devinst *pi, 427bf21cd93STycho Nightingale struct vqueue_info *queues); 428bf21cd93STycho Nightingale int vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix); 429bf21cd93STycho Nightingale void vi_reset_dev(struct virtio_softc *); 430bf21cd93STycho Nightingale void vi_set_io_bar(struct virtio_softc *, int); 431bf21cd93STycho Nightingale 432b0de25cbSAndy Fiddaman int vq_getchain(struct vqueue_info *vq, struct iovec *iov, int niov, 433b0de25cbSAndy Fiddaman struct vi_req *reqp); 434154972afSPatrick Mooney void vq_retchains(struct vqueue_info *vq, uint16_t n_chains); 435154972afSPatrick Mooney void vq_relchain_prepare(struct vqueue_info *vq, uint16_t idx, 436154972afSPatrick Mooney uint32_t iolen); 437154972afSPatrick Mooney void vq_relchain_publish(struct vqueue_info *vq); 4384c87aefeSPatrick Mooney void vq_relchain(struct vqueue_info *vq, uint16_t idx, uint32_t iolen); 439bf21cd93STycho Nightingale void vq_endchains(struct vqueue_info *vq, int used_all_avail); 440bf21cd93STycho Nightingale 44159d65d31SAndy Fiddaman uint64_t vi_pci_read(struct vmctx *ctx, struct pci_devinst *pi, 442bf21cd93STycho Nightingale int baridx, uint64_t offset, int size); 44359d65d31SAndy Fiddaman void vi_pci_write(struct vmctx *ctx, struct pci_devinst *pi, 444bf21cd93STycho Nightingale int baridx, uint64_t offset, int size, uint64_t value); 445*d4221574SAndy Fiddaman 446*d4221574SAndy Fiddaman #ifndef __FreeBSD__ 447*d4221574SAndy Fiddaman void vi_vq_init(struct virtio_softc *, uint32_t); 448*d4221574SAndy Fiddaman #endif 449*d4221574SAndy Fiddaman 4502b948146SAndy Fiddaman #endif /* _BHYVE_VIRTIO_H_ */ 451