hyperv.h 30 KB

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