hyperv.h revision 307018
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: stable/10/sys/dev/hyperv/include/hyperv.h 307018 2016-10-11 06:35:29Z 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#include <dev/hyperv/include/hyperv_busdma.h>
59
60typedef uint8_t	hv_bool_uint8_t;
61
62#define HV_S_OK			0x00000000
63#define HV_E_FAIL		0x80004005
64#define HV_ERROR_NOT_SUPPORTED	0x80070032
65#define HV_ERROR_MACHINE_LOCKED	0x800704F7
66
67/*
68 * VMBUS version is 32 bit, upper 16 bit for major_number and lower
69 * 16 bit for minor_number.
70 *
71 * 0.13  --  Windows Server 2008
72 * 1.1   --  Windows 7
73 * 2.4   --  Windows 8
74 * 3.0   --  Windows 8.1
75 */
76#define VMBUS_VERSION_WS2008		((0 << 16) | (13))
77#define VMBUS_VERSION_WIN7		((1 << 16) | (1))
78#define VMBUS_VERSION_WIN8		((2 << 16) | (4))
79#define VMBUS_VERSION_WIN8_1		((3 << 16) | (0))
80
81#define VMBUS_VERSION_MAJOR(ver)	(((uint32_t)(ver)) >> 16)
82#define VMBUS_VERSION_MINOR(ver)	(((uint32_t)(ver)) & 0xffff)
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 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
194typedef uint32_t hv_previous_packet_offset;
195
196typedef struct {
197	hv_previous_packet_offset	previous_packet_start_offset;
198	hv_vm_packet_descriptor		descriptor;
199} __packed hv_vm_packet_header;
200
201typedef struct {
202	uint32_t byte_count;
203	uint32_t byte_offset;
204} __packed hv_vm_transfer_page;
205
206typedef 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
215typedef struct {
216	hv_vm_packet_descriptor	d;
217	uint32_t		gpadl;
218	uint32_t		reserved;
219} __packed hv_vm_gpadl_packet_header;
220
221typedef 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
234typedef 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
248typedef 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
260typedef 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
272typedef 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 */
282typedef 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
290typedef 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
300typedef 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 */
322typedef 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
343typedef 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 */
351typedef struct {
352	hv_vmbus_channel_msg_header	header;
353	uint32_t			version;
354} __packed hv_vmbus_channel_query_vmbus_version;
355
356/*
357 * Channel Offer parameters
358 */
359typedef struct {
360	hv_vmbus_channel_msg_header	header;
361	hv_vmbus_channel_offer		offer;
362	uint32_t			child_rel_id;
363	uint8_t				monitor_id;
364	/*
365	 * This field has been split into a bit field on Win7
366	 * and higher.
367	 */
368	uint8_t				monitor_allocated:1;
369	uint8_t				reserved:7;
370	/*
371	 * Following fields were added in win7 and higher.
372	 * Make sure to check the version before accessing these fields.
373	 *
374	 * If "is_dedicated_interrupt" is set, we must not set the
375	 * associated bit in the channel bitmap while sending the
376	 * interrupt to the host.
377	 *
378	 * connection_id is used in signaling the host.
379	 */
380	uint16_t			is_dedicated_interrupt:1;
381	uint16_t			reserved1:15;
382	uint32_t			connection_id;
383} __packed hv_vmbus_channel_offer_channel;
384
385/*
386 * Rescind Offer parameters
387 */
388typedef struct
389{
390    hv_vmbus_channel_msg_header	header;
391    uint32_t			child_rel_id;
392} __packed hv_vmbus_channel_rescind_offer;
393
394typedef struct {
395	hv_vmbus_channel_msg_header	header;
396	uint32_t			child_rel_id;
397} __packed hv_vmbus_channel_relid_released;
398
399#define HW_MACADDR_LEN	6
400
401enum {
402	HV_VMBUS_IVAR_TYPE,
403	HV_VMBUS_IVAR_INSTANCE,
404	HV_VMBUS_IVAR_NODE,
405	HV_VMBUS_IVAR_DEVCTX
406};
407
408#define HV_VMBUS_ACCESSOR(var, ivar, type) \
409		__BUS_ACCESSOR(vmbus, var, HV_VMBUS, ivar, type)
410
411HV_VMBUS_ACCESSOR(type, TYPE,  const char *)
412HV_VMBUS_ACCESSOR(devctx, DEVCTX,  struct hv_device *)
413
414
415/*
416 * Common defines for Hyper-V ICs
417 */
418#define HV_ICMSGTYPE_NEGOTIATE		0
419#define HV_ICMSGTYPE_HEARTBEAT		1
420#define HV_ICMSGTYPE_KVPEXCHANGE	2
421#define HV_ICMSGTYPE_SHUTDOWN		3
422#define HV_ICMSGTYPE_TIMESYNC		4
423#define HV_ICMSGTYPE_VSS		5
424
425#define HV_ICMSGHDRFLAG_TRANSACTION	1
426#define HV_ICMSGHDRFLAG_REQUEST		2
427#define HV_ICMSGHDRFLAG_RESPONSE	4
428
429typedef struct hv_vmbus_pipe_hdr {
430	uint32_t flags;
431	uint32_t msgsize;
432} __packed hv_vmbus_pipe_hdr;
433
434typedef struct hv_vmbus_ic_version {
435	uint16_t major;
436	uint16_t minor;
437} __packed hv_vmbus_ic_version;
438
439typedef struct hv_vmbus_icmsg_hdr {
440	hv_vmbus_ic_version	icverframe;
441	uint16_t		icmsgtype;
442	hv_vmbus_ic_version	icvermsg;
443	uint16_t		icmsgsize;
444	uint32_t		status;
445	uint8_t			ictransaction_id;
446	uint8_t			icflags;
447	uint8_t			reserved[2];
448} __packed hv_vmbus_icmsg_hdr;
449
450typedef struct hv_vmbus_icmsg_negotiate {
451	uint16_t		icframe_vercnt;
452	uint16_t		icmsg_vercnt;
453	uint32_t		reserved;
454	hv_vmbus_ic_version	icversion_data[1]; /* any size array */
455} __packed hv_vmbus_icmsg_negotiate;
456
457typedef struct hv_vmbus_shutdown_msg_data {
458	uint32_t		reason_code;
459	uint32_t		timeout_seconds;
460	uint32_t 		flags;
461	uint8_t			display_message[2048];
462} __packed hv_vmbus_shutdown_msg_data;
463
464typedef struct hv_vmbus_heartbeat_msg_data {
465	uint64_t 		seq_num;
466	uint32_t 		reserved[8];
467} __packed hv_vmbus_heartbeat_msg_data;
468
469typedef struct {
470	/*
471	 * offset in bytes from the start of ring data below
472	 */
473	volatile uint32_t       write_index;
474	/*
475	 * offset in bytes from the start of ring data below
476	 */
477	volatile uint32_t       read_index;
478	/*
479	 * NOTE: The interrupt_mask field is used only for channels, but
480	 * vmbus connection also uses this data structure
481	 */
482	volatile uint32_t       interrupt_mask;
483	/* pad it to PAGE_SIZE so that data starts on a page */
484	uint8_t                 reserved[4084];
485
486	/*
487	 * WARNING: Ring data starts here + ring_data_start_offset
488	 *  !!! DO NOT place any fields below this !!!
489	 */
490	uint8_t			buffer[0];	/* doubles as interrupt mask */
491} __packed hv_vmbus_ring_buffer;
492
493typedef struct {
494	int		length;
495	int		offset;
496	uint64_t	pfn;
497} __packed hv_vmbus_page_buffer;
498
499typedef struct {
500	int		length;
501	int		offset;
502	uint64_t	pfn_array[HV_MAX_MULTIPAGE_BUFFER_COUNT];
503} __packed hv_vmbus_multipage_buffer;
504
505typedef struct {
506	hv_vmbus_ring_buffer*	ring_buffer;
507	uint32_t		ring_size;	/* Include the shared header */
508	struct mtx		ring_lock;
509	uint32_t		ring_data_size;	/* ring_size */
510	uint32_t		ring_data_start_offset;
511} hv_vmbus_ring_buffer_info;
512
513typedef void (*hv_vmbus_pfn_channel_callback)(void *context);
514
515typedef enum {
516	HV_CHANNEL_OFFER_STATE,
517	HV_CHANNEL_OPENING_STATE,
518	HV_CHANNEL_OPEN_STATE,
519	HV_CHANNEL_OPENED_STATE,
520	HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE,
521} hv_vmbus_channel_state;
522
523/*
524 *  Connection identifier type
525 */
526typedef union {
527	uint32_t		as_uint32_t;
528	struct {
529		uint32_t	id:24;
530		uint32_t	reserved:8;
531	} u;
532
533} __packed hv_vmbus_connection_id;
534
535typedef struct hv_vmbus_channel {
536	struct hv_device*		device;
537	struct vmbus_softc		*vmbus_sc;
538	hv_vmbus_channel_state		state;
539	hv_vmbus_channel_offer_channel	offer_msg;
540	/*
541	 * These are based on the offer_msg.monitor_id.
542	 * Save it here for easy access.
543	 */
544	uint8_t				monitor_group;
545	uint8_t				monitor_bit;
546
547	uint32_t			ring_buffer_gpadl_handle;
548	/*
549	 * Allocated memory for ring buffer
550	 */
551	void*				ring_buffer_pages;
552	unsigned long			ring_buffer_size;
553	uint32_t			ring_buffer_page_count;
554	/*
555	 * send to parent
556	 */
557	hv_vmbus_ring_buffer_info	outbound;
558	/*
559	 * receive from parent
560	 */
561	hv_vmbus_ring_buffer_info	inbound;
562
563	struct taskqueue *		rxq;
564	struct task			channel_task;
565	hv_vmbus_pfn_channel_callback	on_channel_callback;
566	void*				channel_callback_context;
567
568	/*
569	 * If batched_reading is set to "true", mask the interrupt
570	 * and read until the channel is empty.
571	 * If batched_reading is set to "false", the channel is not
572	 * going to perform batched reading.
573	 *
574	 * Batched reading is enabled by default; specific
575	 * drivers that don't want this behavior can turn it off.
576	 */
577	boolean_t			batched_reading;
578
579	boolean_t			is_dedicated_interrupt;
580
581	struct hypercall_sigevt_in	*ch_sigevt;
582	struct hyperv_dma		ch_sigevt_dma;
583
584	/*
585	 * From Win8, this field specifies the target virtual process
586	 * on which to deliver the interupt from the host to guest.
587	 * Before Win8, all channel interrupts would only be
588	 * delivered on cpu 0. Setting this value to 0 would preserve
589	 * the earlier behavior.
590	 */
591	uint32_t			target_vcpu;
592	/* The corresponding CPUID in the guest */
593	uint32_t			target_cpu;
594
595	/*
596	 * Support for multi-channels.
597	 * The initial offer is considered the primary channel and this
598	 * offer message will indicate if the host supports multi-channels.
599	 * The guest is free to ask for multi-channels to be offerred and can
600	 * open these multi-channels as a normal "primary" channel. However,
601	 * all multi-channels will have the same type and instance guids as the
602	 * primary channel. Requests sent on a given channel will result in a
603	 * response on the same channel.
604	 */
605
606	struct mtx			sc_lock;
607
608	/*
609	 * Link list of all the multi-channels if this is a primary channel
610	 */
611	TAILQ_HEAD(, hv_vmbus_channel)	sc_list_anchor;
612	TAILQ_ENTRY(hv_vmbus_channel)	sc_list_entry;
613	int				subchan_cnt;
614
615	/*
616	 * The primary channel this sub-channle belongs to.
617	 * This will be NULL for the primary channel.
618	 */
619	struct hv_vmbus_channel		*primary_channel;
620
621	/*
622	 * Driver private data
623	 */
624	void				*hv_chan_priv1;
625	void				*hv_chan_priv2;
626	void				*hv_chan_priv3;
627
628	struct task			ch_detach_task;
629	TAILQ_ENTRY(hv_vmbus_channel)	ch_link;
630} hv_vmbus_channel;
631
632#define HV_VMBUS_CHAN_ISPRIMARY(chan)	((chan)->primary_channel == NULL)
633
634static inline void
635hv_set_channel_read_state(hv_vmbus_channel* channel, boolean_t state)
636{
637	channel->batched_reading = state;
638}
639
640typedef struct hv_device {
641	hv_guid		    class_id;
642	hv_guid		    device_id;
643	device_t	    device;
644	hv_vmbus_channel*   channel;
645} hv_device;
646
647
648
649int		hv_vmbus_channel_recv_packet(
650				hv_vmbus_channel*	channel,
651				void*			buffer,
652				uint32_t		buffer_len,
653				uint32_t*		buffer_actual_len,
654				uint64_t*		request_id);
655
656int		hv_vmbus_channel_recv_packet_raw(
657				hv_vmbus_channel*	channel,
658				void*			buffer,
659				uint32_t		buffer_len,
660				uint32_t*		buffer_actual_len,
661				uint64_t*		request_id);
662
663int		hv_vmbus_channel_open(
664				hv_vmbus_channel*	channel,
665				uint32_t		send_ring_buffer_size,
666				uint32_t		recv_ring_buffer_size,
667				void*			user_data,
668				uint32_t		user_data_len,
669				hv_vmbus_pfn_channel_callback
670							pfn_on_channel_callback,
671				void*			context);
672
673void		hv_vmbus_channel_close(hv_vmbus_channel *channel);
674
675int		hv_vmbus_channel_send_packet(
676				hv_vmbus_channel*	channel,
677				void*			buffer,
678				uint32_t		buffer_len,
679				uint64_t		request_id,
680				hv_vmbus_packet_type	type,
681				uint32_t		flags);
682
683int		hv_vmbus_channel_send_packet_pagebuffer(
684				hv_vmbus_channel*	channel,
685				hv_vmbus_page_buffer	page_buffers[],
686				uint32_t		page_count,
687				void*			buffer,
688				uint32_t		buffer_len,
689				uint64_t		request_id);
690
691int		hv_vmbus_channel_send_packet_multipagebuffer(
692				hv_vmbus_channel*	    channel,
693				hv_vmbus_multipage_buffer*  multi_page_buffer,
694				void*			    buffer,
695				uint32_t		    buffer_len,
696				uint64_t		    request_id);
697
698int		hv_vmbus_channel_establish_gpadl(
699				hv_vmbus_channel*	channel,
700				/* must be phys and virt contiguous */
701				void*			contig_buffer,
702				/*  page-size multiple	*/
703				uint32_t		size,
704				uint32_t*		gpadl_handle);
705
706int		hv_vmbus_channel_teardown_gpdal(
707				hv_vmbus_channel*	channel,
708				uint32_t		gpadl_handle);
709
710struct hv_vmbus_channel* vmbus_select_outgoing_channel(struct hv_vmbus_channel *promary);
711
712void		vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu);
713void		vmbus_channel_cpu_rr(struct hv_vmbus_channel *chan);
714struct hv_vmbus_channel **
715		vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt);
716void		vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt);
717
718/**
719 * @brief Get physical address from virtual
720 */
721static inline unsigned long
722hv_get_phys_addr(void *virt)
723{
724	unsigned long ret;
725	ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK));
726	return (ret);
727}
728
729#endif  /* __HYPERV_H__ */
730