1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2017, Bryan Venteicher <bryanv@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef _VIRTIO_PCI_H
30 #define _VIRTIO_PCI_H
31
32 struct vtpci_interrupt {
33 struct resource *vti_irq;
34 int vti_rid;
35 void *vti_handler;
36 };
37
38 struct vtpci_virtqueue {
39 struct virtqueue *vtv_vq;
40 int vtv_no_intr;
41 int vtv_notify_offset;
42 };
43
44 struct vtpci_common {
45 device_t vtpci_dev;
46 uint64_t vtpci_host_features;
47 uint64_t vtpci_features;
48 struct vtpci_virtqueue *vtpci_vqs;
49 int vtpci_nvqs;
50
51 uint32_t vtpci_flags;
52 #define VTPCI_FLAG_NO_MSI 0x0001
53 #define VTPCI_FLAG_NO_MSIX 0x0002
54 #define VTPCI_FLAG_MODERN 0x0004
55 #define VTPCI_FLAG_INTX 0x1000
56 #define VTPCI_FLAG_MSI 0x2000
57 #define VTPCI_FLAG_MSIX 0x4000
58 #define VTPCI_FLAG_SHARED_MSIX 0x8000
59 #define VTPCI_FLAG_ITYPE_MASK 0xF000
60
61 /* The VirtIO PCI "bus" will only ever have one child. */
62 device_t vtpci_child_dev;
63 struct virtio_feature_desc *vtpci_child_feat_desc;
64
65 /*
66 * Ideally, each virtqueue that the driver provides a callback for will
67 * receive its own MSIX vector. If there are not sufficient vectors
68 * available, then attempt to have all the VQs share one vector. For
69 * MSIX, the configuration changed notifications must be on their own
70 * vector.
71 *
72 * If MSIX is not available, attempt to have the whole device share
73 * one MSI vector, and then, finally, one intx interrupt.
74 */
75 struct vtpci_interrupt vtpci_device_interrupt;
76 struct vtpci_interrupt *vtpci_msix_vq_interrupts;
77 int vtpci_nmsix_resources;
78 };
79
80 extern int vtpci_disable_msix;
81
82 static inline device_t
vtpci_child_device(struct vtpci_common * cn)83 vtpci_child_device(struct vtpci_common *cn)
84 {
85 return (cn->vtpci_child_dev);
86 }
87
88 static inline bool
vtpci_is_msix_available(struct vtpci_common * cn)89 vtpci_is_msix_available(struct vtpci_common *cn)
90 {
91 return ((cn->vtpci_flags & VTPCI_FLAG_NO_MSIX) == 0);
92 }
93
94 static inline bool
vtpci_is_msix_enabled(struct vtpci_common * cn)95 vtpci_is_msix_enabled(struct vtpci_common *cn)
96 {
97 return ((cn->vtpci_flags & VTPCI_FLAG_MSIX) != 0);
98 }
99
100 static inline bool
vtpci_is_modern(struct vtpci_common * cn)101 vtpci_is_modern(struct vtpci_common *cn)
102 {
103 return ((cn->vtpci_flags & VTPCI_FLAG_MODERN) != 0);
104 }
105
106 static inline int
vtpci_virtqueue_count(struct vtpci_common * cn)107 vtpci_virtqueue_count(struct vtpci_common *cn)
108 {
109 return (cn->vtpci_nvqs);
110 }
111
112 void vtpci_init(struct vtpci_common *cn, device_t dev, bool modern);
113 int vtpci_add_child(struct vtpci_common *cn);
114 int vtpci_delete_child(struct vtpci_common *cn);
115 void vtpci_child_detached(struct vtpci_common *cn);
116 int vtpci_reinit(struct vtpci_common *cn);
117
118 uint64_t vtpci_negotiate_features(struct vtpci_common *cn,
119 uint64_t child_features, uint64_t host_features);
120 bool vtpci_with_feature(struct vtpci_common *cn, uint64_t feature);
121
122 int vtpci_read_ivar(struct vtpci_common *cn, int index, uintptr_t *result);
123 int vtpci_write_ivar(struct vtpci_common *cn, int index, uintptr_t value);
124
125 int vtpci_alloc_virtqueues(struct vtpci_common *cn, int nvqs,
126 struct vq_alloc_info *vq_info);
127 int vtpci_setup_interrupts(struct vtpci_common *cn, enum intr_type type);
128 void vtpci_release_child_resources(struct vtpci_common *cn);
129
130 #endif /* _VIRTIO_PCI_H */
131