hyperv.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. *
  3. * Copyright (c) 2011, Microsoft Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. *
  18. * Authors:
  19. * Haiyang Zhang <haiyangz@microsoft.com>
  20. * Hank Janssen <hjanssen@microsoft.com>
  21. * K. Y. Srinivasan <kys@microsoft.com>
  22. *
  23. */
  24. #ifndef _HYPERV_H
  25. #define _HYPERV_H
  26. #include <linux/types.h>
  27. /*
  28. * An implementation of HyperV key value pair (KVP) functionality for Linux.
  29. *
  30. *
  31. * Copyright (C) 2010, Novell, Inc.
  32. * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
  33. *
  34. */
  35. /*
  36. * Maximum value size - used for both key names and value data, and includes
  37. * any applicable NULL terminators.
  38. *
  39. * Note: This limit is somewhat arbitrary, but falls easily within what is
  40. * supported for all native guests (back to Win 2000) and what is reasonable
  41. * for the IC KVP exchange functionality. Note that Windows Me/98/95 are
  42. * limited to 255 character key names.
  43. *
  44. * MSDN recommends not storing data values larger than 2048 bytes in the
  45. * registry.
  46. *
  47. * Note: This value is used in defining the KVP exchange message - this value
  48. * cannot be modified without affecting the message size and compatibility.
  49. */
  50. /*
  51. * bytes, including any null terminators
  52. */
  53. #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE (2048)
  54. /*
  55. * Maximum key size - the registry limit for the length of an entry name
  56. * is 256 characters, including the null terminator
  57. */
  58. #define HV_KVP_EXCHANGE_MAX_KEY_SIZE (512)
  59. /*
  60. * In Linux, we implement the KVP functionality in two components:
  61. * 1) The kernel component which is packaged as part of the hv_utils driver
  62. * is responsible for communicating with the host and responsible for
  63. * implementing the host/guest protocol. 2) A user level daemon that is
  64. * responsible for data gathering.
  65. *
  66. * Host/Guest Protocol: The host iterates over an index and expects the guest
  67. * to assign a key name to the index and also return the value corresponding to
  68. * the key. The host will have atmost one KVP transaction outstanding at any
  69. * given point in time. The host side iteration stops when the guest returns
  70. * an error. Microsoft has specified the following mapping of key names to
  71. * host specified index:
  72. *
  73. * Index Key Name
  74. * 0 FullyQualifiedDomainName
  75. * 1 IntegrationServicesVersion
  76. * 2 NetworkAddressIPv4
  77. * 3 NetworkAddressIPv6
  78. * 4 OSBuildNumber
  79. * 5 OSName
  80. * 6 OSMajorVersion
  81. * 7 OSMinorVersion
  82. * 8 OSVersion
  83. * 9 ProcessorArchitecture
  84. *
  85. * The Windows host expects the Key Name and Key Value to be encoded in utf16.
  86. *
  87. * Guest Kernel/KVP Daemon Protocol: As noted earlier, we implement all of the
  88. * data gathering functionality in a user mode daemon. The user level daemon
  89. * is also responsible for binding the key name to the index as well. The
  90. * kernel and user-level daemon communicate using a connector channel.
  91. *
  92. * The user mode component first registers with the
  93. * the kernel component. Subsequently, the kernel component requests, data
  94. * for the specified keys. In response to this message the user mode component
  95. * fills in the value corresponding to the specified key. We overload the
  96. * sequence field in the cn_msg header to define our KVP message types.
  97. *
  98. *
  99. * The kernel component simply acts as a conduit for communication between the
  100. * Windows host and the user-level daemon. The kernel component passes up the
  101. * index received from the Host to the user-level daemon. If the index is
  102. * valid (supported), the corresponding key as well as its
  103. * value (both are strings) is returned. If the index is invalid
  104. * (not supported), a NULL key string is returned.
  105. */
  106. /*
  107. * Registry value types.
  108. */
  109. #define REG_SZ 1
  110. #define REG_U32 4
  111. #define REG_U64 8
  112. /*
  113. * As we look at expanding the KVP functionality to include
  114. * IP injection functionality, we need to maintain binary
  115. * compatibility with older daemons.
  116. *
  117. * The KVP opcodes are defined by the host and it was unfortunate
  118. * that I chose to treat the registration operation as part of the
  119. * KVP operations defined by the host.
  120. * Here is the level of compatibility
  121. * (between the user level daemon and the kernel KVP driver) that we
  122. * will implement:
  123. *
  124. * An older daemon will always be supported on a newer driver.
  125. * A given user level daemon will require a minimal version of the
  126. * kernel driver.
  127. * If we cannot handle the version differences, we will fail gracefully
  128. * (this can happen when we have a user level daemon that is more
  129. * advanced than the KVP driver.
  130. *
  131. * We will use values used in this handshake for determining if we have
  132. * workable user level daemon and the kernel driver. We begin by taking the
  133. * registration opcode out of the KVP opcode namespace. We will however,
  134. * maintain compatibility with the existing user-level daemon code.
  135. */
  136. /*
  137. * Daemon code not supporting IP injection (legacy daemon).
  138. */
  139. #define KVP_OP_REGISTER 4
  140. /*
  141. * Daemon code supporting IP injection.
  142. * The KVP opcode field is used to communicate the
  143. * registration information; so define a namespace that
  144. * will be distinct from the host defined KVP opcode.
  145. */
  146. #define KVP_OP_REGISTER1 100
  147. enum hv_kvp_exchg_op {
  148. KVP_OP_GET = 0,
  149. KVP_OP_SET,
  150. KVP_OP_DELETE,
  151. KVP_OP_ENUMERATE,
  152. KVP_OP_GET_IP_INFO,
  153. KVP_OP_SET_IP_INFO,
  154. KVP_OP_COUNT /* Number of operations, must be last. */
  155. };
  156. enum hv_kvp_exchg_pool {
  157. KVP_POOL_EXTERNAL = 0,
  158. KVP_POOL_GUEST,
  159. KVP_POOL_AUTO,
  160. KVP_POOL_AUTO_EXTERNAL,
  161. KVP_POOL_AUTO_INTERNAL,
  162. KVP_POOL_COUNT /* Number of pools, must be last. */
  163. };
  164. /*
  165. * Some Hyper-V status codes.
  166. */
  167. #define HV_S_OK 0x00000000
  168. #define HV_E_FAIL 0x80004005
  169. #define HV_S_CONT 0x80070103
  170. #define HV_ERROR_NOT_SUPPORTED 0x80070032
  171. #define HV_ERROR_MACHINE_LOCKED 0x800704F7
  172. #define HV_ERROR_DEVICE_NOT_CONNECTED 0x8007048F
  173. #define HV_INVALIDARG 0x80070057
  174. #define HV_GUID_NOTFOUND 0x80041002
  175. #define ADDR_FAMILY_NONE 0x00
  176. #define ADDR_FAMILY_IPV4 0x01
  177. #define ADDR_FAMILY_IPV6 0x02
  178. #define MAX_ADAPTER_ID_SIZE 128
  179. #define MAX_IP_ADDR_SIZE 1024
  180. #define MAX_GATEWAY_SIZE 512
  181. struct hv_kvp_ipaddr_value {
  182. __u16 adapter_id[MAX_ADAPTER_ID_SIZE];
  183. __u8 addr_family;
  184. __u8 dhcp_enabled;
  185. __u16 ip_addr[MAX_IP_ADDR_SIZE];
  186. __u16 sub_net[MAX_IP_ADDR_SIZE];
  187. __u16 gate_way[MAX_GATEWAY_SIZE];
  188. __u16 dns_addr[MAX_IP_ADDR_SIZE];
  189. } __attribute__((packed));
  190. struct hv_kvp_hdr {
  191. __u8 operation;
  192. __u8 pool;
  193. __u16 pad;
  194. } __attribute__((packed));
  195. struct hv_kvp_exchg_msg_value {
  196. __u32 value_type;
  197. __u32 key_size;
  198. __u32 value_size;
  199. __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  200. union {
  201. __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
  202. __u32 value_u32;
  203. __u64 value_u64;
  204. };
  205. } __attribute__((packed));
  206. struct hv_kvp_msg_enumerate {
  207. __u32 index;
  208. struct hv_kvp_exchg_msg_value data;
  209. } __attribute__((packed));
  210. struct hv_kvp_msg_get {
  211. struct hv_kvp_exchg_msg_value data;
  212. };
  213. struct hv_kvp_msg_set {
  214. struct hv_kvp_exchg_msg_value data;
  215. };
  216. struct hv_kvp_msg_delete {
  217. __u32 key_size;
  218. __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  219. };
  220. struct hv_kvp_register {
  221. __u8 version[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  222. };
  223. struct hv_kvp_msg {
  224. union {
  225. struct hv_kvp_hdr kvp_hdr;
  226. int error;
  227. };
  228. union {
  229. struct hv_kvp_msg_get kvp_get;
  230. struct hv_kvp_msg_set kvp_set;
  231. struct hv_kvp_msg_delete kvp_delete;
  232. struct hv_kvp_msg_enumerate kvp_enum_data;
  233. struct hv_kvp_ipaddr_value kvp_ip_val;
  234. struct hv_kvp_register kvp_register;
  235. } body;
  236. } __attribute__((packed));
  237. struct hv_kvp_ip_msg {
  238. __u8 operation;
  239. __u8 pool;
  240. struct hv_kvp_ipaddr_value kvp_ip_val;
  241. } __attribute__((packed));
  242. #ifdef __KERNEL__
  243. #include <linux/scatterlist.h>
  244. #include <linux/list.h>
  245. #include <linux/uuid.h>
  246. #include <linux/timer.h>
  247. #include <linux/workqueue.h>
  248. #include <linux/completion.h>
  249. #include <linux/device.h>
  250. #include <linux/mod_devicetable.h>
  251. #define MAX_PAGE_BUFFER_COUNT 19
  252. #define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
  253. #pragma pack(push, 1)
  254. /* Single-page buffer */
  255. struct hv_page_buffer {
  256. u32 len;
  257. u32 offset;
  258. u64 pfn;
  259. };
  260. /* Multiple-page buffer */
  261. struct hv_multipage_buffer {
  262. /* Length and Offset determines the # of pfns in the array */
  263. u32 len;
  264. u32 offset;
  265. u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
  266. };
  267. /* 0x18 includes the proprietary packet header */
  268. #define MAX_PAGE_BUFFER_PACKET (0x18 + \
  269. (sizeof(struct hv_page_buffer) * \
  270. MAX_PAGE_BUFFER_COUNT))
  271. #define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
  272. sizeof(struct hv_multipage_buffer))
  273. #pragma pack(pop)
  274. struct hv_ring_buffer {
  275. /* Offset in bytes from the start of ring data below */
  276. u32 write_index;
  277. /* Offset in bytes from the start of ring data below */
  278. u32 read_index;
  279. u32 interrupt_mask;
  280. /*
  281. * Win8 uses some of the reserved bits to implement
  282. * interrupt driven flow management. On the send side
  283. * we can request that the receiver interrupt the sender
  284. * when the ring transitions from being full to being able
  285. * to handle a message of size "pending_send_sz".
  286. *
  287. * Add necessary state for this enhancement.
  288. */
  289. u32 pending_send_sz;
  290. u32 reserved1[12];
  291. union {
  292. struct {
  293. u32 feat_pending_send_sz:1;
  294. };
  295. u32 value;
  296. } feature_bits;
  297. /* Pad it to PAGE_SIZE so that data starts on page boundary */
  298. u8 reserved2[4028];
  299. /*
  300. * Ring data starts here + RingDataStartOffset
  301. * !!! DO NOT place any fields below this !!!
  302. */
  303. u8 buffer[0];
  304. } __packed;
  305. struct hv_ring_buffer_info {
  306. struct hv_ring_buffer *ring_buffer;
  307. u32 ring_size; /* Include the shared header */
  308. spinlock_t ring_lock;
  309. u32 ring_datasize; /* < ring_size */
  310. u32 ring_data_startoffset;
  311. };
  312. struct hv_ring_buffer_debug_info {
  313. u32 current_interrupt_mask;
  314. u32 current_read_index;
  315. u32 current_write_index;
  316. u32 bytes_avail_toread;
  317. u32 bytes_avail_towrite;
  318. };
  319. /*
  320. *
  321. * hv_get_ringbuffer_availbytes()
  322. *
  323. * Get number of bytes available to read and to write to
  324. * for the specified ring buffer
  325. */
  326. static inline void
  327. hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
  328. u32 *read, u32 *write)
  329. {
  330. u32 read_loc, write_loc, dsize;
  331. smp_read_barrier_depends();
  332. /* Capture the read/write indices before they changed */
  333. read_loc = rbi->ring_buffer->read_index;
  334. write_loc = rbi->ring_buffer->write_index;
  335. dsize = rbi->ring_datasize;
  336. *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  337. read_loc - write_loc;
  338. *read = dsize - *write;
  339. }
  340. /*
  341. * We use the same version numbering for all Hyper-V modules.
  342. *
  343. * Definition of versioning is as follows;
  344. *
  345. * Major Number Changes for these scenarios;
  346. * 1. When a new version of Windows Hyper-V
  347. * is released.
  348. * 2. A Major change has occurred in the
  349. * Linux IC's.
  350. * (For example the merge for the first time
  351. * into the kernel) Every time the Major Number
  352. * changes, the Revision number is reset to 0.
  353. * Minor Number Changes when new functionality is added
  354. * to the Linux IC's that is not a bug fix.
  355. *
  356. * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
  357. */
  358. #define HV_DRV_VERSION "3.1"
  359. /*
  360. * VMBUS version is 32 bit entity broken up into
  361. * two 16 bit quantities: major_number. minor_number.
  362. *
  363. * 0 . 13 (Windows Server 2008)
  364. * 1 . 1 (Windows 7)
  365. * 2 . 4 (Windows 8)
  366. */
  367. #define VERSION_WS2008 ((0 << 16) | (13))
  368. #define VERSION_WIN7 ((1 << 16) | (1))
  369. #define VERSION_WIN8 ((2 << 16) | (4))
  370. #define VERSION_INVAL -1
  371. /* Make maximum size of pipe payload of 16K */
  372. #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
  373. /* Define PipeMode values. */
  374. #define VMBUS_PIPE_TYPE_BYTE 0x00000000
  375. #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
  376. /* The size of the user defined data buffer for non-pipe offers. */
  377. #define MAX_USER_DEFINED_BYTES 120
  378. /* The size of the user defined data buffer for pipe offers. */
  379. #define MAX_PIPE_USER_DEFINED_BYTES 116
  380. /*
  381. * At the center of the Channel Management library is the Channel Offer. This
  382. * struct contains the fundamental information about an offer.
  383. */
  384. struct vmbus_channel_offer {
  385. uuid_le if_type;
  386. uuid_le if_instance;
  387. /*
  388. * These two fields are not currently used.
  389. */
  390. u64 reserved1;
  391. u64 reserved2;
  392. u16 chn_flags;
  393. u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
  394. union {
  395. /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
  396. struct {
  397. unsigned char user_def[MAX_USER_DEFINED_BYTES];
  398. } std;
  399. /*
  400. * Pipes:
  401. * The following sructure is an integrated pipe protocol, which
  402. * is implemented on top of standard user-defined data. Pipe
  403. * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
  404. * use.
  405. */
  406. struct {
  407. u32 pipe_mode;
  408. unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
  409. } pipe;
  410. } u;
  411. /*
  412. * The sub_channel_index is defined in win8.
  413. */
  414. u16 sub_channel_index;
  415. u16 reserved3;
  416. } __packed;
  417. /* Server Flags */
  418. #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
  419. #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
  420. #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
  421. #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
  422. #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
  423. #define VMBUS_CHANNEL_PARENT_OFFER 0x200
  424. #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
  425. struct vmpacket_descriptor {
  426. u16 type;
  427. u16 offset8;
  428. u16 len8;
  429. u16 flags;
  430. u64 trans_id;
  431. } __packed;
  432. struct vmpacket_header {
  433. u32 prev_pkt_start_offset;
  434. struct vmpacket_descriptor descriptor;
  435. } __packed;
  436. struct vmtransfer_page_range {
  437. u32 byte_count;
  438. u32 byte_offset;
  439. } __packed;
  440. struct vmtransfer_page_packet_header {
  441. struct vmpacket_descriptor d;
  442. u16 xfer_pageset_id;
  443. u8 sender_owns_set;
  444. u8 reserved;
  445. u32 range_cnt;
  446. struct vmtransfer_page_range ranges[1];
  447. } __packed;
  448. struct vmgpadl_packet_header {
  449. struct vmpacket_descriptor d;
  450. u32 gpadl;
  451. u32 reserved;
  452. } __packed;
  453. struct vmadd_remove_transfer_page_set {
  454. struct vmpacket_descriptor d;
  455. u32 gpadl;
  456. u16 xfer_pageset_id;
  457. u16 reserved;
  458. } __packed;
  459. /*
  460. * This structure defines a range in guest physical space that can be made to
  461. * look virtually contiguous.
  462. */
  463. struct gpa_range {
  464. u32 byte_count;
  465. u32 byte_offset;
  466. u64 pfn_array[0];
  467. };
  468. /*
  469. * This is the format for an Establish Gpadl packet, which contains a handle by
  470. * which this GPADL will be known and a set of GPA ranges associated with it.
  471. * This can be converted to a MDL by the guest OS. If there are multiple GPA
  472. * ranges, then the resulting MDL will be "chained," representing multiple VA
  473. * ranges.
  474. */
  475. struct vmestablish_gpadl {
  476. struct vmpacket_descriptor d;
  477. u32 gpadl;
  478. u32 range_cnt;
  479. struct gpa_range range[1];
  480. } __packed;
  481. /*
  482. * This is the format for a Teardown Gpadl packet, which indicates that the
  483. * GPADL handle in the Establish Gpadl packet will never be referenced again.
  484. */
  485. struct vmteardown_gpadl {
  486. struct vmpacket_descriptor d;
  487. u32 gpadl;
  488. u32 reserved; /* for alignment to a 8-byte boundary */
  489. } __packed;
  490. /*
  491. * This is the format for a GPA-Direct packet, which contains a set of GPA
  492. * ranges, in addition to commands and/or data.
  493. */
  494. struct vmdata_gpa_direct {
  495. struct vmpacket_descriptor d;
  496. u32 reserved;
  497. u32 range_cnt;
  498. struct gpa_range range[1];
  499. } __packed;
  500. /* This is the format for a Additional Data Packet. */
  501. struct vmadditional_data {
  502. struct vmpacket_descriptor d;
  503. u64 total_bytes;
  504. u32 offset;
  505. u32 byte_cnt;
  506. unsigned char data[1];
  507. } __packed;
  508. union vmpacket_largest_possible_header {
  509. struct vmpacket_descriptor simple_hdr;
  510. struct vmtransfer_page_packet_header xfer_page_hdr;
  511. struct vmgpadl_packet_header gpadl_hdr;
  512. struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
  513. struct vmestablish_gpadl establish_gpadl_hdr;
  514. struct vmteardown_gpadl teardown_gpadl_hdr;
  515. struct vmdata_gpa_direct data_gpa_direct_hdr;
  516. };
  517. #define VMPACKET_DATA_START_ADDRESS(__packet) \
  518. (void *)(((unsigned char *)__packet) + \
  519. ((struct vmpacket_descriptor)__packet)->offset8 * 8)
  520. #define VMPACKET_DATA_LENGTH(__packet) \
  521. ((((struct vmpacket_descriptor)__packet)->len8 - \
  522. ((struct vmpacket_descriptor)__packet)->offset8) * 8)
  523. #define VMPACKET_TRANSFER_MODE(__packet) \
  524. (((struct IMPACT)__packet)->type)
  525. enum vmbus_packet_type {
  526. VM_PKT_INVALID = 0x0,
  527. VM_PKT_SYNCH = 0x1,
  528. VM_PKT_ADD_XFER_PAGESET = 0x2,
  529. VM_PKT_RM_XFER_PAGESET = 0x3,
  530. VM_PKT_ESTABLISH_GPADL = 0x4,
  531. VM_PKT_TEARDOWN_GPADL = 0x5,
  532. VM_PKT_DATA_INBAND = 0x6,
  533. VM_PKT_DATA_USING_XFER_PAGES = 0x7,
  534. VM_PKT_DATA_USING_GPADL = 0x8,
  535. VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
  536. VM_PKT_CANCEL_REQUEST = 0xa,
  537. VM_PKT_COMP = 0xb,
  538. VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
  539. VM_PKT_ADDITIONAL_DATA = 0xd
  540. };
  541. #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
  542. /* Version 1 messages */
  543. enum vmbus_channel_message_type {
  544. CHANNELMSG_INVALID = 0,
  545. CHANNELMSG_OFFERCHANNEL = 1,
  546. CHANNELMSG_RESCIND_CHANNELOFFER = 2,
  547. CHANNELMSG_REQUESTOFFERS = 3,
  548. CHANNELMSG_ALLOFFERS_DELIVERED = 4,
  549. CHANNELMSG_OPENCHANNEL = 5,
  550. CHANNELMSG_OPENCHANNEL_RESULT = 6,
  551. CHANNELMSG_CLOSECHANNEL = 7,
  552. CHANNELMSG_GPADL_HEADER = 8,
  553. CHANNELMSG_GPADL_BODY = 9,
  554. CHANNELMSG_GPADL_CREATED = 10,
  555. CHANNELMSG_GPADL_TEARDOWN = 11,
  556. CHANNELMSG_GPADL_TORNDOWN = 12,
  557. CHANNELMSG_RELID_RELEASED = 13,
  558. CHANNELMSG_INITIATE_CONTACT = 14,
  559. CHANNELMSG_VERSION_RESPONSE = 15,
  560. CHANNELMSG_UNLOAD = 16,
  561. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  562. CHANNELMSG_VIEWRANGE_ADD = 17,
  563. CHANNELMSG_VIEWRANGE_REMOVE = 18,
  564. #endif
  565. CHANNELMSG_COUNT
  566. };
  567. struct vmbus_channel_message_header {
  568. enum vmbus_channel_message_type msgtype;
  569. u32 padding;
  570. } __packed;
  571. /* Query VMBus Version parameters */
  572. struct vmbus_channel_query_vmbus_version {
  573. struct vmbus_channel_message_header header;
  574. u32 version;
  575. } __packed;
  576. /* VMBus Version Supported parameters */
  577. struct vmbus_channel_version_supported {
  578. struct vmbus_channel_message_header header;
  579. u8 version_supported;
  580. } __packed;
  581. /* Offer Channel parameters */
  582. struct vmbus_channel_offer_channel {
  583. struct vmbus_channel_message_header header;
  584. struct vmbus_channel_offer offer;
  585. u32 child_relid;
  586. u8 monitorid;
  587. /*
  588. * win7 and beyond splits this field into a bit field.
  589. */
  590. u8 monitor_allocated:1;
  591. u8 reserved:7;
  592. /*
  593. * These are new fields added in win7 and later.
  594. * Do not access these fields without checking the
  595. * negotiated protocol.
  596. *
  597. * If "is_dedicated_interrupt" is set, we must not set the
  598. * associated bit in the channel bitmap while sending the
  599. * interrupt to the host.
  600. *
  601. * connection_id is to be used in signaling the host.
  602. */
  603. u16 is_dedicated_interrupt:1;
  604. u16 reserved1:15;
  605. u32 connection_id;
  606. } __packed;
  607. /* Rescind Offer parameters */
  608. struct vmbus_channel_rescind_offer {
  609. struct vmbus_channel_message_header header;
  610. u32 child_relid;
  611. } __packed;
  612. /*
  613. * Request Offer -- no parameters, SynIC message contains the partition ID
  614. * Set Snoop -- no parameters, SynIC message contains the partition ID
  615. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  616. * All Offers Delivered -- no parameters, SynIC message contains the partition
  617. * ID
  618. * Flush Client -- no parameters, SynIC message contains the partition ID
  619. */
  620. /* Open Channel parameters */
  621. struct vmbus_channel_open_channel {
  622. struct vmbus_channel_message_header header;
  623. /* Identifies the specific VMBus channel that is being opened. */
  624. u32 child_relid;
  625. /* ID making a particular open request at a channel offer unique. */
  626. u32 openid;
  627. /* GPADL for the channel's ring buffer. */
  628. u32 ringbuffer_gpadlhandle;
  629. /* GPADL for the channel's server context save area. */
  630. u32 server_contextarea_gpadlhandle;
  631. /*
  632. * The upstream ring buffer begins at offset zero in the memory
  633. * described by RingBufferGpadlHandle. The downstream ring buffer
  634. * follows it at this offset (in pages).
  635. */
  636. u32 downstream_ringbuffer_pageoffset;
  637. /* User-specific data to be passed along to the server endpoint. */
  638. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  639. } __packed;
  640. /* Open Channel Result parameters */
  641. struct vmbus_channel_open_result {
  642. struct vmbus_channel_message_header header;
  643. u32 child_relid;
  644. u32 openid;
  645. u32 status;
  646. } __packed;
  647. /* Close channel parameters; */
  648. struct vmbus_channel_close_channel {
  649. struct vmbus_channel_message_header header;
  650. u32 child_relid;
  651. } __packed;
  652. /* Channel Message GPADL */
  653. #define GPADL_TYPE_RING_BUFFER 1
  654. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  655. #define GPADL_TYPE_TRANSACTION 8
  656. /*
  657. * The number of PFNs in a GPADL message is defined by the number of
  658. * pages that would be spanned by ByteCount and ByteOffset. If the
  659. * implied number of PFNs won't fit in this packet, there will be a
  660. * follow-up packet that contains more.
  661. */
  662. struct vmbus_channel_gpadl_header {
  663. struct vmbus_channel_message_header header;
  664. u32 child_relid;
  665. u32 gpadl;
  666. u16 range_buflen;
  667. u16 rangecount;
  668. struct gpa_range range[0];
  669. } __packed;
  670. /* This is the followup packet that contains more PFNs. */
  671. struct vmbus_channel_gpadl_body {
  672. struct vmbus_channel_message_header header;
  673. u32 msgnumber;
  674. u32 gpadl;
  675. u64 pfn[0];
  676. } __packed;
  677. struct vmbus_channel_gpadl_created {
  678. struct vmbus_channel_message_header header;
  679. u32 child_relid;
  680. u32 gpadl;
  681. u32 creation_status;
  682. } __packed;
  683. struct vmbus_channel_gpadl_teardown {
  684. struct vmbus_channel_message_header header;
  685. u32 child_relid;
  686. u32 gpadl;
  687. } __packed;
  688. struct vmbus_channel_gpadl_torndown {
  689. struct vmbus_channel_message_header header;
  690. u32 gpadl;
  691. } __packed;
  692. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  693. struct vmbus_channel_view_range_add {
  694. struct vmbus_channel_message_header header;
  695. PHYSICAL_ADDRESS viewrange_base;
  696. u64 viewrange_length;
  697. u32 child_relid;
  698. } __packed;
  699. struct vmbus_channel_view_range_remove {
  700. struct vmbus_channel_message_header header;
  701. PHYSICAL_ADDRESS viewrange_base;
  702. u32 child_relid;
  703. } __packed;
  704. #endif
  705. struct vmbus_channel_relid_released {
  706. struct vmbus_channel_message_header header;
  707. u32 child_relid;
  708. } __packed;
  709. struct vmbus_channel_initiate_contact {
  710. struct vmbus_channel_message_header header;
  711. u32 vmbus_version_requested;
  712. u32 padding2;
  713. u64 interrupt_page;
  714. u64 monitor_page1;
  715. u64 monitor_page2;
  716. } __packed;
  717. struct vmbus_channel_version_response {
  718. struct vmbus_channel_message_header header;
  719. u8 version_supported;
  720. } __packed;
  721. enum vmbus_channel_state {
  722. CHANNEL_OFFER_STATE,
  723. CHANNEL_OPENING_STATE,
  724. CHANNEL_OPEN_STATE,
  725. };
  726. struct vmbus_channel_debug_info {
  727. u32 relid;
  728. enum vmbus_channel_state state;
  729. uuid_le interfacetype;
  730. uuid_le interface_instance;
  731. u32 monitorid;
  732. u32 servermonitor_pending;
  733. u32 servermonitor_latency;
  734. u32 servermonitor_connectionid;
  735. u32 clientmonitor_pending;
  736. u32 clientmonitor_latency;
  737. u32 clientmonitor_connectionid;
  738. struct hv_ring_buffer_debug_info inbound;
  739. struct hv_ring_buffer_debug_info outbound;
  740. };
  741. /*
  742. * Represents each channel msg on the vmbus connection This is a
  743. * variable-size data structure depending on the msg type itself
  744. */
  745. struct vmbus_channel_msginfo {
  746. /* Bookkeeping stuff */
  747. struct list_head msglistentry;
  748. /* So far, this is only used to handle gpadl body message */
  749. struct list_head submsglist;
  750. /* Synchronize the request/response if needed */
  751. struct completion waitevent;
  752. union {
  753. struct vmbus_channel_version_supported version_supported;
  754. struct vmbus_channel_open_result open_result;
  755. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  756. struct vmbus_channel_gpadl_created gpadl_created;
  757. struct vmbus_channel_version_response version_response;
  758. } response;
  759. u32 msgsize;
  760. /*
  761. * The channel message that goes out on the "wire".
  762. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  763. */
  764. unsigned char msg[0];
  765. };
  766. struct vmbus_close_msg {
  767. struct vmbus_channel_msginfo info;
  768. struct vmbus_channel_close_channel msg;
  769. };
  770. /* Define connection identifier type. */
  771. union hv_connection_id {
  772. u32 asu32;
  773. struct {
  774. u32 id:24;
  775. u32 reserved:8;
  776. } u;
  777. };
  778. /* Definition of the hv_signal_event hypercall input structure. */
  779. struct hv_input_signal_event {
  780. union hv_connection_id connectionid;
  781. u16 flag_number;
  782. u16 rsvdz;
  783. };
  784. struct hv_input_signal_event_buffer {
  785. u64 align8;
  786. struct hv_input_signal_event event;
  787. };
  788. struct vmbus_channel {
  789. struct list_head listentry;
  790. struct hv_device *device_obj;
  791. struct work_struct work;
  792. enum vmbus_channel_state state;
  793. struct vmbus_channel_offer_channel offermsg;
  794. /*
  795. * These are based on the OfferMsg.MonitorId.
  796. * Save it here for easy access.
  797. */
  798. u8 monitor_grp;
  799. u8 monitor_bit;
  800. u32 ringbuffer_gpadlhandle;
  801. /* Allocated memory for ring buffer */
  802. void *ringbuffer_pages;
  803. u32 ringbuffer_pagecount;
  804. struct hv_ring_buffer_info outbound; /* send to parent */
  805. struct hv_ring_buffer_info inbound; /* receive from parent */
  806. spinlock_t inbound_lock;
  807. struct workqueue_struct *controlwq;
  808. struct vmbus_close_msg close_msg;
  809. /* Channel callback are invoked in this workqueue context */
  810. /* HANDLE dataWorkQueue; */
  811. void (*onchannel_callback)(void *context);
  812. void *channel_callback_context;
  813. /*
  814. * A channel can be marked for efficient (batched)
  815. * reading:
  816. * If batched_reading is set to "true", we read until the
  817. * channel is empty and hold off interrupts from the host
  818. * during the entire read process.
  819. * If batched_reading is set to "false", the client is not
  820. * going to perform batched reading.
  821. *
  822. * By default we will enable batched reading; specific
  823. * drivers that don't want this behavior can turn it off.
  824. */
  825. bool batched_reading;
  826. bool is_dedicated_interrupt;
  827. struct hv_input_signal_event_buffer sig_buf;
  828. struct hv_input_signal_event *sig_event;
  829. };
  830. static inline void set_channel_read_state(struct vmbus_channel *c, bool state)
  831. {
  832. c->batched_reading = state;
  833. }
  834. void vmbus_onmessage(void *context);
  835. int vmbus_request_offers(void);
  836. /* The format must be the same as struct vmdata_gpa_direct */
  837. struct vmbus_channel_packet_page_buffer {
  838. u16 type;
  839. u16 dataoffset8;
  840. u16 length8;
  841. u16 flags;
  842. u64 transactionid;
  843. u32 reserved;
  844. u32 rangecount;
  845. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  846. } __packed;
  847. /* The format must be the same as struct vmdata_gpa_direct */
  848. struct vmbus_channel_packet_multipage_buffer {
  849. u16 type;
  850. u16 dataoffset8;
  851. u16 length8;
  852. u16 flags;
  853. u64 transactionid;
  854. u32 reserved;
  855. u32 rangecount; /* Always 1 in this case */
  856. struct hv_multipage_buffer range;
  857. } __packed;
  858. extern int vmbus_open(struct vmbus_channel *channel,
  859. u32 send_ringbuffersize,
  860. u32 recv_ringbuffersize,
  861. void *userdata,
  862. u32 userdatalen,
  863. void(*onchannel_callback)(void *context),
  864. void *context);
  865. extern void vmbus_close(struct vmbus_channel *channel);
  866. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  867. const void *buffer,
  868. u32 bufferLen,
  869. u64 requestid,
  870. enum vmbus_packet_type type,
  871. u32 flags);
  872. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  873. struct hv_page_buffer pagebuffers[],
  874. u32 pagecount,
  875. void *buffer,
  876. u32 bufferlen,
  877. u64 requestid);
  878. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  879. struct hv_multipage_buffer *mpb,
  880. void *buffer,
  881. u32 bufferlen,
  882. u64 requestid);
  883. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  884. void *kbuffer,
  885. u32 size,
  886. u32 *gpadl_handle);
  887. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  888. u32 gpadl_handle);
  889. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  890. void *buffer,
  891. u32 bufferlen,
  892. u32 *buffer_actual_len,
  893. u64 *requestid);
  894. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  895. void *buffer,
  896. u32 bufferlen,
  897. u32 *buffer_actual_len,
  898. u64 *requestid);
  899. extern void vmbus_get_debug_info(struct vmbus_channel *channel,
  900. struct vmbus_channel_debug_info *debug);
  901. extern void vmbus_ontimer(unsigned long data);
  902. struct hv_dev_port_info {
  903. u32 int_mask;
  904. u32 read_idx;
  905. u32 write_idx;
  906. u32 bytes_avail_toread;
  907. u32 bytes_avail_towrite;
  908. };
  909. /* Base driver object */
  910. struct hv_driver {
  911. const char *name;
  912. /* the device type supported by this driver */
  913. uuid_le dev_type;
  914. const struct hv_vmbus_device_id *id_table;
  915. struct device_driver driver;
  916. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  917. int (*remove)(struct hv_device *);
  918. void (*shutdown)(struct hv_device *);
  919. };
  920. /* Base device object */
  921. struct hv_device {
  922. /* the device type id of this device */
  923. uuid_le dev_type;
  924. /* the device instance id of this device */
  925. uuid_le dev_instance;
  926. struct device device;
  927. struct vmbus_channel *channel;
  928. };
  929. static inline struct hv_device *device_to_hv_device(struct device *d)
  930. {
  931. return container_of(d, struct hv_device, device);
  932. }
  933. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  934. {
  935. return container_of(d, struct hv_driver, driver);
  936. }
  937. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  938. {
  939. dev_set_drvdata(&dev->device, data);
  940. }
  941. static inline void *hv_get_drvdata(struct hv_device *dev)
  942. {
  943. return dev_get_drvdata(&dev->device);
  944. }
  945. /* Vmbus interface */
  946. #define vmbus_driver_register(driver) \
  947. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  948. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  949. struct module *owner,
  950. const char *mod_name);
  951. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  952. /**
  953. * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
  954. *
  955. * This macro is used to create a struct hv_vmbus_device_id that matches a
  956. * specific device.
  957. */
  958. #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
  959. g8, g9, ga, gb, gc, gd, ge, gf) \
  960. .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
  961. g8, g9, ga, gb, gc, gd, ge, gf },
  962. /*
  963. * Common header for Hyper-V ICs
  964. */
  965. #define ICMSGTYPE_NEGOTIATE 0
  966. #define ICMSGTYPE_HEARTBEAT 1
  967. #define ICMSGTYPE_KVPEXCHANGE 2
  968. #define ICMSGTYPE_SHUTDOWN 3
  969. #define ICMSGTYPE_TIMESYNC 4
  970. #define ICMSGTYPE_VSS 5
  971. #define ICMSGHDRFLAG_TRANSACTION 1
  972. #define ICMSGHDRFLAG_REQUEST 2
  973. #define ICMSGHDRFLAG_RESPONSE 4
  974. /*
  975. * While we want to handle util services as regular devices,
  976. * there is only one instance of each of these services; so
  977. * we statically allocate the service specific state.
  978. */
  979. struct hv_util_service {
  980. u8 *recv_buffer;
  981. void (*util_cb)(void *);
  982. int (*util_init)(struct hv_util_service *);
  983. void (*util_deinit)(void);
  984. };
  985. struct vmbuspipe_hdr {
  986. u32 flags;
  987. u32 msgsize;
  988. } __packed;
  989. struct ic_version {
  990. u16 major;
  991. u16 minor;
  992. } __packed;
  993. struct icmsg_hdr {
  994. struct ic_version icverframe;
  995. u16 icmsgtype;
  996. struct ic_version icvermsg;
  997. u16 icmsgsize;
  998. u32 status;
  999. u8 ictransaction_id;
  1000. u8 icflags;
  1001. u8 reserved[2];
  1002. } __packed;
  1003. struct icmsg_negotiate {
  1004. u16 icframe_vercnt;
  1005. u16 icmsg_vercnt;
  1006. u32 reserved;
  1007. struct ic_version icversion_data[1]; /* any size array */
  1008. } __packed;
  1009. struct shutdown_msg_data {
  1010. u32 reason_code;
  1011. u32 timeout_seconds;
  1012. u32 flags;
  1013. u8 display_message[2048];
  1014. } __packed;
  1015. struct heartbeat_msg_data {
  1016. u64 seq_num;
  1017. u32 reserved[8];
  1018. } __packed;
  1019. /* Time Sync IC defs */
  1020. #define ICTIMESYNCFLAG_PROBE 0
  1021. #define ICTIMESYNCFLAG_SYNC 1
  1022. #define ICTIMESYNCFLAG_SAMPLE 2
  1023. #ifdef __x86_64__
  1024. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1025. #else
  1026. #define WLTIMEDELTA 116444736000000000LL
  1027. #endif
  1028. struct ictimesync_data {
  1029. u64 parenttime;
  1030. u64 childtime;
  1031. u64 roundtriptime;
  1032. u8 flags;
  1033. } __packed;
  1034. struct hyperv_service_callback {
  1035. u8 msg_type;
  1036. char *log_msg;
  1037. uuid_le data;
  1038. struct vmbus_channel *channel;
  1039. void (*callback) (void *context);
  1040. };
  1041. #define MAX_SRV_VER 0x7ffffff
  1042. extern void vmbus_prep_negotiate_resp(struct icmsg_hdr *,
  1043. struct icmsg_negotiate *, u8 *, int,
  1044. int);
  1045. int hv_kvp_init(struct hv_util_service *);
  1046. void hv_kvp_deinit(void);
  1047. void hv_kvp_onchannelcallback(void *);
  1048. /*
  1049. * Negotiated version with the Host.
  1050. */
  1051. extern __u32 vmbus_proto_version;
  1052. #endif /* __KERNEL__ */
  1053. #endif /* _HYPERV_H */