hyperv.h 26 KB

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