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