hyperv.h 26 KB

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