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