hyperv.h 30 KB

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