xref: /freebsd/sys/dev/hyperv/vmbus/vmbus_reg.h (revision 907b59d76938e654f0d040a888e8dfca3de1e222)
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  * $FreeBSD$
27  */
28 
29 #ifndef _VMBUS_REG_H_
30 #define _VMBUS_REG_H_
31 
32 #include <sys/param.h>
33 #include <dev/hyperv/include/hyperv.h> /* XXX for hyperv_guid */
34 #include <dev/hyperv/include/vmbus.h>
35 #include <dev/hyperv/vmbus/hyperv_reg.h>
36 
37 /*
38  * Hyper-V SynIC message format.
39  */
40 
41 #define VMBUS_MSG_DSIZE_MAX		240
42 #define VMBUS_MSG_SIZE			256
43 
44 struct vmbus_message {
45 	uint32_t	msg_type;	/* HYPERV_MSGTYPE_ */
46 	uint8_t		msg_dsize;	/* data size */
47 	uint8_t		msg_flags;	/* VMBUS_MSGFLAG_ */
48 	uint16_t	msg_rsvd;
49 	uint64_t	msg_id;
50 	uint8_t		msg_data[VMBUS_MSG_DSIZE_MAX];
51 } __packed;
52 CTASSERT(sizeof(struct vmbus_message) == VMBUS_MSG_SIZE);
53 
54 #define VMBUS_MSGFLAG_PENDING		0x01
55 
56 /*
57  * Hyper-V SynIC event flags
58  */
59 
60 #ifdef __LP64__
61 #define VMBUS_EVTFLAGS_MAX	32
62 #define VMBUS_EVTFLAG_SHIFT	6
63 #else
64 #define VMBUS_EVTFLAGS_MAX	64
65 #define VMBUS_EVTFLAG_SHIFT	5
66 #endif
67 #define VMBUS_EVTFLAG_LEN	(1 << VMBUS_EVTFLAG_SHIFT)
68 #define VMBUS_EVTFLAG_MASK	(VMBUS_EVTFLAG_LEN - 1)
69 #define VMBUS_EVTFLAGS_SIZE	256
70 
71 struct vmbus_evtflags {
72 	u_long		evt_flags[VMBUS_EVTFLAGS_MAX];
73 } __packed;
74 CTASSERT(sizeof(struct vmbus_evtflags) == VMBUS_EVTFLAGS_SIZE);
75 
76 /*
77  * Hyper-V Monitor Notification Facility
78  */
79 
80 struct vmbus_mon_trig {
81 	uint32_t	mt_pending;
82 	uint32_t	mt_armed;
83 } __packed;
84 
85 #define VMBUS_MONTRIGS_MAX	4
86 #define VMBUS_MONTRIG_LEN	32
87 
88 struct vmbus_mnf {
89 	uint32_t	mnf_state;
90 	uint32_t	mnf_rsvd1;
91 
92 	struct vmbus_mon_trig mnf_trigs[VMBUS_MONTRIGS_MAX];
93 	uint8_t		mnf_rsvd2[536];
94 
95 	uint16_t	mnf_lat[VMBUS_MONTRIGS_MAX][VMBUS_MONTRIG_LEN];
96 	uint8_t		mnf_rsvd3[256];
97 
98 	struct hyperv_mon_param
99 			mnf_param[VMBUS_MONTRIGS_MAX][VMBUS_MONTRIG_LEN];
100 	uint8_t		mnf_rsvd4[1984];
101 } __packed;
102 CTASSERT(sizeof(struct vmbus_mnf) == PAGE_SIZE);
103 
104 /*
105  * Buffer ring
106  */
107 struct vmbus_bufring {
108 	/*
109 	 * If br_windex == br_rindex, this bufring is empty; this
110 	 * means we can _not_ write data to the bufring, if the
111 	 * write is going to make br_windex same as br_rindex.
112 	 */
113 	volatile uint32_t	br_windex;
114 	volatile uint32_t	br_rindex;
115 
116 	/*
117 	 * Interrupt mask {0,1}
118 	 *
119 	 * For TX bufring, host set this to 1, when it is processing
120 	 * the TX bufring, so that we can safely skip the TX event
121 	 * notification to host.
122 	 *
123 	 * For RX bufring, once this is set to 1 by us, host will not
124 	 * further dispatch interrupts to us, even if there are data
125 	 * pending on the RX bufring.  This effectively disables the
126 	 * interrupt of the channel to which this RX bufring is attached.
127 	 */
128 	volatile uint32_t	br_imask;
129 
130 	uint8_t			br_rsvd[4084];
131 	uint8_t			br_data[];
132 } __packed;
133 CTASSERT(sizeof(struct vmbus_bufring) == PAGE_SIZE);
134 
135 /*
136  * Channel
137  */
138 
139 #define VMBUS_CHAN_MAX_COMPAT	256
140 #define VMBUS_CHAN_MAX		(VMBUS_EVTFLAG_LEN * VMBUS_EVTFLAGS_MAX)
141 
142 /*
143  * Channel packets
144  */
145 
146 #define VMBUS_CHANPKT_SIZE_ALIGN	(1 << VMBUS_CHANPKT_SIZE_SHIFT)
147 
148 #define VMBUS_CHANPKT_SETLEN(pktlen, len)		\
149 do {							\
150 	(pktlen) = (len) >> VMBUS_CHANPKT_SIZE_SHIFT;	\
151 } while (0)
152 
153 #define VMBUS_CHANPKT_TOTLEN(tlen)	\
154 	roundup2((tlen), VMBUS_CHANPKT_SIZE_ALIGN)
155 
156 struct vmbus_chanpkt {
157 	struct vmbus_chanpkt_hdr cp_hdr;
158 } __packed;
159 
160 struct vmbus_chanpkt_sglist {
161 	struct vmbus_chanpkt_hdr cp_hdr;
162 	uint32_t	cp_rsvd;
163 	uint32_t	cp_gpa_cnt;
164 	struct vmbus_gpa cp_gpa[];
165 } __packed;
166 
167 struct vmbus_chanpkt_prplist {
168 	struct vmbus_chanpkt_hdr cp_hdr;
169 	uint32_t	cp_rsvd;
170 	uint32_t	cp_range_cnt;
171 	struct vmbus_gpa_range cp_range[];
172 } __packed;
173 
174 /*
175  * Channel messages
176  * - Embedded in vmbus_message.msg_data, e.g. response and notification.
177  * - Embedded in hypercall_postmsg_in.hc_data, e.g. request.
178  */
179 
180 #define VMBUS_CHANMSG_TYPE_CHOFFER		1	/* NOTE */
181 #define VMBUS_CHANMSG_TYPE_CHRESCIND		2	/* NOTE */
182 #define VMBUS_CHANMSG_TYPE_CHREQUEST		3	/* REQ */
183 #define VMBUS_CHANMSG_TYPE_CHOFFER_DONE		4	/* NOTE */
184 #define VMBUS_CHANMSG_TYPE_CHOPEN		5	/* REQ */
185 #define VMBUS_CHANMSG_TYPE_CHOPEN_RESP		6	/* RESP */
186 #define VMBUS_CHANMSG_TYPE_CHCLOSE		7	/* REQ */
187 #define VMBUS_CHANMSG_TYPE_GPADL_CONN		8	/* REQ */
188 #define VMBUS_CHANMSG_TYPE_GPADL_SUBCONN	9	/* REQ */
189 #define VMBUS_CHANMSG_TYPE_GPADL_CONNRESP	10	/* RESP */
190 #define VMBUS_CHANMSG_TYPE_GPADL_DISCONN	11	/* REQ */
191 #define VMBUS_CHANMSG_TYPE_GPADL_DISCONNRESP	12	/* RESP */
192 #define VMBUS_CHANMSG_TYPE_CHFREE		13	/* REQ */
193 #define VMBUS_CHANMSG_TYPE_CONNECT		14	/* REQ */
194 #define VMBUS_CHANMSG_TYPE_CONNECT_RESP		15	/* RESP */
195 #define VMBUS_CHANMSG_TYPE_DISCONNECT		16	/* REQ */
196 #define VMBUS_CHANMSG_TYPE_MAX			22
197 
198 struct vmbus_chanmsg_hdr {
199 	uint32_t	chm_type;	/* VMBUS_CHANMSG_TYPE_ */
200 	uint32_t	chm_rsvd;
201 } __packed;
202 
203 /* VMBUS_CHANMSG_TYPE_CONNECT */
204 struct vmbus_chanmsg_connect {
205 	struct vmbus_chanmsg_hdr chm_hdr;
206 	uint32_t	chm_ver;
207 	uint32_t	chm_rsvd;
208 	uint64_t	chm_evtflags;
209 	uint64_t	chm_mnf1;
210 	uint64_t	chm_mnf2;
211 } __packed;
212 
213 /* VMBUS_CHANMSG_TYPE_CONNECT_RESP */
214 struct vmbus_chanmsg_connect_resp {
215 	struct vmbus_chanmsg_hdr chm_hdr;
216 	uint8_t		chm_done;
217 } __packed;
218 
219 /* VMBUS_CHANMSG_TYPE_CHREQUEST */
220 struct vmbus_chanmsg_chrequest {
221 	struct vmbus_chanmsg_hdr chm_hdr;
222 } __packed;
223 
224 /* VMBUS_CHANMSG_TYPE_DISCONNECT */
225 struct vmbus_chanmsg_disconnect {
226 	struct vmbus_chanmsg_hdr chm_hdr;
227 } __packed;
228 
229 /* VMBUS_CHANMSG_TYPE_CHOPEN */
230 struct vmbus_chanmsg_chopen {
231 	struct vmbus_chanmsg_hdr chm_hdr;
232 	uint32_t	chm_chanid;
233 	uint32_t	chm_openid;
234 	uint32_t	chm_gpadl;
235 	uint32_t	chm_vcpuid;
236 	uint32_t	chm_txbr_pgcnt;
237 #define VMBUS_CHANMSG_CHOPEN_UDATA_SIZE	120
238 	uint8_t		chm_udata[VMBUS_CHANMSG_CHOPEN_UDATA_SIZE];
239 } __packed;
240 
241 /* VMBUS_CHANMSG_TYPE_CHOPEN_RESP */
242 struct vmbus_chanmsg_chopen_resp {
243 	struct vmbus_chanmsg_hdr chm_hdr;
244 	uint32_t	chm_chanid;
245 	uint32_t	chm_openid;
246 	uint32_t	chm_status;
247 } __packed;
248 
249 /* VMBUS_CHANMSG_TYPE_GPADL_CONN */
250 struct vmbus_chanmsg_gpadl_conn {
251 	struct vmbus_chanmsg_hdr chm_hdr;
252 	uint32_t	chm_chanid;
253 	uint32_t	chm_gpadl;
254 	uint16_t	chm_range_len;
255 	uint16_t	chm_range_cnt;
256 	struct vmbus_gpa_range chm_range;
257 } __packed;
258 
259 #define VMBUS_CHANMSG_GPADL_CONN_PGMAX		26
260 CTASSERT(__offsetof(struct vmbus_chanmsg_gpadl_conn,
261     chm_range.gpa_page[VMBUS_CHANMSG_GPADL_CONN_PGMAX]) <=
262     HYPERCALL_POSTMSGIN_DSIZE_MAX);
263 
264 /* VMBUS_CHANMSG_TYPE_GPADL_SUBCONN */
265 struct vmbus_chanmsg_gpadl_subconn {
266 	struct vmbus_chanmsg_hdr chm_hdr;
267 	uint32_t	chm_msgno;
268 	uint32_t	chm_gpadl;
269 	uint64_t	chm_gpa_page[];
270 } __packed;
271 
272 #define VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX	28
273 CTASSERT(__offsetof(struct vmbus_chanmsg_gpadl_subconn,
274     chm_gpa_page[VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX]) <=
275     HYPERCALL_POSTMSGIN_DSIZE_MAX);
276 
277 /* VMBUS_CHANMSG_TYPE_GPADL_CONNRESP */
278 struct vmbus_chanmsg_gpadl_connresp {
279 	struct vmbus_chanmsg_hdr chm_hdr;
280 	uint32_t	chm_chanid;
281 	uint32_t	chm_gpadl;
282 	uint32_t	chm_status;
283 } __packed;
284 
285 /* VMBUS_CHANMSG_TYPE_CHCLOSE */
286 struct vmbus_chanmsg_chclose {
287 	struct vmbus_chanmsg_hdr chm_hdr;
288 	uint32_t	chm_chanid;
289 } __packed;
290 
291 /* VMBUS_CHANMSG_TYPE_GPADL_DISCONN */
292 struct vmbus_chanmsg_gpadl_disconn {
293 	struct vmbus_chanmsg_hdr chm_hdr;
294 	uint32_t	chm_chanid;
295 	uint32_t	chm_gpadl;
296 } __packed;
297 
298 /* VMBUS_CHANMSG_TYPE_CHFREE */
299 struct vmbus_chanmsg_chfree {
300 	struct vmbus_chanmsg_hdr chm_hdr;
301 	uint32_t	chm_chanid;
302 } __packed;
303 
304 /* VMBUS_CHANMSG_TYPE_CHRESCIND */
305 struct vmbus_chanmsg_chrescind {
306 	struct vmbus_chanmsg_hdr chm_hdr;
307 	uint32_t	chm_chanid;
308 } __packed;
309 
310 /* VMBUS_CHANMSG_TYPE_CHOFFER */
311 struct vmbus_chanmsg_choffer {
312 	struct vmbus_chanmsg_hdr chm_hdr;
313 	struct hyperv_guid chm_chtype;
314 	struct hyperv_guid chm_chinst;
315 	uint64_t	chm_chlat;	/* unit: 100ns */
316 	uint32_t	chm_chrev;
317 	uint32_t	chm_svrctx_sz;
318 	uint16_t	chm_chflags;
319 	uint16_t	chm_mmio_sz;	/* unit: MB */
320 	uint8_t		chm_udata[120];
321 	uint16_t	chm_subidx;
322 	uint16_t	chm_rsvd;
323 	uint32_t	chm_chanid;
324 	uint8_t		chm_montrig;
325 	uint8_t		chm_flags1;	/* VMBUS_CHOFFER_FLAG1_ */
326 	uint16_t	chm_flags2;
327 	uint32_t	chm_connid;
328 } __packed;
329 CTASSERT(sizeof(struct vmbus_chanmsg_choffer) <= VMBUS_MSG_DSIZE_MAX);
330 
331 #define VMBUS_CHOFFER_FLAG1_HASMNF	0x01
332 
333 #endif	/* !_VMBUS_REG_H_ */
334