hyperv.h 29 KB

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