hyperv.h 27 KB

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