hyperv.h 29 KB

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