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