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