Drivers: hv: Update the ring buffer structure to match win8 functionality
[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         /*
329          * Win8 uses some of the reserved bits to implement
330          * interrupt driven flow management. On the send side
331          * we can request that the receiver interrupt the sender
332          * when the ring transitions from being full to being able
333          * to handle a message of size "pending_send_sz".
334          *
335          * Add necessary state for this enhancement.
336          */
337         u32 pending_send_sz;
338
339         u32 reserved1[12];
340
341         union {
342                 struct {
343                         u32 feat_pending_send_sz:1;
344                 };
345                 u32 value;
346         } feature_bits;
347
348         /* Pad it to PAGE_SIZE so that data starts on page boundary */
349         u8      reserved2[4028];
350
351         /*
352          * Ring data starts here + RingDataStartOffset
353          * !!! DO NOT place any fields below this !!!
354          */
355         u8 buffer[0];
356 } __packed;
357
358 struct hv_ring_buffer_info {
359         struct hv_ring_buffer *ring_buffer;
360         u32 ring_size;                  /* Include the shared header */
361         spinlock_t ring_lock;
362
363         u32 ring_datasize;              /* < ring_size */
364         u32 ring_data_startoffset;
365 };
366
367 struct hv_ring_buffer_debug_info {
368         u32 current_interrupt_mask;
369         u32 current_read_index;
370         u32 current_write_index;
371         u32 bytes_avail_toread;
372         u32 bytes_avail_towrite;
373 };
374
375
376 /*
377  *
378  * hv_get_ringbuffer_availbytes()
379  *
380  * Get number of bytes available to read and to write to
381  * for the specified ring buffer
382  */
383 static inline void
384 hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
385                           u32 *read, u32 *write)
386 {
387         u32 read_loc, write_loc, dsize;
388
389         smp_read_barrier_depends();
390
391         /* Capture the read/write indices before they changed */
392         read_loc = rbi->ring_buffer->read_index;
393         write_loc = rbi->ring_buffer->write_index;
394         dsize = rbi->ring_datasize;
395
396         *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
397                 read_loc - write_loc;
398         *read = dsize - *write;
399 }
400
401
402 /*
403  * We use the same version numbering for all Hyper-V modules.
404  *
405  * Definition of versioning is as follows;
406  *
407  *      Major Number    Changes for these scenarios;
408  *                      1.      When a new version of Windows Hyper-V
409  *                              is released.
410  *                      2.      A Major change has occurred in the
411  *                              Linux IC's.
412  *                      (For example the merge for the first time
413  *                      into the kernel) Every time the Major Number
414  *                      changes, the Revision number is reset to 0.
415  *      Minor Number    Changes when new functionality is added
416  *                      to the Linux IC's that is not a bug fix.
417  *
418  * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
419  */
420 #define HV_DRV_VERSION           "3.1"
421
422
423 /* Make maximum size of pipe payload of 16K */
424 #define MAX_PIPE_DATA_PAYLOAD           (sizeof(u8) * 16384)
425
426 /* Define PipeMode values. */
427 #define VMBUS_PIPE_TYPE_BYTE            0x00000000
428 #define VMBUS_PIPE_TYPE_MESSAGE         0x00000004
429
430 /* The size of the user defined data buffer for non-pipe offers. */
431 #define MAX_USER_DEFINED_BYTES          120
432
433 /* The size of the user defined data buffer for pipe offers. */
434 #define MAX_PIPE_USER_DEFINED_BYTES     116
435
436 /*
437  * At the center of the Channel Management library is the Channel Offer. This
438  * struct contains the fundamental information about an offer.
439  */
440 struct vmbus_channel_offer {
441         uuid_le if_type;
442         uuid_le if_instance;
443         u64 int_latency; /* in 100ns units */
444         u32 if_revision;
445         u32 server_ctx_size;    /* in bytes */
446         u16 chn_flags;
447         u16 mmio_megabytes;             /* in bytes * 1024 * 1024 */
448
449         union {
450                 /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
451                 struct {
452                         unsigned char user_def[MAX_USER_DEFINED_BYTES];
453                 } std;
454
455                 /*
456                  * Pipes:
457                  * The following sructure is an integrated pipe protocol, which
458                  * is implemented on top of standard user-defined data. Pipe
459                  * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
460                  * use.
461                  */
462                 struct {
463                         u32  pipe_mode;
464                         unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
465                 } pipe;
466         } u;
467         u32 padding;
468 } __packed;
469
470 /* Server Flags */
471 #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE        1
472 #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES    2
473 #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS            4
474 #define VMBUS_CHANNEL_NAMED_PIPE_MODE                   0x10
475 #define VMBUS_CHANNEL_LOOPBACK_OFFER                    0x100
476 #define VMBUS_CHANNEL_PARENT_OFFER                      0x200
477 #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION    0x400
478
479 struct vmpacket_descriptor {
480         u16 type;
481         u16 offset8;
482         u16 len8;
483         u16 flags;
484         u64 trans_id;
485 } __packed;
486
487 struct vmpacket_header {
488         u32 prev_pkt_start_offset;
489         struct vmpacket_descriptor descriptor;
490 } __packed;
491
492 struct vmtransfer_page_range {
493         u32 byte_count;
494         u32 byte_offset;
495 } __packed;
496
497 struct vmtransfer_page_packet_header {
498         struct vmpacket_descriptor d;
499         u16 xfer_pageset_id;
500         u8  sender_owns_set;
501         u8 reserved;
502         u32 range_cnt;
503         struct vmtransfer_page_range ranges[1];
504 } __packed;
505
506 struct vmgpadl_packet_header {
507         struct vmpacket_descriptor d;
508         u32 gpadl;
509         u32 reserved;
510 } __packed;
511
512 struct vmadd_remove_transfer_page_set {
513         struct vmpacket_descriptor d;
514         u32 gpadl;
515         u16 xfer_pageset_id;
516         u16 reserved;
517 } __packed;
518
519 /*
520  * This structure defines a range in guest physical space that can be made to
521  * look virtually contiguous.
522  */
523 struct gpa_range {
524         u32 byte_count;
525         u32 byte_offset;
526         u64 pfn_array[0];
527 };
528
529 /*
530  * This is the format for an Establish Gpadl packet, which contains a handle by
531  * which this GPADL will be known and a set of GPA ranges associated with it.
532  * This can be converted to a MDL by the guest OS.  If there are multiple GPA
533  * ranges, then the resulting MDL will be "chained," representing multiple VA
534  * ranges.
535  */
536 struct vmestablish_gpadl {
537         struct vmpacket_descriptor d;
538         u32 gpadl;
539         u32 range_cnt;
540         struct gpa_range range[1];
541 } __packed;
542
543 /*
544  * This is the format for a Teardown Gpadl packet, which indicates that the
545  * GPADL handle in the Establish Gpadl packet will never be referenced again.
546  */
547 struct vmteardown_gpadl {
548         struct vmpacket_descriptor d;
549         u32 gpadl;
550         u32 reserved;   /* for alignment to a 8-byte boundary */
551 } __packed;
552
553 /*
554  * This is the format for a GPA-Direct packet, which contains a set of GPA
555  * ranges, in addition to commands and/or data.
556  */
557 struct vmdata_gpa_direct {
558         struct vmpacket_descriptor d;
559         u32 reserved;
560         u32 range_cnt;
561         struct gpa_range range[1];
562 } __packed;
563
564 /* This is the format for a Additional Data Packet. */
565 struct vmadditional_data {
566         struct vmpacket_descriptor d;
567         u64 total_bytes;
568         u32 offset;
569         u32 byte_cnt;
570         unsigned char data[1];
571 } __packed;
572
573 union vmpacket_largest_possible_header {
574         struct vmpacket_descriptor simple_hdr;
575         struct vmtransfer_page_packet_header xfer_page_hdr;
576         struct vmgpadl_packet_header gpadl_hdr;
577         struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
578         struct vmestablish_gpadl establish_gpadl_hdr;
579         struct vmteardown_gpadl teardown_gpadl_hdr;
580         struct vmdata_gpa_direct data_gpa_direct_hdr;
581 };
582
583 #define VMPACKET_DATA_START_ADDRESS(__packet)   \
584         (void *)(((unsigned char *)__packet) +  \
585          ((struct vmpacket_descriptor)__packet)->offset8 * 8)
586
587 #define VMPACKET_DATA_LENGTH(__packet)          \
588         ((((struct vmpacket_descriptor)__packet)->len8 -        \
589           ((struct vmpacket_descriptor)__packet)->offset8) * 8)
590
591 #define VMPACKET_TRANSFER_MODE(__packet)        \
592         (((struct IMPACT)__packet)->type)
593
594 enum vmbus_packet_type {
595         VM_PKT_INVALID                          = 0x0,
596         VM_PKT_SYNCH                            = 0x1,
597         VM_PKT_ADD_XFER_PAGESET                 = 0x2,
598         VM_PKT_RM_XFER_PAGESET                  = 0x3,
599         VM_PKT_ESTABLISH_GPADL                  = 0x4,
600         VM_PKT_TEARDOWN_GPADL                   = 0x5,
601         VM_PKT_DATA_INBAND                      = 0x6,
602         VM_PKT_DATA_USING_XFER_PAGES            = 0x7,
603         VM_PKT_DATA_USING_GPADL                 = 0x8,
604         VM_PKT_DATA_USING_GPA_DIRECT            = 0x9,
605         VM_PKT_CANCEL_REQUEST                   = 0xa,
606         VM_PKT_COMP                             = 0xb,
607         VM_PKT_DATA_USING_ADDITIONAL_PKT        = 0xc,
608         VM_PKT_ADDITIONAL_DATA                  = 0xd
609 };
610
611 #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED     1
612
613
614 /* Version 1 messages */
615 enum vmbus_channel_message_type {
616         CHANNELMSG_INVALID                      =  0,
617         CHANNELMSG_OFFERCHANNEL         =  1,
618         CHANNELMSG_RESCIND_CHANNELOFFER =  2,
619         CHANNELMSG_REQUESTOFFERS                =  3,
620         CHANNELMSG_ALLOFFERS_DELIVERED  =  4,
621         CHANNELMSG_OPENCHANNEL          =  5,
622         CHANNELMSG_OPENCHANNEL_RESULT           =  6,
623         CHANNELMSG_CLOSECHANNEL         =  7,
624         CHANNELMSG_GPADL_HEADER         =  8,
625         CHANNELMSG_GPADL_BODY                   =  9,
626         CHANNELMSG_GPADL_CREATED                = 10,
627         CHANNELMSG_GPADL_TEARDOWN               = 11,
628         CHANNELMSG_GPADL_TORNDOWN               = 12,
629         CHANNELMSG_RELID_RELEASED               = 13,
630         CHANNELMSG_INITIATE_CONTACT             = 14,
631         CHANNELMSG_VERSION_RESPONSE             = 15,
632         CHANNELMSG_UNLOAD                       = 16,
633 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
634         CHANNELMSG_VIEWRANGE_ADD                = 17,
635         CHANNELMSG_VIEWRANGE_REMOVE             = 18,
636 #endif
637         CHANNELMSG_COUNT
638 };
639
640 struct vmbus_channel_message_header {
641         enum vmbus_channel_message_type msgtype;
642         u32 padding;
643 } __packed;
644
645 /* Query VMBus Version parameters */
646 struct vmbus_channel_query_vmbus_version {
647         struct vmbus_channel_message_header header;
648         u32 version;
649 } __packed;
650
651 /* VMBus Version Supported parameters */
652 struct vmbus_channel_version_supported {
653         struct vmbus_channel_message_header header;
654         u8 version_supported;
655 } __packed;
656
657 /* Offer Channel parameters */
658 struct vmbus_channel_offer_channel {
659         struct vmbus_channel_message_header header;
660         struct vmbus_channel_offer offer;
661         u32 child_relid;
662         u8 monitorid;
663         u8 monitor_allocated;
664 } __packed;
665
666 /* Rescind Offer parameters */
667 struct vmbus_channel_rescind_offer {
668         struct vmbus_channel_message_header header;
669         u32 child_relid;
670 } __packed;
671
672 /*
673  * Request Offer -- no parameters, SynIC message contains the partition ID
674  * Set Snoop -- no parameters, SynIC message contains the partition ID
675  * Clear Snoop -- no parameters, SynIC message contains the partition ID
676  * All Offers Delivered -- no parameters, SynIC message contains the partition
677  *                         ID
678  * Flush Client -- no parameters, SynIC message contains the partition ID
679  */
680
681 /* Open Channel parameters */
682 struct vmbus_channel_open_channel {
683         struct vmbus_channel_message_header header;
684
685         /* Identifies the specific VMBus channel that is being opened. */
686         u32 child_relid;
687
688         /* ID making a particular open request at a channel offer unique. */
689         u32 openid;
690
691         /* GPADL for the channel's ring buffer. */
692         u32 ringbuffer_gpadlhandle;
693
694         /* GPADL for the channel's server context save area. */
695         u32 server_contextarea_gpadlhandle;
696
697         /*
698         * The upstream ring buffer begins at offset zero in the memory
699         * described by RingBufferGpadlHandle. The downstream ring buffer
700         * follows it at this offset (in pages).
701         */
702         u32 downstream_ringbuffer_pageoffset;
703
704         /* User-specific data to be passed along to the server endpoint. */
705         unsigned char userdata[MAX_USER_DEFINED_BYTES];
706 } __packed;
707
708 /* Open Channel Result parameters */
709 struct vmbus_channel_open_result {
710         struct vmbus_channel_message_header header;
711         u32 child_relid;
712         u32 openid;
713         u32 status;
714 } __packed;
715
716 /* Close channel parameters; */
717 struct vmbus_channel_close_channel {
718         struct vmbus_channel_message_header header;
719         u32 child_relid;
720 } __packed;
721
722 /* Channel Message GPADL */
723 #define GPADL_TYPE_RING_BUFFER          1
724 #define GPADL_TYPE_SERVER_SAVE_AREA     2
725 #define GPADL_TYPE_TRANSACTION          8
726
727 /*
728  * The number of PFNs in a GPADL message is defined by the number of
729  * pages that would be spanned by ByteCount and ByteOffset.  If the
730  * implied number of PFNs won't fit in this packet, there will be a
731  * follow-up packet that contains more.
732  */
733 struct vmbus_channel_gpadl_header {
734         struct vmbus_channel_message_header header;
735         u32 child_relid;
736         u32 gpadl;
737         u16 range_buflen;
738         u16 rangecount;
739         struct gpa_range range[0];
740 } __packed;
741
742 /* This is the followup packet that contains more PFNs. */
743 struct vmbus_channel_gpadl_body {
744         struct vmbus_channel_message_header header;
745         u32 msgnumber;
746         u32 gpadl;
747         u64 pfn[0];
748 } __packed;
749
750 struct vmbus_channel_gpadl_created {
751         struct vmbus_channel_message_header header;
752         u32 child_relid;
753         u32 gpadl;
754         u32 creation_status;
755 } __packed;
756
757 struct vmbus_channel_gpadl_teardown {
758         struct vmbus_channel_message_header header;
759         u32 child_relid;
760         u32 gpadl;
761 } __packed;
762
763 struct vmbus_channel_gpadl_torndown {
764         struct vmbus_channel_message_header header;
765         u32 gpadl;
766 } __packed;
767
768 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
769 struct vmbus_channel_view_range_add {
770         struct vmbus_channel_message_header header;
771         PHYSICAL_ADDRESS viewrange_base;
772         u64 viewrange_length;
773         u32 child_relid;
774 } __packed;
775
776 struct vmbus_channel_view_range_remove {
777         struct vmbus_channel_message_header header;
778         PHYSICAL_ADDRESS viewrange_base;
779         u32 child_relid;
780 } __packed;
781 #endif
782
783 struct vmbus_channel_relid_released {
784         struct vmbus_channel_message_header header;
785         u32 child_relid;
786 } __packed;
787
788 struct vmbus_channel_initiate_contact {
789         struct vmbus_channel_message_header header;
790         u32 vmbus_version_requested;
791         u32 padding2;
792         u64 interrupt_page;
793         u64 monitor_page1;
794         u64 monitor_page2;
795 } __packed;
796
797 struct vmbus_channel_version_response {
798         struct vmbus_channel_message_header header;
799         u8 version_supported;
800 } __packed;
801
802 enum vmbus_channel_state {
803         CHANNEL_OFFER_STATE,
804         CHANNEL_OPENING_STATE,
805         CHANNEL_OPEN_STATE,
806 };
807
808 struct vmbus_channel_debug_info {
809         u32 relid;
810         enum vmbus_channel_state state;
811         uuid_le interfacetype;
812         uuid_le interface_instance;
813         u32 monitorid;
814         u32 servermonitor_pending;
815         u32 servermonitor_latency;
816         u32 servermonitor_connectionid;
817         u32 clientmonitor_pending;
818         u32 clientmonitor_latency;
819         u32 clientmonitor_connectionid;
820
821         struct hv_ring_buffer_debug_info inbound;
822         struct hv_ring_buffer_debug_info outbound;
823 };
824
825 /*
826  * Represents each channel msg on the vmbus connection This is a
827  * variable-size data structure depending on the msg type itself
828  */
829 struct vmbus_channel_msginfo {
830         /* Bookkeeping stuff */
831         struct list_head msglistentry;
832
833         /* So far, this is only used to handle gpadl body message */
834         struct list_head submsglist;
835
836         /* Synchronize the request/response if needed */
837         struct completion  waitevent;
838         union {
839                 struct vmbus_channel_version_supported version_supported;
840                 struct vmbus_channel_open_result open_result;
841                 struct vmbus_channel_gpadl_torndown gpadl_torndown;
842                 struct vmbus_channel_gpadl_created gpadl_created;
843                 struct vmbus_channel_version_response version_response;
844         } response;
845
846         u32 msgsize;
847         /*
848          * The channel message that goes out on the "wire".
849          * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
850          */
851         unsigned char msg[0];
852 };
853
854 struct vmbus_close_msg {
855         struct vmbus_channel_msginfo info;
856         struct vmbus_channel_close_channel msg;
857 };
858
859 struct vmbus_channel {
860         struct list_head listentry;
861
862         struct hv_device *device_obj;
863
864         struct work_struct work;
865
866         enum vmbus_channel_state state;
867
868         struct vmbus_channel_offer_channel offermsg;
869         /*
870          * These are based on the OfferMsg.MonitorId.
871          * Save it here for easy access.
872          */
873         u8 monitor_grp;
874         u8 monitor_bit;
875
876         u32 ringbuffer_gpadlhandle;
877
878         /* Allocated memory for ring buffer */
879         void *ringbuffer_pages;
880         u32 ringbuffer_pagecount;
881         struct hv_ring_buffer_info outbound;    /* send to parent */
882         struct hv_ring_buffer_info inbound;     /* receive from parent */
883         spinlock_t inbound_lock;
884         struct workqueue_struct *controlwq;
885
886         struct vmbus_close_msg close_msg;
887
888         /* Channel callback are invoked in this workqueue context */
889         /* HANDLE dataWorkQueue; */
890
891         void (*onchannel_callback)(void *context);
892         void *channel_callback_context;
893
894         /*
895          * A channel can be marked for efficient (batched)
896          * reading:
897          * If batched_reading is set to "true", we read until the
898          * channel is empty and hold off interrupts from the host
899          * during the entire read process.
900          * If batched_reading is set to "false", the client is not
901          * going to perform batched reading.
902          *
903          * By default we will enable batched reading; specific
904          * drivers that don't want this behavior can turn it off.
905          */
906
907         bool batched_reading;
908 };
909
910 static inline void set_channel_read_state(struct vmbus_channel *c, bool state)
911 {
912         c->batched_reading = state;
913 }
914
915 void vmbus_onmessage(void *context);
916
917 int vmbus_request_offers(void);
918
919 /* The format must be the same as struct vmdata_gpa_direct */
920 struct vmbus_channel_packet_page_buffer {
921         u16 type;
922         u16 dataoffset8;
923         u16 length8;
924         u16 flags;
925         u64 transactionid;
926         u32 reserved;
927         u32 rangecount;
928         struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
929 } __packed;
930
931 /* The format must be the same as struct vmdata_gpa_direct */
932 struct vmbus_channel_packet_multipage_buffer {
933         u16 type;
934         u16 dataoffset8;
935         u16 length8;
936         u16 flags;
937         u64 transactionid;
938         u32 reserved;
939         u32 rangecount;         /* Always 1 in this case */
940         struct hv_multipage_buffer range;
941 } __packed;
942
943
944 extern int vmbus_open(struct vmbus_channel *channel,
945                             u32 send_ringbuffersize,
946                             u32 recv_ringbuffersize,
947                             void *userdata,
948                             u32 userdatalen,
949                             void(*onchannel_callback)(void *context),
950                             void *context);
951
952 extern void vmbus_close(struct vmbus_channel *channel);
953
954 extern int vmbus_sendpacket(struct vmbus_channel *channel,
955                                   const void *buffer,
956                                   u32 bufferLen,
957                                   u64 requestid,
958                                   enum vmbus_packet_type type,
959                                   u32 flags);
960
961 extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
962                                             struct hv_page_buffer pagebuffers[],
963                                             u32 pagecount,
964                                             void *buffer,
965                                             u32 bufferlen,
966                                             u64 requestid);
967
968 extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
969                                         struct hv_multipage_buffer *mpb,
970                                         void *buffer,
971                                         u32 bufferlen,
972                                         u64 requestid);
973
974 extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
975                                       void *kbuffer,
976                                       u32 size,
977                                       u32 *gpadl_handle);
978
979 extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
980                                      u32 gpadl_handle);
981
982 extern int vmbus_recvpacket(struct vmbus_channel *channel,
983                                   void *buffer,
984                                   u32 bufferlen,
985                                   u32 *buffer_actual_len,
986                                   u64 *requestid);
987
988 extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
989                                      void *buffer,
990                                      u32 bufferlen,
991                                      u32 *buffer_actual_len,
992                                      u64 *requestid);
993
994
995 extern void vmbus_get_debug_info(struct vmbus_channel *channel,
996                                      struct vmbus_channel_debug_info *debug);
997
998 extern void vmbus_ontimer(unsigned long data);
999
1000 struct hv_dev_port_info {
1001         u32 int_mask;
1002         u32 read_idx;
1003         u32 write_idx;
1004         u32 bytes_avail_toread;
1005         u32 bytes_avail_towrite;
1006 };
1007
1008 /* Base driver object */
1009 struct hv_driver {
1010         const char *name;
1011
1012         /* the device type supported by this driver */
1013         uuid_le dev_type;
1014         const struct hv_vmbus_device_id *id_table;
1015
1016         struct device_driver driver;
1017
1018         int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
1019         int (*remove)(struct hv_device *);
1020         void (*shutdown)(struct hv_device *);
1021
1022 };
1023
1024 /* Base device object */
1025 struct hv_device {
1026         /* the device type id of this device */
1027         uuid_le dev_type;
1028
1029         /* the device instance id of this device */
1030         uuid_le dev_instance;
1031
1032         struct device device;
1033
1034         struct vmbus_channel *channel;
1035 };
1036
1037
1038 static inline struct hv_device *device_to_hv_device(struct device *d)
1039 {
1040         return container_of(d, struct hv_device, device);
1041 }
1042
1043 static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
1044 {
1045         return container_of(d, struct hv_driver, driver);
1046 }
1047
1048 static inline void hv_set_drvdata(struct hv_device *dev, void *data)
1049 {
1050         dev_set_drvdata(&dev->device, data);
1051 }
1052
1053 static inline void *hv_get_drvdata(struct hv_device *dev)
1054 {
1055         return dev_get_drvdata(&dev->device);
1056 }
1057
1058 /* Vmbus interface */
1059 #define vmbus_driver_register(driver)   \
1060         __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
1061 int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
1062                                          struct module *owner,
1063                                          const char *mod_name);
1064 void vmbus_driver_unregister(struct hv_driver *hv_driver);
1065
1066 /**
1067  * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
1068  *
1069  * This macro is used to create a struct hv_vmbus_device_id that matches a
1070  * specific device.
1071  */
1072 #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7,    \
1073                      g8, g9, ga, gb, gc, gd, ge, gf)    \
1074         .guid = { g0, g1, g2, g3, g4, g5, g6, g7,       \
1075                   g8, g9, ga, gb, gc, gd, ge, gf },
1076
1077 /*
1078  * Common header for Hyper-V ICs
1079  */
1080
1081 #define ICMSGTYPE_NEGOTIATE             0
1082 #define ICMSGTYPE_HEARTBEAT             1
1083 #define ICMSGTYPE_KVPEXCHANGE           2
1084 #define ICMSGTYPE_SHUTDOWN              3
1085 #define ICMSGTYPE_TIMESYNC              4
1086 #define ICMSGTYPE_VSS                   5
1087
1088 #define ICMSGHDRFLAG_TRANSACTION        1
1089 #define ICMSGHDRFLAG_REQUEST            2
1090 #define ICMSGHDRFLAG_RESPONSE           4
1091
1092
1093 /*
1094  * While we want to handle util services as regular devices,
1095  * there is only one instance of each of these services; so
1096  * we statically allocate the service specific state.
1097  */
1098
1099 struct hv_util_service {
1100         u8 *recv_buffer;
1101         void (*util_cb)(void *);
1102         int (*util_init)(struct hv_util_service *);
1103         void (*util_deinit)(void);
1104 };
1105
1106 struct vmbuspipe_hdr {
1107         u32 flags;
1108         u32 msgsize;
1109 } __packed;
1110
1111 struct ic_version {
1112         u16 major;
1113         u16 minor;
1114 } __packed;
1115
1116 struct icmsg_hdr {
1117         struct ic_version icverframe;
1118         u16 icmsgtype;
1119         struct ic_version icvermsg;
1120         u16 icmsgsize;
1121         u32 status;
1122         u8 ictransaction_id;
1123         u8 icflags;
1124         u8 reserved[2];
1125 } __packed;
1126
1127 struct icmsg_negotiate {
1128         u16 icframe_vercnt;
1129         u16 icmsg_vercnt;
1130         u32 reserved;
1131         struct ic_version icversion_data[1]; /* any size array */
1132 } __packed;
1133
1134 struct shutdown_msg_data {
1135         u32 reason_code;
1136         u32 timeout_seconds;
1137         u32 flags;
1138         u8  display_message[2048];
1139 } __packed;
1140
1141 struct heartbeat_msg_data {
1142         u64 seq_num;
1143         u32 reserved[8];
1144 } __packed;
1145
1146 /* Time Sync IC defs */
1147 #define ICTIMESYNCFLAG_PROBE    0
1148 #define ICTIMESYNCFLAG_SYNC     1
1149 #define ICTIMESYNCFLAG_SAMPLE   2
1150
1151 #ifdef __x86_64__
1152 #define WLTIMEDELTA     116444736000000000L     /* in 100ns unit */
1153 #else
1154 #define WLTIMEDELTA     116444736000000000LL
1155 #endif
1156
1157 struct ictimesync_data {
1158         u64 parenttime;
1159         u64 childtime;
1160         u64 roundtriptime;
1161         u8 flags;
1162 } __packed;
1163
1164 struct hyperv_service_callback {
1165         u8 msg_type;
1166         char *log_msg;
1167         uuid_le data;
1168         struct vmbus_channel *channel;
1169         void (*callback) (void *context);
1170 };
1171
1172 #define MAX_SRV_VER     0x7ffffff
1173 extern void vmbus_prep_negotiate_resp(struct icmsg_hdr *,
1174                                         struct icmsg_negotiate *, u8 *, int,
1175                                         int);
1176
1177 int hv_kvp_init(struct hv_util_service *);
1178 void hv_kvp_deinit(void);
1179 void hv_kvp_onchannelcallback(void *);
1180
1181 #endif /* __KERNEL__ */
1182 #endif /* _HYPERV_H */