18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (c) 2011, Microsoft Corporation.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Authors:
78c2ecf20Sopenharmony_ci *   Haiyang Zhang <haiyangz@microsoft.com>
88c2ecf20Sopenharmony_ci *   Hank Janssen  <hjanssen@microsoft.com>
98c2ecf20Sopenharmony_ci *   K. Y. Srinivasan <kys@microsoft.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef _HYPERV_VMBUS_H
138c2ecf20Sopenharmony_ci#define _HYPERV_VMBUS_H
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/list.h>
168c2ecf20Sopenharmony_ci#include <linux/bitops.h>
178c2ecf20Sopenharmony_ci#include <asm/sync_bitops.h>
188c2ecf20Sopenharmony_ci#include <asm/hyperv-tlfs.h>
198c2ecf20Sopenharmony_ci#include <linux/atomic.h>
208c2ecf20Sopenharmony_ci#include <linux/hyperv.h>
218c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include "hv_trace.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * Timeout for services such as KVP and fcopy.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#define HV_UTIL_TIMEOUT 30
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Timeout for guest-host handshake for services.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#define HV_UTIL_NEGO_TIMEOUT 55
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Definitions for the monitored notification facility */
378c2ecf20Sopenharmony_ciunion hv_monitor_trigger_group {
388c2ecf20Sopenharmony_ci	u64 as_uint64;
398c2ecf20Sopenharmony_ci	struct {
408c2ecf20Sopenharmony_ci		u32 pending;
418c2ecf20Sopenharmony_ci		u32 armed;
428c2ecf20Sopenharmony_ci	};
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct hv_monitor_parameter {
468c2ecf20Sopenharmony_ci	union hv_connection_id connectionid;
478c2ecf20Sopenharmony_ci	u16 flagnumber;
488c2ecf20Sopenharmony_ci	u16 rsvdz;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciunion hv_monitor_trigger_state {
528c2ecf20Sopenharmony_ci	u32 asu32;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	struct {
558c2ecf20Sopenharmony_ci		u32 group_enable:4;
568c2ecf20Sopenharmony_ci		u32 rsvdz:28;
578c2ecf20Sopenharmony_ci	};
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* struct hv_monitor_page Layout */
618c2ecf20Sopenharmony_ci/* ------------------------------------------------------ */
628c2ecf20Sopenharmony_ci/* | 0   | TriggerState (4 bytes) | Rsvd1 (4 bytes)     | */
638c2ecf20Sopenharmony_ci/* | 8   | TriggerGroup[0]                              | */
648c2ecf20Sopenharmony_ci/* | 10  | TriggerGroup[1]                              | */
658c2ecf20Sopenharmony_ci/* | 18  | TriggerGroup[2]                              | */
668c2ecf20Sopenharmony_ci/* | 20  | TriggerGroup[3]                              | */
678c2ecf20Sopenharmony_ci/* | 28  | Rsvd2[0]                                     | */
688c2ecf20Sopenharmony_ci/* | 30  | Rsvd2[1]                                     | */
698c2ecf20Sopenharmony_ci/* | 38  | Rsvd2[2]                                     | */
708c2ecf20Sopenharmony_ci/* | 40  | NextCheckTime[0][0]    | NextCheckTime[0][1] | */
718c2ecf20Sopenharmony_ci/* | ...                                                | */
728c2ecf20Sopenharmony_ci/* | 240 | Latency[0][0..3]                             | */
738c2ecf20Sopenharmony_ci/* | 340 | Rsvz3[0]                                     | */
748c2ecf20Sopenharmony_ci/* | 440 | Parameter[0][0]                              | */
758c2ecf20Sopenharmony_ci/* | 448 | Parameter[0][1]                              | */
768c2ecf20Sopenharmony_ci/* | ...                                                | */
778c2ecf20Sopenharmony_ci/* | 840 | Rsvd4[0]                                     | */
788c2ecf20Sopenharmony_ci/* ------------------------------------------------------ */
798c2ecf20Sopenharmony_cistruct hv_monitor_page {
808c2ecf20Sopenharmony_ci	union hv_monitor_trigger_state trigger_state;
818c2ecf20Sopenharmony_ci	u32 rsvdz1;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	union hv_monitor_trigger_group trigger_group[4];
848c2ecf20Sopenharmony_ci	u64 rsvdz2[3];
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	s32 next_checktime[4][32];
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	u16 latency[4][32];
898c2ecf20Sopenharmony_ci	u64 rsvdz3[32];
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	struct hv_monitor_parameter parameter[4][32];
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	u8 rsvdz4[1984];
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define HV_HYPERCALL_PARAM_ALIGN	sizeof(u64)
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* Definition of the hv_post_message hypercall input structure. */
998c2ecf20Sopenharmony_cistruct hv_input_post_message {
1008c2ecf20Sopenharmony_ci	union hv_connection_id connectionid;
1018c2ecf20Sopenharmony_ci	u32 reserved;
1028c2ecf20Sopenharmony_ci	u32 message_type;
1038c2ecf20Sopenharmony_ci	u32 payload_size;
1048c2ecf20Sopenharmony_ci	u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cienum {
1098c2ecf20Sopenharmony_ci	VMBUS_MESSAGE_CONNECTION_ID	= 1,
1108c2ecf20Sopenharmony_ci	VMBUS_MESSAGE_CONNECTION_ID_4	= 4,
1118c2ecf20Sopenharmony_ci	VMBUS_MESSAGE_PORT_ID		= 1,
1128c2ecf20Sopenharmony_ci	VMBUS_EVENT_CONNECTION_ID	= 2,
1138c2ecf20Sopenharmony_ci	VMBUS_EVENT_PORT_ID		= 2,
1148c2ecf20Sopenharmony_ci	VMBUS_MONITOR_CONNECTION_ID	= 3,
1158c2ecf20Sopenharmony_ci	VMBUS_MONITOR_PORT_ID		= 3,
1168c2ecf20Sopenharmony_ci	VMBUS_MESSAGE_SINT		= 2,
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/*
1208c2ecf20Sopenharmony_ci * Per cpu state for channel handling
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_cistruct hv_per_cpu_context {
1238c2ecf20Sopenharmony_ci	void *synic_message_page;
1248c2ecf20Sopenharmony_ci	void *synic_event_page;
1258c2ecf20Sopenharmony_ci	/*
1268c2ecf20Sopenharmony_ci	 * buffer to post messages to the host.
1278c2ecf20Sopenharmony_ci	 */
1288c2ecf20Sopenharmony_ci	void *post_msg_page;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * Starting with win8, we can take channel interrupts on any CPU;
1328c2ecf20Sopenharmony_ci	 * we will manage the tasklet that handles events messages on a per CPU
1338c2ecf20Sopenharmony_ci	 * basis.
1348c2ecf20Sopenharmony_ci	 */
1358c2ecf20Sopenharmony_ci	struct tasklet_struct msg_dpc;
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistruct hv_context {
1398c2ecf20Sopenharmony_ci	/* We only support running on top of Hyper-V
1408c2ecf20Sopenharmony_ci	 * So at this point this really can only contain the Hyper-V ID
1418c2ecf20Sopenharmony_ci	 */
1428c2ecf20Sopenharmony_ci	u64 guestid;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	struct hv_per_cpu_context __percpu *cpu_context;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/*
1478c2ecf20Sopenharmony_ci	 * To manage allocations in a NUMA node.
1488c2ecf20Sopenharmony_ci	 * Array indexed by numa node ID.
1498c2ecf20Sopenharmony_ci	 */
1508c2ecf20Sopenharmony_ci	struct cpumask *hv_numa_map;
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciextern struct hv_context hv_context;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* Hv Interface */
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciextern int hv_init(void);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciextern int hv_post_message(union hv_connection_id connection_id,
1608c2ecf20Sopenharmony_ci			 enum hv_message_type message_type,
1618c2ecf20Sopenharmony_ci			 void *payload, size_t payload_size);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciextern int hv_synic_alloc(void);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ciextern void hv_synic_free(void);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciextern void hv_synic_enable_regs(unsigned int cpu);
1688c2ecf20Sopenharmony_ciextern int hv_synic_init(unsigned int cpu);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ciextern void hv_synic_disable_regs(unsigned int cpu);
1718c2ecf20Sopenharmony_ciextern int hv_synic_cleanup(unsigned int cpu);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* Interface */
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_civoid hv_ringbuffer_pre_init(struct vmbus_channel *channel);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciint hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
1788c2ecf20Sopenharmony_ci		       struct page *pages, u32 pagecnt);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_civoid hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciint hv_ringbuffer_write(struct vmbus_channel *channel,
1838c2ecf20Sopenharmony_ci			const struct kvec *kv_list, u32 kv_count);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciint hv_ringbuffer_read(struct vmbus_channel *channel,
1868c2ecf20Sopenharmony_ci		       void *buffer, u32 buflen, u32 *buffer_actual_len,
1878c2ecf20Sopenharmony_ci		       u64 *requestid, bool raw);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/*
1908c2ecf20Sopenharmony_ci * The Maximum number of channels (16348) is determined by the size of the
1918c2ecf20Sopenharmony_ci * interrupt page, which is HV_HYP_PAGE_SIZE. 1/2 of HV_HYP_PAGE_SIZE is to
1928c2ecf20Sopenharmony_ci * send endpoint interrupts, and the other is to receive endpoint interrupts.
1938c2ecf20Sopenharmony_ci */
1948c2ecf20Sopenharmony_ci#define MAX_NUM_CHANNELS	((HV_HYP_PAGE_SIZE >> 1) << 3)
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/* The value here must be in multiple of 32 */
1978c2ecf20Sopenharmony_ci/* TODO: Need to make this configurable */
1988c2ecf20Sopenharmony_ci#define MAX_NUM_CHANNELS_SUPPORTED	256
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci#define MAX_CHANNEL_RELIDS					\
2018c2ecf20Sopenharmony_ci	max(MAX_NUM_CHANNELS_SUPPORTED, HV_EVENT_FLAGS_COUNT)
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cienum vmbus_connect_state {
2048c2ecf20Sopenharmony_ci	DISCONNECTED,
2058c2ecf20Sopenharmony_ci	CONNECTING,
2068c2ecf20Sopenharmony_ci	CONNECTED,
2078c2ecf20Sopenharmony_ci	DISCONNECTING
2088c2ecf20Sopenharmony_ci};
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci#define MAX_SIZE_CHANNEL_MESSAGE	HV_MESSAGE_PAYLOAD_BYTE_COUNT
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci * The CPU that Hyper-V will interrupt for VMBUS messages, such as
2148c2ecf20Sopenharmony_ci * CHANNELMSG_OFFERCHANNEL and CHANNELMSG_RESCIND_CHANNELOFFER.
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_ci#define VMBUS_CONNECT_CPU	0
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistruct vmbus_connection {
2198c2ecf20Sopenharmony_ci	u32 msg_conn_id;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	atomic_t offer_in_progress;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	enum vmbus_connect_state conn_state;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	atomic_t next_gpadl_handle;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	struct completion  unload_event;
2288c2ecf20Sopenharmony_ci	/*
2298c2ecf20Sopenharmony_ci	 * Represents channel interrupts. Each bit position represents a
2308c2ecf20Sopenharmony_ci	 * channel.  When a channel sends an interrupt via VMBUS, it finds its
2318c2ecf20Sopenharmony_ci	 * bit in the sendInterruptPage, set it and calls Hv to generate a port
2328c2ecf20Sopenharmony_ci	 * event. The other end receives the port event and parse the
2338c2ecf20Sopenharmony_ci	 * recvInterruptPage to see which bit is set
2348c2ecf20Sopenharmony_ci	 */
2358c2ecf20Sopenharmony_ci	void *int_page;
2368c2ecf20Sopenharmony_ci	void *send_int_page;
2378c2ecf20Sopenharmony_ci	void *recv_int_page;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/*
2408c2ecf20Sopenharmony_ci	 * 2 pages - 1st page for parent->child notification and 2nd
2418c2ecf20Sopenharmony_ci	 * is child->parent notification
2428c2ecf20Sopenharmony_ci	 */
2438c2ecf20Sopenharmony_ci	struct hv_monitor_page *monitor_pages[2];
2448c2ecf20Sopenharmony_ci	struct list_head chn_msg_list;
2458c2ecf20Sopenharmony_ci	spinlock_t channelmsg_lock;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	/* List of channels */
2488c2ecf20Sopenharmony_ci	struct list_head chn_list;
2498c2ecf20Sopenharmony_ci	struct mutex channel_mutex;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* Array of channels */
2528c2ecf20Sopenharmony_ci	struct vmbus_channel **channels;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/*
2558c2ecf20Sopenharmony_ci	 * An offer message is handled first on the work_queue, and then
2568c2ecf20Sopenharmony_ci	 * is further handled on handle_primary_chan_wq or
2578c2ecf20Sopenharmony_ci	 * handle_sub_chan_wq.
2588c2ecf20Sopenharmony_ci	 */
2598c2ecf20Sopenharmony_ci	struct workqueue_struct *work_queue;
2608c2ecf20Sopenharmony_ci	struct workqueue_struct *handle_primary_chan_wq;
2618c2ecf20Sopenharmony_ci	struct workqueue_struct *handle_sub_chan_wq;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/*
2648c2ecf20Sopenharmony_ci	 * The number of sub-channels and hv_sock channels that should be
2658c2ecf20Sopenharmony_ci	 * cleaned up upon suspend: sub-channels will be re-created upon
2668c2ecf20Sopenharmony_ci	 * resume, and hv_sock channels should not survive suspend.
2678c2ecf20Sopenharmony_ci	 */
2688c2ecf20Sopenharmony_ci	atomic_t nr_chan_close_on_suspend;
2698c2ecf20Sopenharmony_ci	/*
2708c2ecf20Sopenharmony_ci	 * vmbus_bus_suspend() waits for "nr_chan_close_on_suspend" to
2718c2ecf20Sopenharmony_ci	 * drop to zero.
2728c2ecf20Sopenharmony_ci	 */
2738c2ecf20Sopenharmony_ci	struct completion ready_for_suspend_event;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/*
2768c2ecf20Sopenharmony_ci	 * The number of primary channels that should be "fixed up"
2778c2ecf20Sopenharmony_ci	 * upon resume: these channels are re-offered upon resume, and some
2788c2ecf20Sopenharmony_ci	 * fields of the channel offers (i.e. child_relid and connection_id)
2798c2ecf20Sopenharmony_ci	 * can change, so the old offermsg must be fixed up, before the resume
2808c2ecf20Sopenharmony_ci	 * callbacks of the VSC drivers start to further touch the channels.
2818c2ecf20Sopenharmony_ci	 */
2828c2ecf20Sopenharmony_ci	atomic_t nr_chan_fixup_on_resume;
2838c2ecf20Sopenharmony_ci	/*
2848c2ecf20Sopenharmony_ci	 * vmbus_bus_resume() waits for "nr_chan_fixup_on_resume" to
2858c2ecf20Sopenharmony_ci	 * drop to zero.
2868c2ecf20Sopenharmony_ci	 */
2878c2ecf20Sopenharmony_ci	struct completion ready_for_resume_event;
2888c2ecf20Sopenharmony_ci};
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistruct vmbus_msginfo {
2928c2ecf20Sopenharmony_ci	/* Bookkeeping stuff */
2938c2ecf20Sopenharmony_ci	struct list_head msglist_entry;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	/* The message itself */
2968c2ecf20Sopenharmony_ci	unsigned char msg[];
2978c2ecf20Sopenharmony_ci};
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ciextern struct vmbus_connection vmbus_connection;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ciint vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic inline void vmbus_send_interrupt(u32 relid)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	sync_set_bit(relid, vmbus_connection.send_int_page);
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cienum vmbus_message_handler_type {
3108c2ecf20Sopenharmony_ci	/* The related handler can sleep. */
3118c2ecf20Sopenharmony_ci	VMHT_BLOCKING = 0,
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* The related handler must NOT sleep. */
3148c2ecf20Sopenharmony_ci	VMHT_NON_BLOCKING = 1,
3158c2ecf20Sopenharmony_ci};
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistruct vmbus_channel_message_table_entry {
3188c2ecf20Sopenharmony_ci	enum vmbus_channel_message_type message_type;
3198c2ecf20Sopenharmony_ci	enum vmbus_message_handler_type handler_type;
3208c2ecf20Sopenharmony_ci	void (*message_handler)(struct vmbus_channel_message_header *msg);
3218c2ecf20Sopenharmony_ci	u32 min_payload_len;
3228c2ecf20Sopenharmony_ci};
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ciextern const struct vmbus_channel_message_table_entry
3258c2ecf20Sopenharmony_ci	channel_message_table[CHANNELMSG_COUNT];
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci/* General vmbus interface */
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistruct hv_device *vmbus_device_create(const guid_t *type,
3318c2ecf20Sopenharmony_ci				      const guid_t *instance,
3328c2ecf20Sopenharmony_ci				      struct vmbus_channel *channel);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ciint vmbus_device_register(struct hv_device *child_device_obj);
3358c2ecf20Sopenharmony_civoid vmbus_device_unregister(struct hv_device *device_obj);
3368c2ecf20Sopenharmony_ciint vmbus_add_channel_kobj(struct hv_device *device_obj,
3378c2ecf20Sopenharmony_ci			   struct vmbus_channel *channel);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_civoid vmbus_remove_channel_attr_group(struct vmbus_channel *channel);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_civoid vmbus_channel_map_relid(struct vmbus_channel *channel);
3428c2ecf20Sopenharmony_civoid vmbus_channel_unmap_relid(struct vmbus_channel *channel);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistruct vmbus_channel *relid2channel(u32 relid);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_civoid vmbus_free_channels(void);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci/* Connection interface */
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ciint vmbus_connect(void);
3518c2ecf20Sopenharmony_civoid vmbus_disconnect(void);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ciint vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_civoid vmbus_on_event(unsigned long data);
3568c2ecf20Sopenharmony_civoid vmbus_on_msg_dpc(unsigned long data);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ciint hv_kvp_init(struct hv_util_service *srv);
3598c2ecf20Sopenharmony_civoid hv_kvp_deinit(void);
3608c2ecf20Sopenharmony_ciint hv_kvp_pre_suspend(void);
3618c2ecf20Sopenharmony_ciint hv_kvp_pre_resume(void);
3628c2ecf20Sopenharmony_civoid hv_kvp_onchannelcallback(void *context);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ciint hv_vss_init(struct hv_util_service *srv);
3658c2ecf20Sopenharmony_civoid hv_vss_deinit(void);
3668c2ecf20Sopenharmony_ciint hv_vss_pre_suspend(void);
3678c2ecf20Sopenharmony_ciint hv_vss_pre_resume(void);
3688c2ecf20Sopenharmony_civoid hv_vss_onchannelcallback(void *context);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ciint hv_fcopy_init(struct hv_util_service *srv);
3718c2ecf20Sopenharmony_civoid hv_fcopy_deinit(void);
3728c2ecf20Sopenharmony_ciint hv_fcopy_pre_suspend(void);
3738c2ecf20Sopenharmony_ciint hv_fcopy_pre_resume(void);
3748c2ecf20Sopenharmony_civoid hv_fcopy_onchannelcallback(void *context);
3758c2ecf20Sopenharmony_civoid vmbus_initiate_unload(bool crash);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic inline void hv_poll_channel(struct vmbus_channel *channel,
3788c2ecf20Sopenharmony_ci				   void (*cb)(void *))
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	if (!channel)
3818c2ecf20Sopenharmony_ci		return;
3828c2ecf20Sopenharmony_ci	cb(channel);
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cienum hvutil_device_state {
3868c2ecf20Sopenharmony_ci	HVUTIL_DEVICE_INIT = 0,  /* driver is loaded, waiting for userspace */
3878c2ecf20Sopenharmony_ci	HVUTIL_READY,            /* userspace is registered */
3888c2ecf20Sopenharmony_ci	HVUTIL_HOSTMSG_RECEIVED, /* message from the host was received */
3898c2ecf20Sopenharmony_ci	HVUTIL_USERSPACE_REQ,    /* request to userspace was sent */
3908c2ecf20Sopenharmony_ci	HVUTIL_USERSPACE_RECV,   /* reply from userspace was received */
3918c2ecf20Sopenharmony_ci	HVUTIL_DEVICE_DYING,     /* driver unload is in progress */
3928c2ecf20Sopenharmony_ci};
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cienum delay {
3958c2ecf20Sopenharmony_ci	INTERRUPT_DELAY = 0,
3968c2ecf20Sopenharmony_ci	MESSAGE_DELAY   = 1,
3978c2ecf20Sopenharmony_ci};
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ciextern const struct vmbus_device vmbus_devs[];
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic inline bool hv_is_perf_channel(struct vmbus_channel *channel)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	return vmbus_devs[channel->device_id].perf_device;
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic inline bool hv_is_alloced_cpu(unsigned int cpu)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct vmbus_channel *channel, *sc;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	lockdep_assert_held(&vmbus_connection.channel_mutex);
4118c2ecf20Sopenharmony_ci	/*
4128c2ecf20Sopenharmony_ci	 * List additions/deletions as well as updates of the target CPUs are
4138c2ecf20Sopenharmony_ci	 * protected by channel_mutex.
4148c2ecf20Sopenharmony_ci	 */
4158c2ecf20Sopenharmony_ci	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
4168c2ecf20Sopenharmony_ci		if (!hv_is_perf_channel(channel))
4178c2ecf20Sopenharmony_ci			continue;
4188c2ecf20Sopenharmony_ci		if (channel->target_cpu == cpu)
4198c2ecf20Sopenharmony_ci			return true;
4208c2ecf20Sopenharmony_ci		list_for_each_entry(sc, &channel->sc_list, sc_list) {
4218c2ecf20Sopenharmony_ci			if (sc->target_cpu == cpu)
4228c2ecf20Sopenharmony_ci				return true;
4238c2ecf20Sopenharmony_ci		}
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci	return false;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic inline void hv_set_alloced_cpu(unsigned int cpu)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	cpumask_set_cpu(cpu, &hv_context.hv_numa_map[cpu_to_node(cpu)]);
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic inline void hv_clear_alloced_cpu(unsigned int cpu)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	if (hv_is_alloced_cpu(cpu))
4368c2ecf20Sopenharmony_ci		return;
4378c2ecf20Sopenharmony_ci	cpumask_clear_cpu(cpu, &hv_context.hv_numa_map[cpu_to_node(cpu)]);
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic inline void hv_update_alloced_cpus(unsigned int old_cpu,
4418c2ecf20Sopenharmony_ci					  unsigned int new_cpu)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	hv_set_alloced_cpu(new_cpu);
4448c2ecf20Sopenharmony_ci	hv_clear_alloced_cpu(old_cpu);
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERV_TESTING
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ciint hv_debug_add_dev_dir(struct hv_device *dev);
4508c2ecf20Sopenharmony_civoid hv_debug_rm_dev_dir(struct hv_device *dev);
4518c2ecf20Sopenharmony_civoid hv_debug_rm_all_dir(void);
4528c2ecf20Sopenharmony_ciint hv_debug_init(void);
4538c2ecf20Sopenharmony_civoid hv_debug_delay_test(struct vmbus_channel *channel, enum delay delay_type);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci#else /* CONFIG_HYPERV_TESTING */
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic inline void hv_debug_rm_dev_dir(struct hv_device *dev) {};
4588c2ecf20Sopenharmony_cistatic inline void hv_debug_rm_all_dir(void) {};
4598c2ecf20Sopenharmony_cistatic inline void hv_debug_delay_test(struct vmbus_channel *channel,
4608c2ecf20Sopenharmony_ci				       enum delay delay_type) {};
4618c2ecf20Sopenharmony_cistatic inline int hv_debug_init(void)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	return -1;
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic inline int hv_debug_add_dev_dir(struct hv_device *dev)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	return -1;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci#endif /* CONFIG_HYPERV_TESTING */
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci#endif /* _HYPERV_VMBUS_H */
474