hyperv_vmbus.h 11 KB

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