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