hyperv.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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. * A channel can be marked for efficient (batched)
  754. * reading:
  755. * If batched_reading is set to "true", we read until the
  756. * channel is empty and hold off interrupts from the host
  757. * during the entire read process.
  758. * If batched_reading is set to "false", the client is not
  759. * going to perform batched reading.
  760. *
  761. * By default we will enable batched reading; specific
  762. * drivers that don't want this behavior can turn it off.
  763. */
  764. bool batched_reading;
  765. };
  766. static inline void set_channel_read_state(struct vmbus_channel *c, bool state)
  767. {
  768. c->batched_reading = state;
  769. }
  770. void vmbus_onmessage(void *context);
  771. int vmbus_request_offers(void);
  772. /* The format must be the same as struct vmdata_gpa_direct */
  773. struct vmbus_channel_packet_page_buffer {
  774. u16 type;
  775. u16 dataoffset8;
  776. u16 length8;
  777. u16 flags;
  778. u64 transactionid;
  779. u32 reserved;
  780. u32 rangecount;
  781. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  782. } __packed;
  783. /* The format must be the same as struct vmdata_gpa_direct */
  784. struct vmbus_channel_packet_multipage_buffer {
  785. u16 type;
  786. u16 dataoffset8;
  787. u16 length8;
  788. u16 flags;
  789. u64 transactionid;
  790. u32 reserved;
  791. u32 rangecount; /* Always 1 in this case */
  792. struct hv_multipage_buffer range;
  793. } __packed;
  794. extern int vmbus_open(struct vmbus_channel *channel,
  795. u32 send_ringbuffersize,
  796. u32 recv_ringbuffersize,
  797. void *userdata,
  798. u32 userdatalen,
  799. void(*onchannel_callback)(void *context),
  800. void *context);
  801. extern void vmbus_close(struct vmbus_channel *channel);
  802. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  803. const void *buffer,
  804. u32 bufferLen,
  805. u64 requestid,
  806. enum vmbus_packet_type type,
  807. u32 flags);
  808. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  809. struct hv_page_buffer pagebuffers[],
  810. u32 pagecount,
  811. void *buffer,
  812. u32 bufferlen,
  813. u64 requestid);
  814. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  815. struct hv_multipage_buffer *mpb,
  816. void *buffer,
  817. u32 bufferlen,
  818. u64 requestid);
  819. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  820. void *kbuffer,
  821. u32 size,
  822. u32 *gpadl_handle);
  823. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  824. u32 gpadl_handle);
  825. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  826. void *buffer,
  827. u32 bufferlen,
  828. u32 *buffer_actual_len,
  829. u64 *requestid);
  830. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  831. void *buffer,
  832. u32 bufferlen,
  833. u32 *buffer_actual_len,
  834. u64 *requestid);
  835. extern void vmbus_get_debug_info(struct vmbus_channel *channel,
  836. struct vmbus_channel_debug_info *debug);
  837. extern void vmbus_ontimer(unsigned long data);
  838. struct hv_dev_port_info {
  839. u32 int_mask;
  840. u32 read_idx;
  841. u32 write_idx;
  842. u32 bytes_avail_toread;
  843. u32 bytes_avail_towrite;
  844. };
  845. /* Base driver object */
  846. struct hv_driver {
  847. const char *name;
  848. /* the device type supported by this driver */
  849. uuid_le dev_type;
  850. const struct hv_vmbus_device_id *id_table;
  851. struct device_driver driver;
  852. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  853. int (*remove)(struct hv_device *);
  854. void (*shutdown)(struct hv_device *);
  855. };
  856. /* Base device object */
  857. struct hv_device {
  858. /* the device type id of this device */
  859. uuid_le dev_type;
  860. /* the device instance id of this device */
  861. uuid_le dev_instance;
  862. struct device device;
  863. struct vmbus_channel *channel;
  864. };
  865. static inline struct hv_device *device_to_hv_device(struct device *d)
  866. {
  867. return container_of(d, struct hv_device, device);
  868. }
  869. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  870. {
  871. return container_of(d, struct hv_driver, driver);
  872. }
  873. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  874. {
  875. dev_set_drvdata(&dev->device, data);
  876. }
  877. static inline void *hv_get_drvdata(struct hv_device *dev)
  878. {
  879. return dev_get_drvdata(&dev->device);
  880. }
  881. /* Vmbus interface */
  882. #define vmbus_driver_register(driver) \
  883. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  884. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  885. struct module *owner,
  886. const char *mod_name);
  887. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  888. /**
  889. * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
  890. *
  891. * This macro is used to create a struct hv_vmbus_device_id that matches a
  892. * specific device.
  893. */
  894. #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
  895. g8, g9, ga, gb, gc, gd, ge, gf) \
  896. .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
  897. g8, g9, ga, gb, gc, gd, ge, gf },
  898. /*
  899. * Common header for Hyper-V ICs
  900. */
  901. #define ICMSGTYPE_NEGOTIATE 0
  902. #define ICMSGTYPE_HEARTBEAT 1
  903. #define ICMSGTYPE_KVPEXCHANGE 2
  904. #define ICMSGTYPE_SHUTDOWN 3
  905. #define ICMSGTYPE_TIMESYNC 4
  906. #define ICMSGTYPE_VSS 5
  907. #define ICMSGHDRFLAG_TRANSACTION 1
  908. #define ICMSGHDRFLAG_REQUEST 2
  909. #define ICMSGHDRFLAG_RESPONSE 4
  910. /*
  911. * While we want to handle util services as regular devices,
  912. * there is only one instance of each of these services; so
  913. * we statically allocate the service specific state.
  914. */
  915. struct hv_util_service {
  916. u8 *recv_buffer;
  917. void (*util_cb)(void *);
  918. int (*util_init)(struct hv_util_service *);
  919. void (*util_deinit)(void);
  920. };
  921. struct vmbuspipe_hdr {
  922. u32 flags;
  923. u32 msgsize;
  924. } __packed;
  925. struct ic_version {
  926. u16 major;
  927. u16 minor;
  928. } __packed;
  929. struct icmsg_hdr {
  930. struct ic_version icverframe;
  931. u16 icmsgtype;
  932. struct ic_version icvermsg;
  933. u16 icmsgsize;
  934. u32 status;
  935. u8 ictransaction_id;
  936. u8 icflags;
  937. u8 reserved[2];
  938. } __packed;
  939. struct icmsg_negotiate {
  940. u16 icframe_vercnt;
  941. u16 icmsg_vercnt;
  942. u32 reserved;
  943. struct ic_version icversion_data[1]; /* any size array */
  944. } __packed;
  945. struct shutdown_msg_data {
  946. u32 reason_code;
  947. u32 timeout_seconds;
  948. u32 flags;
  949. u8 display_message[2048];
  950. } __packed;
  951. struct heartbeat_msg_data {
  952. u64 seq_num;
  953. u32 reserved[8];
  954. } __packed;
  955. /* Time Sync IC defs */
  956. #define ICTIMESYNCFLAG_PROBE 0
  957. #define ICTIMESYNCFLAG_SYNC 1
  958. #define ICTIMESYNCFLAG_SAMPLE 2
  959. #ifdef __x86_64__
  960. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  961. #else
  962. #define WLTIMEDELTA 116444736000000000LL
  963. #endif
  964. struct ictimesync_data {
  965. u64 parenttime;
  966. u64 childtime;
  967. u64 roundtriptime;
  968. u8 flags;
  969. } __packed;
  970. struct hyperv_service_callback {
  971. u8 msg_type;
  972. char *log_msg;
  973. uuid_le data;
  974. struct vmbus_channel *channel;
  975. void (*callback) (void *context);
  976. };
  977. #define MAX_SRV_VER 0x7ffffff
  978. extern void vmbus_prep_negotiate_resp(struct icmsg_hdr *,
  979. struct icmsg_negotiate *, u8 *, int,
  980. int);
  981. int hv_kvp_init(struct hv_util_service *);
  982. void hv_kvp_deinit(void);
  983. void hv_kvp_onchannelcallback(void *);
  984. #endif /* __KERNEL__ */
  985. #endif /* _HYPERV_H */