hyperv.h 38 KB

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