summaryrefslogtreecommitdiffstats
path: root/sys/dev/hyperv/include/hyperv.h
blob: 852e14ea5690a313fcc1339ccbd9d1bcc00979d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/*-
 * Copyright (c) 2009-2012 Microsoft Corp.
 * Copyright (c) 2012 NetApp Inc.
 * Copyright (c) 2012 Citrix Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/**
 * HyperV definitions for messages that are sent between instances of the
 * Channel Management Library in separate partitions, or in some cases,
 * back to itself.
 */

#ifndef __HYPERV_H__
#define __HYPERV_H__

#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/kthread.h>
#include <sys/taskqueue.h>
#include <sys/systm.h>
#include <sys/lock.h>
#include <sys/sema.h>
#include <sys/smp.h>
#include <sys/mutex.h>
#include <sys/bus.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>

#include <amd64/include/xen/synch_bitops.h>
#include <amd64/include/atomic.h>

typedef uint8_t	hv_bool_uint8_t;

#define HV_S_OK			0x00000000
#define HV_E_FAIL		0x80004005
#define HV_ERROR_NOT_SUPPORTED	0x80070032
#define HV_ERROR_MACHINE_LOCKED	0x800704F7

/*
 * VMBUS version is 32 bit, upper 16 bit for major_number and lower
 * 16 bit for minor_number.
 *
 * 0.13  --  Windows Server 2008
 * 1.1   --  Windows 7
 * 2.4   --  Windows 8
 * 3.0   --  Windows 8.1
 */
#define HV_VMBUS_VERSION_WS2008		((0 << 16) | (13))
#define HV_VMBUS_VERSION_WIN7		((1 << 16) | (1))
#define HV_VMBUS_VERSION_WIN8		((2 << 16) | (4))
#define HV_VMBUS_VERSION_WIN8_1		((3 << 16) | (0))

#define HV_VMBUS_VERSION_INVALID	-1

#define HV_VMBUS_VERSION_CURRENT	HV_VMBUS_VERSION_WIN8_1

/*
 * Make maximum size of pipe payload of 16K
 */

#define HV_MAX_PIPE_DATA_PAYLOAD	(sizeof(BYTE) * 16384)

/*
 * Define pipe_mode values
 */

#define HV_VMBUS_PIPE_TYPE_BYTE		0x00000000
#define HV_VMBUS_PIPE_TYPE_MESSAGE	0x00000004

/*
 * The size of the user defined data buffer for non-pipe offers
 */

#define HV_MAX_USER_DEFINED_BYTES	120

/*
 *  The size of the user defined data buffer for pipe offers
 */

#define HV_MAX_PIPE_USER_DEFINED_BYTES	116


#define HV_MAX_PAGE_BUFFER_COUNT	32
#define HV_MAX_MULTIPAGE_BUFFER_COUNT	32

#define HV_ALIGN_UP(value, align)					\
		(((value) & (align-1)) ?				\
		    (((value) + (align-1)) & ~(align-1) ) : (value))

#define HV_ALIGN_DOWN(value, align) ( (value) & ~(align-1) )

#define HV_NUM_PAGES_SPANNED(addr, len)					\
		((HV_ALIGN_UP(addr+len, PAGE_SIZE) -			\
		    HV_ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )

typedef struct hv_guid {
	 unsigned char data[16];
} __packed hv_guid;

#define HV_NIC_GUID							\
	.data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,	\
		0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}

#define HV_IDE_GUID							\
	.data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,	\
		 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}

#define HV_SCSI_GUID							\
	.data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,	\
		 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}

/*
 * At the center of the Channel Management library is
 * the Channel Offer. This struct contains the
 * fundamental information about an offer.
 */

typedef struct hv_vmbus_channel_offer {
	hv_guid		interface_type;
	hv_guid		interface_instance;
	uint64_t	interrupt_latency_in_100ns_units;
	uint32_t	interface_revision;
	uint32_t	server_context_area_size; /* in bytes */
	uint16_t	channel_flags;
	uint16_t	mmio_megabytes;		  /* in bytes * 1024 * 1024 */
	union
	{
        /*
         * Non-pipes: The user has HV_MAX_USER_DEFINED_BYTES bytes.
         */
		struct {
			uint8_t	user_defined[HV_MAX_USER_DEFINED_BYTES];
		} __packed standard;

        /*
         * Pipes: The following structure is an integrated pipe protocol, which
         *        is implemented on top of standard user-defined data. pipe
         *        clients  have HV_MAX_PIPE_USER_DEFINED_BYTES left for their
         *        own use.
         */
		struct {
			uint32_t	pipe_mode;
			uint8_t	user_defined[HV_MAX_PIPE_USER_DEFINED_BYTES];
		} __packed pipe;
	} u;

	/*
	 * Sub_channel_index, newly added in Win8.
	 */
	uint16_t	sub_channel_index;
	uint16_t	padding;

} __packed hv_vmbus_channel_offer;

typedef uint32_t hv_gpadl_handle;

typedef struct {
	uint16_t type;
	uint16_t data_offset8;
	uint16_t length8;
	uint16_t flags;
	uint64_t transaction_id;
} __packed hv_vm_packet_descriptor;

typedef uint32_t hv_previous_packet_offset;

typedef struct {
	hv_previous_packet_offset	previous_packet_start_offset;
	hv_vm_packet_descriptor		descriptor;
} __packed hv_vm_packet_header;

typedef struct {
	uint32_t byte_count;
	uint32_t byte_offset;
} __packed hv_vm_transfer_page;

typedef struct {
	hv_vm_packet_descriptor	d;
	uint16_t		transfer_page_set_id;
	hv_bool_uint8_t		sender_owns_set;
	uint8_t			reserved;
	uint32_t		range_count;
	hv_vm_transfer_page	ranges[1];
} __packed hv_vm_transfer_page_packet_header;

typedef struct {
	hv_vm_packet_descriptor	d;
	uint32_t		gpadl;
	uint32_t		reserved;
} __packed hv_vm_gpadl_packet_header;

typedef struct {
	hv_vm_packet_descriptor	d;
	uint32_t		gpadl;
	uint16_t		transfer_page_set_id;
	uint16_t		reserved;
} __packed hv_vm_add_remove_transfer_page_set;

/*
 * This structure defines a range in guest
 * physical space that can be made
 * to look virtually contiguous.
 */

typedef struct {
	uint32_t byte_count;
	uint32_t byte_offset;
	uint64_t pfn_array[0];
} __packed hv_gpa_range;

/*
 * This is the format for an Establish Gpadl packet, which contains a handle
 * by which this GPADL will be known and a set of GPA ranges associated with
 * it.  This can be converted to a MDL by the guest OS.  If there are multiple
 * GPA ranges, then the resulting MDL will be "chained," representing multiple
 * VA ranges.
 */

typedef struct {
	hv_vm_packet_descriptor	d;
	uint32_t		gpadl;
	uint32_t		range_count;
	hv_gpa_range		range[1];
} __packed hv_vm_establish_gpadl;

/*
 * This is the format for a Teardown Gpadl packet, which indicates that the
 * GPADL handle in the Establish Gpadl packet will never be referenced again.
 */

typedef struct {
	hv_vm_packet_descriptor	d;
	uint32_t		gpadl;
				/* for alignment to a 8-byte boundary */
	uint32_t		reserved;
} __packed hv_vm_teardown_gpadl;

/*
 * This is the format for a GPA-Direct packet, which contains a set of GPA
 * ranges, in addition to commands and/or data.
 */

typedef struct {
	hv_vm_packet_descriptor	d;
	uint32_t		reserved;
	uint32_t		range_count;
	hv_gpa_range		range[1];
} __packed hv_vm_data_gpa_direct;

/*
 * This is the format for a Additional data Packet.
 */
typedef struct {
	hv_vm_packet_descriptor	d;
	uint64_t		total_bytes;
	uint32_t		byte_offset;
	uint32_t		byte_count;
	uint8_t			data[1];
} __packed hv_vm_additional_data;

typedef union {
	hv_vm_packet_descriptor             simple_header;
	hv_vm_transfer_page_packet_header   transfer_page_header;
	hv_vm_gpadl_packet_header           gpadl_header;
	hv_vm_add_remove_transfer_page_set  add_remove_transfer_page_header;
	hv_vm_establish_gpadl               establish_gpadl_header;
	hv_vm_teardown_gpadl                teardown_gpadl_header;
	hv_vm_data_gpa_direct               data_gpa_direct_header;
} __packed hv_vm_packet_largest_possible_header;

typedef enum {
	HV_VMBUS_PACKET_TYPE_INVALID				= 0x0,
	HV_VMBUS_PACKET_TYPES_SYNCH				= 0x1,
	HV_VMBUS_PACKET_TYPE_ADD_TRANSFER_PAGE_SET		= 0x2,
	HV_VMBUS_PACKET_TYPE_REMOVE_TRANSFER_PAGE_SET		= 0x3,
	HV_VMBUS_PACKET_TYPE_ESTABLISH_GPADL			= 0x4,
	HV_VMBUS_PACKET_TYPE_TEAR_DOWN_GPADL			= 0x5,
	HV_VMBUS_PACKET_TYPE_DATA_IN_BAND			= 0x6,
	HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES		= 0x7,
	HV_VMBUS_PACKET_TYPE_DATA_USING_GPADL			= 0x8,
	HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT		= 0x9,
	HV_VMBUS_PACKET_TYPE_CANCEL_REQUEST			= 0xa,
	HV_VMBUS_PACKET_TYPE_COMPLETION				= 0xb,
	HV_VMBUS_PACKET_TYPE_DATA_USING_ADDITIONAL_PACKETS	= 0xc,
	HV_VMBUS_PACKET_TYPE_ADDITIONAL_DATA = 0xd
} hv_vmbus_packet_type;

#define HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED    1

/*
 * Version 1 messages
 */
typedef enum {
	HV_CHANNEL_MESSAGE_INVALID			= 0,
	HV_CHANNEL_MESSAGE_OFFER_CHANNEL		= 1,
	HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER	= 2,
	HV_CHANNEL_MESSAGE_REQUEST_OFFERS		= 3,
	HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED		= 4,
	HV_CHANNEL_MESSAGE_OPEN_CHANNEL			= 5,
	HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT		= 6,
	HV_CHANNEL_MESSAGE_CLOSE_CHANNEL		= 7,
	HV_CHANNEL_MESSAGEL_GPADL_HEADER		= 8,
	HV_CHANNEL_MESSAGE_GPADL_BODY			= 9,
	HV_CHANNEL_MESSAGE_GPADL_CREATED		= 10,
	HV_CHANNEL_MESSAGE_GPADL_TEARDOWN		= 11,
	HV_CHANNEL_MESSAGE_GPADL_TORNDOWN		= 12,
	HV_CHANNEL_MESSAGE_REL_ID_RELEASED		= 13,
	HV_CHANNEL_MESSAGE_INITIATED_CONTACT		= 14,
	HV_CHANNEL_MESSAGE_VERSION_RESPONSE		= 15,
	HV_CHANNEL_MESSAGE_UNLOAD			= 16,
	HV_CHANNEL_MESSAGE_COUNT
} hv_vmbus_channel_msg_type;

typedef struct {
	hv_vmbus_channel_msg_type	message_type;
	uint32_t			padding;
} __packed hv_vmbus_channel_msg_header;

/*
 * Query VMBus Version parameters
 */
typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			version;
} __packed hv_vmbus_channel_query_vmbus_version;

/*
 * VMBus Version Supported parameters
 */
typedef struct {
	hv_vmbus_channel_msg_header	header;
	hv_bool_uint8_t			version_supported;
} __packed hv_vmbus_channel_version_supported;

/*
 * Channel Offer parameters
 */
typedef struct {
	hv_vmbus_channel_msg_header	header;
	hv_vmbus_channel_offer		offer;
	uint32_t			child_rel_id;
	uint8_t				monitor_id;
	/*
	 * This field has been split into a bit field on Win7
	 * and higher.
	 */
	uint8_t				monitor_allocated:1;
	uint8_t				reserved:7;
	/*
	 * Following fields were added in win7 and higher.
	 * Make sure to check the version before accessing these fields.
	 *
	 * If "is_dedicated_interrupt" is set, we must not set the
	 * associated bit in the channel bitmap while sending the
	 * interrupt to the host.
	 *
	 * connection_id is used in signaling the host.
	 */
	uint16_t			is_dedicated_interrupt:1;
	uint16_t			reserved1:15;
	uint32_t			connection_id;
} __packed hv_vmbus_channel_offer_channel;

/*
 * Rescind Offer parameters
 */
typedef struct
{
    hv_vmbus_channel_msg_header	header;
    uint32_t			child_rel_id;
} __packed hv_vmbus_channel_rescind_offer;


/*
 * Request Offer -- no parameters, SynIC message contains the partition ID
 *
 * Set Snoop -- no parameters, SynIC message contains the partition ID
 *
 * Clear Snoop -- no parameters, SynIC message contains the partition ID
 *
 * All Offers Delivered -- no parameters, SynIC message contains the
 * partition ID
 *
 * Flush Client -- no parameters, SynIC message contains the partition ID
 */


/*
 * Open Channel parameters
 */
typedef struct
{
    hv_vmbus_channel_msg_header header;

    /*
     * Identifies the specific VMBus channel that is being opened.
     */
    uint32_t		child_rel_id;

    /*
     * ID making a particular open request at a channel offer unique.
     */
    uint32_t		open_id;

    /*
     * GPADL for the channel's ring buffer.
     */
    hv_gpadl_handle	ring_buffer_gpadl_handle;

    /*
     * Before win8, all incoming channel interrupts are only
     * delivered on cpu 0. Setting this value to 0 would
     * preserve the earlier behavior.
     */
    uint32_t		target_vcpu;

    /*
     * The upstream ring buffer begins at offset zero in the memory described
     * by ring_buffer_gpadl_handle. The downstream ring buffer follows it at
     * this offset (in pages).
     */
    uint32_t		downstream_ring_buffer_page_offset;

    /*
     * User-specific data to be passed along to the server endpoint.
     */
    uint8_t		user_data[HV_MAX_USER_DEFINED_BYTES];

} __packed hv_vmbus_channel_open_channel;

typedef uint32_t hv_nt_status;

/*
 * Open Channel Result parameters
 */
typedef struct
{
	hv_vmbus_channel_msg_header	header;
	uint32_t			child_rel_id;
	uint32_t			open_id;
	hv_nt_status			status;
} __packed hv_vmbus_channel_open_result;

/*
 * Close channel parameters
 */
typedef struct
{
	hv_vmbus_channel_msg_header	header;
	uint32_t			child_rel_id;
} __packed hv_vmbus_channel_close_channel;

/*
 * Channel Message GPADL
 */
#define HV_GPADL_TYPE_RING_BUFFER	1
#define HV_GPADL_TYPE_SERVER_SAVE_AREA	2
#define HV_GPADL_TYPE_TRANSACTION	8

/*
 * The number of PFNs in a GPADL message is defined by the number of pages
 * that would be spanned by byte_count and byte_offset.  If the implied number
 * of PFNs won't fit in this packet, there will be a follow-up packet that
 * contains more
 */

typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			child_rel_id;
	uint32_t			gpadl;
	uint16_t			range_buf_len;
	uint16_t			range_count;
	hv_gpa_range			range[0];
} __packed hv_vmbus_channel_gpadl_header;

/*
 * This is the follow-up packet that contains more PFNs
 */
typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			message_number;
	uint32_t 			gpadl;
	uint64_t 			pfn[0];
} __packed hv_vmbus_channel_gpadl_body;

typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			child_rel_id;
	uint32_t			gpadl;
	uint32_t			creation_status;
} __packed hv_vmbus_channel_gpadl_created;

typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			child_rel_id;
	uint32_t			gpadl;
} __packed hv_vmbus_channel_gpadl_teardown;

typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			gpadl;
} __packed hv_vmbus_channel_gpadl_torndown;

typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			child_rel_id;
} __packed hv_vmbus_channel_relid_released;

typedef struct {
	hv_vmbus_channel_msg_header	header;
	uint32_t			vmbus_version_requested;
	uint32_t			padding2;
	uint64_t			interrupt_page;
	uint64_t			monitor_page_1;
	uint64_t			monitor_page_2;
} __packed hv_vmbus_channel_initiate_contact;

typedef struct {
	hv_vmbus_channel_msg_header header;
	hv_bool_uint8_t		version_supported;
} __packed hv_vmbus_channel_version_response;

typedef hv_vmbus_channel_msg_header hv_vmbus_channel_unload;

#define HW_MACADDR_LEN	6

/*
 * Fixme:  Added to quiet "typeof" errors involving hv_vmbus.h when
 * the including C file was compiled with "-std=c99".
 */
#ifndef typeof
#define typeof __typeof
#endif

#ifndef NULL
#define NULL  (void *)0
#endif

typedef void *hv_vmbus_handle;

#ifndef CONTAINING_RECORD
#define CONTAINING_RECORD(address, type, field) ((type *)(	\
		(uint8_t *)(address) -				\
		(uint8_t *)(&((type *)0)->field)))
#endif /* CONTAINING_RECORD */


#define container_of(ptr, type, member) ({				\
		__typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
		(type *)( (char *)__mptr - offsetof(type,member) );})

enum {
	HV_VMBUS_IVAR_TYPE,
	HV_VMBUS_IVAR_INSTANCE,
	HV_VMBUS_IVAR_NODE,
	HV_VMBUS_IVAR_DEVCTX
};

#define HV_VMBUS_ACCESSOR(var, ivar, type) \
		__BUS_ACCESSOR(vmbus, var, HV_VMBUS, ivar, type)

HV_VMBUS_ACCESSOR(type, TYPE,  const char *)
HV_VMBUS_ACCESSOR(devctx, DEVCTX,  struct hv_device *)


/*
 * Common defines for Hyper-V ICs
 */
#define HV_ICMSGTYPE_NEGOTIATE		0
#define HV_ICMSGTYPE_HEARTBEAT		1
#define HV_ICMSGTYPE_KVPEXCHANGE	2
#define HV_ICMSGTYPE_SHUTDOWN		3
#define HV_ICMSGTYPE_TIMESYNC		4
#define HV_ICMSGTYPE_VSS		5

#define HV_ICMSGHDRFLAG_TRANSACTION	1
#define HV_ICMSGHDRFLAG_REQUEST		2
#define HV_ICMSGHDRFLAG_RESPONSE	4

typedef struct hv_vmbus_pipe_hdr {
	uint32_t flags;
	uint32_t msgsize;
} __packed hv_vmbus_pipe_hdr;

typedef struct hv_vmbus_ic_version {
	uint16_t major;
	uint16_t minor;
} __packed hv_vmbus_ic_version;

typedef struct hv_vmbus_icmsg_hdr {
	hv_vmbus_ic_version	icverframe;
	uint16_t		icmsgtype;
	hv_vmbus_ic_version	icvermsg;
	uint16_t		icmsgsize;
	uint32_t		status;
	uint8_t			ictransaction_id;
	uint8_t			icflags;
	uint8_t			reserved[2];
} __packed hv_vmbus_icmsg_hdr;

typedef struct hv_vmbus_icmsg_negotiate {
	uint16_t		icframe_vercnt;
	uint16_t		icmsg_vercnt;
	uint32_t		reserved;
	hv_vmbus_ic_version	icversion_data[1]; /* any size array */
} __packed hv_vmbus_icmsg_negotiate;

typedef struct hv_vmbus_shutdown_msg_data {
	uint32_t		reason_code;
	uint32_t		timeout_seconds;
	uint32_t 		flags;
	uint8_t			display_message[2048];
} __packed hv_vmbus_shutdown_msg_data;

typedef struct hv_vmbus_heartbeat_msg_data {
	uint64_t 		seq_num;
	uint32_t 		reserved[8];
} __packed hv_vmbus_heartbeat_msg_data;

typedef struct {
	/*
	 * offset in bytes from the start of ring data below
	 */
	volatile uint32_t       write_index;
	/*
	 * offset in bytes from the start of ring data below
	 */
	volatile uint32_t       read_index;
	/*
	 * NOTE: The interrupt_mask field is used only for channels, but
	 * vmbus connection also uses this data structure
	 */
	volatile uint32_t       interrupt_mask;
	/* pad it to PAGE_SIZE so that data starts on a page */
	uint8_t                 reserved[4084];

	/*
	 * WARNING: Ring data starts here + ring_data_start_offset
	 *  !!! DO NOT place any fields below this !!!
	 */
	uint8_t			buffer[0];	/* doubles as interrupt mask */
} __packed hv_vmbus_ring_buffer;

typedef struct {
	int		length;
	int		offset;
	uint64_t	pfn;
} __packed hv_vmbus_page_buffer;

typedef struct {
	int		length;
	int		offset;
	uint64_t	pfn_array[HV_MAX_MULTIPAGE_BUFFER_COUNT];
} __packed hv_vmbus_multipage_buffer;

typedef struct {
	hv_vmbus_ring_buffer*	ring_buffer;
	uint32_t		ring_size;	/* Include the shared header */
	struct mtx		ring_lock;
	uint32_t		ring_data_size;	/* ring_size */
	uint32_t		ring_data_start_offset;
} hv_vmbus_ring_buffer_info;

typedef void (*hv_vmbus_pfn_channel_callback)(void *context);
typedef void (*hv_vmbus_sc_creation_callback)(void *context);

typedef enum {
	HV_CHANNEL_OFFER_STATE,
	HV_CHANNEL_OPENING_STATE,
	HV_CHANNEL_OPEN_STATE,
	HV_CHANNEL_OPENED_STATE,
	HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE,
} hv_vmbus_channel_state;

/*
 *  Connection identifier type
 */
typedef union {
	uint32_t		as_uint32_t;
	struct {
		uint32_t	id:24;
		uint32_t	reserved:8;
	} u;

} __packed hv_vmbus_connection_id;

/*
 * Definition of the hv_vmbus_signal_event hypercall input structure
 */
typedef struct {
	hv_vmbus_connection_id	connection_id;
	uint16_t		flag_number;
	uint16_t		rsvd_z;
} __packed hv_vmbus_input_signal_event;

typedef struct {
	uint64_t			align8;
	hv_vmbus_input_signal_event	event;
} __packed hv_vmbus_input_signal_event_buffer;

typedef struct hv_vmbus_channel {
	TAILQ_ENTRY(hv_vmbus_channel)	list_entry;
	struct hv_device*		device;
	hv_vmbus_channel_state		state;
	hv_vmbus_channel_offer_channel	offer_msg;
	/*
	 * These are based on the offer_msg.monitor_id.
	 * Save it here for easy access.
	 */
	uint8_t				monitor_group;
	uint8_t				monitor_bit;

	uint32_t			ring_buffer_gpadl_handle;
	/*
	 * Allocated memory for ring buffer
	 */
	void*				ring_buffer_pages;
	unsigned long			ring_buffer_size;
	uint32_t			ring_buffer_page_count;
	/*
	 * send to parent
	 */
	hv_vmbus_ring_buffer_info	outbound;
	/*
	 * receive from parent
	 */
	hv_vmbus_ring_buffer_info	inbound;

	struct mtx			inbound_lock;

	struct taskqueue *		rxq;
	struct task			channel_task;
	hv_vmbus_pfn_channel_callback	on_channel_callback;
	void*				channel_callback_context;

	/*
	 * If batched_reading is set to "true", mask the interrupt
	 * and read until the channel is empty.
	 * If batched_reading is set to "false", the channel is not
	 * going to perform batched reading.
	 *
	 * Batched reading is enabled by default; specific
	 * drivers that don't want this behavior can turn it off.
	 */
	boolean_t			batched_reading;

	boolean_t			is_dedicated_interrupt;

	/*
	 * Used as an input param for HV_CALL_SIGNAL_EVENT hypercall.
	 */
	hv_vmbus_input_signal_event_buffer	signal_event_buffer;
	/*
	 * 8-bytes aligned of the buffer above
	 */
	hv_vmbus_input_signal_event	*signal_event_param;

	/*
	 * From Win8, this field specifies the target virtual process
	 * on which to deliver the interupt from the host to guest.
	 * Before Win8, all channel interrupts would only be
	 * delivered on cpu 0. Setting this value to 0 would preserve
	 * the earlier behavior.
	 */
	uint32_t			target_vcpu;
	/* The corresponding CPUID in the guest */
	uint32_t			target_cpu;

	/*
	 * Support for multi-channels.
	 * The initial offer is considered the primary channel and this
	 * offer message will indicate if the host supports multi-channels.
	 * The guest is free to ask for multi-channels to be offerred and can
	 * open these multi-channels as a normal "primary" channel. However,
	 * all multi-channels will have the same type and instance guids as the
	 * primary channel. Requests sent on a given channel will result in a
	 * response on the same channel.
	 */

	/*
	 * Multi-channel creation callback. This callback will be called in
	 * process context when a Multi-channel offer is received from the host.
	 * The guest can open the Multi-channel in the context of this callback.
	 */
	hv_vmbus_sc_creation_callback	sc_creation_callback;

	struct mtx			sc_lock;

	/*
	 * Link list of all the multi-channels if this is a primary channel
	 */
	TAILQ_HEAD(, hv_vmbus_channel)	sc_list_anchor;
	TAILQ_ENTRY(hv_vmbus_channel)	sc_list_entry;

	/*
	 * The primary channel this sub-channle belongs to.
	 * This will be NULL for the primary channel.
	 */
	struct hv_vmbus_channel		*primary_channel;
	/*
	 * Support per channel state for use by vmbus drivers.
	 */
	void				*per_channel_state;
} hv_vmbus_channel;

static inline void
hv_set_channel_read_state(hv_vmbus_channel* channel, boolean_t state)
{
	channel->batched_reading = state;
}

typedef struct hv_device {
	hv_guid		    class_id;
	hv_guid		    device_id;
	device_t	    device;
	hv_vmbus_channel*   channel;
} hv_device;



int		hv_vmbus_channel_recv_packet(
				hv_vmbus_channel*	channel,
				void*			buffer,
				uint32_t		buffer_len,
				uint32_t*		buffer_actual_len,
				uint64_t*		request_id);

int		hv_vmbus_channel_recv_packet_raw(
				hv_vmbus_channel*	channel,
				void*			buffer,
				uint32_t		buffer_len,
				uint32_t*		buffer_actual_len,
				uint64_t*		request_id);

int		hv_vmbus_channel_open(
				hv_vmbus_channel*	channel,
				uint32_t		send_ring_buffer_size,
				uint32_t		recv_ring_buffer_size,
				void*			user_data,
				uint32_t		user_data_len,
				hv_vmbus_pfn_channel_callback
							pfn_on_channel_callback,
				void*			context);

void		hv_vmbus_channel_close(hv_vmbus_channel *channel);

int		hv_vmbus_channel_send_packet(
				hv_vmbus_channel*	channel,
				void*			buffer,
				uint32_t		buffer_len,
				uint64_t		request_id,
				hv_vmbus_packet_type	type,
				uint32_t		flags);

int		hv_vmbus_channel_send_packet_pagebuffer(
				hv_vmbus_channel*	channel,
				hv_vmbus_page_buffer	page_buffers[],
				uint32_t		page_count,
				void*			buffer,
				uint32_t		buffer_len,
				uint64_t		request_id);

int		hv_vmbus_channel_send_packet_multipagebuffer(
				hv_vmbus_channel*	    channel,
				hv_vmbus_multipage_buffer*  multi_page_buffer,
				void*			    buffer,
				uint32_t		    buffer_len,
				uint64_t		    request_id);

int		hv_vmbus_channel_establish_gpadl(
				hv_vmbus_channel*	channel,
				/* must be phys and virt contiguous */
				void*			contig_buffer,
				/*  page-size multiple	*/
				uint32_t		size,
				uint32_t*		gpadl_handle);

int		hv_vmbus_channel_teardown_gpdal(
				hv_vmbus_channel*	channel,
				uint32_t		gpadl_handle);

struct hv_vmbus_channel* vmbus_select_outgoing_channel(struct hv_vmbus_channel *promary);

/*
 * Work abstraction defines
 */
typedef struct hv_work_queue {
	struct taskqueue*	queue;
	struct proc*		proc;
	struct sema*		work_sema;
} hv_work_queue;

typedef struct hv_work_item {
	struct task	work;
	void		(*callback)(void *);
	void*		context;
	hv_work_queue*	wq;
} hv_work_item;

struct hv_work_queue*	hv_work_queue_create(char* name);

void			hv_work_queue_close(struct hv_work_queue* wq);

int			hv_queue_work_item(
				hv_work_queue*	wq,
				void		(*callback)(void *),
				void*		context);
/**
 * @brief Get physical address from virtual
 */
static inline unsigned long
hv_get_phys_addr(void *virt)
{
	unsigned long ret;
	ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK));
	return (ret);
}


/**
 * KVP related structures
 * 
 */
typedef struct hv_vmbus_service {
        hv_guid       guid;             /* Hyper-V GUID */
        char          *name;            /* name of service */
        boolean_t     enabled;          /* service enabled */
        hv_work_queue *work_queue;      /* background work queue */

        /*
         * function to initialize service
         */
        int (*init)(struct hv_vmbus_service *);

        /*
         * function to process Hyper-V messages
         */
        void (*callback)(void *);
} hv_vmbus_service;

extern uint8_t* receive_buffer[];
extern hv_vmbus_service service_table[];
extern uint32_t hv_vmbus_protocal_version;

void hv_kvp_callback(void *context);
int hv_kvp_init(hv_vmbus_service *serv);
void hv_kvp_deinit(void);

#endif  /* __HYPERV_H__ */

OpenPOWER on IntegriCloud