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