hyperv.h 31 KB

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