2 * Copyright (c) 2009-2012 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
30 * HyperV definitions for messages that are sent between instances of the
31 * Channel Management Library in separate partitions, or in some cases,
38 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/malloc.h>
42 #include <sys/kthread.h>
43 #include <sys/taskqueue.h>
44 #include <sys/systm.h>
47 #include <sys/mutex.h>
50 #include <vm/vm_param.h>
53 #include <amd64/include/xen/synch_bitops.h>
54 #include <amd64/include/atomic.h>
56 typedef uint8_t hv_bool_uint8_t;
58 #define HV_S_OK 0x00000000
59 #define HV_E_FAIL 0x80004005
60 #define HV_ERROR_NOT_SUPPORTED 0x80070032
61 #define HV_ERROR_MACHINE_LOCKED 0x800704F7
64 * A revision number of vmbus that is used for ensuring both ends on a
65 * partition are using compatible versions.
68 #define HV_VMBUS_REVISION_NUMBER 13
71 * Make maximum size of pipe payload of 16K
74 #define HV_MAX_PIPE_DATA_PAYLOAD (sizeof(BYTE) * 16384)
77 * Define pipe_mode values
80 #define HV_VMBUS_PIPE_TYPE_BYTE 0x00000000
81 #define HV_VMBUS_PIPE_TYPE_MESSAGE 0x00000004
84 * The size of the user defined data buffer for non-pipe offers
87 #define HV_MAX_USER_DEFINED_BYTES 120
90 * The size of the user defined data buffer for pipe offers
93 #define HV_MAX_PIPE_USER_DEFINED_BYTES 116
96 #define HV_MAX_PAGE_BUFFER_COUNT 16
97 #define HV_MAX_MULTIPAGE_BUFFER_COUNT 32
99 #define HV_ALIGN_UP(value, align) \
100 (((value) & (align-1)) ? \
101 (((value) + (align-1)) & ~(align-1) ) : (value))
103 #define HV_ALIGN_DOWN(value, align) ( (value) & ~(align-1) )
105 #define HV_NUM_PAGES_SPANNED(addr, len) \
106 ((HV_ALIGN_UP(addr+len, PAGE_SIZE) - \
107 HV_ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
109 typedef struct hv_guid {
110 unsigned char data[16];
114 * At the center of the Channel Management library is
115 * the Channel Offer. This struct contains the
116 * fundamental information about an offer.
119 typedef struct hv_vmbus_channel_offer {
120 hv_guid interface_type;
121 hv_guid interface_instance;
122 uint64_t interrupt_latency_in_100ns_units;
123 uint32_t interface_revision;
124 uint32_t server_context_area_size; /* in bytes */
125 uint16_t channel_flags;
126 uint16_t mmio_megabytes; /* in bytes * 1024 * 1024 */
130 * Non-pipes: The user has HV_MAX_USER_DEFINED_BYTES bytes.
133 uint8_t user_defined[HV_MAX_USER_DEFINED_BYTES];
137 * Pipes: The following structure is an integrated pipe protocol, which
138 * is implemented on top of standard user-defined data. pipe
139 * clients have HV_MAX_PIPE_USER_DEFINED_BYTES left for their
144 uint8_t user_defined[HV_MAX_PIPE_USER_DEFINED_BYTES];
150 } __packed hv_vmbus_channel_offer;
152 typedef uint32_t hv_gpadl_handle;
156 uint16_t data_offset8;
159 uint64_t transaction_id;
160 } __packed hv_vm_packet_descriptor;
162 typedef uint32_t hv_previous_packet_offset;
165 hv_previous_packet_offset previous_packet_start_offset;
166 hv_vm_packet_descriptor descriptor;
167 } __packed hv_vm_packet_header;
171 uint32_t byte_offset;
172 } __packed hv_vm_transfer_page;
175 hv_vm_packet_descriptor d;
176 uint16_t transfer_page_set_id;
177 hv_bool_uint8_t sender_owns_set;
179 uint32_t range_count;
180 hv_vm_transfer_page ranges[1];
181 } __packed hv_vm_transfer_page_packet_header;
184 hv_vm_packet_descriptor d;
187 } __packed hv_vm_gpadl_packet_header;
190 hv_vm_packet_descriptor d;
192 uint16_t transfer_page_set_id;
194 } __packed hv_vm_add_remove_transfer_page_set;
197 * This structure defines a range in guest
198 * physical space that can be made
199 * to look virtually contiguous.
204 uint32_t byte_offset;
205 uint64_t pfn_array[0];
206 } __packed hv_gpa_range;
209 * This is the format for an Establish Gpadl packet, which contains a handle
210 * by which this GPADL will be known and a set of GPA ranges associated with
211 * it. This can be converted to a MDL by the guest OS. If there are multiple
212 * GPA ranges, then the resulting MDL will be "chained," representing multiple
217 hv_vm_packet_descriptor d;
219 uint32_t range_count;
220 hv_gpa_range range[1];
221 } __packed hv_vm_establish_gpadl;
224 * This is the format for a Teardown Gpadl packet, which indicates that the
225 * GPADL handle in the Establish Gpadl packet will never be referenced again.
229 hv_vm_packet_descriptor d;
231 /* for alignment to a 8-byte boundary */
233 } __packed hv_vm_teardown_gpadl;
236 * This is the format for a GPA-Direct packet, which contains a set of GPA
237 * ranges, in addition to commands and/or data.
241 hv_vm_packet_descriptor d;
243 uint32_t range_count;
244 hv_gpa_range range[1];
245 } __packed hv_vm_data_gpa_direct;
248 * This is the format for a Additional data Packet.
251 hv_vm_packet_descriptor d;
252 uint64_t total_bytes;
253 uint32_t byte_offset;
256 } __packed hv_vm_additional_data;
259 hv_vm_packet_descriptor simple_header;
260 hv_vm_transfer_page_packet_header transfer_page_header;
261 hv_vm_gpadl_packet_header gpadl_header;
262 hv_vm_add_remove_transfer_page_set add_remove_transfer_page_header;
263 hv_vm_establish_gpadl establish_gpadl_header;
264 hv_vm_teardown_gpadl teardown_gpadl_header;
265 hv_vm_data_gpa_direct data_gpa_direct_header;
266 } __packed hv_vm_packet_largest_possible_header;
269 HV_VMBUS_PACKET_TYPE_INVALID = 0x0,
270 HV_VMBUS_PACKET_TYPES_SYNCH = 0x1,
271 HV_VMBUS_PACKET_TYPE_ADD_TRANSFER_PAGE_SET = 0x2,
272 HV_VMBUS_PACKET_TYPE_REMOVE_TRANSFER_PAGE_SET = 0x3,
273 HV_VMBUS_PACKET_TYPE_ESTABLISH_GPADL = 0x4,
274 HV_VMBUS_PACKET_TYPE_TEAR_DOWN_GPADL = 0x5,
275 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND = 0x6,
276 HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES = 0x7,
277 HV_VMBUS_PACKET_TYPE_DATA_USING_GPADL = 0x8,
278 HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT = 0x9,
279 HV_VMBUS_PACKET_TYPE_CANCEL_REQUEST = 0xa,
280 HV_VMBUS_PACKET_TYPE_COMPLETION = 0xb,
281 HV_VMBUS_PACKET_TYPE_DATA_USING_ADDITIONAL_PACKETS = 0xc,
282 HV_VMBUS_PACKET_TYPE_ADDITIONAL_DATA = 0xd
283 } hv_vmbus_packet_type;
285 #define HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
291 HV_CHANNEL_MESSAGE_INVALID = 0,
292 HV_CHANNEL_MESSAGE_OFFER_CHANNEL = 1,
293 HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER = 2,
294 HV_CHANNEL_MESSAGE_REQUEST_OFFERS = 3,
295 HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED = 4,
296 HV_CHANNEL_MESSAGE_OPEN_CHANNEL = 5,
297 HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT = 6,
298 HV_CHANNEL_MESSAGE_CLOSE_CHANNEL = 7,
299 HV_CHANNEL_MESSAGEL_GPADL_HEADER = 8,
300 HV_CHANNEL_MESSAGE_GPADL_BODY = 9,
301 HV_CHANNEL_MESSAGE_GPADL_CREATED = 10,
302 HV_CHANNEL_MESSAGE_GPADL_TEARDOWN = 11,
303 HV_CHANNEL_MESSAGE_GPADL_TORNDOWN = 12,
304 HV_CHANNEL_MESSAGE_REL_ID_RELEASED = 13,
305 HV_CHANNEL_MESSAGE_INITIATED_CONTACT = 14,
306 HV_CHANNEL_MESSAGE_VERSION_RESPONSE = 15,
307 HV_CHANNEL_MESSAGE_UNLOAD = 16,
309 #ifdef HV_VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
310 HV_CHANNEL_MESSAGE_VIEW_RANGE_ADD = 17,
311 HV_CHANNEL_MESSAGE_VIEW_RANGE_REMOVE = 18,
313 HV_CHANNEL_MESSAGE_COUNT
314 } hv_vmbus_channel_msg_type;
317 hv_vmbus_channel_msg_type message_type;
319 } __packed hv_vmbus_channel_msg_header;
322 * Query VMBus Version parameters
325 hv_vmbus_channel_msg_header header;
327 } __packed hv_vmbus_channel_query_vmbus_version;
330 * VMBus Version Supported parameters
333 hv_vmbus_channel_msg_header header;
334 hv_bool_uint8_t version_supported;
335 } __packed hv_vmbus_channel_version_supported;
338 * Channel Offer parameters
341 hv_vmbus_channel_msg_header header;
342 hv_vmbus_channel_offer offer;
343 uint32_t child_rel_id;
345 hv_bool_uint8_t monitor_allocated;
346 } __packed hv_vmbus_channel_offer_channel;
349 * Rescind Offer parameters
353 hv_vmbus_channel_msg_header header;
354 uint32_t child_rel_id;
355 } __packed hv_vmbus_channel_rescind_offer;
359 * Request Offer -- no parameters, SynIC message contains the partition ID
361 * Set Snoop -- no parameters, SynIC message contains the partition ID
363 * Clear Snoop -- no parameters, SynIC message contains the partition ID
365 * All Offers Delivered -- no parameters, SynIC message contains the
368 * Flush Client -- no parameters, SynIC message contains the partition ID
373 * Open Channel parameters
377 hv_vmbus_channel_msg_header header;
380 * Identifies the specific VMBus channel that is being opened.
382 uint32_t child_rel_id;
385 * ID making a particular open request at a channel offer unique.
390 * GPADL for the channel's ring buffer.
392 hv_gpadl_handle ring_buffer_gpadl_handle;
395 * GPADL for the channel's server context save area.
397 hv_gpadl_handle server_context_area_gpadl_handle;
400 * The upstream ring buffer begins at offset zero in the memory described
401 * by ring_buffer_gpadl_handle. The downstream ring buffer follows it at
402 * this offset (in pages).
404 uint32_t downstream_ring_buffer_page_offset;
407 * User-specific data to be passed along to the server endpoint.
409 uint8_t user_data[HV_MAX_USER_DEFINED_BYTES];
411 } __packed hv_vmbus_channel_open_channel;
413 typedef uint32_t hv_nt_status;
416 * Open Channel Result parameters
420 hv_vmbus_channel_msg_header header;
421 uint32_t child_rel_id;
424 } __packed hv_vmbus_channel_open_result;
427 * Close channel parameters
431 hv_vmbus_channel_msg_header header;
432 uint32_t child_rel_id;
433 } __packed hv_vmbus_channel_close_channel;
436 * Channel Message GPADL
438 #define HV_GPADL_TYPE_RING_BUFFER 1
439 #define HV_GPADL_TYPE_SERVER_SAVE_AREA 2
440 #define HV_GPADL_TYPE_TRANSACTION 8
443 * The number of PFNs in a GPADL message is defined by the number of pages
444 * that would be spanned by byte_count and byte_offset. If the implied number
445 * of PFNs won't fit in this packet, there will be a follow-up packet that
450 hv_vmbus_channel_msg_header header;
451 uint32_t child_rel_id;
453 uint16_t range_buf_len;
454 uint16_t range_count;
455 hv_gpa_range range[0];
456 } __packed hv_vmbus_channel_gpadl_header;
459 * This is the follow-up packet that contains more PFNs
462 hv_vmbus_channel_msg_header header;
463 uint32_t message_number;
466 } __packed hv_vmbus_channel_gpadl_body;
469 hv_vmbus_channel_msg_header header;
470 uint32_t child_rel_id;
472 uint32_t creation_status;
473 } __packed hv_vmbus_channel_gpadl_created;
476 hv_vmbus_channel_msg_header header;
477 uint32_t child_rel_id;
479 } __packed hv_vmbus_channel_gpadl_teardown;
482 hv_vmbus_channel_msg_header header;
484 } __packed hv_vmbus_channel_gpadl_torndown;
487 hv_vmbus_channel_msg_header header;
488 uint32_t child_rel_id;
489 } __packed hv_vmbus_channel_relid_released;
492 hv_vmbus_channel_msg_header header;
493 uint32_t vmbus_version_requested;
495 uint64_t interrupt_page;
496 uint64_t monitor_page_1;
497 uint64_t monitor_page_2;
498 } __packed hv_vmbus_channel_initiate_contact;
501 hv_vmbus_channel_msg_header header;
502 hv_bool_uint8_t version_supported;
503 } __packed hv_vmbus_channel_version_response;
505 typedef hv_vmbus_channel_msg_header hv_vmbus_channel_unload;
507 #define HW_MACADDR_LEN 6
510 * Fixme: Added to quiet "typeof" errors involving hv_vmbus.h when
511 * the including C file was compiled with "-std=c99".
514 #define typeof __typeof
518 #define NULL (void *)0
521 typedef void *hv_vmbus_handle;
523 #ifndef CONTAINING_RECORD
524 #define CONTAINING_RECORD(address, type, field) ((type *)( \
525 (uint8_t *)(address) - \
526 (uint8_t *)(&((type *)0)->field)))
527 #endif /* CONTAINING_RECORD */
530 #define container_of(ptr, type, member) ({ \
531 __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
532 (type *)( (char *)__mptr - offsetof(type,member) );})
536 HV_VMBUS_IVAR_INSTANCE,
541 #define HV_VMBUS_ACCESSOR(var, ivar, type) \
542 __BUS_ACCESSOR(vmbus, var, HV_VMBUS, ivar, type)
544 HV_VMBUS_ACCESSOR(type, TYPE, const char *)
545 HV_VMBUS_ACCESSOR(devctx, DEVCTX, struct hv_device *)
549 * Common defines for Hyper-V ICs
551 #define HV_ICMSGTYPE_NEGOTIATE 0
552 #define HV_ICMSGTYPE_HEARTBEAT 1
553 #define HV_ICMSGTYPE_KVPEXCHANGE 2
554 #define HV_ICMSGTYPE_SHUTDOWN 3
555 #define HV_ICMSGTYPE_TIMESYNC 4
556 #define HV_ICMSGTYPE_VSS 5
558 #define HV_ICMSGHDRFLAG_TRANSACTION 1
559 #define HV_ICMSGHDRFLAG_REQUEST 2
560 #define HV_ICMSGHDRFLAG_RESPONSE 4
562 typedef struct hv_vmbus_pipe_hdr {
565 } __packed hv_vmbus_pipe_hdr;
567 typedef struct hv_vmbus_ic_version {
570 } __packed hv_vmbus_ic_version;
572 typedef struct hv_vmbus_icmsg_hdr {
573 hv_vmbus_ic_version icverframe;
575 hv_vmbus_ic_version icvermsg;
578 uint8_t ictransaction_id;
581 } __packed hv_vmbus_icmsg_hdr;
583 typedef struct hv_vmbus_icmsg_negotiate {
584 uint16_t icframe_vercnt;
585 uint16_t icmsg_vercnt;
587 hv_vmbus_ic_version icversion_data[1]; /* any size array */
588 } __packed hv_vmbus_icmsg_negotiate;
590 typedef struct hv_vmbus_shutdown_msg_data {
591 uint32_t reason_code;
592 uint32_t timeout_seconds;
594 uint8_t display_message[2048];
595 } __packed hv_vmbus_shutdown_msg_data;
597 typedef struct hv_vmbus_heartbeat_msg_data {
599 uint32_t reserved[8];
600 } __packed hv_vmbus_heartbeat_msg_data;
604 * offset in bytes from the start of ring data below
606 volatile uint32_t write_index;
608 * offset in bytes from the start of ring data below
610 volatile uint32_t read_index;
612 * NOTE: The interrupt_mask field is used only for channels, but
613 * vmbus connection also uses this data structure
615 volatile uint32_t interrupt_mask;
616 /* pad it to PAGE_SIZE so that data starts on a page */
617 uint8_t reserved[4084];
620 * WARNING: Ring data starts here + ring_data_start_offset
621 * !!! DO NOT place any fields below this !!!
623 uint8_t buffer[0]; /* doubles as interrupt mask */
624 } __packed hv_vmbus_ring_buffer;
630 } __packed hv_vmbus_page_buffer;
635 uint64_t pfn_array[HV_MAX_MULTIPAGE_BUFFER_COUNT];
636 } __packed hv_vmbus_multipage_buffer;
639 hv_vmbus_ring_buffer* ring_buffer;
640 uint32_t ring_size; /* Include the shared header */
641 struct mtx ring_lock;
642 uint32_t ring_data_size; /* ring_size */
643 uint32_t ring_data_start_offset;
644 } hv_vmbus_ring_buffer_info;
646 typedef void (*hv_vmbus_pfn_channel_callback)(void *context);
649 HV_CHANNEL_OFFER_STATE,
650 HV_CHANNEL_OPENING_STATE,
651 HV_CHANNEL_OPEN_STATE,
652 HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE,
653 } hv_vmbus_channel_state;
655 typedef struct hv_vmbus_channel {
656 TAILQ_ENTRY(hv_vmbus_channel) list_entry;
657 struct hv_device* device;
658 hv_vmbus_channel_state state;
659 hv_vmbus_channel_offer_channel offer_msg;
661 * These are based on the offer_msg.monitor_id.
662 * Save it here for easy access.
664 uint8_t monitor_group;
667 uint32_t ring_buffer_gpadl_handle;
669 * Allocated memory for ring buffer
671 void* ring_buffer_pages;
672 uint32_t ring_buffer_page_count;
676 hv_vmbus_ring_buffer_info outbound;
678 * receive from parent
680 hv_vmbus_ring_buffer_info inbound;
682 struct mtx inbound_lock;
683 hv_vmbus_handle control_work_queue;
685 hv_vmbus_pfn_channel_callback on_channel_callback;
686 void* channel_callback_context;
690 typedef struct hv_device {
694 hv_vmbus_channel* channel;
699 int hv_vmbus_channel_recv_packet(
700 hv_vmbus_channel* channel,
703 uint32_t* buffer_actual_len,
704 uint64_t* request_id);
706 int hv_vmbus_channel_recv_packet_raw(
707 hv_vmbus_channel* channel,
710 uint32_t* buffer_actual_len,
711 uint64_t* request_id);
713 int hv_vmbus_channel_open(
714 hv_vmbus_channel* channel,
715 uint32_t send_ring_buffer_size,
716 uint32_t recv_ring_buffer_size,
718 uint32_t user_data_len,
719 hv_vmbus_pfn_channel_callback
720 pfn_on_channel_callback,
723 void hv_vmbus_channel_close(hv_vmbus_channel *channel);
725 int hv_vmbus_channel_send_packet(
726 hv_vmbus_channel* channel,
730 hv_vmbus_packet_type type,
733 int hv_vmbus_channel_send_packet_pagebuffer(
734 hv_vmbus_channel* channel,
735 hv_vmbus_page_buffer page_buffers[],
739 uint64_t request_id);
741 int hv_vmbus_channel_send_packet_multipagebuffer(
742 hv_vmbus_channel* channel,
743 hv_vmbus_multipage_buffer* multi_page_buffer,
746 uint64_t request_id);
748 int hv_vmbus_channel_establish_gpadl(
749 hv_vmbus_channel* channel,
750 /* must be phys and virt contiguous */
752 /* page-size multiple */
754 uint32_t* gpadl_handle);
756 int hv_vmbus_channel_teardown_gpdal(
757 hv_vmbus_channel* channel,
758 uint32_t gpadl_handle);
761 * Work abstraction defines
763 typedef struct hv_work_queue {
764 struct taskqueue* queue;
766 struct sema* work_sema;
769 typedef struct hv_work_item {
771 void (*callback)(void *);
776 struct hv_work_queue* hv_work_queue_create(char* name);
778 void hv_work_queue_close(struct hv_work_queue* wq);
780 int hv_queue_work_item(
782 void (*callback)(void *),
785 * @brief Get physical address from virtual
787 static inline unsigned long
788 hv_get_phys_addr(void *virt)
791 ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK));
795 #endif /* __HYPERV_H__ */