hyperv.h 31 KB

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