blob: 7585d5533e43f15aa118465062872d193ebe51d7 [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
K. Y. Srinivasan9b5957802012-08-13 10:06:51 -0700125/*
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
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800165enum hv_kvp_exchg_op {
166 KVP_OP_GET = 0,
167 KVP_OP_SET,
168 KVP_OP_DELETE,
169 KVP_OP_ENUMERATE,
K. Y. Srinivasan9b5957802012-08-13 10:06:51 -0700170 KVP_OP_GET_IP_INFO,
171 KVP_OP_SET_IP_INFO,
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800172 KVP_OP_COUNT /* Number of operations, must be last. */
173};
174
175enum 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
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700184/*
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
K. Y. Srinivasan9b5957802012-08-13 10:06:51 -0700195#define ADDR_FAMILY_NONE 0x00
196#define ADDR_FAMILY_IPV4 0x01
197#define ADDR_FAMILY_IPV6 0x02
198
199#define MAX_ADAPTER_ID_SIZE 128
200#define MAX_IP_ADDR_SIZE 1024
201#define MAX_GATEWAY_SIZE 512
202
203
204struct hv_kvp_ipaddr_value {
205 __u16 adapter_id[MAX_ADAPTER_ID_SIZE];
206 __u8 addr_family;
207 __u8 dhcp_enabled;
208 __u16 ip_addr[MAX_IP_ADDR_SIZE];
209 __u16 sub_net[MAX_IP_ADDR_SIZE];
210 __u16 gate_way[MAX_GATEWAY_SIZE];
211 __u16 dns_addr[MAX_IP_ADDR_SIZE];
212} __attribute__((packed));
213
214
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800215struct hv_kvp_hdr {
K. Y. Srinivasan59a084a2012-02-02 16:56:48 -0800216 __u8 operation;
217 __u8 pool;
218 __u16 pad;
219} __attribute__((packed));
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800220
221struct hv_kvp_exchg_msg_value {
K. Y. Srinivasan59a084a2012-02-02 16:56:48 -0800222 __u32 value_type;
223 __u32 key_size;
224 __u32 value_size;
225 __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800226 union {
227 __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
228 __u32 value_u32;
229 __u64 value_u64;
230 };
K. Y. Srinivasan59a084a2012-02-02 16:56:48 -0800231} __attribute__((packed));
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800232
233struct hv_kvp_msg_enumerate {
K. Y. Srinivasan59a084a2012-02-02 16:56:48 -0800234 __u32 index;
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800235 struct hv_kvp_exchg_msg_value data;
K. Y. Srinivasan59a084a2012-02-02 16:56:48 -0800236} __attribute__((packed));
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800237
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800238struct hv_kvp_msg_get {
239 struct hv_kvp_exchg_msg_value data;
240};
241
242struct hv_kvp_msg_set {
243 struct hv_kvp_exchg_msg_value data;
244};
245
246struct hv_kvp_msg_delete {
247 __u32 key_size;
248 __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
249};
250
251struct hv_kvp_register {
252 __u8 version[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
253};
254
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800255struct hv_kvp_msg {
K. Y. Srinivasan9b5957802012-08-13 10:06:51 -0700256 union {
257 struct hv_kvp_hdr kvp_hdr;
258 int error;
259 };
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800260 union {
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800261 struct hv_kvp_msg_get kvp_get;
262 struct hv_kvp_msg_set kvp_set;
263 struct hv_kvp_msg_delete kvp_delete;
264 struct hv_kvp_msg_enumerate kvp_enum_data;
K. Y. Srinivasan9b5957802012-08-13 10:06:51 -0700265 struct hv_kvp_ipaddr_value kvp_ip_val;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800266 struct hv_kvp_register kvp_register;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800267 } body;
K. Y. Srinivasan59a084a2012-02-02 16:56:48 -0800268} __attribute__((packed));
K. Y. Srinivasan29394372012-01-27 15:55:58 -0800269
K. Y. Srinivasan9b5957802012-08-13 10:06:51 -0700270struct hv_kvp_ip_msg {
271 __u8 operation;
272 __u8 pool;
273 struct hv_kvp_ipaddr_value kvp_ip_val;
274} __attribute__((packed));
275
K. Y. Srinivasan59a084a2012-02-02 16:56:48 -0800276#ifdef __KERNEL__
K. Y. Srinivasan8ff3e6f2011-05-12 19:34:27 -0700277#include <linux/scatterlist.h>
278#include <linux/list.h>
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700279#include <linux/uuid.h>
K. Y. Srinivasan8ff3e6f2011-05-12 19:34:27 -0700280#include <linux/timer.h>
281#include <linux/workqueue.h>
282#include <linux/completion.h>
283#include <linux/device.h>
K. Y. Srinivasan2e2c1d12011-08-25 09:48:31 -0700284#include <linux/mod_devicetable.h>
K. Y. Srinivasan8ff3e6f2011-05-12 19:34:27 -0700285
286
Haiyang Zhangc31c1512012-02-02 07:18:00 +0000287#define MAX_PAGE_BUFFER_COUNT 19
K. Y. Srinivasana363bf72011-05-12 19:34:16 -0700288#define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
289
290#pragma pack(push, 1)
291
292/* Single-page buffer */
293struct hv_page_buffer {
294 u32 len;
295 u32 offset;
296 u64 pfn;
297};
298
299/* Multiple-page buffer */
300struct hv_multipage_buffer {
301 /* Length and Offset determines the # of pfns in the array */
302 u32 len;
303 u32 offset;
304 u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
305};
306
307/* 0x18 includes the proprietary packet header */
308#define MAX_PAGE_BUFFER_PACKET (0x18 + \
309 (sizeof(struct hv_page_buffer) * \
310 MAX_PAGE_BUFFER_COUNT))
311#define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
312 sizeof(struct hv_multipage_buffer))
313
314
315#pragma pack(pop)
316
K. Y. Srinivasan7effffb2011-05-12 19:34:17 -0700317struct hv_ring_buffer {
318 /* Offset in bytes from the start of ring data below */
319 u32 write_index;
320
321 /* Offset in bytes from the start of ring data below */
322 u32 read_index;
323
324 u32 interrupt_mask;
325
326 /* Pad it to PAGE_SIZE so that data starts on page boundary */
327 u8 reserved[4084];
328
329 /* NOTE:
330 * The interrupt_mask field is used only for channels but since our
331 * vmbus connection also uses this data structure and its data starts
332 * here, we commented out this field.
333 */
334
335 /*
336 * Ring data starts here + RingDataStartOffset
337 * !!! DO NOT place any fields below this !!!
338 */
339 u8 buffer[0];
340} __packed;
341
342struct hv_ring_buffer_info {
343 struct hv_ring_buffer *ring_buffer;
344 u32 ring_size; /* Include the shared header */
345 spinlock_t ring_lock;
346
347 u32 ring_datasize; /* < ring_size */
348 u32 ring_data_startoffset;
349};
350
351struct hv_ring_buffer_debug_info {
352 u32 current_interrupt_mask;
353 u32 current_read_index;
354 u32 current_write_index;
355 u32 bytes_avail_toread;
356 u32 bytes_avail_towrite;
357};
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -0700358
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000359
360/*
361 *
362 * hv_get_ringbuffer_availbytes()
363 *
364 * Get number of bytes available to read and to write to
365 * for the specified ring buffer
366 */
367static inline void
368hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
369 u32 *read, u32 *write)
370{
371 u32 read_loc, write_loc, dsize;
372
373 smp_read_barrier_depends();
374
375 /* Capture the read/write indices before they changed */
376 read_loc = rbi->ring_buffer->read_index;
377 write_loc = rbi->ring_buffer->write_index;
378 dsize = rbi->ring_datasize;
379
380 *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
381 read_loc - write_loc;
382 *read = dsize - *write;
383}
384
385
K. Y. Srinivasanf7c6dfd2011-05-12 19:34:18 -0700386/*
387 * We use the same version numbering for all Hyper-V modules.
388 *
389 * Definition of versioning is as follows;
390 *
391 * Major Number Changes for these scenarios;
392 * 1. When a new version of Windows Hyper-V
393 * is released.
394 * 2. A Major change has occurred in the
395 * Linux IC's.
396 * (For example the merge for the first time
397 * into the kernel) Every time the Major Number
398 * changes, the Revision number is reset to 0.
399 * Minor Number Changes when new functionality is added
400 * to the Linux IC's that is not a bug fix.
401 *
402 * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
403 */
404#define HV_DRV_VERSION "3.1"
405
406
K. Y. Srinivasan517d8dc2011-05-12 19:34:19 -0700407/*
408 * A revision number of vmbus that is used for ensuring both ends on a
409 * partition are using compatible versions.
410 */
411#define VMBUS_REVISION_NUMBER 13
412
413/* Make maximum size of pipe payload of 16K */
414#define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
415
416/* Define PipeMode values. */
417#define VMBUS_PIPE_TYPE_BYTE 0x00000000
418#define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
419
420/* The size of the user defined data buffer for non-pipe offers. */
421#define MAX_USER_DEFINED_BYTES 120
422
423/* The size of the user defined data buffer for pipe offers. */
424#define MAX_PIPE_USER_DEFINED_BYTES 116
425
426/*
427 * At the center of the Channel Management library is the Channel Offer. This
428 * struct contains the fundamental information about an offer.
429 */
430struct vmbus_channel_offer {
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700431 uuid_le if_type;
432 uuid_le if_instance;
K. Y. Srinivasan517d8dc2011-05-12 19:34:19 -0700433 u64 int_latency; /* in 100ns units */
434 u32 if_revision;
435 u32 server_ctx_size; /* in bytes */
436 u16 chn_flags;
437 u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
438
439 union {
440 /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
441 struct {
442 unsigned char user_def[MAX_USER_DEFINED_BYTES];
443 } std;
444
445 /*
446 * Pipes:
447 * The following sructure is an integrated pipe protocol, which
448 * is implemented on top of standard user-defined data. Pipe
449 * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
450 * use.
451 */
452 struct {
453 u32 pipe_mode;
454 unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
455 } pipe;
456 } u;
457 u32 padding;
458} __packed;
459
460/* Server Flags */
461#define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
462#define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
463#define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
464#define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
465#define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
466#define VMBUS_CHANNEL_PARENT_OFFER 0x200
467#define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
468
K. Y. Srinivasan50ed40e02011-05-12 19:34:20 -0700469struct vmpacket_descriptor {
470 u16 type;
471 u16 offset8;
472 u16 len8;
473 u16 flags;
474 u64 trans_id;
475} __packed;
476
477struct vmpacket_header {
478 u32 prev_pkt_start_offset;
479 struct vmpacket_descriptor descriptor;
480} __packed;
481
482struct vmtransfer_page_range {
483 u32 byte_count;
484 u32 byte_offset;
485} __packed;
486
487struct vmtransfer_page_packet_header {
488 struct vmpacket_descriptor d;
489 u16 xfer_pageset_id;
K. Y. Srinivasan1508d812012-08-16 08:23:20 -0700490 u8 sender_owns_set;
K. Y. Srinivasan50ed40e02011-05-12 19:34:20 -0700491 u8 reserved;
492 u32 range_cnt;
493 struct vmtransfer_page_range ranges[1];
494} __packed;
495
496struct vmgpadl_packet_header {
497 struct vmpacket_descriptor d;
498 u32 gpadl;
499 u32 reserved;
500} __packed;
501
502struct vmadd_remove_transfer_page_set {
503 struct vmpacket_descriptor d;
504 u32 gpadl;
505 u16 xfer_pageset_id;
506 u16 reserved;
507} __packed;
508
509/*
510 * This structure defines a range in guest physical space that can be made to
511 * look virtually contiguous.
512 */
513struct gpa_range {
514 u32 byte_count;
515 u32 byte_offset;
516 u64 pfn_array[0];
517};
518
519/*
520 * This is the format for an Establish Gpadl packet, which contains a handle by
521 * which this GPADL will be known and a set of GPA ranges associated with it.
522 * This can be converted to a MDL by the guest OS. If there are multiple GPA
523 * ranges, then the resulting MDL will be "chained," representing multiple VA
524 * ranges.
525 */
526struct vmestablish_gpadl {
527 struct vmpacket_descriptor d;
528 u32 gpadl;
529 u32 range_cnt;
530 struct gpa_range range[1];
531} __packed;
532
533/*
534 * This is the format for a Teardown Gpadl packet, which indicates that the
535 * GPADL handle in the Establish Gpadl packet will never be referenced again.
536 */
537struct vmteardown_gpadl {
538 struct vmpacket_descriptor d;
539 u32 gpadl;
540 u32 reserved; /* for alignment to a 8-byte boundary */
541} __packed;
542
543/*
544 * This is the format for a GPA-Direct packet, which contains a set of GPA
545 * ranges, in addition to commands and/or data.
546 */
547struct vmdata_gpa_direct {
548 struct vmpacket_descriptor d;
549 u32 reserved;
550 u32 range_cnt;
551 struct gpa_range range[1];
552} __packed;
553
554/* This is the format for a Additional Data Packet. */
555struct vmadditional_data {
556 struct vmpacket_descriptor d;
557 u64 total_bytes;
558 u32 offset;
559 u32 byte_cnt;
560 unsigned char data[1];
561} __packed;
562
563union vmpacket_largest_possible_header {
564 struct vmpacket_descriptor simple_hdr;
565 struct vmtransfer_page_packet_header xfer_page_hdr;
566 struct vmgpadl_packet_header gpadl_hdr;
567 struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
568 struct vmestablish_gpadl establish_gpadl_hdr;
569 struct vmteardown_gpadl teardown_gpadl_hdr;
570 struct vmdata_gpa_direct data_gpa_direct_hdr;
571};
572
573#define VMPACKET_DATA_START_ADDRESS(__packet) \
574 (void *)(((unsigned char *)__packet) + \
575 ((struct vmpacket_descriptor)__packet)->offset8 * 8)
576
577#define VMPACKET_DATA_LENGTH(__packet) \
578 ((((struct vmpacket_descriptor)__packet)->len8 - \
579 ((struct vmpacket_descriptor)__packet)->offset8) * 8)
580
581#define VMPACKET_TRANSFER_MODE(__packet) \
582 (((struct IMPACT)__packet)->type)
583
584enum vmbus_packet_type {
585 VM_PKT_INVALID = 0x0,
586 VM_PKT_SYNCH = 0x1,
587 VM_PKT_ADD_XFER_PAGESET = 0x2,
588 VM_PKT_RM_XFER_PAGESET = 0x3,
589 VM_PKT_ESTABLISH_GPADL = 0x4,
590 VM_PKT_TEARDOWN_GPADL = 0x5,
591 VM_PKT_DATA_INBAND = 0x6,
592 VM_PKT_DATA_USING_XFER_PAGES = 0x7,
593 VM_PKT_DATA_USING_GPADL = 0x8,
594 VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
595 VM_PKT_CANCEL_REQUEST = 0xa,
596 VM_PKT_COMP = 0xb,
597 VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
598 VM_PKT_ADDITIONAL_DATA = 0xd
599};
600
601#define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
K. Y. Srinivasan517d8dc2011-05-12 19:34:19 -0700602
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700603
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700604/* Version 1 messages */
605enum vmbus_channel_message_type {
606 CHANNELMSG_INVALID = 0,
607 CHANNELMSG_OFFERCHANNEL = 1,
608 CHANNELMSG_RESCIND_CHANNELOFFER = 2,
609 CHANNELMSG_REQUESTOFFERS = 3,
610 CHANNELMSG_ALLOFFERS_DELIVERED = 4,
611 CHANNELMSG_OPENCHANNEL = 5,
612 CHANNELMSG_OPENCHANNEL_RESULT = 6,
613 CHANNELMSG_CLOSECHANNEL = 7,
614 CHANNELMSG_GPADL_HEADER = 8,
615 CHANNELMSG_GPADL_BODY = 9,
616 CHANNELMSG_GPADL_CREATED = 10,
617 CHANNELMSG_GPADL_TEARDOWN = 11,
618 CHANNELMSG_GPADL_TORNDOWN = 12,
619 CHANNELMSG_RELID_RELEASED = 13,
620 CHANNELMSG_INITIATE_CONTACT = 14,
621 CHANNELMSG_VERSION_RESPONSE = 15,
622 CHANNELMSG_UNLOAD = 16,
623#ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
624 CHANNELMSG_VIEWRANGE_ADD = 17,
625 CHANNELMSG_VIEWRANGE_REMOVE = 18,
626#endif
627 CHANNELMSG_COUNT
628};
629
630struct vmbus_channel_message_header {
631 enum vmbus_channel_message_type msgtype;
632 u32 padding;
633} __packed;
634
635/* Query VMBus Version parameters */
636struct vmbus_channel_query_vmbus_version {
637 struct vmbus_channel_message_header header;
638 u32 version;
639} __packed;
640
641/* VMBus Version Supported parameters */
642struct vmbus_channel_version_supported {
643 struct vmbus_channel_message_header header;
K. Y. Srinivasan1508d812012-08-16 08:23:20 -0700644 u8 version_supported;
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700645} __packed;
646
647/* Offer Channel parameters */
648struct vmbus_channel_offer_channel {
649 struct vmbus_channel_message_header header;
650 struct vmbus_channel_offer offer;
651 u32 child_relid;
652 u8 monitorid;
K. Y. Srinivasan1508d812012-08-16 08:23:20 -0700653 u8 monitor_allocated;
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700654} __packed;
655
656/* Rescind Offer parameters */
657struct vmbus_channel_rescind_offer {
658 struct vmbus_channel_message_header header;
659 u32 child_relid;
660} __packed;
661
662/*
663 * Request Offer -- no parameters, SynIC message contains the partition ID
664 * Set Snoop -- no parameters, SynIC message contains the partition ID
665 * Clear Snoop -- no parameters, SynIC message contains the partition ID
666 * All Offers Delivered -- no parameters, SynIC message contains the partition
667 * ID
668 * Flush Client -- no parameters, SynIC message contains the partition ID
669 */
670
671/* Open Channel parameters */
672struct vmbus_channel_open_channel {
673 struct vmbus_channel_message_header header;
674
675 /* Identifies the specific VMBus channel that is being opened. */
676 u32 child_relid;
677
678 /* ID making a particular open request at a channel offer unique. */
679 u32 openid;
680
681 /* GPADL for the channel's ring buffer. */
682 u32 ringbuffer_gpadlhandle;
683
684 /* GPADL for the channel's server context save area. */
685 u32 server_contextarea_gpadlhandle;
686
687 /*
688 * The upstream ring buffer begins at offset zero in the memory
689 * described by RingBufferGpadlHandle. The downstream ring buffer
690 * follows it at this offset (in pages).
691 */
692 u32 downstream_ringbuffer_pageoffset;
693
694 /* User-specific data to be passed along to the server endpoint. */
695 unsigned char userdata[MAX_USER_DEFINED_BYTES];
696} __packed;
697
698/* Open Channel Result parameters */
699struct vmbus_channel_open_result {
700 struct vmbus_channel_message_header header;
701 u32 child_relid;
702 u32 openid;
703 u32 status;
704} __packed;
705
706/* Close channel parameters; */
707struct vmbus_channel_close_channel {
708 struct vmbus_channel_message_header header;
709 u32 child_relid;
710} __packed;
711
712/* Channel Message GPADL */
713#define GPADL_TYPE_RING_BUFFER 1
714#define GPADL_TYPE_SERVER_SAVE_AREA 2
715#define GPADL_TYPE_TRANSACTION 8
716
717/*
718 * The number of PFNs in a GPADL message is defined by the number of
719 * pages that would be spanned by ByteCount and ByteOffset. If the
720 * implied number of PFNs won't fit in this packet, there will be a
721 * follow-up packet that contains more.
722 */
723struct vmbus_channel_gpadl_header {
724 struct vmbus_channel_message_header header;
725 u32 child_relid;
726 u32 gpadl;
727 u16 range_buflen;
728 u16 rangecount;
729 struct gpa_range range[0];
730} __packed;
731
732/* This is the followup packet that contains more PFNs. */
733struct vmbus_channel_gpadl_body {
734 struct vmbus_channel_message_header header;
735 u32 msgnumber;
736 u32 gpadl;
737 u64 pfn[0];
738} __packed;
739
740struct vmbus_channel_gpadl_created {
741 struct vmbus_channel_message_header header;
742 u32 child_relid;
743 u32 gpadl;
744 u32 creation_status;
745} __packed;
746
747struct vmbus_channel_gpadl_teardown {
748 struct vmbus_channel_message_header header;
749 u32 child_relid;
750 u32 gpadl;
751} __packed;
752
753struct vmbus_channel_gpadl_torndown {
754 struct vmbus_channel_message_header header;
755 u32 gpadl;
756} __packed;
757
758#ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
759struct vmbus_channel_view_range_add {
760 struct vmbus_channel_message_header header;
761 PHYSICAL_ADDRESS viewrange_base;
762 u64 viewrange_length;
763 u32 child_relid;
764} __packed;
765
766struct vmbus_channel_view_range_remove {
767 struct vmbus_channel_message_header header;
768 PHYSICAL_ADDRESS viewrange_base;
769 u32 child_relid;
770} __packed;
771#endif
772
773struct vmbus_channel_relid_released {
774 struct vmbus_channel_message_header header;
775 u32 child_relid;
776} __packed;
777
778struct vmbus_channel_initiate_contact {
779 struct vmbus_channel_message_header header;
780 u32 vmbus_version_requested;
781 u32 padding2;
782 u64 interrupt_page;
783 u64 monitor_page1;
784 u64 monitor_page2;
785} __packed;
786
787struct vmbus_channel_version_response {
788 struct vmbus_channel_message_header header;
K. Y. Srinivasan1508d812012-08-16 08:23:20 -0700789 u8 version_supported;
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700790} __packed;
791
792enum vmbus_channel_state {
793 CHANNEL_OFFER_STATE,
794 CHANNEL_OPENING_STATE,
795 CHANNEL_OPEN_STATE,
796};
797
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700798struct vmbus_channel_debug_info {
799 u32 relid;
800 enum vmbus_channel_state state;
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700801 uuid_le interfacetype;
802 uuid_le interface_instance;
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700803 u32 monitorid;
804 u32 servermonitor_pending;
805 u32 servermonitor_latency;
806 u32 servermonitor_connectionid;
807 u32 clientmonitor_pending;
808 u32 clientmonitor_latency;
809 u32 clientmonitor_connectionid;
810
811 struct hv_ring_buffer_debug_info inbound;
812 struct hv_ring_buffer_debug_info outbound;
813};
814
815/*
816 * Represents each channel msg on the vmbus connection This is a
817 * variable-size data structure depending on the msg type itself
818 */
819struct vmbus_channel_msginfo {
820 /* Bookkeeping stuff */
821 struct list_head msglistentry;
822
823 /* So far, this is only used to handle gpadl body message */
824 struct list_head submsglist;
825
826 /* Synchronize the request/response if needed */
827 struct completion waitevent;
828 union {
829 struct vmbus_channel_version_supported version_supported;
830 struct vmbus_channel_open_result open_result;
831 struct vmbus_channel_gpadl_torndown gpadl_torndown;
832 struct vmbus_channel_gpadl_created gpadl_created;
833 struct vmbus_channel_version_response version_response;
834 } response;
835
836 u32 msgsize;
837 /*
838 * The channel message that goes out on the "wire".
839 * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
840 */
841 unsigned char msg[0];
842};
843
K. Y. Srinivasanf9f1db82011-06-06 15:49:58 -0700844struct vmbus_close_msg {
845 struct vmbus_channel_msginfo info;
846 struct vmbus_channel_close_channel msg;
847};
848
K. Y. Srinivasan7d7c75c2011-06-06 15:49:57 -0700849struct vmbus_channel {
850 struct list_head listentry;
851
852 struct hv_device *device_obj;
853
854 struct work_struct work;
855
856 enum vmbus_channel_state state;
K. Y. Srinivasan7d7c75c2011-06-06 15:49:57 -0700857
858 struct vmbus_channel_offer_channel offermsg;
859 /*
860 * These are based on the OfferMsg.MonitorId.
861 * Save it here for easy access.
862 */
863 u8 monitor_grp;
864 u8 monitor_bit;
865
866 u32 ringbuffer_gpadlhandle;
867
868 /* Allocated memory for ring buffer */
869 void *ringbuffer_pages;
870 u32 ringbuffer_pagecount;
871 struct hv_ring_buffer_info outbound; /* send to parent */
872 struct hv_ring_buffer_info inbound; /* receive from parent */
873 spinlock_t inbound_lock;
874 struct workqueue_struct *controlwq;
875
K. Y. Srinivasanf9f1db82011-06-06 15:49:58 -0700876 struct vmbus_close_msg close_msg;
877
K. Y. Srinivasan7d7c75c2011-06-06 15:49:57 -0700878 /* Channel callback are invoked in this workqueue context */
879 /* HANDLE dataWorkQueue; */
880
881 void (*onchannel_callback)(void *context);
882 void *channel_callback_context;
883};
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700884
K. Y. Srinivasanb56dda02011-05-12 19:34:21 -0700885void vmbus_onmessage(void *context);
886
887int vmbus_request_offers(void);
888
K. Y. Srinivasanc35470b2011-05-12 19:34:22 -0700889/* The format must be the same as struct vmdata_gpa_direct */
890struct vmbus_channel_packet_page_buffer {
891 u16 type;
892 u16 dataoffset8;
893 u16 length8;
894 u16 flags;
895 u64 transactionid;
896 u32 reserved;
897 u32 rangecount;
898 struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
899} __packed;
900
901/* The format must be the same as struct vmdata_gpa_direct */
902struct vmbus_channel_packet_multipage_buffer {
903 u16 type;
904 u16 dataoffset8;
905 u16 length8;
906 u16 flags;
907 u64 transactionid;
908 u32 reserved;
909 u32 rangecount; /* Always 1 in this case */
910 struct hv_multipage_buffer range;
911} __packed;
912
913
914extern int vmbus_open(struct vmbus_channel *channel,
915 u32 send_ringbuffersize,
916 u32 recv_ringbuffersize,
917 void *userdata,
918 u32 userdatalen,
919 void(*onchannel_callback)(void *context),
920 void *context);
921
922extern void vmbus_close(struct vmbus_channel *channel);
923
924extern int vmbus_sendpacket(struct vmbus_channel *channel,
925 const void *buffer,
926 u32 bufferLen,
927 u64 requestid,
928 enum vmbus_packet_type type,
929 u32 flags);
930
931extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
932 struct hv_page_buffer pagebuffers[],
933 u32 pagecount,
934 void *buffer,
935 u32 bufferlen,
936 u64 requestid);
937
938extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
939 struct hv_multipage_buffer *mpb,
940 void *buffer,
941 u32 bufferlen,
942 u64 requestid);
943
944extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
945 void *kbuffer,
946 u32 size,
947 u32 *gpadl_handle);
948
949extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
950 u32 gpadl_handle);
951
952extern int vmbus_recvpacket(struct vmbus_channel *channel,
953 void *buffer,
954 u32 bufferlen,
955 u32 *buffer_actual_len,
956 u64 *requestid);
957
958extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
959 void *buffer,
960 u32 bufferlen,
961 u32 *buffer_actual_len,
962 u64 *requestid);
963
K. Y. Srinivasanc35470b2011-05-12 19:34:22 -0700964
965extern void vmbus_get_debug_info(struct vmbus_channel *channel,
966 struct vmbus_channel_debug_info *debug);
967
968extern void vmbus_ontimer(unsigned long data);
969
K. Y. Srinivasan35ea09c2011-05-12 19:34:24 -0700970struct hv_dev_port_info {
971 u32 int_mask;
972 u32 read_idx;
973 u32 write_idx;
974 u32 bytes_avail_toread;
975 u32 bytes_avail_towrite;
976};
977
K. Y. Srinivasan35ea09c2011-05-12 19:34:24 -0700978/* Base driver object */
979struct hv_driver {
980 const char *name;
981
982 /* the device type supported by this driver */
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700983 uuid_le dev_type;
K. Y. Srinivasan2e2c1d12011-08-25 09:48:31 -0700984 const struct hv_vmbus_device_id *id_table;
K. Y. Srinivasan35ea09c2011-05-12 19:34:24 -0700985
986 struct device_driver driver;
987
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700988 int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
K. Y. Srinivasan35ea09c2011-05-12 19:34:24 -0700989 int (*remove)(struct hv_device *);
990 void (*shutdown)(struct hv_device *);
991
992};
993
994/* Base device object */
995struct hv_device {
996 /* the device type id of this device */
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700997 uuid_le dev_type;
K. Y. Srinivasan35ea09c2011-05-12 19:34:24 -0700998
999 /* the device instance id of this device */
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -07001000 uuid_le dev_instance;
K. Y. Srinivasan35ea09c2011-05-12 19:34:24 -07001001
1002 struct device device;
1003
1004 struct vmbus_channel *channel;
K. Y. Srinivasan35ea09c2011-05-12 19:34:24 -07001005};
1006
K. Y. Srinivasan27b5b3c2011-05-12 19:34:25 -07001007
1008static inline struct hv_device *device_to_hv_device(struct device *d)
1009{
1010 return container_of(d, struct hv_device, device);
1011}
1012
1013static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
1014{
1015 return container_of(d, struct hv_driver, driver);
1016}
1017
K. Y. Srinivasanab101e82011-09-13 10:59:40 -07001018static inline void hv_set_drvdata(struct hv_device *dev, void *data)
1019{
1020 dev_set_drvdata(&dev->device, data);
1021}
1022
1023static inline void *hv_get_drvdata(struct hv_device *dev)
1024{
1025 return dev_get_drvdata(&dev->device);
1026}
K. Y. Srinivasan27b5b3c2011-05-12 19:34:25 -07001027
1028/* Vmbus interface */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001029#define vmbus_driver_register(driver) \
1030 __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
1031int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
1032 struct module *owner,
1033 const char *mod_name);
1034void vmbus_driver_unregister(struct hv_driver *hv_driver);
K. Y. Srinivasan27b5b3c2011-05-12 19:34:25 -07001035
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001036/**
1037 * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
1038 *
1039 * This macro is used to create a struct hv_vmbus_device_id that matches a
1040 * specific device.
1041 */
1042#define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
1043 g8, g9, ga, gb, gc, gd, ge, gf) \
1044 .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
1045 g8, g9, ga, gb, gc, gd, ge, gf },
1046
K. Y. Srinivasanb1897022011-05-12 19:34:26 -07001047/*
1048 * Common header for Hyper-V ICs
1049 */
1050
1051#define ICMSGTYPE_NEGOTIATE 0
1052#define ICMSGTYPE_HEARTBEAT 1
1053#define ICMSGTYPE_KVPEXCHANGE 2
1054#define ICMSGTYPE_SHUTDOWN 3
1055#define ICMSGTYPE_TIMESYNC 4
1056#define ICMSGTYPE_VSS 5
1057
1058#define ICMSGHDRFLAG_TRANSACTION 1
1059#define ICMSGHDRFLAG_REQUEST 2
1060#define ICMSGHDRFLAG_RESPONSE 4
1061
K. Y. Srinivasanb1897022011-05-12 19:34:26 -07001062
K. Y. Srinivasana29b6432011-09-18 10:31:33 -07001063/*
1064 * While we want to handle util services as regular devices,
1065 * there is only one instance of each of these services; so
1066 * we statically allocate the service specific state.
1067 */
1068
1069struct hv_util_service {
1070 u8 *recv_buffer;
1071 void (*util_cb)(void *);
1072 int (*util_init)(struct hv_util_service *);
1073 void (*util_deinit)(void);
1074};
1075
K. Y. Srinivasanb1897022011-05-12 19:34:26 -07001076struct vmbuspipe_hdr {
1077 u32 flags;
1078 u32 msgsize;
1079} __packed;
1080
1081struct ic_version {
1082 u16 major;
1083 u16 minor;
1084} __packed;
1085
1086struct icmsg_hdr {
1087 struct ic_version icverframe;
1088 u16 icmsgtype;
1089 struct ic_version icvermsg;
1090 u16 icmsgsize;
1091 u32 status;
1092 u8 ictransaction_id;
1093 u8 icflags;
1094 u8 reserved[2];
1095} __packed;
1096
1097struct icmsg_negotiate {
1098 u16 icframe_vercnt;
1099 u16 icmsg_vercnt;
1100 u32 reserved;
1101 struct ic_version icversion_data[1]; /* any size array */
1102} __packed;
1103
1104struct shutdown_msg_data {
1105 u32 reason_code;
1106 u32 timeout_seconds;
1107 u32 flags;
1108 u8 display_message[2048];
1109} __packed;
1110
1111struct heartbeat_msg_data {
1112 u64 seq_num;
1113 u32 reserved[8];
1114} __packed;
1115
1116/* Time Sync IC defs */
1117#define ICTIMESYNCFLAG_PROBE 0
1118#define ICTIMESYNCFLAG_SYNC 1
1119#define ICTIMESYNCFLAG_SAMPLE 2
1120
1121#ifdef __x86_64__
1122#define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
1123#else
1124#define WLTIMEDELTA 116444736000000000LL
1125#endif
1126
1127struct ictimesync_data {
1128 u64 parenttime;
1129 u64 childtime;
1130 u64 roundtriptime;
1131 u8 flags;
1132} __packed;
1133
K. Y. Srinivasanb1897022011-05-12 19:34:26 -07001134struct hyperv_service_callback {
1135 u8 msg_type;
1136 char *log_msg;
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -07001137 uuid_le data;
K. Y. Srinivasanb1897022011-05-12 19:34:26 -07001138 struct vmbus_channel *channel;
1139 void (*callback) (void *context);
1140};
1141
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -07001142#define MAX_SRV_VER 0x7ffffff
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -06001143extern void vmbus_prep_negotiate_resp(struct icmsg_hdr *,
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -07001144 struct icmsg_negotiate *, u8 *, int,
1145 int);
K. Y. Srinivasanb1897022011-05-12 19:34:26 -07001146
K. Y. Srinivasan29394372012-01-27 15:55:58 -08001147int hv_kvp_init(struct hv_util_service *);
1148void hv_kvp_deinit(void);
1149void hv_kvp_onchannelcallback(void *);
1150
1151#endif /* __KERNEL__ */
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -07001152#endif /* _HYPERV_H */