hyperv.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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. * For performance critical channels (storage, networking
  735. * etc,), Hyper-V has a mechanism to enhance the throughput
  736. * at the expense of latency:
  737. * When the host is to be signaled, we just set a bit in a shared page
  738. * and this bit will be inspected by the hypervisor within a certain
  739. * window and if the bit is set, the host will be signaled. The window
  740. * of time is the monitor latency - currently around 100 usecs. This
  741. * mechanism improves throughput by:
  742. *
  743. * A) Making the host more efficient - each time it wakes up,
  744. * potentially it will process morev number of packets. The
  745. * monitor latency allows a batch to build up.
  746. * B) By deferring the hypercall to signal, we will also minimize
  747. * the interrupts.
  748. *
  749. * Clearly, these optimizations improve throughput at the expense of
  750. * latency. Furthermore, since the channel is shared for both
  751. * control and data messages, control messages currently suffer
  752. * unnecessary latency adversley impacting performance and boot
  753. * time. To fix this issue, permit tagging the channel as being
  754. * in "low latency" mode. In this mode, we will bypass the monitor
  755. * mechanism.
  756. */
  757. bool low_latency;
  758. };
  759. static inline void set_channel_lock_state(struct vmbus_channel *c, bool state)
  760. {
  761. c->acquire_ring_lock = state;
  762. }
  763. static inline bool is_hvsock_channel(const struct vmbus_channel *c)
  764. {
  765. return !!(c->offermsg.offer.chn_flags &
  766. VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER);
  767. }
  768. static inline void set_channel_signal_state(struct vmbus_channel *c,
  769. enum hv_signal_policy policy)
  770. {
  771. c->signal_policy = policy;
  772. }
  773. static inline void set_channel_read_state(struct vmbus_channel *c, bool state)
  774. {
  775. c->batched_reading = state;
  776. }
  777. static inline void set_per_channel_state(struct vmbus_channel *c, void *s)
  778. {
  779. c->per_channel_state = s;
  780. }
  781. static inline void *get_per_channel_state(struct vmbus_channel *c)
  782. {
  783. return c->per_channel_state;
  784. }
  785. static inline void set_channel_pending_send_size(struct vmbus_channel *c,
  786. u32 size)
  787. {
  788. c->outbound.ring_buffer->pending_send_sz = size;
  789. }
  790. static inline void set_low_latency_mode(struct vmbus_channel *c)
  791. {
  792. c->low_latency = true;
  793. }
  794. static inline void clear_low_latency_mode(struct vmbus_channel *c)
  795. {
  796. c->low_latency = false;
  797. }
  798. void vmbus_onmessage(void *context);
  799. int vmbus_request_offers(void);
  800. /*
  801. * APIs for managing sub-channels.
  802. */
  803. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  804. void (*sc_cr_cb)(struct vmbus_channel *new_sc));
  805. void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
  806. void (*chn_rescind_cb)(struct vmbus_channel *));
  807. /*
  808. * Retrieve the (sub) channel on which to send an outgoing request.
  809. * When a primary channel has multiple sub-channels, we choose a
  810. * channel whose VCPU binding is closest to the VCPU on which
  811. * this call is being made.
  812. */
  813. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary);
  814. /*
  815. * Check if sub-channels have already been offerred. This API will be useful
  816. * when the driver is unloaded after establishing sub-channels. In this case,
  817. * when the driver is re-loaded, the driver would have to check if the
  818. * subchannels have already been established before attempting to request
  819. * the creation of sub-channels.
  820. * This function returns TRUE to indicate that subchannels have already been
  821. * created.
  822. * This function should be invoked after setting the callback function for
  823. * sub-channel creation.
  824. */
  825. bool vmbus_are_subchannels_present(struct vmbus_channel *primary);
  826. /* The format must be the same as struct vmdata_gpa_direct */
  827. struct vmbus_channel_packet_page_buffer {
  828. u16 type;
  829. u16 dataoffset8;
  830. u16 length8;
  831. u16 flags;
  832. u64 transactionid;
  833. u32 reserved;
  834. u32 rangecount;
  835. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  836. } __packed;
  837. /* The format must be the same as struct vmdata_gpa_direct */
  838. struct vmbus_channel_packet_multipage_buffer {
  839. u16 type;
  840. u16 dataoffset8;
  841. u16 length8;
  842. u16 flags;
  843. u64 transactionid;
  844. u32 reserved;
  845. u32 rangecount; /* Always 1 in this case */
  846. struct hv_multipage_buffer range;
  847. } __packed;
  848. /* The format must be the same as struct vmdata_gpa_direct */
  849. struct vmbus_packet_mpb_array {
  850. u16 type;
  851. u16 dataoffset8;
  852. u16 length8;
  853. u16 flags;
  854. u64 transactionid;
  855. u32 reserved;
  856. u32 rangecount; /* Always 1 in this case */
  857. struct hv_mpb_array range;
  858. } __packed;
  859. extern int vmbus_open(struct vmbus_channel *channel,
  860. u32 send_ringbuffersize,
  861. u32 recv_ringbuffersize,
  862. void *userdata,
  863. u32 userdatalen,
  864. void(*onchannel_callback)(void *context),
  865. void *context);
  866. extern void vmbus_close(struct vmbus_channel *channel);
  867. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  868. void *buffer,
  869. u32 bufferLen,
  870. u64 requestid,
  871. enum vmbus_packet_type type,
  872. u32 flags);
  873. extern int vmbus_sendpacket_ctl(struct vmbus_channel *channel,
  874. void *buffer,
  875. u32 bufferLen,
  876. u64 requestid,
  877. enum vmbus_packet_type type,
  878. u32 flags,
  879. bool kick_q);
  880. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  881. struct hv_page_buffer pagebuffers[],
  882. u32 pagecount,
  883. void *buffer,
  884. u32 bufferlen,
  885. u64 requestid);
  886. extern int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
  887. struct hv_page_buffer pagebuffers[],
  888. u32 pagecount,
  889. void *buffer,
  890. u32 bufferlen,
  891. u64 requestid,
  892. u32 flags,
  893. bool kick_q);
  894. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  895. struct hv_multipage_buffer *mpb,
  896. void *buffer,
  897. u32 bufferlen,
  898. u64 requestid);
  899. extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  900. struct vmbus_packet_mpb_array *mpb,
  901. u32 desc_size,
  902. void *buffer,
  903. u32 bufferlen,
  904. u64 requestid);
  905. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  906. void *kbuffer,
  907. u32 size,
  908. u32 *gpadl_handle);
  909. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  910. u32 gpadl_handle);
  911. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  912. void *buffer,
  913. u32 bufferlen,
  914. u32 *buffer_actual_len,
  915. u64 *requestid);
  916. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  917. void *buffer,
  918. u32 bufferlen,
  919. u32 *buffer_actual_len,
  920. u64 *requestid);
  921. extern void vmbus_ontimer(unsigned long data);
  922. /* Base driver object */
  923. struct hv_driver {
  924. const char *name;
  925. /*
  926. * A hvsock offer, which has a VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER
  927. * channel flag, actually doesn't mean a synthetic device because the
  928. * offer's if_type/if_instance can change for every new hvsock
  929. * connection.
  930. *
  931. * However, to facilitate the notification of new-offer/rescind-offer
  932. * from vmbus driver to hvsock driver, we can handle hvsock offer as
  933. * a special vmbus device, and hence we need the below flag to
  934. * indicate if the driver is the hvsock driver or not: we need to
  935. * specially treat the hvosck offer & driver in vmbus_match().
  936. */
  937. bool hvsock;
  938. /* the device type supported by this driver */
  939. uuid_le dev_type;
  940. const struct hv_vmbus_device_id *id_table;
  941. struct device_driver driver;
  942. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  943. int (*remove)(struct hv_device *);
  944. void (*shutdown)(struct hv_device *);
  945. };
  946. /* Base device object */
  947. struct hv_device {
  948. /* the device type id of this device */
  949. uuid_le dev_type;
  950. /* the device instance id of this device */
  951. uuid_le dev_instance;
  952. u16 vendor_id;
  953. u16 device_id;
  954. struct device device;
  955. struct vmbus_channel *channel;
  956. };
  957. static inline struct hv_device *device_to_hv_device(struct device *d)
  958. {
  959. return container_of(d, struct hv_device, device);
  960. }
  961. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  962. {
  963. return container_of(d, struct hv_driver, driver);
  964. }
  965. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  966. {
  967. dev_set_drvdata(&dev->device, data);
  968. }
  969. static inline void *hv_get_drvdata(struct hv_device *dev)
  970. {
  971. return dev_get_drvdata(&dev->device);
  972. }
  973. /* Vmbus interface */
  974. #define vmbus_driver_register(driver) \
  975. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  976. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  977. struct module *owner,
  978. const char *mod_name);
  979. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  980. void vmbus_hvsock_device_unregister(struct vmbus_channel *channel);
  981. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  982. resource_size_t min, resource_size_t max,
  983. resource_size_t size, resource_size_t align,
  984. bool fb_overlap_ok);
  985. void vmbus_free_mmio(resource_size_t start, resource_size_t size);
  986. int vmbus_cpu_number_to_vp_number(int cpu_number);
  987. u64 hv_do_hypercall(u64 control, void *input, void *output);
  988. /*
  989. * GUID definitions of various offer types - services offered to the guest.
  990. */
  991. /*
  992. * Network GUID
  993. * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
  994. */
  995. #define HV_NIC_GUID \
  996. .guid = UUID_LE(0xf8615163, 0xdf3e, 0x46c5, 0x91, 0x3f, \
  997. 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e)
  998. /*
  999. * IDE GUID
  1000. * {32412632-86cb-44a2-9b5c-50d1417354f5}
  1001. */
  1002. #define HV_IDE_GUID \
  1003. .guid = UUID_LE(0x32412632, 0x86cb, 0x44a2, 0x9b, 0x5c, \
  1004. 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  1005. /*
  1006. * SCSI GUID
  1007. * {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
  1008. */
  1009. #define HV_SCSI_GUID \
  1010. .guid = UUID_LE(0xba6163d9, 0x04a1, 0x4d29, 0xb6, 0x05, \
  1011. 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  1012. /*
  1013. * Shutdown GUID
  1014. * {0e0b6031-5213-4934-818b-38d90ced39db}
  1015. */
  1016. #define HV_SHUTDOWN_GUID \
  1017. .guid = UUID_LE(0x0e0b6031, 0x5213, 0x4934, 0x81, 0x8b, \
  1018. 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb)
  1019. /*
  1020. * Time Synch GUID
  1021. * {9527E630-D0AE-497b-ADCE-E80AB0175CAF}
  1022. */
  1023. #define HV_TS_GUID \
  1024. .guid = UUID_LE(0x9527e630, 0xd0ae, 0x497b, 0xad, 0xce, \
  1025. 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
  1026. /*
  1027. * Heartbeat GUID
  1028. * {57164f39-9115-4e78-ab55-382f3bd5422d}
  1029. */
  1030. #define HV_HEART_BEAT_GUID \
  1031. .guid = UUID_LE(0x57164f39, 0x9115, 0x4e78, 0xab, 0x55, \
  1032. 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
  1033. /*
  1034. * KVP GUID
  1035. * {a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}
  1036. */
  1037. #define HV_KVP_GUID \
  1038. .guid = UUID_LE(0xa9a0f4e7, 0x5a45, 0x4d96, 0xb8, 0x27, \
  1039. 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6)
  1040. /*
  1041. * Dynamic memory GUID
  1042. * {525074dc-8985-46e2-8057-a307dc18a502}
  1043. */
  1044. #define HV_DM_GUID \
  1045. .guid = UUID_LE(0x525074dc, 0x8985, 0x46e2, 0x80, 0x57, \
  1046. 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02)
  1047. /*
  1048. * Mouse GUID
  1049. * {cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}
  1050. */
  1051. #define HV_MOUSE_GUID \
  1052. .guid = UUID_LE(0xcfa8b69e, 0x5b4a, 0x4cc0, 0xb9, 0x8b, \
  1053. 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a)
  1054. /*
  1055. * Keyboard GUID
  1056. * {f912ad6d-2b17-48ea-bd65-f927a61c7684}
  1057. */
  1058. #define HV_KBD_GUID \
  1059. .guid = UUID_LE(0xf912ad6d, 0x2b17, 0x48ea, 0xbd, 0x65, \
  1060. 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84)
  1061. /*
  1062. * VSS (Backup/Restore) GUID
  1063. */
  1064. #define HV_VSS_GUID \
  1065. .guid = UUID_LE(0x35fa2e29, 0xea23, 0x4236, 0x96, 0xae, \
  1066. 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40)
  1067. /*
  1068. * Synthetic Video GUID
  1069. * {DA0A7802-E377-4aac-8E77-0558EB1073F8}
  1070. */
  1071. #define HV_SYNTHVID_GUID \
  1072. .guid = UUID_LE(0xda0a7802, 0xe377, 0x4aac, 0x8e, 0x77, \
  1073. 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8)
  1074. /*
  1075. * Synthetic FC GUID
  1076. * {2f9bcc4a-0069-4af3-b76b-6fd0be528cda}
  1077. */
  1078. #define HV_SYNTHFC_GUID \
  1079. .guid = UUID_LE(0x2f9bcc4a, 0x0069, 0x4af3, 0xb7, 0x6b, \
  1080. 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda)
  1081. /*
  1082. * Guest File Copy Service
  1083. * {34D14BE3-DEE4-41c8-9AE7-6B174977C192}
  1084. */
  1085. #define HV_FCOPY_GUID \
  1086. .guid = UUID_LE(0x34d14be3, 0xdee4, 0x41c8, 0x9a, 0xe7, \
  1087. 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92)
  1088. /*
  1089. * NetworkDirect. This is the guest RDMA service.
  1090. * {8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}
  1091. */
  1092. #define HV_ND_GUID \
  1093. .guid = UUID_LE(0x8c2eaf3d, 0x32a7, 0x4b09, 0xab, 0x99, \
  1094. 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01)
  1095. /*
  1096. * PCI Express Pass Through
  1097. * {44C4F61D-4444-4400-9D52-802E27EDE19F}
  1098. */
  1099. #define HV_PCIE_GUID \
  1100. .guid = UUID_LE(0x44c4f61d, 0x4444, 0x4400, 0x9d, 0x52, \
  1101. 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f)
  1102. /*
  1103. * Common header for Hyper-V ICs
  1104. */
  1105. #define ICMSGTYPE_NEGOTIATE 0
  1106. #define ICMSGTYPE_HEARTBEAT 1
  1107. #define ICMSGTYPE_KVPEXCHANGE 2
  1108. #define ICMSGTYPE_SHUTDOWN 3
  1109. #define ICMSGTYPE_TIMESYNC 4
  1110. #define ICMSGTYPE_VSS 5
  1111. #define ICMSGHDRFLAG_TRANSACTION 1
  1112. #define ICMSGHDRFLAG_REQUEST 2
  1113. #define ICMSGHDRFLAG_RESPONSE 4
  1114. /*
  1115. * While we want to handle util services as regular devices,
  1116. * there is only one instance of each of these services; so
  1117. * we statically allocate the service specific state.
  1118. */
  1119. struct hv_util_service {
  1120. u8 *recv_buffer;
  1121. void *channel;
  1122. void (*util_cb)(void *);
  1123. int (*util_init)(struct hv_util_service *);
  1124. void (*util_deinit)(void);
  1125. };
  1126. struct vmbuspipe_hdr {
  1127. u32 flags;
  1128. u32 msgsize;
  1129. } __packed;
  1130. struct ic_version {
  1131. u16 major;
  1132. u16 minor;
  1133. } __packed;
  1134. struct icmsg_hdr {
  1135. struct ic_version icverframe;
  1136. u16 icmsgtype;
  1137. struct ic_version icvermsg;
  1138. u16 icmsgsize;
  1139. u32 status;
  1140. u8 ictransaction_id;
  1141. u8 icflags;
  1142. u8 reserved[2];
  1143. } __packed;
  1144. struct icmsg_negotiate {
  1145. u16 icframe_vercnt;
  1146. u16 icmsg_vercnt;
  1147. u32 reserved;
  1148. struct ic_version icversion_data[1]; /* any size array */
  1149. } __packed;
  1150. struct shutdown_msg_data {
  1151. u32 reason_code;
  1152. u32 timeout_seconds;
  1153. u32 flags;
  1154. u8 display_message[2048];
  1155. } __packed;
  1156. struct heartbeat_msg_data {
  1157. u64 seq_num;
  1158. u32 reserved[8];
  1159. } __packed;
  1160. /* Time Sync IC defs */
  1161. #define ICTIMESYNCFLAG_PROBE 0
  1162. #define ICTIMESYNCFLAG_SYNC 1
  1163. #define ICTIMESYNCFLAG_SAMPLE 2
  1164. #ifdef __x86_64__
  1165. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1166. #else
  1167. #define WLTIMEDELTA 116444736000000000LL
  1168. #endif
  1169. struct ictimesync_data {
  1170. u64 parenttime;
  1171. u64 childtime;
  1172. u64 roundtriptime;
  1173. u8 flags;
  1174. } __packed;
  1175. struct hyperv_service_callback {
  1176. u8 msg_type;
  1177. char *log_msg;
  1178. uuid_le data;
  1179. struct vmbus_channel *channel;
  1180. void (*callback) (void *context);
  1181. };
  1182. #define MAX_SRV_VER 0x7ffffff
  1183. extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *,
  1184. struct icmsg_negotiate *, u8 *, int,
  1185. int);
  1186. void hv_event_tasklet_disable(struct vmbus_channel *channel);
  1187. void hv_event_tasklet_enable(struct vmbus_channel *channel);
  1188. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid);
  1189. /*
  1190. * Negotiated version with the Host.
  1191. */
  1192. extern __u32 vmbus_proto_version;
  1193. int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
  1194. const uuid_le *shv_host_servie_id);
  1195. void vmbus_set_event(struct vmbus_channel *channel);
  1196. /* Get the start of the ring buffer. */
  1197. static inline void *
  1198. hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
  1199. {
  1200. return (void *)ring_info->ring_buffer->buffer;
  1201. }
  1202. /*
  1203. * To optimize the flow management on the send-side,
  1204. * when the sender is blocked because of lack of
  1205. * sufficient space in the ring buffer, potential the
  1206. * consumer of the ring buffer can signal the producer.
  1207. * This is controlled by the following parameters:
  1208. *
  1209. * 1. pending_send_sz: This is the size in bytes that the
  1210. * producer is trying to send.
  1211. * 2. The feature bit feat_pending_send_sz set to indicate if
  1212. * the consumer of the ring will signal when the ring
  1213. * state transitions from being full to a state where
  1214. * there is room for the producer to send the pending packet.
  1215. */
  1216. static inline bool hv_need_to_signal_on_read(struct hv_ring_buffer_info *rbi)
  1217. {
  1218. u32 cur_write_sz;
  1219. u32 pending_sz;
  1220. /*
  1221. * Issue a full memory barrier before making the signaling decision.
  1222. * Here is the reason for having this barrier:
  1223. * If the reading of the pend_sz (in this function)
  1224. * were to be reordered and read before we commit the new read
  1225. * index (in the calling function) we could
  1226. * have a problem. If the host were to set the pending_sz after we
  1227. * have sampled pending_sz and go to sleep before we commit the
  1228. * read index, we could miss sending the interrupt. Issue a full
  1229. * memory barrier to address this.
  1230. */
  1231. virt_mb();
  1232. pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
  1233. /* If the other end is not blocked on write don't bother. */
  1234. if (pending_sz == 0)
  1235. return false;
  1236. cur_write_sz = hv_get_bytes_to_write(rbi);
  1237. if (cur_write_sz >= pending_sz)
  1238. return true;
  1239. return false;
  1240. }
  1241. /*
  1242. * An API to support in-place processing of incoming VMBUS packets.
  1243. */
  1244. #define VMBUS_PKT_TRAILER 8
  1245. static inline struct vmpacket_descriptor *
  1246. get_next_pkt_raw(struct vmbus_channel *channel)
  1247. {
  1248. struct hv_ring_buffer_info *ring_info = &channel->inbound;
  1249. u32 read_loc = ring_info->priv_read_index;
  1250. void *ring_buffer = hv_get_ring_buffer(ring_info);
  1251. struct vmpacket_descriptor *cur_desc;
  1252. u32 packetlen;
  1253. u32 dsize = ring_info->ring_datasize;
  1254. u32 delta = read_loc - ring_info->ring_buffer->read_index;
  1255. u32 bytes_avail_toread = (hv_get_bytes_to_read(ring_info) - delta);
  1256. if (bytes_avail_toread < sizeof(struct vmpacket_descriptor))
  1257. return NULL;
  1258. if ((read_loc + sizeof(*cur_desc)) > dsize)
  1259. return NULL;
  1260. cur_desc = ring_buffer + read_loc;
  1261. packetlen = cur_desc->len8 << 3;
  1262. /*
  1263. * If the packet under consideration is wrapping around,
  1264. * return failure.
  1265. */
  1266. if ((read_loc + packetlen + VMBUS_PKT_TRAILER) > (dsize - 1))
  1267. return NULL;
  1268. return cur_desc;
  1269. }
  1270. /*
  1271. * A helper function to step through packets "in-place"
  1272. * This API is to be called after each successful call
  1273. * get_next_pkt_raw().
  1274. */
  1275. static inline void put_pkt_raw(struct vmbus_channel *channel,
  1276. struct vmpacket_descriptor *desc)
  1277. {
  1278. struct hv_ring_buffer_info *ring_info = &channel->inbound;
  1279. u32 read_loc = ring_info->priv_read_index;
  1280. u32 packetlen = desc->len8 << 3;
  1281. u32 dsize = ring_info->ring_datasize;
  1282. if ((read_loc + packetlen + VMBUS_PKT_TRAILER) > dsize)
  1283. BUG();
  1284. /*
  1285. * Include the packet trailer.
  1286. */
  1287. ring_info->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
  1288. }
  1289. /*
  1290. * This call commits the read index and potentially signals the host.
  1291. * Here is the pattern for using the "in-place" consumption APIs:
  1292. *
  1293. * while (get_next_pkt_raw() {
  1294. * process the packet "in-place";
  1295. * put_pkt_raw();
  1296. * }
  1297. * if (packets processed in place)
  1298. * commit_rd_index();
  1299. */
  1300. static inline void commit_rd_index(struct vmbus_channel *channel)
  1301. {
  1302. struct hv_ring_buffer_info *ring_info = &channel->inbound;
  1303. /*
  1304. * Make sure all reads are done before we update the read index since
  1305. * the writer may start writing to the read area once the read index
  1306. * is updated.
  1307. */
  1308. virt_rmb();
  1309. ring_info->ring_buffer->read_index = ring_info->priv_read_index;
  1310. if (hv_need_to_signal_on_read(ring_info))
  1311. vmbus_set_event(channel);
  1312. }
  1313. #endif /* _HYPERV_H */