hyperv.h 31 KB

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