hyperv.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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. };
  108. /*
  109. *
  110. * hv_get_ringbuffer_availbytes()
  111. *
  112. * Get number of bytes available to read and to write to
  113. * for the specified ring buffer
  114. */
  115. static inline void
  116. hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
  117. u32 *read, u32 *write)
  118. {
  119. u32 read_loc, write_loc, dsize;
  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. enum hv_signal_policy {
  540. HV_SIGNAL_POLICY_DEFAULT = 0,
  541. HV_SIGNAL_POLICY_EXPLICIT,
  542. };
  543. struct vmbus_channel {
  544. /* Unique channel id */
  545. int id;
  546. struct list_head listentry;
  547. struct hv_device *device_obj;
  548. enum vmbus_channel_state state;
  549. struct vmbus_channel_offer_channel offermsg;
  550. /*
  551. * These are based on the OfferMsg.MonitorId.
  552. * Save it here for easy access.
  553. */
  554. u8 monitor_grp;
  555. u8 monitor_bit;
  556. bool rescind; /* got rescind msg */
  557. u32 ringbuffer_gpadlhandle;
  558. /* Allocated memory for ring buffer */
  559. void *ringbuffer_pages;
  560. u32 ringbuffer_pagecount;
  561. struct hv_ring_buffer_info outbound; /* send to parent */
  562. struct hv_ring_buffer_info inbound; /* receive from parent */
  563. spinlock_t inbound_lock;
  564. struct vmbus_close_msg close_msg;
  565. /* Channel callback are invoked in this workqueue context */
  566. /* HANDLE dataWorkQueue; */
  567. void (*onchannel_callback)(void *context);
  568. void *channel_callback_context;
  569. /*
  570. * A channel can be marked for efficient (batched)
  571. * reading:
  572. * If batched_reading is set to "true", we read until the
  573. * channel is empty and hold off interrupts from the host
  574. * during the entire read process.
  575. * If batched_reading is set to "false", the client is not
  576. * going to perform batched reading.
  577. *
  578. * By default we will enable batched reading; specific
  579. * drivers that don't want this behavior can turn it off.
  580. */
  581. bool batched_reading;
  582. bool is_dedicated_interrupt;
  583. struct hv_input_signal_event_buffer sig_buf;
  584. struct hv_input_signal_event *sig_event;
  585. /*
  586. * Starting with win8, this field will be used to specify
  587. * the target virtual processor on which to deliver the interrupt for
  588. * the host to guest communication.
  589. * Prior to win8, incoming channel interrupts would only
  590. * be delivered on cpu 0. Setting this value to 0 would
  591. * preserve the earlier behavior.
  592. */
  593. u32 target_vp;
  594. /* The corresponding CPUID in the guest */
  595. u32 target_cpu;
  596. /*
  597. * State to manage the CPU affiliation of channels.
  598. */
  599. struct cpumask alloced_cpus_in_node;
  600. int numa_node;
  601. /*
  602. * Support for sub-channels. For high performance devices,
  603. * it will be useful to have multiple sub-channels to support
  604. * a scalable communication infrastructure with the host.
  605. * The support for sub-channels is implemented as an extention
  606. * to the current infrastructure.
  607. * The initial offer is considered the primary channel and this
  608. * offer message will indicate if the host supports sub-channels.
  609. * The guest is free to ask for sub-channels to be offerred and can
  610. * open these sub-channels as a normal "primary" channel. However,
  611. * all sub-channels will have the same type and instance guids as the
  612. * primary channel. Requests sent on a given channel will result in a
  613. * response on the same channel.
  614. */
  615. /*
  616. * Sub-channel creation callback. This callback will be called in
  617. * process context when a sub-channel offer is received from the host.
  618. * The guest can open the sub-channel in the context of this callback.
  619. */
  620. void (*sc_creation_callback)(struct vmbus_channel *new_sc);
  621. /*
  622. * The spinlock to protect the structure. It is being used to protect
  623. * test-and-set access to various attributes of the structure as well
  624. * as all sc_list operations.
  625. */
  626. spinlock_t lock;
  627. /*
  628. * All Sub-channels of a primary channel are linked here.
  629. */
  630. struct list_head sc_list;
  631. /*
  632. * Current number of sub-channels.
  633. */
  634. int num_sc;
  635. /*
  636. * Number of a sub-channel (position within sc_list) which is supposed
  637. * to be used as the next outgoing channel.
  638. */
  639. int next_oc;
  640. /*
  641. * The primary channel this sub-channel belongs to.
  642. * This will be NULL for the primary channel.
  643. */
  644. struct vmbus_channel *primary_channel;
  645. /*
  646. * Support per-channel state for use by vmbus drivers.
  647. */
  648. void *per_channel_state;
  649. /*
  650. * To support per-cpu lookup mapping of relid to channel,
  651. * link up channels based on their CPU affinity.
  652. */
  653. struct list_head percpu_list;
  654. /*
  655. * Host signaling policy: The default policy will be
  656. * based on the ring buffer state. We will also support
  657. * a policy where the client driver can have explicit
  658. * signaling control.
  659. */
  660. enum hv_signal_policy signal_policy;
  661. };
  662. static inline void set_channel_signal_state(struct vmbus_channel *c,
  663. enum hv_signal_policy policy)
  664. {
  665. c->signal_policy = policy;
  666. }
  667. static inline void set_channel_read_state(struct vmbus_channel *c, bool state)
  668. {
  669. c->batched_reading = state;
  670. }
  671. static inline void set_per_channel_state(struct vmbus_channel *c, void *s)
  672. {
  673. c->per_channel_state = s;
  674. }
  675. static inline void *get_per_channel_state(struct vmbus_channel *c)
  676. {
  677. return c->per_channel_state;
  678. }
  679. void vmbus_onmessage(void *context);
  680. int vmbus_request_offers(void);
  681. /*
  682. * APIs for managing sub-channels.
  683. */
  684. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  685. void (*sc_cr_cb)(struct vmbus_channel *new_sc));
  686. /*
  687. * Retrieve the (sub) channel on which to send an outgoing request.
  688. * When a primary channel has multiple sub-channels, we choose a
  689. * channel whose VCPU binding is closest to the VCPU on which
  690. * this call is being made.
  691. */
  692. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary);
  693. /*
  694. * Check if sub-channels have already been offerred. This API will be useful
  695. * when the driver is unloaded after establishing sub-channels. In this case,
  696. * when the driver is re-loaded, the driver would have to check if the
  697. * subchannels have already been established before attempting to request
  698. * the creation of sub-channels.
  699. * This function returns TRUE to indicate that subchannels have already been
  700. * created.
  701. * This function should be invoked after setting the callback function for
  702. * sub-channel creation.
  703. */
  704. bool vmbus_are_subchannels_present(struct vmbus_channel *primary);
  705. /* The format must be the same as struct vmdata_gpa_direct */
  706. struct vmbus_channel_packet_page_buffer {
  707. u16 type;
  708. u16 dataoffset8;
  709. u16 length8;
  710. u16 flags;
  711. u64 transactionid;
  712. u32 reserved;
  713. u32 rangecount;
  714. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  715. } __packed;
  716. /* The format must be the same as struct vmdata_gpa_direct */
  717. struct vmbus_channel_packet_multipage_buffer {
  718. u16 type;
  719. u16 dataoffset8;
  720. u16 length8;
  721. u16 flags;
  722. u64 transactionid;
  723. u32 reserved;
  724. u32 rangecount; /* Always 1 in this case */
  725. struct hv_multipage_buffer range;
  726. } __packed;
  727. /* The format must be the same as struct vmdata_gpa_direct */
  728. struct vmbus_packet_mpb_array {
  729. u16 type;
  730. u16 dataoffset8;
  731. u16 length8;
  732. u16 flags;
  733. u64 transactionid;
  734. u32 reserved;
  735. u32 rangecount; /* Always 1 in this case */
  736. struct hv_mpb_array range;
  737. } __packed;
  738. extern int vmbus_open(struct vmbus_channel *channel,
  739. u32 send_ringbuffersize,
  740. u32 recv_ringbuffersize,
  741. void *userdata,
  742. u32 userdatalen,
  743. void(*onchannel_callback)(void *context),
  744. void *context);
  745. extern void vmbus_close(struct vmbus_channel *channel);
  746. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  747. void *buffer,
  748. u32 bufferLen,
  749. u64 requestid,
  750. enum vmbus_packet_type type,
  751. u32 flags);
  752. extern int vmbus_sendpacket_ctl(struct vmbus_channel *channel,
  753. void *buffer,
  754. u32 bufferLen,
  755. u64 requestid,
  756. enum vmbus_packet_type type,
  757. u32 flags,
  758. bool kick_q);
  759. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  760. struct hv_page_buffer pagebuffers[],
  761. u32 pagecount,
  762. void *buffer,
  763. u32 bufferlen,
  764. u64 requestid);
  765. extern int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
  766. struct hv_page_buffer pagebuffers[],
  767. u32 pagecount,
  768. void *buffer,
  769. u32 bufferlen,
  770. u64 requestid,
  771. u32 flags,
  772. bool kick_q);
  773. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  774. struct hv_multipage_buffer *mpb,
  775. void *buffer,
  776. u32 bufferlen,
  777. u64 requestid);
  778. extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  779. struct vmbus_packet_mpb_array *mpb,
  780. u32 desc_size,
  781. void *buffer,
  782. u32 bufferlen,
  783. u64 requestid);
  784. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  785. void *kbuffer,
  786. u32 size,
  787. u32 *gpadl_handle);
  788. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  789. u32 gpadl_handle);
  790. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  791. void *buffer,
  792. u32 bufferlen,
  793. u32 *buffer_actual_len,
  794. u64 *requestid);
  795. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  796. void *buffer,
  797. u32 bufferlen,
  798. u32 *buffer_actual_len,
  799. u64 *requestid);
  800. extern void vmbus_ontimer(unsigned long data);
  801. /* Base driver object */
  802. struct hv_driver {
  803. const char *name;
  804. /* the device type supported by this driver */
  805. uuid_le dev_type;
  806. const struct hv_vmbus_device_id *id_table;
  807. struct device_driver driver;
  808. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  809. int (*remove)(struct hv_device *);
  810. void (*shutdown)(struct hv_device *);
  811. };
  812. /* Base device object */
  813. struct hv_device {
  814. /* the device type id of this device */
  815. uuid_le dev_type;
  816. /* the device instance id of this device */
  817. uuid_le dev_instance;
  818. struct device device;
  819. struct vmbus_channel *channel;
  820. };
  821. static inline struct hv_device *device_to_hv_device(struct device *d)
  822. {
  823. return container_of(d, struct hv_device, device);
  824. }
  825. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  826. {
  827. return container_of(d, struct hv_driver, driver);
  828. }
  829. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  830. {
  831. dev_set_drvdata(&dev->device, data);
  832. }
  833. static inline void *hv_get_drvdata(struct hv_device *dev)
  834. {
  835. return dev_get_drvdata(&dev->device);
  836. }
  837. /* Vmbus interface */
  838. #define vmbus_driver_register(driver) \
  839. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  840. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  841. struct module *owner,
  842. const char *mod_name);
  843. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  844. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  845. resource_size_t min, resource_size_t max,
  846. resource_size_t size, resource_size_t align,
  847. bool fb_overlap_ok);
  848. int vmbus_cpu_number_to_vp_number(int cpu_number);
  849. u64 hv_do_hypercall(u64 control, void *input, void *output);
  850. /*
  851. * GUID definitions of various offer types - services offered to the guest.
  852. */
  853. /*
  854. * Network GUID
  855. * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
  856. */
  857. #define HV_NIC_GUID \
  858. .guid = UUID_LE(0xf8615163, 0xdf3e, 0x46c5, 0x91, 0x3f, \
  859. 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e)
  860. /*
  861. * IDE GUID
  862. * {32412632-86cb-44a2-9b5c-50d1417354f5}
  863. */
  864. #define HV_IDE_GUID \
  865. .guid = UUID_LE(0x32412632, 0x86cb, 0x44a2, 0x9b, 0x5c, \
  866. 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  867. /*
  868. * SCSI GUID
  869. * {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
  870. */
  871. #define HV_SCSI_GUID \
  872. .guid = UUID_LE(0xba6163d9, 0x04a1, 0x4d29, 0xb6, 0x05, \
  873. 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  874. /*
  875. * Shutdown GUID
  876. * {0e0b6031-5213-4934-818b-38d90ced39db}
  877. */
  878. #define HV_SHUTDOWN_GUID \
  879. .guid = UUID_LE(0x0e0b6031, 0x5213, 0x4934, 0x81, 0x8b, \
  880. 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb)
  881. /*
  882. * Time Synch GUID
  883. * {9527E630-D0AE-497b-ADCE-E80AB0175CAF}
  884. */
  885. #define HV_TS_GUID \
  886. .guid = UUID_LE(0x9527e630, 0xd0ae, 0x497b, 0xad, 0xce, \
  887. 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
  888. /*
  889. * Heartbeat GUID
  890. * {57164f39-9115-4e78-ab55-382f3bd5422d}
  891. */
  892. #define HV_HEART_BEAT_GUID \
  893. .guid = UUID_LE(0x57164f39, 0x9115, 0x4e78, 0xab, 0x55, \
  894. 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
  895. /*
  896. * KVP GUID
  897. * {a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}
  898. */
  899. #define HV_KVP_GUID \
  900. .guid = UUID_LE(0xa9a0f4e7, 0x5a45, 0x4d96, 0xb8, 0x27, \
  901. 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6)
  902. /*
  903. * Dynamic memory GUID
  904. * {525074dc-8985-46e2-8057-a307dc18a502}
  905. */
  906. #define HV_DM_GUID \
  907. .guid = UUID_LE(0x525074dc, 0x8985, 0x46e2, 0x80, 0x57, \
  908. 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02)
  909. /*
  910. * Mouse GUID
  911. * {cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}
  912. */
  913. #define HV_MOUSE_GUID \
  914. .guid = UUID_LE(0xcfa8b69e, 0x5b4a, 0x4cc0, 0xb9, 0x8b, \
  915. 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a)
  916. /*
  917. * VSS (Backup/Restore) GUID
  918. */
  919. #define HV_VSS_GUID \
  920. .guid = UUID_LE(0x35fa2e29, 0xea23, 0x4236, 0x96, 0xae, \
  921. 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40)
  922. /*
  923. * Synthetic Video GUID
  924. * {DA0A7802-E377-4aac-8E77-0558EB1073F8}
  925. */
  926. #define HV_SYNTHVID_GUID \
  927. .guid = UUID_LE(0xda0a7802, 0xe377, 0x4aac, 0x8e, 0x77, \
  928. 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8)
  929. /*
  930. * Synthetic FC GUID
  931. * {2f9bcc4a-0069-4af3-b76b-6fd0be528cda}
  932. */
  933. #define HV_SYNTHFC_GUID \
  934. .guid = UUID_LE(0x2f9bcc4a, 0x0069, 0x4af3, 0xb7, 0x6b, \
  935. 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda)
  936. /*
  937. * Guest File Copy Service
  938. * {34D14BE3-DEE4-41c8-9AE7-6B174977C192}
  939. */
  940. #define HV_FCOPY_GUID \
  941. .guid = UUID_LE(0x34d14be3, 0xdee4, 0x41c8, 0x9a, 0xe7, \
  942. 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92)
  943. /*
  944. * NetworkDirect. This is the guest RDMA service.
  945. * {8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}
  946. */
  947. #define HV_ND_GUID \
  948. .guid = UUID_LE(0x8c2eaf3d, 0x32a7, 0x4b09, 0xab, 0x99, \
  949. 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01)
  950. /*
  951. * PCI Express Pass Through
  952. * {44C4F61D-4444-4400-9D52-802E27EDE19F}
  953. */
  954. #define HV_PCIE_GUID \
  955. .guid = UUID_LE(0x44c4f61d, 0x4444, 0x4400, 0x9d, 0x52, \
  956. 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f)
  957. /*
  958. * Common header for Hyper-V ICs
  959. */
  960. #define ICMSGTYPE_NEGOTIATE 0
  961. #define ICMSGTYPE_HEARTBEAT 1
  962. #define ICMSGTYPE_KVPEXCHANGE 2
  963. #define ICMSGTYPE_SHUTDOWN 3
  964. #define ICMSGTYPE_TIMESYNC 4
  965. #define ICMSGTYPE_VSS 5
  966. #define ICMSGHDRFLAG_TRANSACTION 1
  967. #define ICMSGHDRFLAG_REQUEST 2
  968. #define ICMSGHDRFLAG_RESPONSE 4
  969. /*
  970. * While we want to handle util services as regular devices,
  971. * there is only one instance of each of these services; so
  972. * we statically allocate the service specific state.
  973. */
  974. struct hv_util_service {
  975. u8 *recv_buffer;
  976. void (*util_cb)(void *);
  977. int (*util_init)(struct hv_util_service *);
  978. void (*util_deinit)(void);
  979. };
  980. struct vmbuspipe_hdr {
  981. u32 flags;
  982. u32 msgsize;
  983. } __packed;
  984. struct ic_version {
  985. u16 major;
  986. u16 minor;
  987. } __packed;
  988. struct icmsg_hdr {
  989. struct ic_version icverframe;
  990. u16 icmsgtype;
  991. struct ic_version icvermsg;
  992. u16 icmsgsize;
  993. u32 status;
  994. u8 ictransaction_id;
  995. u8 icflags;
  996. u8 reserved[2];
  997. } __packed;
  998. struct icmsg_negotiate {
  999. u16 icframe_vercnt;
  1000. u16 icmsg_vercnt;
  1001. u32 reserved;
  1002. struct ic_version icversion_data[1]; /* any size array */
  1003. } __packed;
  1004. struct shutdown_msg_data {
  1005. u32 reason_code;
  1006. u32 timeout_seconds;
  1007. u32 flags;
  1008. u8 display_message[2048];
  1009. } __packed;
  1010. struct heartbeat_msg_data {
  1011. u64 seq_num;
  1012. u32 reserved[8];
  1013. } __packed;
  1014. /* Time Sync IC defs */
  1015. #define ICTIMESYNCFLAG_PROBE 0
  1016. #define ICTIMESYNCFLAG_SYNC 1
  1017. #define ICTIMESYNCFLAG_SAMPLE 2
  1018. #ifdef __x86_64__
  1019. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1020. #else
  1021. #define WLTIMEDELTA 116444736000000000LL
  1022. #endif
  1023. struct ictimesync_data {
  1024. u64 parenttime;
  1025. u64 childtime;
  1026. u64 roundtriptime;
  1027. u8 flags;
  1028. } __packed;
  1029. struct hyperv_service_callback {
  1030. u8 msg_type;
  1031. char *log_msg;
  1032. uuid_le data;
  1033. struct vmbus_channel *channel;
  1034. void (*callback) (void *context);
  1035. };
  1036. #define MAX_SRV_VER 0x7ffffff
  1037. extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *,
  1038. struct icmsg_negotiate *, u8 *, int,
  1039. int);
  1040. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid);
  1041. /*
  1042. * Negotiated version with the Host.
  1043. */
  1044. extern __u32 vmbus_proto_version;
  1045. #endif /* _HYPERV_H */