hyperv.h 42 KB

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