hyperv.h 32 KB

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