Drivers: hv: Add new message types to enhance KVP.
[linux-flexiantxendom0-3.2.10.git] / include / linux / hyperv.h
1 /*
2  *
3  * Copyright (c) 2011, Microsoft Corporation.
4  *
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.
8  *
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
12  * more details.
13  *
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.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *   K. Y. Srinivasan <kys@microsoft.com>
22  *
23  */
24
25 #ifndef _HYPERV_H
26 #define _HYPERV_H
27
28 #include <linux/types.h>
29
30 /*
31  * An implementation of HyperV key value pair (KVP) functionality for Linux.
32  *
33  *
34  * Copyright (C) 2010, Novell, Inc.
35  * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
36  *
37  */
38
39 /*
40  * Maximum value size - used for both key names and value data, and includes
41  * any applicable NULL terminators.
42  *
43  * Note:  This limit is somewhat arbitrary, but falls easily within what is
44  * supported for all native guests (back to Win 2000) and what is reasonable
45  * for the IC KVP exchange functionality.  Note that Windows Me/98/95 are
46  * limited to 255 character key names.
47  *
48  * MSDN recommends not storing data values larger than 2048 bytes in the
49  * registry.
50  *
51  * Note:  This value is used in defining the KVP exchange message - this value
52  * cannot be modified without affecting the message size and compatibility.
53  */
54
55 /*
56  * bytes, including any null terminators
57  */
58 #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE          (2048)
59
60
61 /*
62  * Maximum key size - the registry limit for the length of an entry name
63  * is 256 characters, including the null terminator
64  */
65
66 #define HV_KVP_EXCHANGE_MAX_KEY_SIZE            (512)
67
68 /*
69  * In Linux, we implement the KVP functionality in two components:
70  * 1) The kernel component which is packaged as part of the hv_utils driver
71  * is responsible for communicating with the host and responsible for
72  * implementing the host/guest protocol. 2) A user level daemon that is
73  * responsible for data gathering.
74  *
75  * Host/Guest Protocol: The host iterates over an index and expects the guest
76  * to assign a key name to the index and also return the value corresponding to
77  * the key. The host will have atmost one KVP transaction outstanding at any
78  * given point in time. The host side iteration stops when the guest returns
79  * an error. Microsoft has specified the following mapping of key names to
80  * host specified index:
81  *
82  *      Index           Key Name
83  *      0               FullyQualifiedDomainName
84  *      1               IntegrationServicesVersion
85  *      2               NetworkAddressIPv4
86  *      3               NetworkAddressIPv6
87  *      4               OSBuildNumber
88  *      5               OSName
89  *      6               OSMajorVersion
90  *      7               OSMinorVersion
91  *      8               OSVersion
92  *      9               ProcessorArchitecture
93  *
94  * The Windows host expects the Key Name and Key Value to be encoded in utf16.
95  *
96  * Guest Kernel/KVP Daemon Protocol: As noted earlier, we implement all of the
97  * data gathering functionality in a user mode daemon. The user level daemon
98  * is also responsible for binding the key name to the index as well. The
99  * kernel and user-level daemon communicate using a connector channel.
100  *
101  * The user mode component first registers with the
102  * the kernel component. Subsequently, the kernel component requests, data
103  * for the specified keys. In response to this message the user mode component
104  * fills in the value corresponding to the specified key. We overload the
105  * sequence field in the cn_msg header to define our KVP message types.
106  *
107  *
108  * The kernel component simply acts as a conduit for communication between the
109  * Windows host and the user-level daemon. The kernel component passes up the
110  * index received from the Host to the user-level daemon. If the index is
111  * valid (supported), the corresponding key as well as its
112  * value (both are strings) is returned. If the index is invalid
113  * (not supported), a NULL key string is returned.
114  */
115
116
117 /*
118  * Registry value types.
119  */
120
121 #define REG_SZ 1
122
123 enum hv_kvp_exchg_op {
124         KVP_OP_GET = 0,
125         KVP_OP_SET,
126         KVP_OP_DELETE,
127         KVP_OP_ENUMERATE,
128         KVP_OP_REGISTER,
129         KVP_OP_COUNT /* Number of operations, must be last. */
130 };
131
132 enum hv_kvp_exchg_pool {
133         KVP_POOL_EXTERNAL = 0,
134         KVP_POOL_GUEST,
135         KVP_POOL_AUTO,
136         KVP_POOL_AUTO_EXTERNAL,
137         KVP_POOL_AUTO_INTERNAL,
138         KVP_POOL_COUNT /* Number of pools, must be last. */
139 };
140
141 struct hv_kvp_hdr {
142         __u8 operation;
143         __u8 pool;
144         __u16 pad;
145 } __attribute__((packed));
146
147 struct hv_kvp_exchg_msg_value {
148         __u32 value_type;
149         __u32 key_size;
150         __u32 value_size;
151         __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
152         union {
153                 __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
154                 __u32 value_u32;
155                 __u64 value_u64;
156         };
157 } __attribute__((packed));
158
159 struct hv_kvp_msg_enumerate {
160         __u32 index;
161         struct hv_kvp_exchg_msg_value data;
162 } __attribute__((packed));
163
164 struct hv_kvp_msg_get {
165         struct hv_kvp_exchg_msg_value data;
166 };
167
168 struct hv_kvp_msg_set {
169         struct hv_kvp_exchg_msg_value data;
170 };
171
172 struct hv_kvp_msg_delete {
173         __u32 key_size;
174         __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
175 };
176
177 struct hv_kvp_register {
178         __u8 version[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
179 };
180
181 struct hv_kvp_msg {
182         struct hv_kvp_hdr       kvp_hdr;
183         union {
184                 struct hv_kvp_msg_get           kvp_get;
185                 struct hv_kvp_msg_set           kvp_set;
186                 struct hv_kvp_msg_delete        kvp_delete;
187                 struct hv_kvp_msg_enumerate     kvp_enum_data;
188                 struct hv_kvp_register          kvp_register;
189         } body;
190 } __attribute__((packed));
191
192 #ifdef __KERNEL__
193 #include <linux/scatterlist.h>
194 #include <linux/list.h>
195 #include <linux/uuid.h>
196 #include <linux/timer.h>
197 #include <linux/workqueue.h>
198 #include <linux/completion.h>
199 #include <linux/device.h>
200 #include <linux/mod_devicetable.h>
201
202
203 #define MAX_PAGE_BUFFER_COUNT                           19
204 #define MAX_MULTIPAGE_BUFFER_COUNT                      32 /* 128K */
205
206 #pragma pack(push, 1)
207
208 /* Single-page buffer */
209 struct hv_page_buffer {
210         u32 len;
211         u32 offset;
212         u64 pfn;
213 };
214
215 /* Multiple-page buffer */
216 struct hv_multipage_buffer {
217         /* Length and Offset determines the # of pfns in the array */
218         u32 len;
219         u32 offset;
220         u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
221 };
222
223 /* 0x18 includes the proprietary packet header */
224 #define MAX_PAGE_BUFFER_PACKET          (0x18 +                 \
225                                         (sizeof(struct hv_page_buffer) * \
226                                          MAX_PAGE_BUFFER_COUNT))
227 #define MAX_MULTIPAGE_BUFFER_PACKET     (0x18 +                 \
228                                          sizeof(struct hv_multipage_buffer))
229
230
231 #pragma pack(pop)
232
233 struct hv_ring_buffer {
234         /* Offset in bytes from the start of ring data below */
235         u32 write_index;
236
237         /* Offset in bytes from the start of ring data below */
238         u32 read_index;
239
240         u32 interrupt_mask;
241
242         /* Pad it to PAGE_SIZE so that data starts on page boundary */
243         u8      reserved[4084];
244
245         /* NOTE:
246          * The interrupt_mask field is used only for channels but since our
247          * vmbus connection also uses this data structure and its data starts
248          * here, we commented out this field.
249          */
250
251         /*
252          * Ring data starts here + RingDataStartOffset
253          * !!! DO NOT place any fields below this !!!
254          */
255         u8 buffer[0];
256 } __packed;
257
258 struct hv_ring_buffer_info {
259         struct hv_ring_buffer *ring_buffer;
260         u32 ring_size;                  /* Include the shared header */
261         spinlock_t ring_lock;
262
263         u32 ring_datasize;              /* < ring_size */
264         u32 ring_data_startoffset;
265 };
266
267 struct hv_ring_buffer_debug_info {
268         u32 current_interrupt_mask;
269         u32 current_read_index;
270         u32 current_write_index;
271         u32 bytes_avail_toread;
272         u32 bytes_avail_towrite;
273 };
274
275 /*
276  * We use the same version numbering for all Hyper-V modules.
277  *
278  * Definition of versioning is as follows;
279  *
280  *      Major Number    Changes for these scenarios;
281  *                      1.      When a new version of Windows Hyper-V
282  *                              is released.
283  *                      2.      A Major change has occurred in the
284  *                              Linux IC's.
285  *                      (For example the merge for the first time
286  *                      into the kernel) Every time the Major Number
287  *                      changes, the Revision number is reset to 0.
288  *      Minor Number    Changes when new functionality is added
289  *                      to the Linux IC's that is not a bug fix.
290  *
291  * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
292  */
293 #define HV_DRV_VERSION           "3.1"
294
295
296 /*
297  * A revision number of vmbus that is used for ensuring both ends on a
298  * partition are using compatible versions.
299  */
300 #define VMBUS_REVISION_NUMBER           13
301
302 /* Make maximum size of pipe payload of 16K */
303 #define MAX_PIPE_DATA_PAYLOAD           (sizeof(u8) * 16384)
304
305 /* Define PipeMode values. */
306 #define VMBUS_PIPE_TYPE_BYTE            0x00000000
307 #define VMBUS_PIPE_TYPE_MESSAGE         0x00000004
308
309 /* The size of the user defined data buffer for non-pipe offers. */
310 #define MAX_USER_DEFINED_BYTES          120
311
312 /* The size of the user defined data buffer for pipe offers. */
313 #define MAX_PIPE_USER_DEFINED_BYTES     116
314
315 /*
316  * At the center of the Channel Management library is the Channel Offer. This
317  * struct contains the fundamental information about an offer.
318  */
319 struct vmbus_channel_offer {
320         uuid_le if_type;
321         uuid_le if_instance;
322         u64 int_latency; /* in 100ns units */
323         u32 if_revision;
324         u32 server_ctx_size;    /* in bytes */
325         u16 chn_flags;
326         u16 mmio_megabytes;             /* in bytes * 1024 * 1024 */
327
328         union {
329                 /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
330                 struct {
331                         unsigned char user_def[MAX_USER_DEFINED_BYTES];
332                 } std;
333
334                 /*
335                  * Pipes:
336                  * The following sructure is an integrated pipe protocol, which
337                  * is implemented on top of standard user-defined data. Pipe
338                  * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
339                  * use.
340                  */
341                 struct {
342                         u32  pipe_mode;
343                         unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
344                 } pipe;
345         } u;
346         u32 padding;
347 } __packed;
348
349 /* Server Flags */
350 #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE        1
351 #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES    2
352 #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS            4
353 #define VMBUS_CHANNEL_NAMED_PIPE_MODE                   0x10
354 #define VMBUS_CHANNEL_LOOPBACK_OFFER                    0x100
355 #define VMBUS_CHANNEL_PARENT_OFFER                      0x200
356 #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION    0x400
357
358 struct vmpacket_descriptor {
359         u16 type;
360         u16 offset8;
361         u16 len8;
362         u16 flags;
363         u64 trans_id;
364 } __packed;
365
366 struct vmpacket_header {
367         u32 prev_pkt_start_offset;
368         struct vmpacket_descriptor descriptor;
369 } __packed;
370
371 struct vmtransfer_page_range {
372         u32 byte_count;
373         u32 byte_offset;
374 } __packed;
375
376 struct vmtransfer_page_packet_header {
377         struct vmpacket_descriptor d;
378         u16 xfer_pageset_id;
379         bool sender_owns_set;
380         u8 reserved;
381         u32 range_cnt;
382         struct vmtransfer_page_range ranges[1];
383 } __packed;
384
385 struct vmgpadl_packet_header {
386         struct vmpacket_descriptor d;
387         u32 gpadl;
388         u32 reserved;
389 } __packed;
390
391 struct vmadd_remove_transfer_page_set {
392         struct vmpacket_descriptor d;
393         u32 gpadl;
394         u16 xfer_pageset_id;
395         u16 reserved;
396 } __packed;
397
398 /*
399  * This structure defines a range in guest physical space that can be made to
400  * look virtually contiguous.
401  */
402 struct gpa_range {
403         u32 byte_count;
404         u32 byte_offset;
405         u64 pfn_array[0];
406 };
407
408 /*
409  * This is the format for an Establish Gpadl packet, which contains a handle by
410  * which this GPADL will be known and a set of GPA ranges associated with it.
411  * This can be converted to a MDL by the guest OS.  If there are multiple GPA
412  * ranges, then the resulting MDL will be "chained," representing multiple VA
413  * ranges.
414  */
415 struct vmestablish_gpadl {
416         struct vmpacket_descriptor d;
417         u32 gpadl;
418         u32 range_cnt;
419         struct gpa_range range[1];
420 } __packed;
421
422 /*
423  * This is the format for a Teardown Gpadl packet, which indicates that the
424  * GPADL handle in the Establish Gpadl packet will never be referenced again.
425  */
426 struct vmteardown_gpadl {
427         struct vmpacket_descriptor d;
428         u32 gpadl;
429         u32 reserved;   /* for alignment to a 8-byte boundary */
430 } __packed;
431
432 /*
433  * This is the format for a GPA-Direct packet, which contains a set of GPA
434  * ranges, in addition to commands and/or data.
435  */
436 struct vmdata_gpa_direct {
437         struct vmpacket_descriptor d;
438         u32 reserved;
439         u32 range_cnt;
440         struct gpa_range range[1];
441 } __packed;
442
443 /* This is the format for a Additional Data Packet. */
444 struct vmadditional_data {
445         struct vmpacket_descriptor d;
446         u64 total_bytes;
447         u32 offset;
448         u32 byte_cnt;
449         unsigned char data[1];
450 } __packed;
451
452 union vmpacket_largest_possible_header {
453         struct vmpacket_descriptor simple_hdr;
454         struct vmtransfer_page_packet_header xfer_page_hdr;
455         struct vmgpadl_packet_header gpadl_hdr;
456         struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
457         struct vmestablish_gpadl establish_gpadl_hdr;
458         struct vmteardown_gpadl teardown_gpadl_hdr;
459         struct vmdata_gpa_direct data_gpa_direct_hdr;
460 };
461
462 #define VMPACKET_DATA_START_ADDRESS(__packet)   \
463         (void *)(((unsigned char *)__packet) +  \
464          ((struct vmpacket_descriptor)__packet)->offset8 * 8)
465
466 #define VMPACKET_DATA_LENGTH(__packet)          \
467         ((((struct vmpacket_descriptor)__packet)->len8 -        \
468           ((struct vmpacket_descriptor)__packet)->offset8) * 8)
469
470 #define VMPACKET_TRANSFER_MODE(__packet)        \
471         (((struct IMPACT)__packet)->type)
472
473 enum vmbus_packet_type {
474         VM_PKT_INVALID                          = 0x0,
475         VM_PKT_SYNCH                            = 0x1,
476         VM_PKT_ADD_XFER_PAGESET                 = 0x2,
477         VM_PKT_RM_XFER_PAGESET                  = 0x3,
478         VM_PKT_ESTABLISH_GPADL                  = 0x4,
479         VM_PKT_TEARDOWN_GPADL                   = 0x5,
480         VM_PKT_DATA_INBAND                      = 0x6,
481         VM_PKT_DATA_USING_XFER_PAGES            = 0x7,
482         VM_PKT_DATA_USING_GPADL                 = 0x8,
483         VM_PKT_DATA_USING_GPA_DIRECT            = 0x9,
484         VM_PKT_CANCEL_REQUEST                   = 0xa,
485         VM_PKT_COMP                             = 0xb,
486         VM_PKT_DATA_USING_ADDITIONAL_PKT        = 0xc,
487         VM_PKT_ADDITIONAL_DATA                  = 0xd
488 };
489
490 #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED     1
491
492
493 /* Version 1 messages */
494 enum vmbus_channel_message_type {
495         CHANNELMSG_INVALID                      =  0,
496         CHANNELMSG_OFFERCHANNEL         =  1,
497         CHANNELMSG_RESCIND_CHANNELOFFER =  2,
498         CHANNELMSG_REQUESTOFFERS                =  3,
499         CHANNELMSG_ALLOFFERS_DELIVERED  =  4,
500         CHANNELMSG_OPENCHANNEL          =  5,
501         CHANNELMSG_OPENCHANNEL_RESULT           =  6,
502         CHANNELMSG_CLOSECHANNEL         =  7,
503         CHANNELMSG_GPADL_HEADER         =  8,
504         CHANNELMSG_GPADL_BODY                   =  9,
505         CHANNELMSG_GPADL_CREATED                = 10,
506         CHANNELMSG_GPADL_TEARDOWN               = 11,
507         CHANNELMSG_GPADL_TORNDOWN               = 12,
508         CHANNELMSG_RELID_RELEASED               = 13,
509         CHANNELMSG_INITIATE_CONTACT             = 14,
510         CHANNELMSG_VERSION_RESPONSE             = 15,
511         CHANNELMSG_UNLOAD                       = 16,
512 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
513         CHANNELMSG_VIEWRANGE_ADD                = 17,
514         CHANNELMSG_VIEWRANGE_REMOVE             = 18,
515 #endif
516         CHANNELMSG_COUNT
517 };
518
519 struct vmbus_channel_message_header {
520         enum vmbus_channel_message_type msgtype;
521         u32 padding;
522 } __packed;
523
524 /* Query VMBus Version parameters */
525 struct vmbus_channel_query_vmbus_version {
526         struct vmbus_channel_message_header header;
527         u32 version;
528 } __packed;
529
530 /* VMBus Version Supported parameters */
531 struct vmbus_channel_version_supported {
532         struct vmbus_channel_message_header header;
533         bool version_supported;
534 } __packed;
535
536 /* Offer Channel parameters */
537 struct vmbus_channel_offer_channel {
538         struct vmbus_channel_message_header header;
539         struct vmbus_channel_offer offer;
540         u32 child_relid;
541         u8 monitorid;
542         bool monitor_allocated;
543 } __packed;
544
545 /* Rescind Offer parameters */
546 struct vmbus_channel_rescind_offer {
547         struct vmbus_channel_message_header header;
548         u32 child_relid;
549 } __packed;
550
551 /*
552  * Request Offer -- no parameters, SynIC message contains the partition ID
553  * Set Snoop -- no parameters, SynIC message contains the partition ID
554  * Clear Snoop -- no parameters, SynIC message contains the partition ID
555  * All Offers Delivered -- no parameters, SynIC message contains the partition
556  *                         ID
557  * Flush Client -- no parameters, SynIC message contains the partition ID
558  */
559
560 /* Open Channel parameters */
561 struct vmbus_channel_open_channel {
562         struct vmbus_channel_message_header header;
563
564         /* Identifies the specific VMBus channel that is being opened. */
565         u32 child_relid;
566
567         /* ID making a particular open request at a channel offer unique. */
568         u32 openid;
569
570         /* GPADL for the channel's ring buffer. */
571         u32 ringbuffer_gpadlhandle;
572
573         /* GPADL for the channel's server context save area. */
574         u32 server_contextarea_gpadlhandle;
575
576         /*
577         * The upstream ring buffer begins at offset zero in the memory
578         * described by RingBufferGpadlHandle. The downstream ring buffer
579         * follows it at this offset (in pages).
580         */
581         u32 downstream_ringbuffer_pageoffset;
582
583         /* User-specific data to be passed along to the server endpoint. */
584         unsigned char userdata[MAX_USER_DEFINED_BYTES];
585 } __packed;
586
587 /* Open Channel Result parameters */
588 struct vmbus_channel_open_result {
589         struct vmbus_channel_message_header header;
590         u32 child_relid;
591         u32 openid;
592         u32 status;
593 } __packed;
594
595 /* Close channel parameters; */
596 struct vmbus_channel_close_channel {
597         struct vmbus_channel_message_header header;
598         u32 child_relid;
599 } __packed;
600
601 /* Channel Message GPADL */
602 #define GPADL_TYPE_RING_BUFFER          1
603 #define GPADL_TYPE_SERVER_SAVE_AREA     2
604 #define GPADL_TYPE_TRANSACTION          8
605
606 /*
607  * The number of PFNs in a GPADL message is defined by the number of
608  * pages that would be spanned by ByteCount and ByteOffset.  If the
609  * implied number of PFNs won't fit in this packet, there will be a
610  * follow-up packet that contains more.
611  */
612 struct vmbus_channel_gpadl_header {
613         struct vmbus_channel_message_header header;
614         u32 child_relid;
615         u32 gpadl;
616         u16 range_buflen;
617         u16 rangecount;
618         struct gpa_range range[0];
619 } __packed;
620
621 /* This is the followup packet that contains more PFNs. */
622 struct vmbus_channel_gpadl_body {
623         struct vmbus_channel_message_header header;
624         u32 msgnumber;
625         u32 gpadl;
626         u64 pfn[0];
627 } __packed;
628
629 struct vmbus_channel_gpadl_created {
630         struct vmbus_channel_message_header header;
631         u32 child_relid;
632         u32 gpadl;
633         u32 creation_status;
634 } __packed;
635
636 struct vmbus_channel_gpadl_teardown {
637         struct vmbus_channel_message_header header;
638         u32 child_relid;
639         u32 gpadl;
640 } __packed;
641
642 struct vmbus_channel_gpadl_torndown {
643         struct vmbus_channel_message_header header;
644         u32 gpadl;
645 } __packed;
646
647 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
648 struct vmbus_channel_view_range_add {
649         struct vmbus_channel_message_header header;
650         PHYSICAL_ADDRESS viewrange_base;
651         u64 viewrange_length;
652         u32 child_relid;
653 } __packed;
654
655 struct vmbus_channel_view_range_remove {
656         struct vmbus_channel_message_header header;
657         PHYSICAL_ADDRESS viewrange_base;
658         u32 child_relid;
659 } __packed;
660 #endif
661
662 struct vmbus_channel_relid_released {
663         struct vmbus_channel_message_header header;
664         u32 child_relid;
665 } __packed;
666
667 struct vmbus_channel_initiate_contact {
668         struct vmbus_channel_message_header header;
669         u32 vmbus_version_requested;
670         u32 padding2;
671         u64 interrupt_page;
672         u64 monitor_page1;
673         u64 monitor_page2;
674 } __packed;
675
676 struct vmbus_channel_version_response {
677         struct vmbus_channel_message_header header;
678         bool version_supported;
679 } __packed;
680
681 enum vmbus_channel_state {
682         CHANNEL_OFFER_STATE,
683         CHANNEL_OPENING_STATE,
684         CHANNEL_OPEN_STATE,
685 };
686
687 struct vmbus_channel_debug_info {
688         u32 relid;
689         enum vmbus_channel_state state;
690         uuid_le interfacetype;
691         uuid_le interface_instance;
692         u32 monitorid;
693         u32 servermonitor_pending;
694         u32 servermonitor_latency;
695         u32 servermonitor_connectionid;
696         u32 clientmonitor_pending;
697         u32 clientmonitor_latency;
698         u32 clientmonitor_connectionid;
699
700         struct hv_ring_buffer_debug_info inbound;
701         struct hv_ring_buffer_debug_info outbound;
702 };
703
704 /*
705  * Represents each channel msg on the vmbus connection This is a
706  * variable-size data structure depending on the msg type itself
707  */
708 struct vmbus_channel_msginfo {
709         /* Bookkeeping stuff */
710         struct list_head msglistentry;
711
712         /* So far, this is only used to handle gpadl body message */
713         struct list_head submsglist;
714
715         /* Synchronize the request/response if needed */
716         struct completion  waitevent;
717         union {
718                 struct vmbus_channel_version_supported version_supported;
719                 struct vmbus_channel_open_result open_result;
720                 struct vmbus_channel_gpadl_torndown gpadl_torndown;
721                 struct vmbus_channel_gpadl_created gpadl_created;
722                 struct vmbus_channel_version_response version_response;
723         } response;
724
725         u32 msgsize;
726         /*
727          * The channel message that goes out on the "wire".
728          * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
729          */
730         unsigned char msg[0];
731 };
732
733 struct vmbus_close_msg {
734         struct vmbus_channel_msginfo info;
735         struct vmbus_channel_close_channel msg;
736 };
737
738 struct vmbus_channel {
739         struct list_head listentry;
740
741         struct hv_device *device_obj;
742
743         struct work_struct work;
744
745         enum vmbus_channel_state state;
746
747         struct vmbus_channel_offer_channel offermsg;
748         /*
749          * These are based on the OfferMsg.MonitorId.
750          * Save it here for easy access.
751          */
752         u8 monitor_grp;
753         u8 monitor_bit;
754
755         u32 ringbuffer_gpadlhandle;
756
757         /* Allocated memory for ring buffer */
758         void *ringbuffer_pages;
759         u32 ringbuffer_pagecount;
760         struct hv_ring_buffer_info outbound;    /* send to parent */
761         struct hv_ring_buffer_info inbound;     /* receive from parent */
762         spinlock_t inbound_lock;
763         struct workqueue_struct *controlwq;
764
765         struct vmbus_close_msg close_msg;
766
767         /* Channel callback are invoked in this workqueue context */
768         /* HANDLE dataWorkQueue; */
769
770         void (*onchannel_callback)(void *context);
771         void *channel_callback_context;
772 };
773
774 void vmbus_onmessage(void *context);
775
776 int vmbus_request_offers(void);
777
778 /* The format must be the same as struct vmdata_gpa_direct */
779 struct vmbus_channel_packet_page_buffer {
780         u16 type;
781         u16 dataoffset8;
782         u16 length8;
783         u16 flags;
784         u64 transactionid;
785         u32 reserved;
786         u32 rangecount;
787         struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
788 } __packed;
789
790 /* The format must be the same as struct vmdata_gpa_direct */
791 struct vmbus_channel_packet_multipage_buffer {
792         u16 type;
793         u16 dataoffset8;
794         u16 length8;
795         u16 flags;
796         u64 transactionid;
797         u32 reserved;
798         u32 rangecount;         /* Always 1 in this case */
799         struct hv_multipage_buffer range;
800 } __packed;
801
802
803 extern int vmbus_open(struct vmbus_channel *channel,
804                             u32 send_ringbuffersize,
805                             u32 recv_ringbuffersize,
806                             void *userdata,
807                             u32 userdatalen,
808                             void(*onchannel_callback)(void *context),
809                             void *context);
810
811 extern void vmbus_close(struct vmbus_channel *channel);
812
813 extern int vmbus_sendpacket(struct vmbus_channel *channel,
814                                   const void *buffer,
815                                   u32 bufferLen,
816                                   u64 requestid,
817                                   enum vmbus_packet_type type,
818                                   u32 flags);
819
820 extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
821                                             struct hv_page_buffer pagebuffers[],
822                                             u32 pagecount,
823                                             void *buffer,
824                                             u32 bufferlen,
825                                             u64 requestid);
826
827 extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
828                                         struct hv_multipage_buffer *mpb,
829                                         void *buffer,
830                                         u32 bufferlen,
831                                         u64 requestid);
832
833 extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
834                                       void *kbuffer,
835                                       u32 size,
836                                       u32 *gpadl_handle);
837
838 extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
839                                      u32 gpadl_handle);
840
841 extern int vmbus_recvpacket(struct vmbus_channel *channel,
842                                   void *buffer,
843                                   u32 bufferlen,
844                                   u32 *buffer_actual_len,
845                                   u64 *requestid);
846
847 extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
848                                      void *buffer,
849                                      u32 bufferlen,
850                                      u32 *buffer_actual_len,
851                                      u64 *requestid);
852
853
854 extern void vmbus_get_debug_info(struct vmbus_channel *channel,
855                                      struct vmbus_channel_debug_info *debug);
856
857 extern void vmbus_ontimer(unsigned long data);
858
859 struct hv_dev_port_info {
860         u32 int_mask;
861         u32 read_idx;
862         u32 write_idx;
863         u32 bytes_avail_toread;
864         u32 bytes_avail_towrite;
865 };
866
867 /* Base driver object */
868 struct hv_driver {
869         const char *name;
870
871         /* the device type supported by this driver */
872         uuid_le dev_type;
873         const struct hv_vmbus_device_id *id_table;
874
875         struct device_driver driver;
876
877         int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
878         int (*remove)(struct hv_device *);
879         void (*shutdown)(struct hv_device *);
880
881 };
882
883 /* Base device object */
884 struct hv_device {
885         /* the device type id of this device */
886         uuid_le dev_type;
887
888         /* the device instance id of this device */
889         uuid_le dev_instance;
890
891         struct device device;
892
893         struct vmbus_channel *channel;
894 };
895
896
897 static inline struct hv_device *device_to_hv_device(struct device *d)
898 {
899         return container_of(d, struct hv_device, device);
900 }
901
902 static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
903 {
904         return container_of(d, struct hv_driver, driver);
905 }
906
907 static inline void hv_set_drvdata(struct hv_device *dev, void *data)
908 {
909         dev_set_drvdata(&dev->device, data);
910 }
911
912 static inline void *hv_get_drvdata(struct hv_device *dev)
913 {
914         return dev_get_drvdata(&dev->device);
915 }
916
917 /* Vmbus interface */
918 #define vmbus_driver_register(driver)   \
919         __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
920 int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
921                                          struct module *owner,
922                                          const char *mod_name);
923 void vmbus_driver_unregister(struct hv_driver *hv_driver);
924
925 /**
926  * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
927  *
928  * This macro is used to create a struct hv_vmbus_device_id that matches a
929  * specific device.
930  */
931 #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7,    \
932                      g8, g9, ga, gb, gc, gd, ge, gf)    \
933         .guid = { g0, g1, g2, g3, g4, g5, g6, g7,       \
934                   g8, g9, ga, gb, gc, gd, ge, gf },
935
936 /*
937  * Common header for Hyper-V ICs
938  */
939
940 #define ICMSGTYPE_NEGOTIATE             0
941 #define ICMSGTYPE_HEARTBEAT             1
942 #define ICMSGTYPE_KVPEXCHANGE           2
943 #define ICMSGTYPE_SHUTDOWN              3
944 #define ICMSGTYPE_TIMESYNC              4
945 #define ICMSGTYPE_VSS                   5
946
947 #define ICMSGHDRFLAG_TRANSACTION        1
948 #define ICMSGHDRFLAG_REQUEST            2
949 #define ICMSGHDRFLAG_RESPONSE           4
950
951 #define HV_S_OK                         0x00000000
952 #define HV_E_FAIL                       0x80004005
953 #define HV_ERROR_NOT_SUPPORTED          0x80070032
954 #define HV_ERROR_MACHINE_LOCKED         0x800704F7
955
956 /*
957  * While we want to handle util services as regular devices,
958  * there is only one instance of each of these services; so
959  * we statically allocate the service specific state.
960  */
961
962 struct hv_util_service {
963         u8 *recv_buffer;
964         void (*util_cb)(void *);
965         int (*util_init)(struct hv_util_service *);
966         void (*util_deinit)(void);
967 };
968
969 struct vmbuspipe_hdr {
970         u32 flags;
971         u32 msgsize;
972 } __packed;
973
974 struct ic_version {
975         u16 major;
976         u16 minor;
977 } __packed;
978
979 struct icmsg_hdr {
980         struct ic_version icverframe;
981         u16 icmsgtype;
982         struct ic_version icvermsg;
983         u16 icmsgsize;
984         u32 status;
985         u8 ictransaction_id;
986         u8 icflags;
987         u8 reserved[2];
988 } __packed;
989
990 struct icmsg_negotiate {
991         u16 icframe_vercnt;
992         u16 icmsg_vercnt;
993         u32 reserved;
994         struct ic_version icversion_data[1]; /* any size array */
995 } __packed;
996
997 struct shutdown_msg_data {
998         u32 reason_code;
999         u32 timeout_seconds;
1000         u32 flags;
1001         u8  display_message[2048];
1002 } __packed;
1003
1004 struct heartbeat_msg_data {
1005         u64 seq_num;
1006         u32 reserved[8];
1007 } __packed;
1008
1009 /* Time Sync IC defs */
1010 #define ICTIMESYNCFLAG_PROBE    0
1011 #define ICTIMESYNCFLAG_SYNC     1
1012 #define ICTIMESYNCFLAG_SAMPLE   2
1013
1014 #ifdef __x86_64__
1015 #define WLTIMEDELTA     116444736000000000L     /* in 100ns unit */
1016 #else
1017 #define WLTIMEDELTA     116444736000000000LL
1018 #endif
1019
1020 struct ictimesync_data {
1021         u64 parenttime;
1022         u64 childtime;
1023         u64 roundtriptime;
1024         u8 flags;
1025 } __packed;
1026
1027 struct hyperv_service_callback {
1028         u8 msg_type;
1029         char *log_msg;
1030         uuid_le data;
1031         struct vmbus_channel *channel;
1032         void (*callback) (void *context);
1033 };
1034
1035 extern void vmbus_prep_negotiate_resp(struct icmsg_hdr *,
1036                                       struct icmsg_negotiate *, u8 *);
1037
1038 int hv_kvp_init(struct hv_util_service *);
1039 void hv_kvp_deinit(void);
1040 void hv_kvp_onchannelcallback(void *);
1041
1042 #endif /* __KERNEL__ */
1043 #endif /* _HYPERV_H */