]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - sys/dev/hyperv/vmbus/hv_vmbus_priv.h
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / sys / dev / hyperv / vmbus / hv_vmbus_priv.h
1 /*-
2  * Copyright (c) 2009-2012 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #ifndef __HYPERV_PRIV_H__
32 #define __HYPERV_PRIV_H__
33
34 #include <sys/param.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/sema.h>
38
39 #include <dev/hyperv/include/hyperv.h>
40
41
42 /*
43  *  Status codes for hypervisor operations.
44  */
45
46 typedef uint16_t hv_vmbus_status;
47
48 #define HV_MESSAGE_SIZE                 (256)
49 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT   (240)
50 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT  (30)
51 #define HV_ANY_VP                       (0xFFFFFFFF)
52
53 /*
54  * Synthetic interrupt controller flag constants.
55  */
56
57 #define HV_EVENT_FLAGS_COUNT        (256 * 8)
58 #define HV_EVENT_FLAGS_BYTE_COUNT   (256)
59 #define HV_EVENT_FLAGS_DWORD_COUNT  (256 / sizeof(uint32_t))
60
61 /**
62  * max channel count <== event_flags_dword_count * bit_of_dword
63  */
64 #define HV_CHANNEL_DWORD_LEN        (32)
65 #define HV_CHANNEL_MAX_COUNT        \
66         ((HV_EVENT_FLAGS_DWORD_COUNT) * HV_CHANNEL_DWORD_LEN)
67 /*
68  * MessageId: HV_STATUS_INSUFFICIENT_BUFFERS
69  * MessageText:
70  *    You did not supply enough message buffers to send a message.
71  */
72
73 #define HV_STATUS_INSUFFICIENT_BUFFERS   ((uint16_t)0x0013)
74
75 typedef void (*hv_vmbus_channel_callback)(void *context);
76
77 typedef struct {
78         void*           data;
79         uint32_t        length;
80 } hv_vmbus_sg_buffer_list;
81
82 typedef struct {
83         uint32_t        current_interrupt_mask;
84         uint32_t        current_read_index;
85         uint32_t        current_write_index;
86         uint32_t        bytes_avail_to_read;
87         uint32_t        bytes_avail_to_write;
88 } hv_vmbus_ring_buffer_debug_info;
89
90 typedef struct {
91         uint32_t                rel_id;
92         hv_vmbus_channel_state  state;
93         hv_guid                 interface_type;
94         hv_guid                 interface_instance;
95         uint32_t                monitor_id;
96         uint32_t                server_monitor_pending;
97         uint32_t                server_monitor_latency;
98         uint32_t                server_monitor_connection_id;
99         uint32_t                client_monitor_pending;
100         uint32_t                client_monitor_latency;
101         uint32_t                client_monitor_connection_id;
102         hv_vmbus_ring_buffer_debug_info inbound;
103         hv_vmbus_ring_buffer_debug_info outbound;
104 } hv_vmbus_channel_debug_info;
105
106 typedef union {
107         hv_vmbus_channel_version_supported      version_supported;
108         hv_vmbus_channel_open_result            open_result;
109         hv_vmbus_channel_gpadl_torndown         gpadl_torndown;
110         hv_vmbus_channel_gpadl_created          gpadl_created;
111         hv_vmbus_channel_version_response       version_response;
112 } hv_vmbus_channel_msg_response;
113
114 /*
115  * Represents each channel msg on the vmbus connection
116  * This is a variable-size data structure depending on
117  * the msg type itself
118  */
119 typedef struct hv_vmbus_channel_msg_info {
120         /*
121          * Bookkeeping stuff
122          */
123         TAILQ_ENTRY(hv_vmbus_channel_msg_info)  msg_list_entry;
124         /*
125          * So far, this is only used to handle
126          * gpadl body message
127          */
128         TAILQ_HEAD(, hv_vmbus_channel_msg_info) sub_msg_list_anchor;
129         /*
130          * Synchronize the request/response if
131          * needed.
132          * KYS: Use a semaphore for now.
133          * Not perf critical.
134          */
135         struct sema                             wait_sema;
136         hv_vmbus_channel_msg_response           response;
137         uint32_t                                message_size;
138         /**
139          * The channel message that goes out on
140          *  the "wire". It will contain at
141          *  minimum the
142          *  hv_vmbus_channel_msg_header
143          * header.
144          */
145         unsigned char                           msg[0];
146 } hv_vmbus_channel_msg_info;
147
148 /*
149  * The format must be the same as hv_vm_data_gpa_direct
150  */
151 typedef struct hv_vmbus_channel_packet_page_buffer {
152         uint16_t                type;
153         uint16_t                data_offset8;
154         uint16_t                length8;
155         uint16_t                flags;
156         uint64_t                transaction_id;
157         uint32_t                reserved;
158         uint32_t                range_count;
159         hv_vmbus_page_buffer    range[HV_MAX_PAGE_BUFFER_COUNT];
160 } __packed hv_vmbus_channel_packet_page_buffer;
161
162 /*
163  * The format must be the same as hv_vm_data_gpa_direct
164  */
165 typedef struct hv_vmbus_channel_packet_multipage_buffer {
166         uint16_t                        type;
167         uint16_t                        data_offset8;
168         uint16_t                        length8;
169         uint16_t                        flags;
170         uint64_t                        transaction_id;
171         uint32_t                        reserved;
172         uint32_t                        range_count; /* Always 1 in this case */
173         hv_vmbus_multipage_buffer       range;
174 } __packed hv_vmbus_channel_packet_multipage_buffer;
175
176 enum {
177         HV_VMBUS_MESSAGE_CONNECTION_ID  = 1,
178         HV_VMBUS_MESSAGE_PORT_ID        = 1,
179         HV_VMBUS_EVENT_CONNECTION_ID    = 2,
180         HV_VMBUS_EVENT_PORT_ID          = 2,
181         HV_VMBUS_MONITOR_CONNECTION_ID  = 3,
182         HV_VMBUS_MONITOR_PORT_ID        = 3,
183         HV_VMBUS_MESSAGE_SINT           = 2
184 };
185
186 #define HV_PRESENT_BIT          0x80000000
187
188 #define HV_HYPERCALL_PARAM_ALIGN sizeof(uint64_t)
189
190 typedef struct {
191         uint64_t        guest_id;
192         void*           hypercall_page;
193         hv_bool_uint8_t syn_ic_initialized;
194
195         hv_vmbus_handle syn_ic_msg_page[MAXCPU];
196         hv_vmbus_handle syn_ic_event_page[MAXCPU];
197         /*
198          * For FreeBSD cpuid to Hyper-V vcpuid mapping.
199          */
200         uint32_t        hv_vcpu_index[MAXCPU];
201         /*
202          * Each cpu has its own software interrupt handler for channel
203          * event and msg handling.
204          */
205         struct intr_event               *hv_event_intr_event[MAXCPU];
206         struct intr_event               *hv_msg_intr_event[MAXCPU];
207         void                            *event_swintr[MAXCPU];
208         void                            *msg_swintr[MAXCPU];
209         /*
210          * Host use this vector to intrrupt guest for vmbus channel
211          * event and msg.
212          */
213         unsigned int                    hv_cb_vector;
214 } hv_vmbus_context;
215
216 /*
217  * Define hypervisor message types
218  */
219 typedef enum {
220
221         HV_MESSAGE_TYPE_NONE                            = 0x00000000,
222
223         /*
224          * Memory access messages
225          */
226         HV_MESSAGE_TYPE_UNMAPPED_GPA                    = 0x80000000,
227         HV_MESSAGE_TYPE_GPA_INTERCEPT                   = 0x80000001,
228
229         /*
230          * Timer notification messages
231          */
232         HV_MESSAGE_TIMER_EXPIRED                        = 0x80000010,
233
234         /*
235          * Error messages
236          */
237         HV_MESSAGE_TYPE_INVALID_VP_REGISTER_VALUE       = 0x80000020,
238         HV_MESSAGE_TYPE_UNRECOVERABLE_EXCEPTION         = 0x80000021,
239         HV_MESSAGE_TYPE_UNSUPPORTED_FEATURE             = 0x80000022,
240
241         /*
242          * Trace buffer complete messages
243          */
244         HV_MESSAGE_TYPE_EVENT_LOG_BUFFER_COMPLETE       = 0x80000040,
245
246         /*
247          * Platform-specific processor intercept messages
248          */
249         HV_MESSAGE_TYPE_X64_IO_PORT_INTERCEPT           = 0x80010000,
250         HV_MESSAGE_TYPE_X64_MSR_INTERCEPT               = 0x80010001,
251         HV_MESSAGE_TYPE_X64_CPU_INTERCEPT               = 0x80010002,
252         HV_MESSAGE_TYPE_X64_EXCEPTION_INTERCEPT         = 0x80010003,
253         HV_MESSAGE_TYPE_X64_APIC_EOI                    = 0x80010004,
254         HV_MESSAGE_TYPE_X64_LEGACY_FP_ERROR             = 0x80010005
255
256 } hv_vmbus_msg_type;
257
258 /*
259  * Define port identifier type
260  */
261 typedef union _hv_vmbus_port_id {
262         uint32_t        as_uint32_t;
263         struct {
264                 uint32_t        id:24;
265                 uint32_t        reserved:8;
266         } u ;
267 } hv_vmbus_port_id;
268
269 /*
270  * Define synthetic interrupt controller message flag
271  */
272 typedef union {
273         uint8_t as_uint8_t;
274         struct {
275                 uint8_t message_pending:1;
276                 uint8_t reserved:7;
277         } u;
278 } hv_vmbus_msg_flags;
279
280 typedef uint64_t hv_vmbus_partition_id;
281
282 /*
283  * Define synthetic interrupt controller message header
284  */
285 typedef struct {
286         hv_vmbus_msg_type       message_type;
287         uint8_t                 payload_size;
288         hv_vmbus_msg_flags      message_flags;
289         uint8_t                 reserved[2];
290         union {
291                 hv_vmbus_partition_id   sender;
292                 hv_vmbus_port_id        port;
293         } u;
294 } hv_vmbus_msg_header;
295
296 /*
297  *  Define synthetic interrupt controller message format
298  */
299 typedef struct {
300         hv_vmbus_msg_header     header;
301         union {
302                 uint64_t        payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
303         } u ;
304 } hv_vmbus_message;
305
306 /*
307  *  Maximum channels is determined by the size of the interrupt
308  *  page which is PAGE_SIZE. 1/2 of PAGE_SIZE is for
309  *  send endpoint interrupt and the other is receive
310  *  endpoint interrupt.
311  *
312  *   Note: (PAGE_SIZE >> 1) << 3 allocates 16348 channels
313  */
314 #define HV_MAX_NUM_CHANNELS                     (PAGE_SIZE >> 1) << 3
315
316 /*
317  * (The value here must be in multiple of 32)
318  */
319 #define HV_MAX_NUM_CHANNELS_SUPPORTED           256
320
321 /*
322  * VM Bus connection states
323  */
324 typedef enum {
325         HV_DISCONNECTED,
326         HV_CONNECTING,
327         HV_CONNECTED,
328         HV_DISCONNECTING
329 } hv_vmbus_connect_state;
330
331 #define HV_MAX_SIZE_CHANNEL_MESSAGE     HV_MESSAGE_PAYLOAD_BYTE_COUNT
332
333
334 typedef struct {
335         hv_vmbus_connect_state                  connect_state;
336         uint32_t                                next_gpadl_handle;
337         /**
338          * Represents channel interrupts. Each bit position
339          * represents a channel.
340          * When a channel sends an interrupt via VMBUS, it
341          * finds its bit in the send_interrupt_page, set it and
342          * calls Hv to generate a port event. The other end
343          * receives the port event and parse the
344          * recv_interrupt_page to see which bit is set
345          */
346         void                                    *interrupt_page;
347         void                                    *send_interrupt_page;
348         void                                    *recv_interrupt_page;
349         /*
350          * 2 pages - 1st page for parent->child
351          * notification and 2nd is child->parent
352          * notification
353          */
354         void                                    *monitor_pages;
355         TAILQ_HEAD(, hv_vmbus_channel_msg_info) channel_msg_anchor;
356         struct mtx                              channel_msg_lock;
357         /**
358          * List of primary channels. Sub channels will be linked
359          * under their primary channel.
360          */
361         TAILQ_HEAD(, hv_vmbus_channel)          channel_anchor;
362         struct mtx                              channel_lock;
363
364         /**
365          * channel table for fast lookup through id.
366          */
367         hv_vmbus_channel                        **channels;
368         hv_vmbus_handle                         work_queue;
369         struct sema                             control_sema;
370 } hv_vmbus_connection;
371
372 typedef union {
373         uint64_t as_uint64_t;
374         struct {
375                 uint64_t build_number           : 16;
376                 uint64_t service_version        : 8; /* Service Pack, etc. */
377                 uint64_t minor_version          : 8;
378                 uint64_t major_version          : 8;
379                 /*
380                  * HV_GUEST_OS_MICROSOFT_IDS (If Vendor=MS)
381                  * HV_GUEST_OS_VENDOR
382                  */
383                 uint64_t os_id                  : 8;
384                 uint64_t vendor_id              : 16;
385         } u;
386 } hv_vmbus_x64_msr_guest_os_id_contents;
387
388
389 typedef union {
390         uint64_t as_uint64_t;
391         struct {
392                 uint64_t enable :1;
393                 uint64_t reserved :11;
394                 uint64_t guest_physical_address :52;
395         } u;
396 } hv_vmbus_x64_msr_hypercall_contents;
397
398 typedef union {
399         uint32_t as_uint32_t;
400         struct {
401                 uint32_t group_enable :4;
402                 uint32_t rsvd_z :28;
403         } u;
404 } hv_vmbus_monitor_trigger_state;
405
406 typedef union {
407         uint64_t as_uint64_t;
408         struct {
409                 uint32_t pending;
410                 uint32_t armed;
411         } u;
412 } hv_vmbus_monitor_trigger_group;
413
414 typedef struct {
415         hv_vmbus_connection_id  connection_id;
416         uint16_t                flag_number;
417         uint16_t                rsvd_z;
418 } hv_vmbus_monitor_parameter;
419
420 /*
421  * hv_vmbus_monitor_page Layout
422  * ------------------------------------------------------
423  * | 0   | trigger_state (4 bytes) | Rsvd1 (4 bytes)     |
424  * | 8   | trigger_group[0]                              |
425  * | 10  | trigger_group[1]                              |
426  * | 18  | trigger_group[2]                              |
427  * | 20  | trigger_group[3]                              |
428  * | 28  | Rsvd2[0]                                      |
429  * | 30  | Rsvd2[1]                                      |
430  * | 38  | Rsvd2[2]                                      |
431  * | 40  | next_check_time[0][0] | next_check_time[0][1] |
432  * | ...                                                 |
433  * | 240 | latency[0][0..3]                              |
434  * | 340 | Rsvz3[0]                                      |
435  * | 440 | parameter[0][0]                               |
436  * | 448 | parameter[0][1]                               |
437  * | ...                                                 |
438  * | 840 | Rsvd4[0]                                      |
439  * ------------------------------------------------------
440  */
441
442 typedef struct {
443         hv_vmbus_monitor_trigger_state  trigger_state;
444         uint32_t                        rsvd_z1;
445
446         hv_vmbus_monitor_trigger_group  trigger_group[4];
447         uint64_t                        rsvd_z2[3];
448
449         int32_t                         next_check_time[4][32];
450
451         uint16_t                        latency[4][32];
452         uint64_t                        rsvd_z3[32];
453
454         hv_vmbus_monitor_parameter      parameter[4][32];
455
456         uint8_t                         rsvd_z4[1984];
457 } hv_vmbus_monitor_page;
458
459 /*
460  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
461  * is set by CPUID(HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES).
462  */
463 typedef enum {
464         HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES                 = 0x00000001,
465         HV_CPU_ID_FUNCTION_HV_VENDOR_AND_MAX_FUNCTION           = 0x40000000,
466         HV_CPU_ID_FUNCTION_HV_INTERFACE                         = 0x40000001,
467         /*
468          * The remaining functions depend on the value
469          * of hv_cpu_id_function_interface
470          */
471         HV_CPU_ID_FUNCTION_MS_HV_VERSION                        = 0x40000002,
472         HV_CPU_ID_FUNCTION_MS_HV_FEATURES                       = 0x40000003,
473         HV_CPU_ID_FUNCTION_MS_HV_ENLIGHTENMENT_INFORMATION      = 0x40000004,
474         HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS          = 0x40000005
475
476 } hv_vmbus_cpuid_function;
477
478 /*
479  * Define the format of the SIMP register
480  */
481 typedef union {
482         uint64_t as_uint64_t;
483         struct {
484                 uint64_t simp_enabled   : 1;
485                 uint64_t preserved      : 11;
486                 uint64_t base_simp_gpa  : 52;
487         } u;
488 } hv_vmbus_synic_simp;
489
490 /*
491  * Define the format of the SIEFP register
492  */
493 typedef union {
494         uint64_t as_uint64_t;
495         struct {
496                 uint64_t siefp_enabled  : 1;
497                 uint64_t preserved      : 11;
498                 uint64_t base_siefp_gpa : 52;
499         } u;
500 } hv_vmbus_synic_siefp;
501
502 /*
503  * Define synthetic interrupt source
504  */
505 typedef union {
506         uint64_t as_uint64_t;
507         struct {
508                 uint64_t vector         : 8;
509                 uint64_t reserved1      : 8;
510                 uint64_t masked         : 1;
511                 uint64_t auto_eoi       : 1;
512                 uint64_t reserved2      : 46;
513         } u;
514 } hv_vmbus_synic_sint;
515
516 /*
517  * Timer configuration register.
518  */
519 union hv_timer_config {
520         uint64_t as_uint64;
521         struct {
522                 uint64_t enable:1;
523                 uint64_t periodic:1;
524                 uint64_t lazy:1;
525                 uint64_t auto_enable:1;
526                 uint64_t reserved_z0:12;
527                 uint64_t sintx:4;
528                 uint64_t reserved_z1:44;
529         };
530 };
531
532 /*
533  * Define syn_ic control register
534  */
535 typedef union _hv_vmbus_synic_scontrol {
536     uint64_t as_uint64_t;
537     struct {
538         uint64_t enable         : 1;
539         uint64_t reserved       : 63;
540     } u;
541 } hv_vmbus_synic_scontrol;
542
543 /*
544  *  Define the hv_vmbus_post_message hypercall input structure
545  */
546 typedef struct {
547         hv_vmbus_connection_id  connection_id;
548         uint32_t                reserved;
549         hv_vmbus_msg_type       message_type;
550         uint32_t                payload_size;
551         uint64_t                payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
552 } hv_vmbus_input_post_message;
553
554 /*
555  * Define the synthetic interrupt controller event flags format
556  */
557 typedef union {
558         uint8_t         flags8[HV_EVENT_FLAGS_BYTE_COUNT];
559         uint32_t        flags32[HV_EVENT_FLAGS_DWORD_COUNT];
560 } hv_vmbus_synic_event_flags;
561
562 #define HV_X64_CPUID_MIN        (0x40000005)
563 #define HV_X64_CPUID_MAX        (0x4000ffff)
564
565 /*
566  * Declare the MSR used to identify the guest OS
567  */
568 #define HV_X64_MSR_GUEST_OS_ID  (0x40000000)
569 /*
570  *  Declare the MSR used to setup pages used to communicate with the hypervisor
571  */
572 #define HV_X64_MSR_HYPERCALL    (0x40000001)
573 /* MSR used to provide vcpu index */
574 #define HV_X64_MSR_VP_INDEX     (0x40000002)
575
576 #define HV_X64_MSR_TIME_REF_COUNT      (0x40000020)
577
578 /*
579  * Define synthetic interrupt controller model specific registers
580  */
581 #define HV_X64_MSR_SCONTROL   (0x40000080)
582 #define HV_X64_MSR_SVERSION   (0x40000081)
583 #define HV_X64_MSR_SIEFP      (0x40000082)
584 #define HV_X64_MSR_SIMP       (0x40000083)
585 #define HV_X64_MSR_EOM        (0x40000084)
586
587 #define HV_X64_MSR_SINT0      (0x40000090)
588 #define HV_X64_MSR_SINT1      (0x40000091)
589 #define HV_X64_MSR_SINT2      (0x40000092)
590 #define HV_X64_MSR_SINT3      (0x40000093)
591 #define HV_X64_MSR_SINT4      (0x40000094)
592 #define HV_X64_MSR_SINT5      (0x40000095)
593 #define HV_X64_MSR_SINT6      (0x40000096)
594 #define HV_X64_MSR_SINT7      (0x40000097)
595 #define HV_X64_MSR_SINT8      (0x40000098)
596 #define HV_X64_MSR_SINT9      (0x40000099)
597 #define HV_X64_MSR_SINT10     (0x4000009A)
598 #define HV_X64_MSR_SINT11     (0x4000009B)
599 #define HV_X64_MSR_SINT12     (0x4000009C)
600 #define HV_X64_MSR_SINT13     (0x4000009D)
601 #define HV_X64_MSR_SINT14     (0x4000009E)
602 #define HV_X64_MSR_SINT15     (0x4000009F)
603
604 /*
605  * Synthetic Timer MSRs. Four timers per vcpu.
606  */
607 #define HV_X64_MSR_STIMER0_CONFIG               0x400000B0
608 #define HV_X64_MSR_STIMER0_COUNT                0x400000B1
609 #define HV_X64_MSR_STIMER1_CONFIG               0x400000B2
610 #define HV_X64_MSR_STIMER1_COUNT                0x400000B3
611 #define HV_X64_MSR_STIMER2_CONFIG               0x400000B4
612 #define HV_X64_MSR_STIMER2_COUNT                0x400000B5
613 #define HV_X64_MSR_STIMER3_CONFIG               0x400000B6
614 #define HV_X64_MSR_STIMER3_COUNT                0x400000B7
615
616 /*
617  * Declare the various hypercall operations
618  */
619 typedef enum {
620         HV_CALL_POST_MESSAGE    = 0x005c,
621         HV_CALL_SIGNAL_EVENT    = 0x005d,
622 } hv_vmbus_call_code;
623
624 /**
625  * Global variables
626  */
627
628 extern hv_vmbus_context         hv_vmbus_g_context;
629 extern hv_vmbus_connection      hv_vmbus_g_connection;
630
631 typedef void (*vmbus_msg_handler)(hv_vmbus_channel_msg_header *msg);
632
633 typedef struct hv_vmbus_channel_msg_table_entry {
634         hv_vmbus_channel_msg_type    messageType;
635
636         bool   handler_no_sleep; /* true: the handler doesn't sleep */
637         vmbus_msg_handler   messageHandler;
638 } hv_vmbus_channel_msg_table_entry;
639
640 extern hv_vmbus_channel_msg_table_entry g_channel_message_table[];
641
642 /*
643  * Private, VM Bus functions
644  */
645
646 int                     hv_vmbus_ring_buffer_init(
647                                 hv_vmbus_ring_buffer_info       *ring_info,
648                                 void                            *buffer,
649                                 uint32_t                        buffer_len);
650
651 void                    hv_ring_buffer_cleanup(
652                                 hv_vmbus_ring_buffer_info       *ring_info);
653
654 int                     hv_ring_buffer_write(
655                                 hv_vmbus_ring_buffer_info       *ring_info,
656                                 hv_vmbus_sg_buffer_list         sg_buffers[],
657                                 uint32_t                        sg_buff_count,
658                                 boolean_t                       *need_sig);
659
660 int                     hv_ring_buffer_peek(
661                                 hv_vmbus_ring_buffer_info       *ring_info,
662                                 void                            *buffer,
663                                 uint32_t                        buffer_len);
664
665 int                     hv_ring_buffer_read(
666                                 hv_vmbus_ring_buffer_info       *ring_info,
667                                 void                            *buffer,
668                                 uint32_t                        buffer_len,
669                                 uint32_t                        offset);
670
671 uint32_t                hv_vmbus_get_ring_buffer_interrupt_mask(
672                                 hv_vmbus_ring_buffer_info       *ring_info);
673
674 void                    hv_vmbus_dump_ring_info(
675                                 hv_vmbus_ring_buffer_info       *ring_info,
676                                 char                            *prefix);
677
678 void                    hv_ring_buffer_read_begin(
679                                 hv_vmbus_ring_buffer_info       *ring_info);
680
681 uint32_t                hv_ring_buffer_read_end(
682                                 hv_vmbus_ring_buffer_info       *ring_info);
683
684 hv_vmbus_channel*       hv_vmbus_allocate_channel(void);
685 void                    hv_vmbus_free_vmbus_channel(hv_vmbus_channel *channel);
686 void                    hv_vmbus_on_channel_message(void *context);
687 int                     hv_vmbus_request_channel_offers(void);
688 void                    hv_vmbus_release_unattached_channels(void);
689 int                     hv_vmbus_init(void);
690 void                    hv_vmbus_cleanup(void);
691
692 uint16_t                hv_vmbus_post_msg_via_msg_ipc(
693                                 hv_vmbus_connection_id  connection_id,
694                                 hv_vmbus_msg_type       message_type,
695                                 void                    *payload,
696                                 size_t                  payload_size);
697
698 uint16_t                hv_vmbus_signal_event(void *con_id);
699 void                    hv_vmbus_synic_init(void *irq_arg);
700 void                    hv_vmbus_synic_cleanup(void *arg);
701 int                     hv_vmbus_query_hypervisor_presence(void);
702
703 struct hv_device*       hv_vmbus_child_device_create(
704                                 hv_guid                 device_type,
705                                 hv_guid                 device_instance,
706                                 hv_vmbus_channel        *channel);
707
708 int                     hv_vmbus_child_device_register(
709                                         struct hv_device *child_dev);
710 int                     hv_vmbus_child_device_unregister(
711                                         struct hv_device *child_dev);
712
713 /**
714  * Connection interfaces
715  */
716 int                     hv_vmbus_connect(void);
717 int                     hv_vmbus_disconnect(void);
718 int                     hv_vmbus_post_message(void *buffer, size_t buf_size);
719 int                     hv_vmbus_set_event(hv_vmbus_channel *channel);
720 void                    hv_vmbus_on_events(void *);
721
722 /**
723  * Event Timer interfaces
724  */
725 void                    hv_et_init(void);
726 void                    hv_et_intr(struct trapframe*);
727
728 /*
729  * The guest OS needs to register the guest ID with the hypervisor.
730  * The guest ID is a 64 bit entity and the structure of this ID is
731  * specified in the Hyper-V specification:
732  *
733  * http://msdn.microsoft.com/en-us/library/windows/
734  * hardware/ff542653%28v=vs.85%29.aspx
735  *
736  * While the current guideline does not specify how FreeBSD guest ID(s)
737  * need to be generated, our plan is to publish the guidelines for
738  * FreeBSD and other guest operating systems that currently are hosted
739  * on Hyper-V. The implementation here conforms to this yet
740  * unpublished guidelines.
741  *
742  * Bit(s)
743  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
744  * 62:56 - Os Type; Linux is 0x100, FreeBSD is 0x200
745  * 55:48 - Distro specific identification
746  * 47:16 - FreeBSD kernel version number
747  * 15:0  - Distro specific identification
748  *
749  */
750
751 #define HV_FREEBSD_VENDOR_ID    0x8200
752 #define HV_FREEBSD_GUEST_ID     hv_generate_guest_id(0,0)
753
754 static inline  uint64_t hv_generate_guest_id(
755         uint8_t distro_id_part1,
756         uint16_t distro_id_part2)
757 {
758         uint64_t guest_id;
759         guest_id =  (((uint64_t)HV_FREEBSD_VENDOR_ID) << 48);
760         guest_id |= (((uint64_t)(distro_id_part1)) << 48);
761         guest_id |= (((uint64_t)(__FreeBSD_version)) << 16); /* in param.h */
762         guest_id |= ((uint64_t)(distro_id_part2));
763         return guest_id;
764 }
765
766 typedef struct {
767         unsigned int    vector;
768         void            *page_buffers[2 * MAXCPU];
769 } hv_setup_args;
770
771 #endif  /* __HYPERV_PRIV_H__ */