hyperv.h 40 KB

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