hyperv.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 <linux/scatterlist.h>
  27. #include <linux/list.h>
  28. #include <linux/uuid.h>
  29. #include <linux/timer.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/completion.h>
  32. #include <linux/device.h>
  33. #include <linux/mod_devicetable.h>
  34. #include <asm/hyperv.h>
  35. #define MAX_PAGE_BUFFER_COUNT 16
  36. #define MAX_MULTIPAGE_BUFFER_COUNT 32 /* 128K */
  37. #pragma pack(push, 1)
  38. /* Single-page buffer */
  39. struct hv_page_buffer {
  40. u32 len;
  41. u32 offset;
  42. u64 pfn;
  43. };
  44. /* Multiple-page buffer */
  45. struct hv_multipage_buffer {
  46. /* Length and Offset determines the # of pfns in the array */
  47. u32 len;
  48. u32 offset;
  49. u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
  50. };
  51. /* 0x18 includes the proprietary packet header */
  52. #define MAX_PAGE_BUFFER_PACKET (0x18 + \
  53. (sizeof(struct hv_page_buffer) * \
  54. MAX_PAGE_BUFFER_COUNT))
  55. #define MAX_MULTIPAGE_BUFFER_PACKET (0x18 + \
  56. sizeof(struct hv_multipage_buffer))
  57. #pragma pack(pop)
  58. struct hv_ring_buffer {
  59. /* Offset in bytes from the start of ring data below */
  60. u32 write_index;
  61. /* Offset in bytes from the start of ring data below */
  62. u32 read_index;
  63. u32 interrupt_mask;
  64. /* Pad it to PAGE_SIZE so that data starts on page boundary */
  65. u8 reserved[4084];
  66. /* NOTE:
  67. * The interrupt_mask field is used only for channels but since our
  68. * vmbus connection also uses this data structure and its data starts
  69. * here, we commented out this field.
  70. */
  71. /*
  72. * Ring data starts here + RingDataStartOffset
  73. * !!! DO NOT place any fields below this !!!
  74. */
  75. u8 buffer[0];
  76. } __packed;
  77. struct hv_ring_buffer_info {
  78. struct hv_ring_buffer *ring_buffer;
  79. u32 ring_size; /* Include the shared header */
  80. spinlock_t ring_lock;
  81. u32 ring_datasize; /* < ring_size */
  82. u32 ring_data_startoffset;
  83. };
  84. struct hv_ring_buffer_debug_info {
  85. u32 current_interrupt_mask;
  86. u32 current_read_index;
  87. u32 current_write_index;
  88. u32 bytes_avail_toread;
  89. u32 bytes_avail_towrite;
  90. };
  91. /*
  92. * We use the same version numbering for all Hyper-V modules.
  93. *
  94. * Definition of versioning is as follows;
  95. *
  96. * Major Number Changes for these scenarios;
  97. * 1. When a new version of Windows Hyper-V
  98. * is released.
  99. * 2. A Major change has occurred in the
  100. * Linux IC's.
  101. * (For example the merge for the first time
  102. * into the kernel) Every time the Major Number
  103. * changes, the Revision number is reset to 0.
  104. * Minor Number Changes when new functionality is added
  105. * to the Linux IC's that is not a bug fix.
  106. *
  107. * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
  108. */
  109. #define HV_DRV_VERSION "3.1"
  110. /*
  111. * A revision number of vmbus that is used for ensuring both ends on a
  112. * partition are using compatible versions.
  113. */
  114. #define VMBUS_REVISION_NUMBER 13
  115. /* Make maximum size of pipe payload of 16K */
  116. #define MAX_PIPE_DATA_PAYLOAD (sizeof(u8) * 16384)
  117. /* Define PipeMode values. */
  118. #define VMBUS_PIPE_TYPE_BYTE 0x00000000
  119. #define VMBUS_PIPE_TYPE_MESSAGE 0x00000004
  120. /* The size of the user defined data buffer for non-pipe offers. */
  121. #define MAX_USER_DEFINED_BYTES 120
  122. /* The size of the user defined data buffer for pipe offers. */
  123. #define MAX_PIPE_USER_DEFINED_BYTES 116
  124. /*
  125. * At the center of the Channel Management library is the Channel Offer. This
  126. * struct contains the fundamental information about an offer.
  127. */
  128. struct vmbus_channel_offer {
  129. uuid_le if_type;
  130. uuid_le if_instance;
  131. u64 int_latency; /* in 100ns units */
  132. u32 if_revision;
  133. u32 server_ctx_size; /* in bytes */
  134. u16 chn_flags;
  135. u16 mmio_megabytes; /* in bytes * 1024 * 1024 */
  136. union {
  137. /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
  138. struct {
  139. unsigned char user_def[MAX_USER_DEFINED_BYTES];
  140. } std;
  141. /*
  142. * Pipes:
  143. * The following sructure is an integrated pipe protocol, which
  144. * is implemented on top of standard user-defined data. Pipe
  145. * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
  146. * use.
  147. */
  148. struct {
  149. u32 pipe_mode;
  150. unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
  151. } pipe;
  152. } u;
  153. u32 padding;
  154. } __packed;
  155. /* Server Flags */
  156. #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE 1
  157. #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES 2
  158. #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS 4
  159. #define VMBUS_CHANNEL_NAMED_PIPE_MODE 0x10
  160. #define VMBUS_CHANNEL_LOOPBACK_OFFER 0x100
  161. #define VMBUS_CHANNEL_PARENT_OFFER 0x200
  162. #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION 0x400
  163. struct vmpacket_descriptor {
  164. u16 type;
  165. u16 offset8;
  166. u16 len8;
  167. u16 flags;
  168. u64 trans_id;
  169. } __packed;
  170. struct vmpacket_header {
  171. u32 prev_pkt_start_offset;
  172. struct vmpacket_descriptor descriptor;
  173. } __packed;
  174. struct vmtransfer_page_range {
  175. u32 byte_count;
  176. u32 byte_offset;
  177. } __packed;
  178. struct vmtransfer_page_packet_header {
  179. struct vmpacket_descriptor d;
  180. u16 xfer_pageset_id;
  181. bool sender_owns_set;
  182. u8 reserved;
  183. u32 range_cnt;
  184. struct vmtransfer_page_range ranges[1];
  185. } __packed;
  186. struct vmgpadl_packet_header {
  187. struct vmpacket_descriptor d;
  188. u32 gpadl;
  189. u32 reserved;
  190. } __packed;
  191. struct vmadd_remove_transfer_page_set {
  192. struct vmpacket_descriptor d;
  193. u32 gpadl;
  194. u16 xfer_pageset_id;
  195. u16 reserved;
  196. } __packed;
  197. /*
  198. * This structure defines a range in guest physical space that can be made to
  199. * look virtually contiguous.
  200. */
  201. struct gpa_range {
  202. u32 byte_count;
  203. u32 byte_offset;
  204. u64 pfn_array[0];
  205. };
  206. /*
  207. * This is the format for an Establish Gpadl packet, which contains a handle by
  208. * which this GPADL will be known and a set of GPA ranges associated with it.
  209. * This can be converted to a MDL by the guest OS. If there are multiple GPA
  210. * ranges, then the resulting MDL will be "chained," representing multiple VA
  211. * ranges.
  212. */
  213. struct vmestablish_gpadl {
  214. struct vmpacket_descriptor d;
  215. u32 gpadl;
  216. u32 range_cnt;
  217. struct gpa_range range[1];
  218. } __packed;
  219. /*
  220. * This is the format for a Teardown Gpadl packet, which indicates that the
  221. * GPADL handle in the Establish Gpadl packet will never be referenced again.
  222. */
  223. struct vmteardown_gpadl {
  224. struct vmpacket_descriptor d;
  225. u32 gpadl;
  226. u32 reserved; /* for alignment to a 8-byte boundary */
  227. } __packed;
  228. /*
  229. * This is the format for a GPA-Direct packet, which contains a set of GPA
  230. * ranges, in addition to commands and/or data.
  231. */
  232. struct vmdata_gpa_direct {
  233. struct vmpacket_descriptor d;
  234. u32 reserved;
  235. u32 range_cnt;
  236. struct gpa_range range[1];
  237. } __packed;
  238. /* This is the format for a Additional Data Packet. */
  239. struct vmadditional_data {
  240. struct vmpacket_descriptor d;
  241. u64 total_bytes;
  242. u32 offset;
  243. u32 byte_cnt;
  244. unsigned char data[1];
  245. } __packed;
  246. union vmpacket_largest_possible_header {
  247. struct vmpacket_descriptor simple_hdr;
  248. struct vmtransfer_page_packet_header xfer_page_hdr;
  249. struct vmgpadl_packet_header gpadl_hdr;
  250. struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
  251. struct vmestablish_gpadl establish_gpadl_hdr;
  252. struct vmteardown_gpadl teardown_gpadl_hdr;
  253. struct vmdata_gpa_direct data_gpa_direct_hdr;
  254. };
  255. #define VMPACKET_DATA_START_ADDRESS(__packet) \
  256. (void *)(((unsigned char *)__packet) + \
  257. ((struct vmpacket_descriptor)__packet)->offset8 * 8)
  258. #define VMPACKET_DATA_LENGTH(__packet) \
  259. ((((struct vmpacket_descriptor)__packet)->len8 - \
  260. ((struct vmpacket_descriptor)__packet)->offset8) * 8)
  261. #define VMPACKET_TRANSFER_MODE(__packet) \
  262. (((struct IMPACT)__packet)->type)
  263. enum vmbus_packet_type {
  264. VM_PKT_INVALID = 0x0,
  265. VM_PKT_SYNCH = 0x1,
  266. VM_PKT_ADD_XFER_PAGESET = 0x2,
  267. VM_PKT_RM_XFER_PAGESET = 0x3,
  268. VM_PKT_ESTABLISH_GPADL = 0x4,
  269. VM_PKT_TEARDOWN_GPADL = 0x5,
  270. VM_PKT_DATA_INBAND = 0x6,
  271. VM_PKT_DATA_USING_XFER_PAGES = 0x7,
  272. VM_PKT_DATA_USING_GPADL = 0x8,
  273. VM_PKT_DATA_USING_GPA_DIRECT = 0x9,
  274. VM_PKT_CANCEL_REQUEST = 0xa,
  275. VM_PKT_COMP = 0xb,
  276. VM_PKT_DATA_USING_ADDITIONAL_PKT = 0xc,
  277. VM_PKT_ADDITIONAL_DATA = 0xd
  278. };
  279. #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED 1
  280. /* Version 1 messages */
  281. enum vmbus_channel_message_type {
  282. CHANNELMSG_INVALID = 0,
  283. CHANNELMSG_OFFERCHANNEL = 1,
  284. CHANNELMSG_RESCIND_CHANNELOFFER = 2,
  285. CHANNELMSG_REQUESTOFFERS = 3,
  286. CHANNELMSG_ALLOFFERS_DELIVERED = 4,
  287. CHANNELMSG_OPENCHANNEL = 5,
  288. CHANNELMSG_OPENCHANNEL_RESULT = 6,
  289. CHANNELMSG_CLOSECHANNEL = 7,
  290. CHANNELMSG_GPADL_HEADER = 8,
  291. CHANNELMSG_GPADL_BODY = 9,
  292. CHANNELMSG_GPADL_CREATED = 10,
  293. CHANNELMSG_GPADL_TEARDOWN = 11,
  294. CHANNELMSG_GPADL_TORNDOWN = 12,
  295. CHANNELMSG_RELID_RELEASED = 13,
  296. CHANNELMSG_INITIATE_CONTACT = 14,
  297. CHANNELMSG_VERSION_RESPONSE = 15,
  298. CHANNELMSG_UNLOAD = 16,
  299. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  300. CHANNELMSG_VIEWRANGE_ADD = 17,
  301. CHANNELMSG_VIEWRANGE_REMOVE = 18,
  302. #endif
  303. CHANNELMSG_COUNT
  304. };
  305. struct vmbus_channel_message_header {
  306. enum vmbus_channel_message_type msgtype;
  307. u32 padding;
  308. } __packed;
  309. /* Query VMBus Version parameters */
  310. struct vmbus_channel_query_vmbus_version {
  311. struct vmbus_channel_message_header header;
  312. u32 version;
  313. } __packed;
  314. /* VMBus Version Supported parameters */
  315. struct vmbus_channel_version_supported {
  316. struct vmbus_channel_message_header header;
  317. bool version_supported;
  318. } __packed;
  319. /* Offer Channel parameters */
  320. struct vmbus_channel_offer_channel {
  321. struct vmbus_channel_message_header header;
  322. struct vmbus_channel_offer offer;
  323. u32 child_relid;
  324. u8 monitorid;
  325. bool monitor_allocated;
  326. } __packed;
  327. /* Rescind Offer parameters */
  328. struct vmbus_channel_rescind_offer {
  329. struct vmbus_channel_message_header header;
  330. u32 child_relid;
  331. } __packed;
  332. /*
  333. * Request Offer -- no parameters, SynIC message contains the partition ID
  334. * Set Snoop -- no parameters, SynIC message contains the partition ID
  335. * Clear Snoop -- no parameters, SynIC message contains the partition ID
  336. * All Offers Delivered -- no parameters, SynIC message contains the partition
  337. * ID
  338. * Flush Client -- no parameters, SynIC message contains the partition ID
  339. */
  340. /* Open Channel parameters */
  341. struct vmbus_channel_open_channel {
  342. struct vmbus_channel_message_header header;
  343. /* Identifies the specific VMBus channel that is being opened. */
  344. u32 child_relid;
  345. /* ID making a particular open request at a channel offer unique. */
  346. u32 openid;
  347. /* GPADL for the channel's ring buffer. */
  348. u32 ringbuffer_gpadlhandle;
  349. /* GPADL for the channel's server context save area. */
  350. u32 server_contextarea_gpadlhandle;
  351. /*
  352. * The upstream ring buffer begins at offset zero in the memory
  353. * described by RingBufferGpadlHandle. The downstream ring buffer
  354. * follows it at this offset (in pages).
  355. */
  356. u32 downstream_ringbuffer_pageoffset;
  357. /* User-specific data to be passed along to the server endpoint. */
  358. unsigned char userdata[MAX_USER_DEFINED_BYTES];
  359. } __packed;
  360. /* Open Channel Result parameters */
  361. struct vmbus_channel_open_result {
  362. struct vmbus_channel_message_header header;
  363. u32 child_relid;
  364. u32 openid;
  365. u32 status;
  366. } __packed;
  367. /* Close channel parameters; */
  368. struct vmbus_channel_close_channel {
  369. struct vmbus_channel_message_header header;
  370. u32 child_relid;
  371. } __packed;
  372. /* Channel Message GPADL */
  373. #define GPADL_TYPE_RING_BUFFER 1
  374. #define GPADL_TYPE_SERVER_SAVE_AREA 2
  375. #define GPADL_TYPE_TRANSACTION 8
  376. /*
  377. * The number of PFNs in a GPADL message is defined by the number of
  378. * pages that would be spanned by ByteCount and ByteOffset. If the
  379. * implied number of PFNs won't fit in this packet, there will be a
  380. * follow-up packet that contains more.
  381. */
  382. struct vmbus_channel_gpadl_header {
  383. struct vmbus_channel_message_header header;
  384. u32 child_relid;
  385. u32 gpadl;
  386. u16 range_buflen;
  387. u16 rangecount;
  388. struct gpa_range range[0];
  389. } __packed;
  390. /* This is the followup packet that contains more PFNs. */
  391. struct vmbus_channel_gpadl_body {
  392. struct vmbus_channel_message_header header;
  393. u32 msgnumber;
  394. u32 gpadl;
  395. u64 pfn[0];
  396. } __packed;
  397. struct vmbus_channel_gpadl_created {
  398. struct vmbus_channel_message_header header;
  399. u32 child_relid;
  400. u32 gpadl;
  401. u32 creation_status;
  402. } __packed;
  403. struct vmbus_channel_gpadl_teardown {
  404. struct vmbus_channel_message_header header;
  405. u32 child_relid;
  406. u32 gpadl;
  407. } __packed;
  408. struct vmbus_channel_gpadl_torndown {
  409. struct vmbus_channel_message_header header;
  410. u32 gpadl;
  411. } __packed;
  412. #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
  413. struct vmbus_channel_view_range_add {
  414. struct vmbus_channel_message_header header;
  415. PHYSICAL_ADDRESS viewrange_base;
  416. u64 viewrange_length;
  417. u32 child_relid;
  418. } __packed;
  419. struct vmbus_channel_view_range_remove {
  420. struct vmbus_channel_message_header header;
  421. PHYSICAL_ADDRESS viewrange_base;
  422. u32 child_relid;
  423. } __packed;
  424. #endif
  425. struct vmbus_channel_relid_released {
  426. struct vmbus_channel_message_header header;
  427. u32 child_relid;
  428. } __packed;
  429. struct vmbus_channel_initiate_contact {
  430. struct vmbus_channel_message_header header;
  431. u32 vmbus_version_requested;
  432. u32 padding2;
  433. u64 interrupt_page;
  434. u64 monitor_page1;
  435. u64 monitor_page2;
  436. } __packed;
  437. struct vmbus_channel_version_response {
  438. struct vmbus_channel_message_header header;
  439. bool version_supported;
  440. } __packed;
  441. enum vmbus_channel_state {
  442. CHANNEL_OFFER_STATE,
  443. CHANNEL_OPENING_STATE,
  444. CHANNEL_OPEN_STATE,
  445. };
  446. struct vmbus_channel_debug_info {
  447. u32 relid;
  448. enum vmbus_channel_state state;
  449. uuid_le interfacetype;
  450. uuid_le interface_instance;
  451. u32 monitorid;
  452. u32 servermonitor_pending;
  453. u32 servermonitor_latency;
  454. u32 servermonitor_connectionid;
  455. u32 clientmonitor_pending;
  456. u32 clientmonitor_latency;
  457. u32 clientmonitor_connectionid;
  458. struct hv_ring_buffer_debug_info inbound;
  459. struct hv_ring_buffer_debug_info outbound;
  460. };
  461. /*
  462. * Represents each channel msg on the vmbus connection This is a
  463. * variable-size data structure depending on the msg type itself
  464. */
  465. struct vmbus_channel_msginfo {
  466. /* Bookkeeping stuff */
  467. struct list_head msglistentry;
  468. /* So far, this is only used to handle gpadl body message */
  469. struct list_head submsglist;
  470. /* Synchronize the request/response if needed */
  471. struct completion waitevent;
  472. union {
  473. struct vmbus_channel_version_supported version_supported;
  474. struct vmbus_channel_open_result open_result;
  475. struct vmbus_channel_gpadl_torndown gpadl_torndown;
  476. struct vmbus_channel_gpadl_created gpadl_created;
  477. struct vmbus_channel_version_response version_response;
  478. } response;
  479. u32 msgsize;
  480. /*
  481. * The channel message that goes out on the "wire".
  482. * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
  483. */
  484. unsigned char msg[0];
  485. };
  486. struct vmbus_close_msg {
  487. struct vmbus_channel_msginfo info;
  488. struct vmbus_channel_close_channel msg;
  489. };
  490. struct vmbus_channel {
  491. struct list_head listentry;
  492. struct hv_device *device_obj;
  493. struct work_struct work;
  494. enum vmbus_channel_state state;
  495. struct vmbus_channel_offer_channel offermsg;
  496. /*
  497. * These are based on the OfferMsg.MonitorId.
  498. * Save it here for easy access.
  499. */
  500. u8 monitor_grp;
  501. u8 monitor_bit;
  502. u32 ringbuffer_gpadlhandle;
  503. /* Allocated memory for ring buffer */
  504. void *ringbuffer_pages;
  505. u32 ringbuffer_pagecount;
  506. struct hv_ring_buffer_info outbound; /* send to parent */
  507. struct hv_ring_buffer_info inbound; /* receive from parent */
  508. spinlock_t inbound_lock;
  509. struct workqueue_struct *controlwq;
  510. struct vmbus_close_msg close_msg;
  511. /* Channel callback are invoked in this workqueue context */
  512. /* HANDLE dataWorkQueue; */
  513. void (*onchannel_callback)(void *context);
  514. void *channel_callback_context;
  515. };
  516. void free_channel(struct vmbus_channel *channel);
  517. void vmbus_onmessage(void *context);
  518. int vmbus_request_offers(void);
  519. /* The format must be the same as struct vmdata_gpa_direct */
  520. struct vmbus_channel_packet_page_buffer {
  521. u16 type;
  522. u16 dataoffset8;
  523. u16 length8;
  524. u16 flags;
  525. u64 transactionid;
  526. u32 reserved;
  527. u32 rangecount;
  528. struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
  529. } __packed;
  530. /* The format must be the same as struct vmdata_gpa_direct */
  531. struct vmbus_channel_packet_multipage_buffer {
  532. u16 type;
  533. u16 dataoffset8;
  534. u16 length8;
  535. u16 flags;
  536. u64 transactionid;
  537. u32 reserved;
  538. u32 rangecount; /* Always 1 in this case */
  539. struct hv_multipage_buffer range;
  540. } __packed;
  541. extern int vmbus_open(struct vmbus_channel *channel,
  542. u32 send_ringbuffersize,
  543. u32 recv_ringbuffersize,
  544. void *userdata,
  545. u32 userdatalen,
  546. void(*onchannel_callback)(void *context),
  547. void *context);
  548. extern void vmbus_close(struct vmbus_channel *channel);
  549. extern int vmbus_sendpacket(struct vmbus_channel *channel,
  550. const void *buffer,
  551. u32 bufferLen,
  552. u64 requestid,
  553. enum vmbus_packet_type type,
  554. u32 flags);
  555. extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
  556. struct hv_page_buffer pagebuffers[],
  557. u32 pagecount,
  558. void *buffer,
  559. u32 bufferlen,
  560. u64 requestid);
  561. extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
  562. struct hv_multipage_buffer *mpb,
  563. void *buffer,
  564. u32 bufferlen,
  565. u64 requestid);
  566. extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
  567. void *kbuffer,
  568. u32 size,
  569. u32 *gpadl_handle);
  570. extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
  571. u32 gpadl_handle);
  572. extern int vmbus_recvpacket(struct vmbus_channel *channel,
  573. void *buffer,
  574. u32 bufferlen,
  575. u32 *buffer_actual_len,
  576. u64 *requestid);
  577. extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
  578. void *buffer,
  579. u32 bufferlen,
  580. u32 *buffer_actual_len,
  581. u64 *requestid);
  582. extern void vmbus_get_debug_info(struct vmbus_channel *channel,
  583. struct vmbus_channel_debug_info *debug);
  584. extern void vmbus_ontimer(unsigned long data);
  585. #define LOWORD(dw) ((unsigned short)(dw))
  586. #define HIWORD(dw) ((unsigned short)(((unsigned int) (dw) >> 16) & 0xFFFF))
  587. #define VMBUS 0x0001
  588. #define STORVSC 0x0002
  589. #define NETVSC 0x0004
  590. #define INPUTVSC 0x0008
  591. #define BLKVSC 0x0010
  592. #define VMBUS_DRV 0x0100
  593. #define STORVSC_DRV 0x0200
  594. #define NETVSC_DRV 0x0400
  595. #define INPUTVSC_DRV 0x0800
  596. #define BLKVSC_DRV 0x1000
  597. #define ALL_MODULES (VMBUS |\
  598. STORVSC |\
  599. NETVSC |\
  600. INPUTVSC |\
  601. BLKVSC |\
  602. VMBUS_DRV |\
  603. STORVSC_DRV |\
  604. NETVSC_DRV |\
  605. INPUTVSC_DRV|\
  606. BLKVSC_DRV)
  607. /* Logging Level */
  608. #define ERROR_LVL 3
  609. #define WARNING_LVL 4
  610. #define INFO_LVL 6
  611. #define DEBUG_LVL 7
  612. #define DEBUG_LVL_ENTEREXIT 8
  613. #define DEBUG_RING_LVL 9
  614. extern unsigned int vmbus_loglevel;
  615. #define DPRINT(mod, lvl, fmt, args...) do {\
  616. if ((mod & (HIWORD(vmbus_loglevel))) && \
  617. (lvl <= LOWORD(vmbus_loglevel))) \
  618. printk(KERN_DEBUG #mod": %s() " fmt "\n", __func__, ## args);\
  619. } while (0)
  620. #define DPRINT_DBG(mod, fmt, args...) do {\
  621. if ((mod & (HIWORD(vmbus_loglevel))) && \
  622. (DEBUG_LVL <= LOWORD(vmbus_loglevel))) \
  623. printk(KERN_DEBUG #mod": %s() " fmt "\n", __func__, ## args);\
  624. } while (0)
  625. #define DPRINT_INFO(mod, fmt, args...) do {\
  626. if ((mod & (HIWORD(vmbus_loglevel))) && \
  627. (INFO_LVL <= LOWORD(vmbus_loglevel))) \
  628. printk(KERN_INFO #mod": " fmt "\n", ## args);\
  629. } while (0)
  630. #define DPRINT_WARN(mod, fmt, args...) do {\
  631. if ((mod & (HIWORD(vmbus_loglevel))) && \
  632. (WARNING_LVL <= LOWORD(vmbus_loglevel))) \
  633. printk(KERN_WARNING #mod": WARNING! " fmt "\n", ## args);\
  634. } while (0)
  635. #define DPRINT_ERR(mod, fmt, args...) do {\
  636. if ((mod & (HIWORD(vmbus_loglevel))) && \
  637. (ERROR_LVL <= LOWORD(vmbus_loglevel))) \
  638. printk(KERN_ERR #mod": %s() ERROR!! " fmt "\n", \
  639. __func__, ## args);\
  640. } while (0)
  641. struct hv_driver;
  642. struct hv_device;
  643. struct hv_dev_port_info {
  644. u32 int_mask;
  645. u32 read_idx;
  646. u32 write_idx;
  647. u32 bytes_avail_toread;
  648. u32 bytes_avail_towrite;
  649. };
  650. struct hv_device_info {
  651. u32 chn_id;
  652. u32 chn_state;
  653. uuid_le chn_type;
  654. uuid_le chn_instance;
  655. u32 monitor_id;
  656. u32 server_monitor_pending;
  657. u32 server_monitor_latency;
  658. u32 server_monitor_conn_id;
  659. u32 client_monitor_pending;
  660. u32 client_monitor_latency;
  661. u32 client_monitor_conn_id;
  662. struct hv_dev_port_info inbound;
  663. struct hv_dev_port_info outbound;
  664. };
  665. /* Base driver object */
  666. struct hv_driver {
  667. const char *name;
  668. /* the device type supported by this driver */
  669. uuid_le dev_type;
  670. const struct hv_vmbus_device_id *id_table;
  671. struct device_driver driver;
  672. int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
  673. int (*remove)(struct hv_device *);
  674. void (*shutdown)(struct hv_device *);
  675. };
  676. /* Base device object */
  677. struct hv_device {
  678. /* the device type id of this device */
  679. uuid_le dev_type;
  680. /* the device instance id of this device */
  681. uuid_le dev_instance;
  682. struct device device;
  683. struct vmbus_channel *channel;
  684. };
  685. static inline struct hv_device *device_to_hv_device(struct device *d)
  686. {
  687. return container_of(d, struct hv_device, device);
  688. }
  689. static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
  690. {
  691. return container_of(d, struct hv_driver, driver);
  692. }
  693. static inline void hv_set_drvdata(struct hv_device *dev, void *data)
  694. {
  695. dev_set_drvdata(&dev->device, data);
  696. }
  697. static inline void *hv_get_drvdata(struct hv_device *dev)
  698. {
  699. return dev_get_drvdata(&dev->device);
  700. }
  701. /* Vmbus interface */
  702. #define vmbus_driver_register(driver) \
  703. __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  704. int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
  705. struct module *owner,
  706. const char *mod_name);
  707. void vmbus_driver_unregister(struct hv_driver *hv_driver);
  708. /**
  709. * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
  710. *
  711. * This macro is used to create a struct hv_vmbus_device_id that matches a
  712. * specific device.
  713. */
  714. #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7, \
  715. g8, g9, ga, gb, gc, gd, ge, gf) \
  716. .guid = { g0, g1, g2, g3, g4, g5, g6, g7, \
  717. g8, g9, ga, gb, gc, gd, ge, gf },
  718. /*
  719. * Common header for Hyper-V ICs
  720. */
  721. #define ICMSGTYPE_NEGOTIATE 0
  722. #define ICMSGTYPE_HEARTBEAT 1
  723. #define ICMSGTYPE_KVPEXCHANGE 2
  724. #define ICMSGTYPE_SHUTDOWN 3
  725. #define ICMSGTYPE_TIMESYNC 4
  726. #define ICMSGTYPE_VSS 5
  727. #define ICMSGHDRFLAG_TRANSACTION 1
  728. #define ICMSGHDRFLAG_REQUEST 2
  729. #define ICMSGHDRFLAG_RESPONSE 4
  730. #define HV_S_OK 0x00000000
  731. #define HV_E_FAIL 0x80004005
  732. #define HV_ERROR_NOT_SUPPORTED 0x80070032
  733. #define HV_ERROR_MACHINE_LOCKED 0x800704F7
  734. /*
  735. * While we want to handle util services as regular devices,
  736. * there is only one instance of each of these services; so
  737. * we statically allocate the service specific state.
  738. */
  739. struct hv_util_service {
  740. u8 *recv_buffer;
  741. void (*util_cb)(void *);
  742. int (*util_init)(struct hv_util_service *);
  743. void (*util_deinit)(void);
  744. };
  745. struct vmbuspipe_hdr {
  746. u32 flags;
  747. u32 msgsize;
  748. } __packed;
  749. struct ic_version {
  750. u16 major;
  751. u16 minor;
  752. } __packed;
  753. struct icmsg_hdr {
  754. struct ic_version icverframe;
  755. u16 icmsgtype;
  756. struct ic_version icvermsg;
  757. u16 icmsgsize;
  758. u32 status;
  759. u8 ictransaction_id;
  760. u8 icflags;
  761. u8 reserved[2];
  762. } __packed;
  763. struct icmsg_negotiate {
  764. u16 icframe_vercnt;
  765. u16 icmsg_vercnt;
  766. u32 reserved;
  767. struct ic_version icversion_data[1]; /* any size array */
  768. } __packed;
  769. struct shutdown_msg_data {
  770. u32 reason_code;
  771. u32 timeout_seconds;
  772. u32 flags;
  773. u8 display_message[2048];
  774. } __packed;
  775. struct heartbeat_msg_data {
  776. u64 seq_num;
  777. u32 reserved[8];
  778. } __packed;
  779. /* Time Sync IC defs */
  780. #define ICTIMESYNCFLAG_PROBE 0
  781. #define ICTIMESYNCFLAG_SYNC 1
  782. #define ICTIMESYNCFLAG_SAMPLE 2
  783. #ifdef __x86_64__
  784. #define WLTIMEDELTA 116444736000000000L /* in 100ns unit */
  785. #else
  786. #define WLTIMEDELTA 116444736000000000LL
  787. #endif
  788. struct ictimesync_data {
  789. u64 parenttime;
  790. u64 childtime;
  791. u64 roundtriptime;
  792. u8 flags;
  793. } __packed;
  794. struct hyperv_service_callback {
  795. u8 msg_type;
  796. char *log_msg;
  797. uuid_le data;
  798. struct vmbus_channel *channel;
  799. void (*callback) (void *context);
  800. };
  801. extern void prep_negotiate_resp(struct icmsg_hdr *,
  802. struct icmsg_negotiate *, u8 *);
  803. #endif /* _HYPERV_H */