hyperv.h 29 KB

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