xref: /freebsd-13-stable/sys/dev/hyperv/vmbus/vmbus_reg.h (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
1 /*-
2  * Copyright (c) 2016 Microsoft Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef _VMBUS_REG_H_
28 #define _VMBUS_REG_H_
29 
30 #include <sys/param.h>
31 #include <dev/hyperv/include/hyperv.h> /* XXX for hyperv_guid */
32 #include <dev/hyperv/include/vmbus.h>
33 #include <dev/hyperv/vmbus/hyperv_reg.h>
34 
35 /*
36  * Hyper-V SynIC message format.
37  */
38 
39 #define VMBUS_MSG_DSIZE_MAX		240
40 #define VMBUS_MSG_SIZE			256
41 
42 struct vmbus_message {
43 	uint32_t	msg_type;	/* HYPERV_MSGTYPE_ */
44 	uint8_t		msg_dsize;	/* data size */
45 	uint8_t		msg_flags;	/* VMBUS_MSGFLAG_ */
46 	uint16_t	msg_rsvd;
47 	uint64_t	msg_id;
48 	uint8_t		msg_data[VMBUS_MSG_DSIZE_MAX];
49 } __packed;
50 CTASSERT(sizeof(struct vmbus_message) == VMBUS_MSG_SIZE);
51 
52 #define VMBUS_MSGFLAG_PENDING		0x01
53 
54 /*
55  * Hyper-V SynIC event flags
56  */
57 
58 #ifdef __LP64__
59 #define VMBUS_EVTFLAGS_MAX	32
60 #define VMBUS_EVTFLAG_SHIFT	6
61 #else
62 #define VMBUS_EVTFLAGS_MAX	64
63 #define VMBUS_EVTFLAG_SHIFT	5
64 #endif
65 #define VMBUS_EVTFLAG_LEN	(1 << VMBUS_EVTFLAG_SHIFT)
66 #define VMBUS_EVTFLAG_MASK	(VMBUS_EVTFLAG_LEN - 1)
67 #define VMBUS_EVTFLAGS_SIZE	256
68 
69 struct vmbus_evtflags {
70 	u_long		evt_flags[VMBUS_EVTFLAGS_MAX];
71 } __packed;
72 CTASSERT(sizeof(struct vmbus_evtflags) == VMBUS_EVTFLAGS_SIZE);
73 
74 /*
75  * Hyper-V Monitor Notification Facility
76  */
77 
78 struct vmbus_mon_trig {
79 	uint32_t	mt_pending;
80 	uint32_t	mt_armed;
81 } __packed;
82 
83 #define VMBUS_MONTRIGS_MAX	4
84 #define VMBUS_MONTRIG_LEN	32
85 
86 struct vmbus_mnf {
87 	uint32_t	mnf_state;
88 	uint32_t	mnf_rsvd1;
89 
90 	struct vmbus_mon_trig mnf_trigs[VMBUS_MONTRIGS_MAX];
91 	uint8_t		mnf_rsvd2[536];
92 
93 	uint16_t	mnf_lat[VMBUS_MONTRIGS_MAX][VMBUS_MONTRIG_LEN];
94 	uint8_t		mnf_rsvd3[256];
95 
96 	struct hyperv_mon_param
97 			mnf_param[VMBUS_MONTRIGS_MAX][VMBUS_MONTRIG_LEN];
98 	uint8_t		mnf_rsvd4[1984];
99 } __packed;
100 CTASSERT(sizeof(struct vmbus_mnf) == PAGE_SIZE);
101 
102 /*
103  * Buffer ring
104  */
105 struct vmbus_bufring {
106 	/*
107 	 * If br_windex == br_rindex, this bufring is empty; this
108 	 * means we can _not_ write data to the bufring, if the
109 	 * write is going to make br_windex same as br_rindex.
110 	 */
111 	volatile uint32_t	br_windex;
112 	volatile uint32_t	br_rindex;
113 
114 	/*
115 	 * Interrupt mask {0,1}
116 	 *
117 	 * For TX bufring, host set this to 1, when it is processing
118 	 * the TX bufring, so that we can safely skip the TX event
119 	 * notification to host.
120 	 *
121 	 * For RX bufring, once this is set to 1 by us, host will not
122 	 * further dispatch interrupts to us, even if there are data
123 	 * pending on the RX bufring.  This effectively disables the
124 	 * interrupt of the channel to which this RX bufring is attached.
125 	 */
126 	volatile uint32_t	br_imask;
127 
128 	/*
129 	 * WS2012/Win8 and later versions of Hyper-V implement interrupt
130 	 * driven flow management. The feature bit feat_pending_snd_sz
131 	 * is set by the host on the host->guest buffer ring, and by the
132 	 * guest on the guest->host buffer ring.
133 	 *
134 	 * The meaning of the feature bit is a bit complex in that it has
135 	 * semantics that apply to both buffer rings.  If the guest sets
136 	 * the feature bit in the guest->host buffer ring, the guest is
137 	 * telling the host that:
138 	 * 1) It will set the br_pending_snd_sz field in the guest->host buffer
139 	 *    ring when it is waiting for space to become available, and
140 	 * 2) It will read the pending_send_sz field in the host->guest
141 	 *    ring buffer and interrupt the host when it frees enough space
142 	 *
143 	 * Similarly, if the host sets the feature bit in the host->guest
144 	 * ring buffer, the host is telling the guest that:
145 	 * 1) It will set the pending_send_sz field in the host->guest ring
146 	 *    buffer when it is waiting for space to become available, and
147 	 * 2) It will read the pending_send_sz field in the guest->host
148 	 *    ring buffer and interrupt the guest when it frees enough space
149 	 *
150 	 * If either the guest or host does not set the feature bit that it
151 	 * owns, that guest or host must do polling if it encounters a full
152 	 * ring buffer, and not signal the other end with an interrupt.
153 	 */
154 	volatile uint32_t	br_pending_snd_sz;
155 	uint32_t		br_rsvd1[12];
156 	union	{
157 		struct {
158 			uint32_t feat_pending_snd_sz:1;
159 		};
160 		uint32_t value;
161 	} br_feature_bits;
162 
163 	/* Padding to PAGE_SIZE */
164 	uint8_t			br_rsvd2[4020];
165 
166 	/*
167 	 * Total guest to host interrupt count
168 	 * - For rx ring, this counts the guest signaling host when this rx
169 	 * ring changing from full to not full.
170 	 *
171 	 * - For tx ring, this counts the guest signaling host when this tx
172 	 * ring changing from empty to non empty.
173 	 */
174 	uint64_t		br_g2h_intr_cnt;
175 
176 	uint8_t			br_data[];
177 } __packed;
178 CTASSERT(sizeof(struct vmbus_bufring) == PAGE_SIZE);
179 
180 /*
181  * Channel
182  */
183 
184 #define VMBUS_CHAN_MAX_COMPAT	256
185 #define VMBUS_CHAN_MAX		(VMBUS_EVTFLAG_LEN * VMBUS_EVTFLAGS_MAX)
186 
187 /*
188  * Channel packets
189  */
190 
191 #define VMBUS_CHANPKT_SIZE_ALIGN	(1 << VMBUS_CHANPKT_SIZE_SHIFT)
192 
193 #define VMBUS_CHANPKT_SETLEN(pktlen, len)		\
194 do {							\
195 	(pktlen) = (len) >> VMBUS_CHANPKT_SIZE_SHIFT;	\
196 } while (0)
197 
198 #define VMBUS_CHANPKT_TOTLEN(tlen)	\
199 	roundup2((tlen), VMBUS_CHANPKT_SIZE_ALIGN)
200 
201 #define VMBUS_CHANPKT_HLEN_MIN		\
202 	(sizeof(struct vmbus_chanpkt_hdr) >> VMBUS_CHANPKT_SIZE_SHIFT)
203 
204 struct vmbus_chanpkt {
205 	struct vmbus_chanpkt_hdr cp_hdr;
206 } __packed;
207 
208 struct vmbus_chanpkt_sglist {
209 	struct vmbus_chanpkt_hdr cp_hdr;
210 	uint32_t	cp_rsvd;
211 	uint32_t	cp_gpa_cnt;
212 	struct vmbus_gpa cp_gpa[];
213 } __packed;
214 
215 struct vmbus_chanpkt_prplist {
216 	struct vmbus_chanpkt_hdr cp_hdr;
217 	uint32_t	cp_rsvd;
218 	uint32_t	cp_range_cnt;
219 	struct vmbus_gpa_range cp_range[];
220 } __packed;
221 
222 /*
223  * Channel messages
224  * - Embedded in vmbus_message.msg_data, e.g. response and notification.
225  * - Embedded in hypercall_postmsg_in.hc_data, e.g. request.
226  */
227 
228 #define VMBUS_CHANMSG_TYPE_CHOFFER		1	/* NOTE */
229 #define VMBUS_CHANMSG_TYPE_CHRESCIND		2	/* NOTE */
230 #define VMBUS_CHANMSG_TYPE_CHREQUEST		3	/* REQ */
231 #define VMBUS_CHANMSG_TYPE_CHOFFER_DONE		4	/* NOTE */
232 #define VMBUS_CHANMSG_TYPE_CHOPEN		5	/* REQ */
233 #define VMBUS_CHANMSG_TYPE_CHOPEN_RESP		6	/* RESP */
234 #define VMBUS_CHANMSG_TYPE_CHCLOSE		7	/* REQ */
235 #define VMBUS_CHANMSG_TYPE_GPADL_CONN		8	/* REQ */
236 #define VMBUS_CHANMSG_TYPE_GPADL_SUBCONN	9	/* REQ */
237 #define VMBUS_CHANMSG_TYPE_GPADL_CONNRESP	10	/* RESP */
238 #define VMBUS_CHANMSG_TYPE_GPADL_DISCONN	11	/* REQ */
239 #define VMBUS_CHANMSG_TYPE_GPADL_DISCONNRESP	12	/* RESP */
240 #define VMBUS_CHANMSG_TYPE_CHFREE		13	/* REQ */
241 #define VMBUS_CHANMSG_TYPE_CONNECT		14	/* REQ */
242 #define VMBUS_CHANMSG_TYPE_CONNECT_RESP		15	/* RESP */
243 #define VMBUS_CHANMSG_TYPE_DISCONNECT		16	/* REQ */
244 #define VMBUS_CHANMSG_TYPE_17			17
245 #define VMBUS_CHANMSG_TYPE_18			18
246 #define VMBUS_CHANMSG_TYPE_19			19
247 #define VMBUS_CHANMSG_TYPE_20			20
248 #define VMBUS_CHANMSG_TYPE_TL_CONN		21	/* REQ */
249 #define VMBUS_CHANMSG_TYPE_22			22
250 #define VMBUS_CHANMSG_TYPE_TL_RESULT		23	/* RESP */
251 #define VMBUS_CHANMSG_TYPE_MAX			24
252 
253 struct vmbus_chanmsg_hdr {
254 	uint32_t	chm_type;	/* VMBUS_CHANMSG_TYPE_ */
255 	uint32_t	chm_rsvd;
256 } __packed;
257 
258 /* VMBUS_CHANMSG_TYPE_CONNECT */
259 struct vmbus_chanmsg_connect {
260 	struct vmbus_chanmsg_hdr chm_hdr;
261 	uint32_t	chm_ver;
262 	uint32_t	chm_rsvd;
263 	uint64_t	chm_evtflags;
264 	uint64_t	chm_mnf1;
265 	uint64_t	chm_mnf2;
266 } __packed;
267 
268 /* VMBUS_CHANMSG_TYPE_CONNECT_RESP */
269 struct vmbus_chanmsg_connect_resp {
270 	struct vmbus_chanmsg_hdr chm_hdr;
271 	uint8_t		chm_done;
272 } __packed;
273 
274 /* VMBUS_CHANMSG_TYPE_CHREQUEST */
275 struct vmbus_chanmsg_chrequest {
276 	struct vmbus_chanmsg_hdr chm_hdr;
277 } __packed;
278 
279 /* VMBUS_CHANMSG_TYPE_DISCONNECT */
280 struct vmbus_chanmsg_disconnect {
281 	struct vmbus_chanmsg_hdr chm_hdr;
282 } __packed;
283 
284 /* VMBUS_CHANMSG_TYPE_TL_CONN */
285 /* Hyper-V socket guest connect request */
286 struct vmbus_chanmsg_tl_connect {
287 	struct vmbus_chanmsg_hdr chm_hdr;
288 	struct hyperv_guid guest_endpoint_id;
289 	struct hyperv_guid host_service_id;
290 } __packed;
291 
292 
293 /* VMBUS_CHANMSG_TYPE_CHOPEN */
294 struct vmbus_chanmsg_chopen {
295 	struct vmbus_chanmsg_hdr chm_hdr;
296 	uint32_t	chm_chanid;
297 	uint32_t	chm_openid;
298 	uint32_t	chm_gpadl;
299 	uint32_t	chm_vcpuid;
300 	uint32_t	chm_txbr_pgcnt;
301 #define VMBUS_CHANMSG_CHOPEN_UDATA_SIZE	120
302 	uint8_t		chm_udata[VMBUS_CHANMSG_CHOPEN_UDATA_SIZE];
303 } __packed;
304 
305 /* VMBUS_CHANMSG_TYPE_CHOPEN_RESP */
306 struct vmbus_chanmsg_chopen_resp {
307 	struct vmbus_chanmsg_hdr chm_hdr;
308 	uint32_t	chm_chanid;
309 	uint32_t	chm_openid;
310 	uint32_t	chm_status;
311 } __packed;
312 
313 /* VMBUS_CHANMSG_TYPE_GPADL_CONN */
314 struct vmbus_chanmsg_gpadl_conn {
315 	struct vmbus_chanmsg_hdr chm_hdr;
316 	uint32_t	chm_chanid;
317 	uint32_t	chm_gpadl;
318 	uint16_t	chm_range_len;
319 	uint16_t	chm_range_cnt;
320 	struct vmbus_gpa_range chm_range;
321 } __packed;
322 
323 #define VMBUS_CHANMSG_GPADL_CONN_PGMAX		26
324 CTASSERT(__offsetof(struct vmbus_chanmsg_gpadl_conn,
325     chm_range.gpa_page[VMBUS_CHANMSG_GPADL_CONN_PGMAX]) <=
326     HYPERCALL_POSTMSGIN_DSIZE_MAX);
327 
328 /* VMBUS_CHANMSG_TYPE_GPADL_SUBCONN */
329 struct vmbus_chanmsg_gpadl_subconn {
330 	struct vmbus_chanmsg_hdr chm_hdr;
331 	uint32_t	chm_msgno;
332 	uint32_t	chm_gpadl;
333 	uint64_t	chm_gpa_page[];
334 } __packed;
335 
336 #define VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX	28
337 CTASSERT(__offsetof(struct vmbus_chanmsg_gpadl_subconn,
338     chm_gpa_page[VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX]) <=
339     HYPERCALL_POSTMSGIN_DSIZE_MAX);
340 
341 /* VMBUS_CHANMSG_TYPE_GPADL_CONNRESP */
342 struct vmbus_chanmsg_gpadl_connresp {
343 	struct vmbus_chanmsg_hdr chm_hdr;
344 	uint32_t	chm_chanid;
345 	uint32_t	chm_gpadl;
346 	uint32_t	chm_status;
347 } __packed;
348 
349 /* VMBUS_CHANMSG_TYPE_CHCLOSE */
350 struct vmbus_chanmsg_chclose {
351 	struct vmbus_chanmsg_hdr chm_hdr;
352 	uint32_t	chm_chanid;
353 } __packed;
354 
355 /* VMBUS_CHANMSG_TYPE_GPADL_DISCONN */
356 struct vmbus_chanmsg_gpadl_disconn {
357 	struct vmbus_chanmsg_hdr chm_hdr;
358 	uint32_t	chm_chanid;
359 	uint32_t	chm_gpadl;
360 } __packed;
361 
362 /* VMBUS_CHANMSG_TYPE_CHFREE */
363 struct vmbus_chanmsg_chfree {
364 	struct vmbus_chanmsg_hdr chm_hdr;
365 	uint32_t	chm_chanid;
366 } __packed;
367 
368 /* VMBUS_CHANMSG_TYPE_CHRESCIND */
369 struct vmbus_chanmsg_chrescind {
370 	struct vmbus_chanmsg_hdr chm_hdr;
371 	uint32_t	chm_chanid;
372 } __packed;
373 
374 /* Size of the user defined data buffer for non-pipe offers */
375 #define VMBUS_CHANMSG_CHOFFER_UDATA_SIZE		120
376 
377 /* Size of the user defined data buffer for pipe offers. */
378 #define VMBUS_CHANMSG_CHOFFER_UDATA_PIPE_SIZE		116
379 
380 /* VMBUS_CHANMSG_TYPE_CHOFFER */
381 struct vmbus_chanmsg_choffer {
382 	struct vmbus_chanmsg_hdr chm_hdr;
383 	struct hyperv_guid chm_chtype;
384 	struct hyperv_guid chm_chinst;
385 	uint64_t	chm_chlat;	/* unit: 100ns */
386 	uint32_t	chm_chrev;
387 	uint32_t	chm_svrctx_sz;
388 	uint16_t	chm_chflags;
389 	uint16_t	chm_mmio_sz;	/* unit: MB */
390 
391 	union {
392 		/* Non-pipes */
393 		struct {
394 			uint8_t	user_def[VMBUS_CHANMSG_CHOFFER_UDATA_SIZE];
395 		} std;
396 		/*
397 		 * Pipes:
398 		 * For integrated pipe protocol, which is implemented on
399 		 * top of standard user-defined data. Pipe clients have
400 		 * VMBUS_CHANMSG_CHOFFER_UDATA_PIPE_SIZE bytes left for
401 		 * their own user.
402 		 */
403 		struct {
404 			uint32_t pipe_mode;
405 			uint8_t
406 			    user_def[VMBUS_CHANMSG_CHOFFER_UDATA_PIPE_SIZE];
407 		} pipe;
408 	} chm_udata;
409 
410 	uint16_t	chm_subidx;
411 	uint16_t	chm_rsvd;
412 	uint32_t	chm_chanid;
413 	uint8_t		chm_montrig;
414 	uint8_t		chm_flags1;	/* VMBUS_CHOFFER_FLAG1_ */
415 	uint16_t	chm_flags2;
416 	uint32_t	chm_connid;
417 } __packed;
418 CTASSERT(sizeof(struct vmbus_chanmsg_choffer) <= VMBUS_MSG_DSIZE_MAX);
419 
420 /* Server Flag */
421 #define VMBUS_CHAN_TLNPI_PROVIDER_OFFER			0x2000
422 
423 #define VMBUS_CHOFFER_FLAG1_HASMNF	0x01
424 
425 #endif	/* !_VMBUS_REG_H_ */
426