]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/vmbus/hv_vmbus_priv.h
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.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 /*
185  *  Connection identifier type
186  */
187 typedef union {
188         uint32_t                as_uint32_t;
189         struct {
190                 uint32_t        id:24;
191                 uint32_t        reserved:8;
192         } u;
193
194 } __packed hv_vmbus_connection_id;
195
196 /*
197  * Definition of the hv_vmbus_signal_event hypercall input structure
198  */
199 typedef struct {
200         hv_vmbus_connection_id  connection_id;
201         uint16_t                flag_number;
202         uint16_t                rsvd_z;
203 } __packed hv_vmbus_input_signal_event;
204
205 typedef struct {
206         uint64_t                        align8;
207         hv_vmbus_input_signal_event     event;
208 } __packed hv_vmbus_input_signal_event_buffer;
209
210 typedef struct {
211         uint64_t        guest_id;
212         void*           hypercall_page;
213         hv_bool_uint8_t syn_ic_initialized;
214         /*
215          * This is used as an input param to HV_CALL_SIGNAL_EVENT hypercall.
216          * The input param is immutable  in our usage and
217          * must be dynamic mem (vs stack or global).
218          */
219         hv_vmbus_input_signal_event_buffer      *signal_event_buffer;
220         /*
221          * 8-bytes aligned of the buffer above
222          */
223         hv_vmbus_input_signal_event             *signal_event_param;
224
225         hv_vmbus_handle syn_ic_msg_page[MAXCPU];
226         hv_vmbus_handle syn_ic_event_page[MAXCPU];
227 } hv_vmbus_context;
228
229 /*
230  * Define hypervisor message types
231  */
232 typedef enum {
233
234         HV_MESSAGE_TYPE_NONE                            = 0x00000000,
235
236         /*
237          * Memory access messages
238          */
239         HV_MESSAGE_TYPE_UNMAPPED_GPA                    = 0x80000000,
240         HV_MESSAGE_TYPE_GPA_INTERCEPT                   = 0x80000001,
241
242         /*
243          * Timer notification messages
244          */
245         HV_MESSAGE_TIMER_EXPIRED                        = 0x80000010,
246
247         /*
248          * Error messages
249          */
250         HV_MESSAGE_TYPE_INVALID_VP_REGISTER_VALUE       = 0x80000020,
251         HV_MESSAGE_TYPE_UNRECOVERABLE_EXCEPTION         = 0x80000021,
252         HV_MESSAGE_TYPE_UNSUPPORTED_FEATURE             = 0x80000022,
253
254         /*
255          * Trace buffer complete messages
256          */
257         HV_MESSAGE_TYPE_EVENT_LOG_BUFFER_COMPLETE       = 0x80000040,
258
259         /*
260          * Platform-specific processor intercept messages
261          */
262         HV_MESSAGE_TYPE_X64_IO_PORT_INTERCEPT           = 0x80010000,
263         HV_MESSAGE_TYPE_X64_MSR_INTERCEPT               = 0x80010001,
264         HV_MESSAGE_TYPE_X64_CPU_INTERCEPT               = 0x80010002,
265         HV_MESSAGE_TYPE_X64_EXCEPTION_INTERCEPT         = 0x80010003,
266         HV_MESSAGE_TYPE_X64_APIC_EOI                    = 0x80010004,
267         HV_MESSAGE_TYPE_X64_LEGACY_FP_ERROR             = 0x80010005
268
269 } hv_vmbus_msg_type;
270
271 /*
272  * Define port identifier type
273  */
274 typedef union _hv_vmbus_port_id {
275         uint32_t        as_uint32_t;
276         struct {
277                 uint32_t        id:24;
278                 uint32_t        reserved:8;
279         } u ;
280 } hv_vmbus_port_id;
281
282 /*
283  * Define synthetic interrupt controller message flag
284  */
285 typedef union {
286         uint8_t as_uint8_t;
287         struct {
288                 uint8_t message_pending:1;
289                 uint8_t reserved:7;
290         } u;
291 } hv_vmbus_msg_flags;
292
293 typedef uint64_t hv_vmbus_partition_id;
294
295 /*
296  * Define synthetic interrupt controller message header
297  */
298 typedef struct {
299         hv_vmbus_msg_type       message_type;
300         uint8_t                 payload_size;
301         hv_vmbus_msg_flags      message_flags;
302         uint8_t                 reserved[2];
303         union {
304                 hv_vmbus_partition_id   sender;
305                 hv_vmbus_port_id        port;
306         } u;
307 } hv_vmbus_msg_header;
308
309 /*
310  *  Define synthetic interrupt controller message format
311  */
312 typedef struct {
313         hv_vmbus_msg_header     header;
314         union {
315                 uint64_t        payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
316         } u ;
317 } hv_vmbus_message;
318
319 /*
320  *  Maximum channels is determined by the size of the interrupt
321  *  page which is PAGE_SIZE. 1/2 of PAGE_SIZE is for
322  *  send endpoint interrupt and the other is receive
323  *  endpoint interrupt.
324  *
325  *   Note: (PAGE_SIZE >> 1) << 3 allocates 16348 channels
326  */
327 #define HV_MAX_NUM_CHANNELS                     (PAGE_SIZE >> 1) << 3
328
329 /*
330  * (The value here must be in multiple of 32)
331  */
332 #define HV_MAX_NUM_CHANNELS_SUPPORTED           256
333
334 /*
335  * VM Bus connection states
336  */
337 typedef enum {
338         HV_DISCONNECTED,
339         HV_CONNECTING,
340         HV_CONNECTED,
341         HV_DISCONNECTING
342 } hv_vmbus_connect_state;
343
344 #define HV_MAX_SIZE_CHANNEL_MESSAGE     HV_MESSAGE_PAYLOAD_BYTE_COUNT
345
346
347 typedef struct {
348         hv_vmbus_connect_state                  connect_state;
349         uint32_t                                next_gpadl_handle;
350         /**
351          * Represents channel interrupts. Each bit position
352          * represents a channel.
353          * When a channel sends an interrupt via VMBUS, it
354          * finds its bit in the send_interrupt_page, set it and
355          * calls Hv to generate a port event. The other end
356          * receives the port event and parse the
357          * recv_interrupt_page to see which bit is set
358          */
359         void                                    *interrupt_page;
360         void                                    *send_interrupt_page;
361         void                                    *recv_interrupt_page;
362         /*
363          * 2 pages - 1st page for parent->child
364          * notification and 2nd is child->parent
365          * notification
366          */
367         void                                    *monitor_pages;
368         TAILQ_HEAD(, hv_vmbus_channel_msg_info) channel_msg_anchor;
369         struct mtx                              channel_msg_lock;
370         /**
371          * List of channels
372          */
373         TAILQ_HEAD(, hv_vmbus_channel)          channel_anchor;
374         struct mtx                              channel_lock;
375
376         hv_vmbus_handle                         work_queue;
377         struct sema                             control_sema;
378 } hv_vmbus_connection;
379
380 /*
381  * Declare the MSR used to identify the guest OS
382  */
383 #define HV_X64_MSR_GUEST_OS_ID  0x40000000
384
385 typedef union {
386         uint64_t as_uint64_t;
387         struct {
388                 uint64_t build_number           : 16;
389                 uint64_t service_version        : 8; /* Service Pack, etc. */
390                 uint64_t minor_version          : 8;
391                 uint64_t major_version          : 8;
392                 /*
393                  * HV_GUEST_OS_MICROSOFT_IDS (If Vendor=MS)
394                  * HV_GUEST_OS_VENDOR
395                  */
396                 uint64_t os_id                  : 8;
397                 uint64_t vendor_id              : 16;
398         } u;
399 } hv_vmbus_x64_msr_guest_os_id_contents;
400
401 /*
402  *  Declare the MSR used to setup pages used to communicate with the hypervisor
403  */
404 #define HV_X64_MSR_HYPERCALL    0x40000001
405
406 typedef union {
407         uint64_t as_uint64_t;
408         struct {
409                 uint64_t enable :1;
410                 uint64_t reserved :11;
411                 uint64_t guest_physical_address :52;
412         } u;
413 } hv_vmbus_x64_msr_hypercall_contents;
414
415 typedef union {
416         uint32_t as_uint32_t;
417         struct {
418                 uint32_t group_enable :4;
419                 uint32_t rsvd_z :28;
420         } u;
421 } hv_vmbus_monitor_trigger_state;
422
423 typedef union {
424         uint64_t as_uint64_t;
425         struct {
426                 uint32_t pending;
427                 uint32_t armed;
428         } u;
429 } hv_vmbus_monitor_trigger_group;
430
431 typedef struct {
432         hv_vmbus_connection_id  connection_id;
433         uint16_t                flag_number;
434         uint16_t                rsvd_z;
435 } hv_vmbus_monitor_parameter;
436
437 /*
438  * hv_vmbus_monitor_page Layout
439  * ------------------------------------------------------
440  * | 0   | trigger_state (4 bytes) | Rsvd1 (4 bytes)     |
441  * | 8   | trigger_group[0]                              |
442  * | 10  | trigger_group[1]                              |
443  * | 18  | trigger_group[2]                              |
444  * | 20  | trigger_group[3]                              |
445  * | 28  | Rsvd2[0]                                      |
446  * | 30  | Rsvd2[1]                                      |
447  * | 38  | Rsvd2[2]                                      |
448  * | 40  | next_check_time[0][0] | next_check_time[0][1] |
449  * | ...                                                 |
450  * | 240 | latency[0][0..3]                              |
451  * | 340 | Rsvz3[0]                                      |
452  * | 440 | parameter[0][0]                               |
453  * | 448 | parameter[0][1]                               |
454  * | ...                                                 |
455  * | 840 | Rsvd4[0]                                      |
456  * ------------------------------------------------------
457  */
458
459 typedef struct {
460         hv_vmbus_monitor_trigger_state  trigger_state;
461         uint32_t                        rsvd_z1;
462
463         hv_vmbus_monitor_trigger_group  trigger_group[4];
464         uint64_t                        rsvd_z2[3];
465
466         int32_t                         next_check_time[4][32];
467
468         uint16_t                        latency[4][32];
469         uint64_t                        rsvd_z3[32];
470
471         hv_vmbus_monitor_parameter      parameter[4][32];
472
473         uint8_t                         rsvd_z4[1984];
474 } hv_vmbus_monitor_page;
475
476 /*
477  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
478  * is set by CPUID(HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES).
479  */
480 typedef enum {
481         HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES                 = 0x00000001,
482         HV_CPU_ID_FUNCTION_HV_VENDOR_AND_MAX_FUNCTION           = 0x40000000,
483         HV_CPU_ID_FUNCTION_HV_INTERFACE                         = 0x40000001,
484         /*
485          * The remaining functions depend on the value
486          * of hv_cpu_id_function_interface
487          */
488         HV_CPU_ID_FUNCTION_MS_HV_VERSION                        = 0x40000002,
489         HV_CPU_ID_FUNCTION_MS_HV_FEATURES                       = 0x40000003,
490         HV_CPU_ID_FUNCTION_MS_HV_ENLIGHTENMENT_INFORMATION      = 0x40000004,
491         HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS          = 0x40000005
492
493 } hv_vmbus_cpuid_function;
494
495 /*
496  * Define the format of the SIMP register
497  */
498 typedef union {
499         uint64_t as_uint64_t;
500         struct {
501                 uint64_t simp_enabled   : 1;
502                 uint64_t preserved      : 11;
503                 uint64_t base_simp_gpa  : 52;
504         } u;
505 } hv_vmbus_synic_simp;
506
507 /*
508  * Define the format of the SIEFP register
509  */
510 typedef union {
511         uint64_t as_uint64_t;
512         struct {
513                 uint64_t siefp_enabled  : 1;
514                 uint64_t preserved      : 11;
515                 uint64_t base_siefp_gpa : 52;
516         } u;
517 } hv_vmbus_synic_siefp;
518
519 /*
520  * Define synthetic interrupt source
521  */
522 typedef union {
523         uint64_t as_uint64_t;
524         struct {
525                 uint64_t vector         : 8;
526                 uint64_t reserved1      : 8;
527                 uint64_t masked         : 1;
528                 uint64_t auto_eoi       : 1;
529                 uint64_t reserved2      : 46;
530         } u;
531 } hv_vmbus_synic_sint;
532
533 /*
534  * Define syn_ic control register
535  */
536 typedef union _hv_vmbus_synic_scontrol {
537     uint64_t as_uint64_t;
538     struct {
539         uint64_t enable         : 1;
540         uint64_t reserved       : 63;
541     } u;
542 } hv_vmbus_synic_scontrol;
543
544 /*
545  *  Define the hv_vmbus_post_message hypercall input structure
546  */
547 typedef struct {
548         hv_vmbus_connection_id  connection_id;
549         uint32_t                reserved;
550         hv_vmbus_msg_type       message_type;
551         uint32_t                payload_size;
552         uint64_t                payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
553 } hv_vmbus_input_post_message;
554
555 /*
556  * Define the synthetic interrupt controller event flags format
557  */
558 typedef union {
559         uint8_t         flags8[HV_EVENT_FLAGS_BYTE_COUNT];
560         uint32_t        flags32[HV_EVENT_FLAGS_DWORD_COUNT];
561 } hv_vmbus_synic_event_flags;
562
563
564 /*
565  * Define synthetic interrupt controller model specific registers
566  */
567 #define HV_X64_MSR_SCONTROL   (0x40000080)
568 #define HV_X64_MSR_SVERSION   (0x40000081)
569 #define HV_X64_MSR_SIEFP      (0x40000082)
570 #define HV_X64_MSR_SIMP       (0x40000083)
571 #define HV_X64_MSR_EOM        (0x40000084)
572
573 #define HV_X64_MSR_SINT0      (0x40000090)
574 #define HV_X64_MSR_SINT1      (0x40000091)
575 #define HV_X64_MSR_SINT2      (0x40000092)
576 #define HV_X64_MSR_SINT3      (0x40000093)
577 #define HV_X64_MSR_SINT4      (0x40000094)
578 #define HV_X64_MSR_SINT5      (0x40000095)
579 #define HV_X64_MSR_SINT6      (0x40000096)
580 #define HV_X64_MSR_SINT7      (0x40000097)
581 #define HV_X64_MSR_SINT8      (0x40000098)
582 #define HV_X64_MSR_SINT9      (0x40000099)
583 #define HV_X64_MSR_SINT10     (0x4000009A)
584 #define HV_X64_MSR_SINT11     (0x4000009B)
585 #define HV_X64_MSR_SINT12     (0x4000009C)
586 #define HV_X64_MSR_SINT13     (0x4000009D)
587 #define HV_X64_MSR_SINT14     (0x4000009E)
588 #define HV_X64_MSR_SINT15     (0x4000009F)
589
590 /*
591  * Declare the various hypercall operations
592  */
593 typedef enum {
594         HV_CALL_POST_MESSAGE    = 0x005c,
595         HV_CALL_SIGNAL_EVENT    = 0x005d,
596 } hv_vmbus_call_code;
597
598 /**
599  * Global variables
600  */
601
602 extern hv_vmbus_context         hv_vmbus_g_context;
603 extern hv_vmbus_connection      hv_vmbus_g_connection;
604
605
606 /*
607  * Private, VM Bus functions
608  */
609
610 int                     hv_vmbus_ring_buffer_init(
611                                 hv_vmbus_ring_buffer_info       *ring_info,
612                                 void                            *buffer,
613                                 uint32_t                        buffer_len);
614
615 void                    hv_ring_buffer_cleanup(
616                                 hv_vmbus_ring_buffer_info       *ring_info);
617
618 int                     hv_ring_buffer_write(
619                                 hv_vmbus_ring_buffer_info       *ring_info,
620                                 hv_vmbus_sg_buffer_list         sg_buffers[],
621                                 uint32_t                        sg_buff_count);
622
623 int                     hv_ring_buffer_peek(
624                                 hv_vmbus_ring_buffer_info       *ring_info,
625                                 void                            *buffer,
626                                 uint32_t                        buffer_len);
627
628 int                     hv_ring_buffer_read(
629                                 hv_vmbus_ring_buffer_info       *ring_info,
630                                 void                            *buffer,
631                                 uint32_t                        buffer_len,
632                                 uint32_t                        offset);
633
634 uint32_t                hv_vmbus_get_ring_buffer_interrupt_mask(
635                                 hv_vmbus_ring_buffer_info       *ring_info);
636
637 void                    hv_vmbus_dump_ring_info(
638                                 hv_vmbus_ring_buffer_info       *ring_info,
639                                 char                            *prefix);
640
641 hv_vmbus_channel*       hv_vmbus_allocate_channel(void);
642 void                    hv_vmbus_free_vmbus_channel(hv_vmbus_channel *channel);
643 void                    hv_vmbus_on_channel_message(void *context);
644 int                     hv_vmbus_request_channel_offers(void);
645 void                    hv_vmbus_release_unattached_channels(void);
646 int                     hv_vmbus_init(void);
647 void                    hv_vmbus_cleanup(void);
648
649 uint16_t                hv_vmbus_post_msg_via_msg_ipc(
650                                 hv_vmbus_connection_id  connection_id,
651                                 hv_vmbus_msg_type       message_type,
652                                 void                    *payload,
653                                 size_t                  payload_size);
654
655 uint16_t                hv_vmbus_signal_event(void);
656 void                    hv_vmbus_synic_init(void *irq_arg);
657 void                    hv_vmbus_synic_cleanup(void *arg);
658 int                     hv_vmbus_query_hypervisor_presence(void);
659
660 struct hv_device*       hv_vmbus_child_device_create(
661                                 hv_guid                 device_type,
662                                 hv_guid                 device_instance,
663                                 hv_vmbus_channel        *channel);
664
665 int                     hv_vmbus_child_device_register(
666                                         struct hv_device *child_dev);
667 int                     hv_vmbus_child_device_unregister(
668                                         struct hv_device *child_dev);
669 hv_vmbus_channel*       hv_vmbus_get_channel_from_rel_id(uint32_t rel_id);
670
671 /**
672  * Connection interfaces
673  */
674 int                     hv_vmbus_connect(void);
675 int                     hv_vmbus_disconnect(void);
676 int                     hv_vmbus_post_message(void *buffer, size_t buf_size);
677 int                     hv_vmbus_set_event(uint32_t child_rel_id);
678 void                    hv_vmbus_on_events(void *);
679
680
681 /*
682  * The guest OS needs to register the guest ID with the hypervisor.
683  * The guest ID is a 64 bit entity and the structure of this ID is
684  * specified in the Hyper-V specification:
685  *
686  * http://msdn.microsoft.com/en-us/library/windows/
687  * hardware/ff542653%28v=vs.85%29.aspx
688  *
689  * While the current guideline does not specify how FreeBSD guest ID(s)
690  * need to be generated, our plan is to publish the guidelines for
691  * FreeBSD and other guest operating systems that currently are hosted
692  * on Hyper-V. The implementation here conforms to this yet
693  * unpublished guidelines.
694  *
695  * Bit(s)
696  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
697  * 62:56 - Os Type; Linux is 0x100, FreeBSD is 0x200
698  * 55:48 - Distro specific identification
699  * 47:16 - FreeBSD kernel version number
700  * 15:0  - Distro specific identification
701  *
702  */
703
704 #define HV_FREEBSD_VENDOR_ID    0x8200
705 #define HV_FREEBSD_GUEST_ID     hv_generate_guest_id(0,0)
706
707 static inline  uint64_t hv_generate_guest_id(
708         uint8_t distro_id_part1,
709         uint16_t distro_id_part2)
710 {
711         uint64_t guest_id;
712         guest_id =  (((uint64_t)HV_FREEBSD_VENDOR_ID) << 48);
713         guest_id |= (((uint64_t)(distro_id_part1)) << 48);
714         guest_id |= (((uint64_t)(__FreeBSD_version)) << 16); /* in param.h */
715         guest_id |= ((uint64_t)(distro_id_part2));
716         return guest_id;
717 }
718
719 typedef struct {
720         unsigned int    vector;
721         void            *page_buffers[2];
722 } hv_setup_args;
723
724 #endif  /* __HYPERV_PRIV_H__ */