hyperv.h 41 KB

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