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