1336f459cSPeter Grehan /*- 29da9560cSBryan Venteicher * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 37282444bSPedro F. Giffuni * 49da9560cSBryan Venteicher * Copyright (c) 2017, Bryan Venteicher <bryanv@FreeBSD.org> 59da9560cSBryan Venteicher * All rights reserved. 610b59a9bSPeter Grehan * 7336f459cSPeter Grehan * Redistribution and use in source and binary forms, with or without 8336f459cSPeter Grehan * modification, are permitted provided that the following conditions 9336f459cSPeter Grehan * are met: 10336f459cSPeter Grehan * 1. Redistributions of source code must retain the above copyright 119da9560cSBryan Venteicher * notice unmodified, this list of conditions, and the following 129da9560cSBryan Venteicher * disclaimer. 13336f459cSPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright 14336f459cSPeter Grehan * notice, this list of conditions and the following disclaimer in the 15336f459cSPeter Grehan * documentation and/or other materials provided with the distribution. 169da9560cSBryan Venteicher * 179da9560cSBryan Venteicher * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 189da9560cSBryan Venteicher * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 199da9560cSBryan Venteicher * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 209da9560cSBryan Venteicher * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 219da9560cSBryan Venteicher * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 229da9560cSBryan Venteicher * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 239da9560cSBryan Venteicher * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 249da9560cSBryan Venteicher * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 259da9560cSBryan Venteicher * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 269da9560cSBryan Venteicher * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27336f459cSPeter Grehan * 2810b59a9bSPeter Grehan * $FreeBSD$ 2910b59a9bSPeter Grehan */ 3010b59a9bSPeter Grehan 3110b59a9bSPeter Grehan #ifndef _VIRTIO_PCI_H 3210b59a9bSPeter Grehan #define _VIRTIO_PCI_H 3310b59a9bSPeter Grehan 349da9560cSBryan Venteicher struct vtpci_interrupt { 359da9560cSBryan Venteicher struct resource *vti_irq; 369da9560cSBryan Venteicher int vti_rid; 379da9560cSBryan Venteicher void *vti_handler; 389da9560cSBryan Venteicher }; 3910b59a9bSPeter Grehan 409da9560cSBryan Venteicher struct vtpci_virtqueue { 419da9560cSBryan Venteicher struct virtqueue *vtv_vq; 429da9560cSBryan Venteicher int vtv_no_intr; 439da9560cSBryan Venteicher int vtv_notify_offset; 449da9560cSBryan Venteicher }; 459da9560cSBryan Venteicher 469da9560cSBryan Venteicher struct vtpci_common { 479da9560cSBryan Venteicher device_t vtpci_dev; 48*703f17d6SBryan Venteicher uint64_t vtpci_host_features; 499da9560cSBryan Venteicher uint64_t vtpci_features; 509da9560cSBryan Venteicher struct vtpci_virtqueue *vtpci_vqs; 519da9560cSBryan Venteicher int vtpci_nvqs; 529da9560cSBryan Venteicher 539da9560cSBryan Venteicher uint32_t vtpci_flags; 549da9560cSBryan Venteicher #define VTPCI_FLAG_NO_MSI 0x0001 559da9560cSBryan Venteicher #define VTPCI_FLAG_NO_MSIX 0x0002 569da9560cSBryan Venteicher #define VTPCI_FLAG_MODERN 0x0004 579da9560cSBryan Venteicher #define VTPCI_FLAG_INTX 0x1000 589da9560cSBryan Venteicher #define VTPCI_FLAG_MSI 0x2000 599da9560cSBryan Venteicher #define VTPCI_FLAG_MSIX 0x4000 609da9560cSBryan Venteicher #define VTPCI_FLAG_SHARED_MSIX 0x8000 619da9560cSBryan Venteicher #define VTPCI_FLAG_ITYPE_MASK 0xF000 629da9560cSBryan Venteicher 639da9560cSBryan Venteicher /* The VirtIO PCI "bus" will only ever have one child. */ 649da9560cSBryan Venteicher device_t vtpci_child_dev; 659da9560cSBryan Venteicher struct virtio_feature_desc *vtpci_child_feat_desc; 6610b59a9bSPeter Grehan 6710b59a9bSPeter Grehan /* 689da9560cSBryan Venteicher * Ideally, each virtqueue that the driver provides a callback for will 699da9560cSBryan Venteicher * receive its own MSIX vector. If there are not sufficient vectors 709da9560cSBryan Venteicher * available, then attempt to have all the VQs share one vector. For 719da9560cSBryan Venteicher * MSIX, the configuration changed notifications must be on their own 729da9560cSBryan Venteicher * vector. 739da9560cSBryan Venteicher * 749da9560cSBryan Venteicher * If MSIX is not available, attempt to have the whole device share 759da9560cSBryan Venteicher * one MSI vector, and then, finally, one intx interrupt. 7610b59a9bSPeter Grehan */ 779da9560cSBryan Venteicher struct vtpci_interrupt vtpci_device_interrupt; 789da9560cSBryan Venteicher struct vtpci_interrupt *vtpci_msix_vq_interrupts; 799da9560cSBryan Venteicher int vtpci_nmsix_resources; 809da9560cSBryan Venteicher }; 8110b59a9bSPeter Grehan 829da9560cSBryan Venteicher extern int vtpci_disable_msix; 8310b59a9bSPeter Grehan 849da9560cSBryan Venteicher static inline device_t 859da9560cSBryan Venteicher vtpci_child_device(struct vtpci_common *cn) 869da9560cSBryan Venteicher { 879da9560cSBryan Venteicher return (cn->vtpci_child_dev); 889da9560cSBryan Venteicher } 8910b59a9bSPeter Grehan 909da9560cSBryan Venteicher static inline bool 919da9560cSBryan Venteicher vtpci_is_msix_available(struct vtpci_common *cn) 929da9560cSBryan Venteicher { 939da9560cSBryan Venteicher return ((cn->vtpci_flags & VTPCI_FLAG_NO_MSIX) == 0); 949da9560cSBryan Venteicher } 9510b59a9bSPeter Grehan 969da9560cSBryan Venteicher static inline bool 979da9560cSBryan Venteicher vtpci_is_msix_enabled(struct vtpci_common *cn) 989da9560cSBryan Venteicher { 999da9560cSBryan Venteicher return ((cn->vtpci_flags & VTPCI_FLAG_MSIX) != 0); 1009da9560cSBryan Venteicher } 1019da9560cSBryan Venteicher 1029da9560cSBryan Venteicher static inline bool 1039da9560cSBryan Venteicher vtpci_is_modern(struct vtpci_common *cn) 1049da9560cSBryan Venteicher { 1059da9560cSBryan Venteicher return ((cn->vtpci_flags & VTPCI_FLAG_MODERN) != 0); 1069da9560cSBryan Venteicher } 1079da9560cSBryan Venteicher 1089da9560cSBryan Venteicher static inline int 1099da9560cSBryan Venteicher vtpci_virtqueue_count(struct vtpci_common *cn) 1109da9560cSBryan Venteicher { 1119da9560cSBryan Venteicher return (cn->vtpci_nvqs); 1129da9560cSBryan Venteicher } 1139da9560cSBryan Venteicher 1149da9560cSBryan Venteicher void vtpci_init(struct vtpci_common *cn, device_t dev, bool modern); 1159da9560cSBryan Venteicher int vtpci_add_child(struct vtpci_common *cn); 1169da9560cSBryan Venteicher int vtpci_delete_child(struct vtpci_common *cn); 1179da9560cSBryan Venteicher void vtpci_child_detached(struct vtpci_common *cn); 1189da9560cSBryan Venteicher int vtpci_reinit(struct vtpci_common *cn); 1199da9560cSBryan Venteicher 1209da9560cSBryan Venteicher uint64_t vtpci_negotiate_features(struct vtpci_common *cn, 1219da9560cSBryan Venteicher uint64_t child_features, uint64_t host_features); 1229da9560cSBryan Venteicher int vtpci_with_feature(struct vtpci_common *cn, uint64_t feature); 1239da9560cSBryan Venteicher 1249da9560cSBryan Venteicher int vtpci_read_ivar(struct vtpci_common *cn, int index, uintptr_t *result); 1259da9560cSBryan Venteicher int vtpci_write_ivar(struct vtpci_common *cn, int index, uintptr_t value); 1269da9560cSBryan Venteicher 1279da9560cSBryan Venteicher int vtpci_alloc_virtqueues(struct vtpci_common *cn, int flags, int nvqs, 1289da9560cSBryan Venteicher struct vq_alloc_info *vq_info); 1299da9560cSBryan Venteicher int vtpci_setup_interrupts(struct vtpci_common *cn, enum intr_type type); 1309da9560cSBryan Venteicher void vtpci_release_child_resources(struct vtpci_common *cn); 13110b59a9bSPeter Grehan 13210b59a9bSPeter Grehan #endif /* _VIRTIO_PCI_H */ 133