vmbusvar.h revision 1.2.2.3 1 1.2.2.3 martin /* $NetBSD: vmbusvar.h,v 1.2.2.3 2020/04/08 14:08:05 martin Exp $ */
2 1.2.2.2 christos /* $OpenBSD: hypervvar.h,v 1.13 2017/06/23 19:05:42 mikeb Exp $ */
3 1.2.2.2 christos
4 1.2.2.2 christos /*
5 1.2.2.2 christos * Copyright (c) 2016 Mike Belopuhov <mike (at) esdenera.com>
6 1.2.2.2 christos *
7 1.2.2.2 christos * Permission to use, copy, modify, and distribute this software for any
8 1.2.2.2 christos * purpose with or without fee is hereby granted, provided that the above
9 1.2.2.2 christos * copyright notice and this permission notice appear in all copies.
10 1.2.2.2 christos *
11 1.2.2.2 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.2.2.2 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.2.2.2 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.2.2.2 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.2.2.2 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.2.2.2 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.2.2.2 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.2.2.2 christos */
19 1.2.2.2 christos
20 1.2.2.2 christos #ifndef _VMBUSVAR_H_
21 1.2.2.2 christos #define _VMBUSVAR_H_
22 1.2.2.2 christos
23 1.2.2.2 christos #include <sys/param.h>
24 1.2.2.2 christos #include <sys/device.h>
25 1.2.2.2 christos #include <sys/atomic.h>
26 1.2.2.2 christos #include <sys/bus.h>
27 1.2.2.3 martin #include <sys/condvar.h>
28 1.2.2.2 christos #include <sys/evcnt.h>
29 1.2.2.2 christos #include <sys/kcpuset.h>
30 1.2.2.2 christos #include <sys/mutex.h>
31 1.2.2.2 christos #include <sys/pool.h>
32 1.2.2.2 christos
33 1.2.2.2 christos #include <dev/hyperv/hypervreg.h>
34 1.2.2.2 christos #include <dev/hyperv/hypervvar.h>
35 1.2.2.2 christos
36 1.2.2.2 christos /* #define HYPERV_DEBUG */
37 1.2.2.2 christos
38 1.2.2.2 christos #ifdef HYPERV_DEBUG
39 1.2.2.2 christos #define DPRINTF(x...) printf(x)
40 1.2.2.2 christos #else
41 1.2.2.2 christos #define DPRINTF(x...)
42 1.2.2.2 christos #endif
43 1.2.2.2 christos
44 1.2.2.2 christos typedef void (*vmbus_channel_callback_t)(void *);
45 1.2.2.2 christos
46 1.2.2.2 christos struct vmbus_softc;
47 1.2.2.3 martin struct vmbus_channel;
48 1.2.2.2 christos
49 1.2.2.2 christos struct vmbus_msg {
50 1.2.2.2 christos uint64_t msg_flags;
51 1.2.2.2 christos #define MSGF_NOSLEEP __BIT(0)
52 1.2.2.2 christos #define MSGF_NOQUEUE __BIT(1)
53 1.2.2.2 christos #define MSGF_ORPHANED __BIT(2)
54 1.2.2.2 christos struct hyperv_hypercall_postmsg_in msg_req __aligned(8);
55 1.2.2.2 christos void *msg_rsp;
56 1.2.2.2 christos size_t msg_rsplen;
57 1.2.2.2 christos TAILQ_ENTRY(vmbus_msg) msg_entry;
58 1.2.2.2 christos };
59 1.2.2.2 christos __CTASSERT((offsetof(struct vmbus_msg, msg_req) % 8) == 0);
60 1.2.2.2 christos TAILQ_HEAD(vmbus_queue, vmbus_msg);
61 1.2.2.2 christos
62 1.2.2.3 martin struct vmbus_dev {
63 1.2.2.3 martin int vd_type;
64 1.2.2.3 martin #define VMBUS_DEV_TYPE_ATTACH 0
65 1.2.2.3 martin #define VMBUS_DEV_TYPE_DETACH 1
66 1.2.2.3 martin struct vmbus_channel *vd_chan;
67 1.2.2.3 martin SIMPLEQ_ENTRY(vmbus_dev) vd_entry;
68 1.2.2.2 christos };
69 1.2.2.3 martin SIMPLEQ_HEAD(vmbus_devq, vmbus_dev);
70 1.2.2.2 christos
71 1.2.2.2 christos struct vmbus_ring_data {
72 1.2.2.2 christos struct vmbus_bufring *rd_ring;
73 1.2.2.2 christos uint32_t rd_size;
74 1.2.2.2 christos kmutex_t rd_lock;
75 1.2.2.2 christos uint32_t rd_prod;
76 1.2.2.2 christos uint32_t rd_cons;
77 1.2.2.2 christos uint32_t rd_dsize;
78 1.2.2.2 christos };
79 1.2.2.2 christos
80 1.2.2.2 christos TAILQ_HEAD(vmbus_channels, vmbus_channel);
81 1.2.2.2 christos
82 1.2.2.2 christos struct vmbus_channel {
83 1.2.2.2 christos struct vmbus_softc *ch_sc;
84 1.2.2.2 christos device_t ch_dev;
85 1.2.2.2 christos u_int ch_refs;
86 1.2.2.2 christos
87 1.2.2.2 christos int ch_state;
88 1.2.2.2 christos #define VMBUS_CHANSTATE_OFFERED 1
89 1.2.2.2 christos #define VMBUS_CHANSTATE_OPENED 2
90 1.2.2.2 christos #define VMBUS_CHANSTATE_CLOSING 3
91 1.2.2.2 christos #define VMBUS_CHANSTATE_CLOSED 4
92 1.2.2.2 christos uint32_t ch_id;
93 1.2.2.2 christos uint16_t ch_subidx;
94 1.2.2.2 christos
95 1.2.2.2 christos struct hyperv_guid ch_type;
96 1.2.2.2 christos struct hyperv_guid ch_inst;
97 1.2.2.2 christos char ch_ident[38];
98 1.2.2.2 christos
99 1.2.2.2 christos void *ch_ring;
100 1.2.2.2 christos uint32_t ch_ring_gpadl;
101 1.2.2.2 christos u_long ch_ring_size;
102 1.2.2.2 christos struct hyperv_dma ch_ring_dma;
103 1.2.2.2 christos
104 1.2.2.2 christos struct vmbus_ring_data ch_wrd;
105 1.2.2.2 christos struct vmbus_ring_data ch_rrd;
106 1.2.2.2 christos
107 1.2.2.2 christos int ch_cpuid;
108 1.2.2.2 christos uint32_t ch_vcpu;
109 1.2.2.2 christos
110 1.2.2.2 christos void (*ch_handler)(void *);
111 1.2.2.2 christos void *ch_ctx;
112 1.2.2.2 christos struct evcnt ch_evcnt;
113 1.2.2.2 christos void *ch_taskq;
114 1.2.2.2 christos
115 1.2.2.2 christos uint32_t ch_flags;
116 1.2.2.2 christos #define CHF_BATCHED __BIT(0)
117 1.2.2.2 christos #define CHF_MONITOR __BIT(1)
118 1.2.2.3 martin #define CHF_REVOKED __BIT(2)
119 1.2.2.2 christos
120 1.2.2.2 christos uint8_t ch_mgroup;
121 1.2.2.2 christos uint8_t ch_mindex;
122 1.2.2.2 christos struct hyperv_mon_param *ch_monprm;
123 1.2.2.2 christos struct hyperv_dma ch_monprm_dma;
124 1.2.2.2 christos
125 1.2.2.2 christos TAILQ_ENTRY(vmbus_channel) ch_entry;
126 1.2.2.2 christos
127 1.2.2.2 christos kmutex_t ch_subchannel_lock;
128 1.2.2.2 christos struct vmbus_channels ch_subchannels;
129 1.2.2.2 christos u_int ch_subchannel_count;
130 1.2.2.2 christos TAILQ_ENTRY(vmbus_channel) ch_subentry;
131 1.2.2.2 christos struct vmbus_channel *ch_primary_channel;
132 1.2.2.2 christos };
133 1.2.2.2 christos
134 1.2.2.2 christos #define VMBUS_CHAN_ISPRIMARY(chan) ((chan)->ch_subidx == 0)
135 1.2.2.2 christos
136 1.2.2.2 christos struct vmbus_attach_args {
137 1.2.2.2 christos struct hyperv_guid *aa_type;
138 1.2.2.2 christos struct hyperv_guid *aa_inst;
139 1.2.2.2 christos char *aa_ident;
140 1.2.2.2 christos struct vmbus_channel *aa_chan;
141 1.2.2.2 christos bus_space_tag_t aa_iot;
142 1.2.2.2 christos bus_space_tag_t aa_memt;
143 1.2.2.2 christos };
144 1.2.2.2 christos
145 1.2.2.2 christos struct vmbus_percpu_data {
146 1.2.2.2 christos void *simp; /* Synthetic Interrupt Message Page */
147 1.2.2.2 christos void *siep; /* Synthetic Interrupt Event Flags Page */
148 1.2.2.2 christos
149 1.2.2.2 christos /* Rarely used fields */
150 1.2.2.2 christos struct hyperv_dma simp_dma;
151 1.2.2.2 christos struct hyperv_dma siep_dma;
152 1.2.2.2 christos } __aligned(CACHE_LINE_SIZE);
153 1.2.2.2 christos
154 1.2.2.2 christos struct vmbus_softc {
155 1.2.2.2 christos device_t sc_dev;
156 1.2.2.2 christos bus_space_tag_t sc_iot;
157 1.2.2.2 christos bus_space_tag_t sc_memt;
158 1.2.2.2 christos bus_dma_tag_t sc_dmat;
159 1.2.2.2 christos
160 1.2.2.2 christos pool_cache_t sc_msgpool;
161 1.2.2.2 christos
162 1.2.2.2 christos void *sc_msg_sih;
163 1.2.2.2 christos
164 1.2.2.2 christos u_long *sc_wevents; /* Write events */
165 1.2.2.2 christos u_long *sc_revents; /* Read events */
166 1.2.2.2 christos struct vmbus_mnf *sc_monitor[2];
167 1.2.2.2 christos struct vmbus_percpu_data sc_percpu[MAXCPUS];
168 1.2.2.2 christos
169 1.2.2.2 christos /*
170 1.2.2.2 christos * Rarely used fields
171 1.2.2.2 christos */
172 1.2.2.2 christos uint32_t sc_flags;
173 1.2.2.2 christos #define VMBUS_SCFLAG_SYNIC __BIT(0)
174 1.2.2.2 christos #define VMBUS_SCFLAG_CONNECTED __BIT(1)
175 1.2.2.2 christos #define VMBUS_SCFLAG_OFFERS_DELIVERED __BIT(2)
176 1.2.2.2 christos uint32_t sc_proto;
177 1.2.2.2 christos int sc_channel_max;
178 1.2.2.2 christos
179 1.2.2.2 christos kcpuset_t *sc_intr_cpuset;
180 1.2.2.2 christos
181 1.2.2.2 christos /* Shared memory for Write/Read events */
182 1.2.2.2 christos void *sc_events;
183 1.2.2.2 christos struct hyperv_dma sc_events_dma;
184 1.2.2.2 christos
185 1.2.2.2 christos struct hyperv_dma sc_monitor_dma[2];
186 1.2.2.2 christos
187 1.2.2.2 christos struct vmbus_queue sc_reqs; /* Request queue */
188 1.2.2.2 christos kmutex_t sc_req_lock;
189 1.2.2.2 christos struct vmbus_queue sc_rsps; /* Response queue */
190 1.2.2.2 christos kmutex_t sc_rsp_lock;
191 1.2.2.2 christos
192 1.2.2.3 martin struct vmbus_devq sc_devq;
193 1.2.2.3 martin kmutex_t sc_devq_lock;
194 1.2.2.3 martin kcondvar_t sc_devq_cv;
195 1.2.2.2 christos
196 1.2.2.2 christos struct vmbus_channels sc_channels;
197 1.2.2.2 christos kmutex_t sc_channel_lock;
198 1.2.2.2 christos
199 1.2.2.2 christos volatile uint32_t sc_handle;
200 1.2.2.2 christos };
201 1.2.2.2 christos
202 1.2.2.2 christos static __inline void
203 1.2.2.2 christos clear_bit(u_int b, volatile void *p)
204 1.2.2.2 christos {
205 1.2.2.2 christos atomic_and_32(((volatile u_int *)p) + (b >> 5), ~(1 << (b & 0x1f)));
206 1.2.2.2 christos }
207 1.2.2.2 christos
208 1.2.2.2 christos static __inline void
209 1.2.2.2 christos set_bit(u_int b, volatile void *p)
210 1.2.2.2 christos {
211 1.2.2.2 christos atomic_or_32(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f));
212 1.2.2.2 christos }
213 1.2.2.2 christos
214 1.2.2.2 christos static __inline int
215 1.2.2.2 christos test_bit(u_int b, volatile void *p)
216 1.2.2.2 christos {
217 1.2.2.2 christos return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f)));
218 1.2.2.2 christos }
219 1.2.2.2 christos
220 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_network;
221 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_ide;
222 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_scsi;
223 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_shutdown;
224 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_timesync;
225 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_heartbeat;
226 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_kvp;
227 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_vss;
228 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_dynmem;
229 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_mouse;
230 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_kbd;
231 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_video;
232 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_fc;
233 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_fcopy;
234 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_pcie;
235 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_netdir;
236 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_rdesktop;
237 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma1;
238 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma2;
239 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma3;
240 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma4;
241 1.2.2.2 christos
242 1.2.2.2 christos int vmbus_match(device_t, cfdata_t, void *);
243 1.2.2.2 christos int vmbus_attach(struct vmbus_softc *);
244 1.2.2.2 christos int vmbus_detach(struct vmbus_softc *, int);
245 1.2.2.2 christos
246 1.2.2.2 christos int vmbus_handle_alloc(struct vmbus_channel *, const struct hyperv_dma *,
247 1.2.2.2 christos uint32_t, uint32_t *);
248 1.2.2.2 christos void vmbus_handle_free(struct vmbus_channel *, uint32_t);
249 1.2.2.2 christos int vmbus_channel_open(struct vmbus_channel *, size_t, void *, size_t,
250 1.2.2.2 christos vmbus_channel_callback_t, void *);
251 1.2.2.2 christos int vmbus_channel_close(struct vmbus_channel *);
252 1.2.2.2 christos int vmbus_channel_close_direct(struct vmbus_channel *);
253 1.2.2.2 christos int vmbus_channel_setdeferred(struct vmbus_channel *, const char *);
254 1.2.2.2 christos void vmbus_channel_schedule(struct vmbus_channel *);
255 1.2.2.2 christos int vmbus_channel_send(struct vmbus_channel *, void *, uint32_t, uint64_t,
256 1.2.2.2 christos int, uint32_t);
257 1.2.2.2 christos int vmbus_channel_send_sgl(struct vmbus_channel *, struct vmbus_gpa *,
258 1.2.2.2 christos uint32_t, void *, uint32_t, uint64_t);
259 1.2.2.2 christos int vmbus_channel_send_prpl(struct vmbus_channel *,
260 1.2.2.2 christos struct vmbus_gpa_range *, uint32_t, void *, uint32_t, uint64_t);
261 1.2.2.2 christos int vmbus_channel_recv(struct vmbus_channel *, void *, uint32_t, uint32_t *,
262 1.2.2.2 christos uint64_t *, int);
263 1.2.2.2 christos void vmbus_channel_cpu_set(struct vmbus_channel *, int);
264 1.2.2.2 christos void vmbus_channel_cpu_rr(struct vmbus_channel *);
265 1.2.2.3 martin bool vmbus_channel_is_revoked(struct vmbus_channel *);
266 1.2.2.2 christos
267 1.2.2.2 christos struct vmbus_channel **
268 1.2.2.2 christos vmbus_subchannel_get(struct vmbus_channel *, int);
269 1.2.2.2 christos void vmbus_subchannel_put(struct vmbus_channel **, int);
270 1.2.2.2 christos
271 1.2.2.2 christos #endif /* _VMBUSVAR_H_ */
272