hyperv.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. *
  4. * Copyright (c) 2011, Microsoft Corporation.
  5. *
  6. * Authors:
  7. * Haiyang Zhang <[email protected]>
  8. * Hank Janssen <[email protected]>
  9. * K. Y. Srinivasan <[email protected]>
  10. */
  11. #ifndef _HYPERV_H
  12. #define _HYPERV_H
  13. #include <uapi/linux/hyperv.h>
  14. #include <linux/mm.h>
  15. #include <linux/types.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/list.h>
  18. #include <linux/timer.h>
  19. #include <linux/completion.h>
  20. #include <linux/device.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/reciprocal_div.h>
  24. #include <asm/hyperv-tlfs.h>
  25. #define MAX_PAGE_BUFFER_COUNT 32
  26. #define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
  27. #pragma pack(push, 1)
  28. /*
  29. * Types for GPADL, decides is how GPADL header is created.
  30. *
  31. * It doesn't make much difference between BUFFER and RING if PAGE_SIZE is the
  32. * same as HV_HYP_PAGE_SIZE.
  33. *
  34. * If PAGE_SIZE is bigger than HV_HYP_PAGE_SIZE, the headers of ring buffers
  35. * will be of PAGE_SIZE, however, only the first HV_HYP_PAGE will be put
  36. * into gpadl, therefore the number for HV_HYP_PAGE and the indexes of each
  37. * HV_HYP_PAGE will be different between different types of GPADL, for example
  38. * if PAGE_SIZE is 64K:
  39. *
  40. * BUFFER:
  41. *
  42. * gva: |-- 64k --|-- 64k --| ... |
  43. * gpa: | 4k | 4k | ... | 4k | 4k | 4k | ... | 4k |
  44. * index: 0 1 2 15 16 17 18 .. 31 32 ...
  45. * | | ... | | | ... | ...
  46. * v V V V V V
  47. * gpadl: | 4k | 4k | ... | 4k | 4k | 4k | ... | 4k | ... |
  48. * index: 0 1 2 ... 15 16 17 18 .. 31 32 ...
  49. *
  50. * RING:
  51. *
  52. * | header | data | header | data |
  53. * gva: |-- 64k --|-- 64k --| ... |-- 64k --|-- 64k --| ... |
  54. * gpa: | 4k | .. | 4k | 4k | ... | 4k | ... | 4k | .. | 4k | .. | ... |
  55. * index: 0 1 16 17 18 31 ... n n+1 n+16 ... 2n
  56. * | / / / | / /
  57. * | / / / | / /
  58. * | / / ... / ... | / ... /
  59. * | / / / | / /
  60. * | / / / | / /
  61. * V V V V V V v
  62. * gpadl: | 4k | 4k | ... | ... | 4k | 4k | ... |
  63. * index: 0 1 2 ... 16 ... n-15 n-14 n-13 ... 2n-30
  64. */
  65. enum hv_gpadl_type {
  66. HV_GPADL_BUFFER,
  67. HV_GPADL_RING
  68. };
  69. /* Single-page buffer */
  70. struct hv_page_buffer {
  71. u32 len;
  72. u32 offset;
  73. u64 pfn;
  74. };
  75. /* Multiple-page buffer */
  76. struct hv_multipage_buffer {
  77. /* Length and Offset determines the # of pfns in the array */
  78. u32 len;
  79. u32 offset;
  80. u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
  81. };
  82. /*
  83. * Multiple-page buffer array; the pfn array is variable size:
  84. * The number of entries in the PFN array is determined by
  85. * "len" and "offset".
  86. */
  87. struct hv_mpb_array {
  88. /* Length and Offset determines the # of pfns in the array */
  89. u32 len;
  90. u32 offset;
  91. u64 pfn_array[];
  92. };
  93. /* 0x18 includes the proprietary packet header */
  94. #define MAX_PAGE_BUFFER_PACKET (0x18 + \
  95. (sizeof(struct hv_page_buffer) * \
  96. MAX_PAGE_BUFFER_COUNT))
  97. #define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
  98. sizeof(struct hv_multipage_buffer))
  99. #pragma pack(pop)
  100. struct hv_ring_buffer {
  101. /* Offset in bytes from the start of ring data below */
  102. u32 write_index;
  103. /* Offset in bytes from the start of ring data below */
  104. u32 read_index;
  105. u32 interrupt_mask;
  106. /*
  107. * WS2012/Win8 and later versions of Hyper-V implement interrupt
  108. * driven flow management. The feature bit feat_pending_send_sz
  109. * is set by the host on the host->guest ring buffer, and by the
  110. * guest on the guest->host ring buffer.
  111. *
  112. * The meaning of the feature bit is a bit complex in that it has
  113. * semantics that apply to both ring buffers. If the guest sets
  114. * the feature bit in the guest->host ring buffer, the guest is
  115. * telling the host that:
  116. * 1) It will set the pending_send_sz field in the guest->host ring
  117. * buffer when it is waiting for space to become available, and
  118. * 2) It will read the pending_send_sz field in the host->guest
  119. * ring buffer and interrupt the host when it frees enough space
  120. *
  121. * Similarly, if the host sets the feature bit in the host->guest
  122. * ring buffer, the host is telling the guest that:
  123. * 1) It will set the pending_send_sz field in the host->guest ring
  124. * buffer when it is waiting for space to become available, and
  125. * 2) It will read the pending_send_sz field in the guest->host
  126. * ring buffer and interrupt the guest when it frees enough space
  127. *
  128. * If either the guest or host does not set the feature bit that it
  129. * owns, that guest or host must do polling if it encounters a full
  130. * ring buffer, and not signal the other end with an interrupt.
  131. */
  132. u32 pending_send_sz;
  133. u32 reserved1[12];
  134. union {
  135. struct {
  136. u32 feat_pending_send_sz:1;
  137. };
  138. u32 value;
  139. } feature_bits;
  140. /* Pad it to PAGE_SIZE so that data starts on page boundary */
  141. u8 reserved2[PAGE_SIZE - 68];
  142. /*
  143. * Ring data starts here + RingDataStartOffset
  144. * !!! DO NOT place any fields below this !!!
  145. */
  146. u8 buffer[];
  147. } __packed;
  148. /* Calculate the proper size of a ringbuffer, it must be page-aligned */
  149. #define VMBUS_RING_SIZE(payload_sz) PAGE_ALIGN(sizeof(struct hv_ring_buffer) + \
  150. (payload_sz))
  151. struct hv_ring_buffer_info {
  152. struct hv_ring_buffer *ring_buffer;
  153. u32 ring_size; /* Include the shared header */
  154. struct reciprocal_value ring_size_div10_reciprocal;
  155. spinlock_t ring_lock;
  156. u32 ring_datasize; /* < ring_size */
  157. u32 priv_read_index;
  158. /*
  159. * The ring buffer mutex lock. This lock prevents the ring buffer from
  160. * being freed while the ring buffer is being accessed.
  161. */
  162. struct mutex ring_buffer_mutex;
  163. /* Buffer that holds a copy of an incoming host packet */
  164. void *pkt_buffer;
  165. u32 pkt_buffer_size;
  166. };
  167. static inline u32 hv_get_bytes_to_read(const struct hv_ring_buffer_info *rbi)
  168. {
  169. u32 read_loc, write_loc, dsize, read;
  170. dsize = rbi->ring_datasize;
  171. read_loc = rbi->ring_buffer->read_index;
  172. write_loc = READ_ONCE(rbi->ring_buffer->write_index);
  173. read = write_loc >= read_loc ? (write_loc - read_loc) :
  174. (dsize - read_loc) + write_loc;
  175. return read;
  176. }
  177. static inline u32 hv_get_bytes_to_write(const struct hv_ring_buffer_info *rbi)
  178. {
  179. u32 read_loc, write_loc, dsize, write;
  180. dsize = rbi->ring_datasize;
  181. read_loc = READ_ONCE(rbi->ring_buffer->read_index);
  182. write_loc = rbi->ring_buffer->write_index;
  183. write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  184. read_loc - write_loc;
  185. return write;
  186. }
  187. static inline u32 hv_get_avail_to_write_percent(
  188. const struct hv_ring_buffer_info *rbi)
  189. {
  190. u32 avail_write = hv_get_bytes_to_write(rbi);
  191. return reciprocal_divide(
  192. (avail_write << 3) + (avail_write << 1),
  193. rbi->ring_size_div10_reciprocal);
  194. }
  195. /*
  196. * VMBUS version is 32 bit entity broken up into
  197. * two 16 bit quantities: major_number. minor_number.
  198. *
  199. * 0 . 13 (Windows Server 2008)
  200. * 1 . 1 (Windows 7, WS2008 R2)
  201. * 2 . 4 (Windows 8, WS2012)
  202. * 3 . 0 (Windows 8.1, WS2012 R2)
  203. * 4 . 0 (Windows 10)
  204. * 4 . 1 (Windows 10 RS3)
  205. * 5 . 0 (Newer Windows 10)
  206. * 5 . 1 (Windows 10 RS4)
  207. * 5 . 2 (Windows Server 2019, RS5)
  208. * 5 . 3 (Windows Server 2022)
  209. *
  210. * The WS2008 and WIN7 versions are listed here for
  211. * completeness but are no longer supported in the
  212. * Linux kernel.
  213. */
  214. #define VERSION_WS2008 ((0 << 16) | (13))
  215. #define VERSION_WIN7 ((1 << 16) | (1))
  216. #define VERSION_WIN8 ((2 << 16) | (4))
  217. #define VERSION_WIN8_1 ((3 << 16) | (0))
  218. #define VERSION_WIN10 ((4 << 16) | (0))
  219. #define VERSION_WIN10_V4_1 ((4 << 16) | (1))
  220. #define VERSION_WIN10_V5 ((5 << 16) | (0))
  221. #define VERSION_WIN10_V5_1 ((5 << 16) | (1))
  222. #define VERSION_WIN10_V5_2 ((5 << 16) | (2))
  223. #define VERSION_WIN10_V5_3 ((5 << 16) | (3))
  224. /* Make maximum size of pipe payload of 16K */
  225. #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
  226. /* Define PipeMode values. */
  227. #define VMBUS_PIPE_TYPE_BYTE 0x00000000
  228. #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
  229. /* The size of the user defined data buffer for non-pipe offers. */
  230. #define MAX_USER_DEFINED_BYTES 120
  231. /* The size of the user defined data buffer for pipe offers. */
  232. #define MAX_PIPE_USER_DEFINED_BYTES 116
  233. /*
  234. * At the center of the Channel Management library is the Channel Offer. This
  235. * struct contains the fundamental information about an offer.
  236. */
  237. struct vmbus_channel_offer {
  238. guid_t if_type;
  239. guid_t if_instance;
  240. /*
  241. * These two fields are not currently used.
  242. */
  243. u64 reserved1;
  244. u64 reserved2;
  245. u16 chn_flags;
  246. u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
  247. union {
  248. /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
  249. struct {
  250. unsigned char user_def[MAX_USER_DEFINED_BYTES];
  251. } std;
  252. /*
  253. * Pipes:
  254. * The following structure is an integrated pipe protocol, which
  255. * is implemented on top of standard user-defined data. Pipe
  256. * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
  257. * use.
  258. */
  259. struct {
  260. u32 pipe_mode;
  261. unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
  262. } pipe;
  263. } u;
  264. /*
  265. * The sub_channel_index is defined in Win8: a value of zero means a
  266. * primary channel and a value of non-zero means a sub-channel.
  267. *
  268. * Before Win8, the field is reserved, meaning it's always zero.
  269. */
  270. u16 sub_channel_index;
  271. u16 reserved3;
  272. } __packed;
  273. /* Server Flags */
  274. #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
  275. #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
  276. #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
  277. #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
  278. #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
  279. #define VMBUS_CHANNEL_PARENT_OFFER 0x200
  280. #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
  281. #define VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER 0x2000
  282. struct vmpacket_descriptor {
  283. u16 type;
  284. u16 offset8;
  285. u16 len8;
  286. u16 flags;
  287. u64 trans_id;
  288. } __packed;
  289. struct vmpacket_header {
  290. u32 prev_pkt_start_offset;
  291. struct vmpacket_descriptor descriptor;
  292. } __packed;
  293. struct vmtransfer_page_range {
  294. u32 byte_count;
  295. u32 byte_offset;
  296. } __packed;
  297. struct vmtransfer_page_packet_header {
  298. struct vmpacket_descriptor d;
  299. u16 xfer_pageset_id;
  300. u8 sender_owns_set;
  301. u8 reserved;
  302. u32 range_cnt;
  303. struct vmtransfer_page_range ranges[1];
  304. } __packed;
  305. struct vmgpadl_packet_header {
  306. struct vmpacket_descriptor d;
  307. u32 gpadl;
  308. u32 reserved;
  309. } __packed;
  310. struct vmadd_remove_transfer_page_set {
  311. struct vmpacket_descriptor d;
  312. u32 gpadl;
  313. u16 xfer_pageset_id;
  314. u16 reserved;
  315. } __packed;
  316. /*
  317. * This structure defines a range in guest physical space that can be made to
  318. * look virtually contiguous.
  319. */
  320. struct gpa_range {
  321. u32 byte_count;
  322. u32 byte_offset;
  323. u64 pfn_array[];
  324. };
  325. /*
  326. * This is the format for an Establish Gpadl packet, which contains a handle by
  327. * which this GPADL will be known and a set of GPA ranges associated with it.
  328. * This can be converted to a MDL by the guest OS. If there are multiple GPA
  329. * ranges, then the resulting MDL will be "chained," representing multiple VA
  330. * ranges.
  331. */
  332. struct vmestablish_gpadl {
  333. struct vmpacket_descriptor d;
  334. u32 gpadl;
  335. u32 range_cnt;
  336. struct gpa_range range[1];
  337. } __packed;
  338. /*
  339. * This is the format for a Teardown Gpadl packet, which indicates that the
  340. * GPADL handle in the Establish Gpadl packet will never be referenced again.
  341. */
  342. struct vmteardown_gpadl {
  343. struct vmpacket_descriptor d;
  344. u32 gpadl;
  345. u32 reserved; /* for alignment to a 8-byte boundary */
  346. } __packed;
  347. /*
  348. * This is the format for a GPA-Direct packet, which contains a set of GPA
  349. * ranges, in addition to commands and/or data.
  350. */
  351. struct vmdata_gpa_direct {
  352. struct vmpacket_descriptor d;
  353. u32 reserved;
  354. u32 range_cnt;
  355. struct gpa_range range[1];
  356. } __packed;
  357. /* This is the format for a Additional Data Packet. */
  358. struct vmadditional_data {
  359. struct vmpacket_descriptor d;
  360. u64 total_bytes;
  361. u32 offset;
  362. u32 byte_cnt;
  363. unsigned char data[1];
  364. } __packed;
  365. union vmpacket_largest_possible_header {
  366. struct vmpacket_descriptor simple_hdr;
  367. struct vmtransfer_page_packet_header xfer_page_hdr;
  368. struct vmgpadl_packet_header gpadl_hdr;
  369. struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
  370. struct vmestablish_gpadl establish_gpadl_hdr;
  371. struct vmteardown_gpadl teardown_gpadl_hdr;
  372. struct vmdata_gpa_direct data_gpa_direct_hdr;
  373. };
  374. #define VMPACKET_DATA_START_ADDRESS(__packet) \
  375. (void *)(((unsigned char *)__packet) + \
  376. ((struct vmpacket_descriptor)__packet)->offset8 * 8)
  377. #define VMPACKET_DATA_LENGTH(__packet) \
  378. ((((struct vmpacket_descriptor)__packet)->len8 - \
  379. ((struct vmpacket_descriptor)__packet)->offset8) * 8)
  380. #define VMPACKET_TRANSFER_MODE(__packet) \
  381. (((struct IMPACT)__packet)->type)
  382. enum vmbus_packet_type {
  383. VM_PKT_INVALID = 0x0,
  384. VM_PKT_SYNCH = 0x1,
  385. VM_PKT_ADD_XFER_PAGESET = 0x2,
  386. VM_PKT_RM_XFER_PAGESET = 0x3,
  387. VM_PKT_ESTABLISH_GPADL = 0x4,
  388. VM_PKT_TEARDOWN_GPADL = 0x5,
  389. VM_PKT_DATA_INBAND = 0x6,
  390. VM_PKT_DATA_USING_XFER_PAGES = 0x7,
  391. VM_PKT_DATA_USING_GPADL = 0x8,
  392. VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
  393. VM_PKT_CANCEL_REQUEST = 0xa,
  394. VM_PKT_COMP = 0xb,
  395. VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
  396. VM_PKT_ADDITIONAL_DATA = 0xd
  397. };
  398. #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
  399. /* Version 1 messages */
  400. enum vmbus_channel_message_type {
  401. CHANNELMSG_INVALID = 0,
  402. CHANNELMSG_OFFERCHANNEL = 1,
  403. CHANNELMSG_RESCIND_CHANNELOFFER = 2,
  404. CHANNELMSG_REQUESTOFFERS = 3,
  405. CHANNELMSG_ALLOFFERS_DELIVERED = 4,
  406. CHANNELMSG_OPENCHANNEL = 5,
  407. CHANNELMSG_OPENCHANNEL_RESULT = 6,
  408. CHANNELMSG_CLOSECHANNEL = 7,
  409. CHANNELMSG_GPADL_HEADER = 8,
  410. CHANNELMSG_GPADL_BODY = 9,
  411. CHANNELMSG_GPADL_CREATED = 10,
  412. CHANNELMSG_GPADL_TEARDOWN = 11,
  413. CHANNELMSG_GPADL_TORNDOWN = 12,
  414. CHANNELMSG_RELID_RELEASED = 13,
  415. CHANNELMSG_INITIATE_CONTACT = 14,
  416. CHANNELMSG_VERSION_RESPONSE = 15,
  417. CHANNELMSG_UNLOAD = 16,
  418. CHANNELMSG_UNLOAD_RESPONSE = 17,
  419. CHANNELMSG_18 = 18,
  420. CHANNELMSG_19 = 19,
  421. CHANNELMSG_20 = 20,
  422. CHANNELMSG_TL_CONNECT_REQUEST = 21,
  423. CHANNELMSG_MODIFYCHANNEL = 22,
  424. CHANNELMSG_TL_CONNECT_RESULT = 23,
  425. CHANNELMSG_MODIFYCHANNEL_RESPONSE = 24,
  426. CHANNELMSG_COUNT
  427. };
  428. /* Hyper-V supports about 2048 channels, and the RELIDs start with 1. */
  429. #define INVALID_RELID U32_MAX
  430. struct vmbus_channel_message_header {
  431. enum vmbus_channel_message_type msgtype;
  432. u32 padding;
  433. } __packed;
  434. /* Query VMBus Version parameters */
  435. struct vmbus_channel_query_vmbus_version {
  436. struct vmbus_channel_message_header header;
  437. u32 version;
  438. } __packed;
  439. /* VMBus Version Supported parameters */
  440. struct vmbus_channel_version_supported {
  441. struct vmbus_channel_message_header header;
  442. u8 version_supported;
  443. } __packed;
  444. /* Offer Channel parameters */
  445. struct vmbus_channel_offer_channel {
  446. struct vmbus_channel_message_header header;
  447. struct vmbus_channel_offer offer;
  448. u32 child_relid;
  449. u8 monitorid;
  450. /*
  451. * win7 and beyond splits this field into a bit field.
  452. */
  453. u8 monitor_allocated:1;
  454. u8 reserved:7;
  455. /*
  456. * These are new fields added in win7 and later.
  457. * Do not access these fields without checking the
  458. * negotiated protocol.
  459. *
  460. * If "is_dedicated_interrupt" is set, we must not set the
  461. * associated bit in the channel bitmap while sending the
  462. * interrupt to the host.
  463. *
  464. * connection_id is to be used in signaling the host.
  465. */
  466. u16 is_dedicated_interrupt:1;
  467. u16 reserved1:15;
  468. u32 connection_id;
  469. } __packed;
  470. /* Rescind Offer parameters */
  471. struct vmbus_channel_rescind_offer {
  472. struct vmbus_channel_message_header header;
  473. u32 child_relid;
  474. } __packed;
  475. /*
  476. * Request Offer -- no parameters, SynIC message contains the partition ID
  477. * Set Snoop -- no parameters, SynIC message contains the partition ID
  478. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  479. * All Offers Delivered -- no parameters, SynIC message contains the partition
  480. * ID
  481. * Flush Client -- no parameters, SynIC message contains the partition ID
  482. */
  483. /* Open Channel parameters */
  484. struct vmbus_channel_open_channel {
  485. struct vmbus_channel_message_header header;
  486. /* Identifies the specific VMBus channel that is being opened. */
  487. u32 child_relid;
  488. /* ID making a particular open request at a channel offer unique. */
  489. u32 openid;
  490. /* GPADL for the channel's ring buffer. */
  491. u32 ringbuffer_gpadlhandle;
  492. /*
  493. * Starting with win8, this field will be used to specify
  494. * the target virtual processor on which to deliver the interrupt for
  495. * the host to guest communication.
  496. * Prior to win8, incoming channel interrupts would only
  497. * be delivered on cpu 0. Setting this value to 0 would
  498. * preserve the earlier behavior.
  499. */
  500. u32 target_vp;
  501. /*
  502. * The upstream ring buffer begins at offset zero in the memory
  503. * described by RingBufferGpadlHandle. The downstream ring buffer
  504. * follows it at this offset (in pages).
  505. */
  506. u32 downstream_ringbuffer_pageoffset;
  507. /* User-specific data to be passed along to the server endpoint. */
  508. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  509. } __packed;
  510. /* Open Channel Result parameters */
  511. struct vmbus_channel_open_result {
  512. struct vmbus_channel_message_header header;
  513. u32 child_relid;
  514. u32 openid;
  515. u32 status;
  516. } __packed;
  517. /* Modify Channel Result parameters */
  518. struct vmbus_channel_modifychannel_response {
  519. struct vmbus_channel_message_header header;
  520. u32 child_relid;
  521. u32 status;
  522. } __packed;
  523. /* Close channel parameters; */
  524. struct vmbus_channel_close_channel {
  525. struct vmbus_channel_message_header header;
  526. u32 child_relid;
  527. } __packed;
  528. /* Channel Message GPADL */
  529. #define GPADL_TYPE_RING_BUFFER 1
  530. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  531. #define GPADL_TYPE_TRANSACTION 8
  532. /*
  533. * The number of PFNs in a GPADL message is defined by the number of
  534. * pages that would be spanned by ByteCount and ByteOffset. If the
  535. * implied number of PFNs won't fit in this packet, there will be a
  536. * follow-up packet that contains more.
  537. */
  538. struct vmbus_channel_gpadl_header {
  539. struct vmbus_channel_message_header header;
  540. u32 child_relid;
  541. u32 gpadl;
  542. u16 range_buflen;
  543. u16 rangecount;
  544. struct gpa_range range[];
  545. } __packed;
  546. /* This is the followup packet that contains more PFNs. */
  547. struct vmbus_channel_gpadl_body {
  548. struct vmbus_channel_message_header header;
  549. u32 msgnumber;
  550. u32 gpadl;
  551. u64 pfn[];
  552. } __packed;
  553. struct vmbus_channel_gpadl_created {
  554. struct vmbus_channel_message_header header;
  555. u32 child_relid;
  556. u32 gpadl;
  557. u32 creation_status;
  558. } __packed;
  559. struct vmbus_channel_gpadl_teardown {
  560. struct vmbus_channel_message_header header;
  561. u32 child_relid;
  562. u32 gpadl;
  563. } __packed;
  564. struct vmbus_channel_gpadl_torndown {
  565. struct vmbus_channel_message_header header;
  566. u32 gpadl;
  567. } __packed;
  568. struct vmbus_channel_relid_released {
  569. struct vmbus_channel_message_header header;
  570. u32 child_relid;
  571. } __packed;
  572. struct vmbus_channel_initiate_contact {
  573. struct vmbus_channel_message_header header;
  574. u32 vmbus_version_requested;
  575. u32 target_vcpu; /* The VCPU the host should respond to */
  576. union {
  577. u64 interrupt_page;
  578. struct {
  579. u8 msg_sint;
  580. u8 padding1[3];
  581. u32 padding2;
  582. };
  583. };
  584. u64 monitor_page1;
  585. u64 monitor_page2;
  586. } __packed;
  587. /* Hyper-V socket: guest's connect()-ing to host */
  588. struct vmbus_channel_tl_connect_request {
  589. struct vmbus_channel_message_header header;
  590. guid_t guest_endpoint_id;
  591. guid_t host_service_id;
  592. } __packed;
  593. /* Modify Channel parameters, cf. vmbus_send_modifychannel() */
  594. struct vmbus_channel_modifychannel {
  595. struct vmbus_channel_message_header header;
  596. u32 child_relid;
  597. u32 target_vp;
  598. } __packed;
  599. struct vmbus_channel_version_response {
  600. struct vmbus_channel_message_header header;
  601. u8 version_supported;
  602. u8 connection_state;
  603. u16 padding;
  604. /*
  605. * On new hosts that support VMBus protocol 5.0, we must use
  606. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
  607. * and for subsequent messages, we must use the Message Connection ID
  608. * field in the host-returned Version Response Message.
  609. *
  610. * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
  611. */
  612. u32 msg_conn_id;
  613. } __packed;
  614. enum vmbus_channel_state {
  615. CHANNEL_OFFER_STATE,
  616. CHANNEL_OPENING_STATE,
  617. CHANNEL_OPEN_STATE,
  618. CHANNEL_OPENED_STATE,
  619. };
  620. /*
  621. * Represents each channel msg on the vmbus connection This is a
  622. * variable-size data structure depending on the msg type itself
  623. */
  624. struct vmbus_channel_msginfo {
  625. /* Bookkeeping stuff */
  626. struct list_head msglistentry;
  627. /* So far, this is only used to handle gpadl body message */
  628. struct list_head submsglist;
  629. /* Synchronize the request/response if needed */
  630. struct completion waitevent;
  631. struct vmbus_channel *waiting_channel;
  632. union {
  633. struct vmbus_channel_version_supported version_supported;
  634. struct vmbus_channel_open_result open_result;
  635. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  636. struct vmbus_channel_gpadl_created gpadl_created;
  637. struct vmbus_channel_version_response version_response;
  638. struct vmbus_channel_modifychannel_response modify_response;
  639. } response;
  640. u32 msgsize;
  641. /*
  642. * The channel message that goes out on the "wire".
  643. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  644. */
  645. unsigned char msg[];
  646. };
  647. struct vmbus_close_msg {
  648. struct vmbus_channel_msginfo info;
  649. struct vmbus_channel_close_channel msg;
  650. };
  651. /* Define connection identifier type. */
  652. union hv_connection_id {
  653. u32 asu32;
  654. struct {
  655. u32 id:24;
  656. u32 reserved:8;
  657. } u;
  658. };
  659. enum vmbus_device_type {
  660. HV_IDE = 0,
  661. HV_SCSI,
  662. HV_FC,
  663. HV_NIC,
  664. HV_ND,
  665. HV_PCIE,
  666. HV_FB,
  667. HV_KBD,
  668. HV_MOUSE,
  669. HV_KVP,
  670. HV_TS,
  671. HV_HB,
  672. HV_SHUTDOWN,
  673. HV_FCOPY,
  674. HV_BACKUP,
  675. HV_DM,
  676. HV_UNKNOWN,
  677. };
  678. /*
  679. * Provides request ids for VMBus. Encapsulates guest memory
  680. * addresses and stores the next available slot in req_arr
  681. * to generate new ids in constant time.
  682. */
  683. struct vmbus_requestor {
  684. u64 *req_arr;
  685. unsigned long *req_bitmap; /* is a given slot available? */
  686. u32 size;
  687. u64 next_request_id;
  688. spinlock_t req_lock; /* provides atomicity */
  689. };
  690. #define VMBUS_NO_RQSTOR U64_MAX
  691. #define VMBUS_RQST_ERROR (U64_MAX - 1)
  692. #define VMBUS_RQST_ADDR_ANY U64_MAX
  693. /* NetVSC-specific */
  694. #define VMBUS_RQST_ID_NO_RESPONSE (U64_MAX - 2)
  695. /* StorVSC-specific */
  696. #define VMBUS_RQST_INIT (U64_MAX - 2)
  697. #define VMBUS_RQST_RESET (U64_MAX - 3)
  698. struct vmbus_device {
  699. u16 dev_type;
  700. guid_t guid;
  701. bool perf_device;
  702. bool allowed_in_isolated;
  703. };
  704. #define VMBUS_DEFAULT_MAX_PKT_SIZE 4096
  705. struct vmbus_gpadl {
  706. u32 gpadl_handle;
  707. u32 size;
  708. void *buffer;
  709. };
  710. struct vmbus_channel {
  711. struct list_head listentry;
  712. struct hv_device *device_obj;
  713. enum vmbus_channel_state state;
  714. struct vmbus_channel_offer_channel offermsg;
  715. /*
  716. * These are based on the OfferMsg.MonitorId.
  717. * Save it here for easy access.
  718. */
  719. u8 monitor_grp;
  720. u8 monitor_bit;
  721. bool rescind; /* got rescind msg */
  722. bool rescind_ref; /* got rescind msg, got channel reference */
  723. struct completion rescind_event;
  724. struct vmbus_gpadl ringbuffer_gpadlhandle;
  725. /* Allocated memory for ring buffer */
  726. struct page *ringbuffer_page;
  727. u32 ringbuffer_pagecount;
  728. u32 ringbuffer_send_offset;
  729. struct hv_ring_buffer_info outbound; /* send to parent */
  730. struct hv_ring_buffer_info inbound; /* receive from parent */
  731. struct vmbus_close_msg close_msg;
  732. /* Statistics */
  733. u64 interrupts; /* Host to Guest interrupts */
  734. u64 sig_events; /* Guest to Host events */
  735. /*
  736. * Guest to host interrupts caused by the outbound ring buffer changing
  737. * from empty to not empty.
  738. */
  739. u64 intr_out_empty;
  740. /*
  741. * Indicates that a full outbound ring buffer was encountered. The flag
  742. * is set to true when a full outbound ring buffer is encountered and
  743. * set to false when a write to the outbound ring buffer is completed.
  744. */
  745. bool out_full_flag;
  746. /* Channel callback's invoked in softirq context */
  747. struct tasklet_struct callback_event;
  748. void (*onchannel_callback)(void *context);
  749. void *channel_callback_context;
  750. void (*change_target_cpu_callback)(struct vmbus_channel *channel,
  751. u32 old, u32 new);
  752. /*
  753. * Synchronize channel scheduling and channel removal; see the inline
  754. * comments in vmbus_chan_sched() and vmbus_reset_channel_cb().
  755. */
  756. spinlock_t sched_lock;
  757. /*
  758. * A channel can be marked for one of three modes of reading:
  759. * BATCHED - callback called from taslket and should read
  760. * channel until empty. Interrupts from the host
  761. * are masked while read is in process (default).
  762. * DIRECT - callback called from tasklet (softirq).
  763. * ISR - callback called in interrupt context and must
  764. * invoke its own deferred processing.
  765. * Host interrupts are disabled and must be re-enabled
  766. * when ring is empty.
  767. */
  768. enum hv_callback_mode {
  769. HV_CALL_BATCHED,
  770. HV_CALL_DIRECT,
  771. HV_CALL_ISR
  772. } callback_mode;
  773. bool is_dedicated_interrupt;
  774. u64 sig_event;
  775. /*
  776. * Starting with win8, this field will be used to specify the
  777. * target CPU on which to deliver the interrupt for the host
  778. * to guest communication.
  779. *
  780. * Prior to win8, incoming channel interrupts would only be
  781. * delivered on CPU 0. Setting this value to 0 would preserve
  782. * the earlier behavior.
  783. */
  784. u32 target_cpu;
  785. /*
  786. * Support for sub-channels. For high performance devices,
  787. * it will be useful to have multiple sub-channels to support
  788. * a scalable communication infrastructure with the host.
  789. * The support for sub-channels is implemented as an extension
  790. * to the current infrastructure.
  791. * The initial offer is considered the primary channel and this
  792. * offer message will indicate if the host supports sub-channels.
  793. * The guest is free to ask for sub-channels to be offered and can
  794. * open these sub-channels as a normal "primary" channel. However,
  795. * all sub-channels will have the same type and instance guids as the
  796. * primary channel. Requests sent on a given channel will result in a
  797. * response on the same channel.
  798. */
  799. /*
  800. * Sub-channel creation callback. This callback will be called in
  801. * process context when a sub-channel offer is received from the host.
  802. * The guest can open the sub-channel in the context of this callback.
  803. */
  804. void (*sc_creation_callback)(struct vmbus_channel *new_sc);
  805. /*
  806. * Channel rescind callback. Some channels (the hvsock ones), need to
  807. * register a callback which is invoked in vmbus_onoffer_rescind().
  808. */
  809. void (*chn_rescind_callback)(struct vmbus_channel *channel);
  810. /*
  811. * All Sub-channels of a primary channel are linked here.
  812. */
  813. struct list_head sc_list;
  814. /*
  815. * The primary channel this sub-channel belongs to.
  816. * This will be NULL for the primary channel.
  817. */
  818. struct vmbus_channel *primary_channel;
  819. /*
  820. * Support per-channel state for use by vmbus drivers.
  821. */
  822. void *per_channel_state;
  823. /*
  824. * Defer freeing channel until after all cpu's have
  825. * gone through grace period.
  826. */
  827. struct rcu_head rcu;
  828. /*
  829. * For sysfs per-channel properties.
  830. */
  831. struct kobject kobj;
  832. /*
  833. * For performance critical channels (storage, networking
  834. * etc,), Hyper-V has a mechanism to enhance the throughput
  835. * at the expense of latency:
  836. * When the host is to be signaled, we just set a bit in a shared page
  837. * and this bit will be inspected by the hypervisor within a certain
  838. * window and if the bit is set, the host will be signaled. The window
  839. * of time is the monitor latency - currently around 100 usecs. This
  840. * mechanism improves throughput by:
  841. *
  842. * A) Making the host more efficient - each time it wakes up,
  843. * potentially it will process morev number of packets. The
  844. * monitor latency allows a batch to build up.
  845. * B) By deferring the hypercall to signal, we will also minimize
  846. * the interrupts.
  847. *
  848. * Clearly, these optimizations improve throughput at the expense of
  849. * latency. Furthermore, since the channel is shared for both
  850. * control and data messages, control messages currently suffer
  851. * unnecessary latency adversely impacting performance and boot
  852. * time. To fix this issue, permit tagging the channel as being
  853. * in "low latency" mode. In this mode, we will bypass the monitor
  854. * mechanism.
  855. */
  856. bool low_latency;
  857. bool probe_done;
  858. /*
  859. * Cache the device ID here for easy access; this is useful, in
  860. * particular, in situations where the channel's device_obj has
  861. * not been allocated/initialized yet.
  862. */
  863. u16 device_id;
  864. /*
  865. * We must offload the handling of the primary/sub channels
  866. * from the single-threaded vmbus_connection.work_queue to
  867. * two different workqueue, otherwise we can block
  868. * vmbus_connection.work_queue and hang: see vmbus_process_offer().
  869. */
  870. struct work_struct add_channel_work;
  871. /*
  872. * Guest to host interrupts caused by the inbound ring buffer changing
  873. * from full to not full while a packet is waiting.
  874. */
  875. u64 intr_in_full;
  876. /*
  877. * The total number of write operations that encountered a full
  878. * outbound ring buffer.
  879. */
  880. u64 out_full_total;
  881. /*
  882. * The number of write operations that were the first to encounter a
  883. * full outbound ring buffer.
  884. */
  885. u64 out_full_first;
  886. /* enabling/disabling fuzz testing on the channel (default is false)*/
  887. bool fuzz_testing_state;
  888. /*
  889. * Interrupt delay will delay the guest from emptying the ring buffer
  890. * for a specific amount of time. The delay is in microseconds and will
  891. * be between 1 to a maximum of 1000, its default is 0 (no delay).
  892. * The Message delay will delay guest reading on a per message basis
  893. * in microseconds between 1 to 1000 with the default being 0
  894. * (no delay).
  895. */
  896. u32 fuzz_testing_interrupt_delay;
  897. u32 fuzz_testing_message_delay;
  898. /* callback to generate a request ID from a request address */
  899. u64 (*next_request_id_callback)(struct vmbus_channel *channel, u64 rqst_addr);
  900. /* callback to retrieve a request address from a request ID */
  901. u64 (*request_addr_callback)(struct vmbus_channel *channel, u64 rqst_id);
  902. /* request/transaction ids for VMBus */
  903. struct vmbus_requestor requestor;
  904. u32 rqstor_size;
  905. /* The max size of a packet on this channel */
  906. u32 max_pkt_size;
  907. };
  908. #define lock_requestor(channel, flags) \
  909. do { \
  910. struct vmbus_requestor *rqstor = &(channel)->requestor; \
  911. \
  912. spin_lock_irqsave(&rqstor->req_lock, flags); \
  913. } while (0)
  914. static __always_inline void unlock_requestor(struct vmbus_channel *channel,
  915. unsigned long flags)
  916. {
  917. struct vmbus_requestor *rqstor = &channel->requestor;
  918. spin_unlock_irqrestore(&rqstor->req_lock, flags);
  919. }
  920. u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr);
  921. u64 __vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
  922. u64 rqst_addr);
  923. u64 vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
  924. u64 rqst_addr);
  925. u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id);
  926. static inline bool is_hvsock_offer(const struct vmbus_channel_offer_channel *o)
  927. {
  928. return !!(o->offer.chn_flags & VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER);
  929. }
  930. static inline bool is_hvsock_channel(const struct vmbus_channel *c)
  931. {
  932. return is_hvsock_offer(&c->offermsg);
  933. }
  934. static inline bool is_sub_channel(const struct vmbus_channel *c)
  935. {
  936. return c->offermsg.offer.sub_channel_index != 0;
  937. }
  938. static inline void set_channel_read_mode(struct vmbus_channel *c,
  939. enum hv_callback_mode mode)
  940. {
  941. c->callback_mode = mode;
  942. }
  943. static inline void set_per_channel_state(struct vmbus_channel *c, void *s)
  944. {
  945. c->per_channel_state = s;
  946. }
  947. static inline void *get_per_channel_state(struct vmbus_channel *c)
  948. {
  949. return c->per_channel_state;
  950. }
  951. static inline void set_channel_pending_send_size(struct vmbus_channel *c,
  952. u32 size)
  953. {
  954. unsigned long flags;
  955. if (size) {
  956. spin_lock_irqsave(&c->outbound.ring_lock, flags);
  957. ++c->out_full_total;
  958. if (!c->out_full_flag) {
  959. ++c->out_full_first;
  960. c->out_full_flag = true;
  961. }
  962. spin_unlock_irqrestore(&c->outbound.ring_lock, flags);
  963. } else {
  964. c->out_full_flag = false;
  965. }
  966. c->outbound.ring_buffer->pending_send_sz = size;
  967. }
  968. void vmbus_onmessage(struct vmbus_channel_message_header *hdr);
  969. int vmbus_request_offers(void);
  970. /*
  971. * APIs for managing sub-channels.
  972. */
  973. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  974. void (*sc_cr_cb)(struct vmbus_channel *new_sc));
  975. void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
  976. void (*chn_rescind_cb)(struct vmbus_channel *));
  977. /* The format must be the same as struct vmdata_gpa_direct */
  978. struct vmbus_channel_packet_page_buffer {
  979. u16 type;
  980. u16 dataoffset8;
  981. u16 length8;
  982. u16 flags;
  983. u64 transactionid;
  984. u32 reserved;
  985. u32 rangecount;
  986. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  987. } __packed;
  988. /* The format must be the same as struct vmdata_gpa_direct */
  989. struct vmbus_channel_packet_multipage_buffer {
  990. u16 type;
  991. u16 dataoffset8;
  992. u16 length8;
  993. u16 flags;
  994. u64 transactionid;
  995. u32 reserved;
  996. u32 rangecount; /* Always 1 in this case */
  997. struct hv_multipage_buffer range;
  998. } __packed;
  999. /* The format must be the same as struct vmdata_gpa_direct */
  1000. struct vmbus_packet_mpb_array {
  1001. u16 type;
  1002. u16 dataoffset8;
  1003. u16 length8;
  1004. u16 flags;
  1005. u64 transactionid;
  1006. u32 reserved;
  1007. u32 rangecount; /* Always 1 in this case */
  1008. struct hv_mpb_array range;
  1009. } __packed;
  1010. int vmbus_alloc_ring(struct vmbus_channel *channel,
  1011. u32 send_size, u32 recv_size);
  1012. void vmbus_free_ring(struct vmbus_channel *channel);
  1013. int vmbus_connect_ring(struct vmbus_channel *channel,
  1014. void (*onchannel_callback)(void *context),
  1015. void *context);
  1016. int vmbus_disconnect_ring(struct vmbus_channel *channel);
  1017. extern int vmbus_open(struct vmbus_channel *channel,
  1018. u32 send_ringbuffersize,
  1019. u32 recv_ringbuffersize,
  1020. void *userdata,
  1021. u32 userdatalen,
  1022. void (*onchannel_callback)(void *context),
  1023. void *context);
  1024. extern void vmbus_close(struct vmbus_channel *channel);
  1025. extern int vmbus_sendpacket_getid(struct vmbus_channel *channel,
  1026. void *buffer,
  1027. u32 bufferLen,
  1028. u64 requestid,
  1029. u64 *trans_id,
  1030. enum vmbus_packet_type type,
  1031. u32 flags);
  1032. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  1033. void *buffer,
  1034. u32 bufferLen,
  1035. u64 requestid,
  1036. enum vmbus_packet_type type,
  1037. u32 flags);
  1038. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  1039. struct hv_page_buffer pagebuffers[],
  1040. u32 pagecount,
  1041. void *buffer,
  1042. u32 bufferlen,
  1043. u64 requestid);
  1044. extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
  1045. struct vmbus_packet_mpb_array *mpb,
  1046. u32 desc_size,
  1047. void *buffer,
  1048. u32 bufferlen,
  1049. u64 requestid);
  1050. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  1051. void *kbuffer,
  1052. u32 size,
  1053. struct vmbus_gpadl *gpadl);
  1054. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  1055. struct vmbus_gpadl *gpadl);
  1056. void vmbus_reset_channel_cb(struct vmbus_channel *channel);
  1057. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  1058. void *buffer,
  1059. u32 bufferlen,
  1060. u32 *buffer_actual_len,
  1061. u64 *requestid);
  1062. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  1063. void *buffer,
  1064. u32 bufferlen,
  1065. u32 *buffer_actual_len,
  1066. u64 *requestid);
  1067. extern void vmbus_ontimer(unsigned long data);
  1068. /* Base driver object */
  1069. struct hv_driver {
  1070. const char *name;
  1071. /*
  1072. * A hvsock offer, which has a VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER
  1073. * channel flag, actually doesn't mean a synthetic device because the
  1074. * offer's if_type/if_instance can change for every new hvsock
  1075. * connection.
  1076. *
  1077. * However, to facilitate the notification of new-offer/rescind-offer
  1078. * from vmbus driver to hvsock driver, we can handle hvsock offer as
  1079. * a special vmbus device, and hence we need the below flag to
  1080. * indicate if the driver is the hvsock driver or not: we need to
  1081. * specially treat the hvosck offer & driver in vmbus_match().
  1082. */
  1083. bool hvsock;
  1084. /* the device type supported by this driver */
  1085. guid_t dev_type;
  1086. const struct hv_vmbus_device_id *id_table;
  1087. struct device_driver driver;
  1088. /* dynamic device GUID's */
  1089. struct {
  1090. spinlock_t lock;
  1091. struct list_head list;
  1092. } dynids;
  1093. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  1094. int (*remove)(struct hv_device *);
  1095. void (*shutdown)(struct hv_device *);
  1096. int (*suspend)(struct hv_device *);
  1097. int (*resume)(struct hv_device *);
  1098. };
  1099. /* Base device object */
  1100. struct hv_device {
  1101. /* the device type id of this device */
  1102. guid_t dev_type;
  1103. /* the device instance id of this device */
  1104. guid_t dev_instance;
  1105. u16 vendor_id;
  1106. u16 device_id;
  1107. struct device device;
  1108. /*
  1109. * Driver name to force a match. Do not set directly, because core
  1110. * frees it. Use driver_set_override() to set or clear it.
  1111. */
  1112. const char *driver_override;
  1113. struct vmbus_channel *channel;
  1114. struct kset *channels_kset;
  1115. struct device_dma_parameters dma_parms;
  1116. u64 dma_mask;
  1117. /* place holder to keep track of the dir for hv device in debugfs */
  1118. struct dentry *debug_dir;
  1119. };
  1120. static inline struct hv_device *device_to_hv_device(struct device *d)
  1121. {
  1122. return container_of(d, struct hv_device, device);
  1123. }
  1124. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  1125. {
  1126. return container_of(d, struct hv_driver, driver);
  1127. }
  1128. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  1129. {
  1130. dev_set_drvdata(&dev->device, data);
  1131. }
  1132. static inline void *hv_get_drvdata(struct hv_device *dev)
  1133. {
  1134. return dev_get_drvdata(&dev->device);
  1135. }
  1136. struct hv_ring_buffer_debug_info {
  1137. u32 current_interrupt_mask;
  1138. u32 current_read_index;
  1139. u32 current_write_index;
  1140. u32 bytes_avail_toread;
  1141. u32 bytes_avail_towrite;
  1142. };
  1143. int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
  1144. struct hv_ring_buffer_debug_info *debug_info);
  1145. bool hv_ringbuffer_spinlock_busy(struct vmbus_channel *channel);
  1146. /* Vmbus interface */
  1147. #define vmbus_driver_register(driver) \
  1148. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  1149. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  1150. struct module *owner,
  1151. const char *mod_name);
  1152. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  1153. void vmbus_hvsock_device_unregister(struct vmbus_channel *channel);
  1154. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  1155. resource_size_t min, resource_size_t max,
  1156. resource_size_t size, resource_size_t align,
  1157. bool fb_overlap_ok);
  1158. void vmbus_free_mmio(resource_size_t start, resource_size_t size);
  1159. /*
  1160. * GUID definitions of various offer types - services offered to the guest.
  1161. */
  1162. /*
  1163. * Network GUID
  1164. * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
  1165. */
  1166. #define HV_NIC_GUID \
  1167. .guid = GUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x91, 0x3f, \
  1168. 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e)
  1169. /*
  1170. * IDE GUID
  1171. * {32412632-86cb-44a2-9b5c-50d1417354f5}
  1172. */
  1173. #define HV_IDE_GUID \
  1174. .guid = GUID_INIT(0x32412632, 0x86cb, 0x44a2, 0x9b, 0x5c, \
  1175. 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  1176. /*
  1177. * SCSI GUID
  1178. * {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
  1179. */
  1180. #define HV_SCSI_GUID \
  1181. .guid = GUID_INIT(0xba6163d9, 0x04a1, 0x4d29, 0xb6, 0x05, \
  1182. 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  1183. /*
  1184. * Shutdown GUID
  1185. * {0e0b6031-5213-4934-818b-38d90ced39db}
  1186. */
  1187. #define HV_SHUTDOWN_GUID \
  1188. .guid = GUID_INIT(0x0e0b6031, 0x5213, 0x4934, 0x81, 0x8b, \
  1189. 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb)
  1190. /*
  1191. * Time Synch GUID
  1192. * {9527E630-D0AE-497b-ADCE-E80AB0175CAF}
  1193. */
  1194. #define HV_TS_GUID \
  1195. .guid = GUID_INIT(0x9527e630, 0xd0ae, 0x497b, 0xad, 0xce, \
  1196. 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
  1197. /*
  1198. * Heartbeat GUID
  1199. * {57164f39-9115-4e78-ab55-382f3bd5422d}
  1200. */
  1201. #define HV_HEART_BEAT_GUID \
  1202. .guid = GUID_INIT(0x57164f39, 0x9115, 0x4e78, 0xab, 0x55, \
  1203. 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
  1204. /*
  1205. * KVP GUID
  1206. * {a9a0f4e7-5a45-4d96-b827-8a841e8c03e6}
  1207. */
  1208. #define HV_KVP_GUID \
  1209. .guid = GUID_INIT(0xa9a0f4e7, 0x5a45, 0x4d96, 0xb8, 0x27, \
  1210. 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6)
  1211. /*
  1212. * Dynamic memory GUID
  1213. * {525074dc-8985-46e2-8057-a307dc18a502}
  1214. */
  1215. #define HV_DM_GUID \
  1216. .guid = GUID_INIT(0x525074dc, 0x8985, 0x46e2, 0x80, 0x57, \
  1217. 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02)
  1218. /*
  1219. * Mouse GUID
  1220. * {cfa8b69e-5b4a-4cc0-b98b-8ba1a1f3f95a}
  1221. */
  1222. #define HV_MOUSE_GUID \
  1223. .guid = GUID_INIT(0xcfa8b69e, 0x5b4a, 0x4cc0, 0xb9, 0x8b, \
  1224. 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a)
  1225. /*
  1226. * Keyboard GUID
  1227. * {f912ad6d-2b17-48ea-bd65-f927a61c7684}
  1228. */
  1229. #define HV_KBD_GUID \
  1230. .guid = GUID_INIT(0xf912ad6d, 0x2b17, 0x48ea, 0xbd, 0x65, \
  1231. 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84)
  1232. /*
  1233. * VSS (Backup/Restore) GUID
  1234. */
  1235. #define HV_VSS_GUID \
  1236. .guid = GUID_INIT(0x35fa2e29, 0xea23, 0x4236, 0x96, 0xae, \
  1237. 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40)
  1238. /*
  1239. * Synthetic Video GUID
  1240. * {DA0A7802-E377-4aac-8E77-0558EB1073F8}
  1241. */
  1242. #define HV_SYNTHVID_GUID \
  1243. .guid = GUID_INIT(0xda0a7802, 0xe377, 0x4aac, 0x8e, 0x77, \
  1244. 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8)
  1245. /*
  1246. * Synthetic FC GUID
  1247. * {2f9bcc4a-0069-4af3-b76b-6fd0be528cda}
  1248. */
  1249. #define HV_SYNTHFC_GUID \
  1250. .guid = GUID_INIT(0x2f9bcc4a, 0x0069, 0x4af3, 0xb7, 0x6b, \
  1251. 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda)
  1252. /*
  1253. * Guest File Copy Service
  1254. * {34D14BE3-DEE4-41c8-9AE7-6B174977C192}
  1255. */
  1256. #define HV_FCOPY_GUID \
  1257. .guid = GUID_INIT(0x34d14be3, 0xdee4, 0x41c8, 0x9a, 0xe7, \
  1258. 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92)
  1259. /*
  1260. * NetworkDirect. This is the guest RDMA service.
  1261. * {8c2eaf3d-32a7-4b09-ab99-bd1f1c86b501}
  1262. */
  1263. #define HV_ND_GUID \
  1264. .guid = GUID_INIT(0x8c2eaf3d, 0x32a7, 0x4b09, 0xab, 0x99, \
  1265. 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01)
  1266. /*
  1267. * PCI Express Pass Through
  1268. * {44C4F61D-4444-4400-9D52-802E27EDE19F}
  1269. */
  1270. #define HV_PCIE_GUID \
  1271. .guid = GUID_INIT(0x44c4f61d, 0x4444, 0x4400, 0x9d, 0x52, \
  1272. 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f)
  1273. /*
  1274. * Linux doesn't support these 4 devices: the first two are for
  1275. * Automatic Virtual Machine Activation, the third is for
  1276. * Remote Desktop Virtualization, and the fourth is Initial
  1277. * Machine Configuration (IMC) used only by Windows guests.
  1278. * {f8e65716-3cb3-4a06-9a60-1889c5cccab5}
  1279. * {3375baf4-9e15-4b30-b765-67acb10d607b}
  1280. * {276aacf4-ac15-426c-98dd-7521ad3f01fe}
  1281. * {c376c1c3-d276-48d2-90a9-c04748072c60}
  1282. */
  1283. #define HV_AVMA1_GUID \
  1284. .guid = GUID_INIT(0xf8e65716, 0x3cb3, 0x4a06, 0x9a, 0x60, \
  1285. 0x18, 0x89, 0xc5, 0xcc, 0xca, 0xb5)
  1286. #define HV_AVMA2_GUID \
  1287. .guid = GUID_INIT(0x3375baf4, 0x9e15, 0x4b30, 0xb7, 0x65, \
  1288. 0x67, 0xac, 0xb1, 0x0d, 0x60, 0x7b)
  1289. #define HV_RDV_GUID \
  1290. .guid = GUID_INIT(0x276aacf4, 0xac15, 0x426c, 0x98, 0xdd, \
  1291. 0x75, 0x21, 0xad, 0x3f, 0x01, 0xfe)
  1292. #define HV_IMC_GUID \
  1293. .guid = GUID_INIT(0xc376c1c3, 0xd276, 0x48d2, 0x90, 0xa9, \
  1294. 0xc0, 0x47, 0x48, 0x07, 0x2c, 0x60)
  1295. /*
  1296. * Common header for Hyper-V ICs
  1297. */
  1298. #define ICMSGTYPE_NEGOTIATE 0
  1299. #define ICMSGTYPE_HEARTBEAT 1
  1300. #define ICMSGTYPE_KVPEXCHANGE 2
  1301. #define ICMSGTYPE_SHUTDOWN 3
  1302. #define ICMSGTYPE_TIMESYNC 4
  1303. #define ICMSGTYPE_VSS 5
  1304. #define ICMSGTYPE_FCOPY 7
  1305. #define ICMSGHDRFLAG_TRANSACTION 1
  1306. #define ICMSGHDRFLAG_REQUEST 2
  1307. #define ICMSGHDRFLAG_RESPONSE 4
  1308. /*
  1309. * While we want to handle util services as regular devices,
  1310. * there is only one instance of each of these services; so
  1311. * we statically allocate the service specific state.
  1312. */
  1313. struct hv_util_service {
  1314. u8 *recv_buffer;
  1315. void *channel;
  1316. void (*util_cb)(void *);
  1317. int (*util_init)(struct hv_util_service *);
  1318. void (*util_deinit)(void);
  1319. int (*util_pre_suspend)(void);
  1320. int (*util_pre_resume)(void);
  1321. };
  1322. struct vmbuspipe_hdr {
  1323. u32 flags;
  1324. u32 msgsize;
  1325. } __packed;
  1326. struct ic_version {
  1327. u16 major;
  1328. u16 minor;
  1329. } __packed;
  1330. struct icmsg_hdr {
  1331. struct ic_version icverframe;
  1332. u16 icmsgtype;
  1333. struct ic_version icvermsg;
  1334. u16 icmsgsize;
  1335. u32 status;
  1336. u8 ictransaction_id;
  1337. u8 icflags;
  1338. u8 reserved[2];
  1339. } __packed;
  1340. #define IC_VERSION_NEGOTIATION_MAX_VER_COUNT 100
  1341. #define ICMSG_HDR (sizeof(struct vmbuspipe_hdr) + sizeof(struct icmsg_hdr))
  1342. #define ICMSG_NEGOTIATE_PKT_SIZE(icframe_vercnt, icmsg_vercnt) \
  1343. (ICMSG_HDR + sizeof(struct icmsg_negotiate) + \
  1344. (((icframe_vercnt) + (icmsg_vercnt)) * sizeof(struct ic_version)))
  1345. struct icmsg_negotiate {
  1346. u16 icframe_vercnt;
  1347. u16 icmsg_vercnt;
  1348. u32 reserved;
  1349. struct ic_version icversion_data[]; /* any size array */
  1350. } __packed;
  1351. struct shutdown_msg_data {
  1352. u32 reason_code;
  1353. u32 timeout_seconds;
  1354. u32 flags;
  1355. u8 display_message[2048];
  1356. } __packed;
  1357. struct heartbeat_msg_data {
  1358. u64 seq_num;
  1359. u32 reserved[8];
  1360. } __packed;
  1361. /* Time Sync IC defs */
  1362. #define ICTIMESYNCFLAG_PROBE 0
  1363. #define ICTIMESYNCFLAG_SYNC 1
  1364. #define ICTIMESYNCFLAG_SAMPLE 2
  1365. #ifdef __x86_64__
  1366. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  1367. #else
  1368. #define WLTIMEDELTA 116444736000000000LL
  1369. #endif
  1370. struct ictimesync_data {
  1371. u64 parenttime;
  1372. u64 childtime;
  1373. u64 roundtriptime;
  1374. u8 flags;
  1375. } __packed;
  1376. struct ictimesync_ref_data {
  1377. u64 parenttime;
  1378. u64 vmreferencetime;
  1379. u8 flags;
  1380. char leapflags;
  1381. char stratum;
  1382. u8 reserved[3];
  1383. } __packed;
  1384. struct hyperv_service_callback {
  1385. u8 msg_type;
  1386. char *log_msg;
  1387. guid_t data;
  1388. struct vmbus_channel *channel;
  1389. void (*callback)(void *context);
  1390. };
  1391. struct hv_dma_range {
  1392. dma_addr_t dma;
  1393. u32 mapping_size;
  1394. };
  1395. #define MAX_SRV_VER 0x7ffffff
  1396. extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf, u32 buflen,
  1397. const int *fw_version, int fw_vercnt,
  1398. const int *srv_version, int srv_vercnt,
  1399. int *nego_fw_version, int *nego_srv_version);
  1400. void hv_process_channel_removal(struct vmbus_channel *channel);
  1401. void vmbus_setevent(struct vmbus_channel *channel);
  1402. /*
  1403. * Negotiated version with the Host.
  1404. */
  1405. extern __u32 vmbus_proto_version;
  1406. int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
  1407. const guid_t *shv_host_servie_id);
  1408. int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp);
  1409. void vmbus_set_event(struct vmbus_channel *channel);
  1410. /* Get the start of the ring buffer. */
  1411. static inline void *
  1412. hv_get_ring_buffer(const struct hv_ring_buffer_info *ring_info)
  1413. {
  1414. return ring_info->ring_buffer->buffer;
  1415. }
  1416. /*
  1417. * Mask off host interrupt callback notifications
  1418. */
  1419. static inline void hv_begin_read(struct hv_ring_buffer_info *rbi)
  1420. {
  1421. rbi->ring_buffer->interrupt_mask = 1;
  1422. /* make sure mask update is not reordered */
  1423. virt_mb();
  1424. }
  1425. /*
  1426. * Re-enable host callback and return number of outstanding bytes
  1427. */
  1428. static inline u32 hv_end_read(struct hv_ring_buffer_info *rbi)
  1429. {
  1430. rbi->ring_buffer->interrupt_mask = 0;
  1431. /* make sure mask update is not reordered */
  1432. virt_mb();
  1433. /*
  1434. * Now check to see if the ring buffer is still empty.
  1435. * If it is not, we raced and we need to process new
  1436. * incoming messages.
  1437. */
  1438. return hv_get_bytes_to_read(rbi);
  1439. }
  1440. /*
  1441. * An API to support in-place processing of incoming VMBUS packets.
  1442. */
  1443. /* Get data payload associated with descriptor */
  1444. static inline void *hv_pkt_data(const struct vmpacket_descriptor *desc)
  1445. {
  1446. return (void *)((unsigned long)desc + (desc->offset8 << 3));
  1447. }
  1448. /* Get data size associated with descriptor */
  1449. static inline u32 hv_pkt_datalen(const struct vmpacket_descriptor *desc)
  1450. {
  1451. return (desc->len8 << 3) - (desc->offset8 << 3);
  1452. }
  1453. /* Get packet length associated with descriptor */
  1454. static inline u32 hv_pkt_len(const struct vmpacket_descriptor *desc)
  1455. {
  1456. return desc->len8 << 3;
  1457. }
  1458. struct vmpacket_descriptor *
  1459. hv_pkt_iter_first(struct vmbus_channel *channel);
  1460. struct vmpacket_descriptor *
  1461. __hv_pkt_iter_next(struct vmbus_channel *channel,
  1462. const struct vmpacket_descriptor *pkt);
  1463. void hv_pkt_iter_close(struct vmbus_channel *channel);
  1464. static inline struct vmpacket_descriptor *
  1465. hv_pkt_iter_next(struct vmbus_channel *channel,
  1466. const struct vmpacket_descriptor *pkt)
  1467. {
  1468. struct vmpacket_descriptor *nxt;
  1469. nxt = __hv_pkt_iter_next(channel, pkt);
  1470. if (!nxt)
  1471. hv_pkt_iter_close(channel);
  1472. return nxt;
  1473. }
  1474. #define foreach_vmbus_pkt(pkt, channel) \
  1475. for (pkt = hv_pkt_iter_first(channel); pkt; \
  1476. pkt = hv_pkt_iter_next(channel, pkt))
  1477. /*
  1478. * Interface for passing data between SR-IOV PF and VF drivers. The VF driver
  1479. * sends requests to read and write blocks. Each block must be 128 bytes or
  1480. * smaller. Optionally, the VF driver can register a callback function which
  1481. * will be invoked when the host says that one or more of the first 64 block
  1482. * IDs is "invalid" which means that the VF driver should reread them.
  1483. */
  1484. #define HV_CONFIG_BLOCK_SIZE_MAX 128
  1485. int hyperv_read_cfg_blk(struct pci_dev *dev, void *buf, unsigned int buf_len,
  1486. unsigned int block_id, unsigned int *bytes_returned);
  1487. int hyperv_write_cfg_blk(struct pci_dev *dev, void *buf, unsigned int len,
  1488. unsigned int block_id);
  1489. int hyperv_reg_block_invalidate(struct pci_dev *dev, void *context,
  1490. void (*block_invalidate)(void *context,
  1491. u64 block_mask));
  1492. struct hyperv_pci_block_ops {
  1493. int (*read_block)(struct pci_dev *dev, void *buf, unsigned int buf_len,
  1494. unsigned int block_id, unsigned int *bytes_returned);
  1495. int (*write_block)(struct pci_dev *dev, void *buf, unsigned int len,
  1496. unsigned int block_id);
  1497. int (*reg_blk_invalidate)(struct pci_dev *dev, void *context,
  1498. void (*block_invalidate)(void *context,
  1499. u64 block_mask));
  1500. };
  1501. extern struct hyperv_pci_block_ops hvpci_block_ops;
  1502. static inline unsigned long virt_to_hvpfn(void *addr)
  1503. {
  1504. phys_addr_t paddr;
  1505. if (is_vmalloc_addr(addr))
  1506. paddr = page_to_phys(vmalloc_to_page(addr)) +
  1507. offset_in_page(addr);
  1508. else
  1509. paddr = __pa(addr);
  1510. return paddr >> HV_HYP_PAGE_SHIFT;
  1511. }
  1512. #define NR_HV_HYP_PAGES_IN_PAGE (PAGE_SIZE / HV_HYP_PAGE_SIZE)
  1513. #define offset_in_hvpage(ptr) ((unsigned long)(ptr) & ~HV_HYP_PAGE_MASK)
  1514. #define HVPFN_UP(x) (((x) + HV_HYP_PAGE_SIZE-1) >> HV_HYP_PAGE_SHIFT)
  1515. #define HVPFN_DOWN(x) ((x) >> HV_HYP_PAGE_SHIFT)
  1516. #define page_to_hvpfn(page) (page_to_pfn(page) * NR_HV_HYP_PAGES_IN_PAGE)
  1517. #endif /* _HYPERV_H */