hyperv.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*
  2. *
  3. * Copyright (c) 2011, Microsoft Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. *
  18. * Authors:
  19. * Haiyang Zhang <haiyangz@microsoft.com>
  20. * Hank Janssen <hjanssen@microsoft.com>
  21. * K. Y. Srinivasan <kys@microsoft.com>
  22. *
  23. */
  24. #ifndef _HYPERV_H
  25. #define _HYPERV_H
  26. #include <linux/types.h>
  27. /*
  28. * An implementation of HyperV key value pair (KVP) functionality for Linux.
  29. *
  30. *
  31. * Copyright (C) 2010, Novell, Inc.
  32. * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
  33. *
  34. */
  35. /*
  36. * Maximum value size - used for both key names and value data, and includes
  37. * any applicable NULL terminators.
  38. *
  39. * Note: This limit is somewhat arbitrary, but falls easily within what is
  40. * supported for all native guests (back to Win 2000) and what is reasonable
  41. * for the IC KVP exchange functionality. Note that Windows Me/98/95 are
  42. * limited to 255 character key names.
  43. *
  44. * MSDN recommends not storing data values larger than 2048 bytes in the
  45. * registry.
  46. *
  47. * Note: This value is used in defining the KVP exchange message - this value
  48. * cannot be modified without affecting the message size and compatibility.
  49. */
  50. /*
  51. * bytes, including any null terminators
  52. */
  53. #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE (2048)
  54. /*
  55. * Maximum key size - the registry limit for the length of an entry name
  56. * is 256 characters, including the null terminator
  57. */
  58. #define HV_KVP_EXCHANGE_MAX_KEY_SIZE (512)
  59. /*
  60. * In Linux, we implement the KVP functionality in two components:
  61. * 1) The kernel component which is packaged as part of the hv_utils driver
  62. * is responsible for communicating with the host and responsible for
  63. * implementing the host/guest protocol. 2) A user level daemon that is
  64. * responsible for data gathering.
  65. *
  66. * Host/Guest Protocol: The host iterates over an index and expects the guest
  67. * to assign a key name to the index and also return the value corresponding to
  68. * the key. The host will have atmost one KVP transaction outstanding at any
  69. * given point in time. The host side iteration stops when the guest returns
  70. * an error. Microsoft has specified the following mapping of key names to
  71. * host specified index:
  72. *
  73. * Index Key Name
  74. * 0 FullyQualifiedDomainName
  75. * 1 IntegrationServicesVersion
  76. * 2 NetworkAddressIPv4
  77. * 3 NetworkAddressIPv6
  78. * 4 OSBuildNumber
  79. * 5 OSName
  80. * 6 OSMajorVersion
  81. * 7 OSMinorVersion
  82. * 8 OSVersion
  83. * 9 ProcessorArchitecture
  84. *
  85. * The Windows host expects the Key Name and Key Value to be encoded in utf16.
  86. *
  87. * Guest Kernel/KVP Daemon Protocol: As noted earlier, we implement all of the
  88. * data gathering functionality in a user mode daemon. The user level daemon
  89. * is also responsible for binding the key name to the index as well. The
  90. * kernel and user-level daemon communicate using a connector channel.
  91. *
  92. * The user mode component first registers with the
  93. * the kernel component. Subsequently, the kernel component requests, data
  94. * for the specified keys. In response to this message the user mode component
  95. * fills in the value corresponding to the specified key. We overload the
  96. * sequence field in the cn_msg header to define our KVP message types.
  97. *
  98. *
  99. * The kernel component simply acts as a conduit for communication between the
  100. * Windows host and the user-level daemon. The kernel component passes up the
  101. * index received from the Host to the user-level daemon. If the index is
  102. * valid (supported), the corresponding key as well as its
  103. * value (both are strings) is returned. If the index is invalid
  104. * (not supported), a NULL key string is returned.
  105. */
  106. /*
  107. * Registry value types.
  108. */
  109. #define REG_SZ 1
  110. #define REG_U32 4
  111. #define REG_U64 8
  112. enum hv_kvp_exchg_op {
  113. KVP_OP_GET = 0,
  114. KVP_OP_SET,
  115. KVP_OP_DELETE,
  116. KVP_OP_ENUMERATE,
  117. KVP_OP_REGISTER,
  118. KVP_OP_COUNT /* Number of operations, must be last. */
  119. };
  120. enum hv_kvp_exchg_pool {
  121. KVP_POOL_EXTERNAL = 0,
  122. KVP_POOL_GUEST,
  123. KVP_POOL_AUTO,
  124. KVP_POOL_AUTO_EXTERNAL,
  125. KVP_POOL_AUTO_INTERNAL,
  126. KVP_POOL_COUNT /* Number of pools, must be last. */
  127. };
  128. struct hv_kvp_hdr {
  129. __u8 operation;
  130. __u8 pool;
  131. __u16 pad;
  132. } __attribute__((packed));
  133. struct hv_kvp_exchg_msg_value {
  134. __u32 value_type;
  135. __u32 key_size;
  136. __u32 value_size;
  137. __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  138. union {
  139. __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
  140. __u32 value_u32;
  141. __u64 value_u64;
  142. };
  143. } __attribute__((packed));
  144. struct hv_kvp_msg_enumerate {
  145. __u32 index;
  146. struct hv_kvp_exchg_msg_value data;
  147. } __attribute__((packed));
  148. struct hv_kvp_msg_get {
  149. struct hv_kvp_exchg_msg_value data;
  150. };
  151. struct hv_kvp_msg_set {
  152. struct hv_kvp_exchg_msg_value data;
  153. };
  154. struct hv_kvp_msg_delete {
  155. __u32 key_size;
  156. __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  157. };
  158. struct hv_kvp_register {
  159. __u8 version[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  160. };
  161. struct hv_kvp_msg {
  162. struct hv_kvp_hdr kvp_hdr;
  163. union {
  164. struct hv_kvp_msg_get kvp_get;
  165. struct hv_kvp_msg_set kvp_set;
  166. struct hv_kvp_msg_delete kvp_delete;
  167. struct hv_kvp_msg_enumerate kvp_enum_data;
  168. struct hv_kvp_register kvp_register;
  169. } body;
  170. } __attribute__((packed));
  171. #ifdef __KERNEL__
  172. #include <linux/scatterlist.h>
  173. #include <linux/list.h>
  174. #include <linux/uuid.h>
  175. #include <linux/timer.h>
  176. #include <linux/workqueue.h>
  177. #include <linux/completion.h>
  178. #include <linux/device.h>
  179. #include <linux/mod_devicetable.h>
  180. #define MAX_PAGE_BUFFER_COUNT 19
  181. #define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
  182. #pragma pack(push, 1)
  183. /* Single-page buffer */
  184. struct hv_page_buffer {
  185. u32 len;
  186. u32 offset;
  187. u64 pfn;
  188. };
  189. /* Multiple-page buffer */
  190. struct hv_multipage_buffer {
  191. /* Length and Offset determines the # of pfns in the array */
  192. u32 len;
  193. u32 offset;
  194. u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
  195. };
  196. /* 0x18 includes the proprietary packet header */
  197. #define MAX_PAGE_BUFFER_PACKET (0x18 + \
  198. (sizeof(struct hv_page_buffer) * \
  199. MAX_PAGE_BUFFER_COUNT))
  200. #define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
  201. sizeof(struct hv_multipage_buffer))
  202. #pragma pack(pop)
  203. struct hv_ring_buffer {
  204. /* Offset in bytes from the start of ring data below */
  205. u32 write_index;
  206. /* Offset in bytes from the start of ring data below */
  207. u32 read_index;
  208. u32 interrupt_mask;
  209. /* Pad it to PAGE_SIZE so that data starts on page boundary */
  210. u8 reserved[4084];
  211. /* NOTE:
  212. * The interrupt_mask field is used only for channels but since our
  213. * vmbus connection also uses this data structure and its data starts
  214. * here, we commented out this field.
  215. */
  216. /*
  217. * Ring data starts here + RingDataStartOffset
  218. * !!! DO NOT place any fields below this !!!
  219. */
  220. u8 buffer[0];
  221. } __packed;
  222. struct hv_ring_buffer_info {
  223. struct hv_ring_buffer *ring_buffer;
  224. u32 ring_size; /* Include the shared header */
  225. spinlock_t ring_lock;
  226. u32 ring_datasize; /* < ring_size */
  227. u32 ring_data_startoffset;
  228. };
  229. struct hv_ring_buffer_debug_info {
  230. u32 current_interrupt_mask;
  231. u32 current_read_index;
  232. u32 current_write_index;
  233. u32 bytes_avail_toread;
  234. u32 bytes_avail_towrite;
  235. };
  236. /*
  237. * We use the same version numbering for all Hyper-V modules.
  238. *
  239. * Definition of versioning is as follows;
  240. *
  241. * Major Number Changes for these scenarios;
  242. * 1. When a new version of Windows Hyper-V
  243. * is released.
  244. * 2. A Major change has occurred in the
  245. * Linux IC's.
  246. * (For example the merge for the first time
  247. * into the kernel) Every time the Major Number
  248. * changes, the Revision number is reset to 0.
  249. * Minor Number Changes when new functionality is added
  250. * to the Linux IC's that is not a bug fix.
  251. *
  252. * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
  253. */
  254. #define HV_DRV_VERSION "3.1"
  255. /*
  256. * A revision number of vmbus that is used for ensuring both ends on a
  257. * partition are using compatible versions.
  258. */
  259. #define VMBUS_REVISION_NUMBER 13
  260. /* Make maximum size of pipe payload of 16K */
  261. #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
  262. /* Define PipeMode values. */
  263. #define VMBUS_PIPE_TYPE_BYTE 0x00000000
  264. #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
  265. /* The size of the user defined data buffer for non-pipe offers. */
  266. #define MAX_USER_DEFINED_BYTES 120
  267. /* The size of the user defined data buffer for pipe offers. */
  268. #define MAX_PIPE_USER_DEFINED_BYTES 116
  269. /*
  270. * At the center of the Channel Management library is the Channel Offer. This
  271. * struct contains the fundamental information about an offer.
  272. */
  273. struct vmbus_channel_offer {
  274. uuid_le if_type;
  275. uuid_le if_instance;
  276. u64 int_latency; /* in 100ns units */
  277. u32 if_revision;
  278. u32 server_ctx_size; /* in bytes */
  279. u16 chn_flags;
  280. u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
  281. union {
  282. /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
  283. struct {
  284. unsigned char user_def[MAX_USER_DEFINED_BYTES];
  285. } std;
  286. /*
  287. * Pipes:
  288. * The following sructure is an integrated pipe protocol, which
  289. * is implemented on top of standard user-defined data. Pipe
  290. * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
  291. * use.
  292. */
  293. struct {
  294. u32 pipe_mode;
  295. unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
  296. } pipe;
  297. } u;
  298. u32 padding;
  299. } __packed;
  300. /* Server Flags */
  301. #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
  302. #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
  303. #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
  304. #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
  305. #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
  306. #define VMBUS_CHANNEL_PARENT_OFFER 0x200
  307. #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
  308. struct vmpacket_descriptor {
  309. u16 type;
  310. u16 offset8;
  311. u16 len8;
  312. u16 flags;
  313. u64 trans_id;
  314. } __packed;
  315. struct vmpacket_header {
  316. u32 prev_pkt_start_offset;
  317. struct vmpacket_descriptor descriptor;
  318. } __packed;
  319. struct vmtransfer_page_range {
  320. u32 byte_count;
  321. u32 byte_offset;
  322. } __packed;
  323. struct vmtransfer_page_packet_header {
  324. struct vmpacket_descriptor d;
  325. u16 xfer_pageset_id;
  326. bool sender_owns_set;
  327. u8 reserved;
  328. u32 range_cnt;
  329. struct vmtransfer_page_range ranges[1];
  330. } __packed;
  331. struct vmgpadl_packet_header {
  332. struct vmpacket_descriptor d;
  333. u32 gpadl;
  334. u32 reserved;
  335. } __packed;
  336. struct vmadd_remove_transfer_page_set {
  337. struct vmpacket_descriptor d;
  338. u32 gpadl;
  339. u16 xfer_pageset_id;
  340. u16 reserved;
  341. } __packed;
  342. /*
  343. * This structure defines a range in guest physical space that can be made to
  344. * look virtually contiguous.
  345. */
  346. struct gpa_range {
  347. u32 byte_count;
  348. u32 byte_offset;
  349. u64 pfn_array[0];
  350. };
  351. /*
  352. * This is the format for an Establish Gpadl packet, which contains a handle by
  353. * which this GPADL will be known and a set of GPA ranges associated with it.
  354. * This can be converted to a MDL by the guest OS. If there are multiple GPA
  355. * ranges, then the resulting MDL will be "chained," representing multiple VA
  356. * ranges.
  357. */
  358. struct vmestablish_gpadl {
  359. struct vmpacket_descriptor d;
  360. u32 gpadl;
  361. u32 range_cnt;
  362. struct gpa_range range[1];
  363. } __packed;
  364. /*
  365. * This is the format for a Teardown Gpadl packet, which indicates that the
  366. * GPADL handle in the Establish Gpadl packet will never be referenced again.
  367. */
  368. struct vmteardown_gpadl {
  369. struct vmpacket_descriptor d;
  370. u32 gpadl;
  371. u32 reserved; /* for alignment to a 8-byte boundary */
  372. } __packed;
  373. /*
  374. * This is the format for a GPA-Direct packet, which contains a set of GPA
  375. * ranges, in addition to commands and/or data.
  376. */
  377. struct vmdata_gpa_direct {
  378. struct vmpacket_descriptor d;
  379. u32 reserved;
  380. u32 range_cnt;
  381. struct gpa_range range[1];
  382. } __packed;
  383. /* This is the format for a Additional Data Packet. */
  384. struct vmadditional_data {
  385. struct vmpacket_descriptor d;
  386. u64 total_bytes;
  387. u32 offset;
  388. u32 byte_cnt;
  389. unsigned char data[1];
  390. } __packed;
  391. union vmpacket_largest_possible_header {
  392. struct vmpacket_descriptor simple_hdr;
  393. struct vmtransfer_page_packet_header xfer_page_hdr;
  394. struct vmgpadl_packet_header gpadl_hdr;
  395. struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
  396. struct vmestablish_gpadl establish_gpadl_hdr;
  397. struct vmteardown_gpadl teardown_gpadl_hdr;
  398. struct vmdata_gpa_direct data_gpa_direct_hdr;
  399. };
  400. #define VMPACKET_DATA_START_ADDRESS(__packet) \
  401. (void *)(((unsigned char *)__packet) + \
  402. ((struct vmpacket_descriptor)__packet)->offset8 * 8)
  403. #define VMPACKET_DATA_LENGTH(__packet) \
  404. ((((struct vmpacket_descriptor)__packet)->len8 - \
  405. ((struct vmpacket_descriptor)__packet)->offset8) * 8)
  406. #define VMPACKET_TRANSFER_MODE(__packet) \
  407. (((struct IMPACT)__packet)->type)
  408. enum vmbus_packet_type {
  409. VM_PKT_INVALID = 0x0,
  410. VM_PKT_SYNCH = 0x1,
  411. VM_PKT_ADD_XFER_PAGESET = 0x2,
  412. VM_PKT_RM_XFER_PAGESET = 0x3,
  413. VM_PKT_ESTABLISH_GPADL = 0x4,
  414. VM_PKT_TEARDOWN_GPADL = 0x5,
  415. VM_PKT_DATA_INBAND = 0x6,
  416. VM_PKT_DATA_USING_XFER_PAGES = 0x7,
  417. VM_PKT_DATA_USING_GPADL = 0x8,
  418. VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
  419. VM_PKT_CANCEL_REQUEST = 0xa,
  420. VM_PKT_COMP = 0xb,
  421. VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
  422. VM_PKT_ADDITIONAL_DATA = 0xd
  423. };
  424. #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
  425. /* Version 1 messages */
  426. enum vmbus_channel_message_type {
  427. CHANNELMSG_INVALID = 0,
  428. CHANNELMSG_OFFERCHANNEL = 1,
  429. CHANNELMSG_RESCIND_CHANNELOFFER = 2,
  430. CHANNELMSG_REQUESTOFFERS = 3,
  431. CHANNELMSG_ALLOFFERS_DELIVERED = 4,
  432. CHANNELMSG_OPENCHANNEL = 5,
  433. CHANNELMSG_OPENCHANNEL_RESULT = 6,
  434. CHANNELMSG_CLOSECHANNEL = 7,
  435. CHANNELMSG_GPADL_HEADER = 8,
  436. CHANNELMSG_GPADL_BODY = 9,
  437. CHANNELMSG_GPADL_CREATED = 10,
  438. CHANNELMSG_GPADL_TEARDOWN = 11,
  439. CHANNELMSG_GPADL_TORNDOWN = 12,
  440. CHANNELMSG_RELID_RELEASED = 13,
  441. CHANNELMSG_INITIATE_CONTACT = 14,
  442. CHANNELMSG_VERSION_RESPONSE = 15,
  443. CHANNELMSG_UNLOAD = 16,
  444. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  445. CHANNELMSG_VIEWRANGE_ADD = 17,
  446. CHANNELMSG_VIEWRANGE_REMOVE = 18,
  447. #endif
  448. CHANNELMSG_COUNT
  449. };
  450. struct vmbus_channel_message_header {
  451. enum vmbus_channel_message_type msgtype;
  452. u32 padding;
  453. } __packed;
  454. /* Query VMBus Version parameters */
  455. struct vmbus_channel_query_vmbus_version {
  456. struct vmbus_channel_message_header header;
  457. u32 version;
  458. } __packed;
  459. /* VMBus Version Supported parameters */
  460. struct vmbus_channel_version_supported {
  461. struct vmbus_channel_message_header header;
  462. bool version_supported;
  463. } __packed;
  464. /* Offer Channel parameters */
  465. struct vmbus_channel_offer_channel {
  466. struct vmbus_channel_message_header header;
  467. struct vmbus_channel_offer offer;
  468. u32 child_relid;
  469. u8 monitorid;
  470. bool monitor_allocated;
  471. } __packed;
  472. /* Rescind Offer parameters */
  473. struct vmbus_channel_rescind_offer {
  474. struct vmbus_channel_message_header header;
  475. u32 child_relid;
  476. } __packed;
  477. /*
  478. * Request Offer -- no parameters, SynIC message contains the partition ID
  479. * Set Snoop -- no parameters, SynIC message contains the partition ID
  480. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  481. * All Offers Delivered -- no parameters, SynIC message contains the partition
  482. * ID
  483. * Flush Client -- no parameters, SynIC message contains the partition ID
  484. */
  485. /* Open Channel parameters */
  486. struct vmbus_channel_open_channel {
  487. struct vmbus_channel_message_header header;
  488. /* Identifies the specific VMBus channel that is being opened. */
  489. u32 child_relid;
  490. /* ID making a particular open request at a channel offer unique. */
  491. u32 openid;
  492. /* GPADL for the channel's ring buffer. */
  493. u32 ringbuffer_gpadlhandle;
  494. /* GPADL for the channel's server context save area. */
  495. u32 server_contextarea_gpadlhandle;
  496. /*
  497. * The upstream ring buffer begins at offset zero in the memory
  498. * described by RingBufferGpadlHandle. The downstream ring buffer
  499. * follows it at this offset (in pages).
  500. */
  501. u32 downstream_ringbuffer_pageoffset;
  502. /* User-specific data to be passed along to the server endpoint. */
  503. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  504. } __packed;
  505. /* Open Channel Result parameters */
  506. struct vmbus_channel_open_result {
  507. struct vmbus_channel_message_header header;
  508. u32 child_relid;
  509. u32 openid;
  510. u32 status;
  511. } __packed;
  512. /* Close channel parameters; */
  513. struct vmbus_channel_close_channel {
  514. struct vmbus_channel_message_header header;
  515. u32 child_relid;
  516. } __packed;
  517. /* Channel Message GPADL */
  518. #define GPADL_TYPE_RING_BUFFER 1
  519. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  520. #define GPADL_TYPE_TRANSACTION 8
  521. /*
  522. * The number of PFNs in a GPADL message is defined by the number of
  523. * pages that would be spanned by ByteCount and ByteOffset. If the
  524. * implied number of PFNs won't fit in this packet, there will be a
  525. * follow-up packet that contains more.
  526. */
  527. struct vmbus_channel_gpadl_header {
  528. struct vmbus_channel_message_header header;
  529. u32 child_relid;
  530. u32 gpadl;
  531. u16 range_buflen;
  532. u16 rangecount;
  533. struct gpa_range range[0];
  534. } __packed;
  535. /* This is the followup packet that contains more PFNs. */
  536. struct vmbus_channel_gpadl_body {
  537. struct vmbus_channel_message_header header;
  538. u32 msgnumber;
  539. u32 gpadl;
  540. u64 pfn[0];
  541. } __packed;
  542. struct vmbus_channel_gpadl_created {
  543. struct vmbus_channel_message_header header;
  544. u32 child_relid;
  545. u32 gpadl;
  546. u32 creation_status;
  547. } __packed;
  548. struct vmbus_channel_gpadl_teardown {
  549. struct vmbus_channel_message_header header;
  550. u32 child_relid;
  551. u32 gpadl;
  552. } __packed;
  553. struct vmbus_channel_gpadl_torndown {
  554. struct vmbus_channel_message_header header;
  555. u32 gpadl;
  556. } __packed;
  557. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  558. struct vmbus_channel_view_range_add {
  559. struct vmbus_channel_message_header header;
  560. PHYSICAL_ADDRESS viewrange_base;
  561. u64 viewrange_length;
  562. u32 child_relid;
  563. } __packed;
  564. struct vmbus_channel_view_range_remove {
  565. struct vmbus_channel_message_header header;
  566. PHYSICAL_ADDRESS viewrange_base;
  567. u32 child_relid;
  568. } __packed;
  569. #endif
  570. struct vmbus_channel_relid_released {
  571. struct vmbus_channel_message_header header;
  572. u32 child_relid;
  573. } __packed;
  574. struct vmbus_channel_initiate_contact {
  575. struct vmbus_channel_message_header header;
  576. u32 vmbus_version_requested;
  577. u32 padding2;
  578. u64 interrupt_page;
  579. u64 monitor_page1;
  580. u64 monitor_page2;
  581. } __packed;
  582. struct vmbus_channel_version_response {
  583. struct vmbus_channel_message_header header;
  584. bool version_supported;
  585. } __packed;
  586. enum vmbus_channel_state {
  587. CHANNEL_OFFER_STATE,
  588. CHANNEL_OPENING_STATE,
  589. CHANNEL_OPEN_STATE,
  590. };
  591. struct vmbus_channel_debug_info {
  592. u32 relid;
  593. enum vmbus_channel_state state;
  594. uuid_le interfacetype;
  595. uuid_le interface_instance;
  596. u32 monitorid;
  597. u32 servermonitor_pending;
  598. u32 servermonitor_latency;
  599. u32 servermonitor_connectionid;
  600. u32 clientmonitor_pending;
  601. u32 clientmonitor_latency;
  602. u32 clientmonitor_connectionid;
  603. struct hv_ring_buffer_debug_info inbound;
  604. struct hv_ring_buffer_debug_info outbound;
  605. };
  606. /*
  607. * Represents each channel msg on the vmbus connection This is a
  608. * variable-size data structure depending on the msg type itself
  609. */
  610. struct vmbus_channel_msginfo {
  611. /* Bookkeeping stuff */
  612. struct list_head msglistentry;
  613. /* So far, this is only used to handle gpadl body message */
  614. struct list_head submsglist;
  615. /* Synchronize the request/response if needed */
  616. struct completion waitevent;
  617. union {
  618. struct vmbus_channel_version_supported version_supported;
  619. struct vmbus_channel_open_result open_result;
  620. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  621. struct vmbus_channel_gpadl_created gpadl_created;
  622. struct vmbus_channel_version_response version_response;
  623. } response;
  624. u32 msgsize;
  625. /*
  626. * The channel message that goes out on the "wire".
  627. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  628. */
  629. unsigned char msg[0];
  630. };
  631. struct vmbus_close_msg {
  632. struct vmbus_channel_msginfo info;
  633. struct vmbus_channel_close_channel msg;
  634. };
  635. struct vmbus_channel {
  636. struct list_head listentry;
  637. struct hv_device *device_obj;
  638. struct work_struct work;
  639. enum vmbus_channel_state state;
  640. struct vmbus_channel_offer_channel offermsg;
  641. /*
  642. * These are based on the OfferMsg.MonitorId.
  643. * Save it here for easy access.
  644. */
  645. u8 monitor_grp;
  646. u8 monitor_bit;
  647. u32 ringbuffer_gpadlhandle;
  648. /* Allocated memory for ring buffer */
  649. void *ringbuffer_pages;
  650. u32 ringbuffer_pagecount;
  651. struct hv_ring_buffer_info outbound; /* send to parent */
  652. struct hv_ring_buffer_info inbound; /* receive from parent */
  653. spinlock_t inbound_lock;
  654. struct workqueue_struct *controlwq;
  655. struct vmbus_close_msg close_msg;
  656. /* Channel callback are invoked in this workqueue context */
  657. /* HANDLE dataWorkQueue; */
  658. void (*onchannel_callback)(void *context);
  659. void *channel_callback_context;
  660. };
  661. void vmbus_onmessage(void *context);
  662. int vmbus_request_offers(void);
  663. /* The format must be the same as struct vmdata_gpa_direct */
  664. struct vmbus_channel_packet_page_buffer {
  665. u16 type;
  666. u16 dataoffset8;
  667. u16 length8;
  668. u16 flags;
  669. u64 transactionid;
  670. u32 reserved;
  671. u32 rangecount;
  672. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  673. } __packed;
  674. /* The format must be the same as struct vmdata_gpa_direct */
  675. struct vmbus_channel_packet_multipage_buffer {
  676. u16 type;
  677. u16 dataoffset8;
  678. u16 length8;
  679. u16 flags;
  680. u64 transactionid;
  681. u32 reserved;
  682. u32 rangecount; /* Always 1 in this case */
  683. struct hv_multipage_buffer range;
  684. } __packed;
  685. extern int vmbus_open(struct vmbus_channel *channel,
  686. u32 send_ringbuffersize,
  687. u32 recv_ringbuffersize,
  688. void *userdata,
  689. u32 userdatalen,
  690. void(*onchannel_callback)(void *context),
  691. void *context);
  692. extern void vmbus_close(struct vmbus_channel *channel);
  693. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  694. const void *buffer,
  695. u32 bufferLen,
  696. u64 requestid,
  697. enum vmbus_packet_type type,
  698. u32 flags);
  699. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  700. struct hv_page_buffer pagebuffers[],
  701. u32 pagecount,
  702. void *buffer,
  703. u32 bufferlen,
  704. u64 requestid);
  705. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  706. struct hv_multipage_buffer *mpb,
  707. void *buffer,
  708. u32 bufferlen,
  709. u64 requestid);
  710. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  711. void *kbuffer,
  712. u32 size,
  713. u32 *gpadl_handle);
  714. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  715. u32 gpadl_handle);
  716. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  717. void *buffer,
  718. u32 bufferlen,
  719. u32 *buffer_actual_len,
  720. u64 *requestid);
  721. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  722. void *buffer,
  723. u32 bufferlen,
  724. u32 *buffer_actual_len,
  725. u64 *requestid);
  726. extern void vmbus_get_debug_info(struct vmbus_channel *channel,
  727. struct vmbus_channel_debug_info *debug);
  728. extern void vmbus_ontimer(unsigned long data);
  729. struct hv_dev_port_info {
  730. u32 int_mask;
  731. u32 read_idx;
  732. u32 write_idx;
  733. u32 bytes_avail_toread;
  734. u32 bytes_avail_towrite;
  735. };
  736. /* Base driver object */
  737. struct hv_driver {
  738. const char *name;
  739. /* the device type supported by this driver */
  740. uuid_le dev_type;
  741. const struct hv_vmbus_device_id *id_table;
  742. struct device_driver driver;
  743. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  744. int (*remove)(struct hv_device *);
  745. void (*shutdown)(struct hv_device *);
  746. };
  747. /* Base device object */
  748. struct hv_device {
  749. /* the device type id of this device */
  750. uuid_le dev_type;
  751. /* the device instance id of this device */
  752. uuid_le dev_instance;
  753. struct device device;
  754. struct vmbus_channel *channel;
  755. };
  756. static inline struct hv_device *device_to_hv_device(struct device *d)
  757. {
  758. return container_of(d, struct hv_device, device);
  759. }
  760. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  761. {
  762. return container_of(d, struct hv_driver, driver);
  763. }
  764. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  765. {
  766. dev_set_drvdata(&dev->device, data);
  767. }
  768. static inline void *hv_get_drvdata(struct hv_device *dev)
  769. {
  770. return dev_get_drvdata(&dev->device);
  771. }
  772. /* Vmbus interface */
  773. #define vmbus_driver_register(driver) \
  774. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  775. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  776. struct module *owner,
  777. const char *mod_name);
  778. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  779. /**
  780. * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
  781. *
  782. * This macro is used to create a struct hv_vmbus_device_id that matches a
  783. * specific device.
  784. */
  785. #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
  786. g8, g9, ga, gb, gc, gd, ge, gf) \
  787. .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
  788. g8, g9, ga, gb, gc, gd, ge, gf },
  789. /*
  790. * Common header for Hyper-V ICs
  791. */
  792. #define ICMSGTYPE_NEGOTIATE 0
  793. #define ICMSGTYPE_HEARTBEAT 1
  794. #define ICMSGTYPE_KVPEXCHANGE 2
  795. #define ICMSGTYPE_SHUTDOWN 3
  796. #define ICMSGTYPE_TIMESYNC 4
  797. #define ICMSGTYPE_VSS 5
  798. #define ICMSGHDRFLAG_TRANSACTION 1
  799. #define ICMSGHDRFLAG_REQUEST 2
  800. #define ICMSGHDRFLAG_RESPONSE 4
  801. #define HV_S_OK 0x00000000
  802. #define HV_E_FAIL 0x80004005
  803. #define HV_S_CONT 0x80070103
  804. #define HV_ERROR_NOT_SUPPORTED 0x80070032
  805. #define HV_ERROR_MACHINE_LOCKED 0x800704F7
  806. /*
  807. * While we want to handle util services as regular devices,
  808. * there is only one instance of each of these services; so
  809. * we statically allocate the service specific state.
  810. */
  811. struct hv_util_service {
  812. u8 *recv_buffer;
  813. void (*util_cb)(void *);
  814. int (*util_init)(struct hv_util_service *);
  815. void (*util_deinit)(void);
  816. };
  817. struct vmbuspipe_hdr {
  818. u32 flags;
  819. u32 msgsize;
  820. } __packed;
  821. struct ic_version {
  822. u16 major;
  823. u16 minor;
  824. } __packed;
  825. struct icmsg_hdr {
  826. struct ic_version icverframe;
  827. u16 icmsgtype;
  828. struct ic_version icvermsg;
  829. u16 icmsgsize;
  830. u32 status;
  831. u8 ictransaction_id;
  832. u8 icflags;
  833. u8 reserved[2];
  834. } __packed;
  835. struct icmsg_negotiate {
  836. u16 icframe_vercnt;
  837. u16 icmsg_vercnt;
  838. u32 reserved;
  839. struct ic_version icversion_data[1]; /* any size array */
  840. } __packed;
  841. struct shutdown_msg_data {
  842. u32 reason_code;
  843. u32 timeout_seconds;
  844. u32 flags;
  845. u8 display_message[2048];
  846. } __packed;
  847. struct heartbeat_msg_data {
  848. u64 seq_num;
  849. u32 reserved[8];
  850. } __packed;
  851. /* Time Sync IC defs */
  852. #define ICTIMESYNCFLAG_PROBE 0
  853. #define ICTIMESYNCFLAG_SYNC 1
  854. #define ICTIMESYNCFLAG_SAMPLE 2
  855. #ifdef __x86_64__
  856. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  857. #else
  858. #define WLTIMEDELTA 116444736000000000LL
  859. #endif
  860. struct ictimesync_data {
  861. u64 parenttime;
  862. u64 childtime;
  863. u64 roundtriptime;
  864. u8 flags;
  865. } __packed;
  866. struct hyperv_service_callback {
  867. u8 msg_type;
  868. char *log_msg;
  869. uuid_le data;
  870. struct vmbus_channel *channel;
  871. void (*callback) (void *context);
  872. };
  873. extern void vmbus_prep_negotiate_resp(struct icmsg_hdr *,
  874. struct icmsg_negotiate *, u8 *);
  875. int hv_kvp_init(struct hv_util_service *);
  876. void hv_kvp_deinit(void);
  877. void hv_kvp_onchannelcallback(void *);
  878. #endif /* __KERNEL__ */
  879. #endif /* _HYPERV_H */