xref: /freebsd-12.1/sys/dev/hyperv/include/hyperv.h (revision 7ff1939d)
1 /*-
2  * Copyright (c) 2009-2012,2016 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
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  * $FreeBSD$
29  */
30 
31 /**
32  * HyperV definitions for messages that are sent between instances of the
33  * Channel Management Library in separate partitions, or in some cases,
34  * back to itself.
35  */
36 
37 #ifndef __HYPERV_H__
38 #define __HYPERV_H__
39 
40 #include <sys/param.h>
41 #include <sys/mbuf.h>
42 #include <sys/queue.h>
43 #include <sys/malloc.h>
44 #include <sys/kthread.h>
45 #include <sys/taskqueue.h>
46 #include <sys/systm.h>
47 #include <sys/lock.h>
48 #include <sys/sema.h>
49 #include <sys/smp.h>
50 #include <sys/mutex.h>
51 #include <sys/bus.h>
52 #include <sys/sysctl.h>
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <vm/pmap.h>
56 
57 #include <amd64/include/xen/synch_bitops.h>
58 #include <amd64/include/atomic.h>
59 #include <dev/hyperv/include/hyperv_busdma.h>
60 
61 typedef uint8_t	hv_bool_uint8_t;
62 
63 #define HV_S_OK			0x00000000
64 #define HV_E_FAIL		0x80004005
65 #define HV_ERROR_NOT_SUPPORTED	0x80070032
66 #define HV_ERROR_MACHINE_LOCKED	0x800704F7
67 
68 /*
69  * VMBUS version is 32 bit, upper 16 bit for major_number and lower
70  * 16 bit for minor_number.
71  *
72  * 0.13  --  Windows Server 2008
73  * 1.1   --  Windows 7
74  * 2.4   --  Windows 8
75  * 3.0   --  Windows 8.1
76  */
77 #define VMBUS_VERSION_WS2008		((0 << 16) | (13))
78 #define VMBUS_VERSION_WIN7		((1 << 16) | (1))
79 #define VMBUS_VERSION_WIN8		((2 << 16) | (4))
80 #define VMBUS_VERSION_WIN8_1		((3 << 16) | (0))
81 
82 #define VMBUS_VERSION_MAJOR(ver)	(((uint32_t)(ver)) >> 16)
83 #define VMBUS_VERSION_MINOR(ver)	(((uint32_t)(ver)) & 0xffff)
84 
85 #define HV_MAX_MULTIPAGE_BUFFER_COUNT	32
86 
87 #define HV_ALIGN_UP(value, align)					\
88 		(((value) & (align-1)) ?				\
89 		    (((value) + (align-1)) & ~(align-1) ) : (value))
90 
91 #define HV_ALIGN_DOWN(value, align) ( (value) & ~(align-1) )
92 
93 #define HV_NUM_PAGES_SPANNED(addr, len)					\
94 		((HV_ALIGN_UP(addr+len, PAGE_SIZE) -			\
95 		    HV_ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
96 
97 struct hyperv_guid {
98 	uint8_t		hv_guid[16];
99 } __packed;
100 
101 #define HYPERV_GUID_STRLEN	40
102 
103 int	hyperv_guid2str(const struct hyperv_guid *, char *, size_t);
104 
105 typedef struct {
106 	uint16_t type;
107 	uint16_t data_offset8;
108 	uint16_t length8;
109 	uint16_t flags;
110 	uint64_t transaction_id;
111 } __packed hv_vm_packet_descriptor;
112 
113 typedef struct {
114 	uint32_t byte_count;
115 	uint32_t byte_offset;
116 } __packed hv_vm_transfer_page;
117 
118 typedef struct {
119 	hv_vm_packet_descriptor	d;
120 	uint16_t		transfer_page_set_id;
121 	hv_bool_uint8_t		sender_owns_set;
122 	uint8_t			reserved;
123 	uint32_t		range_count;
124 	hv_vm_transfer_page	ranges[1];
125 } __packed hv_vm_transfer_page_packet_header;
126 
127 typedef enum {
128 	HV_VMBUS_PACKET_TYPE_INVALID				= 0x0,
129 	HV_VMBUS_PACKET_TYPES_SYNCH				= 0x1,
130 	HV_VMBUS_PACKET_TYPE_ADD_TRANSFER_PAGE_SET		= 0x2,
131 	HV_VMBUS_PACKET_TYPE_REMOVE_TRANSFER_PAGE_SET		= 0x3,
132 	HV_VMBUS_PACKET_TYPE_ESTABLISH_GPADL			= 0x4,
133 	HV_VMBUS_PACKET_TYPE_TEAR_DOWN_GPADL			= 0x5,
134 	HV_VMBUS_PACKET_TYPE_DATA_IN_BAND			= 0x6,
135 	HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES		= 0x7,
136 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPADL			= 0x8,
137 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT		= 0x9,
138 	HV_VMBUS_PACKET_TYPE_CANCEL_REQUEST			= 0xa,
139 	HV_VMBUS_PACKET_TYPE_COMPLETION				= 0xb,
140 	HV_VMBUS_PACKET_TYPE_DATA_USING_ADDITIONAL_PACKETS	= 0xc,
141 	HV_VMBUS_PACKET_TYPE_ADDITIONAL_DATA = 0xd
142 } hv_vmbus_packet_type;
143 
144 #define HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED    1
145 
146 #define HW_MACADDR_LEN	6
147 
148 /*
149  * Common defines for Hyper-V ICs
150  */
151 #define HV_ICMSGTYPE_NEGOTIATE		0
152 #define HV_ICMSGTYPE_HEARTBEAT		1
153 #define HV_ICMSGTYPE_KVPEXCHANGE	2
154 #define HV_ICMSGTYPE_SHUTDOWN		3
155 #define HV_ICMSGTYPE_TIMESYNC		4
156 #define HV_ICMSGTYPE_VSS		5
157 
158 #define HV_ICMSGHDRFLAG_TRANSACTION	1
159 #define HV_ICMSGHDRFLAG_REQUEST		2
160 #define HV_ICMSGHDRFLAG_RESPONSE	4
161 
162 typedef struct hv_vmbus_pipe_hdr {
163 	uint32_t flags;
164 	uint32_t msgsize;
165 } __packed hv_vmbus_pipe_hdr;
166 
167 typedef struct hv_vmbus_ic_version {
168 	uint16_t major;
169 	uint16_t minor;
170 } __packed hv_vmbus_ic_version;
171 
172 typedef struct hv_vmbus_icmsg_hdr {
173 	hv_vmbus_ic_version	icverframe;
174 	uint16_t		icmsgtype;
175 	hv_vmbus_ic_version	icvermsg;
176 	uint16_t		icmsgsize;
177 	uint32_t		status;
178 	uint8_t			ictransaction_id;
179 	uint8_t			icflags;
180 	uint8_t			reserved[2];
181 } __packed hv_vmbus_icmsg_hdr;
182 
183 typedef struct hv_vmbus_icmsg_negotiate {
184 	uint16_t		icframe_vercnt;
185 	uint16_t		icmsg_vercnt;
186 	uint32_t		reserved;
187 	hv_vmbus_ic_version	icversion_data[1]; /* any size array */
188 } __packed hv_vmbus_icmsg_negotiate;
189 
190 typedef struct hv_vmbus_shutdown_msg_data {
191 	uint32_t		reason_code;
192 	uint32_t		timeout_seconds;
193 	uint32_t 		flags;
194 	uint8_t			display_message[2048];
195 } __packed hv_vmbus_shutdown_msg_data;
196 
197 typedef struct hv_vmbus_heartbeat_msg_data {
198 	uint64_t 		seq_num;
199 	uint32_t 		reserved[8];
200 } __packed hv_vmbus_heartbeat_msg_data;
201 
202 typedef struct {
203 	/*
204 	 * offset in bytes from the start of ring data below
205 	 */
206 	volatile uint32_t       write_index;
207 	/*
208 	 * offset in bytes from the start of ring data below
209 	 */
210 	volatile uint32_t       read_index;
211 	/*
212 	 * NOTE: The interrupt_mask field is used only for channels, but
213 	 * vmbus connection also uses this data structure
214 	 */
215 	volatile uint32_t       interrupt_mask;
216 	/* pad it to PAGE_SIZE so that data starts on a page */
217 	uint8_t                 reserved[4084];
218 
219 	/*
220 	 * WARNING: Ring data starts here
221 	 *  !!! DO NOT place any fields below this !!!
222 	 */
223 	uint8_t			buffer[0];	/* doubles as interrupt mask */
224 } __packed hv_vmbus_ring_buffer;
225 
226 typedef struct {
227 	int		length;
228 	int		offset;
229 	uint64_t	pfn_array[HV_MAX_MULTIPAGE_BUFFER_COUNT];
230 } __packed hv_vmbus_multipage_buffer;
231 
232 typedef struct {
233 	hv_vmbus_ring_buffer*	ring_buffer;
234 	struct mtx		ring_lock;
235 	uint32_t		ring_data_size;	/* ring_size */
236 } hv_vmbus_ring_buffer_info;
237 
238 typedef void	(*vmbus_chan_callback_t)(void *);
239 
240 typedef struct hv_vmbus_channel {
241 	device_t			ch_dev;
242 	struct vmbus_softc		*vmbus_sc;
243 	uint32_t			ch_flags;	/* VMBUS_CHAN_FLAG_ */
244 	uint32_t			ch_id;		/* channel id */
245 
246 	/*
247 	 * These are based on the offer_msg.monitor_id.
248 	 * Save it here for easy access.
249 	 */
250 	int				ch_montrig_idx;	/* MNF trig index */
251 	uint32_t			ch_montrig_mask;/* MNF trig mask */
252 
253 	/*
254 	 * send to parent
255 	 */
256 	hv_vmbus_ring_buffer_info	outbound;
257 	/*
258 	 * receive from parent
259 	 */
260 	hv_vmbus_ring_buffer_info	inbound;
261 
262 	struct taskqueue		*ch_tq;
263 	struct task			ch_task;
264 	vmbus_chan_callback_t		ch_cb;
265 	void				*ch_cbarg;
266 
267 	struct hyperv_mon_param		*ch_monprm;
268 	struct hyperv_dma		ch_monprm_dma;
269 
270 	int				ch_cpuid;	/* owner cpu */
271 	/*
272 	 * Virtual cpuid for ch_cpuid; it is used to communicate cpuid
273 	 * related information w/ Hyper-V.  If MSR_HV_VP_INDEX does not
274 	 * exist, ch_vcpuid will always be 0 for compatibility.
275 	 */
276 	uint32_t			ch_vcpuid;
277 
278 	/*
279 	 * If this is a primary channel, ch_subchan* fields
280 	 * contain sub-channels belonging to this primary
281 	 * channel.
282 	 */
283 	struct mtx			ch_subchan_lock;
284 	TAILQ_HEAD(, hv_vmbus_channel)	ch_subchans;
285 	int				ch_subchan_cnt;
286 
287 	/* If this is a sub-channel */
288 	TAILQ_ENTRY(hv_vmbus_channel)	ch_sublink;	/* sub-channel link */
289 	struct hv_vmbus_channel		*ch_prichan;	/* owner primary chan */
290 
291 	/*
292 	 * Driver private data
293 	 */
294 	void				*hv_chan_priv1;
295 	void				*hv_chan_priv2;
296 	void				*hv_chan_priv3;
297 
298 	void				*ch_bufring;	/* TX+RX bufrings */
299 	struct hyperv_dma		ch_bufring_dma;
300 	uint32_t			ch_bufring_gpadl;
301 
302 	struct task			ch_detach_task;
303 	TAILQ_ENTRY(hv_vmbus_channel)	ch_prilink;	/* primary chan link */
304 	uint32_t			ch_subidx;	/* subchan index */
305 	volatile uint32_t		ch_stflags;	/* atomic-op */
306 							/* VMBUS_CHAN_ST_ */
307 	struct hyperv_guid		ch_guid_type;
308 	struct hyperv_guid		ch_guid_inst;
309 
310 	struct sysctl_ctx_list		ch_sysctl_ctx;
311 } hv_vmbus_channel;
312 
313 #define VMBUS_CHAN_ISPRIMARY(chan)	((chan)->ch_subidx == 0)
314 
315 #define VMBUS_CHAN_FLAG_HASMNF		0x0001
316 /*
317  * If this flag is set, this channel's interrupt will be masked in ISR,
318  * and the RX bufring will be drained before this channel's interrupt is
319  * unmasked.
320  *
321  * This flag is turned on by default.  Drivers can turn it off according
322  * to their own requirement.
323  */
324 #define VMBUS_CHAN_FLAG_BATCHREAD	0x0002
325 
326 #define VMBUS_CHAN_ST_OPENED_SHIFT	0
327 #define VMBUS_CHAN_ST_OPENED		(1 << VMBUS_CHAN_ST_OPENED_SHIFT)
328 
329 static inline void
330 hv_set_channel_read_state(hv_vmbus_channel* channel, boolean_t on)
331 {
332 	if (!on)
333 		channel->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
334 	else
335 		channel->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
336 }
337 
338 int		hv_vmbus_channel_recv_packet(
339 				hv_vmbus_channel*	channel,
340 				void*			buffer,
341 				uint32_t		buffer_len,
342 				uint32_t*		buffer_actual_len,
343 				uint64_t*		request_id);
344 
345 int		hv_vmbus_channel_recv_packet_raw(
346 				hv_vmbus_channel*	channel,
347 				void*			buffer,
348 				uint32_t		buffer_len,
349 				uint32_t*		buffer_actual_len,
350 				uint64_t*		request_id);
351 
352 int		hv_vmbus_channel_open(
353 				hv_vmbus_channel*	channel,
354 				uint32_t		send_ring_buffer_size,
355 				uint32_t		recv_ring_buffer_size,
356 				void*			user_data,
357 				uint32_t		user_data_len,
358 				vmbus_chan_callback_t	cb,
359 				void			*cbarg);
360 
361 void		hv_vmbus_channel_close(hv_vmbus_channel *channel);
362 
363 int		hv_vmbus_channel_send_packet(
364 				hv_vmbus_channel*	channel,
365 				void*			buffer,
366 				uint32_t		buffer_len,
367 				uint64_t		request_id,
368 				hv_vmbus_packet_type	type,
369 				uint32_t		flags);
370 
371 int		hv_vmbus_channel_send_packet_multipagebuffer(
372 				hv_vmbus_channel*	    channel,
373 				hv_vmbus_multipage_buffer*  multi_page_buffer,
374 				void*			    buffer,
375 				uint32_t		    buffer_len,
376 				uint64_t		    request_id);
377 
378 int		hv_vmbus_channel_establish_gpadl(
379 				hv_vmbus_channel*	channel,
380 				/* must be phys and virt contiguous */
381 				void*			contig_buffer,
382 				/*  page-size multiple	*/
383 				uint32_t		size,
384 				uint32_t*		gpadl_handle);
385 
386 int		hv_vmbus_channel_teardown_gpdal(
387 				hv_vmbus_channel*	channel,
388 				uint32_t		gpadl_handle);
389 
390 int		vmbus_chan_gpadl_connect(struct hv_vmbus_channel *chan,
391 		    bus_addr_t paddr, int size, uint32_t *gpadl);
392 
393 struct hv_vmbus_channel* vmbus_select_outgoing_channel(struct hv_vmbus_channel *promary);
394 
395 void		vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu);
396 void		vmbus_channel_cpu_rr(struct hv_vmbus_channel *chan);
397 struct hv_vmbus_channel **
398 		vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt);
399 void		vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt);
400 void		vmbus_drain_subchan(struct hv_vmbus_channel *pri_chan);
401 
402 /**
403  * @brief Get physical address from virtual
404  */
405 static inline unsigned long
406 hv_get_phys_addr(void *virt)
407 {
408 	unsigned long ret;
409 	ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK));
410 	return (ret);
411 }
412 
413 static __inline struct hv_vmbus_channel *
414 vmbus_get_channel(device_t dev)
415 {
416 	return device_get_ivars(dev);
417 }
418 
419 #endif  /* __HYPERV_H__ */
420