hyperv.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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/completion.h>
  33. #include <linux/device.h>
  34. #include <linux/mod_devicetable.h>
  35. #include <linux/interrupt.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. u32 priv_write_index;
  108. u32 priv_read_index;
  109. u32 cached_read_index;
  110. };
  111. /*
  112. *
  113. * hv_get_ringbuffer_availbytes()
  114. *
  115. * Get number of bytes available to read and to write to
  116. * for the specified ring buffer
  117. */
  118. static inline void
  119. hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info *rbi,
  120. u32 *read, u32 *write)
  121. {
  122. u32 read_loc, write_loc, dsize;
  123. /* Capture the read/write indices before they changed */
  124. read_loc = rbi->ring_buffer->read_index;
  125. write_loc = rbi->ring_buffer->write_index;
  126. dsize = rbi->ring_datasize;
  127. *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  128. read_loc - write_loc;
  129. *read = dsize - *write;
  130. }
  131. static inline u32 hv_get_bytes_to_read(const struct hv_ring_buffer_info *rbi)
  132. {
  133. u32 read_loc, write_loc, dsize, read;
  134. dsize = rbi->ring_datasize;
  135. read_loc = rbi->ring_buffer->read_index;
  136. write_loc = READ_ONCE(rbi->ring_buffer->write_index);
  137. read = write_loc >= read_loc ? (write_loc - read_loc) :
  138. (dsize - read_loc) + write_loc;
  139. return read;
  140. }
  141. static inline u32 hv_get_bytes_to_write(const struct hv_ring_buffer_info *rbi)
  142. {
  143. u32 read_loc, write_loc, dsize, write;
  144. dsize = rbi->ring_datasize;
  145. read_loc = READ_ONCE(rbi->ring_buffer->read_index);
  146. write_loc = rbi->ring_buffer->write_index;
  147. write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  148. read_loc - write_loc;
  149. return write;
  150. }
  151. static inline u32 hv_get_cached_bytes_to_write(
  152. const struct hv_ring_buffer_info *rbi)
  153. {
  154. u32 read_loc, write_loc, dsize, write;
  155. dsize = rbi->ring_datasize;
  156. read_loc = rbi->cached_read_index;
  157. write_loc = rbi->ring_buffer->write_index;
  158. write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  159. read_loc - write_loc;
  160. return write;
  161. }
  162. /*
  163. * VMBUS version is 32 bit entity broken up into
  164. * two 16 bit quantities: major_number. minor_number.
  165. *
  166. * 0 . 13 (Windows Server 2008)
  167. * 1 . 1 (Windows 7)
  168. * 2 . 4 (Windows 8)
  169. * 3 . 0 (Windows 8 R2)
  170. * 4 . 0 (Windows 10)
  171. */
  172. #define VERSION_WS2008 ((0 << 16) | (13))
  173. #define VERSION_WIN7 ((1 << 16) | (1))
  174. #define VERSION_WIN8 ((2 << 16) | (4))
  175. #define VERSION_WIN8_1 ((3 << 16) | (0))
  176. #define VERSION_WIN10 ((4 << 16) | (0))
  177. #define VERSION_INVAL -1
  178. #define VERSION_CURRENT VERSION_WIN10
  179. /* Make maximum size of pipe payload of 16K */
  180. #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
  181. /* Define PipeMode values. */
  182. #define VMBUS_PIPE_TYPE_BYTE 0x00000000
  183. #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
  184. /* The size of the user defined data buffer for non-pipe offers. */
  185. #define MAX_USER_DEFINED_BYTES 120
  186. /* The size of the user defined data buffer for pipe offers. */
  187. #define MAX_PIPE_USER_DEFINED_BYTES 116
  188. /*
  189. * At the center of the Channel Management library is the Channel Offer. This
  190. * struct contains the fundamental information about an offer.
  191. */
  192. struct vmbus_channel_offer {
  193. uuid_le if_type;
  194. uuid_le if_instance;
  195. /*
  196. * These two fields are not currently used.
  197. */
  198. u64 reserved1;
  199. u64 reserved2;
  200. u16 chn_flags;
  201. u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
  202. union {
  203. /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
  204. struct {
  205. unsigned char user_def[MAX_USER_DEFINED_BYTES];
  206. } std;
  207. /*
  208. * Pipes:
  209. * The following sructure is an integrated pipe protocol, which
  210. * is implemented on top of standard user-defined data. Pipe
  211. * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
  212. * use.
  213. */
  214. struct {
  215. u32 pipe_mode;
  216. unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
  217. } pipe;
  218. } u;
  219. /*
  220. * The sub_channel_index is defined in win8.
  221. */
  222. u16 sub_channel_index;
  223. u16 reserved3;
  224. } __packed;
  225. /* Server Flags */
  226. #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
  227. #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
  228. #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
  229. #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
  230. #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
  231. #define VMBUS_CHANNEL_PARENT_OFFER 0x200
  232. #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
  233. #define VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER 0x2000
  234. struct vmpacket_descriptor {
  235. u16 type;
  236. u16 offset8;
  237. u16 len8;
  238. u16 flags;
  239. u64 trans_id;
  240. } __packed;
  241. struct vmpacket_header {
  242. u32 prev_pkt_start_offset;
  243. struct vmpacket_descriptor descriptor;
  244. } __packed;
  245. struct vmtransfer_page_range {
  246. u32 byte_count;
  247. u32 byte_offset;
  248. } __packed;
  249. struct vmtransfer_page_packet_header {
  250. struct vmpacket_descriptor d;
  251. u16 xfer_pageset_id;
  252. u8 sender_owns_set;
  253. u8 reserved;
  254. u32 range_cnt;
  255. struct vmtransfer_page_range ranges[1];
  256. } __packed;
  257. struct vmgpadl_packet_header {
  258. struct vmpacket_descriptor d;
  259. u32 gpadl;
  260. u32 reserved;
  261. } __packed;
  262. struct vmadd_remove_transfer_page_set {
  263. struct vmpacket_descriptor d;
  264. u32 gpadl;
  265. u16 xfer_pageset_id;
  266. u16 reserved;
  267. } __packed;
  268. /*
  269. * This structure defines a range in guest physical space that can be made to
  270. * look virtually contiguous.
  271. */
  272. struct gpa_range {
  273. u32 byte_count;
  274. u32 byte_offset;
  275. u64 pfn_array[0];
  276. };
  277. /*
  278. * This is the format for an Establish Gpadl packet, which contains a handle by
  279. * which this GPADL will be known and a set of GPA ranges associated with it.
  280. * This can be converted to a MDL by the guest OS. If there are multiple GPA
  281. * ranges, then the resulting MDL will be "chained," representing multiple VA
  282. * ranges.
  283. */
  284. struct vmestablish_gpadl {
  285. struct vmpacket_descriptor d;
  286. u32 gpadl;
  287. u32 range_cnt;
  288. struct gpa_range range[1];
  289. } __packed;
  290. /*
  291. * This is the format for a Teardown Gpadl packet, which indicates that the
  292. * GPADL handle in the Establish Gpadl packet will never be referenced again.
  293. */
  294. struct vmteardown_gpadl {
  295. struct vmpacket_descriptor d;
  296. u32 gpadl;
  297. u32 reserved; /* for alignment to a 8-byte boundary */
  298. } __packed;
  299. /*
  300. * This is the format for a GPA-Direct packet, which contains a set of GPA
  301. * ranges, in addition to commands and/or data.
  302. */
  303. struct vmdata_gpa_direct {
  304. struct vmpacket_descriptor d;
  305. u32 reserved;
  306. u32 range_cnt;
  307. struct gpa_range range[1];
  308. } __packed;
  309. /* This is the format for a Additional Data Packet. */
  310. struct vmadditional_data {
  311. struct vmpacket_descriptor d;
  312. u64 total_bytes;
  313. u32 offset;
  314. u32 byte_cnt;
  315. unsigned char data[1];
  316. } __packed;
  317. union vmpacket_largest_possible_header {
  318. struct vmpacket_descriptor simple_hdr;
  319. struct vmtransfer_page_packet_header xfer_page_hdr;
  320. struct vmgpadl_packet_header gpadl_hdr;
  321. struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
  322. struct vmestablish_gpadl establish_gpadl_hdr;
  323. struct vmteardown_gpadl teardown_gpadl_hdr;
  324. struct vmdata_gpa_direct data_gpa_direct_hdr;
  325. };
  326. #define VMPACKET_DATA_START_ADDRESS(__packet) \
  327. (void *)(((unsigned char *)__packet) + \
  328. ((struct vmpacket_descriptor)__packet)->offset8 * 8)
  329. #define VMPACKET_DATA_LENGTH(__packet) \
  330. ((((struct vmpacket_descriptor)__packet)->len8 - \
  331. ((struct vmpacket_descriptor)__packet)->offset8) * 8)
  332. #define VMPACKET_TRANSFER_MODE(__packet) \
  333. (((struct IMPACT)__packet)->type)
  334. enum vmbus_packet_type {
  335. VM_PKT_INVALID = 0x0,
  336. VM_PKT_SYNCH = 0x1,
  337. VM_PKT_ADD_XFER_PAGESET = 0x2,
  338. VM_PKT_RM_XFER_PAGESET = 0x3,
  339. VM_PKT_ESTABLISH_GPADL = 0x4,
  340. VM_PKT_TEARDOWN_GPADL = 0x5,
  341. VM_PKT_DATA_INBAND = 0x6,
  342. VM_PKT_DATA_USING_XFER_PAGES = 0x7,
  343. VM_PKT_DATA_USING_GPADL = 0x8,
  344. VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
  345. VM_PKT_CANCEL_REQUEST = 0xa,
  346. VM_PKT_COMP = 0xb,
  347. VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
  348. VM_PKT_ADDITIONAL_DATA = 0xd
  349. };
  350. #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
  351. /* Version 1 messages */
  352. enum vmbus_channel_message_type {
  353. CHANNELMSG_INVALID = 0,
  354. CHANNELMSG_OFFERCHANNEL = 1,
  355. CHANNELMSG_RESCIND_CHANNELOFFER = 2,
  356. CHANNELMSG_REQUESTOFFERS = 3,
  357. CHANNELMSG_ALLOFFERS_DELIVERED = 4,
  358. CHANNELMSG_OPENCHANNEL = 5,
  359. CHANNELMSG_OPENCHANNEL_RESULT = 6,
  360. CHANNELMSG_CLOSECHANNEL = 7,
  361. CHANNELMSG_GPADL_HEADER = 8,
  362. CHANNELMSG_GPADL_BODY = 9,
  363. CHANNELMSG_GPADL_CREATED = 10,
  364. CHANNELMSG_GPADL_TEARDOWN = 11,
  365. CHANNELMSG_GPADL_TORNDOWN = 12,
  366. CHANNELMSG_RELID_RELEASED = 13,
  367. CHANNELMSG_INITIATE_CONTACT = 14,
  368. CHANNELMSG_VERSION_RESPONSE = 15,
  369. CHANNELMSG_UNLOAD = 16,
  370. CHANNELMSG_UNLOAD_RESPONSE = 17,
  371. CHANNELMSG_18 = 18,
  372. CHANNELMSG_19 = 19,
  373. CHANNELMSG_20 = 20,
  374. CHANNELMSG_TL_CONNECT_REQUEST = 21,
  375. CHANNELMSG_COUNT
  376. };
  377. struct vmbus_channel_message_header {
  378. enum vmbus_channel_message_type msgtype;
  379. u32 padding;
  380. } __packed;
  381. /* Query VMBus Version parameters */
  382. struct vmbus_channel_query_vmbus_version {
  383. struct vmbus_channel_message_header header;
  384. u32 version;
  385. } __packed;
  386. /* VMBus Version Supported parameters */
  387. struct vmbus_channel_version_supported {
  388. struct vmbus_channel_message_header header;
  389. u8 version_supported;
  390. } __packed;
  391. /* Offer Channel parameters */
  392. struct vmbus_channel_offer_channel {
  393. struct vmbus_channel_message_header header;
  394. struct vmbus_channel_offer offer;
  395. u32 child_relid;
  396. u8 monitorid;
  397. /*
  398. * win7 and beyond splits this field into a bit field.
  399. */
  400. u8 monitor_allocated:1;
  401. u8 reserved:7;
  402. /*
  403. * These are new fields added in win7 and later.
  404. * Do not access these fields without checking the
  405. * negotiated protocol.
  406. *
  407. * If "is_dedicated_interrupt" is set, we must not set the
  408. * associated bit in the channel bitmap while sending the
  409. * interrupt to the host.
  410. *
  411. * connection_id is to be used in signaling the host.
  412. */
  413. u16 is_dedicated_interrupt:1;
  414. u16 reserved1:15;
  415. u32 connection_id;
  416. } __packed;
  417. /* Rescind Offer parameters */
  418. struct vmbus_channel_rescind_offer {
  419. struct vmbus_channel_message_header header;
  420. u32 child_relid;
  421. } __packed;
  422. static inline u32
  423. hv_ringbuffer_pending_size(const struct hv_ring_buffer_info *rbi)
  424. {
  425. return rbi->ring_buffer->pending_send_sz;
  426. }
  427. /*
  428. * Request Offer -- no parameters, SynIC message contains the partition ID
  429. * Set Snoop -- no parameters, SynIC message contains the partition ID
  430. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  431. * All Offers Delivered -- no parameters, SynIC message contains the partition
  432. * ID
  433. * Flush Client -- no parameters, SynIC message contains the partition ID
  434. */
  435. /* Open Channel parameters */
  436. struct vmbus_channel_open_channel {
  437. struct vmbus_channel_message_header header;
  438. /* Identifies the specific VMBus channel that is being opened. */
  439. u32 child_relid;
  440. /* ID making a particular open request at a channel offer unique. */
  441. u32 openid;
  442. /* GPADL for the channel's ring buffer. */
  443. u32 ringbuffer_gpadlhandle;
  444. /*
  445. * Starting with win8, this field will be used to specify
  446. * the target virtual processor on which to deliver the interrupt for
  447. * the host to guest communication.
  448. * Prior to win8, incoming channel interrupts would only
  449. * be delivered on cpu 0. Setting this value to 0 would
  450. * preserve the earlier behavior.
  451. */
  452. u32 target_vp;
  453. /*
  454. * The upstream ring buffer begins at offset zero in the memory
  455. * described by RingBufferGpadlHandle. The downstream ring buffer
  456. * follows it at this offset (in pages).
  457. */
  458. u32 downstream_ringbuffer_pageoffset;
  459. /* User-specific data to be passed along to the server endpoint. */
  460. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  461. } __packed;
  462. /* Open Channel Result parameters */
  463. struct vmbus_channel_open_result {
  464. struct vmbus_channel_message_header header;
  465. u32 child_relid;
  466. u32 openid;
  467. u32 status;
  468. } __packed;
  469. /* Close channel parameters; */
  470. struct vmbus_channel_close_channel {
  471. struct vmbus_channel_message_header header;
  472. u32 child_relid;
  473. } __packed;
  474. /* Channel Message GPADL */
  475. #define GPADL_TYPE_RING_BUFFER 1
  476. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  477. #define GPADL_TYPE_TRANSACTION 8
  478. /*
  479. * The number of PFNs in a GPADL message is defined by the number of
  480. * pages that would be spanned by ByteCount and ByteOffset. If the
  481. * implied number of PFNs won't fit in this packet, there will be a
  482. * follow-up packet that contains more.
  483. */
  484. struct vmbus_channel_gpadl_header {
  485. struct vmbus_channel_message_header header;
  486. u32 child_relid;
  487. u32 gpadl;
  488. u16 range_buflen;
  489. u16 rangecount;
  490. struct gpa_range range[0];
  491. } __packed;
  492. /* This is the followup packet that contains more PFNs. */
  493. struct vmbus_channel_gpadl_body {
  494. struct vmbus_channel_message_header header;
  495. u32 msgnumber;
  496. u32 gpadl;
  497. u64 pfn[0];
  498. } __packed;
  499. struct vmbus_channel_gpadl_created {
  500. struct vmbus_channel_message_header header;
  501. u32 child_relid;
  502. u32 gpadl;
  503. u32 creation_status;
  504. } __packed;
  505. struct vmbus_channel_gpadl_teardown {
  506. struct vmbus_channel_message_header header;
  507. u32 child_relid;
  508. u32 gpadl;
  509. } __packed;
  510. struct vmbus_channel_gpadl_torndown {
  511. struct vmbus_channel_message_header header;
  512. u32 gpadl;
  513. } __packed;
  514. struct vmbus_channel_relid_released {
  515. struct vmbus_channel_message_header header;
  516. u32 child_relid;
  517. } __packed;
  518. struct vmbus_channel_initiate_contact {
  519. struct vmbus_channel_message_header header;
  520. u32 vmbus_version_requested;
  521. u32 target_vcpu; /* The VCPU the host should respond to */
  522. u64 interrupt_page;
  523. u64 monitor_page1;
  524. u64 monitor_page2;
  525. } __packed;
  526. /* Hyper-V socket: guest's connect()-ing to host */
  527. struct vmbus_channel_tl_connect_request {
  528. struct vmbus_channel_message_header header;
  529. uuid_le guest_endpoint_id;
  530. uuid_le host_service_id;
  531. } __packed;
  532. struct vmbus_channel_version_response {
  533. struct vmbus_channel_message_header header;
  534. u8 version_supported;
  535. } __packed;
  536. enum vmbus_channel_state {
  537. CHANNEL_OFFER_STATE,
  538. CHANNEL_OPENING_STATE,
  539. CHANNEL_OPEN_STATE,
  540. CHANNEL_OPENED_STATE,
  541. };
  542. /*
  543. * Represents each channel msg on the vmbus connection This is a
  544. * variable-size data structure depending on the msg type itself
  545. */
  546. struct vmbus_channel_msginfo {
  547. /* Bookkeeping stuff */
  548. struct list_head msglistentry;
  549. /* So far, this is only used to handle gpadl body message */
  550. struct list_head submsglist;
  551. /* Synchronize the request/response if needed */
  552. struct completion waitevent;
  553. struct vmbus_channel *waiting_channel;
  554. union {
  555. struct vmbus_channel_version_supported version_supported;
  556. struct vmbus_channel_open_result open_result;
  557. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  558. struct vmbus_channel_gpadl_created gpadl_created;
  559. struct vmbus_channel_version_response version_response;
  560. } response;
  561. u32 msgsize;
  562. /*
  563. * The channel message that goes out on the "wire".
  564. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  565. */
  566. unsigned char msg[0];
  567. };
  568. struct vmbus_close_msg {
  569. struct vmbus_channel_msginfo info;
  570. struct vmbus_channel_close_channel msg;
  571. };
  572. /* Define connection identifier type. */
  573. union hv_connection_id {
  574. u32 asu32;
  575. struct {
  576. u32 id:24;
  577. u32 reserved:8;
  578. } u;
  579. };
  580. /* Definition of the hv_signal_event hypercall input structure. */
  581. struct hv_input_signal_event {
  582. union hv_connection_id connectionid;
  583. u16 flag_number;
  584. u16 rsvdz;
  585. };
  586. struct hv_input_signal_event_buffer {
  587. u64 align8;
  588. struct hv_input_signal_event event;
  589. };
  590. enum hv_numa_policy {
  591. HV_BALANCED = 0,
  592. HV_LOCALIZED,
  593. };
  594. enum vmbus_device_type {
  595. HV_IDE = 0,
  596. HV_SCSI,
  597. HV_FC,
  598. HV_NIC,
  599. HV_ND,
  600. HV_PCIE,
  601. HV_FB,
  602. HV_KBD,
  603. HV_MOUSE,
  604. HV_KVP,
  605. HV_TS,
  606. HV_HB,
  607. HV_SHUTDOWN,
  608. HV_FCOPY,
  609. HV_BACKUP,
  610. HV_DM,
  611. HV_UNKNOWN,
  612. };
  613. struct vmbus_device {
  614. u16 dev_type;
  615. uuid_le guid;
  616. bool perf_device;
  617. };
  618. struct vmbus_channel {
  619. struct list_head listentry;
  620. struct hv_device *device_obj;
  621. enum vmbus_channel_state state;
  622. struct vmbus_channel_offer_channel offermsg;
  623. /*
  624. * These are based on the OfferMsg.MonitorId.
  625. * Save it here for easy access.
  626. */
  627. u8 monitor_grp;
  628. u8 monitor_bit;
  629. bool rescind; /* got rescind msg */
  630. u32 ringbuffer_gpadlhandle;
  631. /* Allocated memory for ring buffer */
  632. void *ringbuffer_pages;
  633. u32 ringbuffer_pagecount;
  634. struct hv_ring_buffer_info outbound; /* send to parent */
  635. struct hv_ring_buffer_info inbound; /* receive from parent */
  636. struct vmbus_close_msg close_msg;
  637. /* Channel callback's invoked in softirq context */
  638. struct tasklet_struct callback_event;
  639. void (*onchannel_callback)(void *context);
  640. void *channel_callback_context;
  641. /*
  642. * A channel can be marked for one of three modes of reading:
  643. * BATCHED - callback called from taslket and should read
  644. * channel until empty. Interrupts from the host
  645. * are masked while read is in process (default).
  646. * DIRECT - callback called from tasklet (softirq).
  647. * ISR - callback called in interrupt context and must
  648. * invoke its own deferred processing.
  649. * Host interrupts are disabled and must be re-enabled
  650. * when ring is empty.
  651. */
  652. enum hv_callback_mode {
  653. HV_CALL_BATCHED,
  654. HV_CALL_DIRECT,
  655. HV_CALL_ISR
  656. } callback_mode;
  657. bool is_dedicated_interrupt;
  658. struct hv_input_signal_event_buffer sig_buf;
  659. struct hv_input_signal_event *sig_event;
  660. /*
  661. * Starting with win8, this field will be used to specify
  662. * the target virtual processor on which to deliver the interrupt for
  663. * the host to guest communication.
  664. * Prior to win8, incoming channel interrupts would only
  665. * be delivered on cpu 0. Setting this value to 0 would
  666. * preserve the earlier behavior.
  667. */
  668. u32 target_vp;
  669. /* The corresponding CPUID in the guest */
  670. u32 target_cpu;
  671. /*
  672. * State to manage the CPU affiliation of channels.
  673. */
  674. struct cpumask alloced_cpus_in_node;
  675. int numa_node;
  676. /*
  677. * Support for sub-channels. For high performance devices,
  678. * it will be useful to have multiple sub-channels to support
  679. * a scalable communication infrastructure with the host.
  680. * The support for sub-channels is implemented as an extention
  681. * to the current infrastructure.
  682. * The initial offer is considered the primary channel and this
  683. * offer message will indicate if the host supports sub-channels.
  684. * The guest is free to ask for sub-channels to be offerred and can
  685. * open these sub-channels as a normal "primary" channel. However,
  686. * all sub-channels will have the same type and instance guids as the
  687. * primary channel. Requests sent on a given channel will result in a
  688. * response on the same channel.
  689. */
  690. /*
  691. * Sub-channel creation callback. This callback will be called in
  692. * process context when a sub-channel offer is received from the host.
  693. * The guest can open the sub-channel in the context of this callback.
  694. */
  695. void (*sc_creation_callback)(struct vmbus_channel *new_sc);
  696. /*
  697. * Channel rescind callback. Some channels (the hvsock ones), need to
  698. * register a callback which is invoked in vmbus_onoffer_rescind().
  699. */
  700. void (*chn_rescind_callback)(struct vmbus_channel *channel);
  701. /*
  702. * The spinlock to protect the structure. It is being used to protect
  703. * test-and-set access to various attributes of the structure as well
  704. * as all sc_list operations.
  705. */
  706. spinlock_t lock;
  707. /*
  708. * All Sub-channels of a primary channel are linked here.
  709. */
  710. struct list_head sc_list;
  711. /*
  712. * Current number of sub-channels.
  713. */
  714. int num_sc;
  715. /*
  716. * Number of a sub-channel (position within sc_list) which is supposed
  717. * to be used as the next outgoing channel.
  718. */
  719. int next_oc;
  720. /*
  721. * The primary channel this sub-channel belongs to.
  722. * This will be NULL for the primary channel.
  723. */
  724. struct vmbus_channel *primary_channel;
  725. /*
  726. * Support per-channel state for use by vmbus drivers.
  727. */
  728. void *per_channel_state;
  729. /*
  730. * To support per-cpu lookup mapping of relid to channel,
  731. * link up channels based on their CPU affinity.
  732. */
  733. struct list_head percpu_list;
  734. /*
  735. * Defer freeing channel until after all cpu's have
  736. * gone through grace period.
  737. */
  738. struct rcu_head rcu;
  739. /*
  740. * For performance critical channels (storage, networking
  741. * etc,), Hyper-V has a mechanism to enhance the throughput
  742. * at the expense of latency:
  743. * When the host is to be signaled, we just set a bit in a shared page
  744. * and this bit will be inspected by the hypervisor within a certain
  745. * window and if the bit is set, the host will be signaled. The window
  746. * of time is the monitor latency - currently around 100 usecs. This
  747. * mechanism improves throughput by:
  748. *
  749. * A) Making the host more efficient - each time it wakes up,
  750. * potentially it will process morev number of packets. The
  751. * monitor latency allows a batch to build up.
  752. * B) By deferring the hypercall to signal, we will also minimize
  753. * the interrupts.
  754. *
  755. * Clearly, these optimizations improve throughput at the expense of
  756. * latency. Furthermore, since the channel is shared for both
  757. * control and data messages, control messages currently suffer
  758. * unnecessary latency adversley impacting performance and boot
  759. * time. To fix this issue, permit tagging the channel as being
  760. * in "low latency" mode. In this mode, we will bypass the monitor
  761. * mechanism.
  762. */
  763. bool low_latency;
  764. /*
  765. * NUMA distribution policy:
  766. * We support teo policies:
  767. * 1) Balanced: Here all performance critical channels are
  768. * distributed evenly amongst all the NUMA nodes.
  769. * This policy will be the default policy.
  770. * 2) Localized: All channels of a given instance of a
  771. * performance critical service will be assigned CPUs
  772. * within a selected NUMA node.
  773. */
  774. enum hv_numa_policy affinity_policy;
  775. };
  776. static inline bool is_hvsock_channel(const struct vmbus_channel *c)
  777. {
  778. return !!(c->offermsg.offer.chn_flags &
  779. VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER);
  780. }
  781. static inline void set_channel_affinity_state(struct vmbus_channel *c,
  782. enum hv_numa_policy policy)
  783. {
  784. c->affinity_policy = policy;
  785. }
  786. static inline void set_channel_read_mode(struct vmbus_channel *c,
  787. enum hv_callback_mode mode)
  788. {
  789. c->callback_mode = mode;
  790. }
  791. static inline void set_per_channel_state(struct vmbus_channel *c, void *s)
  792. {
  793. c->per_channel_state = s;
  794. }
  795. static inline void *get_per_channel_state(struct vmbus_channel *c)
  796. {
  797. return c->per_channel_state;
  798. }
  799. static inline void set_channel_pending_send_size(struct vmbus_channel *c,
  800. u32 size)
  801. {
  802. c->outbound.ring_buffer->pending_send_sz = size;
  803. }
  804. static inline void set_low_latency_mode(struct vmbus_channel *c)
  805. {
  806. c->low_latency = true;
  807. }
  808. static inline void clear_low_latency_mode(struct vmbus_channel *c)
  809. {
  810. c->low_latency = false;
  811. }
  812. void vmbus_onmessage(void *context);
  813. int vmbus_request_offers(void);
  814. /*
  815. * APIs for managing sub-channels.
  816. */
  817. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  818. void (*sc_cr_cb)(struct vmbus_channel *new_sc));
  819. void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
  820. void (*chn_rescind_cb)(struct vmbus_channel *));
  821. /*
  822. * Retrieve the (sub) channel on which to send an outgoing request.
  823. * When a primary channel has multiple sub-channels, we choose a
  824. * channel whose VCPU binding is closest to the VCPU on which
  825. * this call is being made.
  826. */
  827. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary);
  828. /*
  829. * Check if sub-channels have already been offerred. This API will be useful
  830. * when the driver is unloaded after establishing sub-channels. In this case,
  831. * when the driver is re-loaded, the driver would have to check if the
  832. * subchannels have already been established before attempting to request
  833. * the creation of sub-channels.
  834. * This function returns TRUE to indicate that subchannels have already been
  835. * created.
  836. * This function should be invoked after setting the callback function for
  837. * sub-channel creation.
  838. */
  839. bool vmbus_are_subchannels_present(struct vmbus_channel *primary);
  840. /* The format must be the same as struct vmdata_gpa_direct */
  841. struct vmbus_channel_packet_page_buffer {
  842. u16 type;
  843. u16 dataoffset8;
  844. u16 length8;
  845. u16 flags;
  846. u64 transactionid;
  847. u32 reserved;
  848. u32 rangecount;
  849. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  850. } __packed;
  851. /* The format must be the same as struct vmdata_gpa_direct */
  852. struct vmbus_channel_packet_multipage_buffer {
  853. u16 type;
  854. u16 dataoffset8;
  855. u16 length8;
  856. u16 flags;
  857. u64 transactionid;
  858. u32 reserved;
  859. u32 rangecount; /* Always 1 in this case */
  860. struct hv_multipage_buffer range;
  861. } __packed;
  862. /* The format must be the same as struct vmdata_gpa_direct */
  863. struct vmbus_packet_mpb_array {
  864. u16 type;
  865. u16 dataoffset8;
  866. u16 length8;
  867. u16 flags;
  868. u64 transactionid;
  869. u32 reserved;
  870. u32 rangecount; /* Always 1 in this case */
  871. struct hv_mpb_array range;
  872. } __packed;
  873. extern int vmbus_open(struct vmbus_channel *channel,
  874. u32 send_ringbuffersize,
  875. u32 recv_ringbuffersize,
  876. void *userdata,
  877. u32 userdatalen,
  878. void (*onchannel_callback)(void *context),
  879. void *context);
  880. extern void vmbus_close(struct vmbus_channel *channel);
  881. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  882. void *buffer,
  883. u32 bufferLen,
  884. u64 requestid,
  885. enum vmbus_packet_type type,
  886. u32 flags);
  887. extern int vmbus_sendpacket_ctl(struct vmbus_channel *channel,
  888. void *buffer,
  889. u32 bufferLen,
  890. u64 requestid,
  891. enum vmbus_packet_type type,
  892. u32 flags);
  893. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  894. struct hv_page_buffer pagebuffers[],
  895. u32 pagecount,
  896. void *buffer,
  897. u32 bufferlen,
  898. u64 requestid);
  899. extern int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
  900. struct hv_page_buffer pagebuffers[],
  901. u32 pagecount,
  902. void *buffer,
  903. u32 bufferlen,
  904. u64 requestid,
  905. u32 flags);
  906. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  907. struct hv_multipage_buffer *mpb,
  908. void *buffer,
  909. u32 bufferlen,
  910. u64 requestid);
  911. extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  912. struct vmbus_packet_mpb_array *mpb,
  913. u32 desc_size,
  914. void *buffer,
  915. u32 bufferlen,
  916. u64 requestid);
  917. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  918. void *kbuffer,
  919. u32 size,
  920. u32 *gpadl_handle);
  921. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  922. u32 gpadl_handle);
  923. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  924. void *buffer,
  925. u32 bufferlen,
  926. u32 *buffer_actual_len,
  927. u64 *requestid);
  928. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  929. void *buffer,
  930. u32 bufferlen,
  931. u32 *buffer_actual_len,
  932. u64 *requestid);
  933. extern void vmbus_ontimer(unsigned long data);
  934. /* Base driver object */
  935. struct hv_driver {
  936. const char *name;
  937. /*
  938. * A hvsock offer, which has a VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER
  939. * channel flag, actually doesn't mean a synthetic device because the
  940. * offer's if_type/if_instance can change for every new hvsock
  941. * connection.
  942. *
  943. * However, to facilitate the notification of new-offer/rescind-offer
  944. * from vmbus driver to hvsock driver, we can handle hvsock offer as
  945. * a special vmbus device, and hence we need the below flag to
  946. * indicate if the driver is the hvsock driver or not: we need to
  947. * specially treat the hvosck offer & driver in vmbus_match().
  948. */
  949. bool hvsock;
  950. /* the device type supported by this driver */
  951. uuid_le dev_type;
  952. const struct hv_vmbus_device_id *id_table;
  953. struct device_driver driver;
  954. /* dynamic device GUID's */
  955. struct {
  956. spinlock_t lock;
  957. struct list_head list;
  958. } dynids;
  959. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  960. int (*remove)(struct hv_device *);
  961. void (*shutdown)(struct hv_device *);
  962. };
  963. /* Base device object */
  964. struct hv_device {
  965. /* the device type id of this device */
  966. uuid_le dev_type;
  967. /* the device instance id of this device */
  968. uuid_le dev_instance;
  969. u16 vendor_id;
  970. u16 device_id;
  971. struct device device;
  972. struct vmbus_channel *channel;
  973. };
  974. static inline struct hv_device *device_to_hv_device(struct device *d)
  975. {
  976. return container_of(d, struct hv_device, device);
  977. }
  978. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  979. {
  980. return container_of(d, struct hv_driver, driver);
  981. }
  982. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  983. {
  984. dev_set_drvdata(&dev->device, data);
  985. }
  986. static inline void *hv_get_drvdata(struct hv_device *dev)
  987. {
  988. return dev_get_drvdata(&dev->device);
  989. }
  990. struct hv_ring_buffer_debug_info {
  991. u32 current_interrupt_mask;
  992. u32 current_read_index;
  993. u32 current_write_index;
  994. u32 bytes_avail_toread;
  995. u32 bytes_avail_towrite;
  996. };
  997. void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
  998. struct hv_ring_buffer_debug_info *debug_info);
  999. /* Vmbus interface */
  1000. #define vmbus_driver_register(driver) \
  1001. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  1002. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  1003. struct module *owner,
  1004. const char *mod_name);
  1005. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  1006. void vmbus_hvsock_device_unregister(struct vmbus_channel *channel);
  1007. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  1008. resource_size_t min, resource_size_t max,
  1009. resource_size_t size, resource_size_t align,
  1010. bool fb_overlap_ok);
  1011. void vmbus_free_mmio(resource_size_t start, resource_size_t size);
  1012. int vmbus_cpu_number_to_vp_number(int cpu_number);
  1013. u64 hv_do_hypercall(u64 control, void *input, void *output);
  1014. /*
  1015. * GUID definitions of various offer types - services offered to the guest.
  1016. */
  1017. /*
  1018. * Network GUID
  1019. * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
  1020. */
  1021. #define HV_NIC_GUID \
  1022. .guid = UUID_LE(0xf8615163, 0xdf3e, 0x46c5, 0x91, 0x3f, \
  1023. 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e)
  1024. /*
  1025. * IDE GUID
  1026. * {32412632-86cb-44a2-9b5c-50d1417354f5}
  1027. */
  1028. #define HV_IDE_GUID \
  1029. .guid = UUID_LE(0x32412632, 0x86cb, 0x44a2, 0x9b, 0x5c, \
  1030. 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  1031. /*
  1032. * SCSI GUID
  1033. * {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
  1034. */
  1035. #define HV_SCSI_GUID \
  1036. .guid = UUID_LE(0xba6163d9, 0x04a1, 0x4d29, 0xb6, 0x05, \
  1037. 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  1038. /*
  1039. * Shutdown GUID
  1040. * {0e0b6031-5213-4934-818b-38d90ced39db}
  1041. */
  1042. #define HV_SHUTDOWN_GUID \
  1043. .guid = UUID_LE(0x0e0b6031, 0x5213, 0x4934, 0x81, 0x8b, \
  1044. 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb)
  1045. /*
  1046. * Time Synch GUID
  1047. * {9527E630-D0AE-497b-ADCE-E80AB0175CAF}
  1048. */
  1049. #define HV_TS_GUID \
  1050. .guid = UUID_LE(0x9527e630, 0xd0ae, 0x497b, 0xad, 0xce, \
  1051. 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
  1052. /*
  1053. * Heartbeat GUID
  1054. * {57164f39-9115-4e78-ab55-382f3bd5422d}
  1055. */
  1056. #define HV_HEART_BEAT_GUID \
  1057. .guid = UUID_LE(0x57164f39, 0x9115, 0x4e78, 0xab, 0x55, \
  1058. 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
  1059. /*
  1060. * KVP GUID
  1061. * {a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}
  1062. */
  1063. #define HV_KVP_GUID \
  1064. .guid = UUID_LE(0xa9a0f4e7, 0x5a45, 0x4d96, 0xb8, 0x27, \
  1065. 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6)
  1066. /*
  1067. * Dynamic memory GUID
  1068. * {525074dc-8985-46e2-8057-a307dc18a502}
  1069. */
  1070. #define HV_DM_GUID \
  1071. .guid = UUID_LE(0x525074dc, 0x8985, 0x46e2, 0x80, 0x57, \
  1072. 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02)
  1073. /*
  1074. * Mouse GUID
  1075. * {cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}
  1076. */
  1077. #define HV_MOUSE_GUID \
  1078. .guid = UUID_LE(0xcfa8b69e, 0x5b4a, 0x4cc0, 0xb9, 0x8b, \
  1079. 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a)
  1080. /*
  1081. * Keyboard GUID
  1082. * {f912ad6d-2b17-48ea-bd65-f927a61c7684}
  1083. */
  1084. #define HV_KBD_GUID \
  1085. .guid = UUID_LE(0xf912ad6d, 0x2b17, 0x48ea, 0xbd, 0x65, \
  1086. 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84)
  1087. /*
  1088. * VSS (Backup/Restore) GUID
  1089. */
  1090. #define HV_VSS_GUID \
  1091. .guid = UUID_LE(0x35fa2e29, 0xea23, 0x4236, 0x96, 0xae, \
  1092. 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40)
  1093. /*
  1094. * Synthetic Video GUID
  1095. * {DA0A7802-E377-4aac-8E77-0558EB1073F8}
  1096. */
  1097. #define HV_SYNTHVID_GUID \
  1098. .guid = UUID_LE(0xda0a7802, 0xe377, 0x4aac, 0x8e, 0x77, \
  1099. 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8)
  1100. /*
  1101. * Synthetic FC GUID
  1102. * {2f9bcc4a-0069-4af3-b76b-6fd0be528cda}
  1103. */
  1104. #define HV_SYNTHFC_GUID \
  1105. .guid = UUID_LE(0x2f9bcc4a, 0x0069, 0x4af3, 0xb7, 0x6b, \
  1106. 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda)
  1107. /*
  1108. * Guest File Copy Service
  1109. * {34D14BE3-DEE4-41c8-9AE7-6B174977C192}
  1110. */
  1111. #define HV_FCOPY_GUID \
  1112. .guid = UUID_LE(0x34d14be3, 0xdee4, 0x41c8, 0x9a, 0xe7, \
  1113. 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92)
  1114. /*
  1115. * NetworkDirect. This is the guest RDMA service.
  1116. * {8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}
  1117. */
  1118. #define HV_ND_GUID \
  1119. .guid = UUID_LE(0x8c2eaf3d, 0x32a7, 0x4b09, 0xab, 0x99, \
  1120. 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01)
  1121. /*
  1122. * PCI Express Pass Through
  1123. * {44C4F61D-4444-4400-9D52-802E27EDE19F}
  1124. */
  1125. #define HV_PCIE_GUID \
  1126. .guid = UUID_LE(0x44c4f61d, 0x4444, 0x4400, 0x9d, 0x52, \
  1127. 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f)
  1128. /*
  1129. * Linux doesn't support the 3 devices: the first two are for
  1130. * Automatic Virtual Machine Activation, and the third is for
  1131. * Remote Desktop Virtualization.
  1132. * {f8e65716-3cb3-4a06-9a60-1889c5cccab5}
  1133. * {3375baf4-9e15-4b30-b765-67acb10d607b}
  1134. * {276aacf4-ac15-426c-98dd-7521ad3f01fe}
  1135. */
  1136. #define HV_AVMA1_GUID \
  1137. .guid = UUID_LE(0xf8e65716, 0x3cb3, 0x4a06, 0x9a, 0x60, \
  1138. 0x18, 0x89, 0xc5, 0xcc, 0xca, 0xb5)
  1139. #define HV_AVMA2_GUID \
  1140. .guid = UUID_LE(0x3375baf4, 0x9e15, 0x4b30, 0xb7, 0x65, \
  1141. 0x67, 0xac, 0xb1, 0x0d, 0x60, 0x7b)
  1142. #define HV_RDV_GUID \
  1143. .guid = UUID_LE(0x276aacf4, 0xac15, 0x426c, 0x98, 0xdd, \
  1144. 0x75, 0x21, 0xad, 0x3f, 0x01, 0xfe)
  1145. /*
  1146. * Common header for Hyper-V ICs
  1147. */
  1148. #define ICMSGTYPE_NEGOTIATE 0
  1149. #define ICMSGTYPE_HEARTBEAT 1
  1150. #define ICMSGTYPE_KVPEXCHANGE 2
  1151. #define ICMSGTYPE_SHUTDOWN 3
  1152. #define ICMSGTYPE_TIMESYNC 4
  1153. #define ICMSGTYPE_VSS 5
  1154. #define ICMSGHDRFLAG_TRANSACTION 1
  1155. #define ICMSGHDRFLAG_REQUEST 2
  1156. #define ICMSGHDRFLAG_RESPONSE 4
  1157. /*
  1158. * While we want to handle util services as regular devices,
  1159. * there is only one instance of each of these services; so
  1160. * we statically allocate the service specific state.
  1161. */
  1162. struct hv_util_service {
  1163. u8 *recv_buffer;
  1164. void *channel;
  1165. void (*util_cb)(void *);
  1166. int (*util_init)(struct hv_util_service *);
  1167. void (*util_deinit)(void);
  1168. };
  1169. struct vmbuspipe_hdr {
  1170. u32 flags;
  1171. u32 msgsize;
  1172. } __packed;
  1173. struct ic_version {
  1174. u16 major;
  1175. u16 minor;
  1176. } __packed;
  1177. struct icmsg_hdr {
  1178. struct ic_version icverframe;
  1179. u16 icmsgtype;
  1180. struct ic_version icvermsg;
  1181. u16 icmsgsize;
  1182. u32 status;
  1183. u8 ictransaction_id;
  1184. u8 icflags;
  1185. u8 reserved[2];
  1186. } __packed;
  1187. struct icmsg_negotiate {
  1188. u16 icframe_vercnt;
  1189. u16 icmsg_vercnt;
  1190. u32 reserved;
  1191. struct ic_version icversion_data[1]; /* any size array */
  1192. } __packed;
  1193. struct shutdown_msg_data {
  1194. u32 reason_code;
  1195. u32 timeout_seconds;
  1196. u32 flags;
  1197. u8 display_message[2048];
  1198. } __packed;
  1199. struct heartbeat_msg_data {
  1200. u64 seq_num;
  1201. u32 reserved[8];
  1202. } __packed;
  1203. /* Time Sync IC defs */
  1204. #define ICTIMESYNCFLAG_PROBE 0
  1205. #define ICTIMESYNCFLAG_SYNC 1
  1206. #define ICTIMESYNCFLAG_SAMPLE 2
  1207. #ifdef __x86_64__
  1208. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1209. #else
  1210. #define WLTIMEDELTA 116444736000000000LL
  1211. #endif
  1212. struct ictimesync_data {
  1213. u64 parenttime;
  1214. u64 childtime;
  1215. u64 roundtriptime;
  1216. u8 flags;
  1217. } __packed;
  1218. struct ictimesync_ref_data {
  1219. u64 parenttime;
  1220. u64 vmreferencetime;
  1221. u8 flags;
  1222. char leapflags;
  1223. char stratum;
  1224. u8 reserved[3];
  1225. } __packed;
  1226. struct hyperv_service_callback {
  1227. u8 msg_type;
  1228. char *log_msg;
  1229. uuid_le data;
  1230. struct vmbus_channel *channel;
  1231. void (*callback)(void *context);
  1232. };
  1233. #define MAX_SRV_VER 0x7ffffff
  1234. extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf,
  1235. const int *fw_version, int fw_vercnt,
  1236. const int *srv_version, int srv_vercnt,
  1237. int *nego_fw_version, int *nego_srv_version);
  1238. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid);
  1239. void vmbus_setevent(struct vmbus_channel *channel);
  1240. /*
  1241. * Negotiated version with the Host.
  1242. */
  1243. extern __u32 vmbus_proto_version;
  1244. int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
  1245. const uuid_le *shv_host_servie_id);
  1246. void vmbus_set_event(struct vmbus_channel *channel);
  1247. /* Get the start of the ring buffer. */
  1248. static inline void *
  1249. hv_get_ring_buffer(const struct hv_ring_buffer_info *ring_info)
  1250. {
  1251. return ring_info->ring_buffer->buffer;
  1252. }
  1253. /*
  1254. * To optimize the flow management on the send-side,
  1255. * when the sender is blocked because of lack of
  1256. * sufficient space in the ring buffer, potential the
  1257. * consumer of the ring buffer can signal the producer.
  1258. * This is controlled by the following parameters:
  1259. *
  1260. * 1. pending_send_sz: This is the size in bytes that the
  1261. * producer is trying to send.
  1262. * 2. The feature bit feat_pending_send_sz set to indicate if
  1263. * the consumer of the ring will signal when the ring
  1264. * state transitions from being full to a state where
  1265. * there is room for the producer to send the pending packet.
  1266. */
  1267. static inline void hv_signal_on_read(struct vmbus_channel *channel)
  1268. {
  1269. u32 cur_write_sz, cached_write_sz;
  1270. u32 pending_sz;
  1271. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1272. /*
  1273. * Issue a full memory barrier before making the signaling decision.
  1274. * Here is the reason for having this barrier:
  1275. * If the reading of the pend_sz (in this function)
  1276. * were to be reordered and read before we commit the new read
  1277. * index (in the calling function) we could
  1278. * have a problem. If the host were to set the pending_sz after we
  1279. * have sampled pending_sz and go to sleep before we commit the
  1280. * read index, we could miss sending the interrupt. Issue a full
  1281. * memory barrier to address this.
  1282. */
  1283. virt_mb();
  1284. pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
  1285. /* If the other end is not blocked on write don't bother. */
  1286. if (pending_sz == 0)
  1287. return;
  1288. cur_write_sz = hv_get_bytes_to_write(rbi);
  1289. if (cur_write_sz < pending_sz)
  1290. return;
  1291. cached_write_sz = hv_get_cached_bytes_to_write(rbi);
  1292. if (cached_write_sz < pending_sz)
  1293. vmbus_setevent(channel);
  1294. }
  1295. /*
  1296. * Mask off host interrupt callback notifications
  1297. */
  1298. static inline void hv_begin_read(struct hv_ring_buffer_info *rbi)
  1299. {
  1300. rbi->ring_buffer->interrupt_mask = 1;
  1301. /* make sure mask update is not reordered */
  1302. virt_mb();
  1303. }
  1304. /*
  1305. * Re-enable host callback and return number of outstanding bytes
  1306. */
  1307. static inline u32 hv_end_read(struct hv_ring_buffer_info *rbi)
  1308. {
  1309. rbi->ring_buffer->interrupt_mask = 0;
  1310. /* make sure mask update is not reordered */
  1311. virt_mb();
  1312. /*
  1313. * Now check to see if the ring buffer is still empty.
  1314. * If it is not, we raced and we need to process new
  1315. * incoming messages.
  1316. */
  1317. return hv_get_bytes_to_read(rbi);
  1318. }
  1319. /*
  1320. * An API to support in-place processing of incoming VMBUS packets.
  1321. */
  1322. /* Get data payload associated with descriptor */
  1323. static inline void *hv_pkt_data(const struct vmpacket_descriptor *desc)
  1324. {
  1325. return (void *)((unsigned long)desc + (desc->offset8 << 3));
  1326. }
  1327. /* Get data size associated with descriptor */
  1328. static inline u32 hv_pkt_datalen(const struct vmpacket_descriptor *desc)
  1329. {
  1330. return (desc->len8 << 3) - (desc->offset8 << 3);
  1331. }
  1332. struct vmpacket_descriptor *
  1333. hv_pkt_iter_first(struct vmbus_channel *channel);
  1334. struct vmpacket_descriptor *
  1335. __hv_pkt_iter_next(struct vmbus_channel *channel,
  1336. const struct vmpacket_descriptor *pkt);
  1337. void hv_pkt_iter_close(struct vmbus_channel *channel);
  1338. /*
  1339. * Get next packet descriptor from iterator
  1340. * If at end of list, return NULL and update host.
  1341. */
  1342. static inline struct vmpacket_descriptor *
  1343. hv_pkt_iter_next(struct vmbus_channel *channel,
  1344. const struct vmpacket_descriptor *pkt)
  1345. {
  1346. struct vmpacket_descriptor *nxt;
  1347. nxt = __hv_pkt_iter_next(channel, pkt);
  1348. if (!nxt)
  1349. hv_pkt_iter_close(channel);
  1350. return nxt;
  1351. }
  1352. #define foreach_vmbus_pkt(pkt, channel) \
  1353. for (pkt = hv_pkt_iter_first(channel); pkt; \
  1354. pkt = hv_pkt_iter_next(channel, pkt))
  1355. #endif /* _HYPERV_H */