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