xref: /freebsd/sys/dev/hyperv/include/hyperv.h (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*-
2  * Copyright (c) 2009-2012 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 <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/pmap.h>
55 
56 #include <amd64/include/xen/synch_bitops.h>
57 #include <amd64/include/atomic.h>
58 
59 typedef uint8_t	hv_bool_uint8_t;
60 
61 #define HV_S_OK			0x00000000
62 #define HV_E_FAIL		0x80004005
63 #define HV_ERROR_NOT_SUPPORTED	0x80070032
64 #define HV_ERROR_MACHINE_LOCKED	0x800704F7
65 
66 /*
67  * VMBUS version is 32 bit, upper 16 bit for major_number and lower
68  * 16 bit for minor_number.
69  *
70  * 0.13  --  Windows Server 2008
71  * 1.1   --  Windows 7
72  * 2.4   --  Windows 8
73  * 3.0   --  Windows 8.1
74  */
75 #define HV_VMBUS_VERSION_WS2008		((0 << 16) | (13))
76 #define HV_VMBUS_VERSION_WIN7		((1 << 16) | (1))
77 #define HV_VMBUS_VERSION_WIN8		((2 << 16) | (4))
78 #define HV_VMBUS_VERSION_WIN8_1		((3 << 16) | (0))
79 
80 #define HV_VMBUS_VERSION_INVALID	-1
81 
82 #define HV_VMBUS_VERSION_CURRENT	HV_VMBUS_VERSION_WIN8_1
83 
84 /*
85  * Make maximum size of pipe payload of 16K
86  */
87 
88 #define HV_MAX_PIPE_DATA_PAYLOAD	(sizeof(BYTE) * 16384)
89 
90 /*
91  * Define pipe_mode values
92  */
93 
94 #define HV_VMBUS_PIPE_TYPE_BYTE		0x00000000
95 #define HV_VMBUS_PIPE_TYPE_MESSAGE	0x00000004
96 
97 /*
98  * The size of the user defined data buffer for non-pipe offers
99  */
100 
101 #define HV_MAX_USER_DEFINED_BYTES	120
102 
103 /*
104  *  The size of the user defined data buffer for pipe offers
105  */
106 
107 #define HV_MAX_PIPE_USER_DEFINED_BYTES	116
108 
109 
110 #define HV_MAX_PAGE_BUFFER_COUNT	32
111 #define HV_MAX_MULTIPAGE_BUFFER_COUNT	32
112 
113 #define HV_ALIGN_UP(value, align)					\
114 		(((value) & (align-1)) ?				\
115 		    (((value) + (align-1)) & ~(align-1) ) : (value))
116 
117 #define HV_ALIGN_DOWN(value, align) ( (value) & ~(align-1) )
118 
119 #define HV_NUM_PAGES_SPANNED(addr, len)					\
120 		((HV_ALIGN_UP(addr+len, PAGE_SIZE) -			\
121 		    HV_ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
122 
123 typedef struct hv_guid {
124 	 unsigned char data[16];
125 } __packed hv_guid;
126 
127 int snprintf_hv_guid(char *, size_t, const hv_guid *);
128 
129 #define HV_NIC_GUID							\
130 	.data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,	\
131 		0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
132 
133 #define HV_IDE_GUID							\
134 	.data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,	\
135 		 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
136 
137 #define HV_SCSI_GUID							\
138 	.data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,	\
139 		 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
140 
141 /*
142  * At the center of the Channel Management library is
143  * the Channel Offer. This struct contains the
144  * fundamental information about an offer.
145  */
146 
147 typedef struct hv_vmbus_channel_offer {
148 	hv_guid		interface_type;
149 	hv_guid		interface_instance;
150 	uint64_t	interrupt_latency_in_100ns_units;
151 	uint32_t	interface_revision;
152 	uint32_t	server_context_area_size; /* in bytes */
153 	uint16_t	channel_flags;
154 	uint16_t	mmio_megabytes;		  /* in bytes * 1024 * 1024 */
155 	union
156 	{
157         /*
158          * Non-pipes: The user has HV_MAX_USER_DEFINED_BYTES bytes.
159          */
160 		struct {
161 			uint8_t	user_defined[HV_MAX_USER_DEFINED_BYTES];
162 		} __packed standard;
163 
164         /*
165          * Pipes: The following structure is an integrated pipe protocol, which
166          *        is implemented on top of standard user-defined data. pipe
167          *        clients  have HV_MAX_PIPE_USER_DEFINED_BYTES left for their
168          *        own use.
169          */
170 		struct {
171 			uint32_t	pipe_mode;
172 			uint8_t	user_defined[HV_MAX_PIPE_USER_DEFINED_BYTES];
173 		} __packed pipe;
174 	} u;
175 
176 	/*
177 	 * Sub_channel_index, newly added in Win8.
178 	 */
179 	uint16_t	sub_channel_index;
180 	uint16_t	padding;
181 
182 } __packed hv_vmbus_channel_offer;
183 
184 typedef uint32_t hv_gpadl_handle;
185 
186 typedef struct {
187 	uint16_t type;
188 	uint16_t data_offset8;
189 	uint16_t length8;
190 	uint16_t flags;
191 	uint64_t transaction_id;
192 } __packed hv_vm_packet_descriptor;
193 
194 typedef uint32_t hv_previous_packet_offset;
195 
196 typedef struct {
197 	hv_previous_packet_offset	previous_packet_start_offset;
198 	hv_vm_packet_descriptor		descriptor;
199 } __packed hv_vm_packet_header;
200 
201 typedef struct {
202 	uint32_t byte_count;
203 	uint32_t byte_offset;
204 } __packed hv_vm_transfer_page;
205 
206 typedef struct {
207 	hv_vm_packet_descriptor	d;
208 	uint16_t		transfer_page_set_id;
209 	hv_bool_uint8_t		sender_owns_set;
210 	uint8_t			reserved;
211 	uint32_t		range_count;
212 	hv_vm_transfer_page	ranges[1];
213 } __packed hv_vm_transfer_page_packet_header;
214 
215 typedef struct {
216 	hv_vm_packet_descriptor	d;
217 	uint32_t		gpadl;
218 	uint32_t		reserved;
219 } __packed hv_vm_gpadl_packet_header;
220 
221 typedef struct {
222 	hv_vm_packet_descriptor	d;
223 	uint32_t		gpadl;
224 	uint16_t		transfer_page_set_id;
225 	uint16_t		reserved;
226 } __packed hv_vm_add_remove_transfer_page_set;
227 
228 /*
229  * This structure defines a range in guest
230  * physical space that can be made
231  * to look virtually contiguous.
232  */
233 
234 typedef struct {
235 	uint32_t byte_count;
236 	uint32_t byte_offset;
237 	uint64_t pfn_array[0];
238 } __packed hv_gpa_range;
239 
240 /*
241  * This is the format for an Establish Gpadl packet, which contains a handle
242  * by which this GPADL will be known and a set of GPA ranges associated with
243  * it.  This can be converted to a MDL by the guest OS.  If there are multiple
244  * GPA ranges, then the resulting MDL will be "chained," representing multiple
245  * VA ranges.
246  */
247 
248 typedef struct {
249 	hv_vm_packet_descriptor	d;
250 	uint32_t		gpadl;
251 	uint32_t		range_count;
252 	hv_gpa_range		range[1];
253 } __packed hv_vm_establish_gpadl;
254 
255 /*
256  * This is the format for a Teardown Gpadl packet, which indicates that the
257  * GPADL handle in the Establish Gpadl packet will never be referenced again.
258  */
259 
260 typedef struct {
261 	hv_vm_packet_descriptor	d;
262 	uint32_t		gpadl;
263 				/* for alignment to a 8-byte boundary */
264 	uint32_t		reserved;
265 } __packed hv_vm_teardown_gpadl;
266 
267 /*
268  * This is the format for a GPA-Direct packet, which contains a set of GPA
269  * ranges, in addition to commands and/or data.
270  */
271 
272 typedef struct {
273 	hv_vm_packet_descriptor	d;
274 	uint32_t		reserved;
275 	uint32_t		range_count;
276 	hv_gpa_range		range[1];
277 } __packed hv_vm_data_gpa_direct;
278 
279 /*
280  * This is the format for a Additional data Packet.
281  */
282 typedef struct {
283 	hv_vm_packet_descriptor	d;
284 	uint64_t		total_bytes;
285 	uint32_t		byte_offset;
286 	uint32_t		byte_count;
287 	uint8_t			data[1];
288 } __packed hv_vm_additional_data;
289 
290 typedef union {
291 	hv_vm_packet_descriptor             simple_header;
292 	hv_vm_transfer_page_packet_header   transfer_page_header;
293 	hv_vm_gpadl_packet_header           gpadl_header;
294 	hv_vm_add_remove_transfer_page_set  add_remove_transfer_page_header;
295 	hv_vm_establish_gpadl               establish_gpadl_header;
296 	hv_vm_teardown_gpadl                teardown_gpadl_header;
297 	hv_vm_data_gpa_direct               data_gpa_direct_header;
298 } __packed hv_vm_packet_largest_possible_header;
299 
300 typedef enum {
301 	HV_VMBUS_PACKET_TYPE_INVALID				= 0x0,
302 	HV_VMBUS_PACKET_TYPES_SYNCH				= 0x1,
303 	HV_VMBUS_PACKET_TYPE_ADD_TRANSFER_PAGE_SET		= 0x2,
304 	HV_VMBUS_PACKET_TYPE_REMOVE_TRANSFER_PAGE_SET		= 0x3,
305 	HV_VMBUS_PACKET_TYPE_ESTABLISH_GPADL			= 0x4,
306 	HV_VMBUS_PACKET_TYPE_TEAR_DOWN_GPADL			= 0x5,
307 	HV_VMBUS_PACKET_TYPE_DATA_IN_BAND			= 0x6,
308 	HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES		= 0x7,
309 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPADL			= 0x8,
310 	HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT		= 0x9,
311 	HV_VMBUS_PACKET_TYPE_CANCEL_REQUEST			= 0xa,
312 	HV_VMBUS_PACKET_TYPE_COMPLETION				= 0xb,
313 	HV_VMBUS_PACKET_TYPE_DATA_USING_ADDITIONAL_PACKETS	= 0xc,
314 	HV_VMBUS_PACKET_TYPE_ADDITIONAL_DATA = 0xd
315 } hv_vmbus_packet_type;
316 
317 #define HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED    1
318 
319 /*
320  * Version 1 messages
321  */
322 typedef enum {
323 	HV_CHANNEL_MESSAGE_INVALID			= 0,
324 	HV_CHANNEL_MESSAGE_OFFER_CHANNEL		= 1,
325 	HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER	= 2,
326 	HV_CHANNEL_MESSAGE_REQUEST_OFFERS		= 3,
327 	HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED		= 4,
328 	HV_CHANNEL_MESSAGE_OPEN_CHANNEL			= 5,
329 	HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT		= 6,
330 	HV_CHANNEL_MESSAGE_CLOSE_CHANNEL		= 7,
331 	HV_CHANNEL_MESSAGEL_GPADL_HEADER		= 8,
332 	HV_CHANNEL_MESSAGE_GPADL_BODY			= 9,
333 	HV_CHANNEL_MESSAGE_GPADL_CREATED		= 10,
334 	HV_CHANNEL_MESSAGE_GPADL_TEARDOWN		= 11,
335 	HV_CHANNEL_MESSAGE_GPADL_TORNDOWN		= 12,
336 	HV_CHANNEL_MESSAGE_REL_ID_RELEASED		= 13,
337 	HV_CHANNEL_MESSAGE_INITIATED_CONTACT		= 14,
338 	HV_CHANNEL_MESSAGE_VERSION_RESPONSE		= 15,
339 	HV_CHANNEL_MESSAGE_UNLOAD			= 16,
340 	HV_CHANNEL_MESSAGE_COUNT
341 } hv_vmbus_channel_msg_type;
342 
343 typedef struct {
344 	hv_vmbus_channel_msg_type	message_type;
345 	uint32_t			padding;
346 } __packed hv_vmbus_channel_msg_header;
347 
348 /*
349  * Query VMBus Version parameters
350  */
351 typedef struct {
352 	hv_vmbus_channel_msg_header	header;
353 	uint32_t			version;
354 } __packed hv_vmbus_channel_query_vmbus_version;
355 
356 /*
357  * VMBus Version Supported parameters
358  */
359 typedef struct {
360 	hv_vmbus_channel_msg_header	header;
361 	hv_bool_uint8_t			version_supported;
362 } __packed hv_vmbus_channel_version_supported;
363 
364 /*
365  * Channel Offer parameters
366  */
367 typedef struct {
368 	hv_vmbus_channel_msg_header	header;
369 	hv_vmbus_channel_offer		offer;
370 	uint32_t			child_rel_id;
371 	uint8_t				monitor_id;
372 	/*
373 	 * This field has been split into a bit field on Win7
374 	 * and higher.
375 	 */
376 	uint8_t				monitor_allocated:1;
377 	uint8_t				reserved:7;
378 	/*
379 	 * Following fields were added in win7 and higher.
380 	 * Make sure to check the version before accessing these fields.
381 	 *
382 	 * If "is_dedicated_interrupt" is set, we must not set the
383 	 * associated bit in the channel bitmap while sending the
384 	 * interrupt to the host.
385 	 *
386 	 * connection_id is used in signaling the host.
387 	 */
388 	uint16_t			is_dedicated_interrupt:1;
389 	uint16_t			reserved1:15;
390 	uint32_t			connection_id;
391 } __packed hv_vmbus_channel_offer_channel;
392 
393 /*
394  * Rescind Offer parameters
395  */
396 typedef struct
397 {
398     hv_vmbus_channel_msg_header	header;
399     uint32_t			child_rel_id;
400 } __packed hv_vmbus_channel_rescind_offer;
401 
402 
403 /*
404  * Request Offer -- no parameters, SynIC message contains the partition ID
405  *
406  * Set Snoop -- no parameters, SynIC message contains the partition ID
407  *
408  * Clear Snoop -- no parameters, SynIC message contains the partition ID
409  *
410  * All Offers Delivered -- no parameters, SynIC message contains the
411  * partition ID
412  *
413  * Flush Client -- no parameters, SynIC message contains the partition ID
414  */
415 
416 
417 /*
418  * Open Channel parameters
419  */
420 typedef struct
421 {
422     hv_vmbus_channel_msg_header header;
423 
424     /*
425      * Identifies the specific VMBus channel that is being opened.
426      */
427     uint32_t		child_rel_id;
428 
429     /*
430      * ID making a particular open request at a channel offer unique.
431      */
432     uint32_t		open_id;
433 
434     /*
435      * GPADL for the channel's ring buffer.
436      */
437     hv_gpadl_handle	ring_buffer_gpadl_handle;
438 
439     /*
440      * Before win8, all incoming channel interrupts are only
441      * delivered on cpu 0. Setting this value to 0 would
442      * preserve the earlier behavior.
443      */
444     uint32_t		target_vcpu;
445 
446     /*
447      * The upstream ring buffer begins at offset zero in the memory described
448      * by ring_buffer_gpadl_handle. The downstream ring buffer follows it at
449      * this offset (in pages).
450      */
451     uint32_t		downstream_ring_buffer_page_offset;
452 
453     /*
454      * User-specific data to be passed along to the server endpoint.
455      */
456     uint8_t		user_data[HV_MAX_USER_DEFINED_BYTES];
457 
458 } __packed hv_vmbus_channel_open_channel;
459 
460 typedef uint32_t hv_nt_status;
461 
462 /*
463  * Open Channel Result parameters
464  */
465 typedef struct
466 {
467 	hv_vmbus_channel_msg_header	header;
468 	uint32_t			child_rel_id;
469 	uint32_t			open_id;
470 	hv_nt_status			status;
471 } __packed hv_vmbus_channel_open_result;
472 
473 /*
474  * Close channel parameters
475  */
476 typedef struct
477 {
478 	hv_vmbus_channel_msg_header	header;
479 	uint32_t			child_rel_id;
480 } __packed hv_vmbus_channel_close_channel;
481 
482 /*
483  * Channel Message GPADL
484  */
485 #define HV_GPADL_TYPE_RING_BUFFER	1
486 #define HV_GPADL_TYPE_SERVER_SAVE_AREA	2
487 #define HV_GPADL_TYPE_TRANSACTION	8
488 
489 /*
490  * The number of PFNs in a GPADL message is defined by the number of pages
491  * that would be spanned by byte_count and byte_offset.  If the implied number
492  * of PFNs won't fit in this packet, there will be a follow-up packet that
493  * contains more
494  */
495 
496 typedef struct {
497 	hv_vmbus_channel_msg_header	header;
498 	uint32_t			child_rel_id;
499 	uint32_t			gpadl;
500 	uint16_t			range_buf_len;
501 	uint16_t			range_count;
502 	hv_gpa_range			range[0];
503 } __packed hv_vmbus_channel_gpadl_header;
504 
505 /*
506  * This is the follow-up packet that contains more PFNs
507  */
508 typedef struct {
509 	hv_vmbus_channel_msg_header	header;
510 	uint32_t			message_number;
511 	uint32_t 			gpadl;
512 	uint64_t 			pfn[0];
513 } __packed hv_vmbus_channel_gpadl_body;
514 
515 typedef struct {
516 	hv_vmbus_channel_msg_header	header;
517 	uint32_t			child_rel_id;
518 	uint32_t			gpadl;
519 	uint32_t			creation_status;
520 } __packed hv_vmbus_channel_gpadl_created;
521 
522 typedef struct {
523 	hv_vmbus_channel_msg_header	header;
524 	uint32_t			child_rel_id;
525 	uint32_t			gpadl;
526 } __packed hv_vmbus_channel_gpadl_teardown;
527 
528 typedef struct {
529 	hv_vmbus_channel_msg_header	header;
530 	uint32_t			gpadl;
531 } __packed hv_vmbus_channel_gpadl_torndown;
532 
533 typedef struct {
534 	hv_vmbus_channel_msg_header	header;
535 	uint32_t			child_rel_id;
536 } __packed hv_vmbus_channel_relid_released;
537 
538 typedef struct {
539 	hv_vmbus_channel_msg_header	header;
540 	uint32_t			vmbus_version_requested;
541 	uint32_t			padding2;
542 	uint64_t			interrupt_page;
543 	uint64_t			monitor_page_1;
544 	uint64_t			monitor_page_2;
545 } __packed hv_vmbus_channel_initiate_contact;
546 
547 typedef struct {
548 	hv_vmbus_channel_msg_header header;
549 	hv_bool_uint8_t		version_supported;
550 } __packed hv_vmbus_channel_version_response;
551 
552 typedef hv_vmbus_channel_msg_header hv_vmbus_channel_unload;
553 
554 #define HW_MACADDR_LEN	6
555 
556 /*
557  * Fixme:  Added to quiet "typeof" errors involving hv_vmbus.h when
558  * the including C file was compiled with "-std=c99".
559  */
560 #ifndef typeof
561 #define typeof __typeof
562 #endif
563 
564 #ifndef NULL
565 #define NULL  (void *)0
566 #endif
567 
568 typedef void *hv_vmbus_handle;
569 
570 #ifndef CONTAINING_RECORD
571 #define CONTAINING_RECORD(address, type, field) ((type *)(	\
572 		(uint8_t *)(address) -				\
573 		(uint8_t *)(&((type *)0)->field)))
574 #endif /* CONTAINING_RECORD */
575 
576 
577 #define container_of(ptr, type, member) ({				\
578 		__typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
579 		(type *)( (char *)__mptr - offsetof(type,member) );})
580 
581 enum {
582 	HV_VMBUS_IVAR_TYPE,
583 	HV_VMBUS_IVAR_INSTANCE,
584 	HV_VMBUS_IVAR_NODE,
585 	HV_VMBUS_IVAR_DEVCTX
586 };
587 
588 #define HV_VMBUS_ACCESSOR(var, ivar, type) \
589 		__BUS_ACCESSOR(vmbus, var, HV_VMBUS, ivar, type)
590 
591 HV_VMBUS_ACCESSOR(type, TYPE,  const char *)
592 HV_VMBUS_ACCESSOR(devctx, DEVCTX,  struct hv_device *)
593 
594 
595 /*
596  * Common defines for Hyper-V ICs
597  */
598 #define HV_ICMSGTYPE_NEGOTIATE		0
599 #define HV_ICMSGTYPE_HEARTBEAT		1
600 #define HV_ICMSGTYPE_KVPEXCHANGE	2
601 #define HV_ICMSGTYPE_SHUTDOWN		3
602 #define HV_ICMSGTYPE_TIMESYNC		4
603 #define HV_ICMSGTYPE_VSS		5
604 
605 #define HV_ICMSGHDRFLAG_TRANSACTION	1
606 #define HV_ICMSGHDRFLAG_REQUEST		2
607 #define HV_ICMSGHDRFLAG_RESPONSE	4
608 
609 typedef struct hv_vmbus_pipe_hdr {
610 	uint32_t flags;
611 	uint32_t msgsize;
612 } __packed hv_vmbus_pipe_hdr;
613 
614 typedef struct hv_vmbus_ic_version {
615 	uint16_t major;
616 	uint16_t minor;
617 } __packed hv_vmbus_ic_version;
618 
619 typedef struct hv_vmbus_icmsg_hdr {
620 	hv_vmbus_ic_version	icverframe;
621 	uint16_t		icmsgtype;
622 	hv_vmbus_ic_version	icvermsg;
623 	uint16_t		icmsgsize;
624 	uint32_t		status;
625 	uint8_t			ictransaction_id;
626 	uint8_t			icflags;
627 	uint8_t			reserved[2];
628 } __packed hv_vmbus_icmsg_hdr;
629 
630 typedef struct hv_vmbus_icmsg_negotiate {
631 	uint16_t		icframe_vercnt;
632 	uint16_t		icmsg_vercnt;
633 	uint32_t		reserved;
634 	hv_vmbus_ic_version	icversion_data[1]; /* any size array */
635 } __packed hv_vmbus_icmsg_negotiate;
636 
637 typedef struct hv_vmbus_shutdown_msg_data {
638 	uint32_t		reason_code;
639 	uint32_t		timeout_seconds;
640 	uint32_t 		flags;
641 	uint8_t			display_message[2048];
642 } __packed hv_vmbus_shutdown_msg_data;
643 
644 typedef struct hv_vmbus_heartbeat_msg_data {
645 	uint64_t 		seq_num;
646 	uint32_t 		reserved[8];
647 } __packed hv_vmbus_heartbeat_msg_data;
648 
649 typedef struct {
650 	/*
651 	 * offset in bytes from the start of ring data below
652 	 */
653 	volatile uint32_t       write_index;
654 	/*
655 	 * offset in bytes from the start of ring data below
656 	 */
657 	volatile uint32_t       read_index;
658 	/*
659 	 * NOTE: The interrupt_mask field is used only for channels, but
660 	 * vmbus connection also uses this data structure
661 	 */
662 	volatile uint32_t       interrupt_mask;
663 	/* pad it to PAGE_SIZE so that data starts on a page */
664 	uint8_t                 reserved[4084];
665 
666 	/*
667 	 * WARNING: Ring data starts here + ring_data_start_offset
668 	 *  !!! DO NOT place any fields below this !!!
669 	 */
670 	uint8_t			buffer[0];	/* doubles as interrupt mask */
671 } __packed hv_vmbus_ring_buffer;
672 
673 typedef struct {
674 	int		length;
675 	int		offset;
676 	uint64_t	pfn;
677 } __packed hv_vmbus_page_buffer;
678 
679 typedef struct {
680 	int		length;
681 	int		offset;
682 	uint64_t	pfn_array[HV_MAX_MULTIPAGE_BUFFER_COUNT];
683 } __packed hv_vmbus_multipage_buffer;
684 
685 typedef struct {
686 	hv_vmbus_ring_buffer*	ring_buffer;
687 	uint32_t		ring_size;	/* Include the shared header */
688 	struct mtx		ring_lock;
689 	uint32_t		ring_data_size;	/* ring_size */
690 	uint32_t		ring_data_start_offset;
691 } hv_vmbus_ring_buffer_info;
692 
693 typedef void (*hv_vmbus_pfn_channel_callback)(void *context);
694 typedef void (*hv_vmbus_sc_creation_callback)(void *context);
695 
696 typedef enum {
697 	HV_CHANNEL_OFFER_STATE,
698 	HV_CHANNEL_OPENING_STATE,
699 	HV_CHANNEL_OPEN_STATE,
700 	HV_CHANNEL_OPENED_STATE,
701 	HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE,
702 } hv_vmbus_channel_state;
703 
704 /*
705  *  Connection identifier type
706  */
707 typedef union {
708 	uint32_t		as_uint32_t;
709 	struct {
710 		uint32_t	id:24;
711 		uint32_t	reserved:8;
712 	} u;
713 
714 } __packed hv_vmbus_connection_id;
715 
716 /*
717  * Definition of the hv_vmbus_signal_event hypercall input structure
718  */
719 typedef struct {
720 	hv_vmbus_connection_id	connection_id;
721 	uint16_t		flag_number;
722 	uint16_t		rsvd_z;
723 } __packed hv_vmbus_input_signal_event;
724 
725 typedef struct {
726 	uint64_t			align8;
727 	hv_vmbus_input_signal_event	event;
728 } __packed hv_vmbus_input_signal_event_buffer;
729 
730 typedef struct hv_vmbus_channel {
731 	TAILQ_ENTRY(hv_vmbus_channel)	list_entry;
732 	struct hv_device*		device;
733 	hv_vmbus_channel_state		state;
734 	hv_vmbus_channel_offer_channel	offer_msg;
735 	/*
736 	 * These are based on the offer_msg.monitor_id.
737 	 * Save it here for easy access.
738 	 */
739 	uint8_t				monitor_group;
740 	uint8_t				monitor_bit;
741 
742 	uint32_t			ring_buffer_gpadl_handle;
743 	/*
744 	 * Allocated memory for ring buffer
745 	 */
746 	void*				ring_buffer_pages;
747 	unsigned long			ring_buffer_size;
748 	uint32_t			ring_buffer_page_count;
749 	/*
750 	 * send to parent
751 	 */
752 	hv_vmbus_ring_buffer_info	outbound;
753 	/*
754 	 * receive from parent
755 	 */
756 	hv_vmbus_ring_buffer_info	inbound;
757 
758 	struct taskqueue *		rxq;
759 	struct task			channel_task;
760 	hv_vmbus_pfn_channel_callback	on_channel_callback;
761 	void*				channel_callback_context;
762 
763 	/*
764 	 * If batched_reading is set to "true", mask the interrupt
765 	 * and read until the channel is empty.
766 	 * If batched_reading is set to "false", the channel is not
767 	 * going to perform batched reading.
768 	 *
769 	 * Batched reading is enabled by default; specific
770 	 * drivers that don't want this behavior can turn it off.
771 	 */
772 	boolean_t			batched_reading;
773 
774 	boolean_t			is_dedicated_interrupt;
775 
776 	/*
777 	 * Used as an input param for HV_CALL_SIGNAL_EVENT hypercall.
778 	 */
779 	hv_vmbus_input_signal_event_buffer	signal_event_buffer;
780 	/*
781 	 * 8-bytes aligned of the buffer above
782 	 */
783 	hv_vmbus_input_signal_event	*signal_event_param;
784 
785 	/*
786 	 * From Win8, this field specifies the target virtual process
787 	 * on which to deliver the interupt from the host to guest.
788 	 * Before Win8, all channel interrupts would only be
789 	 * delivered on cpu 0. Setting this value to 0 would preserve
790 	 * the earlier behavior.
791 	 */
792 	uint32_t			target_vcpu;
793 	/* The corresponding CPUID in the guest */
794 	uint32_t			target_cpu;
795 
796 	/*
797 	 * Support for multi-channels.
798 	 * The initial offer is considered the primary channel and this
799 	 * offer message will indicate if the host supports multi-channels.
800 	 * The guest is free to ask for multi-channels to be offerred and can
801 	 * open these multi-channels as a normal "primary" channel. However,
802 	 * all multi-channels will have the same type and instance guids as the
803 	 * primary channel. Requests sent on a given channel will result in a
804 	 * response on the same channel.
805 	 */
806 
807 	/*
808 	 * Multi-channel creation callback. This callback will be called in
809 	 * process context when a Multi-channel offer is received from the host.
810 	 * The guest can open the Multi-channel in the context of this callback.
811 	 */
812 	hv_vmbus_sc_creation_callback	sc_creation_callback;
813 
814 	struct mtx			sc_lock;
815 
816 	/*
817 	 * Link list of all the multi-channels if this is a primary channel
818 	 */
819 	TAILQ_HEAD(, hv_vmbus_channel)	sc_list_anchor;
820 	TAILQ_ENTRY(hv_vmbus_channel)	sc_list_entry;
821 
822 	/*
823 	 * The primary channel this sub-channle belongs to.
824 	 * This will be NULL for the primary channel.
825 	 */
826 	struct hv_vmbus_channel		*primary_channel;
827 
828 	/*
829 	 * Driver private data
830 	 */
831 	void				*hv_chan_priv1;
832 	void				*hv_chan_priv2;
833 	void				*hv_chan_priv3;
834 } hv_vmbus_channel;
835 
836 #define HV_VMBUS_CHAN_ISPRIMARY(chan)	((chan)->primary_channel == NULL)
837 
838 static inline void
839 hv_set_channel_read_state(hv_vmbus_channel* channel, boolean_t state)
840 {
841 	channel->batched_reading = state;
842 }
843 
844 typedef struct hv_device {
845 	hv_guid		    class_id;
846 	hv_guid		    device_id;
847 	device_t	    device;
848 	hv_vmbus_channel*   channel;
849 } hv_device;
850 
851 
852 
853 int		hv_vmbus_channel_recv_packet(
854 				hv_vmbus_channel*	channel,
855 				void*			buffer,
856 				uint32_t		buffer_len,
857 				uint32_t*		buffer_actual_len,
858 				uint64_t*		request_id);
859 
860 int		hv_vmbus_channel_recv_packet_raw(
861 				hv_vmbus_channel*	channel,
862 				void*			buffer,
863 				uint32_t		buffer_len,
864 				uint32_t*		buffer_actual_len,
865 				uint64_t*		request_id);
866 
867 int		hv_vmbus_channel_open(
868 				hv_vmbus_channel*	channel,
869 				uint32_t		send_ring_buffer_size,
870 				uint32_t		recv_ring_buffer_size,
871 				void*			user_data,
872 				uint32_t		user_data_len,
873 				hv_vmbus_pfn_channel_callback
874 							pfn_on_channel_callback,
875 				void*			context);
876 
877 void		hv_vmbus_channel_close(hv_vmbus_channel *channel);
878 
879 int		hv_vmbus_channel_send_packet(
880 				hv_vmbus_channel*	channel,
881 				void*			buffer,
882 				uint32_t		buffer_len,
883 				uint64_t		request_id,
884 				hv_vmbus_packet_type	type,
885 				uint32_t		flags);
886 
887 int		hv_vmbus_channel_send_packet_pagebuffer(
888 				hv_vmbus_channel*	channel,
889 				hv_vmbus_page_buffer	page_buffers[],
890 				uint32_t		page_count,
891 				void*			buffer,
892 				uint32_t		buffer_len,
893 				uint64_t		request_id);
894 
895 int		hv_vmbus_channel_send_packet_multipagebuffer(
896 				hv_vmbus_channel*	    channel,
897 				hv_vmbus_multipage_buffer*  multi_page_buffer,
898 				void*			    buffer,
899 				uint32_t		    buffer_len,
900 				uint64_t		    request_id);
901 
902 int		hv_vmbus_channel_establish_gpadl(
903 				hv_vmbus_channel*	channel,
904 				/* must be phys and virt contiguous */
905 				void*			contig_buffer,
906 				/*  page-size multiple	*/
907 				uint32_t		size,
908 				uint32_t*		gpadl_handle);
909 
910 int		hv_vmbus_channel_teardown_gpdal(
911 				hv_vmbus_channel*	channel,
912 				uint32_t		gpadl_handle);
913 
914 struct hv_vmbus_channel* vmbus_select_outgoing_channel(struct hv_vmbus_channel *promary);
915 
916 void		vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu);
917 
918 /**
919  * @brief Get physical address from virtual
920  */
921 static inline unsigned long
922 hv_get_phys_addr(void *virt)
923 {
924 	unsigned long ret;
925 	ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK));
926 	return (ret);
927 }
928 
929 extern uint32_t hv_vmbus_protocal_version;
930 #endif  /* __HYPERV_H__ */
931