hyperv.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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(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(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(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. /*
  423. * Request Offer -- no parameters, SynIC message contains the partition ID
  424. * Set Snoop -- no parameters, SynIC message contains the partition ID
  425. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  426. * All Offers Delivered -- no parameters, SynIC message contains the partition
  427. * ID
  428. * Flush Client -- no parameters, SynIC message contains the partition ID
  429. */
  430. /* Open Channel parameters */
  431. struct vmbus_channel_open_channel {
  432. struct vmbus_channel_message_header header;
  433. /* Identifies the specific VMBus channel that is being opened. */
  434. u32 child_relid;
  435. /* ID making a particular open request at a channel offer unique. */
  436. u32 openid;
  437. /* GPADL for the channel's ring buffer. */
  438. u32 ringbuffer_gpadlhandle;
  439. /*
  440. * Starting with win8, this field will be used to specify
  441. * the target virtual processor on which to deliver the interrupt for
  442. * the host to guest communication.
  443. * Prior to win8, incoming channel interrupts would only
  444. * be delivered on cpu 0. Setting this value to 0 would
  445. * preserve the earlier behavior.
  446. */
  447. u32 target_vp;
  448. /*
  449. * The upstream ring buffer begins at offset zero in the memory
  450. * described by RingBufferGpadlHandle. The downstream ring buffer
  451. * follows it at this offset (in pages).
  452. */
  453. u32 downstream_ringbuffer_pageoffset;
  454. /* User-specific data to be passed along to the server endpoint. */
  455. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  456. } __packed;
  457. /* Open Channel Result parameters */
  458. struct vmbus_channel_open_result {
  459. struct vmbus_channel_message_header header;
  460. u32 child_relid;
  461. u32 openid;
  462. u32 status;
  463. } __packed;
  464. /* Close channel parameters; */
  465. struct vmbus_channel_close_channel {
  466. struct vmbus_channel_message_header header;
  467. u32 child_relid;
  468. } __packed;
  469. /* Channel Message GPADL */
  470. #define GPADL_TYPE_RING_BUFFER 1
  471. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  472. #define GPADL_TYPE_TRANSACTION 8
  473. /*
  474. * The number of PFNs in a GPADL message is defined by the number of
  475. * pages that would be spanned by ByteCount and ByteOffset. If the
  476. * implied number of PFNs won't fit in this packet, there will be a
  477. * follow-up packet that contains more.
  478. */
  479. struct vmbus_channel_gpadl_header {
  480. struct vmbus_channel_message_header header;
  481. u32 child_relid;
  482. u32 gpadl;
  483. u16 range_buflen;
  484. u16 rangecount;
  485. struct gpa_range range[0];
  486. } __packed;
  487. /* This is the followup packet that contains more PFNs. */
  488. struct vmbus_channel_gpadl_body {
  489. struct vmbus_channel_message_header header;
  490. u32 msgnumber;
  491. u32 gpadl;
  492. u64 pfn[0];
  493. } __packed;
  494. struct vmbus_channel_gpadl_created {
  495. struct vmbus_channel_message_header header;
  496. u32 child_relid;
  497. u32 gpadl;
  498. u32 creation_status;
  499. } __packed;
  500. struct vmbus_channel_gpadl_teardown {
  501. struct vmbus_channel_message_header header;
  502. u32 child_relid;
  503. u32 gpadl;
  504. } __packed;
  505. struct vmbus_channel_gpadl_torndown {
  506. struct vmbus_channel_message_header header;
  507. u32 gpadl;
  508. } __packed;
  509. struct vmbus_channel_relid_released {
  510. struct vmbus_channel_message_header header;
  511. u32 child_relid;
  512. } __packed;
  513. struct vmbus_channel_initiate_contact {
  514. struct vmbus_channel_message_header header;
  515. u32 vmbus_version_requested;
  516. u32 target_vcpu; /* The VCPU the host should respond to */
  517. u64 interrupt_page;
  518. u64 monitor_page1;
  519. u64 monitor_page2;
  520. } __packed;
  521. /* Hyper-V socket: guest's connect()-ing to host */
  522. struct vmbus_channel_tl_connect_request {
  523. struct vmbus_channel_message_header header;
  524. uuid_le guest_endpoint_id;
  525. uuid_le host_service_id;
  526. } __packed;
  527. struct vmbus_channel_version_response {
  528. struct vmbus_channel_message_header header;
  529. u8 version_supported;
  530. } __packed;
  531. enum vmbus_channel_state {
  532. CHANNEL_OFFER_STATE,
  533. CHANNEL_OPENING_STATE,
  534. CHANNEL_OPEN_STATE,
  535. CHANNEL_OPENED_STATE,
  536. };
  537. /*
  538. * Represents each channel msg on the vmbus connection This is a
  539. * variable-size data structure depending on the msg type itself
  540. */
  541. struct vmbus_channel_msginfo {
  542. /* Bookkeeping stuff */
  543. struct list_head msglistentry;
  544. /* So far, this is only used to handle gpadl body message */
  545. struct list_head submsglist;
  546. /* Synchronize the request/response if needed */
  547. struct completion waitevent;
  548. struct vmbus_channel *waiting_channel;
  549. union {
  550. struct vmbus_channel_version_supported version_supported;
  551. struct vmbus_channel_open_result open_result;
  552. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  553. struct vmbus_channel_gpadl_created gpadl_created;
  554. struct vmbus_channel_version_response version_response;
  555. } response;
  556. u32 msgsize;
  557. /*
  558. * The channel message that goes out on the "wire".
  559. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  560. */
  561. unsigned char msg[0];
  562. };
  563. struct vmbus_close_msg {
  564. struct vmbus_channel_msginfo info;
  565. struct vmbus_channel_close_channel msg;
  566. };
  567. /* Define connection identifier type. */
  568. union hv_connection_id {
  569. u32 asu32;
  570. struct {
  571. u32 id:24;
  572. u32 reserved:8;
  573. } u;
  574. };
  575. /* Definition of the hv_signal_event hypercall input structure. */
  576. struct hv_input_signal_event {
  577. union hv_connection_id connectionid;
  578. u16 flag_number;
  579. u16 rsvdz;
  580. };
  581. struct hv_input_signal_event_buffer {
  582. u64 align8;
  583. struct hv_input_signal_event event;
  584. };
  585. enum hv_numa_policy {
  586. HV_BALANCED = 0,
  587. HV_LOCALIZED,
  588. };
  589. enum vmbus_device_type {
  590. HV_IDE = 0,
  591. HV_SCSI,
  592. HV_FC,
  593. HV_NIC,
  594. HV_ND,
  595. HV_PCIE,
  596. HV_FB,
  597. HV_KBD,
  598. HV_MOUSE,
  599. HV_KVP,
  600. HV_TS,
  601. HV_HB,
  602. HV_SHUTDOWN,
  603. HV_FCOPY,
  604. HV_BACKUP,
  605. HV_DM,
  606. HV_UNKNOWN,
  607. };
  608. struct vmbus_device {
  609. u16 dev_type;
  610. uuid_le guid;
  611. bool perf_device;
  612. };
  613. struct vmbus_channel {
  614. struct list_head listentry;
  615. struct hv_device *device_obj;
  616. enum vmbus_channel_state state;
  617. struct vmbus_channel_offer_channel offermsg;
  618. /*
  619. * These are based on the OfferMsg.MonitorId.
  620. * Save it here for easy access.
  621. */
  622. u8 monitor_grp;
  623. u8 monitor_bit;
  624. bool rescind; /* got rescind msg */
  625. u32 ringbuffer_gpadlhandle;
  626. /* Allocated memory for ring buffer */
  627. void *ringbuffer_pages;
  628. u32 ringbuffer_pagecount;
  629. struct hv_ring_buffer_info outbound; /* send to parent */
  630. struct hv_ring_buffer_info inbound; /* receive from parent */
  631. spinlock_t inbound_lock;
  632. struct vmbus_close_msg close_msg;
  633. /* Channel callback's invoked in softirq context */
  634. struct tasklet_struct callback_event;
  635. void (*onchannel_callback)(void *context);
  636. void *channel_callback_context;
  637. /*
  638. * A channel can be marked for one of three modes of reading:
  639. * BATCHED - callback called from taslket and should read
  640. * channel until empty. Interrupts from the host
  641. * are masked while read is in process (default).
  642. * DIRECT - callback called from tasklet (softirq).
  643. * ISR - callback called in interrupt context and must
  644. * invoke its own deferred processing.
  645. * Host interrupts are disabled and must be re-enabled
  646. * when ring is empty.
  647. */
  648. enum hv_callback_mode {
  649. HV_CALL_BATCHED,
  650. HV_CALL_DIRECT,
  651. HV_CALL_ISR
  652. } callback_mode;
  653. bool is_dedicated_interrupt;
  654. struct hv_input_signal_event_buffer sig_buf;
  655. struct hv_input_signal_event *sig_event;
  656. /*
  657. * Starting with win8, this field will be used to specify
  658. * the target virtual processor on which to deliver the interrupt for
  659. * the host to guest communication.
  660. * Prior to win8, incoming channel interrupts would only
  661. * be delivered on cpu 0. Setting this value to 0 would
  662. * preserve the earlier behavior.
  663. */
  664. u32 target_vp;
  665. /* The corresponding CPUID in the guest */
  666. u32 target_cpu;
  667. /*
  668. * State to manage the CPU affiliation of channels.
  669. */
  670. struct cpumask alloced_cpus_in_node;
  671. int numa_node;
  672. /*
  673. * Support for sub-channels. For high performance devices,
  674. * it will be useful to have multiple sub-channels to support
  675. * a scalable communication infrastructure with the host.
  676. * The support for sub-channels is implemented as an extention
  677. * to the current infrastructure.
  678. * The initial offer is considered the primary channel and this
  679. * offer message will indicate if the host supports sub-channels.
  680. * The guest is free to ask for sub-channels to be offerred and can
  681. * open these sub-channels as a normal "primary" channel. However,
  682. * all sub-channels will have the same type and instance guids as the
  683. * primary channel. Requests sent on a given channel will result in a
  684. * response on the same channel.
  685. */
  686. /*
  687. * Sub-channel creation callback. This callback will be called in
  688. * process context when a sub-channel offer is received from the host.
  689. * The guest can open the sub-channel in the context of this callback.
  690. */
  691. void (*sc_creation_callback)(struct vmbus_channel *new_sc);
  692. /*
  693. * Channel rescind callback. Some channels (the hvsock ones), need to
  694. * register a callback which is invoked in vmbus_onoffer_rescind().
  695. */
  696. void (*chn_rescind_callback)(struct vmbus_channel *channel);
  697. /*
  698. * The spinlock to protect the structure. It is being used to protect
  699. * test-and-set access to various attributes of the structure as well
  700. * as all sc_list operations.
  701. */
  702. spinlock_t lock;
  703. /*
  704. * All Sub-channels of a primary channel are linked here.
  705. */
  706. struct list_head sc_list;
  707. /*
  708. * Current number of sub-channels.
  709. */
  710. int num_sc;
  711. /*
  712. * Number of a sub-channel (position within sc_list) which is supposed
  713. * to be used as the next outgoing channel.
  714. */
  715. int next_oc;
  716. /*
  717. * The primary channel this sub-channel belongs to.
  718. * This will be NULL for the primary channel.
  719. */
  720. struct vmbus_channel *primary_channel;
  721. /*
  722. * Support per-channel state for use by vmbus drivers.
  723. */
  724. void *per_channel_state;
  725. /*
  726. * To support per-cpu lookup mapping of relid to channel,
  727. * link up channels based on their CPU affinity.
  728. */
  729. struct list_head percpu_list;
  730. /*
  731. * For performance critical channels (storage, networking
  732. * etc,), Hyper-V has a mechanism to enhance the throughput
  733. * at the expense of latency:
  734. * When the host is to be signaled, we just set a bit in a shared page
  735. * and this bit will be inspected by the hypervisor within a certain
  736. * window and if the bit is set, the host will be signaled. The window
  737. * of time is the monitor latency - currently around 100 usecs. This
  738. * mechanism improves throughput by:
  739. *
  740. * A) Making the host more efficient - each time it wakes up,
  741. * potentially it will process morev number of packets. The
  742. * monitor latency allows a batch to build up.
  743. * B) By deferring the hypercall to signal, we will also minimize
  744. * the interrupts.
  745. *
  746. * Clearly, these optimizations improve throughput at the expense of
  747. * latency. Furthermore, since the channel is shared for both
  748. * control and data messages, control messages currently suffer
  749. * unnecessary latency adversley impacting performance and boot
  750. * time. To fix this issue, permit tagging the channel as being
  751. * in "low latency" mode. In this mode, we will bypass the monitor
  752. * mechanism.
  753. */
  754. bool low_latency;
  755. /*
  756. * NUMA distribution policy:
  757. * We support teo policies:
  758. * 1) Balanced: Here all performance critical channels are
  759. * distributed evenly amongst all the NUMA nodes.
  760. * This policy will be the default policy.
  761. * 2) Localized: All channels of a given instance of a
  762. * performance critical service will be assigned CPUs
  763. * within a selected NUMA node.
  764. */
  765. enum hv_numa_policy affinity_policy;
  766. };
  767. static inline bool is_hvsock_channel(const struct vmbus_channel *c)
  768. {
  769. return !!(c->offermsg.offer.chn_flags &
  770. VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER);
  771. }
  772. static inline void set_channel_affinity_state(struct vmbus_channel *c,
  773. enum hv_numa_policy policy)
  774. {
  775. c->affinity_policy = policy;
  776. }
  777. static inline void set_channel_read_mode(struct vmbus_channel *c,
  778. enum hv_callback_mode mode)
  779. {
  780. c->callback_mode = mode;
  781. }
  782. static inline void set_per_channel_state(struct vmbus_channel *c, void *s)
  783. {
  784. c->per_channel_state = s;
  785. }
  786. static inline void *get_per_channel_state(struct vmbus_channel *c)
  787. {
  788. return c->per_channel_state;
  789. }
  790. static inline void set_channel_pending_send_size(struct vmbus_channel *c,
  791. u32 size)
  792. {
  793. c->outbound.ring_buffer->pending_send_sz = size;
  794. }
  795. static inline void set_low_latency_mode(struct vmbus_channel *c)
  796. {
  797. c->low_latency = true;
  798. }
  799. static inline void clear_low_latency_mode(struct vmbus_channel *c)
  800. {
  801. c->low_latency = false;
  802. }
  803. void vmbus_onmessage(void *context);
  804. int vmbus_request_offers(void);
  805. /*
  806. * APIs for managing sub-channels.
  807. */
  808. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  809. void (*sc_cr_cb)(struct vmbus_channel *new_sc));
  810. void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
  811. void (*chn_rescind_cb)(struct vmbus_channel *));
  812. /*
  813. * Retrieve the (sub) channel on which to send an outgoing request.
  814. * When a primary channel has multiple sub-channels, we choose a
  815. * channel whose VCPU binding is closest to the VCPU on which
  816. * this call is being made.
  817. */
  818. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary);
  819. /*
  820. * Check if sub-channels have already been offerred. This API will be useful
  821. * when the driver is unloaded after establishing sub-channels. In this case,
  822. * when the driver is re-loaded, the driver would have to check if the
  823. * subchannels have already been established before attempting to request
  824. * the creation of sub-channels.
  825. * This function returns TRUE to indicate that subchannels have already been
  826. * created.
  827. * This function should be invoked after setting the callback function for
  828. * sub-channel creation.
  829. */
  830. bool vmbus_are_subchannels_present(struct vmbus_channel *primary);
  831. /* The format must be the same as struct vmdata_gpa_direct */
  832. struct vmbus_channel_packet_page_buffer {
  833. u16 type;
  834. u16 dataoffset8;
  835. u16 length8;
  836. u16 flags;
  837. u64 transactionid;
  838. u32 reserved;
  839. u32 rangecount;
  840. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  841. } __packed;
  842. /* The format must be the same as struct vmdata_gpa_direct */
  843. struct vmbus_channel_packet_multipage_buffer {
  844. u16 type;
  845. u16 dataoffset8;
  846. u16 length8;
  847. u16 flags;
  848. u64 transactionid;
  849. u32 reserved;
  850. u32 rangecount; /* Always 1 in this case */
  851. struct hv_multipage_buffer range;
  852. } __packed;
  853. /* The format must be the same as struct vmdata_gpa_direct */
  854. struct vmbus_packet_mpb_array {
  855. u16 type;
  856. u16 dataoffset8;
  857. u16 length8;
  858. u16 flags;
  859. u64 transactionid;
  860. u32 reserved;
  861. u32 rangecount; /* Always 1 in this case */
  862. struct hv_mpb_array range;
  863. } __packed;
  864. extern int vmbus_open(struct vmbus_channel *channel,
  865. u32 send_ringbuffersize,
  866. u32 recv_ringbuffersize,
  867. void *userdata,
  868. u32 userdatalen,
  869. void(*onchannel_callback)(void *context),
  870. void *context);
  871. extern void vmbus_close(struct vmbus_channel *channel);
  872. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  873. void *buffer,
  874. u32 bufferLen,
  875. u64 requestid,
  876. enum vmbus_packet_type type,
  877. u32 flags);
  878. extern int vmbus_sendpacket_ctl(struct vmbus_channel *channel,
  879. void *buffer,
  880. u32 bufferLen,
  881. u64 requestid,
  882. enum vmbus_packet_type type,
  883. u32 flags);
  884. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  885. struct hv_page_buffer pagebuffers[],
  886. u32 pagecount,
  887. void *buffer,
  888. u32 bufferlen,
  889. u64 requestid);
  890. extern int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
  891. struct hv_page_buffer pagebuffers[],
  892. u32 pagecount,
  893. void *buffer,
  894. u32 bufferlen,
  895. u64 requestid,
  896. u32 flags);
  897. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  898. struct hv_multipage_buffer *mpb,
  899. void *buffer,
  900. u32 bufferlen,
  901. u64 requestid);
  902. extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  903. struct vmbus_packet_mpb_array *mpb,
  904. u32 desc_size,
  905. void *buffer,
  906. u32 bufferlen,
  907. u64 requestid);
  908. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  909. void *kbuffer,
  910. u32 size,
  911. u32 *gpadl_handle);
  912. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  913. u32 gpadl_handle);
  914. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  915. void *buffer,
  916. u32 bufferlen,
  917. u32 *buffer_actual_len,
  918. u64 *requestid);
  919. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  920. void *buffer,
  921. u32 bufferlen,
  922. u32 *buffer_actual_len,
  923. u64 *requestid);
  924. extern void vmbus_ontimer(unsigned long data);
  925. /* Base driver object */
  926. struct hv_driver {
  927. const char *name;
  928. /*
  929. * A hvsock offer, which has a VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER
  930. * channel flag, actually doesn't mean a synthetic device because the
  931. * offer's if_type/if_instance can change for every new hvsock
  932. * connection.
  933. *
  934. * However, to facilitate the notification of new-offer/rescind-offer
  935. * from vmbus driver to hvsock driver, we can handle hvsock offer as
  936. * a special vmbus device, and hence we need the below flag to
  937. * indicate if the driver is the hvsock driver or not: we need to
  938. * specially treat the hvosck offer & driver in vmbus_match().
  939. */
  940. bool hvsock;
  941. /* the device type supported by this driver */
  942. uuid_le dev_type;
  943. const struct hv_vmbus_device_id *id_table;
  944. struct device_driver driver;
  945. /* dynamic device GUID's */
  946. struct {
  947. spinlock_t lock;
  948. struct list_head list;
  949. } dynids;
  950. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  951. int (*remove)(struct hv_device *);
  952. void (*shutdown)(struct hv_device *);
  953. };
  954. /* Base device object */
  955. struct hv_device {
  956. /* the device type id of this device */
  957. uuid_le dev_type;
  958. /* the device instance id of this device */
  959. uuid_le dev_instance;
  960. u16 vendor_id;
  961. u16 device_id;
  962. struct device device;
  963. struct vmbus_channel *channel;
  964. };
  965. static inline struct hv_device *device_to_hv_device(struct device *d)
  966. {
  967. return container_of(d, struct hv_device, device);
  968. }
  969. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  970. {
  971. return container_of(d, struct hv_driver, driver);
  972. }
  973. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  974. {
  975. dev_set_drvdata(&dev->device, data);
  976. }
  977. static inline void *hv_get_drvdata(struct hv_device *dev)
  978. {
  979. return dev_get_drvdata(&dev->device);
  980. }
  981. /* Vmbus interface */
  982. #define vmbus_driver_register(driver) \
  983. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  984. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  985. struct module *owner,
  986. const char *mod_name);
  987. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  988. void vmbus_hvsock_device_unregister(struct vmbus_channel *channel);
  989. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  990. resource_size_t min, resource_size_t max,
  991. resource_size_t size, resource_size_t align,
  992. bool fb_overlap_ok);
  993. void vmbus_free_mmio(resource_size_t start, resource_size_t size);
  994. int vmbus_cpu_number_to_vp_number(int cpu_number);
  995. u64 hv_do_hypercall(u64 control, void *input, void *output);
  996. /*
  997. * GUID definitions of various offer types - services offered to the guest.
  998. */
  999. /*
  1000. * Network GUID
  1001. * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
  1002. */
  1003. #define HV_NIC_GUID \
  1004. .guid = UUID_LE(0xf8615163, 0xdf3e, 0x46c5, 0x91, 0x3f, \
  1005. 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e)
  1006. /*
  1007. * IDE GUID
  1008. * {32412632-86cb-44a2-9b5c-50d1417354f5}
  1009. */
  1010. #define HV_IDE_GUID \
  1011. .guid = UUID_LE(0x32412632, 0x86cb, 0x44a2, 0x9b, 0x5c, \
  1012. 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  1013. /*
  1014. * SCSI GUID
  1015. * {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
  1016. */
  1017. #define HV_SCSI_GUID \
  1018. .guid = UUID_LE(0xba6163d9, 0x04a1, 0x4d29, 0xb6, 0x05, \
  1019. 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  1020. /*
  1021. * Shutdown GUID
  1022. * {0e0b6031-5213-4934-818b-38d90ced39db}
  1023. */
  1024. #define HV_SHUTDOWN_GUID \
  1025. .guid = UUID_LE(0x0e0b6031, 0x5213, 0x4934, 0x81, 0x8b, \
  1026. 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb)
  1027. /*
  1028. * Time Synch GUID
  1029. * {9527E630-D0AE-497b-ADCE-E80AB0175CAF}
  1030. */
  1031. #define HV_TS_GUID \
  1032. .guid = UUID_LE(0x9527e630, 0xd0ae, 0x497b, 0xad, 0xce, \
  1033. 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
  1034. /*
  1035. * Heartbeat GUID
  1036. * {57164f39-9115-4e78-ab55-382f3bd5422d}
  1037. */
  1038. #define HV_HEART_BEAT_GUID \
  1039. .guid = UUID_LE(0x57164f39, 0x9115, 0x4e78, 0xab, 0x55, \
  1040. 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
  1041. /*
  1042. * KVP GUID
  1043. * {a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}
  1044. */
  1045. #define HV_KVP_GUID \
  1046. .guid = UUID_LE(0xa9a0f4e7, 0x5a45, 0x4d96, 0xb8, 0x27, \
  1047. 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6)
  1048. /*
  1049. * Dynamic memory GUID
  1050. * {525074dc-8985-46e2-8057-a307dc18a502}
  1051. */
  1052. #define HV_DM_GUID \
  1053. .guid = UUID_LE(0x525074dc, 0x8985, 0x46e2, 0x80, 0x57, \
  1054. 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02)
  1055. /*
  1056. * Mouse GUID
  1057. * {cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}
  1058. */
  1059. #define HV_MOUSE_GUID \
  1060. .guid = UUID_LE(0xcfa8b69e, 0x5b4a, 0x4cc0, 0xb9, 0x8b, \
  1061. 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a)
  1062. /*
  1063. * Keyboard GUID
  1064. * {f912ad6d-2b17-48ea-bd65-f927a61c7684}
  1065. */
  1066. #define HV_KBD_GUID \
  1067. .guid = UUID_LE(0xf912ad6d, 0x2b17, 0x48ea, 0xbd, 0x65, \
  1068. 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84)
  1069. /*
  1070. * VSS (Backup/Restore) GUID
  1071. */
  1072. #define HV_VSS_GUID \
  1073. .guid = UUID_LE(0x35fa2e29, 0xea23, 0x4236, 0x96, 0xae, \
  1074. 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40)
  1075. /*
  1076. * Synthetic Video GUID
  1077. * {DA0A7802-E377-4aac-8E77-0558EB1073F8}
  1078. */
  1079. #define HV_SYNTHVID_GUID \
  1080. .guid = UUID_LE(0xda0a7802, 0xe377, 0x4aac, 0x8e, 0x77, \
  1081. 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8)
  1082. /*
  1083. * Synthetic FC GUID
  1084. * {2f9bcc4a-0069-4af3-b76b-6fd0be528cda}
  1085. */
  1086. #define HV_SYNTHFC_GUID \
  1087. .guid = UUID_LE(0x2f9bcc4a, 0x0069, 0x4af3, 0xb7, 0x6b, \
  1088. 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda)
  1089. /*
  1090. * Guest File Copy Service
  1091. * {34D14BE3-DEE4-41c8-9AE7-6B174977C192}
  1092. */
  1093. #define HV_FCOPY_GUID \
  1094. .guid = UUID_LE(0x34d14be3, 0xdee4, 0x41c8, 0x9a, 0xe7, \
  1095. 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92)
  1096. /*
  1097. * NetworkDirect. This is the guest RDMA service.
  1098. * {8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}
  1099. */
  1100. #define HV_ND_GUID \
  1101. .guid = UUID_LE(0x8c2eaf3d, 0x32a7, 0x4b09, 0xab, 0x99, \
  1102. 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01)
  1103. /*
  1104. * PCI Express Pass Through
  1105. * {44C4F61D-4444-4400-9D52-802E27EDE19F}
  1106. */
  1107. #define HV_PCIE_GUID \
  1108. .guid = UUID_LE(0x44c4f61d, 0x4444, 0x4400, 0x9d, 0x52, \
  1109. 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f)
  1110. /*
  1111. * Linux doesn't support the 3 devices: the first two are for
  1112. * Automatic Virtual Machine Activation, and the third is for
  1113. * Remote Desktop Virtualization.
  1114. * {f8e65716-3cb3-4a06-9a60-1889c5cccab5}
  1115. * {3375baf4-9e15-4b30-b765-67acb10d607b}
  1116. * {276aacf4-ac15-426c-98dd-7521ad3f01fe}
  1117. */
  1118. #define HV_AVMA1_GUID \
  1119. .guid = UUID_LE(0xf8e65716, 0x3cb3, 0x4a06, 0x9a, 0x60, \
  1120. 0x18, 0x89, 0xc5, 0xcc, 0xca, 0xb5)
  1121. #define HV_AVMA2_GUID \
  1122. .guid = UUID_LE(0x3375baf4, 0x9e15, 0x4b30, 0xb7, 0x65, \
  1123. 0x67, 0xac, 0xb1, 0x0d, 0x60, 0x7b)
  1124. #define HV_RDV_GUID \
  1125. .guid = UUID_LE(0x276aacf4, 0xac15, 0x426c, 0x98, 0xdd, \
  1126. 0x75, 0x21, 0xad, 0x3f, 0x01, 0xfe)
  1127. /*
  1128. * Common header for Hyper-V ICs
  1129. */
  1130. #define ICMSGTYPE_NEGOTIATE 0
  1131. #define ICMSGTYPE_HEARTBEAT 1
  1132. #define ICMSGTYPE_KVPEXCHANGE 2
  1133. #define ICMSGTYPE_SHUTDOWN 3
  1134. #define ICMSGTYPE_TIMESYNC 4
  1135. #define ICMSGTYPE_VSS 5
  1136. #define ICMSGHDRFLAG_TRANSACTION 1
  1137. #define ICMSGHDRFLAG_REQUEST 2
  1138. #define ICMSGHDRFLAG_RESPONSE 4
  1139. /*
  1140. * While we want to handle util services as regular devices,
  1141. * there is only one instance of each of these services; so
  1142. * we statically allocate the service specific state.
  1143. */
  1144. struct hv_util_service {
  1145. u8 *recv_buffer;
  1146. void *channel;
  1147. void (*util_cb)(void *);
  1148. int (*util_init)(struct hv_util_service *);
  1149. void (*util_deinit)(void);
  1150. };
  1151. struct vmbuspipe_hdr {
  1152. u32 flags;
  1153. u32 msgsize;
  1154. } __packed;
  1155. struct ic_version {
  1156. u16 major;
  1157. u16 minor;
  1158. } __packed;
  1159. struct icmsg_hdr {
  1160. struct ic_version icverframe;
  1161. u16 icmsgtype;
  1162. struct ic_version icvermsg;
  1163. u16 icmsgsize;
  1164. u32 status;
  1165. u8 ictransaction_id;
  1166. u8 icflags;
  1167. u8 reserved[2];
  1168. } __packed;
  1169. struct icmsg_negotiate {
  1170. u16 icframe_vercnt;
  1171. u16 icmsg_vercnt;
  1172. u32 reserved;
  1173. struct ic_version icversion_data[1]; /* any size array */
  1174. } __packed;
  1175. struct shutdown_msg_data {
  1176. u32 reason_code;
  1177. u32 timeout_seconds;
  1178. u32 flags;
  1179. u8 display_message[2048];
  1180. } __packed;
  1181. struct heartbeat_msg_data {
  1182. u64 seq_num;
  1183. u32 reserved[8];
  1184. } __packed;
  1185. /* Time Sync IC defs */
  1186. #define ICTIMESYNCFLAG_PROBE 0
  1187. #define ICTIMESYNCFLAG_SYNC 1
  1188. #define ICTIMESYNCFLAG_SAMPLE 2
  1189. #ifdef __x86_64__
  1190. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1191. #else
  1192. #define WLTIMEDELTA 116444736000000000LL
  1193. #endif
  1194. struct ictimesync_data {
  1195. u64 parenttime;
  1196. u64 childtime;
  1197. u64 roundtriptime;
  1198. u8 flags;
  1199. } __packed;
  1200. struct ictimesync_ref_data {
  1201. u64 parenttime;
  1202. u64 vmreferencetime;
  1203. u8 flags;
  1204. char leapflags;
  1205. char stratum;
  1206. u8 reserved[3];
  1207. } __packed;
  1208. struct hyperv_service_callback {
  1209. u8 msg_type;
  1210. char *log_msg;
  1211. uuid_le data;
  1212. struct vmbus_channel *channel;
  1213. void (*callback) (void *context);
  1214. };
  1215. #define MAX_SRV_VER 0x7ffffff
  1216. extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf,
  1217. const int *fw_version, int fw_vercnt,
  1218. const int *srv_version, int srv_vercnt,
  1219. int *nego_fw_version, int *nego_srv_version);
  1220. void hv_event_tasklet_disable(struct vmbus_channel *channel);
  1221. void hv_event_tasklet_enable(struct vmbus_channel *channel);
  1222. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid);
  1223. void vmbus_setevent(struct vmbus_channel *channel);
  1224. /*
  1225. * Negotiated version with the Host.
  1226. */
  1227. extern __u32 vmbus_proto_version;
  1228. int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
  1229. const uuid_le *shv_host_servie_id);
  1230. void vmbus_set_event(struct vmbus_channel *channel);
  1231. /* Get the start of the ring buffer. */
  1232. static inline void *
  1233. hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
  1234. {
  1235. return (void *)ring_info->ring_buffer->buffer;
  1236. }
  1237. /*
  1238. * To optimize the flow management on the send-side,
  1239. * when the sender is blocked because of lack of
  1240. * sufficient space in the ring buffer, potential the
  1241. * consumer of the ring buffer can signal the producer.
  1242. * This is controlled by the following parameters:
  1243. *
  1244. * 1. pending_send_sz: This is the size in bytes that the
  1245. * producer is trying to send.
  1246. * 2. The feature bit feat_pending_send_sz set to indicate if
  1247. * the consumer of the ring will signal when the ring
  1248. * state transitions from being full to a state where
  1249. * there is room for the producer to send the pending packet.
  1250. */
  1251. static inline void hv_signal_on_read(struct vmbus_channel *channel)
  1252. {
  1253. u32 cur_write_sz, cached_write_sz;
  1254. u32 pending_sz;
  1255. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1256. /*
  1257. * Issue a full memory barrier before making the signaling decision.
  1258. * Here is the reason for having this barrier:
  1259. * If the reading of the pend_sz (in this function)
  1260. * were to be reordered and read before we commit the new read
  1261. * index (in the calling function) we could
  1262. * have a problem. If the host were to set the pending_sz after we
  1263. * have sampled pending_sz and go to sleep before we commit the
  1264. * read index, we could miss sending the interrupt. Issue a full
  1265. * memory barrier to address this.
  1266. */
  1267. virt_mb();
  1268. pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
  1269. /* If the other end is not blocked on write don't bother. */
  1270. if (pending_sz == 0)
  1271. return;
  1272. cur_write_sz = hv_get_bytes_to_write(rbi);
  1273. if (cur_write_sz < pending_sz)
  1274. return;
  1275. cached_write_sz = hv_get_cached_bytes_to_write(rbi);
  1276. if (cached_write_sz < pending_sz)
  1277. vmbus_setevent(channel);
  1278. return;
  1279. }
  1280. static inline void
  1281. init_cached_read_index(struct vmbus_channel *channel)
  1282. {
  1283. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1284. rbi->cached_read_index = rbi->ring_buffer->read_index;
  1285. }
  1286. /*
  1287. * Mask off host interrupt callback notifications
  1288. */
  1289. static inline void hv_begin_read(struct hv_ring_buffer_info *rbi)
  1290. {
  1291. rbi->ring_buffer->interrupt_mask = 1;
  1292. /* make sure mask update is not reordered */
  1293. virt_mb();
  1294. }
  1295. /*
  1296. * Re-enable host callback and return number of outstanding bytes
  1297. */
  1298. static inline u32 hv_end_read(struct hv_ring_buffer_info *rbi)
  1299. {
  1300. rbi->ring_buffer->interrupt_mask = 0;
  1301. /* make sure mask update is not reordered */
  1302. virt_mb();
  1303. /*
  1304. * Now check to see if the ring buffer is still empty.
  1305. * If it is not, we raced and we need to process new
  1306. * incoming messages.
  1307. */
  1308. return hv_get_bytes_to_read(rbi);
  1309. }
  1310. /*
  1311. * An API to support in-place processing of incoming VMBUS packets.
  1312. */
  1313. #define VMBUS_PKT_TRAILER 8
  1314. static inline struct vmpacket_descriptor *
  1315. get_next_pkt_raw(struct vmbus_channel *channel)
  1316. {
  1317. struct hv_ring_buffer_info *ring_info = &channel->inbound;
  1318. u32 priv_read_loc = ring_info->priv_read_index;
  1319. void *ring_buffer = hv_get_ring_buffer(ring_info);
  1320. u32 dsize = ring_info->ring_datasize;
  1321. /*
  1322. * delta is the difference between what is available to read and
  1323. * what was already consumed in place. We commit read index after
  1324. * the whole batch is processed.
  1325. */
  1326. u32 delta = priv_read_loc >= ring_info->ring_buffer->read_index ?
  1327. priv_read_loc - ring_info->ring_buffer->read_index :
  1328. (dsize - ring_info->ring_buffer->read_index) + priv_read_loc;
  1329. u32 bytes_avail_toread = (hv_get_bytes_to_read(ring_info) - delta);
  1330. if (bytes_avail_toread < sizeof(struct vmpacket_descriptor))
  1331. return NULL;
  1332. return ring_buffer + priv_read_loc;
  1333. }
  1334. /*
  1335. * A helper function to step through packets "in-place"
  1336. * This API is to be called after each successful call
  1337. * get_next_pkt_raw().
  1338. */
  1339. static inline void put_pkt_raw(struct vmbus_channel *channel,
  1340. struct vmpacket_descriptor *desc)
  1341. {
  1342. struct hv_ring_buffer_info *ring_info = &channel->inbound;
  1343. u32 packetlen = desc->len8 << 3;
  1344. u32 dsize = ring_info->ring_datasize;
  1345. /*
  1346. * Include the packet trailer.
  1347. */
  1348. ring_info->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
  1349. ring_info->priv_read_index %= dsize;
  1350. }
  1351. /*
  1352. * This call commits the read index and potentially signals the host.
  1353. * Here is the pattern for using the "in-place" consumption APIs:
  1354. *
  1355. * init_cached_read_index();
  1356. *
  1357. * while (get_next_pkt_raw() {
  1358. * process the packet "in-place";
  1359. * put_pkt_raw();
  1360. * }
  1361. * if (packets processed in place)
  1362. * commit_rd_index();
  1363. */
  1364. static inline void commit_rd_index(struct vmbus_channel *channel)
  1365. {
  1366. struct hv_ring_buffer_info *ring_info = &channel->inbound;
  1367. /*
  1368. * Make sure all reads are done before we update the read index since
  1369. * the writer may start writing to the read area once the read index
  1370. * is updated.
  1371. */
  1372. virt_rmb();
  1373. ring_info->ring_buffer->read_index = ring_info->priv_read_index;
  1374. hv_signal_on_read(channel);
  1375. }
  1376. #endif /* _HYPERV_H */