hyperv.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /*
  2. *
  3. * Copyright (c) 2011, Microsoft Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. *
  18. * Authors:
  19. * Haiyang Zhang <haiyangz@microsoft.com>
  20. * Hank Janssen <hjanssen@microsoft.com>
  21. * K. Y. Srinivasan <kys@microsoft.com>
  22. *
  23. */
  24. #ifndef _HYPERV_H
  25. #define _HYPERV_H
  26. #include <uapi/linux/hyperv.h>
  27. #include <linux/types.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/list.h>
  30. #include <linux/timer.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/completion.h>
  33. #include <linux/device.h>
  34. #include <linux/mod_devicetable.h>
  35. #define MAX_PAGE_BUFFER_COUNT 32
  36. #define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
  37. #pragma pack(push, 1)
  38. /* Single-page buffer */
  39. struct hv_page_buffer {
  40. u32 len;
  41. u32 offset;
  42. u64 pfn;
  43. };
  44. /* Multiple-page buffer */
  45. struct hv_multipage_buffer {
  46. /* Length and Offset determines the # of pfns in the array */
  47. u32 len;
  48. u32 offset;
  49. u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
  50. };
  51. /*
  52. * Multiple-page buffer array; the pfn array is variable size:
  53. * The number of entries in the PFN array is determined by
  54. * "len" and "offset".
  55. */
  56. struct hv_mpb_array {
  57. /* Length and Offset determines the # of pfns in the array */
  58. u32 len;
  59. u32 offset;
  60. u64 pfn_array[];
  61. };
  62. /* 0x18 includes the proprietary packet header */
  63. #define MAX_PAGE_BUFFER_PACKET (0x18 + \
  64. (sizeof(struct hv_page_buffer) * \
  65. MAX_PAGE_BUFFER_COUNT))
  66. #define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
  67. sizeof(struct hv_multipage_buffer))
  68. #pragma pack(pop)
  69. struct hv_ring_buffer {
  70. /* Offset in bytes from the start of ring data below */
  71. u32 write_index;
  72. /* Offset in bytes from the start of ring data below */
  73. u32 read_index;
  74. u32 interrupt_mask;
  75. /*
  76. * Win8 uses some of the reserved bits to implement
  77. * interrupt driven flow management. On the send side
  78. * we can request that the receiver interrupt the sender
  79. * when the ring transitions from being full to being able
  80. * to handle a message of size "pending_send_sz".
  81. *
  82. * Add necessary state for this enhancement.
  83. */
  84. u32 pending_send_sz;
  85. u32 reserved1[12];
  86. union {
  87. struct {
  88. u32 feat_pending_send_sz:1;
  89. };
  90. u32 value;
  91. } feature_bits;
  92. /* Pad it to PAGE_SIZE so that data starts on page boundary */
  93. u8 reserved2[4028];
  94. /*
  95. * Ring data starts here + RingDataStartOffset
  96. * !!! DO NOT place any fields below this !!!
  97. */
  98. u8 buffer[0];
  99. } __packed;
  100. struct hv_ring_buffer_info {
  101. struct hv_ring_buffer *ring_buffer;
  102. u32 ring_size; /* Include the shared header */
  103. spinlock_t ring_lock;
  104. u32 ring_datasize; /* < ring_size */
  105. u32 ring_data_startoffset;
  106. };
  107. /*
  108. *
  109. * hv_get_ringbuffer_availbytes()
  110. *
  111. * Get number of bytes available to read and to write to
  112. * for the specified ring buffer
  113. */
  114. static inline void
  115. hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
  116. u32 *read, u32 *write)
  117. {
  118. u32 read_loc, write_loc, dsize;
  119. smp_read_barrier_depends();
  120. /* Capture the read/write indices before they changed */
  121. read_loc = rbi->ring_buffer->read_index;
  122. write_loc = rbi->ring_buffer->write_index;
  123. dsize = rbi->ring_datasize;
  124. *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  125. read_loc - write_loc;
  126. *read = dsize - *write;
  127. }
  128. /*
  129. * VMBUS version is 32 bit entity broken up into
  130. * two 16 bit quantities: major_number. minor_number.
  131. *
  132. * 0 . 13 (Windows Server 2008)
  133. * 1 . 1 (Windows 7)
  134. * 2 . 4 (Windows 8)
  135. * 3 . 0 (Windows 8 R2)
  136. * 4 . 0 (Windows 10)
  137. */
  138. #define VERSION_WS2008 ((0 << 16) | (13))
  139. #define VERSION_WIN7 ((1 << 16) | (1))
  140. #define VERSION_WIN8 ((2 << 16) | (4))
  141. #define VERSION_WIN8_1 ((3 << 16) | (0))
  142. #define VERSION_WIN10 ((4 << 16) | (0))
  143. #define VERSION_INVAL -1
  144. #define VERSION_CURRENT VERSION_WIN10
  145. /* Make maximum size of pipe payload of 16K */
  146. #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
  147. /* Define PipeMode values. */
  148. #define VMBUS_PIPE_TYPE_BYTE 0x00000000
  149. #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
  150. /* The size of the user defined data buffer for non-pipe offers. */
  151. #define MAX_USER_DEFINED_BYTES 120
  152. /* The size of the user defined data buffer for pipe offers. */
  153. #define MAX_PIPE_USER_DEFINED_BYTES 116
  154. /*
  155. * At the center of the Channel Management library is the Channel Offer. This
  156. * struct contains the fundamental information about an offer.
  157. */
  158. struct vmbus_channel_offer {
  159. uuid_le if_type;
  160. uuid_le if_instance;
  161. /*
  162. * These two fields are not currently used.
  163. */
  164. u64 reserved1;
  165. u64 reserved2;
  166. u16 chn_flags;
  167. u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
  168. union {
  169. /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
  170. struct {
  171. unsigned char user_def[MAX_USER_DEFINED_BYTES];
  172. } std;
  173. /*
  174. * Pipes:
  175. * The following sructure is an integrated pipe protocol, which
  176. * is implemented on top of standard user-defined data. Pipe
  177. * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
  178. * use.
  179. */
  180. struct {
  181. u32 pipe_mode;
  182. unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
  183. } pipe;
  184. } u;
  185. /*
  186. * The sub_channel_index is defined in win8.
  187. */
  188. u16 sub_channel_index;
  189. u16 reserved3;
  190. } __packed;
  191. /* Server Flags */
  192. #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
  193. #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
  194. #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
  195. #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
  196. #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
  197. #define VMBUS_CHANNEL_PARENT_OFFER 0x200
  198. #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
  199. struct vmpacket_descriptor {
  200. u16 type;
  201. u16 offset8;
  202. u16 len8;
  203. u16 flags;
  204. u64 trans_id;
  205. } __packed;
  206. struct vmpacket_header {
  207. u32 prev_pkt_start_offset;
  208. struct vmpacket_descriptor descriptor;
  209. } __packed;
  210. struct vmtransfer_page_range {
  211. u32 byte_count;
  212. u32 byte_offset;
  213. } __packed;
  214. struct vmtransfer_page_packet_header {
  215. struct vmpacket_descriptor d;
  216. u16 xfer_pageset_id;
  217. u8 sender_owns_set;
  218. u8 reserved;
  219. u32 range_cnt;
  220. struct vmtransfer_page_range ranges[1];
  221. } __packed;
  222. struct vmgpadl_packet_header {
  223. struct vmpacket_descriptor d;
  224. u32 gpadl;
  225. u32 reserved;
  226. } __packed;
  227. struct vmadd_remove_transfer_page_set {
  228. struct vmpacket_descriptor d;
  229. u32 gpadl;
  230. u16 xfer_pageset_id;
  231. u16 reserved;
  232. } __packed;
  233. /*
  234. * This structure defines a range in guest physical space that can be made to
  235. * look virtually contiguous.
  236. */
  237. struct gpa_range {
  238. u32 byte_count;
  239. u32 byte_offset;
  240. u64 pfn_array[0];
  241. };
  242. /*
  243. * This is the format for an Establish Gpadl packet, which contains a handle by
  244. * which this GPADL will be known and a set of GPA ranges associated with it.
  245. * This can be converted to a MDL by the guest OS. If there are multiple GPA
  246. * ranges, then the resulting MDL will be "chained," representing multiple VA
  247. * ranges.
  248. */
  249. struct vmestablish_gpadl {
  250. struct vmpacket_descriptor d;
  251. u32 gpadl;
  252. u32 range_cnt;
  253. struct gpa_range range[1];
  254. } __packed;
  255. /*
  256. * This is the format for a Teardown Gpadl packet, which indicates that the
  257. * GPADL handle in the Establish Gpadl packet will never be referenced again.
  258. */
  259. struct vmteardown_gpadl {
  260. struct vmpacket_descriptor d;
  261. u32 gpadl;
  262. u32 reserved; /* for alignment to a 8-byte boundary */
  263. } __packed;
  264. /*
  265. * This is the format for a GPA-Direct packet, which contains a set of GPA
  266. * ranges, in addition to commands and/or data.
  267. */
  268. struct vmdata_gpa_direct {
  269. struct vmpacket_descriptor d;
  270. u32 reserved;
  271. u32 range_cnt;
  272. struct gpa_range range[1];
  273. } __packed;
  274. /* This is the format for a Additional Data Packet. */
  275. struct vmadditional_data {
  276. struct vmpacket_descriptor d;
  277. u64 total_bytes;
  278. u32 offset;
  279. u32 byte_cnt;
  280. unsigned char data[1];
  281. } __packed;
  282. union vmpacket_largest_possible_header {
  283. struct vmpacket_descriptor simple_hdr;
  284. struct vmtransfer_page_packet_header xfer_page_hdr;
  285. struct vmgpadl_packet_header gpadl_hdr;
  286. struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
  287. struct vmestablish_gpadl establish_gpadl_hdr;
  288. struct vmteardown_gpadl teardown_gpadl_hdr;
  289. struct vmdata_gpa_direct data_gpa_direct_hdr;
  290. };
  291. #define VMPACKET_DATA_START_ADDRESS(__packet) \
  292. (void *)(((unsigned char *)__packet) + \
  293. ((struct vmpacket_descriptor)__packet)->offset8 * 8)
  294. #define VMPACKET_DATA_LENGTH(__packet) \
  295. ((((struct vmpacket_descriptor)__packet)->len8 - \
  296. ((struct vmpacket_descriptor)__packet)->offset8) * 8)
  297. #define VMPACKET_TRANSFER_MODE(__packet) \
  298. (((struct IMPACT)__packet)->type)
  299. enum vmbus_packet_type {
  300. VM_PKT_INVALID = 0x0,
  301. VM_PKT_SYNCH = 0x1,
  302. VM_PKT_ADD_XFER_PAGESET = 0x2,
  303. VM_PKT_RM_XFER_PAGESET = 0x3,
  304. VM_PKT_ESTABLISH_GPADL = 0x4,
  305. VM_PKT_TEARDOWN_GPADL = 0x5,
  306. VM_PKT_DATA_INBAND = 0x6,
  307. VM_PKT_DATA_USING_XFER_PAGES = 0x7,
  308. VM_PKT_DATA_USING_GPADL = 0x8,
  309. VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
  310. VM_PKT_CANCEL_REQUEST = 0xa,
  311. VM_PKT_COMP = 0xb,
  312. VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
  313. VM_PKT_ADDITIONAL_DATA = 0xd
  314. };
  315. #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
  316. /* Version 1 messages */
  317. enum vmbus_channel_message_type {
  318. CHANNELMSG_INVALID = 0,
  319. CHANNELMSG_OFFERCHANNEL = 1,
  320. CHANNELMSG_RESCIND_CHANNELOFFER = 2,
  321. CHANNELMSG_REQUESTOFFERS = 3,
  322. CHANNELMSG_ALLOFFERS_DELIVERED = 4,
  323. CHANNELMSG_OPENCHANNEL = 5,
  324. CHANNELMSG_OPENCHANNEL_RESULT = 6,
  325. CHANNELMSG_CLOSECHANNEL = 7,
  326. CHANNELMSG_GPADL_HEADER = 8,
  327. CHANNELMSG_GPADL_BODY = 9,
  328. CHANNELMSG_GPADL_CREATED = 10,
  329. CHANNELMSG_GPADL_TEARDOWN = 11,
  330. CHANNELMSG_GPADL_TORNDOWN = 12,
  331. CHANNELMSG_RELID_RELEASED = 13,
  332. CHANNELMSG_INITIATE_CONTACT = 14,
  333. CHANNELMSG_VERSION_RESPONSE = 15,
  334. CHANNELMSG_UNLOAD = 16,
  335. CHANNELMSG_UNLOAD_RESPONSE = 17,
  336. CHANNELMSG_COUNT
  337. };
  338. struct vmbus_channel_message_header {
  339. enum vmbus_channel_message_type msgtype;
  340. u32 padding;
  341. } __packed;
  342. /* Query VMBus Version parameters */
  343. struct vmbus_channel_query_vmbus_version {
  344. struct vmbus_channel_message_header header;
  345. u32 version;
  346. } __packed;
  347. /* VMBus Version Supported parameters */
  348. struct vmbus_channel_version_supported {
  349. struct vmbus_channel_message_header header;
  350. u8 version_supported;
  351. } __packed;
  352. /* Offer Channel parameters */
  353. struct vmbus_channel_offer_channel {
  354. struct vmbus_channel_message_header header;
  355. struct vmbus_channel_offer offer;
  356. u32 child_relid;
  357. u8 monitorid;
  358. /*
  359. * win7 and beyond splits this field into a bit field.
  360. */
  361. u8 monitor_allocated:1;
  362. u8 reserved:7;
  363. /*
  364. * These are new fields added in win7 and later.
  365. * Do not access these fields without checking the
  366. * negotiated protocol.
  367. *
  368. * If "is_dedicated_interrupt" is set, we must not set the
  369. * associated bit in the channel bitmap while sending the
  370. * interrupt to the host.
  371. *
  372. * connection_id is to be used in signaling the host.
  373. */
  374. u16 is_dedicated_interrupt:1;
  375. u16 reserved1:15;
  376. u32 connection_id;
  377. } __packed;
  378. /* Rescind Offer parameters */
  379. struct vmbus_channel_rescind_offer {
  380. struct vmbus_channel_message_header header;
  381. u32 child_relid;
  382. } __packed;
  383. /*
  384. * Request Offer -- no parameters, SynIC message contains the partition ID
  385. * Set Snoop -- no parameters, SynIC message contains the partition ID
  386. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  387. * All Offers Delivered -- no parameters, SynIC message contains the partition
  388. * ID
  389. * Flush Client -- no parameters, SynIC message contains the partition ID
  390. */
  391. /* Open Channel parameters */
  392. struct vmbus_channel_open_channel {
  393. struct vmbus_channel_message_header header;
  394. /* Identifies the specific VMBus channel that is being opened. */
  395. u32 child_relid;
  396. /* ID making a particular open request at a channel offer unique. */
  397. u32 openid;
  398. /* GPADL for the channel's ring buffer. */
  399. u32 ringbuffer_gpadlhandle;
  400. /*
  401. * Starting with win8, this field will be used to specify
  402. * the target virtual processor on which to deliver the interrupt for
  403. * the host to guest communication.
  404. * Prior to win8, incoming channel interrupts would only
  405. * be delivered on cpu 0. Setting this value to 0 would
  406. * preserve the earlier behavior.
  407. */
  408. u32 target_vp;
  409. /*
  410. * The upstream ring buffer begins at offset zero in the memory
  411. * described by RingBufferGpadlHandle. The downstream ring buffer
  412. * follows it at this offset (in pages).
  413. */
  414. u32 downstream_ringbuffer_pageoffset;
  415. /* User-specific data to be passed along to the server endpoint. */
  416. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  417. } __packed;
  418. /* Open Channel Result parameters */
  419. struct vmbus_channel_open_result {
  420. struct vmbus_channel_message_header header;
  421. u32 child_relid;
  422. u32 openid;
  423. u32 status;
  424. } __packed;
  425. /* Close channel parameters; */
  426. struct vmbus_channel_close_channel {
  427. struct vmbus_channel_message_header header;
  428. u32 child_relid;
  429. } __packed;
  430. /* Channel Message GPADL */
  431. #define GPADL_TYPE_RING_BUFFER 1
  432. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  433. #define GPADL_TYPE_TRANSACTION 8
  434. /*
  435. * The number of PFNs in a GPADL message is defined by the number of
  436. * pages that would be spanned by ByteCount and ByteOffset. If the
  437. * implied number of PFNs won't fit in this packet, there will be a
  438. * follow-up packet that contains more.
  439. */
  440. struct vmbus_channel_gpadl_header {
  441. struct vmbus_channel_message_header header;
  442. u32 child_relid;
  443. u32 gpadl;
  444. u16 range_buflen;
  445. u16 rangecount;
  446. struct gpa_range range[0];
  447. } __packed;
  448. /* This is the followup packet that contains more PFNs. */
  449. struct vmbus_channel_gpadl_body {
  450. struct vmbus_channel_message_header header;
  451. u32 msgnumber;
  452. u32 gpadl;
  453. u64 pfn[0];
  454. } __packed;
  455. struct vmbus_channel_gpadl_created {
  456. struct vmbus_channel_message_header header;
  457. u32 child_relid;
  458. u32 gpadl;
  459. u32 creation_status;
  460. } __packed;
  461. struct vmbus_channel_gpadl_teardown {
  462. struct vmbus_channel_message_header header;
  463. u32 child_relid;
  464. u32 gpadl;
  465. } __packed;
  466. struct vmbus_channel_gpadl_torndown {
  467. struct vmbus_channel_message_header header;
  468. u32 gpadl;
  469. } __packed;
  470. struct vmbus_channel_relid_released {
  471. struct vmbus_channel_message_header header;
  472. u32 child_relid;
  473. } __packed;
  474. struct vmbus_channel_initiate_contact {
  475. struct vmbus_channel_message_header header;
  476. u32 vmbus_version_requested;
  477. u32 target_vcpu; /* The VCPU the host should respond to */
  478. u64 interrupt_page;
  479. u64 monitor_page1;
  480. u64 monitor_page2;
  481. } __packed;
  482. struct vmbus_channel_version_response {
  483. struct vmbus_channel_message_header header;
  484. u8 version_supported;
  485. } __packed;
  486. enum vmbus_channel_state {
  487. CHANNEL_OFFER_STATE,
  488. CHANNEL_OPENING_STATE,
  489. CHANNEL_OPEN_STATE,
  490. CHANNEL_OPENED_STATE,
  491. };
  492. /*
  493. * Represents each channel msg on the vmbus connection This is a
  494. * variable-size data structure depending on the msg type itself
  495. */
  496. struct vmbus_channel_msginfo {
  497. /* Bookkeeping stuff */
  498. struct list_head msglistentry;
  499. /* So far, this is only used to handle gpadl body message */
  500. struct list_head submsglist;
  501. /* Synchronize the request/response if needed */
  502. struct completion waitevent;
  503. union {
  504. struct vmbus_channel_version_supported version_supported;
  505. struct vmbus_channel_open_result open_result;
  506. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  507. struct vmbus_channel_gpadl_created gpadl_created;
  508. struct vmbus_channel_version_response version_response;
  509. } response;
  510. u32 msgsize;
  511. /*
  512. * The channel message that goes out on the "wire".
  513. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  514. */
  515. unsigned char msg[0];
  516. };
  517. struct vmbus_close_msg {
  518. struct vmbus_channel_msginfo info;
  519. struct vmbus_channel_close_channel msg;
  520. };
  521. /* Define connection identifier type. */
  522. union hv_connection_id {
  523. u32 asu32;
  524. struct {
  525. u32 id:24;
  526. u32 reserved:8;
  527. } u;
  528. };
  529. /* Definition of the hv_signal_event hypercall input structure. */
  530. struct hv_input_signal_event {
  531. union hv_connection_id connectionid;
  532. u16 flag_number;
  533. u16 rsvdz;
  534. };
  535. struct hv_input_signal_event_buffer {
  536. u64 align8;
  537. struct hv_input_signal_event event;
  538. };
  539. struct vmbus_channel {
  540. /* Unique channel id */
  541. int id;
  542. struct list_head listentry;
  543. struct hv_device *device_obj;
  544. enum vmbus_channel_state state;
  545. struct vmbus_channel_offer_channel offermsg;
  546. /*
  547. * These are based on the OfferMsg.MonitorId.
  548. * Save it here for easy access.
  549. */
  550. u8 monitor_grp;
  551. u8 monitor_bit;
  552. bool rescind; /* got rescind msg */
  553. u32 ringbuffer_gpadlhandle;
  554. /* Allocated memory for ring buffer */
  555. void *ringbuffer_pages;
  556. u32 ringbuffer_pagecount;
  557. struct hv_ring_buffer_info outbound; /* send to parent */
  558. struct hv_ring_buffer_info inbound; /* receive from parent */
  559. spinlock_t inbound_lock;
  560. struct vmbus_close_msg close_msg;
  561. /* Channel callback are invoked in this workqueue context */
  562. /* HANDLE dataWorkQueue; */
  563. void (*onchannel_callback)(void *context);
  564. void *channel_callback_context;
  565. /*
  566. * A channel can be marked for efficient (batched)
  567. * reading:
  568. * If batched_reading is set to "true", we read until the
  569. * channel is empty and hold off interrupts from the host
  570. * during the entire read process.
  571. * If batched_reading is set to "false", the client is not
  572. * going to perform batched reading.
  573. *
  574. * By default we will enable batched reading; specific
  575. * drivers that don't want this behavior can turn it off.
  576. */
  577. bool batched_reading;
  578. bool is_dedicated_interrupt;
  579. struct hv_input_signal_event_buffer sig_buf;
  580. struct hv_input_signal_event *sig_event;
  581. /*
  582. * Starting with win8, this field will be used to specify
  583. * the target virtual processor on which to deliver the interrupt for
  584. * the host to guest communication.
  585. * Prior to win8, incoming channel interrupts would only
  586. * be delivered on cpu 0. Setting this value to 0 would
  587. * preserve the earlier behavior.
  588. */
  589. u32 target_vp;
  590. /* The corresponding CPUID in the guest */
  591. u32 target_cpu;
  592. /*
  593. * State to manage the CPU affiliation of channels.
  594. */
  595. struct cpumask alloced_cpus_in_node;
  596. int numa_node;
  597. /*
  598. * Support for sub-channels. For high performance devices,
  599. * it will be useful to have multiple sub-channels to support
  600. * a scalable communication infrastructure with the host.
  601. * The support for sub-channels is implemented as an extention
  602. * to the current infrastructure.
  603. * The initial offer is considered the primary channel and this
  604. * offer message will indicate if the host supports sub-channels.
  605. * The guest is free to ask for sub-channels to be offerred and can
  606. * open these sub-channels as a normal "primary" channel. However,
  607. * all sub-channels will have the same type and instance guids as the
  608. * primary channel. Requests sent on a given channel will result in a
  609. * response on the same channel.
  610. */
  611. /*
  612. * Sub-channel creation callback. This callback will be called in
  613. * process context when a sub-channel offer is received from the host.
  614. * The guest can open the sub-channel in the context of this callback.
  615. */
  616. void (*sc_creation_callback)(struct vmbus_channel *new_sc);
  617. /*
  618. * The spinlock to protect the structure. It is being used to protect
  619. * test-and-set access to various attributes of the structure as well
  620. * as all sc_list operations.
  621. */
  622. spinlock_t lock;
  623. /*
  624. * All Sub-channels of a primary channel are linked here.
  625. */
  626. struct list_head sc_list;
  627. /*
  628. * Current number of sub-channels.
  629. */
  630. int num_sc;
  631. /*
  632. * Number of a sub-channel (position within sc_list) which is supposed
  633. * to be used as the next outgoing channel.
  634. */
  635. int next_oc;
  636. /*
  637. * The primary channel this sub-channel belongs to.
  638. * This will be NULL for the primary channel.
  639. */
  640. struct vmbus_channel *primary_channel;
  641. /*
  642. * Support per-channel state for use by vmbus drivers.
  643. */
  644. void *per_channel_state;
  645. /*
  646. * To support per-cpu lookup mapping of relid to channel,
  647. * link up channels based on their CPU affinity.
  648. */
  649. struct list_head percpu_list;
  650. };
  651. static inline void set_channel_read_state(struct vmbus_channel *c, bool state)
  652. {
  653. c->batched_reading = state;
  654. }
  655. static inline void set_per_channel_state(struct vmbus_channel *c, void *s)
  656. {
  657. c->per_channel_state = s;
  658. }
  659. static inline void *get_per_channel_state(struct vmbus_channel *c)
  660. {
  661. return c->per_channel_state;
  662. }
  663. void vmbus_onmessage(void *context);
  664. int vmbus_request_offers(void);
  665. /*
  666. * APIs for managing sub-channels.
  667. */
  668. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  669. void (*sc_cr_cb)(struct vmbus_channel *new_sc));
  670. /*
  671. * Retrieve the (sub) channel on which to send an outgoing request.
  672. * When a primary channel has multiple sub-channels, we choose a
  673. * channel whose VCPU binding is closest to the VCPU on which
  674. * this call is being made.
  675. */
  676. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary);
  677. /*
  678. * Check if sub-channels have already been offerred. This API will be useful
  679. * when the driver is unloaded after establishing sub-channels. In this case,
  680. * when the driver is re-loaded, the driver would have to check if the
  681. * subchannels have already been established before attempting to request
  682. * the creation of sub-channels.
  683. * This function returns TRUE to indicate that subchannels have already been
  684. * created.
  685. * This function should be invoked after setting the callback function for
  686. * sub-channel creation.
  687. */
  688. bool vmbus_are_subchannels_present(struct vmbus_channel *primary);
  689. /* The format must be the same as struct vmdata_gpa_direct */
  690. struct vmbus_channel_packet_page_buffer {
  691. u16 type;
  692. u16 dataoffset8;
  693. u16 length8;
  694. u16 flags;
  695. u64 transactionid;
  696. u32 reserved;
  697. u32 rangecount;
  698. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  699. } __packed;
  700. /* The format must be the same as struct vmdata_gpa_direct */
  701. struct vmbus_channel_packet_multipage_buffer {
  702. u16 type;
  703. u16 dataoffset8;
  704. u16 length8;
  705. u16 flags;
  706. u64 transactionid;
  707. u32 reserved;
  708. u32 rangecount; /* Always 1 in this case */
  709. struct hv_multipage_buffer range;
  710. } __packed;
  711. /* The format must be the same as struct vmdata_gpa_direct */
  712. struct vmbus_packet_mpb_array {
  713. u16 type;
  714. u16 dataoffset8;
  715. u16 length8;
  716. u16 flags;
  717. u64 transactionid;
  718. u32 reserved;
  719. u32 rangecount; /* Always 1 in this case */
  720. struct hv_mpb_array range;
  721. } __packed;
  722. extern int vmbus_open(struct vmbus_channel *channel,
  723. u32 send_ringbuffersize,
  724. u32 recv_ringbuffersize,
  725. void *userdata,
  726. u32 userdatalen,
  727. void(*onchannel_callback)(void *context),
  728. void *context);
  729. extern void vmbus_close(struct vmbus_channel *channel);
  730. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  731. void *buffer,
  732. u32 bufferLen,
  733. u64 requestid,
  734. enum vmbus_packet_type type,
  735. u32 flags);
  736. extern int vmbus_sendpacket_ctl(struct vmbus_channel *channel,
  737. void *buffer,
  738. u32 bufferLen,
  739. u64 requestid,
  740. enum vmbus_packet_type type,
  741. u32 flags,
  742. bool kick_q);
  743. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  744. struct hv_page_buffer pagebuffers[],
  745. u32 pagecount,
  746. void *buffer,
  747. u32 bufferlen,
  748. u64 requestid);
  749. extern int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
  750. struct hv_page_buffer pagebuffers[],
  751. u32 pagecount,
  752. void *buffer,
  753. u32 bufferlen,
  754. u64 requestid,
  755. u32 flags,
  756. bool kick_q);
  757. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  758. struct hv_multipage_buffer *mpb,
  759. void *buffer,
  760. u32 bufferlen,
  761. u64 requestid);
  762. extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  763. struct vmbus_packet_mpb_array *mpb,
  764. u32 desc_size,
  765. void *buffer,
  766. u32 bufferlen,
  767. u64 requestid);
  768. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  769. void *kbuffer,
  770. u32 size,
  771. u32 *gpadl_handle);
  772. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  773. u32 gpadl_handle);
  774. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  775. void *buffer,
  776. u32 bufferlen,
  777. u32 *buffer_actual_len,
  778. u64 *requestid);
  779. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  780. void *buffer,
  781. u32 bufferlen,
  782. u32 *buffer_actual_len,
  783. u64 *requestid);
  784. extern void vmbus_ontimer(unsigned long data);
  785. /* Base driver object */
  786. struct hv_driver {
  787. const char *name;
  788. /* the device type supported by this driver */
  789. uuid_le dev_type;
  790. const struct hv_vmbus_device_id *id_table;
  791. struct device_driver driver;
  792. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  793. int (*remove)(struct hv_device *);
  794. void (*shutdown)(struct hv_device *);
  795. };
  796. /* Base device object */
  797. struct hv_device {
  798. /* the device type id of this device */
  799. uuid_le dev_type;
  800. /* the device instance id of this device */
  801. uuid_le dev_instance;
  802. struct device device;
  803. struct vmbus_channel *channel;
  804. };
  805. static inline struct hv_device *device_to_hv_device(struct device *d)
  806. {
  807. return container_of(d, struct hv_device, device);
  808. }
  809. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  810. {
  811. return container_of(d, struct hv_driver, driver);
  812. }
  813. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  814. {
  815. dev_set_drvdata(&dev->device, data);
  816. }
  817. static inline void *hv_get_drvdata(struct hv_device *dev)
  818. {
  819. return dev_get_drvdata(&dev->device);
  820. }
  821. /* Vmbus interface */
  822. #define vmbus_driver_register(driver) \
  823. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  824. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  825. struct module *owner,
  826. const char *mod_name);
  827. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  828. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  829. resource_size_t min, resource_size_t max,
  830. resource_size_t size, resource_size_t align,
  831. bool fb_overlap_ok);
  832. /**
  833. * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
  834. *
  835. * This macro is used to create a struct hv_vmbus_device_id that matches a
  836. * specific device.
  837. */
  838. #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
  839. g8, g9, ga, gb, gc, gd, ge, gf) \
  840. .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
  841. g8, g9, ga, gb, gc, gd, ge, gf },
  842. /*
  843. * GUID definitions of various offer types - services offered to the guest.
  844. */
  845. /*
  846. * Network GUID
  847. * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
  848. */
  849. #define HV_NIC_GUID \
  850. .guid = { \
  851. 0x63, 0x51, 0x61, 0xf8, 0x3e, 0xdf, 0xc5, 0x46, \
  852. 0x91, 0x3f, 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e \
  853. }
  854. /*
  855. * IDE GUID
  856. * {32412632-86cb-44a2-9b5c-50d1417354f5}
  857. */
  858. #define HV_IDE_GUID \
  859. .guid = { \
  860. 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, \
  861. 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5 \
  862. }
  863. /*
  864. * SCSI GUID
  865. * {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
  866. */
  867. #define HV_SCSI_GUID \
  868. .guid = { \
  869. 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, \
  870. 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f \
  871. }
  872. /*
  873. * Shutdown GUID
  874. * {0e0b6031-5213-4934-818b-38d90ced39db}
  875. */
  876. #define HV_SHUTDOWN_GUID \
  877. .guid = { \
  878. 0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49, \
  879. 0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb \
  880. }
  881. /*
  882. * Time Synch GUID
  883. * {9527E630-D0AE-497b-ADCE-E80AB0175CAF}
  884. */
  885. #define HV_TS_GUID \
  886. .guid = { \
  887. 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49, \
  888. 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf \
  889. }
  890. /*
  891. * Heartbeat GUID
  892. * {57164f39-9115-4e78-ab55-382f3bd5422d}
  893. */
  894. #define HV_HEART_BEAT_GUID \
  895. .guid = { \
  896. 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e, \
  897. 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d \
  898. }
  899. /*
  900. * KVP GUID
  901. * {a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}
  902. */
  903. #define HV_KVP_GUID \
  904. .guid = { \
  905. 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d, \
  906. 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6 \
  907. }
  908. /*
  909. * Dynamic memory GUID
  910. * {525074dc-8985-46e2-8057-a307dc18a502}
  911. */
  912. #define HV_DM_GUID \
  913. .guid = { \
  914. 0xdc, 0x74, 0x50, 0X52, 0x85, 0x89, 0xe2, 0x46, \
  915. 0x80, 0x57, 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02 \
  916. }
  917. /*
  918. * Mouse GUID
  919. * {cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}
  920. */
  921. #define HV_MOUSE_GUID \
  922. .guid = { \
  923. 0x9e, 0xb6, 0xa8, 0xcf, 0x4a, 0x5b, 0xc0, 0x4c, \
  924. 0xb9, 0x8b, 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a \
  925. }
  926. /*
  927. * VSS (Backup/Restore) GUID
  928. */
  929. #define HV_VSS_GUID \
  930. .guid = { \
  931. 0x29, 0x2e, 0xfa, 0x35, 0x23, 0xea, 0x36, 0x42, \
  932. 0x96, 0xae, 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40 \
  933. }
  934. /*
  935. * Synthetic Video GUID
  936. * {DA0A7802-E377-4aac-8E77-0558EB1073F8}
  937. */
  938. #define HV_SYNTHVID_GUID \
  939. .guid = { \
  940. 0x02, 0x78, 0x0a, 0xda, 0x77, 0xe3, 0xac, 0x4a, \
  941. 0x8e, 0x77, 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8 \
  942. }
  943. /*
  944. * Synthetic FC GUID
  945. * {2f9bcc4a-0069-4af3-b76b-6fd0be528cda}
  946. */
  947. #define HV_SYNTHFC_GUID \
  948. .guid = { \
  949. 0x4A, 0xCC, 0x9B, 0x2F, 0x69, 0x00, 0xF3, 0x4A, \
  950. 0xB7, 0x6B, 0x6F, 0xD0, 0xBE, 0x52, 0x8C, 0xDA \
  951. }
  952. /*
  953. * Guest File Copy Service
  954. * {34D14BE3-DEE4-41c8-9AE7-6B174977C192}
  955. */
  956. #define HV_FCOPY_GUID \
  957. .guid = { \
  958. 0xE3, 0x4B, 0xD1, 0x34, 0xE4, 0xDE, 0xC8, 0x41, \
  959. 0x9A, 0xE7, 0x6B, 0x17, 0x49, 0x77, 0xC1, 0x92 \
  960. }
  961. /*
  962. * NetworkDirect. This is the guest RDMA service.
  963. * {8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}
  964. */
  965. #define HV_ND_GUID \
  966. .guid = { \
  967. 0x3d, 0xaf, 0x2e, 0x8c, 0xa7, 0x32, 0x09, 0x4b, \
  968. 0xab, 0x99, 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01 \
  969. }
  970. /*
  971. * Common header for Hyper-V ICs
  972. */
  973. #define ICMSGTYPE_NEGOTIATE 0
  974. #define ICMSGTYPE_HEARTBEAT 1
  975. #define ICMSGTYPE_KVPEXCHANGE 2
  976. #define ICMSGTYPE_SHUTDOWN 3
  977. #define ICMSGTYPE_TIMESYNC 4
  978. #define ICMSGTYPE_VSS 5
  979. #define ICMSGHDRFLAG_TRANSACTION 1
  980. #define ICMSGHDRFLAG_REQUEST 2
  981. #define ICMSGHDRFLAG_RESPONSE 4
  982. /*
  983. * While we want to handle util services as regular devices,
  984. * there is only one instance of each of these services; so
  985. * we statically allocate the service specific state.
  986. */
  987. struct hv_util_service {
  988. u8 *recv_buffer;
  989. void (*util_cb)(void *);
  990. int (*util_init)(struct hv_util_service *);
  991. void (*util_deinit)(void);
  992. };
  993. struct vmbuspipe_hdr {
  994. u32 flags;
  995. u32 msgsize;
  996. } __packed;
  997. struct ic_version {
  998. u16 major;
  999. u16 minor;
  1000. } __packed;
  1001. struct icmsg_hdr {
  1002. struct ic_version icverframe;
  1003. u16 icmsgtype;
  1004. struct ic_version icvermsg;
  1005. u16 icmsgsize;
  1006. u32 status;
  1007. u8 ictransaction_id;
  1008. u8 icflags;
  1009. u8 reserved[2];
  1010. } __packed;
  1011. struct icmsg_negotiate {
  1012. u16 icframe_vercnt;
  1013. u16 icmsg_vercnt;
  1014. u32 reserved;
  1015. struct ic_version icversion_data[1]; /* any size array */
  1016. } __packed;
  1017. struct shutdown_msg_data {
  1018. u32 reason_code;
  1019. u32 timeout_seconds;
  1020. u32 flags;
  1021. u8 display_message[2048];
  1022. } __packed;
  1023. struct heartbeat_msg_data {
  1024. u64 seq_num;
  1025. u32 reserved[8];
  1026. } __packed;
  1027. /* Time Sync IC defs */
  1028. #define ICTIMESYNCFLAG_PROBE 0
  1029. #define ICTIMESYNCFLAG_SYNC 1
  1030. #define ICTIMESYNCFLAG_SAMPLE 2
  1031. #ifdef __x86_64__
  1032. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1033. #else
  1034. #define WLTIMEDELTA 116444736000000000LL
  1035. #endif
  1036. struct ictimesync_data {
  1037. u64 parenttime;
  1038. u64 childtime;
  1039. u64 roundtriptime;
  1040. u8 flags;
  1041. } __packed;
  1042. struct hyperv_service_callback {
  1043. u8 msg_type;
  1044. char *log_msg;
  1045. uuid_le data;
  1046. struct vmbus_channel *channel;
  1047. void (*callback) (void *context);
  1048. };
  1049. #define MAX_SRV_VER 0x7ffffff
  1050. extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *,
  1051. struct icmsg_negotiate *, u8 *, int,
  1052. int);
  1053. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid);
  1054. /*
  1055. * Negotiated version with the Host.
  1056. */
  1057. extern __u32 vmbus_proto_version;
  1058. #endif /* _HYPERV_H */