hyperv.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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 <uapi/linux/hyperv.h>
  27. #include <linux/types.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/list.h>
  30. #include <linux/timer.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/completion.h>
  33. #include <linux/device.h>
  34. #include <linux/mod_devicetable.h>
  35. #define MAX_PAGE_BUFFER_COUNT 32
  36. #define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
  37. #pragma pack(push, 1)
  38. /* Single-page buffer */
  39. struct hv_page_buffer {
  40. u32 len;
  41. u32 offset;
  42. u64 pfn;
  43. };
  44. /* Multiple-page buffer */
  45. struct hv_multipage_buffer {
  46. /* Length and Offset determines the # of pfns in the array */
  47. u32 len;
  48. u32 offset;
  49. u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
  50. };
  51. /*
  52. * Multiple-page buffer array; the pfn array is variable size:
  53. * The number of entries in the PFN array is determined by
  54. * "len" and "offset".
  55. */
  56. struct hv_mpb_array {
  57. /* Length and Offset determines the # of pfns in the array */
  58. u32 len;
  59. u32 offset;
  60. u64 pfn_array[];
  61. };
  62. /* 0x18 includes the proprietary packet header */
  63. #define MAX_PAGE_BUFFER_PACKET (0x18 + \
  64. (sizeof(struct hv_page_buffer) * \
  65. MAX_PAGE_BUFFER_COUNT))
  66. #define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
  67. sizeof(struct hv_multipage_buffer))
  68. #pragma pack(pop)
  69. struct hv_ring_buffer {
  70. /* Offset in bytes from the start of ring data below */
  71. u32 write_index;
  72. /* Offset in bytes from the start of ring data below */
  73. u32 read_index;
  74. u32 interrupt_mask;
  75. /*
  76. * Win8 uses some of the reserved bits to implement
  77. * interrupt driven flow management. On the send side
  78. * we can request that the receiver interrupt the sender
  79. * when the ring transitions from being full to being able
  80. * to handle a message of size "pending_send_sz".
  81. *
  82. * Add necessary state for this enhancement.
  83. */
  84. u32 pending_send_sz;
  85. u32 reserved1[12];
  86. union {
  87. struct {
  88. u32 feat_pending_send_sz:1;
  89. };
  90. u32 value;
  91. } feature_bits;
  92. /* Pad it to PAGE_SIZE so that data starts on page boundary */
  93. u8 reserved2[4028];
  94. /*
  95. * Ring data starts here + RingDataStartOffset
  96. * !!! DO NOT place any fields below this !!!
  97. */
  98. u8 buffer[0];
  99. } __packed;
  100. struct hv_ring_buffer_info {
  101. struct hv_ring_buffer *ring_buffer;
  102. u32 ring_size; /* Include the shared header */
  103. spinlock_t ring_lock;
  104. u32 ring_datasize; /* < ring_size */
  105. u32 ring_data_startoffset;
  106. };
  107. /*
  108. *
  109. * hv_get_ringbuffer_availbytes()
  110. *
  111. * Get number of bytes available to read and to write to
  112. * for the specified ring buffer
  113. */
  114. static inline void
  115. hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
  116. u32 *read, u32 *write)
  117. {
  118. u32 read_loc, write_loc, dsize;
  119. smp_read_barrier_depends();
  120. /* Capture the read/write indices before they changed */
  121. read_loc = rbi->ring_buffer->read_index;
  122. write_loc = rbi->ring_buffer->write_index;
  123. dsize = rbi->ring_datasize;
  124. *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  125. read_loc - write_loc;
  126. *read = dsize - *write;
  127. }
  128. /*
  129. * VMBUS version is 32 bit entity broken up into
  130. * two 16 bit quantities: major_number. minor_number.
  131. *
  132. * 0 . 13 (Windows Server 2008)
  133. * 1 . 1 (Windows 7)
  134. * 2 . 4 (Windows 8)
  135. * 3 . 0 (Windows 8 R2)
  136. */
  137. #define VERSION_WS2008 ((0 << 16) | (13))
  138. #define VERSION_WIN7 ((1 << 16) | (1))
  139. #define VERSION_WIN8 ((2 << 16) | (4))
  140. #define VERSION_WIN8_1 ((3 << 16) | (0))
  141. #define VERSION_INVAL -1
  142. #define VERSION_CURRENT VERSION_WIN8_1
  143. /* Make maximum size of pipe payload of 16K */
  144. #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
  145. /* Define PipeMode values. */
  146. #define VMBUS_PIPE_TYPE_BYTE 0x00000000
  147. #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
  148. /* The size of the user defined data buffer for non-pipe offers. */
  149. #define MAX_USER_DEFINED_BYTES 120
  150. /* The size of the user defined data buffer for pipe offers. */
  151. #define MAX_PIPE_USER_DEFINED_BYTES 116
  152. /*
  153. * At the center of the Channel Management library is the Channel Offer. This
  154. * struct contains the fundamental information about an offer.
  155. */
  156. struct vmbus_channel_offer {
  157. uuid_le if_type;
  158. uuid_le if_instance;
  159. /*
  160. * These two fields are not currently used.
  161. */
  162. u64 reserved1;
  163. u64 reserved2;
  164. u16 chn_flags;
  165. u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
  166. union {
  167. /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
  168. struct {
  169. unsigned char user_def[MAX_USER_DEFINED_BYTES];
  170. } std;
  171. /*
  172. * Pipes:
  173. * The following sructure is an integrated pipe protocol, which
  174. * is implemented on top of standard user-defined data. Pipe
  175. * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
  176. * use.
  177. */
  178. struct {
  179. u32 pipe_mode;
  180. unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
  181. } pipe;
  182. } u;
  183. /*
  184. * The sub_channel_index is defined in win8.
  185. */
  186. u16 sub_channel_index;
  187. u16 reserved3;
  188. } __packed;
  189. /* Server Flags */
  190. #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
  191. #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
  192. #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
  193. #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
  194. #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
  195. #define VMBUS_CHANNEL_PARENT_OFFER 0x200
  196. #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
  197. struct vmpacket_descriptor {
  198. u16 type;
  199. u16 offset8;
  200. u16 len8;
  201. u16 flags;
  202. u64 trans_id;
  203. } __packed;
  204. struct vmpacket_header {
  205. u32 prev_pkt_start_offset;
  206. struct vmpacket_descriptor descriptor;
  207. } __packed;
  208. struct vmtransfer_page_range {
  209. u32 byte_count;
  210. u32 byte_offset;
  211. } __packed;
  212. struct vmtransfer_page_packet_header {
  213. struct vmpacket_descriptor d;
  214. u16 xfer_pageset_id;
  215. u8 sender_owns_set;
  216. u8 reserved;
  217. u32 range_cnt;
  218. struct vmtransfer_page_range ranges[1];
  219. } __packed;
  220. struct vmgpadl_packet_header {
  221. struct vmpacket_descriptor d;
  222. u32 gpadl;
  223. u32 reserved;
  224. } __packed;
  225. struct vmadd_remove_transfer_page_set {
  226. struct vmpacket_descriptor d;
  227. u32 gpadl;
  228. u16 xfer_pageset_id;
  229. u16 reserved;
  230. } __packed;
  231. /*
  232. * This structure defines a range in guest physical space that can be made to
  233. * look virtually contiguous.
  234. */
  235. struct gpa_range {
  236. u32 byte_count;
  237. u32 byte_offset;
  238. u64 pfn_array[0];
  239. };
  240. /*
  241. * This is the format for an Establish Gpadl packet, which contains a handle by
  242. * which this GPADL will be known and a set of GPA ranges associated with it.
  243. * This can be converted to a MDL by the guest OS. If there are multiple GPA
  244. * ranges, then the resulting MDL will be "chained," representing multiple VA
  245. * ranges.
  246. */
  247. struct vmestablish_gpadl {
  248. struct vmpacket_descriptor d;
  249. u32 gpadl;
  250. u32 range_cnt;
  251. struct gpa_range range[1];
  252. } __packed;
  253. /*
  254. * This is the format for a Teardown Gpadl packet, which indicates that the
  255. * GPADL handle in the Establish Gpadl packet will never be referenced again.
  256. */
  257. struct vmteardown_gpadl {
  258. struct vmpacket_descriptor d;
  259. u32 gpadl;
  260. u32 reserved; /* for alignment to a 8-byte boundary */
  261. } __packed;
  262. /*
  263. * This is the format for a GPA-Direct packet, which contains a set of GPA
  264. * ranges, in addition to commands and/or data.
  265. */
  266. struct vmdata_gpa_direct {
  267. struct vmpacket_descriptor d;
  268. u32 reserved;
  269. u32 range_cnt;
  270. struct gpa_range range[1];
  271. } __packed;
  272. /* This is the format for a Additional Data Packet. */
  273. struct vmadditional_data {
  274. struct vmpacket_descriptor d;
  275. u64 total_bytes;
  276. u32 offset;
  277. u32 byte_cnt;
  278. unsigned char data[1];
  279. } __packed;
  280. union vmpacket_largest_possible_header {
  281. struct vmpacket_descriptor simple_hdr;
  282. struct vmtransfer_page_packet_header xfer_page_hdr;
  283. struct vmgpadl_packet_header gpadl_hdr;
  284. struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
  285. struct vmestablish_gpadl establish_gpadl_hdr;
  286. struct vmteardown_gpadl teardown_gpadl_hdr;
  287. struct vmdata_gpa_direct data_gpa_direct_hdr;
  288. };
  289. #define VMPACKET_DATA_START_ADDRESS(__packet) \
  290. (void *)(((unsigned char *)__packet) + \
  291. ((struct vmpacket_descriptor)__packet)->offset8 * 8)
  292. #define VMPACKET_DATA_LENGTH(__packet) \
  293. ((((struct vmpacket_descriptor)__packet)->len8 - \
  294. ((struct vmpacket_descriptor)__packet)->offset8) * 8)
  295. #define VMPACKET_TRANSFER_MODE(__packet) \
  296. (((struct IMPACT)__packet)->type)
  297. enum vmbus_packet_type {
  298. VM_PKT_INVALID = 0x0,
  299. VM_PKT_SYNCH = 0x1,
  300. VM_PKT_ADD_XFER_PAGESET = 0x2,
  301. VM_PKT_RM_XFER_PAGESET = 0x3,
  302. VM_PKT_ESTABLISH_GPADL = 0x4,
  303. VM_PKT_TEARDOWN_GPADL = 0x5,
  304. VM_PKT_DATA_INBAND = 0x6,
  305. VM_PKT_DATA_USING_XFER_PAGES = 0x7,
  306. VM_PKT_DATA_USING_GPADL = 0x8,
  307. VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
  308. VM_PKT_CANCEL_REQUEST = 0xa,
  309. VM_PKT_COMP = 0xb,
  310. VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
  311. VM_PKT_ADDITIONAL_DATA = 0xd
  312. };
  313. #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
  314. /* Version 1 messages */
  315. enum vmbus_channel_message_type {
  316. CHANNELMSG_INVALID = 0,
  317. CHANNELMSG_OFFERCHANNEL = 1,
  318. CHANNELMSG_RESCIND_CHANNELOFFER = 2,
  319. CHANNELMSG_REQUESTOFFERS = 3,
  320. CHANNELMSG_ALLOFFERS_DELIVERED = 4,
  321. CHANNELMSG_OPENCHANNEL = 5,
  322. CHANNELMSG_OPENCHANNEL_RESULT = 6,
  323. CHANNELMSG_CLOSECHANNEL = 7,
  324. CHANNELMSG_GPADL_HEADER = 8,
  325. CHANNELMSG_GPADL_BODY = 9,
  326. CHANNELMSG_GPADL_CREATED = 10,
  327. CHANNELMSG_GPADL_TEARDOWN = 11,
  328. CHANNELMSG_GPADL_TORNDOWN = 12,
  329. CHANNELMSG_RELID_RELEASED = 13,
  330. CHANNELMSG_INITIATE_CONTACT = 14,
  331. CHANNELMSG_VERSION_RESPONSE = 15,
  332. CHANNELMSG_UNLOAD = 16,
  333. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  334. CHANNELMSG_VIEWRANGE_ADD = 17,
  335. CHANNELMSG_VIEWRANGE_REMOVE = 18,
  336. #endif
  337. CHANNELMSG_COUNT
  338. };
  339. struct vmbus_channel_message_header {
  340. enum vmbus_channel_message_type msgtype;
  341. u32 padding;
  342. } __packed;
  343. /* Query VMBus Version parameters */
  344. struct vmbus_channel_query_vmbus_version {
  345. struct vmbus_channel_message_header header;
  346. u32 version;
  347. } __packed;
  348. /* VMBus Version Supported parameters */
  349. struct vmbus_channel_version_supported {
  350. struct vmbus_channel_message_header header;
  351. u8 version_supported;
  352. } __packed;
  353. /* Offer Channel parameters */
  354. struct vmbus_channel_offer_channel {
  355. struct vmbus_channel_message_header header;
  356. struct vmbus_channel_offer offer;
  357. u32 child_relid;
  358. u8 monitorid;
  359. /*
  360. * win7 and beyond splits this field into a bit field.
  361. */
  362. u8 monitor_allocated:1;
  363. u8 reserved:7;
  364. /*
  365. * These are new fields added in win7 and later.
  366. * Do not access these fields without checking the
  367. * negotiated protocol.
  368. *
  369. * If "is_dedicated_interrupt" is set, we must not set the
  370. * associated bit in the channel bitmap while sending the
  371. * interrupt to the host.
  372. *
  373. * connection_id is to be used in signaling the host.
  374. */
  375. u16 is_dedicated_interrupt:1;
  376. u16 reserved1:15;
  377. u32 connection_id;
  378. } __packed;
  379. /* Rescind Offer parameters */
  380. struct vmbus_channel_rescind_offer {
  381. struct vmbus_channel_message_header header;
  382. u32 child_relid;
  383. } __packed;
  384. /*
  385. * Request Offer -- no parameters, SynIC message contains the partition ID
  386. * Set Snoop -- no parameters, SynIC message contains the partition ID
  387. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  388. * All Offers Delivered -- no parameters, SynIC message contains the partition
  389. * ID
  390. * Flush Client -- no parameters, SynIC message contains the partition ID
  391. */
  392. /* Open Channel parameters */
  393. struct vmbus_channel_open_channel {
  394. struct vmbus_channel_message_header header;
  395. /* Identifies the specific VMBus channel that is being opened. */
  396. u32 child_relid;
  397. /* ID making a particular open request at a channel offer unique. */
  398. u32 openid;
  399. /* GPADL for the channel's ring buffer. */
  400. u32 ringbuffer_gpadlhandle;
  401. /*
  402. * Starting with win8, this field will be used to specify
  403. * the target virtual processor on which to deliver the interrupt for
  404. * the host to guest communication.
  405. * Prior to win8, incoming channel interrupts would only
  406. * be delivered on cpu 0. Setting this value to 0 would
  407. * preserve the earlier behavior.
  408. */
  409. u32 target_vp;
  410. /*
  411. * The upstream ring buffer begins at offset zero in the memory
  412. * described by RingBufferGpadlHandle. The downstream ring buffer
  413. * follows it at this offset (in pages).
  414. */
  415. u32 downstream_ringbuffer_pageoffset;
  416. /* User-specific data to be passed along to the server endpoint. */
  417. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  418. } __packed;
  419. /* Open Channel Result parameters */
  420. struct vmbus_channel_open_result {
  421. struct vmbus_channel_message_header header;
  422. u32 child_relid;
  423. u32 openid;
  424. u32 status;
  425. } __packed;
  426. /* Close channel parameters; */
  427. struct vmbus_channel_close_channel {
  428. struct vmbus_channel_message_header header;
  429. u32 child_relid;
  430. } __packed;
  431. /* Channel Message GPADL */
  432. #define GPADL_TYPE_RING_BUFFER 1
  433. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  434. #define GPADL_TYPE_TRANSACTION 8
  435. /*
  436. * The number of PFNs in a GPADL message is defined by the number of
  437. * pages that would be spanned by ByteCount and ByteOffset. If the
  438. * implied number of PFNs won't fit in this packet, there will be a
  439. * follow-up packet that contains more.
  440. */
  441. struct vmbus_channel_gpadl_header {
  442. struct vmbus_channel_message_header header;
  443. u32 child_relid;
  444. u32 gpadl;
  445. u16 range_buflen;
  446. u16 rangecount;
  447. struct gpa_range range[0];
  448. } __packed;
  449. /* This is the followup packet that contains more PFNs. */
  450. struct vmbus_channel_gpadl_body {
  451. struct vmbus_channel_message_header header;
  452. u32 msgnumber;
  453. u32 gpadl;
  454. u64 pfn[0];
  455. } __packed;
  456. struct vmbus_channel_gpadl_created {
  457. struct vmbus_channel_message_header header;
  458. u32 child_relid;
  459. u32 gpadl;
  460. u32 creation_status;
  461. } __packed;
  462. struct vmbus_channel_gpadl_teardown {
  463. struct vmbus_channel_message_header header;
  464. u32 child_relid;
  465. u32 gpadl;
  466. } __packed;
  467. struct vmbus_channel_gpadl_torndown {
  468. struct vmbus_channel_message_header header;
  469. u32 gpadl;
  470. } __packed;
  471. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  472. struct vmbus_channel_view_range_add {
  473. struct vmbus_channel_message_header header;
  474. PHYSICAL_ADDRESS viewrange_base;
  475. u64 viewrange_length;
  476. u32 child_relid;
  477. } __packed;
  478. struct vmbus_channel_view_range_remove {
  479. struct vmbus_channel_message_header header;
  480. PHYSICAL_ADDRESS viewrange_base;
  481. u32 child_relid;
  482. } __packed;
  483. #endif
  484. struct vmbus_channel_relid_released {
  485. struct vmbus_channel_message_header header;
  486. u32 child_relid;
  487. } __packed;
  488. struct vmbus_channel_initiate_contact {
  489. struct vmbus_channel_message_header header;
  490. u32 vmbus_version_requested;
  491. u32 target_vcpu; /* The VCPU the host should respond to */
  492. u64 interrupt_page;
  493. u64 monitor_page1;
  494. u64 monitor_page2;
  495. } __packed;
  496. struct vmbus_channel_version_response {
  497. struct vmbus_channel_message_header header;
  498. u8 version_supported;
  499. } __packed;
  500. enum vmbus_channel_state {
  501. CHANNEL_OFFER_STATE,
  502. CHANNEL_OPENING_STATE,
  503. CHANNEL_OPEN_STATE,
  504. CHANNEL_OPENED_STATE,
  505. };
  506. /*
  507. * Represents each channel msg on the vmbus connection This is a
  508. * variable-size data structure depending on the msg type itself
  509. */
  510. struct vmbus_channel_msginfo {
  511. /* Bookkeeping stuff */
  512. struct list_head msglistentry;
  513. /* So far, this is only used to handle gpadl body message */
  514. struct list_head submsglist;
  515. /* Synchronize the request/response if needed */
  516. struct completion waitevent;
  517. union {
  518. struct vmbus_channel_version_supported version_supported;
  519. struct vmbus_channel_open_result open_result;
  520. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  521. struct vmbus_channel_gpadl_created gpadl_created;
  522. struct vmbus_channel_version_response version_response;
  523. } response;
  524. u32 msgsize;
  525. /*
  526. * The channel message that goes out on the "wire".
  527. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  528. */
  529. unsigned char msg[0];
  530. };
  531. struct vmbus_close_msg {
  532. struct vmbus_channel_msginfo info;
  533. struct vmbus_channel_close_channel msg;
  534. };
  535. /* Define connection identifier type. */
  536. union hv_connection_id {
  537. u32 asu32;
  538. struct {
  539. u32 id:24;
  540. u32 reserved:8;
  541. } u;
  542. };
  543. /* Definition of the hv_signal_event hypercall input structure. */
  544. struct hv_input_signal_event {
  545. union hv_connection_id connectionid;
  546. u16 flag_number;
  547. u16 rsvdz;
  548. };
  549. struct hv_input_signal_event_buffer {
  550. u64 align8;
  551. struct hv_input_signal_event event;
  552. };
  553. struct vmbus_channel {
  554. /* Unique channel id */
  555. int id;
  556. struct list_head listentry;
  557. struct hv_device *device_obj;
  558. struct work_struct work;
  559. enum vmbus_channel_state state;
  560. struct vmbus_channel_offer_channel offermsg;
  561. /*
  562. * These are based on the OfferMsg.MonitorId.
  563. * Save it here for easy access.
  564. */
  565. u8 monitor_grp;
  566. u8 monitor_bit;
  567. bool rescind; /* got rescind msg */
  568. u32 ringbuffer_gpadlhandle;
  569. /* Allocated memory for ring buffer */
  570. void *ringbuffer_pages;
  571. u32 ringbuffer_pagecount;
  572. struct hv_ring_buffer_info outbound; /* send to parent */
  573. struct hv_ring_buffer_info inbound; /* receive from parent */
  574. spinlock_t inbound_lock;
  575. struct workqueue_struct *controlwq;
  576. struct vmbus_close_msg close_msg;
  577. /* Channel callback are invoked in this workqueue context */
  578. /* HANDLE dataWorkQueue; */
  579. void (*onchannel_callback)(void *context);
  580. void *channel_callback_context;
  581. /*
  582. * A channel can be marked for efficient (batched)
  583. * reading:
  584. * If batched_reading is set to "true", we read until the
  585. * channel is empty and hold off interrupts from the host
  586. * during the entire read process.
  587. * If batched_reading is set to "false", the client is not
  588. * going to perform batched reading.
  589. *
  590. * By default we will enable batched reading; specific
  591. * drivers that don't want this behavior can turn it off.
  592. */
  593. bool batched_reading;
  594. bool is_dedicated_interrupt;
  595. struct hv_input_signal_event_buffer sig_buf;
  596. struct hv_input_signal_event *sig_event;
  597. /*
  598. * Starting with win8, this field will be used to specify
  599. * the target virtual processor on which to deliver the interrupt for
  600. * the host to guest communication.
  601. * Prior to win8, incoming channel interrupts would only
  602. * be delivered on cpu 0. Setting this value to 0 would
  603. * preserve the earlier behavior.
  604. */
  605. u32 target_vp;
  606. /* The corresponding CPUID in the guest */
  607. u32 target_cpu;
  608. /*
  609. * Support for sub-channels. For high performance devices,
  610. * it will be useful to have multiple sub-channels to support
  611. * a scalable communication infrastructure with the host.
  612. * The support for sub-channels is implemented as an extention
  613. * to the current infrastructure.
  614. * The initial offer is considered the primary channel and this
  615. * offer message will indicate if the host supports sub-channels.
  616. * The guest is free to ask for sub-channels to be offerred and can
  617. * open these sub-channels as a normal "primary" channel. However,
  618. * all sub-channels will have the same type and instance guids as the
  619. * primary channel. Requests sent on a given channel will result in a
  620. * response on the same channel.
  621. */
  622. /*
  623. * Sub-channel creation callback. This callback will be called in
  624. * process context when a sub-channel offer is received from the host.
  625. * The guest can open the sub-channel in the context of this callback.
  626. */
  627. void (*sc_creation_callback)(struct vmbus_channel *new_sc);
  628. /*
  629. * The spinlock to protect the structure. It is being used to protect
  630. * test-and-set access to various attributes of the structure as well
  631. * as all sc_list operations.
  632. */
  633. spinlock_t lock;
  634. /*
  635. * All Sub-channels of a primary channel are linked here.
  636. */
  637. struct list_head sc_list;
  638. /*
  639. * The primary channel this sub-channel belongs to.
  640. * This will be NULL for the primary channel.
  641. */
  642. struct vmbus_channel *primary_channel;
  643. /*
  644. * Support per-channel state for use by vmbus drivers.
  645. */
  646. void *per_channel_state;
  647. /*
  648. * To support per-cpu lookup mapping of relid to channel,
  649. * link up channels based on their CPU affinity.
  650. */
  651. struct list_head percpu_list;
  652. int num_sc;
  653. int next_oc;
  654. };
  655. static inline void set_channel_read_state(struct vmbus_channel *c, bool state)
  656. {
  657. c->batched_reading = state;
  658. }
  659. static inline void set_per_channel_state(struct vmbus_channel *c, void *s)
  660. {
  661. c->per_channel_state = s;
  662. }
  663. static inline void *get_per_channel_state(struct vmbus_channel *c)
  664. {
  665. return c->per_channel_state;
  666. }
  667. void vmbus_onmessage(void *context);
  668. int vmbus_request_offers(void);
  669. /*
  670. * APIs for managing sub-channels.
  671. */
  672. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  673. void (*sc_cr_cb)(struct vmbus_channel *new_sc));
  674. /*
  675. * Retrieve the (sub) channel on which to send an outgoing request.
  676. * When a primary channel has multiple sub-channels, we choose a
  677. * channel whose VCPU binding is closest to the VCPU on which
  678. * this call is being made.
  679. */
  680. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary);
  681. /*
  682. * Check if sub-channels have already been offerred. This API will be useful
  683. * when the driver is unloaded after establishing sub-channels. In this case,
  684. * when the driver is re-loaded, the driver would have to check if the
  685. * subchannels have already been established before attempting to request
  686. * the creation of sub-channels.
  687. * This function returns TRUE to indicate that subchannels have already been
  688. * created.
  689. * This function should be invoked after setting the callback function for
  690. * sub-channel creation.
  691. */
  692. bool vmbus_are_subchannels_present(struct vmbus_channel *primary);
  693. /* The format must be the same as struct vmdata_gpa_direct */
  694. struct vmbus_channel_packet_page_buffer {
  695. u16 type;
  696. u16 dataoffset8;
  697. u16 length8;
  698. u16 flags;
  699. u64 transactionid;
  700. u32 reserved;
  701. u32 rangecount;
  702. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  703. } __packed;
  704. /* The format must be the same as struct vmdata_gpa_direct */
  705. struct vmbus_channel_packet_multipage_buffer {
  706. u16 type;
  707. u16 dataoffset8;
  708. u16 length8;
  709. u16 flags;
  710. u64 transactionid;
  711. u32 reserved;
  712. u32 rangecount; /* Always 1 in this case */
  713. struct hv_multipage_buffer range;
  714. } __packed;
  715. /* The format must be the same as struct vmdata_gpa_direct */
  716. struct vmbus_packet_mpb_array {
  717. u16 type;
  718. u16 dataoffset8;
  719. u16 length8;
  720. u16 flags;
  721. u64 transactionid;
  722. u32 reserved;
  723. u32 rangecount; /* Always 1 in this case */
  724. struct hv_mpb_array range;
  725. } __packed;
  726. extern int vmbus_open(struct vmbus_channel *channel,
  727. u32 send_ringbuffersize,
  728. u32 recv_ringbuffersize,
  729. void *userdata,
  730. u32 userdatalen,
  731. void(*onchannel_callback)(void *context),
  732. void *context);
  733. extern void vmbus_close(struct vmbus_channel *channel);
  734. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  735. void *buffer,
  736. u32 bufferLen,
  737. u64 requestid,
  738. enum vmbus_packet_type type,
  739. u32 flags);
  740. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  741. struct hv_page_buffer pagebuffers[],
  742. u32 pagecount,
  743. void *buffer,
  744. u32 bufferlen,
  745. u64 requestid);
  746. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  747. struct hv_multipage_buffer *mpb,
  748. void *buffer,
  749. u32 bufferlen,
  750. u64 requestid);
  751. extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  752. struct vmbus_packet_mpb_array *mpb,
  753. u32 desc_size,
  754. void *buffer,
  755. u32 bufferlen,
  756. u64 requestid);
  757. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  758. void *kbuffer,
  759. u32 size,
  760. u32 *gpadl_handle);
  761. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  762. u32 gpadl_handle);
  763. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  764. void *buffer,
  765. u32 bufferlen,
  766. u32 *buffer_actual_len,
  767. u64 *requestid);
  768. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  769. void *buffer,
  770. u32 bufferlen,
  771. u32 *buffer_actual_len,
  772. u64 *requestid);
  773. extern void vmbus_ontimer(unsigned long data);
  774. /* Base driver object */
  775. struct hv_driver {
  776. const char *name;
  777. /* the device type supported by this driver */
  778. uuid_le dev_type;
  779. const struct hv_vmbus_device_id *id_table;
  780. struct device_driver driver;
  781. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  782. int (*remove)(struct hv_device *);
  783. void (*shutdown)(struct hv_device *);
  784. };
  785. /* Base device object */
  786. struct hv_device {
  787. /* the device type id of this device */
  788. uuid_le dev_type;
  789. /* the device instance id of this device */
  790. uuid_le dev_instance;
  791. struct device device;
  792. struct vmbus_channel *channel;
  793. };
  794. static inline struct hv_device *device_to_hv_device(struct device *d)
  795. {
  796. return container_of(d, struct hv_device, device);
  797. }
  798. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  799. {
  800. return container_of(d, struct hv_driver, driver);
  801. }
  802. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  803. {
  804. dev_set_drvdata(&dev->device, data);
  805. }
  806. static inline void *hv_get_drvdata(struct hv_device *dev)
  807. {
  808. return dev_get_drvdata(&dev->device);
  809. }
  810. /* Vmbus interface */
  811. #define vmbus_driver_register(driver) \
  812. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  813. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  814. struct module *owner,
  815. const char *mod_name);
  816. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  817. /**
  818. * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
  819. *
  820. * This macro is used to create a struct hv_vmbus_device_id that matches a
  821. * specific device.
  822. */
  823. #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
  824. g8, g9, ga, gb, gc, gd, ge, gf) \
  825. .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
  826. g8, g9, ga, gb, gc, gd, ge, gf },
  827. /*
  828. * GUID definitions of various offer types - services offered to the guest.
  829. */
  830. /*
  831. * Network GUID
  832. * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
  833. */
  834. #define HV_NIC_GUID \
  835. .guid = { \
  836. 0x63, 0x51, 0x61, 0xf8, 0x3e, 0xdf, 0xc5, 0x46, \
  837. 0x91, 0x3f, 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e \
  838. }
  839. /*
  840. * IDE GUID
  841. * {32412632-86cb-44a2-9b5c-50d1417354f5}
  842. */
  843. #define HV_IDE_GUID \
  844. .guid = { \
  845. 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, \
  846. 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5 \
  847. }
  848. /*
  849. * SCSI GUID
  850. * {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
  851. */
  852. #define HV_SCSI_GUID \
  853. .guid = { \
  854. 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, \
  855. 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f \
  856. }
  857. /*
  858. * Shutdown GUID
  859. * {0e0b6031-5213-4934-818b-38d90ced39db}
  860. */
  861. #define HV_SHUTDOWN_GUID \
  862. .guid = { \
  863. 0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49, \
  864. 0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb \
  865. }
  866. /*
  867. * Time Synch GUID
  868. * {9527E630-D0AE-497b-ADCE-E80AB0175CAF}
  869. */
  870. #define HV_TS_GUID \
  871. .guid = { \
  872. 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49, \
  873. 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf \
  874. }
  875. /*
  876. * Heartbeat GUID
  877. * {57164f39-9115-4e78-ab55-382f3bd5422d}
  878. */
  879. #define HV_HEART_BEAT_GUID \
  880. .guid = { \
  881. 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e, \
  882. 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d \
  883. }
  884. /*
  885. * KVP GUID
  886. * {a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}
  887. */
  888. #define HV_KVP_GUID \
  889. .guid = { \
  890. 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d, \
  891. 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6 \
  892. }
  893. /*
  894. * Dynamic memory GUID
  895. * {525074dc-8985-46e2-8057-a307dc18a502}
  896. */
  897. #define HV_DM_GUID \
  898. .guid = { \
  899. 0xdc, 0x74, 0x50, 0X52, 0x85, 0x89, 0xe2, 0x46, \
  900. 0x80, 0x57, 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02 \
  901. }
  902. /*
  903. * Mouse GUID
  904. * {cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}
  905. */
  906. #define HV_MOUSE_GUID \
  907. .guid = { \
  908. 0x9e, 0xb6, 0xa8, 0xcf, 0x4a, 0x5b, 0xc0, 0x4c, \
  909. 0xb9, 0x8b, 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a \
  910. }
  911. /*
  912. * VSS (Backup/Restore) GUID
  913. */
  914. #define HV_VSS_GUID \
  915. .guid = { \
  916. 0x29, 0x2e, 0xfa, 0x35, 0x23, 0xea, 0x36, 0x42, \
  917. 0x96, 0xae, 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40 \
  918. }
  919. /*
  920. * Synthetic Video GUID
  921. * {DA0A7802-E377-4aac-8E77-0558EB1073F8}
  922. */
  923. #define HV_SYNTHVID_GUID \
  924. .guid = { \
  925. 0x02, 0x78, 0x0a, 0xda, 0x77, 0xe3, 0xac, 0x4a, \
  926. 0x8e, 0x77, 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8 \
  927. }
  928. /*
  929. * Synthetic FC GUID
  930. * {2f9bcc4a-0069-4af3-b76b-6fd0be528cda}
  931. */
  932. #define HV_SYNTHFC_GUID \
  933. .guid = { \
  934. 0x4A, 0xCC, 0x9B, 0x2F, 0x69, 0x00, 0xF3, 0x4A, \
  935. 0xB7, 0x6B, 0x6F, 0xD0, 0xBE, 0x52, 0x8C, 0xDA \
  936. }
  937. /*
  938. * Guest File Copy Service
  939. * {34D14BE3-DEE4-41c8-9AE7-6B174977C192}
  940. */
  941. #define HV_FCOPY_GUID \
  942. .guid = { \
  943. 0xE3, 0x4B, 0xD1, 0x34, 0xE4, 0xDE, 0xC8, 0x41, \
  944. 0x9A, 0xE7, 0x6B, 0x17, 0x49, 0x77, 0xC1, 0x92 \
  945. }
  946. /*
  947. * NetworkDirect. This is the guest RDMA service.
  948. * {8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}
  949. */
  950. #define HV_ND_GUID \
  951. .guid = { \
  952. 0x3d, 0xaf, 0x2e, 0x8c, 0xa7, 0x32, 0x09, 0x4b, \
  953. 0xab, 0x99, 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01 \
  954. }
  955. /*
  956. * Common header for Hyper-V ICs
  957. */
  958. #define ICMSGTYPE_NEGOTIATE 0
  959. #define ICMSGTYPE_HEARTBEAT 1
  960. #define ICMSGTYPE_KVPEXCHANGE 2
  961. #define ICMSGTYPE_SHUTDOWN 3
  962. #define ICMSGTYPE_TIMESYNC 4
  963. #define ICMSGTYPE_VSS 5
  964. #define ICMSGHDRFLAG_TRANSACTION 1
  965. #define ICMSGHDRFLAG_REQUEST 2
  966. #define ICMSGHDRFLAG_RESPONSE 4
  967. /*
  968. * While we want to handle util services as regular devices,
  969. * there is only one instance of each of these services; so
  970. * we statically allocate the service specific state.
  971. */
  972. struct hv_util_service {
  973. u8 *recv_buffer;
  974. void (*util_cb)(void *);
  975. int (*util_init)(struct hv_util_service *);
  976. void (*util_deinit)(void);
  977. };
  978. struct vmbuspipe_hdr {
  979. u32 flags;
  980. u32 msgsize;
  981. } __packed;
  982. struct ic_version {
  983. u16 major;
  984. u16 minor;
  985. } __packed;
  986. struct icmsg_hdr {
  987. struct ic_version icverframe;
  988. u16 icmsgtype;
  989. struct ic_version icvermsg;
  990. u16 icmsgsize;
  991. u32 status;
  992. u8 ictransaction_id;
  993. u8 icflags;
  994. u8 reserved[2];
  995. } __packed;
  996. struct icmsg_negotiate {
  997. u16 icframe_vercnt;
  998. u16 icmsg_vercnt;
  999. u32 reserved;
  1000. struct ic_version icversion_data[1]; /* any size array */
  1001. } __packed;
  1002. struct shutdown_msg_data {
  1003. u32 reason_code;
  1004. u32 timeout_seconds;
  1005. u32 flags;
  1006. u8 display_message[2048];
  1007. } __packed;
  1008. struct heartbeat_msg_data {
  1009. u64 seq_num;
  1010. u32 reserved[8];
  1011. } __packed;
  1012. /* Time Sync IC defs */
  1013. #define ICTIMESYNCFLAG_PROBE 0
  1014. #define ICTIMESYNCFLAG_SYNC 1
  1015. #define ICTIMESYNCFLAG_SAMPLE 2
  1016. #ifdef __x86_64__
  1017. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1018. #else
  1019. #define WLTIMEDELTA 116444736000000000LL
  1020. #endif
  1021. struct ictimesync_data {
  1022. u64 parenttime;
  1023. u64 childtime;
  1024. u64 roundtriptime;
  1025. u8 flags;
  1026. } __packed;
  1027. struct hyperv_service_callback {
  1028. u8 msg_type;
  1029. char *log_msg;
  1030. uuid_le data;
  1031. struct vmbus_channel *channel;
  1032. void (*callback) (void *context);
  1033. };
  1034. #define MAX_SRV_VER 0x7ffffff
  1035. extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *,
  1036. struct icmsg_negotiate *, u8 *, int,
  1037. int);
  1038. int hv_kvp_init(struct hv_util_service *);
  1039. void hv_kvp_deinit(void);
  1040. void hv_kvp_onchannelcallback(void *);
  1041. int hv_vss_init(struct hv_util_service *);
  1042. void hv_vss_deinit(void);
  1043. void hv_vss_onchannelcallback(void *);
  1044. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid);
  1045. extern struct resource hyperv_mmio;
  1046. /*
  1047. * Negotiated version with the Host.
  1048. */
  1049. extern __u32 vmbus_proto_version;
  1050. #endif /* _HYPERV_H */