hyperv.h 32 KB

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