hyperv_vmbus.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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_VMBUS_H
  25. #define _HYPERV_VMBUS_H
  26. #include <linux/list.h>
  27. #include <asm/sync_bitops.h>
  28. #include <linux/atomic.h>
  29. #include <linux/hyperv.h>
  30. #include <linux/interrupt.h>
  31. /*
  32. * Timeout for services such as KVP and fcopy.
  33. */
  34. #define HV_UTIL_TIMEOUT 30
  35. /*
  36. * Timeout for guest-host handshake for services.
  37. */
  38. #define HV_UTIL_NEGO_TIMEOUT 55
  39. /* Define synthetic interrupt controller flag constants. */
  40. #define HV_EVENT_FLAGS_COUNT (256 * 8)
  41. #define HV_EVENT_FLAGS_LONG_COUNT (256 / sizeof(unsigned long))
  42. /*
  43. * Timer configuration register.
  44. */
  45. union hv_timer_config {
  46. u64 as_uint64;
  47. struct {
  48. u64 enable:1;
  49. u64 periodic:1;
  50. u64 lazy:1;
  51. u64 auto_enable:1;
  52. u64 reserved_z0:12;
  53. u64 sintx:4;
  54. u64 reserved_z1:44;
  55. };
  56. };
  57. /* Define the synthetic interrupt controller event flags format. */
  58. union hv_synic_event_flags {
  59. unsigned long flags[HV_EVENT_FLAGS_LONG_COUNT];
  60. };
  61. /* Define SynIC control register. */
  62. union hv_synic_scontrol {
  63. u64 as_uint64;
  64. struct {
  65. u64 enable:1;
  66. u64 reserved:63;
  67. };
  68. };
  69. /* Define synthetic interrupt source. */
  70. union hv_synic_sint {
  71. u64 as_uint64;
  72. struct {
  73. u64 vector:8;
  74. u64 reserved1:8;
  75. u64 masked:1;
  76. u64 auto_eoi:1;
  77. u64 reserved2:46;
  78. };
  79. };
  80. /* Define the format of the SIMP register */
  81. union hv_synic_simp {
  82. u64 as_uint64;
  83. struct {
  84. u64 simp_enabled:1;
  85. u64 preserved:11;
  86. u64 base_simp_gpa:52;
  87. };
  88. };
  89. /* Define the format of the SIEFP register */
  90. union hv_synic_siefp {
  91. u64 as_uint64;
  92. struct {
  93. u64 siefp_enabled:1;
  94. u64 preserved:11;
  95. u64 base_siefp_gpa:52;
  96. };
  97. };
  98. /* Definitions for the monitored notification facility */
  99. union hv_monitor_trigger_group {
  100. u64 as_uint64;
  101. struct {
  102. u32 pending;
  103. u32 armed;
  104. };
  105. };
  106. struct hv_monitor_parameter {
  107. union hv_connection_id connectionid;
  108. u16 flagnumber;
  109. u16 rsvdz;
  110. };
  111. union hv_monitor_trigger_state {
  112. u32 asu32;
  113. struct {
  114. u32 group_enable:4;
  115. u32 rsvdz:28;
  116. };
  117. };
  118. /* struct hv_monitor_page Layout */
  119. /* ------------------------------------------------------ */
  120. /* | 0 | TriggerState (4 bytes) | Rsvd1 (4 bytes) | */
  121. /* | 8 | TriggerGroup[0] | */
  122. /* | 10 | TriggerGroup[1] | */
  123. /* | 18 | TriggerGroup[2] | */
  124. /* | 20 | TriggerGroup[3] | */
  125. /* | 28 | Rsvd2[0] | */
  126. /* | 30 | Rsvd2[1] | */
  127. /* | 38 | Rsvd2[2] | */
  128. /* | 40 | NextCheckTime[0][0] | NextCheckTime[0][1] | */
  129. /* | ... | */
  130. /* | 240 | Latency[0][0..3] | */
  131. /* | 340 | Rsvz3[0] | */
  132. /* | 440 | Parameter[0][0] | */
  133. /* | 448 | Parameter[0][1] | */
  134. /* | ... | */
  135. /* | 840 | Rsvd4[0] | */
  136. /* ------------------------------------------------------ */
  137. struct hv_monitor_page {
  138. union hv_monitor_trigger_state trigger_state;
  139. u32 rsvdz1;
  140. union hv_monitor_trigger_group trigger_group[4];
  141. u64 rsvdz2[3];
  142. s32 next_checktime[4][32];
  143. u16 latency[4][32];
  144. u64 rsvdz3[32];
  145. struct hv_monitor_parameter parameter[4][32];
  146. u8 rsvdz4[1984];
  147. };
  148. #define HV_HYPERCALL_PARAM_ALIGN sizeof(u64)
  149. /* Definition of the hv_post_message hypercall input structure. */
  150. struct hv_input_post_message {
  151. union hv_connection_id connectionid;
  152. u32 reserved;
  153. u32 message_type;
  154. u32 payload_size;
  155. u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
  156. };
  157. enum {
  158. VMBUS_MESSAGE_CONNECTION_ID = 1,
  159. VMBUS_MESSAGE_PORT_ID = 1,
  160. VMBUS_EVENT_CONNECTION_ID = 2,
  161. VMBUS_EVENT_PORT_ID = 2,
  162. VMBUS_MONITOR_CONNECTION_ID = 3,
  163. VMBUS_MONITOR_PORT_ID = 3,
  164. VMBUS_MESSAGE_SINT = 2,
  165. };
  166. /*
  167. * Per cpu state for channel handling
  168. */
  169. struct hv_per_cpu_context {
  170. void *synic_message_page;
  171. void *synic_event_page;
  172. /*
  173. * buffer to post messages to the host.
  174. */
  175. void *post_msg_page;
  176. /*
  177. * Starting with win8, we can take channel interrupts on any CPU;
  178. * we will manage the tasklet that handles events messages on a per CPU
  179. * basis.
  180. */
  181. struct tasklet_struct msg_dpc;
  182. /*
  183. * To optimize the mapping of relid to channel, maintain
  184. * per-cpu list of the channels based on their CPU affinity.
  185. */
  186. struct list_head chan_list;
  187. struct clock_event_device *clk_evt;
  188. };
  189. struct hv_context {
  190. /* We only support running on top of Hyper-V
  191. * So at this point this really can only contain the Hyper-V ID
  192. */
  193. u64 guestid;
  194. void *tsc_page;
  195. bool synic_initialized;
  196. struct hv_per_cpu_context __percpu *cpu_context;
  197. /*
  198. * Hypervisor's notion of virtual processor ID is different from
  199. * Linux' notion of CPU ID. This information can only be retrieved
  200. * in the context of the calling CPU. Setup a map for easy access
  201. * to this information:
  202. *
  203. * vp_index[a] is the Hyper-V's processor ID corresponding to
  204. * Linux cpuid 'a'.
  205. */
  206. u32 vp_index[NR_CPUS];
  207. /*
  208. * To manage allocations in a NUMA node.
  209. * Array indexed by numa node ID.
  210. */
  211. struct cpumask *hv_numa_map;
  212. };
  213. extern struct hv_context hv_context;
  214. /* Hv Interface */
  215. extern int hv_init(void);
  216. extern int hv_post_message(union hv_connection_id connection_id,
  217. enum hv_message_type message_type,
  218. void *payload, size_t payload_size);
  219. extern int hv_synic_alloc(void);
  220. extern void hv_synic_free(void);
  221. extern int hv_synic_init(unsigned int cpu);
  222. extern int hv_synic_cleanup(unsigned int cpu);
  223. extern void hv_synic_clockevents_cleanup(void);
  224. /* Interface */
  225. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  226. struct page *pages, u32 pagecnt);
  227. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
  228. int hv_ringbuffer_write(struct vmbus_channel *channel,
  229. const struct kvec *kv_list, u32 kv_count);
  230. int hv_ringbuffer_read(struct vmbus_channel *channel,
  231. void *buffer, u32 buflen, u32 *buffer_actual_len,
  232. u64 *requestid, bool raw);
  233. /*
  234. * Maximum channels is determined by the size of the interrupt page
  235. * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt
  236. * and the other is receive endpoint interrupt
  237. */
  238. #define MAX_NUM_CHANNELS ((PAGE_SIZE >> 1) << 3) /* 16348 channels */
  239. /* The value here must be in multiple of 32 */
  240. /* TODO: Need to make this configurable */
  241. #define MAX_NUM_CHANNELS_SUPPORTED 256
  242. enum vmbus_connect_state {
  243. DISCONNECTED,
  244. CONNECTING,
  245. CONNECTED,
  246. DISCONNECTING
  247. };
  248. #define MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT
  249. struct vmbus_connection {
  250. enum vmbus_connect_state conn_state;
  251. atomic_t next_gpadl_handle;
  252. struct completion unload_event;
  253. /*
  254. * Represents channel interrupts. Each bit position represents a
  255. * channel. When a channel sends an interrupt via VMBUS, it finds its
  256. * bit in the sendInterruptPage, set it and calls Hv to generate a port
  257. * event. The other end receives the port event and parse the
  258. * recvInterruptPage to see which bit is set
  259. */
  260. void *int_page;
  261. void *send_int_page;
  262. void *recv_int_page;
  263. /*
  264. * 2 pages - 1st page for parent->child notification and 2nd
  265. * is child->parent notification
  266. */
  267. struct hv_monitor_page *monitor_pages[2];
  268. struct list_head chn_msg_list;
  269. spinlock_t channelmsg_lock;
  270. /* List of channels */
  271. struct list_head chn_list;
  272. struct mutex channel_mutex;
  273. struct workqueue_struct *work_queue;
  274. };
  275. struct vmbus_msginfo {
  276. /* Bookkeeping stuff */
  277. struct list_head msglist_entry;
  278. /* The message itself */
  279. unsigned char msg[0];
  280. };
  281. extern struct vmbus_connection vmbus_connection;
  282. static inline void vmbus_send_interrupt(u32 relid)
  283. {
  284. sync_set_bit(relid, vmbus_connection.send_int_page);
  285. }
  286. enum vmbus_message_handler_type {
  287. /* The related handler can sleep. */
  288. VMHT_BLOCKING = 0,
  289. /* The related handler must NOT sleep. */
  290. VMHT_NON_BLOCKING = 1,
  291. };
  292. struct vmbus_channel_message_table_entry {
  293. enum vmbus_channel_message_type message_type;
  294. enum vmbus_message_handler_type handler_type;
  295. void (*message_handler)(struct vmbus_channel_message_header *msg);
  296. };
  297. extern const struct vmbus_channel_message_table_entry
  298. channel_message_table[CHANNELMSG_COUNT];
  299. /* General vmbus interface */
  300. struct hv_device *vmbus_device_create(const uuid_le *type,
  301. const uuid_le *instance,
  302. struct vmbus_channel *channel);
  303. int vmbus_device_register(struct hv_device *child_device_obj);
  304. void vmbus_device_unregister(struct hv_device *device_obj);
  305. struct vmbus_channel *relid2channel(u32 relid);
  306. void vmbus_free_channels(void);
  307. /* Connection interface */
  308. int vmbus_connect(void);
  309. void vmbus_disconnect(void);
  310. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep);
  311. void vmbus_on_event(unsigned long data);
  312. void vmbus_on_msg_dpc(unsigned long data);
  313. int hv_kvp_init(struct hv_util_service *srv);
  314. void hv_kvp_deinit(void);
  315. void hv_kvp_onchannelcallback(void *context);
  316. int hv_vss_init(struct hv_util_service *srv);
  317. void hv_vss_deinit(void);
  318. void hv_vss_onchannelcallback(void *context);
  319. int hv_fcopy_init(struct hv_util_service *srv);
  320. void hv_fcopy_deinit(void);
  321. void hv_fcopy_onchannelcallback(void *context);
  322. void vmbus_initiate_unload(bool crash);
  323. static inline void hv_poll_channel(struct vmbus_channel *channel,
  324. void (*cb)(void *))
  325. {
  326. if (!channel)
  327. return;
  328. smp_call_function_single(channel->target_cpu, cb, channel, true);
  329. }
  330. enum hvutil_device_state {
  331. HVUTIL_DEVICE_INIT = 0, /* driver is loaded, waiting for userspace */
  332. HVUTIL_READY, /* userspace is registered */
  333. HVUTIL_HOSTMSG_RECEIVED, /* message from the host was received */
  334. HVUTIL_USERSPACE_REQ, /* request to userspace was sent */
  335. HVUTIL_USERSPACE_RECV, /* reply from userspace was received */
  336. HVUTIL_DEVICE_DYING, /* driver unload is in progress */
  337. };
  338. #endif /* _HYPERV_VMBUS_H */