162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * VMware VMCI driver (vmciContext.h)
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2012 VMware, Inc. All rights reserved.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef _VMCI_CONTEXT_H_
962306a36Sopenharmony_ci#define _VMCI_CONTEXT_H_
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/vmw_vmci_defs.h>
1262306a36Sopenharmony_ci#include <linux/atomic.h>
1362306a36Sopenharmony_ci#include <linux/kref.h>
1462306a36Sopenharmony_ci#include <linux/types.h>
1562306a36Sopenharmony_ci#include <linux/wait.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "vmci_handle_array.h"
1862306a36Sopenharmony_ci#include "vmci_datagram.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* Used to determine what checkpoint state to get and set. */
2162306a36Sopenharmony_cienum {
2262306a36Sopenharmony_ci	VMCI_NOTIFICATION_CPT_STATE = 1,
2362306a36Sopenharmony_ci	VMCI_WELLKNOWN_CPT_STATE    = 2,
2462306a36Sopenharmony_ci	VMCI_DG_OUT_STATE           = 3,
2562306a36Sopenharmony_ci	VMCI_DG_IN_STATE            = 4,
2662306a36Sopenharmony_ci	VMCI_DG_IN_SIZE_STATE       = 5,
2762306a36Sopenharmony_ci	VMCI_DOORBELL_CPT_STATE     = 6,
2862306a36Sopenharmony_ci};
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* Host specific struct used for signalling */
3162306a36Sopenharmony_cistruct vmci_host {
3262306a36Sopenharmony_ci	wait_queue_head_t wait_queue;
3362306a36Sopenharmony_ci};
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistruct vmci_handle_list {
3662306a36Sopenharmony_ci	struct list_head node;
3762306a36Sopenharmony_ci	struct vmci_handle handle;
3862306a36Sopenharmony_ci};
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistruct vmci_ctx {
4162306a36Sopenharmony_ci	struct list_head list_item;       /* For global VMCI list. */
4262306a36Sopenharmony_ci	u32 cid;
4362306a36Sopenharmony_ci	struct kref kref;
4462306a36Sopenharmony_ci	struct list_head datagram_queue;  /* Head of per VM queue. */
4562306a36Sopenharmony_ci	u32 pending_datagrams;
4662306a36Sopenharmony_ci	size_t datagram_queue_size;	  /* Size of datagram queue in bytes. */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	/*
4962306a36Sopenharmony_ci	 * Version of the code that created
5062306a36Sopenharmony_ci	 * this context; e.g., VMX.
5162306a36Sopenharmony_ci	 */
5262306a36Sopenharmony_ci	int user_version;
5362306a36Sopenharmony_ci	spinlock_t lock;  /* Locks callQueue and handle_arrays. */
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	/*
5662306a36Sopenharmony_ci	 * queue_pairs attached to.  The array of
5762306a36Sopenharmony_ci	 * handles for queue pairs is accessed
5862306a36Sopenharmony_ci	 * from the code for QP API, and there
5962306a36Sopenharmony_ci	 * it is protected by the QP lock.  It
6062306a36Sopenharmony_ci	 * is also accessed from the context
6162306a36Sopenharmony_ci	 * clean up path, which does not
6262306a36Sopenharmony_ci	 * require a lock.  VMCILock is not
6362306a36Sopenharmony_ci	 * used to protect the QP array field.
6462306a36Sopenharmony_ci	 */
6562306a36Sopenharmony_ci	struct vmci_handle_arr *queue_pair_array;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	/* Doorbells created by context. */
6862306a36Sopenharmony_ci	struct vmci_handle_arr *doorbell_array;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	/* Doorbells pending for context. */
7162306a36Sopenharmony_ci	struct vmci_handle_arr *pending_doorbell_array;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	/* Contexts current context is subscribing to. */
7462306a36Sopenharmony_ci	struct list_head notifier_list;
7562306a36Sopenharmony_ci	unsigned int n_notifiers;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	struct vmci_host host_context;
7862306a36Sopenharmony_ci	u32 priv_flags;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	const struct cred *cred;
8162306a36Sopenharmony_ci	bool *notify;		/* Notify flag pointer - hosted only. */
8262306a36Sopenharmony_ci	struct page *notify_page;	/* Page backing the notify UVA. */
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/* VMCINotifyAddRemoveInfo: Used to add/remove remote context notifications. */
8662306a36Sopenharmony_cistruct vmci_ctx_info {
8762306a36Sopenharmony_ci	u32 remote_cid;
8862306a36Sopenharmony_ci	int result;
8962306a36Sopenharmony_ci};
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/* VMCICptBufInfo: Used to set/get current context's checkpoint state. */
9262306a36Sopenharmony_cistruct vmci_ctx_chkpt_buf_info {
9362306a36Sopenharmony_ci	u64 cpt_buf;
9462306a36Sopenharmony_ci	u32 cpt_type;
9562306a36Sopenharmony_ci	u32 buf_size;
9662306a36Sopenharmony_ci	s32 result;
9762306a36Sopenharmony_ci	u32 _pad;
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/*
10162306a36Sopenharmony_ci * VMCINotificationReceiveInfo: Used to recieve pending notifications
10262306a36Sopenharmony_ci * for doorbells and queue pairs.
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_cistruct vmci_ctx_notify_recv_info {
10562306a36Sopenharmony_ci	u64 db_handle_buf_uva;
10662306a36Sopenharmony_ci	u64 db_handle_buf_size;
10762306a36Sopenharmony_ci	u64 qp_handle_buf_uva;
10862306a36Sopenharmony_ci	u64 qp_handle_buf_size;
10962306a36Sopenharmony_ci	s32 result;
11062306a36Sopenharmony_ci	u32 _pad;
11162306a36Sopenharmony_ci};
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci/*
11462306a36Sopenharmony_ci * Utilility function that checks whether two entities are allowed
11562306a36Sopenharmony_ci * to interact. If one of them is restricted, the other one must
11662306a36Sopenharmony_ci * be trusted.
11762306a36Sopenharmony_ci */
11862306a36Sopenharmony_cistatic inline bool vmci_deny_interaction(u32 part_one, u32 part_two)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	return ((part_one & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
12162306a36Sopenharmony_ci		!(part_two & VMCI_PRIVILEGE_FLAG_TRUSTED)) ||
12262306a36Sopenharmony_ci	       ((part_two & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
12362306a36Sopenharmony_ci		!(part_one & VMCI_PRIVILEGE_FLAG_TRUSTED));
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistruct vmci_ctx *vmci_ctx_create(u32 cid, u32 flags,
12762306a36Sopenharmony_ci				 uintptr_t event_hnd, int version,
12862306a36Sopenharmony_ci				 const struct cred *cred);
12962306a36Sopenharmony_civoid vmci_ctx_destroy(struct vmci_ctx *context);
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cibool vmci_ctx_supports_host_qp(struct vmci_ctx *context);
13262306a36Sopenharmony_ciint vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg);
13362306a36Sopenharmony_ciint vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
13462306a36Sopenharmony_ci			      size_t *max_size, struct vmci_datagram **dg);
13562306a36Sopenharmony_ciint vmci_ctx_pending_datagrams(u32 cid, u32 *pending);
13662306a36Sopenharmony_cistruct vmci_ctx *vmci_ctx_get(u32 cid);
13762306a36Sopenharmony_civoid vmci_ctx_put(struct vmci_ctx *context);
13862306a36Sopenharmony_cibool vmci_ctx_exists(u32 cid);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ciint vmci_ctx_add_notification(u32 context_id, u32 remote_cid);
14162306a36Sopenharmony_ciint vmci_ctx_remove_notification(u32 context_id, u32 remote_cid);
14262306a36Sopenharmony_ciint vmci_ctx_get_chkpt_state(u32 context_id, u32 cpt_type,
14362306a36Sopenharmony_ci			     u32 *num_cids, void **cpt_buf_ptr);
14462306a36Sopenharmony_ciint vmci_ctx_set_chkpt_state(u32 context_id, u32 cpt_type,
14562306a36Sopenharmony_ci			     u32 num_cids, void *cpt_buf);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciint vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle);
14862306a36Sopenharmony_ciint vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle);
14962306a36Sopenharmony_cibool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_civoid vmci_ctx_check_signal_notify(struct vmci_ctx *context);
15262306a36Sopenharmony_civoid vmci_ctx_unset_notify(struct vmci_ctx *context);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ciint vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle);
15562306a36Sopenharmony_ciint vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle);
15662306a36Sopenharmony_ciint vmci_ctx_dbell_destroy_all(u32 context_id);
15762306a36Sopenharmony_ciint vmci_ctx_notify_dbell(u32 cid, struct vmci_handle handle,
15862306a36Sopenharmony_ci			  u32 src_priv_flags);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciint vmci_ctx_rcv_notifications_get(u32 context_id, struct vmci_handle_arr
16162306a36Sopenharmony_ci				   **db_handle_array, struct vmci_handle_arr
16262306a36Sopenharmony_ci				   **qp_handle_array);
16362306a36Sopenharmony_civoid vmci_ctx_rcv_notifications_release(u32 context_id, struct vmci_handle_arr
16462306a36Sopenharmony_ci					*db_handle_array, struct vmci_handle_arr
16562306a36Sopenharmony_ci					*qp_handle_array, bool success);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic inline u32 vmci_ctx_get_id(struct vmci_ctx *context)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	if (!context)
17062306a36Sopenharmony_ci		return VMCI_INVALID_ID;
17162306a36Sopenharmony_ci	return context->cid;
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci#endif /* _VMCI_CONTEXT_H_ */
175