hyperv.h 42 KB

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