xref: /linux/drivers/hv/hyperv_vmbus.h (revision 0710dd08824a6f3b9892fc5be24acd2e4a36f178)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *
4  * Copyright (c) 2011, Microsoft Corporation.
5  *
6  * Authors:
7  *   Haiyang Zhang <haiyangz@microsoft.com>
8  *   Hank Janssen  <hjanssen@microsoft.com>
9  *   K. Y. Srinivasan <kys@microsoft.com>
10  */
11 
12 #ifndef _HYPERV_VMBUS_H
13 #define _HYPERV_VMBUS_H
14 
15 #include <linux/list.h>
16 #include <linux/bitops.h>
17 #include <asm/sync_bitops.h>
18 #include <asm/mshyperv.h>
19 #include <linux/atomic.h>
20 #include <linux/hyperv.h>
21 #include <linux/interrupt.h>
22 #include <hyperv/hvhdk.h>
23 
24 #include "hv_trace.h"
25 
26 /*
27  * Timeout for services such as KVP and fcopy.
28  */
29 #define HV_UTIL_TIMEOUT 30
30 
31 /*
32  * Timeout for guest-host handshake for services.
33  */
34 #define HV_UTIL_NEGO_TIMEOUT 55
35 
36 void vmbus_isr(void);
37 
38 /* Definitions for the monitored notification facility */
39 union hv_monitor_trigger_group {
40 	u64 as_uint64;
41 	struct {
42 		u32 pending;
43 		u32 armed;
44 	};
45 };
46 
47 struct hv_monitor_parameter {
48 	union hv_connection_id connectionid;
49 	u16 flagnumber;
50 	u16 rsvdz;
51 };
52 
53 union hv_monitor_trigger_state {
54 	u32 asu32;
55 
56 	struct {
57 		u32 group_enable:4;
58 		u32 rsvdz:28;
59 	};
60 };
61 
62 /* struct hv_monitor_page Layout */
63 /* ------------------------------------------------------ */
64 /* | 0   | TriggerState (4 bytes) | Rsvd1 (4 bytes)     | */
65 /* | 8   | TriggerGroup[0]                              | */
66 /* | 10  | TriggerGroup[1]                              | */
67 /* | 18  | TriggerGroup[2]                              | */
68 /* | 20  | TriggerGroup[3]                              | */
69 /* | 28  | Rsvd2[0]                                     | */
70 /* | 30  | Rsvd2[1]                                     | */
71 /* | 38  | Rsvd2[2]                                     | */
72 /* | 40  | NextCheckTime[0][0]    | NextCheckTime[0][1] | */
73 /* | ...                                                | */
74 /* | 240 | Latency[0][0..3]                             | */
75 /* | 340 | Rsvz3[0]                                     | */
76 /* | 440 | Parameter[0][0]                              | */
77 /* | 448 | Parameter[0][1]                              | */
78 /* | ...                                                | */
79 /* | 840 | Rsvd4[0]                                     | */
80 /* ------------------------------------------------------ */
81 struct hv_monitor_page {
82 	union hv_monitor_trigger_state trigger_state;
83 	u32 rsvdz1;
84 
85 	union hv_monitor_trigger_group trigger_group[4];
86 	u64 rsvdz2[3];
87 
88 	s32 next_checktime[4][32];
89 
90 	u16 latency[4][32];
91 	u64 rsvdz3[32];
92 
93 	struct hv_monitor_parameter parameter[4][32];
94 
95 	u8 rsvdz4[1984];
96 };
97 
98 #define HV_HYPERCALL_PARAM_ALIGN	sizeof(u64)
99 
100 /* Definition of the hv_post_message hypercall input structure. */
101 struct hv_input_post_message {
102 	union hv_connection_id connectionid;
103 	u32 reserved;
104 	u32 message_type;
105 	u32 payload_size;
106 	u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
107 };
108 
109 
110 enum {
111 	VMBUS_MESSAGE_CONNECTION_ID	= 1,
112 	VMBUS_MESSAGE_CONNECTION_ID_4	= 4,
113 	/* VTL2 redirect connection ID for INITIATE_CONTACT. */
114 	VMBUS_MESSAGE_CONNECTION_ID_REDIRECT = 0x800074,
115 	VMBUS_MESSAGE_PORT_ID		= 1,
116 	VMBUS_EVENT_CONNECTION_ID	= 2,
117 	VMBUS_EVENT_PORT_ID		= 2,
118 	VMBUS_MONITOR_CONNECTION_ID	= 3,
119 	VMBUS_MONITOR_PORT_ID		= 3,
120 	VMBUS_MESSAGE_SINT		= 2,
121 };
122 
123 /*
124  * Per cpu state for channel handling
125  */
126 struct hv_per_cpu_context {
127 	/*
128 	 * SynIC pages for communicating with the host.
129 	 *
130 	 * These pages are accessible to the host partition and the hypervisor.
131 	 * They may be used for exchanging data with the host partition and the
132 	 * hypervisor even when they aren't trusted yet the guest partition
133 	 * must be prepared to handle the malicious behavior.
134 	 */
135 	void *hyp_synic_message_page;
136 	void *hyp_synic_event_page;
137 	/*
138 	 * SynIC pages for communicating with the paravisor.
139 	 *
140 	 * These pages may be accessed from within the guest partition only in
141 	 * CoCo VMs. Neither the host partition nor the hypervisor can access
142 	 * these pages in that case; they are used for exchanging data with the
143 	 * paravisor.
144 	 */
145 	void *para_synic_message_page;
146 	void *para_synic_event_page;
147 
148 	/*
149 	 * The page is only used in hv_post_message() for a TDX VM (with the
150 	 * paravisor) to post a messages to Hyper-V: when such a VM calls
151 	 * HVCALL_POST_MESSAGE, it can't use the hyperv_pcpu_input_arg (which
152 	 * is encrypted in such a VM) as the hypercall input page, because
153 	 * the input page for HVCALL_POST_MESSAGE must be decrypted in such a
154 	 * VM, so post_msg_page (which is decrypted in hv_synic_alloc()) is
155 	 * introduced for this purpose. See hyperv_init() for more comments.
156 	 */
157 	void *post_msg_page;
158 
159 	/*
160 	 * Starting with win8, we can take channel interrupts on any CPU;
161 	 * we will manage the tasklet that handles events messages on a per CPU
162 	 * basis.
163 	 */
164 	struct tasklet_struct msg_dpc;
165 };
166 
167 struct hv_context {
168 	/* We only support running on top of Hyper-V
169 	 * So at this point this really can only contain the Hyper-V ID
170 	 */
171 	u64 guestid;
172 
173 	struct hv_per_cpu_context __percpu *cpu_context;
174 
175 	/*
176 	 * To manage allocations in a NUMA node.
177 	 * Array indexed by numa node ID.
178 	 */
179 	struct cpumask *hv_numa_map;
180 };
181 
182 extern struct hv_context hv_context;
183 
184 /* Hv Interface */
185 
186 extern int hv_init(void);
187 
188 extern int hv_post_message(union hv_connection_id connection_id,
189 			 enum hv_message_type message_type,
190 			 void *payload, size_t payload_size);
191 
192 extern int hv_synic_alloc(void);
193 
194 extern void hv_synic_free(void);
195 
196 extern void hv_hyp_synic_enable_regs(unsigned int cpu);
197 extern int hv_synic_init(unsigned int cpu);
198 
199 extern void hv_hyp_synic_disable_regs(unsigned int cpu);
200 extern int hv_synic_cleanup(unsigned int cpu);
201 
202 /* Interface */
203 
204 void hv_ringbuffer_pre_init(struct vmbus_channel *channel);
205 
206 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
207 		       struct page *pages, u32 pagecnt, u32 max_pkt_size,
208 			   bool confidential);
209 
210 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
211 
212 int hv_ringbuffer_write(struct vmbus_channel *channel,
213 			const struct kvec *kv_list, u32 kv_count,
214 			u64 requestid, u64 *trans_id);
215 
216 int hv_ringbuffer_read(struct vmbus_channel *channel,
217 		       void *buffer, u32 buflen, u32 *buffer_actual_len,
218 		       u64 *requestid, bool raw);
219 
220 /*
221  * The Maximum number of channels (16384) is determined by the size of the
222  * interrupt page, which is HV_HYP_PAGE_SIZE. 1/2 of HV_HYP_PAGE_SIZE is to
223  * send endpoint interrupts, and the other is to receive endpoint interrupts.
224  */
225 #define MAX_NUM_CHANNELS	((HV_HYP_PAGE_SIZE >> 1) << 3)
226 
227 /* The value here must be in multiple of 32 */
228 #define MAX_NUM_CHANNELS_SUPPORTED	256
229 
230 #define MAX_CHANNEL_RELIDS					\
231 	max(MAX_NUM_CHANNELS_SUPPORTED, HV_EVENT_FLAGS_COUNT)
232 
233 enum vmbus_connect_state {
234 	DISCONNECTED,
235 	CONNECTING,
236 	CONNECTED,
237 	DISCONNECTING
238 };
239 
240 #define MAX_SIZE_CHANNEL_MESSAGE	HV_MESSAGE_PAYLOAD_BYTE_COUNT
241 
242 /*
243  * The CPU that Hyper-V will interrupt for VMBUS messages, such as
244  * CHANNELMSG_OFFERCHANNEL and CHANNELMSG_RESCIND_CHANNELOFFER.
245  */
246 #define VMBUS_CONNECT_CPU	0
247 
248 struct vmbus_connection {
249 	u32 msg_conn_id;
250 
251 	atomic_t offer_in_progress;
252 
253 	enum vmbus_connect_state conn_state;
254 
255 	atomic_t next_gpadl_handle;
256 
257 	struct completion  unload_event;
258 	/*
259 	 * Represents channel interrupts. Each bit position represents a
260 	 * channel.  When a channel sends an interrupt via VMBUS, it finds its
261 	 * bit in the sendInterruptPage, set it and calls Hv to generate a port
262 	 * event. The other end receives the port event and parse the
263 	 * recvInterruptPage to see which bit is set
264 	 */
265 	void *int_page;
266 	void *send_int_page;
267 	void *recv_int_page;
268 
269 	/*
270 	 * 2 pages - 1st page for parent->child notification and 2nd
271 	 * is child->parent notification
272 	 */
273 	struct hv_monitor_page *monitor_pages[2];
274 	struct list_head chn_msg_list;
275 	spinlock_t channelmsg_lock;
276 
277 	/* List of channels */
278 	struct list_head chn_list;
279 	struct mutex channel_mutex;
280 
281 	/* Array of channel pointers, indexed by relid */
282 	struct vmbus_channel **channels;
283 	u32 relid_hiwater;
284 
285 	/*
286 	 * An offer message is handled first on the work_queue, and then
287 	 * is further handled on handle_primary_chan_wq or
288 	 * handle_sub_chan_wq.
289 	 */
290 	struct workqueue_struct *work_queue;
291 	struct workqueue_struct *handle_primary_chan_wq;
292 	struct workqueue_struct *handle_sub_chan_wq;
293 	struct workqueue_struct *rescind_work_queue;
294 
295 	/*
296 	 * On suspension of the vmbus, the accumulated offer messages
297 	 * must be dropped.
298 	 */
299 	bool ignore_any_offer_msg;
300 
301 	/*
302 	 * The number of sub-channels and hv_sock channels that should be
303 	 * cleaned up upon suspend: sub-channels will be re-created upon
304 	 * resume, and hv_sock channels should not survive suspend.
305 	 */
306 	atomic_t nr_chan_close_on_suspend;
307 	/*
308 	 * vmbus_bus_suspend() waits for "nr_chan_close_on_suspend" to
309 	 * drop to zero.
310 	 */
311 	struct completion ready_for_suspend_event;
312 
313 	/*
314 	 * Completed once the host has offered all boot-time channels.
315 	 * Note that some channels may still be under process on a workqueue.
316 	 */
317 	struct completion all_offers_delivered_event;
318 };
319 
320 
321 struct vmbus_msginfo {
322 	/* Bookkeeping stuff */
323 	struct list_head msglist_entry;
324 
325 	/* The message itself */
326 	unsigned char msg[];
327 };
328 
329 
330 extern struct vmbus_connection vmbus_connection;
331 
332 int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version);
333 
334 static inline void vmbus_send_interrupt(u32 relid)
335 {
336 	sync_set_bit(relid, vmbus_connection.send_int_page);
337 }
338 
339 enum vmbus_message_handler_type {
340 	/* The related handler can sleep. */
341 	VMHT_BLOCKING = 0,
342 
343 	/* The related handler must NOT sleep. */
344 	VMHT_NON_BLOCKING = 1,
345 };
346 
347 struct vmbus_channel_message_table_entry {
348 	enum vmbus_channel_message_type message_type;
349 	enum vmbus_message_handler_type handler_type;
350 	void (*message_handler)(struct vmbus_channel_message_header *msg);
351 	u32 min_payload_len;
352 };
353 
354 extern const struct vmbus_channel_message_table_entry
355 	channel_message_table[CHANNELMSG_COUNT];
356 
357 
358 /* General vmbus interface */
359 
360 bool vmbus_is_confidential(void);
361 
362 #if IS_ENABLED(CONFIG_HYPERV_VMBUS)
363 /* Free the message slot and signal end-of-message if required */
364 static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
365 {
366 	/*
367 	 * On crash we're reading some other CPU's message page and we need
368 	 * to be careful: this other CPU may already had cleared the header
369 	 * and the host may already had delivered some other message there.
370 	 * In case we blindly write msg->header.message_type we're going
371 	 * to lose it. We can still lose a message of the same type but
372 	 * we count on the fact that there can only be one
373 	 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
374 	 * on crash.
375 	 */
376 	if (!try_cmpxchg(&msg->header.message_type,
377 			 &old_msg_type, HVMSG_NONE))
378 		return;
379 
380 	/*
381 	 * The cmpxchg() above does an implicit memory barrier to
382 	 * ensure the write to MessageType (ie set to
383 	 * HVMSG_NONE) happens before we read the
384 	 * MessagePending and EOMing. Otherwise, the EOMing
385 	 * will not deliver any more messages since there is
386 	 * no empty slot
387 	 */
388 	if (msg->header.message_flags.msg_pending) {
389 		/*
390 		 * This will cause message queue rescan to
391 		 * possibly deliver another msg from the
392 		 * hypervisor
393 		 */
394 		if (vmbus_is_confidential())
395 			hv_para_set_synic_register(HV_MSR_EOM, 0);
396 		else
397 			hv_set_msr(HV_MSR_EOM, 0);
398 	}
399 }
400 
401 extern int vmbus_interrupt;
402 extern int vmbus_irq;
403 #endif /* CONFIG_HYPERV_VMBUS */
404 
405 struct hv_device *vmbus_device_create(const guid_t *type,
406 				      const guid_t *instance,
407 				      struct vmbus_channel *channel);
408 
409 int vmbus_device_register(struct hv_device *child_device_obj);
410 void vmbus_device_unregister(struct hv_device *device_obj);
411 int vmbus_add_channel_kobj(struct hv_device *device_obj,
412 			   struct vmbus_channel *channel);
413 
414 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel);
415 
416 void vmbus_channel_map_relid(struct vmbus_channel *channel);
417 void vmbus_channel_unmap_relid(struct vmbus_channel *channel);
418 
419 struct vmbus_channel *relid2channel(u32 relid);
420 
421 void vmbus_free_channels(void);
422 
423 /* Connection interface */
424 
425 int vmbus_connect(void);
426 void vmbus_disconnect(void);
427 
428 int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep);
429 
430 void vmbus_on_event(unsigned long data);
431 void vmbus_on_msg_dpc(unsigned long data);
432 
433 int hv_kvp_init(struct hv_util_service *srv);
434 int hv_kvp_init_transport(void);
435 void hv_kvp_deinit(void);
436 int hv_kvp_pre_suspend(void);
437 int hv_kvp_pre_resume(void);
438 void hv_kvp_onchannelcallback(void *context);
439 
440 int hv_vss_init(struct hv_util_service *srv);
441 int hv_vss_init_transport(void);
442 void hv_vss_deinit(void);
443 int hv_vss_pre_suspend(void);
444 int hv_vss_pre_resume(void);
445 void hv_vss_onchannelcallback(void *context);
446 
447 static inline void hv_poll_channel(struct vmbus_channel *channel,
448 				   void (*cb)(void *))
449 {
450 	if (!channel)
451 		return;
452 	cb(channel);
453 }
454 
455 enum hvutil_device_state {
456 	HVUTIL_DEVICE_INIT = 0,  /* driver is loaded, waiting for userspace */
457 	HVUTIL_READY,            /* userspace is registered */
458 	HVUTIL_HOSTMSG_RECEIVED, /* message from the host was received */
459 	HVUTIL_USERSPACE_REQ,    /* request to userspace was sent */
460 	HVUTIL_USERSPACE_RECV,   /* reply from userspace was received */
461 	HVUTIL_DEVICE_DYING,     /* driver unload is in progress */
462 };
463 
464 enum delay {
465 	INTERRUPT_DELAY = 0,
466 	MESSAGE_DELAY   = 1,
467 };
468 
469 extern const struct vmbus_device vmbus_devs[];
470 
471 static inline bool hv_is_perf_channel(struct vmbus_channel *channel)
472 {
473 	return vmbus_devs[channel->device_id].perf_device;
474 }
475 
476 static inline size_t hv_dev_ring_size(struct vmbus_channel *channel)
477 {
478 	return vmbus_devs[channel->device_id].pref_ring_size;
479 }
480 
481 static inline bool hv_is_allocated_cpu(unsigned int cpu)
482 {
483 	struct vmbus_channel *channel, *sc;
484 
485 	lockdep_assert_held(&vmbus_connection.channel_mutex);
486 	/*
487 	 * List additions/deletions as well as updates of the target CPUs are
488 	 * protected by channel_mutex.
489 	 */
490 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
491 		if (!hv_is_perf_channel(channel))
492 			continue;
493 		if (channel->target_cpu == cpu)
494 			return true;
495 		list_for_each_entry(sc, &channel->sc_list, sc_list) {
496 			if (sc->target_cpu == cpu)
497 				return true;
498 		}
499 	}
500 	return false;
501 }
502 
503 static inline void hv_set_allocated_cpu(unsigned int cpu)
504 {
505 	cpumask_set_cpu(cpu, &hv_context.hv_numa_map[cpu_to_node(cpu)]);
506 }
507 
508 static inline void hv_clear_allocated_cpu(unsigned int cpu)
509 {
510 	if (hv_is_allocated_cpu(cpu))
511 		return;
512 	cpumask_clear_cpu(cpu, &hv_context.hv_numa_map[cpu_to_node(cpu)]);
513 }
514 
515 static inline void hv_update_allocated_cpus(unsigned int old_cpu,
516 					  unsigned int new_cpu)
517 {
518 	hv_set_allocated_cpu(new_cpu);
519 	hv_clear_allocated_cpu(old_cpu);
520 }
521 
522 #ifdef CONFIG_HYPERV_TESTING
523 
524 int hv_debug_add_dev_dir(struct hv_device *dev);
525 void hv_debug_rm_dev_dir(struct hv_device *dev);
526 void hv_debug_rm_all_dir(void);
527 int hv_debug_init(void);
528 void hv_debug_delay_test(struct vmbus_channel *channel, enum delay delay_type);
529 
530 #else /* CONFIG_HYPERV_TESTING */
531 
532 static inline void hv_debug_rm_dev_dir(struct hv_device *dev) {};
533 static inline void hv_debug_rm_all_dir(void) {};
534 static inline void hv_debug_delay_test(struct vmbus_channel *channel,
535 				       enum delay delay_type) {};
536 static inline int hv_debug_init(void)
537 {
538 	return -1;
539 }
540 
541 static inline int hv_debug_add_dev_dir(struct hv_device *dev)
542 {
543 	return -1;
544 }
545 
546 #endif /* CONFIG_HYPERV_TESTING */
547 
548 /* Create and remove sysfs entry for memory mapped ring buffers for a channel */
549 int hv_create_ring_sysfs(struct vmbus_channel *channel,
550 			 int (*hv_mmap_prepare_ring_buffer)(struct vmbus_channel *channel,
551 							    struct vm_area_desc *desc));
552 int hv_remove_ring_sysfs(struct vmbus_channel *channel);
553 
554 #endif /* _HYPERV_VMBUS_H */
555