3 * Copyright (c) 2011, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 * K. Y. Srinivasan <kys@microsoft.com>
28 #include <linux/scatterlist.h>
29 #include <linux/list.h>
30 #include <linux/uuid.h>
31 #include <linux/timer.h>
32 #include <linux/workqueue.h>
33 #include <linux/completion.h>
34 #include <linux/device.h>
35 #include <linux/mod_devicetable.h>
38 #define MAX_PAGE_BUFFER_COUNT 19
39 #define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
43 /* Single-page buffer */
44 struct hv_page_buffer
{
50 /* Multiple-page buffer */
51 struct hv_multipage_buffer
{
52 /* Length and Offset determines the # of pfns in the array */
55 u64 pfn_array
[MAX_MULTIPAGE_BUFFER_COUNT
];
58 /* 0x18 includes the proprietary packet header */
59 #define MAX_PAGE_BUFFER_PACKET (0x18 + \
60 (sizeof(struct hv_page_buffer) * \
61 MAX_PAGE_BUFFER_COUNT))
62 #define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
63 sizeof(struct hv_multipage_buffer))
68 struct hv_ring_buffer
{
69 /* Offset in bytes from the start of ring data below */
72 /* Offset in bytes from the start of ring data below */
77 /* Pad it to PAGE_SIZE so that data starts on page boundary */
81 * The interrupt_mask field is used only for channels but since our
82 * vmbus connection also uses this data structure and its data starts
83 * here, we commented out this field.
87 * Ring data starts here + RingDataStartOffset
88 * !!! DO NOT place any fields below this !!!
93 struct hv_ring_buffer_info
{
94 struct hv_ring_buffer
*ring_buffer
;
95 u32 ring_size
; /* Include the shared header */
98 u32 ring_datasize
; /* < ring_size */
99 u32 ring_data_startoffset
;
102 struct hv_ring_buffer_debug_info
{
103 u32 current_interrupt_mask
;
104 u32 current_read_index
;
105 u32 current_write_index
;
106 u32 bytes_avail_toread
;
107 u32 bytes_avail_towrite
;
111 * We use the same version numbering for all Hyper-V modules.
113 * Definition of versioning is as follows;
115 * Major Number Changes for these scenarios;
116 * 1. When a new version of Windows Hyper-V
118 * 2. A Major change has occurred in the
120 * (For example the merge for the first time
121 * into the kernel) Every time the Major Number
122 * changes, the Revision number is reset to 0.
123 * Minor Number Changes when new functionality is added
124 * to the Linux IC's that is not a bug fix.
126 * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
128 #define HV_DRV_VERSION "3.1"
132 * A revision number of vmbus that is used for ensuring both ends on a
133 * partition are using compatible versions.
135 #define VMBUS_REVISION_NUMBER 13
137 /* Make maximum size of pipe payload of 16K */
138 #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
140 /* Define PipeMode values. */
141 #define VMBUS_PIPE_TYPE_BYTE 0x00000000
142 #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
144 /* The size of the user defined data buffer for non-pipe offers. */
145 #define MAX_USER_DEFINED_BYTES 120
147 /* The size of the user defined data buffer for pipe offers. */
148 #define MAX_PIPE_USER_DEFINED_BYTES 116
151 * At the center of the Channel Management library is the Channel Offer. This
152 * struct contains the fundamental information about an offer.
154 struct vmbus_channel_offer
{
157 u64 int_latency
; /* in 100ns units */
159 u32 server_ctx_size
; /* in bytes */
161 u16 mmio_megabytes
; /* in bytes * 1024 * 1024 */
164 /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
166 unsigned char user_def
[MAX_USER_DEFINED_BYTES
];
171 * The following sructure is an integrated pipe protocol, which
172 * is implemented on top of standard user-defined data. Pipe
173 * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
178 unsigned char user_def
[MAX_PIPE_USER_DEFINED_BYTES
];
185 #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
186 #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
187 #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
188 #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
189 #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
190 #define VMBUS_CHANNEL_PARENT_OFFER 0x200
191 #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
193 struct vmpacket_descriptor
{
201 struct vmpacket_header
{
202 u32 prev_pkt_start_offset
;
203 struct vmpacket_descriptor descriptor
;
206 struct vmtransfer_page_range
{
211 struct vmtransfer_page_packet_header
{
212 struct vmpacket_descriptor d
;
214 bool sender_owns_set
;
217 struct vmtransfer_page_range ranges
[1];
220 struct vmgpadl_packet_header
{
221 struct vmpacket_descriptor d
;
226 struct vmadd_remove_transfer_page_set
{
227 struct vmpacket_descriptor d
;
234 * This structure defines a range in guest physical space that can be made to
235 * look virtually contiguous.
244 * This is the format for an Establish Gpadl packet, which contains a handle by
245 * which this GPADL will be known and a set of GPA ranges associated with it.
246 * This can be converted to a MDL by the guest OS. If there are multiple GPA
247 * ranges, then the resulting MDL will be "chained," representing multiple VA
250 struct vmestablish_gpadl
{
251 struct vmpacket_descriptor d
;
254 struct gpa_range range
[1];
258 * This is the format for a Teardown Gpadl packet, which indicates that the
259 * GPADL handle in the Establish Gpadl packet will never be referenced again.
261 struct vmteardown_gpadl
{
262 struct vmpacket_descriptor d
;
264 u32 reserved
; /* for alignment to a 8-byte boundary */
268 * This is the format for a GPA-Direct packet, which contains a set of GPA
269 * ranges, in addition to commands and/or data.
271 struct vmdata_gpa_direct
{
272 struct vmpacket_descriptor d
;
275 struct gpa_range range
[1];
278 /* This is the format for a Additional Data Packet. */
279 struct vmadditional_data
{
280 struct vmpacket_descriptor d
;
284 unsigned char data
[1];
287 union vmpacket_largest_possible_header
{
288 struct vmpacket_descriptor simple_hdr
;
289 struct vmtransfer_page_packet_header xfer_page_hdr
;
290 struct vmgpadl_packet_header gpadl_hdr
;
291 struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr
;
292 struct vmestablish_gpadl establish_gpadl_hdr
;
293 struct vmteardown_gpadl teardown_gpadl_hdr
;
294 struct vmdata_gpa_direct data_gpa_direct_hdr
;
297 #define VMPACKET_DATA_START_ADDRESS(__packet) \
298 (void *)(((unsigned char *)__packet) + \
299 ((struct vmpacket_descriptor)__packet)->offset8 * 8)
301 #define VMPACKET_DATA_LENGTH(__packet) \
302 ((((struct vmpacket_descriptor)__packet)->len8 - \
303 ((struct vmpacket_descriptor)__packet)->offset8) * 8)
305 #define VMPACKET_TRANSFER_MODE(__packet) \
306 (((struct IMPACT)__packet)->type)
308 enum vmbus_packet_type
{
309 VM_PKT_INVALID
= 0x0,
311 VM_PKT_ADD_XFER_PAGESET
= 0x2,
312 VM_PKT_RM_XFER_PAGESET
= 0x3,
313 VM_PKT_ESTABLISH_GPADL
= 0x4,
314 VM_PKT_TEARDOWN_GPADL
= 0x5,
315 VM_PKT_DATA_INBAND
= 0x6,
316 VM_PKT_DATA_USING_XFER_PAGES
= 0x7,
317 VM_PKT_DATA_USING_GPADL
= 0x8,
318 VM_PKT_DATA_USING_GPA_DIRECT
= 0x9,
319 VM_PKT_CANCEL_REQUEST
= 0xa,
321 VM_PKT_DATA_USING_ADDITIONAL_PKT
= 0xc,
322 VM_PKT_ADDITIONAL_DATA
= 0xd
325 #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
328 /* Version 1 messages */
329 enum vmbus_channel_message_type
{
330 CHANNELMSG_INVALID
= 0,
331 CHANNELMSG_OFFERCHANNEL
= 1,
332 CHANNELMSG_RESCIND_CHANNELOFFER
= 2,
333 CHANNELMSG_REQUESTOFFERS
= 3,
334 CHANNELMSG_ALLOFFERS_DELIVERED
= 4,
335 CHANNELMSG_OPENCHANNEL
= 5,
336 CHANNELMSG_OPENCHANNEL_RESULT
= 6,
337 CHANNELMSG_CLOSECHANNEL
= 7,
338 CHANNELMSG_GPADL_HEADER
= 8,
339 CHANNELMSG_GPADL_BODY
= 9,
340 CHANNELMSG_GPADL_CREATED
= 10,
341 CHANNELMSG_GPADL_TEARDOWN
= 11,
342 CHANNELMSG_GPADL_TORNDOWN
= 12,
343 CHANNELMSG_RELID_RELEASED
= 13,
344 CHANNELMSG_INITIATE_CONTACT
= 14,
345 CHANNELMSG_VERSION_RESPONSE
= 15,
346 CHANNELMSG_UNLOAD
= 16,
347 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
348 CHANNELMSG_VIEWRANGE_ADD
= 17,
349 CHANNELMSG_VIEWRANGE_REMOVE
= 18,
354 struct vmbus_channel_message_header
{
355 enum vmbus_channel_message_type msgtype
;
359 /* Query VMBus Version parameters */
360 struct vmbus_channel_query_vmbus_version
{
361 struct vmbus_channel_message_header header
;
365 /* VMBus Version Supported parameters */
366 struct vmbus_channel_version_supported
{
367 struct vmbus_channel_message_header header
;
368 bool version_supported
;
371 /* Offer Channel parameters */
372 struct vmbus_channel_offer_channel
{
373 struct vmbus_channel_message_header header
;
374 struct vmbus_channel_offer offer
;
377 bool monitor_allocated
;
380 /* Rescind Offer parameters */
381 struct vmbus_channel_rescind_offer
{
382 struct vmbus_channel_message_header header
;
387 * Request Offer -- no parameters, SynIC message contains the partition ID
388 * Set Snoop -- no parameters, SynIC message contains the partition ID
389 * Clear Snoop -- no parameters, SynIC message contains the partition ID
390 * All Offers Delivered -- no parameters, SynIC message contains the partition
392 * Flush Client -- no parameters, SynIC message contains the partition ID
395 /* Open Channel parameters */
396 struct vmbus_channel_open_channel
{
397 struct vmbus_channel_message_header header
;
399 /* Identifies the specific VMBus channel that is being opened. */
402 /* ID making a particular open request at a channel offer unique. */
405 /* GPADL for the channel's ring buffer. */
406 u32 ringbuffer_gpadlhandle
;
408 /* GPADL for the channel's server context save area. */
409 u32 server_contextarea_gpadlhandle
;
412 * The upstream ring buffer begins at offset zero in the memory
413 * described by RingBufferGpadlHandle. The downstream ring buffer
414 * follows it at this offset (in pages).
416 u32 downstream_ringbuffer_pageoffset
;
418 /* User-specific data to be passed along to the server endpoint. */
419 unsigned char userdata
[MAX_USER_DEFINED_BYTES
];
422 /* Open Channel Result parameters */
423 struct vmbus_channel_open_result
{
424 struct vmbus_channel_message_header header
;
430 /* Close channel parameters; */
431 struct vmbus_channel_close_channel
{
432 struct vmbus_channel_message_header header
;
436 /* Channel Message GPADL */
437 #define GPADL_TYPE_RING_BUFFER 1
438 #define GPADL_TYPE_SERVER_SAVE_AREA 2
439 #define GPADL_TYPE_TRANSACTION 8
442 * The number of PFNs in a GPADL message is defined by the number of
443 * pages that would be spanned by ByteCount and ByteOffset. If the
444 * implied number of PFNs won't fit in this packet, there will be a
445 * follow-up packet that contains more.
447 struct vmbus_channel_gpadl_header
{
448 struct vmbus_channel_message_header header
;
453 struct gpa_range range
[0];
456 /* This is the followup packet that contains more PFNs. */
457 struct vmbus_channel_gpadl_body
{
458 struct vmbus_channel_message_header header
;
464 struct vmbus_channel_gpadl_created
{
465 struct vmbus_channel_message_header header
;
471 struct vmbus_channel_gpadl_teardown
{
472 struct vmbus_channel_message_header header
;
477 struct vmbus_channel_gpadl_torndown
{
478 struct vmbus_channel_message_header header
;
482 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
483 struct vmbus_channel_view_range_add
{
484 struct vmbus_channel_message_header header
;
485 PHYSICAL_ADDRESS viewrange_base
;
486 u64 viewrange_length
;
490 struct vmbus_channel_view_range_remove
{
491 struct vmbus_channel_message_header header
;
492 PHYSICAL_ADDRESS viewrange_base
;
497 struct vmbus_channel_relid_released
{
498 struct vmbus_channel_message_header header
;
502 struct vmbus_channel_initiate_contact
{
503 struct vmbus_channel_message_header header
;
504 u32 vmbus_version_requested
;
511 struct vmbus_channel_version_response
{
512 struct vmbus_channel_message_header header
;
513 bool version_supported
;
516 enum vmbus_channel_state
{
518 CHANNEL_OPENING_STATE
,
522 struct vmbus_channel_debug_info
{
524 enum vmbus_channel_state state
;
525 uuid_le interfacetype
;
526 uuid_le interface_instance
;
528 u32 servermonitor_pending
;
529 u32 servermonitor_latency
;
530 u32 servermonitor_connectionid
;
531 u32 clientmonitor_pending
;
532 u32 clientmonitor_latency
;
533 u32 clientmonitor_connectionid
;
535 struct hv_ring_buffer_debug_info inbound
;
536 struct hv_ring_buffer_debug_info outbound
;
540 * Represents each channel msg on the vmbus connection This is a
541 * variable-size data structure depending on the msg type itself
543 struct vmbus_channel_msginfo
{
544 /* Bookkeeping stuff */
545 struct list_head msglistentry
;
547 /* So far, this is only used to handle gpadl body message */
548 struct list_head submsglist
;
550 /* Synchronize the request/response if needed */
551 struct completion waitevent
;
553 struct vmbus_channel_version_supported version_supported
;
554 struct vmbus_channel_open_result open_result
;
555 struct vmbus_channel_gpadl_torndown gpadl_torndown
;
556 struct vmbus_channel_gpadl_created gpadl_created
;
557 struct vmbus_channel_version_response version_response
;
562 * The channel message that goes out on the "wire".
563 * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
565 unsigned char msg
[0];
568 struct vmbus_close_msg
{
569 struct vmbus_channel_msginfo info
;
570 struct vmbus_channel_close_channel msg
;
573 struct vmbus_channel
{
574 struct list_head listentry
;
576 struct hv_device
*device_obj
;
578 struct work_struct work
;
580 enum vmbus_channel_state state
;
582 struct vmbus_channel_offer_channel offermsg
;
584 * These are based on the OfferMsg.MonitorId.
585 * Save it here for easy access.
590 u32 ringbuffer_gpadlhandle
;
592 /* Allocated memory for ring buffer */
593 void *ringbuffer_pages
;
594 u32 ringbuffer_pagecount
;
595 struct hv_ring_buffer_info outbound
; /* send to parent */
596 struct hv_ring_buffer_info inbound
; /* receive from parent */
597 spinlock_t inbound_lock
;
598 struct workqueue_struct
*controlwq
;
600 struct vmbus_close_msg close_msg
;
602 /* Channel callback are invoked in this workqueue context */
603 /* HANDLE dataWorkQueue; */
605 void (*onchannel_callback
)(void *context
);
606 void *channel_callback_context
;
609 void vmbus_onmessage(void *context
);
611 int vmbus_request_offers(void);
613 /* The format must be the same as struct vmdata_gpa_direct */
614 struct vmbus_channel_packet_page_buffer
{
622 struct hv_page_buffer range
[MAX_PAGE_BUFFER_COUNT
];
625 /* The format must be the same as struct vmdata_gpa_direct */
626 struct vmbus_channel_packet_multipage_buffer
{
633 u32 rangecount
; /* Always 1 in this case */
634 struct hv_multipage_buffer range
;
638 extern int vmbus_open(struct vmbus_channel
*channel
,
639 u32 send_ringbuffersize
,
640 u32 recv_ringbuffersize
,
643 void(*onchannel_callback
)(void *context
),
646 extern void vmbus_close(struct vmbus_channel
*channel
);
648 extern int vmbus_sendpacket(struct vmbus_channel
*channel
,
652 enum vmbus_packet_type type
,
655 extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
656 struct hv_page_buffer pagebuffers
[],
662 extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
663 struct hv_multipage_buffer
*mpb
,
668 extern int vmbus_establish_gpadl(struct vmbus_channel
*channel
,
673 extern int vmbus_teardown_gpadl(struct vmbus_channel
*channel
,
676 extern int vmbus_recvpacket(struct vmbus_channel
*channel
,
679 u32
*buffer_actual_len
,
682 extern int vmbus_recvpacket_raw(struct vmbus_channel
*channel
,
685 u32
*buffer_actual_len
,
689 extern void vmbus_get_debug_info(struct vmbus_channel
*channel
,
690 struct vmbus_channel_debug_info
*debug
);
692 extern void vmbus_ontimer(unsigned long data
);
694 struct hv_dev_port_info
{
698 u32 bytes_avail_toread
;
699 u32 bytes_avail_towrite
;
702 /* Base driver object */
706 /* the device type supported by this driver */
708 const struct hv_vmbus_device_id
*id_table
;
710 struct device_driver driver
;
712 int (*probe
)(struct hv_device
*, const struct hv_vmbus_device_id
*);
713 int (*remove
)(struct hv_device
*);
714 void (*shutdown
)(struct hv_device
*);
718 /* Base device object */
720 /* the device type id of this device */
723 /* the device instance id of this device */
724 uuid_le dev_instance
;
726 struct device device
;
728 struct vmbus_channel
*channel
;
732 static inline struct hv_device
*device_to_hv_device(struct device
*d
)
734 return container_of(d
, struct hv_device
, device
);
737 static inline struct hv_driver
*drv_to_hv_drv(struct device_driver
*d
)
739 return container_of(d
, struct hv_driver
, driver
);
742 static inline void hv_set_drvdata(struct hv_device
*dev
, void *data
)
744 dev_set_drvdata(&dev
->device
, data
);
747 static inline void *hv_get_drvdata(struct hv_device
*dev
)
749 return dev_get_drvdata(&dev
->device
);
752 /* Vmbus interface */
753 #define vmbus_driver_register(driver) \
754 __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
755 int __must_check
__vmbus_driver_register(struct hv_driver
*hv_driver
,
756 struct module
*owner
,
757 const char *mod_name
);
758 void vmbus_driver_unregister(struct hv_driver
*hv_driver
);
761 * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
763 * This macro is used to create a struct hv_vmbus_device_id that matches a
766 #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
767 g8, g9, ga, gb, gc, gd, ge, gf) \
768 .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
769 g8, g9, ga, gb, gc, gd, ge, gf },
772 * Common header for Hyper-V ICs
775 #define ICMSGTYPE_NEGOTIATE 0
776 #define ICMSGTYPE_HEARTBEAT 1
777 #define ICMSGTYPE_KVPEXCHANGE 2
778 #define ICMSGTYPE_SHUTDOWN 3
779 #define ICMSGTYPE_TIMESYNC 4
780 #define ICMSGTYPE_VSS 5
782 #define ICMSGHDRFLAG_TRANSACTION 1
783 #define ICMSGHDRFLAG_REQUEST 2
784 #define ICMSGHDRFLAG_RESPONSE 4
786 #define HV_S_OK 0x00000000
787 #define HV_E_FAIL 0x80004005
788 #define HV_ERROR_NOT_SUPPORTED 0x80070032
789 #define HV_ERROR_MACHINE_LOCKED 0x800704F7
792 * While we want to handle util services as regular devices,
793 * there is only one instance of each of these services; so
794 * we statically allocate the service specific state.
797 struct hv_util_service
{
799 void (*util_cb
)(void *);
800 int (*util_init
)(struct hv_util_service
*);
801 void (*util_deinit
)(void);
804 struct vmbuspipe_hdr
{
815 struct ic_version icverframe
;
817 struct ic_version icvermsg
;
825 struct icmsg_negotiate
{
829 struct ic_version icversion_data
[1]; /* any size array */
832 struct shutdown_msg_data
{
836 u8 display_message
[2048];
839 struct heartbeat_msg_data
{
844 /* Time Sync IC defs */
845 #define ICTIMESYNCFLAG_PROBE 0
846 #define ICTIMESYNCFLAG_SYNC 1
847 #define ICTIMESYNCFLAG_SAMPLE 2
850 #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
852 #define WLTIMEDELTA 116444736000000000LL
855 struct ictimesync_data
{
862 struct hyperv_service_callback
{
866 struct vmbus_channel
*channel
;
867 void (*callback
) (void *context
);
870 extern void vmbus_prep_negotiate_resp(struct icmsg_hdr
*,
871 struct icmsg_negotiate
*, u8
*);
873 #endif /* _HYPERV_H */