18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * VMware VMCI driver (vmciContext.h)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 VMware, Inc. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _VMCI_CONTEXT_H_
98c2ecf20Sopenharmony_ci#define _VMCI_CONTEXT_H_
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/vmw_vmci_defs.h>
128c2ecf20Sopenharmony_ci#include <linux/atomic.h>
138c2ecf20Sopenharmony_ci#include <linux/kref.h>
148c2ecf20Sopenharmony_ci#include <linux/types.h>
158c2ecf20Sopenharmony_ci#include <linux/wait.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "vmci_handle_array.h"
188c2ecf20Sopenharmony_ci#include "vmci_datagram.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Used to determine what checkpoint state to get and set. */
218c2ecf20Sopenharmony_cienum {
228c2ecf20Sopenharmony_ci	VMCI_NOTIFICATION_CPT_STATE = 1,
238c2ecf20Sopenharmony_ci	VMCI_WELLKNOWN_CPT_STATE    = 2,
248c2ecf20Sopenharmony_ci	VMCI_DG_OUT_STATE           = 3,
258c2ecf20Sopenharmony_ci	VMCI_DG_IN_STATE            = 4,
268c2ecf20Sopenharmony_ci	VMCI_DG_IN_SIZE_STATE       = 5,
278c2ecf20Sopenharmony_ci	VMCI_DOORBELL_CPT_STATE     = 6,
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Host specific struct used for signalling */
318c2ecf20Sopenharmony_cistruct vmci_host {
328c2ecf20Sopenharmony_ci	wait_queue_head_t wait_queue;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct vmci_handle_list {
368c2ecf20Sopenharmony_ci	struct list_head node;
378c2ecf20Sopenharmony_ci	struct vmci_handle handle;
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistruct vmci_ctx {
418c2ecf20Sopenharmony_ci	struct list_head list_item;       /* For global VMCI list. */
428c2ecf20Sopenharmony_ci	u32 cid;
438c2ecf20Sopenharmony_ci	struct kref kref;
448c2ecf20Sopenharmony_ci	struct list_head datagram_queue;  /* Head of per VM queue. */
458c2ecf20Sopenharmony_ci	u32 pending_datagrams;
468c2ecf20Sopenharmony_ci	size_t datagram_queue_size;	  /* Size of datagram queue in bytes. */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/*
498c2ecf20Sopenharmony_ci	 * Version of the code that created
508c2ecf20Sopenharmony_ci	 * this context; e.g., VMX.
518c2ecf20Sopenharmony_ci	 */
528c2ecf20Sopenharmony_ci	int user_version;
538c2ecf20Sopenharmony_ci	spinlock_t lock;  /* Locks callQueue and handle_arrays. */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/*
568c2ecf20Sopenharmony_ci	 * queue_pairs attached to.  The array of
578c2ecf20Sopenharmony_ci	 * handles for queue pairs is accessed
588c2ecf20Sopenharmony_ci	 * from the code for QP API, and there
598c2ecf20Sopenharmony_ci	 * it is protected by the QP lock.  It
608c2ecf20Sopenharmony_ci	 * is also accessed from the context
618c2ecf20Sopenharmony_ci	 * clean up path, which does not
628c2ecf20Sopenharmony_ci	 * require a lock.  VMCILock is not
638c2ecf20Sopenharmony_ci	 * used to protect the QP array field.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	struct vmci_handle_arr *queue_pair_array;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* Doorbells created by context. */
688c2ecf20Sopenharmony_ci	struct vmci_handle_arr *doorbell_array;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/* Doorbells pending for context. */
718c2ecf20Sopenharmony_ci	struct vmci_handle_arr *pending_doorbell_array;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* Contexts current context is subscribing to. */
748c2ecf20Sopenharmony_ci	struct list_head notifier_list;
758c2ecf20Sopenharmony_ci	unsigned int n_notifiers;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	struct vmci_host host_context;
788c2ecf20Sopenharmony_ci	u32 priv_flags;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	const struct cred *cred;
818c2ecf20Sopenharmony_ci	bool *notify;		/* Notify flag pointer - hosted only. */
828c2ecf20Sopenharmony_ci	struct page *notify_page;	/* Page backing the notify UVA. */
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* VMCINotifyAddRemoveInfo: Used to add/remove remote context notifications. */
868c2ecf20Sopenharmony_cistruct vmci_ctx_info {
878c2ecf20Sopenharmony_ci	u32 remote_cid;
888c2ecf20Sopenharmony_ci	int result;
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* VMCICptBufInfo: Used to set/get current context's checkpoint state. */
928c2ecf20Sopenharmony_cistruct vmci_ctx_chkpt_buf_info {
938c2ecf20Sopenharmony_ci	u64 cpt_buf;
948c2ecf20Sopenharmony_ci	u32 cpt_type;
958c2ecf20Sopenharmony_ci	u32 buf_size;
968c2ecf20Sopenharmony_ci	s32 result;
978c2ecf20Sopenharmony_ci	u32 _pad;
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/*
1018c2ecf20Sopenharmony_ci * VMCINotificationReceiveInfo: Used to recieve pending notifications
1028c2ecf20Sopenharmony_ci * for doorbells and queue pairs.
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_cistruct vmci_ctx_notify_recv_info {
1058c2ecf20Sopenharmony_ci	u64 db_handle_buf_uva;
1068c2ecf20Sopenharmony_ci	u64 db_handle_buf_size;
1078c2ecf20Sopenharmony_ci	u64 qp_handle_buf_uva;
1088c2ecf20Sopenharmony_ci	u64 qp_handle_buf_size;
1098c2ecf20Sopenharmony_ci	s32 result;
1108c2ecf20Sopenharmony_ci	u32 _pad;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/*
1148c2ecf20Sopenharmony_ci * Utilility function that checks whether two entities are allowed
1158c2ecf20Sopenharmony_ci * to interact. If one of them is restricted, the other one must
1168c2ecf20Sopenharmony_ci * be trusted.
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_cistatic inline bool vmci_deny_interaction(u32 part_one, u32 part_two)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return ((part_one & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
1218c2ecf20Sopenharmony_ci		!(part_two & VMCI_PRIVILEGE_FLAG_TRUSTED)) ||
1228c2ecf20Sopenharmony_ci	       ((part_two & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
1238c2ecf20Sopenharmony_ci		!(part_one & VMCI_PRIVILEGE_FLAG_TRUSTED));
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistruct vmci_ctx *vmci_ctx_create(u32 cid, u32 flags,
1278c2ecf20Sopenharmony_ci				 uintptr_t event_hnd, int version,
1288c2ecf20Sopenharmony_ci				 const struct cred *cred);
1298c2ecf20Sopenharmony_civoid vmci_ctx_destroy(struct vmci_ctx *context);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cibool vmci_ctx_supports_host_qp(struct vmci_ctx *context);
1328c2ecf20Sopenharmony_ciint vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg);
1338c2ecf20Sopenharmony_ciint vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
1348c2ecf20Sopenharmony_ci			      size_t *max_size, struct vmci_datagram **dg);
1358c2ecf20Sopenharmony_ciint vmci_ctx_pending_datagrams(u32 cid, u32 *pending);
1368c2ecf20Sopenharmony_cistruct vmci_ctx *vmci_ctx_get(u32 cid);
1378c2ecf20Sopenharmony_civoid vmci_ctx_put(struct vmci_ctx *context);
1388c2ecf20Sopenharmony_cibool vmci_ctx_exists(u32 cid);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciint vmci_ctx_add_notification(u32 context_id, u32 remote_cid);
1418c2ecf20Sopenharmony_ciint vmci_ctx_remove_notification(u32 context_id, u32 remote_cid);
1428c2ecf20Sopenharmony_ciint vmci_ctx_get_chkpt_state(u32 context_id, u32 cpt_type,
1438c2ecf20Sopenharmony_ci			     u32 *num_cids, void **cpt_buf_ptr);
1448c2ecf20Sopenharmony_ciint vmci_ctx_set_chkpt_state(u32 context_id, u32 cpt_type,
1458c2ecf20Sopenharmony_ci			     u32 num_cids, void *cpt_buf);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ciint vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle);
1488c2ecf20Sopenharmony_ciint vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle);
1498c2ecf20Sopenharmony_cibool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_civoid vmci_ctx_check_signal_notify(struct vmci_ctx *context);
1528c2ecf20Sopenharmony_civoid vmci_ctx_unset_notify(struct vmci_ctx *context);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ciint vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle);
1558c2ecf20Sopenharmony_ciint vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle);
1568c2ecf20Sopenharmony_ciint vmci_ctx_dbell_destroy_all(u32 context_id);
1578c2ecf20Sopenharmony_ciint vmci_ctx_notify_dbell(u32 cid, struct vmci_handle handle,
1588c2ecf20Sopenharmony_ci			  u32 src_priv_flags);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ciint vmci_ctx_rcv_notifications_get(u32 context_id, struct vmci_handle_arr
1618c2ecf20Sopenharmony_ci				   **db_handle_array, struct vmci_handle_arr
1628c2ecf20Sopenharmony_ci				   **qp_handle_array);
1638c2ecf20Sopenharmony_civoid vmci_ctx_rcv_notifications_release(u32 context_id, struct vmci_handle_arr
1648c2ecf20Sopenharmony_ci					*db_handle_array, struct vmci_handle_arr
1658c2ecf20Sopenharmony_ci					*qp_handle_array, bool success);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic inline u32 vmci_ctx_get_id(struct vmci_ctx *context)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	if (!context)
1708c2ecf20Sopenharmony_ci		return VMCI_INVALID_ID;
1718c2ecf20Sopenharmony_ci	return context->cid;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci#endif /* _VMCI_CONTEXT_H_ */
175