xref: /freebsd-12.1/sys/dev/hyperv/include/hyperv.h (revision 2fb2dbe8)
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_PAGE_BUFFER_COUNT	32
86 #define HV_MAX_MULTIPAGE_BUFFER_COUNT	32
87 
88 #define HV_ALIGN_UP(value, align)					\
89 		(((value) & (align-1)) ?				\
90 		    (((value) + (align-1)) & ~(align-1) ) : (value))
91 
92 #define HV_ALIGN_DOWN(value, align) ( (value) & ~(align-1) )
93 
94 #define HV_NUM_PAGES_SPANNED(addr, len)					\
95 		((HV_ALIGN_UP(addr+len, PAGE_SIZE) -			\
96 		    HV_ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
97 
98 struct hyperv_guid {
99 	uint8_t		hv_guid[16];
100 } __packed;
101 
102 #define HYPERV_GUID_STRLEN	40
103 
104 int	hyperv_guid2str(const struct hyperv_guid *, char *, size_t);
105 
106 typedef struct {
107 	uint16_t type;
108 	uint16_t data_offset8;
109 	uint16_t length8;
110 	uint16_t flags;
111 	uint64_t transaction_id;
112 } __packed hv_vm_packet_descriptor;
113 
114 typedef struct {
115 	uint32_t byte_count;
116 	uint32_t byte_offset;
117 } __packed hv_vm_transfer_page;
118 
119 typedef struct {
120 	hv_vm_packet_descriptor	d;
121 	uint16_t		transfer_page_set_id;
122 	hv_bool_uint8_t		sender_owns_set;
123 	uint8_t			reserved;
124 	uint32_t		range_count;
125 	hv_vm_transfer_page	ranges[1];
126 } __packed hv_vm_transfer_page_packet_header;
127 
128 typedef enum {
129 	HV_VMBUS_PACKET_TYPE_INVALID				= 0x0,
130 	HV_VMBUS_PACKET_TYPES_SYNCH				= 0x1,
131 	HV_VMBUS_PACKET_TYPE_ADD_TRANSFER_PAGE_SET		= 0x2,
132 	HV_VMBUS_PACKET_TYPE_REMOVE_TRANSFER_PAGE_SET		= 0x3,
133 	HV_VMBUS_PACKET_TYPE_ESTABLISH_GPADL			= 0x4,
134 	HV_VMBUS_PACKET_TYPE_TEAR_DOWN_GPADL			= 0x5,
135 	HV_VMBUS_PACKET_TYPE_DATA_IN_BAND			= 0x6,
136 	HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES		= 0x7,
137 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPADL			= 0x8,
138 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT		= 0x9,
139 	HV_VMBUS_PACKET_TYPE_CANCEL_REQUEST			= 0xa,
140 	HV_VMBUS_PACKET_TYPE_COMPLETION				= 0xb,
141 	HV_VMBUS_PACKET_TYPE_DATA_USING_ADDITIONAL_PACKETS	= 0xc,
142 	HV_VMBUS_PACKET_TYPE_ADDITIONAL_DATA = 0xd
143 } hv_vmbus_packet_type;
144 
145 #define HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED    1
146 
147 #define HW_MACADDR_LEN	6
148 
149 /*
150  * Common defines for Hyper-V ICs
151  */
152 #define HV_ICMSGTYPE_NEGOTIATE		0
153 #define HV_ICMSGTYPE_HEARTBEAT		1
154 #define HV_ICMSGTYPE_KVPEXCHANGE	2
155 #define HV_ICMSGTYPE_SHUTDOWN		3
156 #define HV_ICMSGTYPE_TIMESYNC		4
157 #define HV_ICMSGTYPE_VSS		5
158 
159 #define HV_ICMSGHDRFLAG_TRANSACTION	1
160 #define HV_ICMSGHDRFLAG_REQUEST		2
161 #define HV_ICMSGHDRFLAG_RESPONSE	4
162 
163 typedef struct hv_vmbus_pipe_hdr {
164 	uint32_t flags;
165 	uint32_t msgsize;
166 } __packed hv_vmbus_pipe_hdr;
167 
168 typedef struct hv_vmbus_ic_version {
169 	uint16_t major;
170 	uint16_t minor;
171 } __packed hv_vmbus_ic_version;
172 
173 typedef struct hv_vmbus_icmsg_hdr {
174 	hv_vmbus_ic_version	icverframe;
175 	uint16_t		icmsgtype;
176 	hv_vmbus_ic_version	icvermsg;
177 	uint16_t		icmsgsize;
178 	uint32_t		status;
179 	uint8_t			ictransaction_id;
180 	uint8_t			icflags;
181 	uint8_t			reserved[2];
182 } __packed hv_vmbus_icmsg_hdr;
183 
184 typedef struct hv_vmbus_icmsg_negotiate {
185 	uint16_t		icframe_vercnt;
186 	uint16_t		icmsg_vercnt;
187 	uint32_t		reserved;
188 	hv_vmbus_ic_version	icversion_data[1]; /* any size array */
189 } __packed hv_vmbus_icmsg_negotiate;
190 
191 typedef struct hv_vmbus_shutdown_msg_data {
192 	uint32_t		reason_code;
193 	uint32_t		timeout_seconds;
194 	uint32_t 		flags;
195 	uint8_t			display_message[2048];
196 } __packed hv_vmbus_shutdown_msg_data;
197 
198 typedef struct hv_vmbus_heartbeat_msg_data {
199 	uint64_t 		seq_num;
200 	uint32_t 		reserved[8];
201 } __packed hv_vmbus_heartbeat_msg_data;
202 
203 typedef struct {
204 	/*
205 	 * offset in bytes from the start of ring data below
206 	 */
207 	volatile uint32_t       write_index;
208 	/*
209 	 * offset in bytes from the start of ring data below
210 	 */
211 	volatile uint32_t       read_index;
212 	/*
213 	 * NOTE: The interrupt_mask field is used only for channels, but
214 	 * vmbus connection also uses this data structure
215 	 */
216 	volatile uint32_t       interrupt_mask;
217 	/* pad it to PAGE_SIZE so that data starts on a page */
218 	uint8_t                 reserved[4084];
219 
220 	/*
221 	 * WARNING: Ring data starts here
222 	 *  !!! DO NOT place any fields below this !!!
223 	 */
224 	uint8_t			buffer[0];	/* doubles as interrupt mask */
225 } __packed hv_vmbus_ring_buffer;
226 
227 typedef struct {
228 	int		length;
229 	int		offset;
230 	uint64_t	pfn;
231 } __packed hv_vmbus_page_buffer;
232 
233 typedef struct {
234 	int		length;
235 	int		offset;
236 	uint64_t	pfn_array[HV_MAX_MULTIPAGE_BUFFER_COUNT];
237 } __packed hv_vmbus_multipage_buffer;
238 
239 typedef struct {
240 	hv_vmbus_ring_buffer*	ring_buffer;
241 	struct mtx		ring_lock;
242 	uint32_t		ring_data_size;	/* ring_size */
243 } hv_vmbus_ring_buffer_info;
244 
245 typedef void (*hv_vmbus_pfn_channel_callback)(void *context);
246 
247 typedef struct hv_vmbus_channel {
248 	device_t			ch_dev;
249 	struct vmbus_softc		*vmbus_sc;
250 	uint32_t			ch_flags;	/* VMBUS_CHAN_FLAG_ */
251 	uint32_t			ch_id;		/* channel id */
252 
253 	/*
254 	 * These are based on the offer_msg.monitor_id.
255 	 * Save it here for easy access.
256 	 */
257 	int				ch_montrig_idx;	/* MNF trig index */
258 	uint32_t			ch_montrig_mask;/* MNF trig mask */
259 
260 	uint32_t			ring_buffer_gpadl_handle;
261 	/*
262 	 * Allocated memory for ring buffer
263 	 */
264 	void*				ring_buffer_pages;
265 	unsigned long			ring_buffer_size;
266 	uint32_t			ring_buffer_page_count;
267 	/*
268 	 * send to parent
269 	 */
270 	hv_vmbus_ring_buffer_info	outbound;
271 	/*
272 	 * receive from parent
273 	 */
274 	hv_vmbus_ring_buffer_info	inbound;
275 
276 	struct taskqueue *		rxq;
277 	struct task			channel_task;
278 	hv_vmbus_pfn_channel_callback	on_channel_callback;
279 	void*				channel_callback_context;
280 
281 	struct hyperv_mon_param		*ch_monprm;
282 	struct hyperv_dma		ch_monprm_dma;
283 
284 	/*
285 	 * From Win8, this field specifies the target virtual process
286 	 * on which to deliver the interrupt from the host to guest.
287 	 * Before Win8, all channel interrupts would only be
288 	 * delivered on cpu 0. Setting this value to 0 would preserve
289 	 * the earlier behavior.
290 	 */
291 	uint32_t			target_vcpu;
292 	/* The corresponding CPUID in the guest */
293 	uint32_t			target_cpu;
294 
295 	/*
296 	 * If this is a primary channel, ch_subchan* fields
297 	 * contain sub-channels belonging to this primary
298 	 * channel.
299 	 */
300 	struct mtx			ch_subchan_lock;
301 	TAILQ_HEAD(, hv_vmbus_channel)	ch_subchans;
302 	int				ch_subchan_cnt;
303 
304 	/* If this is a sub-channel */
305 	TAILQ_ENTRY(hv_vmbus_channel)	ch_sublink;	/* sub-channel link */
306 	struct hv_vmbus_channel		*ch_prichan;	/* owner primary chan */
307 
308 	/*
309 	 * Driver private data
310 	 */
311 	void				*hv_chan_priv1;
312 	void				*hv_chan_priv2;
313 	void				*hv_chan_priv3;
314 
315 	struct task			ch_detach_task;
316 	TAILQ_ENTRY(hv_vmbus_channel)	ch_prilink;	/* primary chan link */
317 	uint32_t			ch_subidx;	/* subchan index */
318 	volatile uint32_t		ch_stflags;	/* atomic-op */
319 							/* VMBUS_CHAN_ST_ */
320 	struct hyperv_guid		ch_guid_type;
321 	struct hyperv_guid		ch_guid_inst;
322 
323 	struct sysctl_ctx_list		ch_sysctl_ctx;
324 } hv_vmbus_channel;
325 
326 #define VMBUS_CHAN_ISPRIMARY(chan)	((chan)->ch_subidx == 0)
327 
328 #define VMBUS_CHAN_FLAG_HASMNF		0x0001
329 /*
330  * If this flag is set, this channel's interrupt will be masked in ISR,
331  * and the RX bufring will be drained before this channel's interrupt is
332  * unmasked.
333  *
334  * This flag is turned on by default.  Drivers can turn it off according
335  * to their own requirement.
336  */
337 #define VMBUS_CHAN_FLAG_BATCHREAD	0x0002
338 
339 #define VMBUS_CHAN_ST_OPENED_SHIFT	0
340 #define VMBUS_CHAN_ST_OPENED		(1 << VMBUS_CHAN_ST_OPENED_SHIFT)
341 
342 static inline void
343 hv_set_channel_read_state(hv_vmbus_channel* channel, boolean_t on)
344 {
345 	if (!on)
346 		channel->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
347 	else
348 		channel->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
349 }
350 
351 int		hv_vmbus_channel_recv_packet(
352 				hv_vmbus_channel*	channel,
353 				void*			buffer,
354 				uint32_t		buffer_len,
355 				uint32_t*		buffer_actual_len,
356 				uint64_t*		request_id);
357 
358 int		hv_vmbus_channel_recv_packet_raw(
359 				hv_vmbus_channel*	channel,
360 				void*			buffer,
361 				uint32_t		buffer_len,
362 				uint32_t*		buffer_actual_len,
363 				uint64_t*		request_id);
364 
365 int		hv_vmbus_channel_open(
366 				hv_vmbus_channel*	channel,
367 				uint32_t		send_ring_buffer_size,
368 				uint32_t		recv_ring_buffer_size,
369 				void*			user_data,
370 				uint32_t		user_data_len,
371 				hv_vmbus_pfn_channel_callback
372 							pfn_on_channel_callback,
373 				void*			context);
374 
375 void		hv_vmbus_channel_close(hv_vmbus_channel *channel);
376 
377 int		hv_vmbus_channel_send_packet(
378 				hv_vmbus_channel*	channel,
379 				void*			buffer,
380 				uint32_t		buffer_len,
381 				uint64_t		request_id,
382 				hv_vmbus_packet_type	type,
383 				uint32_t		flags);
384 
385 int		hv_vmbus_channel_send_packet_pagebuffer(
386 				hv_vmbus_channel*	channel,
387 				hv_vmbus_page_buffer	page_buffers[],
388 				uint32_t		page_count,
389 				void*			buffer,
390 				uint32_t		buffer_len,
391 				uint64_t		request_id);
392 
393 int		hv_vmbus_channel_send_packet_multipagebuffer(
394 				hv_vmbus_channel*	    channel,
395 				hv_vmbus_multipage_buffer*  multi_page_buffer,
396 				void*			    buffer,
397 				uint32_t		    buffer_len,
398 				uint64_t		    request_id);
399 
400 int		hv_vmbus_channel_establish_gpadl(
401 				hv_vmbus_channel*	channel,
402 				/* must be phys and virt contiguous */
403 				void*			contig_buffer,
404 				/*  page-size multiple	*/
405 				uint32_t		size,
406 				uint32_t*		gpadl_handle);
407 
408 int		hv_vmbus_channel_teardown_gpdal(
409 				hv_vmbus_channel*	channel,
410 				uint32_t		gpadl_handle);
411 
412 struct hv_vmbus_channel* vmbus_select_outgoing_channel(struct hv_vmbus_channel *promary);
413 
414 void		vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu);
415 void		vmbus_channel_cpu_rr(struct hv_vmbus_channel *chan);
416 struct hv_vmbus_channel **
417 		vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt);
418 void		vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt);
419 void		vmbus_drain_subchan(struct hv_vmbus_channel *pri_chan);
420 
421 /**
422  * @brief Get physical address from virtual
423  */
424 static inline unsigned long
425 hv_get_phys_addr(void *virt)
426 {
427 	unsigned long ret;
428 	ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK));
429 	return (ret);
430 }
431 
432 static __inline struct hv_vmbus_channel *
433 vmbus_get_channel(device_t dev)
434 {
435 	return device_get_ivars(dev);
436 }
437 
438 #endif  /* __HYPERV_H__ */
439