1221828Sgrehan/*- 2253440Sgrehan * Copyright (c) 2013 Chris Torek <torek @ torek net> 3221828Sgrehan * All rights reserved. 4221828Sgrehan * 5221828Sgrehan * Redistribution and use in source and binary forms, with or without 6221828Sgrehan * modification, are permitted provided that the following conditions 7221828Sgrehan * are met: 8221828Sgrehan * 1. Redistributions of source code must retain the above copyright 9221828Sgrehan * notice, this list of conditions and the following disclaimer. 10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11221828Sgrehan * notice, this list of conditions and the following disclaimer in the 12221828Sgrehan * documentation and/or other materials provided with the distribution. 13221828Sgrehan * 14253440Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17253440Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24221828Sgrehan * SUCH DAMAGE. 25221828Sgrehan * 26221828Sgrehan * $FreeBSD: releng/11.0/usr.sbin/bhyve/virtio.h 280041 2015-03-15 16:09:39Z mav $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#ifndef _VIRTIO_H_ 30221828Sgrehan#define _VIRTIO_H_ 31221828Sgrehan 32253440Sgrehan/* 33253440Sgrehan * These are derived from several virtio specifications. 34253440Sgrehan * 35253440Sgrehan * Some useful links: 36262311Sgrehan * https://github.com/rustyrussell/virtio-spec 37253440Sgrehan * http://people.redhat.com/pbonzini/virtio-spec.pdf 38253440Sgrehan */ 39253440Sgrehan 40253440Sgrehan/* 41253440Sgrehan * A virtual device has zero or more "virtual queues" (virtqueue). 42253440Sgrehan * Each virtqueue uses at least two 4096-byte pages, laid out thus: 43253440Sgrehan * 44253440Sgrehan * +-----------------------------------------------+ 45253440Sgrehan * | "desc": <N> descriptors, 16 bytes each | 46253440Sgrehan * | ----------------------------------------- | 47253440Sgrehan * | "avail": 2 uint16; <N> uint16; 1 uint16 | 48253440Sgrehan * | ----------------------------------------- | 49253440Sgrehan * | pad to 4k boundary | 50253440Sgrehan * +-----------------------------------------------+ 51253440Sgrehan * | "used": 2 x uint16; <N> elems; 1 uint16 | 52253440Sgrehan * | ----------------------------------------- | 53253440Sgrehan * | pad to 4k boundary | 54253440Sgrehan * +-----------------------------------------------+ 55253440Sgrehan * 56253440Sgrehan * The number <N> that appears here is always a power of two and is 57253440Sgrehan * limited to no more than 32768 (as it must fit in a 16-bit field). 58253440Sgrehan * If <N> is sufficiently large, the above will occupy more than 59253440Sgrehan * two pages. In any case, all pages must be physically contiguous 60253440Sgrehan * within the guest's physical address space. 61253440Sgrehan * 62253440Sgrehan * The <N> 16-byte "desc" descriptors consist of a 64-bit guest 63253440Sgrehan * physical address <addr>, a 32-bit length <len>, a 16-bit 64253440Sgrehan * <flags>, and a 16-bit <next> field (all in guest byte order). 65253440Sgrehan * 66253440Sgrehan * There are three flags that may be set : 67253440Sgrehan * NEXT descriptor is chained, so use its "next" field 68253440Sgrehan * WRITE descriptor is for host to write into guest RAM 69253440Sgrehan * (else host is to read from guest RAM) 70253440Sgrehan * INDIRECT descriptor address field is (guest physical) 71253440Sgrehan * address of a linear array of descriptors 72253440Sgrehan * 73253440Sgrehan * Unless INDIRECT is set, <len> is the number of bytes that may 74253440Sgrehan * be read/written from guest physical address <addr>. If 75253440Sgrehan * INDIRECT is set, WRITE is ignored and <len> provides the length 76253440Sgrehan * of the indirect descriptors (and <len> must be a multiple of 77253440Sgrehan * 16). Note that NEXT may still be set in the main descriptor 78253440Sgrehan * pointing to the indirect, and should be set in each indirect 79253440Sgrehan * descriptor that uses the next descriptor (these should generally 80253440Sgrehan * be numbered sequentially). However, INDIRECT must not be set 81253440Sgrehan * in the indirect descriptors. Upon reaching an indirect descriptor 82253440Sgrehan * without a NEXT bit, control returns to the direct descriptors. 83253440Sgrehan * 84253440Sgrehan * Except inside an indirect, each <next> value must be in the 85253440Sgrehan * range [0 .. N) (i.e., the half-open interval). (Inside an 86253440Sgrehan * indirect, each <next> must be in the range [0 .. <len>/16).) 87253440Sgrehan * 88253440Sgrehan * The "avail" data structures reside in the same pages as the 89253440Sgrehan * "desc" structures since both together are used by the device to 90253440Sgrehan * pass information to the hypervisor's virtual driver. These 91253440Sgrehan * begin with a 16-bit <flags> field and 16-bit index <idx>, then 92253440Sgrehan * have <N> 16-bit <ring> values, followed by one final 16-bit 93253440Sgrehan * field <used_event>. The <N> <ring> entries are simply indices 94253440Sgrehan * indices into the descriptor ring (and thus must meet the same 95253440Sgrehan * constraints as each <next> value). However, <idx> is counted 96253440Sgrehan * up from 0 (initially) and simply wraps around after 65535; it 97253440Sgrehan * is taken mod <N> to find the next available entry. 98253440Sgrehan * 99253440Sgrehan * The "used" ring occupies a separate page or pages, and contains 100253440Sgrehan * values written from the virtual driver back to the guest OS. 101253440Sgrehan * This begins with a 16-bit <flags> and 16-bit <idx>, then there 102253440Sgrehan * are <N> "vring_used" elements, followed by a 16-bit <avail_event>. 103253440Sgrehan * The <N> "vring_used" elements consist of a 32-bit <id> and a 104253440Sgrehan * 32-bit <len> (vu_tlen below). The <id> is simply the index of 105253440Sgrehan * the head of a descriptor chain the guest made available 106253440Sgrehan * earlier, and the <len> is the number of bytes actually written, 107253440Sgrehan * e.g., in the case of a network driver that provided a large 108253440Sgrehan * receive buffer but received only a small amount of data. 109253440Sgrehan * 110253440Sgrehan * The two event fields, <used_event> and <avail_event>, in the 111253440Sgrehan * avail and used rings (respectively -- note the reversal!), are 112253440Sgrehan * always provided, but are used only if the virtual device 113253440Sgrehan * negotiates the VIRTIO_RING_F_EVENT_IDX feature during feature 114253440Sgrehan * negotiation. Similarly, both rings provide a flag -- 115253440Sgrehan * VRING_AVAIL_F_NO_INTERRUPT and VRING_USED_F_NO_NOTIFY -- in 116253440Sgrehan * their <flags> field, indicating that the guest does not need an 117253440Sgrehan * interrupt, or that the hypervisor driver does not need a 118253440Sgrehan * notify, when descriptors are added to the corresponding ring. 119253440Sgrehan * (These are provided only for interrupt optimization and need 120253440Sgrehan * not be implemented.) 121253440Sgrehan */ 122221828Sgrehan#define VRING_ALIGN 4096 123221828Sgrehan 124221828Sgrehan#define VRING_DESC_F_NEXT (1 << 0) 125221828Sgrehan#define VRING_DESC_F_WRITE (1 << 1) 126221828Sgrehan#define VRING_DESC_F_INDIRECT (1 << 2) 127221828Sgrehan 128253440Sgrehanstruct virtio_desc { /* AKA vring_desc */ 129253440Sgrehan uint64_t vd_addr; /* guest physical address */ 130253440Sgrehan uint32_t vd_len; /* length of scatter/gather seg */ 131253440Sgrehan uint16_t vd_flags; /* VRING_F_DESC_* */ 132253440Sgrehan uint16_t vd_next; /* next desc if F_NEXT */ 133253440Sgrehan} __packed; 134253440Sgrehan 135253440Sgrehanstruct virtio_used { /* AKA vring_used_elem */ 136253440Sgrehan uint32_t vu_idx; /* head of used descriptor chain */ 137253440Sgrehan uint32_t vu_tlen; /* length written-to */ 138253440Sgrehan} __packed; 139253440Sgrehan 140221828Sgrehan#define VRING_AVAIL_F_NO_INTERRUPT 1 141221828Sgrehan 142253440Sgrehanstruct vring_avail { 143253440Sgrehan uint16_t va_flags; /* VRING_AVAIL_F_* */ 144253440Sgrehan uint16_t va_idx; /* counts to 65535, then cycles */ 145253440Sgrehan uint16_t va_ring[]; /* size N, reported in QNUM value */ 146253440Sgrehan/* uint16_t va_used_event; -- after N ring entries */ 147221828Sgrehan} __packed; 148221828Sgrehan 149253440Sgrehan#define VRING_USED_F_NO_NOTIFY 1 150253440Sgrehanstruct vring_used { 151253440Sgrehan uint16_t vu_flags; /* VRING_USED_F_* */ 152253440Sgrehan uint16_t vu_idx; /* counts to 65535, then cycles */ 153253440Sgrehan struct virtio_used vu_ring[]; /* size N */ 154253440Sgrehan/* uint16_t vu_avail_event; -- after N ring entries */ 155221828Sgrehan} __packed; 156221828Sgrehan 157221828Sgrehan/* 158253440Sgrehan * The address of any given virtual queue is determined by a single 159253440Sgrehan * Page Frame Number register. The guest writes the PFN into the 160253440Sgrehan * PCI config space. However, a device that has two or more 161253440Sgrehan * virtqueues can have a different PFN, and size, for each queue. 162253440Sgrehan * The number of queues is determinable via the PCI config space 163253440Sgrehan * VTCFG_R_QSEL register. Writes to QSEL select the queue: 0 means 164253440Sgrehan * queue #0, 1 means queue#1, etc. Once a queue is selected, the 165253440Sgrehan * remaining PFN and QNUM registers refer to that queue. 166253440Sgrehan * 167253440Sgrehan * QNUM is a read-only register containing a nonzero power of two 168253440Sgrehan * that indicates the (hypervisor's) queue size. Or, if reading it 169253440Sgrehan * produces zero, the hypervisor does not have a corresponding 170253440Sgrehan * queue. (The number of possible queues depends on the virtual 171253440Sgrehan * device. The block device has just one; the network device 172253440Sgrehan * provides either two -- 0 = receive, 1 = transmit -- or three, 173253440Sgrehan * with 2 = control.) 174253440Sgrehan * 175253440Sgrehan * PFN is a read/write register giving the physical page address of 176253440Sgrehan * the virtqueue in guest memory (the guest must allocate enough space 177253440Sgrehan * based on the hypervisor's provided QNUM). 178253440Sgrehan * 179253440Sgrehan * QNOTIFY is effectively write-only: when the guest writes a queue 180253440Sgrehan * number to the register, the hypervisor should scan the specified 181253440Sgrehan * virtqueue. (Reading QNOTIFY currently always gets 0). 182253440Sgrehan */ 183253440Sgrehan 184253440Sgrehan/* 185221828Sgrehan * PFN register shift amount 186221828Sgrehan */ 187253440Sgrehan#define VRING_PFN 12 188221828Sgrehan 189221828Sgrehan/* 190221828Sgrehan * Virtio device types 191253440Sgrehan * 192253440Sgrehan * XXX Should really be merged with <dev/virtio/virtio.h> defines 193221828Sgrehan */ 194253440Sgrehan#define VIRTIO_TYPE_NET 1 195253440Sgrehan#define VIRTIO_TYPE_BLOCK 2 196253440Sgrehan#define VIRTIO_TYPE_CONSOLE 3 197253440Sgrehan#define VIRTIO_TYPE_ENTROPY 4 198253440Sgrehan#define VIRTIO_TYPE_BALLOON 5 199253440Sgrehan#define VIRTIO_TYPE_IOMEMORY 6 200253440Sgrehan#define VIRTIO_TYPE_RPMSG 7 201253440Sgrehan#define VIRTIO_TYPE_SCSI 8 202253440Sgrehan#define VIRTIO_TYPE_9P 9 203221828Sgrehan 204253440Sgrehan/* experimental IDs start at 65535 and work down */ 205253440Sgrehan 206221828Sgrehan/* 207221828Sgrehan * PCI vendor/device IDs 208221828Sgrehan */ 209253440Sgrehan#define VIRTIO_VENDOR 0x1AF4 210253440Sgrehan#define VIRTIO_DEV_NET 0x1000 211253440Sgrehan#define VIRTIO_DEV_BLOCK 0x1001 212264055Sgrehan#define VIRTIO_DEV_RANDOM 0x1002 213221828Sgrehan 214221828Sgrehan/* 215253440Sgrehan * PCI config space constants. 216253440Sgrehan * 217253440Sgrehan * If MSI-X is enabled, the ISR register is generally not used, 218253440Sgrehan * and the configuration vector and queue vector appear at offsets 219253440Sgrehan * 20 and 22 with the remaining configuration registers at 24. 220253440Sgrehan * If MSI-X is not enabled, those two registers disappear and 221253440Sgrehan * the remaining configuration registers start at offset 20. 222221828Sgrehan */ 223221828Sgrehan#define VTCFG_R_HOSTCAP 0 224221828Sgrehan#define VTCFG_R_GUESTCAP 4 225221828Sgrehan#define VTCFG_R_PFN 8 226221828Sgrehan#define VTCFG_R_QNUM 12 227221828Sgrehan#define VTCFG_R_QSEL 14 228221828Sgrehan#define VTCFG_R_QNOTIFY 16 229221828Sgrehan#define VTCFG_R_STATUS 18 230221828Sgrehan#define VTCFG_R_ISR 19 231246109Sneel#define VTCFG_R_CFGVEC 20 232246109Sneel#define VTCFG_R_QVEC 22 233221828Sgrehan#define VTCFG_R_CFG0 20 /* No MSI-X */ 234221828Sgrehan#define VTCFG_R_CFG1 24 /* With MSI-X */ 235221828Sgrehan#define VTCFG_R_MSIX 20 236221828Sgrehan 237253440Sgrehan/* 238253440Sgrehan * Bits in VTCFG_R_STATUS. Guests need not actually set any of these, 239253440Sgrehan * but a guest writing 0 to this register means "please reset". 240253440Sgrehan */ 241253440Sgrehan#define VTCFG_STATUS_ACK 0x01 /* guest OS has acknowledged dev */ 242253440Sgrehan#define VTCFG_STATUS_DRIVER 0x02 /* guest OS driver is loaded */ 243253440Sgrehan#define VTCFG_STATUS_DRIVER_OK 0x04 /* guest OS driver ready */ 244253440Sgrehan#define VTCFG_STATUS_FAILED 0x80 /* guest has given up on this dev */ 245253440Sgrehan 246253440Sgrehan/* 247253440Sgrehan * Bits in VTCFG_R_ISR. These apply only if not using MSI-X. 248253440Sgrehan * 249253440Sgrehan * (We don't [yet?] ever use CONF_CHANGED.) 250253440Sgrehan */ 251253440Sgrehan#define VTCFG_ISR_QUEUES 0x01 /* re-scan queues */ 252253440Sgrehan#define VTCFG_ISR_CONF_CHANGED 0x80 /* configuration changed */ 253253440Sgrehan 254253440Sgrehan#define VIRTIO_MSI_NO_VECTOR 0xFFFF 255253440Sgrehan 256253440Sgrehan/* 257253440Sgrehan * Feature flags. 258253440Sgrehan * Note: bits 0 through 23 are reserved to each device type. 259253440Sgrehan */ 260250197Sneel#define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24) 261253440Sgrehan#define VIRTIO_RING_F_INDIRECT_DESC (1 << 28) 262253440Sgrehan#define VIRTIO_RING_F_EVENT_IDX (1 << 29) 263250197Sneel 264247523Sneel/* From section 2.3, "Virtqueue Configuration", of the virtio specification */ 265253440Sgrehanstatic inline size_t 266247523Sneelvring_size(u_int qsz) 267247523Sneel{ 268253440Sgrehan size_t size; 269247523Sneel 270253440Sgrehan /* constant 3 below = va_flags, va_idx, va_used_event */ 271247523Sneel size = sizeof(struct virtio_desc) * qsz + sizeof(uint16_t) * (3 + qsz); 272247523Sneel size = roundup2(size, VRING_ALIGN); 273247523Sneel 274253440Sgrehan /* constant 3 below = vu_flags, vu_idx, vu_avail_event */ 275247523Sneel size += sizeof(uint16_t) * 3 + sizeof(struct virtio_used) * qsz; 276247523Sneel size = roundup2(size, VRING_ALIGN); 277247523Sneel 278247523Sneel return (size); 279247523Sneel} 280247523Sneel 281253440Sgrehanstruct vmctx; 282253440Sgrehanstruct pci_devinst; 283253440Sgrehanstruct vqueue_info; 284253440Sgrehan 285253440Sgrehan/* 286253440Sgrehan * A virtual device, with some number (possibly 0) of virtual 287253440Sgrehan * queues and some size (possibly 0) of configuration-space 288253440Sgrehan * registers private to the device. The virtio_softc should come 289253440Sgrehan * at the front of each "derived class", so that a pointer to the 290253440Sgrehan * virtio_softc is also a pointer to the more specific, derived- 291253440Sgrehan * from-virtio driver's softc. 292253440Sgrehan * 293253440Sgrehan * Note: inside each hypervisor virtio driver, changes to these 294253440Sgrehan * data structures must be locked against other threads, if any. 295253440Sgrehan * Except for PCI config space register read/write, we assume each 296253440Sgrehan * driver does the required locking, but we need a pointer to the 297253440Sgrehan * lock (if there is one) for PCI config space read/write ops. 298253440Sgrehan * 299253440Sgrehan * When the guest reads or writes the device's config space, the 300253440Sgrehan * generic layer checks for operations on the special registers 301253440Sgrehan * described above. If the offset of the register(s) being read 302253440Sgrehan * or written is past the CFG area (CFG0 or CFG1), the request is 303253440Sgrehan * passed on to the virtual device, after subtracting off the 304253440Sgrehan * generic-layer size. (So, drivers can just use the offset as 305253440Sgrehan * an offset into "struct config", for instance.) 306253440Sgrehan * 307253440Sgrehan * (The virtio layer also makes sure that the read or write is to/ 308253440Sgrehan * from a "good" config offset, hence vc_cfgsize, and on BAR #0. 309253440Sgrehan * However, the driver must verify the read or write size and offset 310253440Sgrehan * and that no one is writing a readonly register.) 311253440Sgrehan * 312253440Sgrehan * The BROKED flag ("this thing done gone and broked") is for future 313253440Sgrehan * use. 314253440Sgrehan */ 315253440Sgrehan#define VIRTIO_USE_MSIX 0x01 316253440Sgrehan#define VIRTIO_EVENT_IDX 0x02 /* use the event-index values */ 317253440Sgrehan#define VIRTIO_BROKED 0x08 /* ??? */ 318253440Sgrehan 319253440Sgrehanstruct virtio_softc { 320253440Sgrehan struct virtio_consts *vs_vc; /* constants (see below) */ 321253440Sgrehan int vs_flags; /* VIRTIO_* flags from above */ 322253440Sgrehan pthread_mutex_t *vs_mtx; /* POSIX mutex, if any */ 323253440Sgrehan struct pci_devinst *vs_pi; /* PCI device instance */ 324253440Sgrehan uint32_t vs_negotiated_caps; /* negotiated capabilities */ 325253440Sgrehan struct vqueue_info *vs_queues; /* one per vc_nvq */ 326253440Sgrehan int vs_curq; /* current queue */ 327253440Sgrehan uint8_t vs_status; /* value from last status write */ 328253440Sgrehan uint8_t vs_isr; /* ISR flags, if not MSI-X */ 329253440Sgrehan uint16_t vs_msix_cfg_idx; /* MSI-X vector for config event */ 330253440Sgrehan}; 331253440Sgrehan 332261268Sjhb#define VS_LOCK(vs) \ 333261268Sjhbdo { \ 334261268Sjhb if (vs->vs_mtx) \ 335261268Sjhb pthread_mutex_lock(vs->vs_mtx); \ 336261268Sjhb} while (0) 337261268Sjhb 338261268Sjhb#define VS_UNLOCK(vs) \ 339261268Sjhbdo { \ 340261268Sjhb if (vs->vs_mtx) \ 341261268Sjhb pthread_mutex_unlock(vs->vs_mtx); \ 342261268Sjhb} while (0) 343261268Sjhb 344253440Sgrehanstruct virtio_consts { 345253440Sgrehan const char *vc_name; /* name of driver (for diagnostics) */ 346253440Sgrehan int vc_nvq; /* number of virtual queues */ 347253440Sgrehan size_t vc_cfgsize; /* size of dev-specific config regs */ 348253440Sgrehan void (*vc_reset)(void *); /* called on virtual device reset */ 349253440Sgrehan void (*vc_qnotify)(void *, struct vqueue_info *); 350253440Sgrehan /* called on QNOTIFY if no VQ notify */ 351253440Sgrehan int (*vc_cfgread)(void *, int, int, uint32_t *); 352253440Sgrehan /* called to read config regs */ 353253440Sgrehan int (*vc_cfgwrite)(void *, int, int, uint32_t); 354253440Sgrehan /* called to write config regs */ 355271299Sgrehan void (*vc_apply_features)(void *, uint64_t); 356271299Sgrehan /* called to apply negotiated features */ 357268276Sgrehan uint64_t vc_hv_caps; /* hypervisor-provided capabilities */ 358253440Sgrehan}; 359253440Sgrehan 360253440Sgrehan/* 361253440Sgrehan * Data structure allocated (statically) per virtual queue. 362253440Sgrehan * 363253440Sgrehan * Drivers may change vq_qsize after a reset. When the guest OS 364253440Sgrehan * requests a device reset, the hypervisor first calls 365253440Sgrehan * vs->vs_vc->vc_reset(); then the data structure below is 366253440Sgrehan * reinitialized (for each virtqueue: vs->vs_vc->vc_nvq). 367253440Sgrehan * 368253440Sgrehan * The remaining fields should only be fussed-with by the generic 369253440Sgrehan * code. 370253440Sgrehan * 371253440Sgrehan * Note: the addresses of vq_desc, vq_avail, and vq_used are all 372253440Sgrehan * computable from each other, but it's a lot simpler if we just 373253440Sgrehan * keep a pointer to each one. The event indices are similarly 374253440Sgrehan * (but more easily) computable, and this time we'll compute them: 375253440Sgrehan * they're just XX_ring[N]. 376253440Sgrehan */ 377253440Sgrehan#define VQ_ALLOC 0x01 /* set once we have a pfn */ 378253440Sgrehan#define VQ_BROKED 0x02 /* ??? */ 379253440Sgrehanstruct vqueue_info { 380253440Sgrehan uint16_t vq_qsize; /* size of this queue (a power of 2) */ 381253440Sgrehan void (*vq_notify)(void *, struct vqueue_info *); 382253440Sgrehan /* called instead of vc_notify, if not NULL */ 383253440Sgrehan 384253440Sgrehan struct virtio_softc *vq_vs; /* backpointer to softc */ 385253440Sgrehan uint16_t vq_num; /* we're the num'th queue in the softc */ 386253440Sgrehan 387253440Sgrehan uint16_t vq_flags; /* flags (see above) */ 388253440Sgrehan uint16_t vq_last_avail; /* a recent value of vq_avail->va_idx */ 389253440Sgrehan uint16_t vq_save_used; /* saved vq_used->vu_idx; see vq_endchains */ 390253440Sgrehan uint16_t vq_msix_idx; /* MSI-X index, or VIRTIO_MSI_NO_VECTOR */ 391253440Sgrehan 392253440Sgrehan uint32_t vq_pfn; /* PFN of virt queue (not shifted!) */ 393253440Sgrehan 394253440Sgrehan volatile struct virtio_desc *vq_desc; /* descriptor array */ 395253440Sgrehan volatile struct vring_avail *vq_avail; /* the "avail" ring */ 396253440Sgrehan volatile struct vring_used *vq_used; /* the "used" ring */ 397253440Sgrehan 398253440Sgrehan}; 399253440Sgrehan/* as noted above, these are sort of backwards, name-wise */ 400253440Sgrehan#define VQ_AVAIL_EVENT_IDX(vq) \ 401253440Sgrehan (*(volatile uint16_t *)&(vq)->vq_used->vu_ring[(vq)->vq_qsize]) 402253440Sgrehan#define VQ_USED_EVENT_IDX(vq) \ 403253440Sgrehan ((vq)->vq_avail->va_ring[(vq)->vq_qsize]) 404253440Sgrehan 405253440Sgrehan/* 406253440Sgrehan * Is this ring ready for I/O? 407253440Sgrehan */ 408253440Sgrehanstatic inline int 409253440Sgrehanvq_ring_ready(struct vqueue_info *vq) 410253440Sgrehan{ 411253440Sgrehan 412253440Sgrehan return (vq->vq_flags & VQ_ALLOC); 413253440Sgrehan} 414253440Sgrehan 415253440Sgrehan/* 416253440Sgrehan * Are there "available" descriptors? (This does not count 417253440Sgrehan * how many, just returns True if there are some.) 418253440Sgrehan */ 419253440Sgrehanstatic inline int 420253440Sgrehanvq_has_descs(struct vqueue_info *vq) 421253440Sgrehan{ 422253440Sgrehan 423253440Sgrehan return (vq_ring_ready(vq) && vq->vq_last_avail != 424253440Sgrehan vq->vq_avail->va_idx); 425253440Sgrehan} 426253440Sgrehan 427253440Sgrehan/* 428253440Sgrehan * Deliver an interrupt to guest on the given virtual queue 429253440Sgrehan * (if possible, or a generic MSI interrupt if not using MSI-X). 430253440Sgrehan */ 431253440Sgrehanstatic inline void 432253440Sgrehanvq_interrupt(struct virtio_softc *vs, struct vqueue_info *vq) 433253440Sgrehan{ 434253440Sgrehan 435261268Sjhb if (pci_msix_enabled(vs->vs_pi)) 436253440Sgrehan pci_generate_msix(vs->vs_pi, vq->vq_msix_idx); 437253440Sgrehan else { 438261268Sjhb VS_LOCK(vs); 439253440Sgrehan vs->vs_isr |= VTCFG_ISR_QUEUES; 440253440Sgrehan pci_generate_msi(vs->vs_pi, 0); 441261268Sjhb pci_lintr_assert(vs->vs_pi); 442261268Sjhb VS_UNLOCK(vs); 443253440Sgrehan } 444253440Sgrehan} 445253440Sgrehan 446253440Sgrehanstruct iovec; 447253440Sgrehanvoid vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc, 448253440Sgrehan void *dev_softc, struct pci_devinst *pi, 449253440Sgrehan struct vqueue_info *queues); 450253440Sgrehanint vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix); 451253440Sgrehanvoid vi_reset_dev(struct virtio_softc *); 452253440Sgrehanvoid vi_set_io_bar(struct virtio_softc *, int); 453253440Sgrehan 454280026Smavint vq_getchain(struct vqueue_info *vq, uint16_t *pidx, 455253440Sgrehan struct iovec *iov, int n_iov, uint16_t *flags); 456280041Smavvoid vq_retchain(struct vqueue_info *vq); 457280026Smavvoid vq_relchain(struct vqueue_info *vq, uint16_t idx, uint32_t iolen); 458253440Sgrehanvoid vq_endchains(struct vqueue_info *vq, int used_all_avail); 459253440Sgrehan 460253440Sgrehanuint64_t vi_pci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 461253440Sgrehan int baridx, uint64_t offset, int size); 462253440Sgrehanvoid vi_pci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 463253440Sgrehan int baridx, uint64_t offset, int size, uint64_t value); 464221828Sgrehan#endif /* _VIRTIO_H_ */ 465