1 /*
2  *
3  * Copyright (c) 2011, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *   K. Y. Srinivasan <kys@microsoft.com>
22  *
23  */
24 
25 #ifndef _HYPERV_H
26 #define _HYPERV_H
27 
28 #include <linux/types.h>
29 
30 /*
31  * An implementation of HyperV key value pair (KVP) functionality for Linux.
32  *
33  *
34  * Copyright (C) 2010, Novell, Inc.
35  * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
36  *
37  */
38 
39 /*
40  * Maximum value size - used for both key names and value data, and includes
41  * any applicable NULL terminators.
42  *
43  * Note:  This limit is somewhat arbitrary, but falls easily within what is
44  * supported for all native guests (back to Win 2000) and what is reasonable
45  * for the IC KVP exchange functionality.  Note that Windows Me/98/95 are
46  * limited to 255 character key names.
47  *
48  * MSDN recommends not storing data values larger than 2048 bytes in the
49  * registry.
50  *
51  * Note:  This value is used in defining the KVP exchange message - this value
52  * cannot be modified without affecting the message size and compatibility.
53  */
54 
55 /*
56  * bytes, including any null terminators
57  */
58 #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE          (2048)
59 
60 
61 /*
62  * Maximum key size - the registry limit for the length of an entry name
63  * is 256 characters, including the null terminator
64  */
65 
66 #define HV_KVP_EXCHANGE_MAX_KEY_SIZE            (512)
67 
68 /*
69  * In Linux, we implement the KVP functionality in two components:
70  * 1) The kernel component which is packaged as part of the hv_utils driver
71  * is responsible for communicating with the host and responsible for
72  * implementing the host/guest protocol. 2) A user level daemon that is
73  * responsible for data gathering.
74  *
75  * Host/Guest Protocol: The host iterates over an index and expects the guest
76  * to assign a key name to the index and also return the value corresponding to
77  * the key. The host will have atmost one KVP transaction outstanding at any
78  * given point in time. The host side iteration stops when the guest returns
79  * an error. Microsoft has specified the following mapping of key names to
80  * host specified index:
81  *
82  *	Index		Key Name
83  *	0		FullyQualifiedDomainName
84  *	1		IntegrationServicesVersion
85  *	2		NetworkAddressIPv4
86  *	3		NetworkAddressIPv6
87  *	4		OSBuildNumber
88  *	5		OSName
89  *	6		OSMajorVersion
90  *	7		OSMinorVersion
91  *	8		OSVersion
92  *	9		ProcessorArchitecture
93  *
94  * The Windows host expects the Key Name and Key Value to be encoded in utf16.
95  *
96  * Guest Kernel/KVP Daemon Protocol: As noted earlier, we implement all of the
97  * data gathering functionality in a user mode daemon. The user level daemon
98  * is also responsible for binding the key name to the index as well. The
99  * kernel and user-level daemon communicate using a connector channel.
100  *
101  * The user mode component first registers with the
102  * the kernel component. Subsequently, the kernel component requests, data
103  * for the specified keys. In response to this message the user mode component
104  * fills in the value corresponding to the specified key. We overload the
105  * sequence field in the cn_msg header to define our KVP message types.
106  *
107  *
108  * The kernel component simply acts as a conduit for communication between the
109  * Windows host and the user-level daemon. The kernel component passes up the
110  * index received from the Host to the user-level daemon. If the index is
111  * valid (supported), the corresponding key as well as its
112  * value (both are strings) is returned. If the index is invalid
113  * (not supported), a NULL key string is returned.
114  */
115 
116 
117 /*
118  * Registry value types.
119  */
120 
121 #define REG_SZ 1
122 #define REG_U32 4
123 #define REG_U64 8
124 
125 enum hv_kvp_exchg_op {
126 	KVP_OP_GET = 0,
127 	KVP_OP_SET,
128 	KVP_OP_DELETE,
129 	KVP_OP_ENUMERATE,
130 	KVP_OP_REGISTER,
131 	KVP_OP_COUNT /* Number of operations, must be last. */
132 };
133 
134 enum hv_kvp_exchg_pool {
135 	KVP_POOL_EXTERNAL = 0,
136 	KVP_POOL_GUEST,
137 	KVP_POOL_AUTO,
138 	KVP_POOL_AUTO_EXTERNAL,
139 	KVP_POOL_AUTO_INTERNAL,
140 	KVP_POOL_COUNT /* Number of pools, must be last. */
141 };
142 
143 struct hv_kvp_hdr {
144 	__u8 operation;
145 	__u8 pool;
146 	__u16 pad;
147 } __attribute__((packed));
148 
149 struct hv_kvp_exchg_msg_value {
150 	__u32 value_type;
151 	__u32 key_size;
152 	__u32 value_size;
153 	__u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
154 	union {
155 		__u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
156 		__u32 value_u32;
157 		__u64 value_u64;
158 	};
159 } __attribute__((packed));
160 
161 struct hv_kvp_msg_enumerate {
162 	__u32 index;
163 	struct hv_kvp_exchg_msg_value data;
164 } __attribute__((packed));
165 
166 struct hv_kvp_msg_get {
167 	struct hv_kvp_exchg_msg_value data;
168 };
169 
170 struct hv_kvp_msg_set {
171 	struct hv_kvp_exchg_msg_value data;
172 };
173 
174 struct hv_kvp_msg_delete {
175 	__u32 key_size;
176 	__u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
177 };
178 
179 struct hv_kvp_register {
180 	__u8 version[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
181 };
182 
183 struct hv_kvp_msg {
184 	struct hv_kvp_hdr	kvp_hdr;
185 	union {
186 		struct hv_kvp_msg_get		kvp_get;
187 		struct hv_kvp_msg_set		kvp_set;
188 		struct hv_kvp_msg_delete	kvp_delete;
189 		struct hv_kvp_msg_enumerate	kvp_enum_data;
190 		struct hv_kvp_register		kvp_register;
191 	} body;
192 } __attribute__((packed));
193 
194 #ifdef __KERNEL__
195 #include <linux/scatterlist.h>
196 #include <linux/list.h>
197 #include <linux/uuid.h>
198 #include <linux/timer.h>
199 #include <linux/workqueue.h>
200 #include <linux/completion.h>
201 #include <linux/device.h>
202 #include <linux/mod_devicetable.h>
203 
204 
205 #define MAX_PAGE_BUFFER_COUNT				19
206 #define MAX_MULTIPAGE_BUFFER_COUNT			32 /* 128K */
207 
208 #pragma pack(push, 1)
209 
210 /* Single-page buffer */
211 struct hv_page_buffer {
212 	u32 len;
213 	u32 offset;
214 	u64 pfn;
215 };
216 
217 /* Multiple-page buffer */
218 struct hv_multipage_buffer {
219 	/* Length and Offset determines the # of pfns in the array */
220 	u32 len;
221 	u32 offset;
222 	u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
223 };
224 
225 /* 0x18 includes the proprietary packet header */
226 #define MAX_PAGE_BUFFER_PACKET		(0x18 +			\
227 					(sizeof(struct hv_page_buffer) * \
228 					 MAX_PAGE_BUFFER_COUNT))
229 #define MAX_MULTIPAGE_BUFFER_PACKET	(0x18 +			\
230 					 sizeof(struct hv_multipage_buffer))
231 
232 
233 #pragma pack(pop)
234 
235 struct hv_ring_buffer {
236 	/* Offset in bytes from the start of ring data below */
237 	u32 write_index;
238 
239 	/* Offset in bytes from the start of ring data below */
240 	u32 read_index;
241 
242 	u32 interrupt_mask;
243 
244 	/* Pad it to PAGE_SIZE so that data starts on page boundary */
245 	u8	reserved[4084];
246 
247 	/* NOTE:
248 	 * The interrupt_mask field is used only for channels but since our
249 	 * vmbus connection also uses this data structure and its data starts
250 	 * here, we commented out this field.
251 	 */
252 
253 	/*
254 	 * Ring data starts here + RingDataStartOffset
255 	 * !!! DO NOT place any fields below this !!!
256 	 */
257 	u8 buffer[0];
258 } __packed;
259 
260 struct hv_ring_buffer_info {
261 	struct hv_ring_buffer *ring_buffer;
262 	u32 ring_size;			/* Include the shared header */
263 	spinlock_t ring_lock;
264 
265 	u32 ring_datasize;		/* < ring_size */
266 	u32 ring_data_startoffset;
267 };
268 
269 struct hv_ring_buffer_debug_info {
270 	u32 current_interrupt_mask;
271 	u32 current_read_index;
272 	u32 current_write_index;
273 	u32 bytes_avail_toread;
274 	u32 bytes_avail_towrite;
275 };
276 
277 /*
278  * We use the same version numbering for all Hyper-V modules.
279  *
280  * Definition of versioning is as follows;
281  *
282  *	Major Number	Changes for these scenarios;
283  *			1.	When a new version of Windows Hyper-V
284  *				is released.
285  *			2.	A Major change has occurred in the
286  *				Linux IC's.
287  *			(For example the merge for the first time
288  *			into the kernel) Every time the Major Number
289  *			changes, the Revision number is reset to 0.
290  *	Minor Number	Changes when new functionality is added
291  *			to the Linux IC's that is not a bug fix.
292  *
293  * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
294  */
295 #define HV_DRV_VERSION           "3.1"
296 
297 
298 /*
299  * A revision number of vmbus that is used for ensuring both ends on a
300  * partition are using compatible versions.
301  */
302 #define VMBUS_REVISION_NUMBER		13
303 
304 /* Make maximum size of pipe payload of 16K */
305 #define MAX_PIPE_DATA_PAYLOAD		(sizeof(u8) * 16384)
306 
307 /* Define PipeMode values. */
308 #define VMBUS_PIPE_TYPE_BYTE		0x00000000
309 #define VMBUS_PIPE_TYPE_MESSAGE		0x00000004
310 
311 /* The size of the user defined data buffer for non-pipe offers. */
312 #define MAX_USER_DEFINED_BYTES		120
313 
314 /* The size of the user defined data buffer for pipe offers. */
315 #define MAX_PIPE_USER_DEFINED_BYTES	116
316 
317 /*
318  * At the center of the Channel Management library is the Channel Offer. This
319  * struct contains the fundamental information about an offer.
320  */
321 struct vmbus_channel_offer {
322 	uuid_le if_type;
323 	uuid_le if_instance;
324 	u64 int_latency; /* in 100ns units */
325 	u32 if_revision;
326 	u32 server_ctx_size;	/* in bytes */
327 	u16 chn_flags;
328 	u16 mmio_megabytes;		/* in bytes * 1024 * 1024 */
329 
330 	union {
331 		/* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
332 		struct {
333 			unsigned char user_def[MAX_USER_DEFINED_BYTES];
334 		} std;
335 
336 		/*
337 		 * Pipes:
338 		 * The following sructure is an integrated pipe protocol, which
339 		 * is implemented on top of standard user-defined data. Pipe
340 		 * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
341 		 * use.
342 		 */
343 		struct {
344 			u32  pipe_mode;
345 			unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
346 		} pipe;
347 	} u;
348 	u32 padding;
349 } __packed;
350 
351 /* Server Flags */
352 #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE	1
353 #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES	2
354 #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS		4
355 #define VMBUS_CHANNEL_NAMED_PIPE_MODE			0x10
356 #define VMBUS_CHANNEL_LOOPBACK_OFFER			0x100
357 #define VMBUS_CHANNEL_PARENT_OFFER			0x200
358 #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION	0x400
359 
360 struct vmpacket_descriptor {
361 	u16 type;
362 	u16 offset8;
363 	u16 len8;
364 	u16 flags;
365 	u64 trans_id;
366 } __packed;
367 
368 struct vmpacket_header {
369 	u32 prev_pkt_start_offset;
370 	struct vmpacket_descriptor descriptor;
371 } __packed;
372 
373 struct vmtransfer_page_range {
374 	u32 byte_count;
375 	u32 byte_offset;
376 } __packed;
377 
378 struct vmtransfer_page_packet_header {
379 	struct vmpacket_descriptor d;
380 	u16 xfer_pageset_id;
381 	bool sender_owns_set;
382 	u8 reserved;
383 	u32 range_cnt;
384 	struct vmtransfer_page_range ranges[1];
385 } __packed;
386 
387 struct vmgpadl_packet_header {
388 	struct vmpacket_descriptor d;
389 	u32 gpadl;
390 	u32 reserved;
391 } __packed;
392 
393 struct vmadd_remove_transfer_page_set {
394 	struct vmpacket_descriptor d;
395 	u32 gpadl;
396 	u16 xfer_pageset_id;
397 	u16 reserved;
398 } __packed;
399 
400 /*
401  * This structure defines a range in guest physical space that can be made to
402  * look virtually contiguous.
403  */
404 struct gpa_range {
405 	u32 byte_count;
406 	u32 byte_offset;
407 	u64 pfn_array[0];
408 };
409 
410 /*
411  * This is the format for an Establish Gpadl packet, which contains a handle by
412  * which this GPADL will be known and a set of GPA ranges associated with it.
413  * This can be converted to a MDL by the guest OS.  If there are multiple GPA
414  * ranges, then the resulting MDL will be "chained," representing multiple VA
415  * ranges.
416  */
417 struct vmestablish_gpadl {
418 	struct vmpacket_descriptor d;
419 	u32 gpadl;
420 	u32 range_cnt;
421 	struct gpa_range range[1];
422 } __packed;
423 
424 /*
425  * This is the format for a Teardown Gpadl packet, which indicates that the
426  * GPADL handle in the Establish Gpadl packet will never be referenced again.
427  */
428 struct vmteardown_gpadl {
429 	struct vmpacket_descriptor d;
430 	u32 gpadl;
431 	u32 reserved;	/* for alignment to a 8-byte boundary */
432 } __packed;
433 
434 /*
435  * This is the format for a GPA-Direct packet, which contains a set of GPA
436  * ranges, in addition to commands and/or data.
437  */
438 struct vmdata_gpa_direct {
439 	struct vmpacket_descriptor d;
440 	u32 reserved;
441 	u32 range_cnt;
442 	struct gpa_range range[1];
443 } __packed;
444 
445 /* This is the format for a Additional Data Packet. */
446 struct vmadditional_data {
447 	struct vmpacket_descriptor d;
448 	u64 total_bytes;
449 	u32 offset;
450 	u32 byte_cnt;
451 	unsigned char data[1];
452 } __packed;
453 
454 union vmpacket_largest_possible_header {
455 	struct vmpacket_descriptor simple_hdr;
456 	struct vmtransfer_page_packet_header xfer_page_hdr;
457 	struct vmgpadl_packet_header gpadl_hdr;
458 	struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
459 	struct vmestablish_gpadl establish_gpadl_hdr;
460 	struct vmteardown_gpadl teardown_gpadl_hdr;
461 	struct vmdata_gpa_direct data_gpa_direct_hdr;
462 };
463 
464 #define VMPACKET_DATA_START_ADDRESS(__packet)	\
465 	(void *)(((unsigned char *)__packet) +	\
466 	 ((struct vmpacket_descriptor)__packet)->offset8 * 8)
467 
468 #define VMPACKET_DATA_LENGTH(__packet)		\
469 	((((struct vmpacket_descriptor)__packet)->len8 -	\
470 	  ((struct vmpacket_descriptor)__packet)->offset8) * 8)
471 
472 #define VMPACKET_TRANSFER_MODE(__packet)	\
473 	(((struct IMPACT)__packet)->type)
474 
475 enum vmbus_packet_type {
476 	VM_PKT_INVALID				= 0x0,
477 	VM_PKT_SYNCH				= 0x1,
478 	VM_PKT_ADD_XFER_PAGESET			= 0x2,
479 	VM_PKT_RM_XFER_PAGESET			= 0x3,
480 	VM_PKT_ESTABLISH_GPADL			= 0x4,
481 	VM_PKT_TEARDOWN_GPADL			= 0x5,
482 	VM_PKT_DATA_INBAND			= 0x6,
483 	VM_PKT_DATA_USING_XFER_PAGES		= 0x7,
484 	VM_PKT_DATA_USING_GPADL			= 0x8,
485 	VM_PKT_DATA_USING_GPA_DIRECT		= 0x9,
486 	VM_PKT_CANCEL_REQUEST			= 0xa,
487 	VM_PKT_COMP				= 0xb,
488 	VM_PKT_DATA_USING_ADDITIONAL_PKT	= 0xc,
489 	VM_PKT_ADDITIONAL_DATA			= 0xd
490 };
491 
492 #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED	1
493 
494 
495 /* Version 1 messages */
496 enum vmbus_channel_message_type {
497 	CHANNELMSG_INVALID			=  0,
498 	CHANNELMSG_OFFERCHANNEL		=  1,
499 	CHANNELMSG_RESCIND_CHANNELOFFER	=  2,
500 	CHANNELMSG_REQUESTOFFERS		=  3,
501 	CHANNELMSG_ALLOFFERS_DELIVERED	=  4,
502 	CHANNELMSG_OPENCHANNEL		=  5,
503 	CHANNELMSG_OPENCHANNEL_RESULT		=  6,
504 	CHANNELMSG_CLOSECHANNEL		=  7,
505 	CHANNELMSG_GPADL_HEADER		=  8,
506 	CHANNELMSG_GPADL_BODY			=  9,
507 	CHANNELMSG_GPADL_CREATED		= 10,
508 	CHANNELMSG_GPADL_TEARDOWN		= 11,
509 	CHANNELMSG_GPADL_TORNDOWN		= 12,
510 	CHANNELMSG_RELID_RELEASED		= 13,
511 	CHANNELMSG_INITIATE_CONTACT		= 14,
512 	CHANNELMSG_VERSION_RESPONSE		= 15,
513 	CHANNELMSG_UNLOAD			= 16,
514 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
515 	CHANNELMSG_VIEWRANGE_ADD		= 17,
516 	CHANNELMSG_VIEWRANGE_REMOVE		= 18,
517 #endif
518 	CHANNELMSG_COUNT
519 };
520 
521 struct vmbus_channel_message_header {
522 	enum vmbus_channel_message_type msgtype;
523 	u32 padding;
524 } __packed;
525 
526 /* Query VMBus Version parameters */
527 struct vmbus_channel_query_vmbus_version {
528 	struct vmbus_channel_message_header header;
529 	u32 version;
530 } __packed;
531 
532 /* VMBus Version Supported parameters */
533 struct vmbus_channel_version_supported {
534 	struct vmbus_channel_message_header header;
535 	bool version_supported;
536 } __packed;
537 
538 /* Offer Channel parameters */
539 struct vmbus_channel_offer_channel {
540 	struct vmbus_channel_message_header header;
541 	struct vmbus_channel_offer offer;
542 	u32 child_relid;
543 	u8 monitorid;
544 	bool monitor_allocated;
545 } __packed;
546 
547 /* Rescind Offer parameters */
548 struct vmbus_channel_rescind_offer {
549 	struct vmbus_channel_message_header header;
550 	u32 child_relid;
551 } __packed;
552 
553 /*
554  * Request Offer -- no parameters, SynIC message contains the partition ID
555  * Set Snoop -- no parameters, SynIC message contains the partition ID
556  * Clear Snoop -- no parameters, SynIC message contains the partition ID
557  * All Offers Delivered -- no parameters, SynIC message contains the partition
558  *		           ID
559  * Flush Client -- no parameters, SynIC message contains the partition ID
560  */
561 
562 /* Open Channel parameters */
563 struct vmbus_channel_open_channel {
564 	struct vmbus_channel_message_header header;
565 
566 	/* Identifies the specific VMBus channel that is being opened. */
567 	u32 child_relid;
568 
569 	/* ID making a particular open request at a channel offer unique. */
570 	u32 openid;
571 
572 	/* GPADL for the channel's ring buffer. */
573 	u32 ringbuffer_gpadlhandle;
574 
575 	/* GPADL for the channel's server context save area. */
576 	u32 server_contextarea_gpadlhandle;
577 
578 	/*
579 	* The upstream ring buffer begins at offset zero in the memory
580 	* described by RingBufferGpadlHandle. The downstream ring buffer
581 	* follows it at this offset (in pages).
582 	*/
583 	u32 downstream_ringbuffer_pageoffset;
584 
585 	/* User-specific data to be passed along to the server endpoint. */
586 	unsigned char userdata[MAX_USER_DEFINED_BYTES];
587 } __packed;
588 
589 /* Open Channel Result parameters */
590 struct vmbus_channel_open_result {
591 	struct vmbus_channel_message_header header;
592 	u32 child_relid;
593 	u32 openid;
594 	u32 status;
595 } __packed;
596 
597 /* Close channel parameters; */
598 struct vmbus_channel_close_channel {
599 	struct vmbus_channel_message_header header;
600 	u32 child_relid;
601 } __packed;
602 
603 /* Channel Message GPADL */
604 #define GPADL_TYPE_RING_BUFFER		1
605 #define GPADL_TYPE_SERVER_SAVE_AREA	2
606 #define GPADL_TYPE_TRANSACTION		8
607 
608 /*
609  * The number of PFNs in a GPADL message is defined by the number of
610  * pages that would be spanned by ByteCount and ByteOffset.  If the
611  * implied number of PFNs won't fit in this packet, there will be a
612  * follow-up packet that contains more.
613  */
614 struct vmbus_channel_gpadl_header {
615 	struct vmbus_channel_message_header header;
616 	u32 child_relid;
617 	u32 gpadl;
618 	u16 range_buflen;
619 	u16 rangecount;
620 	struct gpa_range range[0];
621 } __packed;
622 
623 /* This is the followup packet that contains more PFNs. */
624 struct vmbus_channel_gpadl_body {
625 	struct vmbus_channel_message_header header;
626 	u32 msgnumber;
627 	u32 gpadl;
628 	u64 pfn[0];
629 } __packed;
630 
631 struct vmbus_channel_gpadl_created {
632 	struct vmbus_channel_message_header header;
633 	u32 child_relid;
634 	u32 gpadl;
635 	u32 creation_status;
636 } __packed;
637 
638 struct vmbus_channel_gpadl_teardown {
639 	struct vmbus_channel_message_header header;
640 	u32 child_relid;
641 	u32 gpadl;
642 } __packed;
643 
644 struct vmbus_channel_gpadl_torndown {
645 	struct vmbus_channel_message_header header;
646 	u32 gpadl;
647 } __packed;
648 
649 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
650 struct vmbus_channel_view_range_add {
651 	struct vmbus_channel_message_header header;
652 	PHYSICAL_ADDRESS viewrange_base;
653 	u64 viewrange_length;
654 	u32 child_relid;
655 } __packed;
656 
657 struct vmbus_channel_view_range_remove {
658 	struct vmbus_channel_message_header header;
659 	PHYSICAL_ADDRESS viewrange_base;
660 	u32 child_relid;
661 } __packed;
662 #endif
663 
664 struct vmbus_channel_relid_released {
665 	struct vmbus_channel_message_header header;
666 	u32 child_relid;
667 } __packed;
668 
669 struct vmbus_channel_initiate_contact {
670 	struct vmbus_channel_message_header header;
671 	u32 vmbus_version_requested;
672 	u32 padding2;
673 	u64 interrupt_page;
674 	u64 monitor_page1;
675 	u64 monitor_page2;
676 } __packed;
677 
678 struct vmbus_channel_version_response {
679 	struct vmbus_channel_message_header header;
680 	bool version_supported;
681 } __packed;
682 
683 enum vmbus_channel_state {
684 	CHANNEL_OFFER_STATE,
685 	CHANNEL_OPENING_STATE,
686 	CHANNEL_OPEN_STATE,
687 };
688 
689 struct vmbus_channel_debug_info {
690 	u32 relid;
691 	enum vmbus_channel_state state;
692 	uuid_le interfacetype;
693 	uuid_le interface_instance;
694 	u32 monitorid;
695 	u32 servermonitor_pending;
696 	u32 servermonitor_latency;
697 	u32 servermonitor_connectionid;
698 	u32 clientmonitor_pending;
699 	u32 clientmonitor_latency;
700 	u32 clientmonitor_connectionid;
701 
702 	struct hv_ring_buffer_debug_info inbound;
703 	struct hv_ring_buffer_debug_info outbound;
704 };
705 
706 /*
707  * Represents each channel msg on the vmbus connection This is a
708  * variable-size data structure depending on the msg type itself
709  */
710 struct vmbus_channel_msginfo {
711 	/* Bookkeeping stuff */
712 	struct list_head msglistentry;
713 
714 	/* So far, this is only used to handle gpadl body message */
715 	struct list_head submsglist;
716 
717 	/* Synchronize the request/response if needed */
718 	struct completion  waitevent;
719 	union {
720 		struct vmbus_channel_version_supported version_supported;
721 		struct vmbus_channel_open_result open_result;
722 		struct vmbus_channel_gpadl_torndown gpadl_torndown;
723 		struct vmbus_channel_gpadl_created gpadl_created;
724 		struct vmbus_channel_version_response version_response;
725 	} response;
726 
727 	u32 msgsize;
728 	/*
729 	 * The channel message that goes out on the "wire".
730 	 * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
731 	 */
732 	unsigned char msg[0];
733 };
734 
735 struct vmbus_close_msg {
736 	struct vmbus_channel_msginfo info;
737 	struct vmbus_channel_close_channel msg;
738 };
739 
740 struct vmbus_channel {
741 	struct list_head listentry;
742 
743 	struct hv_device *device_obj;
744 
745 	struct work_struct work;
746 
747 	enum vmbus_channel_state state;
748 
749 	struct vmbus_channel_offer_channel offermsg;
750 	/*
751 	 * These are based on the OfferMsg.MonitorId.
752 	 * Save it here for easy access.
753 	 */
754 	u8 monitor_grp;
755 	u8 monitor_bit;
756 
757 	u32 ringbuffer_gpadlhandle;
758 
759 	/* Allocated memory for ring buffer */
760 	void *ringbuffer_pages;
761 	u32 ringbuffer_pagecount;
762 	struct hv_ring_buffer_info outbound;	/* send to parent */
763 	struct hv_ring_buffer_info inbound;	/* receive from parent */
764 	spinlock_t inbound_lock;
765 	struct workqueue_struct *controlwq;
766 
767 	struct vmbus_close_msg close_msg;
768 
769 	/* Channel callback are invoked in this workqueue context */
770 	/* HANDLE dataWorkQueue; */
771 
772 	void (*onchannel_callback)(void *context);
773 	void *channel_callback_context;
774 };
775 
776 void vmbus_onmessage(void *context);
777 
778 int vmbus_request_offers(void);
779 
780 /* The format must be the same as struct vmdata_gpa_direct */
781 struct vmbus_channel_packet_page_buffer {
782 	u16 type;
783 	u16 dataoffset8;
784 	u16 length8;
785 	u16 flags;
786 	u64 transactionid;
787 	u32 reserved;
788 	u32 rangecount;
789 	struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
790 } __packed;
791 
792 /* The format must be the same as struct vmdata_gpa_direct */
793 struct vmbus_channel_packet_multipage_buffer {
794 	u16 type;
795 	u16 dataoffset8;
796 	u16 length8;
797 	u16 flags;
798 	u64 transactionid;
799 	u32 reserved;
800 	u32 rangecount;		/* Always 1 in this case */
801 	struct hv_multipage_buffer range;
802 } __packed;
803 
804 
805 extern int vmbus_open(struct vmbus_channel *channel,
806 			    u32 send_ringbuffersize,
807 			    u32 recv_ringbuffersize,
808 			    void *userdata,
809 			    u32 userdatalen,
810 			    void(*onchannel_callback)(void *context),
811 			    void *context);
812 
813 extern void vmbus_close(struct vmbus_channel *channel);
814 
815 extern int vmbus_sendpacket(struct vmbus_channel *channel,
816 				  const void *buffer,
817 				  u32 bufferLen,
818 				  u64 requestid,
819 				  enum vmbus_packet_type type,
820 				  u32 flags);
821 
822 extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
823 					    struct hv_page_buffer pagebuffers[],
824 					    u32 pagecount,
825 					    void *buffer,
826 					    u32 bufferlen,
827 					    u64 requestid);
828 
829 extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
830 					struct hv_multipage_buffer *mpb,
831 					void *buffer,
832 					u32 bufferlen,
833 					u64 requestid);
834 
835 extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
836 				      void *kbuffer,
837 				      u32 size,
838 				      u32 *gpadl_handle);
839 
840 extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
841 				     u32 gpadl_handle);
842 
843 extern int vmbus_recvpacket(struct vmbus_channel *channel,
844 				  void *buffer,
845 				  u32 bufferlen,
846 				  u32 *buffer_actual_len,
847 				  u64 *requestid);
848 
849 extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
850 				     void *buffer,
851 				     u32 bufferlen,
852 				     u32 *buffer_actual_len,
853 				     u64 *requestid);
854 
855 
856 extern void vmbus_get_debug_info(struct vmbus_channel *channel,
857 				     struct vmbus_channel_debug_info *debug);
858 
859 extern void vmbus_ontimer(unsigned long data);
860 
861 struct hv_dev_port_info {
862 	u32 int_mask;
863 	u32 read_idx;
864 	u32 write_idx;
865 	u32 bytes_avail_toread;
866 	u32 bytes_avail_towrite;
867 };
868 
869 /* Base driver object */
870 struct hv_driver {
871 	const char *name;
872 
873 	/* the device type supported by this driver */
874 	uuid_le dev_type;
875 	const struct hv_vmbus_device_id *id_table;
876 
877 	struct device_driver driver;
878 
879 	int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
880 	int (*remove)(struct hv_device *);
881 	void (*shutdown)(struct hv_device *);
882 
883 };
884 
885 /* Base device object */
886 struct hv_device {
887 	/* the device type id of this device */
888 	uuid_le dev_type;
889 
890 	/* the device instance id of this device */
891 	uuid_le dev_instance;
892 
893 	struct device device;
894 
895 	struct vmbus_channel *channel;
896 };
897 
898 
device_to_hv_device(struct device * d)899 static inline struct hv_device *device_to_hv_device(struct device *d)
900 {
901 	return container_of(d, struct hv_device, device);
902 }
903 
drv_to_hv_drv(struct device_driver * d)904 static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
905 {
906 	return container_of(d, struct hv_driver, driver);
907 }
908 
hv_set_drvdata(struct hv_device * dev,void * data)909 static inline void hv_set_drvdata(struct hv_device *dev, void *data)
910 {
911 	dev_set_drvdata(&dev->device, data);
912 }
913 
hv_get_drvdata(struct hv_device * dev)914 static inline void *hv_get_drvdata(struct hv_device *dev)
915 {
916 	return dev_get_drvdata(&dev->device);
917 }
918 
919 /* Vmbus interface */
920 #define vmbus_driver_register(driver)	\
921 	__vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
922 int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
923 					 struct module *owner,
924 					 const char *mod_name);
925 void vmbus_driver_unregister(struct hv_driver *hv_driver);
926 
927 /**
928  * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
929  *
930  * This macro is used to create a struct hv_vmbus_device_id that matches a
931  * specific device.
932  */
933 #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7,	\
934 		     g8, g9, ga, gb, gc, gd, ge, gf)	\
935 	.guid = { g0, g1, g2, g3, g4, g5, g6, g7,	\
936 		  g8, g9, ga, gb, gc, gd, ge, gf },
937 
938 /*
939  * Common header for Hyper-V ICs
940  */
941 
942 #define ICMSGTYPE_NEGOTIATE		0
943 #define ICMSGTYPE_HEARTBEAT		1
944 #define ICMSGTYPE_KVPEXCHANGE		2
945 #define ICMSGTYPE_SHUTDOWN		3
946 #define ICMSGTYPE_TIMESYNC		4
947 #define ICMSGTYPE_VSS			5
948 
949 #define ICMSGHDRFLAG_TRANSACTION	1
950 #define ICMSGHDRFLAG_REQUEST		2
951 #define ICMSGHDRFLAG_RESPONSE		4
952 
953 #define HV_S_OK				0x00000000
954 #define HV_E_FAIL			0x80004005
955 #define HV_S_CONT			0x80070103
956 #define HV_ERROR_NOT_SUPPORTED		0x80070032
957 #define HV_ERROR_MACHINE_LOCKED		0x800704F7
958 
959 /*
960  * While we want to handle util services as regular devices,
961  * there is only one instance of each of these services; so
962  * we statically allocate the service specific state.
963  */
964 
965 struct hv_util_service {
966 	u8 *recv_buffer;
967 	void (*util_cb)(void *);
968 	int (*util_init)(struct hv_util_service *);
969 	void (*util_deinit)(void);
970 };
971 
972 struct vmbuspipe_hdr {
973 	u32 flags;
974 	u32 msgsize;
975 } __packed;
976 
977 struct ic_version {
978 	u16 major;
979 	u16 minor;
980 } __packed;
981 
982 struct icmsg_hdr {
983 	struct ic_version icverframe;
984 	u16 icmsgtype;
985 	struct ic_version icvermsg;
986 	u16 icmsgsize;
987 	u32 status;
988 	u8 ictransaction_id;
989 	u8 icflags;
990 	u8 reserved[2];
991 } __packed;
992 
993 struct icmsg_negotiate {
994 	u16 icframe_vercnt;
995 	u16 icmsg_vercnt;
996 	u32 reserved;
997 	struct ic_version icversion_data[1]; /* any size array */
998 } __packed;
999 
1000 struct shutdown_msg_data {
1001 	u32 reason_code;
1002 	u32 timeout_seconds;
1003 	u32 flags;
1004 	u8  display_message[2048];
1005 } __packed;
1006 
1007 struct heartbeat_msg_data {
1008 	u64 seq_num;
1009 	u32 reserved[8];
1010 } __packed;
1011 
1012 /* Time Sync IC defs */
1013 #define ICTIMESYNCFLAG_PROBE	0
1014 #define ICTIMESYNCFLAG_SYNC	1
1015 #define ICTIMESYNCFLAG_SAMPLE	2
1016 
1017 #ifdef __x86_64__
1018 #define WLTIMEDELTA	116444736000000000L	/* in 100ns unit */
1019 #else
1020 #define WLTIMEDELTA	116444736000000000LL
1021 #endif
1022 
1023 struct ictimesync_data {
1024 	u64 parenttime;
1025 	u64 childtime;
1026 	u64 roundtriptime;
1027 	u8 flags;
1028 } __packed;
1029 
1030 struct hyperv_service_callback {
1031 	u8 msg_type;
1032 	char *log_msg;
1033 	uuid_le data;
1034 	struct vmbus_channel *channel;
1035 	void (*callback) (void *context);
1036 };
1037 
1038 extern void vmbus_prep_negotiate_resp(struct icmsg_hdr *,
1039 				      struct icmsg_negotiate *, u8 *);
1040 
1041 int hv_kvp_init(struct hv_util_service *);
1042 void hv_kvp_deinit(void);
1043 void hv_kvp_onchannelcallback(void *);
1044 
1045 #endif /* __KERNEL__ */
1046 #endif /* _HYPERV_H */
1047