hyperv_vmbus.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. * To manage allocations in a NUMA node.
  199. * Array indexed by numa node ID.
  200. */
  201. struct cpumask *hv_numa_map;
  202. };
  203. extern struct hv_context hv_context;
  204. /* Hv Interface */
  205. extern int hv_init(void);
  206. extern int hv_post_message(union hv_connection_id connection_id,
  207. enum hv_message_type message_type,
  208. void *payload, size_t payload_size);
  209. extern int hv_synic_alloc(void);
  210. extern void hv_synic_free(void);
  211. extern int hv_synic_init(unsigned int cpu);
  212. extern int hv_synic_cleanup(unsigned int cpu);
  213. extern void hv_synic_clockevents_cleanup(void);
  214. /* Interface */
  215. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  216. struct page *pages, u32 pagecnt);
  217. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
  218. int hv_ringbuffer_write(struct vmbus_channel *channel,
  219. const struct kvec *kv_list, u32 kv_count);
  220. int hv_ringbuffer_read(struct vmbus_channel *channel,
  221. void *buffer, u32 buflen, u32 *buffer_actual_len,
  222. u64 *requestid, bool raw);
  223. /*
  224. * Maximum channels is determined by the size of the interrupt page
  225. * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt
  226. * and the other is receive endpoint interrupt
  227. */
  228. #define MAX_NUM_CHANNELS ((PAGE_SIZE >> 1) << 3) /* 16348 channels */
  229. /* The value here must be in multiple of 32 */
  230. /* TODO: Need to make this configurable */
  231. #define MAX_NUM_CHANNELS_SUPPORTED 256
  232. enum vmbus_connect_state {
  233. DISCONNECTED,
  234. CONNECTING,
  235. CONNECTED,
  236. DISCONNECTING
  237. };
  238. #define MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT
  239. struct vmbus_connection {
  240. /*
  241. * CPU on which the initial host contact was made.
  242. */
  243. int connect_cpu;
  244. atomic_t offer_in_progress;
  245. enum vmbus_connect_state conn_state;
  246. atomic_t next_gpadl_handle;
  247. struct completion unload_event;
  248. /*
  249. * Represents channel interrupts. Each bit position represents a
  250. * channel. When a channel sends an interrupt via VMBUS, it finds its
  251. * bit in the sendInterruptPage, set it and calls Hv to generate a port
  252. * event. The other end receives the port event and parse the
  253. * recvInterruptPage to see which bit is set
  254. */
  255. void *int_page;
  256. void *send_int_page;
  257. void *recv_int_page;
  258. /*
  259. * 2 pages - 1st page for parent->child notification and 2nd
  260. * is child->parent notification
  261. */
  262. struct hv_monitor_page *monitor_pages[2];
  263. struct list_head chn_msg_list;
  264. spinlock_t channelmsg_lock;
  265. /* List of channels */
  266. struct list_head chn_list;
  267. struct mutex channel_mutex;
  268. /*
  269. * An offer message is handled first on the work_queue, and then
  270. * is further handled on handle_primary_chan_wq or
  271. * handle_sub_chan_wq.
  272. */
  273. struct workqueue_struct *work_queue;
  274. struct workqueue_struct *handle_primary_chan_wq;
  275. struct workqueue_struct *handle_sub_chan_wq;
  276. };
  277. struct vmbus_msginfo {
  278. /* Bookkeeping stuff */
  279. struct list_head msglist_entry;
  280. /* The message itself */
  281. unsigned char msg[0];
  282. };
  283. extern struct vmbus_connection vmbus_connection;
  284. static inline void vmbus_send_interrupt(u32 relid)
  285. {
  286. sync_set_bit(relid, vmbus_connection.send_int_page);
  287. }
  288. enum vmbus_message_handler_type {
  289. /* The related handler can sleep. */
  290. VMHT_BLOCKING = 0,
  291. /* The related handler must NOT sleep. */
  292. VMHT_NON_BLOCKING = 1,
  293. };
  294. struct vmbus_channel_message_table_entry {
  295. enum vmbus_channel_message_type message_type;
  296. enum vmbus_message_handler_type handler_type;
  297. void (*message_handler)(struct vmbus_channel_message_header *msg);
  298. };
  299. extern const struct vmbus_channel_message_table_entry
  300. channel_message_table[CHANNELMSG_COUNT];
  301. /* General vmbus interface */
  302. struct hv_device *vmbus_device_create(const uuid_le *type,
  303. const uuid_le *instance,
  304. struct vmbus_channel *channel);
  305. int vmbus_device_register(struct hv_device *child_device_obj);
  306. void vmbus_device_unregister(struct hv_device *device_obj);
  307. struct vmbus_channel *relid2channel(u32 relid);
  308. void vmbus_free_channels(void);
  309. /* Connection interface */
  310. int vmbus_connect(void);
  311. void vmbus_disconnect(void);
  312. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep);
  313. void vmbus_on_event(unsigned long data);
  314. void vmbus_on_msg_dpc(unsigned long data);
  315. int hv_kvp_init(struct hv_util_service *srv);
  316. void hv_kvp_deinit(void);
  317. void hv_kvp_onchannelcallback(void *context);
  318. int hv_vss_init(struct hv_util_service *srv);
  319. void hv_vss_deinit(void);
  320. void hv_vss_onchannelcallback(void *context);
  321. int hv_fcopy_init(struct hv_util_service *srv);
  322. void hv_fcopy_deinit(void);
  323. void hv_fcopy_onchannelcallback(void *context);
  324. void vmbus_initiate_unload(bool crash);
  325. static inline void hv_poll_channel(struct vmbus_channel *channel,
  326. void (*cb)(void *))
  327. {
  328. if (!channel)
  329. return;
  330. if (in_interrupt() && (channel->target_cpu == smp_processor_id())) {
  331. cb(channel);
  332. return;
  333. }
  334. smp_call_function_single(channel->target_cpu, cb, channel, true);
  335. }
  336. enum hvutil_device_state {
  337. HVUTIL_DEVICE_INIT = 0, /* driver is loaded, waiting for userspace */
  338. HVUTIL_READY, /* userspace is registered */
  339. HVUTIL_HOSTMSG_RECEIVED, /* message from the host was received */
  340. HVUTIL_USERSPACE_REQ, /* request to userspace was sent */
  341. HVUTIL_USERSPACE_RECV, /* reply from userspace was received */
  342. HVUTIL_DEVICE_DYING, /* driver unload is in progress */
  343. };
  344. #endif /* _HYPERV_VMBUS_H */