1685a6bf8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
228d6692cSGeorge Zhang /*
328d6692cSGeorge Zhang  * VMware VMCI driver (vmciContext.h)
428d6692cSGeorge Zhang  *
528d6692cSGeorge Zhang  * Copyright (C) 2012 VMware, Inc. All rights reserved.
628d6692cSGeorge Zhang  */
728d6692cSGeorge Zhang 
828d6692cSGeorge Zhang #ifndef _VMCI_CONTEXT_H_
928d6692cSGeorge Zhang #define _VMCI_CONTEXT_H_
1028d6692cSGeorge Zhang 
1128d6692cSGeorge Zhang #include <linux/vmw_vmci_defs.h>
1228d6692cSGeorge Zhang #include <linux/atomic.h>
1328d6692cSGeorge Zhang #include <linux/kref.h>
1428d6692cSGeorge Zhang #include <linux/types.h>
1528d6692cSGeorge Zhang #include <linux/wait.h>
1628d6692cSGeorge Zhang 
1728d6692cSGeorge Zhang #include "vmci_handle_array.h"
1828d6692cSGeorge Zhang #include "vmci_datagram.h"
1928d6692cSGeorge Zhang 
2028d6692cSGeorge Zhang /* Used to determine what checkpoint state to get and set. */
2128d6692cSGeorge Zhang enum {
2228d6692cSGeorge Zhang 	VMCI_NOTIFICATION_CPT_STATE = 1,
2328d6692cSGeorge Zhang 	VMCI_WELLKNOWN_CPT_STATE    = 2,
2428d6692cSGeorge Zhang 	VMCI_DG_OUT_STATE           = 3,
2528d6692cSGeorge Zhang 	VMCI_DG_IN_STATE            = 4,
2628d6692cSGeorge Zhang 	VMCI_DG_IN_SIZE_STATE       = 5,
2728d6692cSGeorge Zhang 	VMCI_DOORBELL_CPT_STATE     = 6,
2828d6692cSGeorge Zhang };
2928d6692cSGeorge Zhang 
3028d6692cSGeorge Zhang /* Host specific struct used for signalling */
3128d6692cSGeorge Zhang struct vmci_host {
3228d6692cSGeorge Zhang 	wait_queue_head_t wait_queue;
3328d6692cSGeorge Zhang };
3428d6692cSGeorge Zhang 
3528d6692cSGeorge Zhang struct vmci_handle_list {
3628d6692cSGeorge Zhang 	struct list_head node;
3728d6692cSGeorge Zhang 	struct vmci_handle handle;
3828d6692cSGeorge Zhang };
3928d6692cSGeorge Zhang 
4028d6692cSGeorge Zhang struct vmci_ctx {
4128d6692cSGeorge Zhang 	struct list_head list_item;       /* For global VMCI list. */
4228d6692cSGeorge Zhang 	u32 cid;
4328d6692cSGeorge Zhang 	struct kref kref;
4428d6692cSGeorge Zhang 	struct list_head datagram_queue;  /* Head of per VM queue. */
4528d6692cSGeorge Zhang 	u32 pending_datagrams;
4628d6692cSGeorge Zhang 	size_t datagram_queue_size;	  /* Size of datagram queue in bytes. */
4728d6692cSGeorge Zhang 
4828d6692cSGeorge Zhang 	/*
4928d6692cSGeorge Zhang 	 * Version of the code that created
5028d6692cSGeorge Zhang 	 * this context; e.g., VMX.
5128d6692cSGeorge Zhang 	 */
5228d6692cSGeorge Zhang 	int user_version;
5328d6692cSGeorge Zhang 	spinlock_t lock;  /* Locks callQueue and handle_arrays. */
5428d6692cSGeorge Zhang 
5528d6692cSGeorge Zhang 	/*
5628d6692cSGeorge Zhang 	 * queue_pairs attached to.  The array of
5728d6692cSGeorge Zhang 	 * handles for queue pairs is accessed
5828d6692cSGeorge Zhang 	 * from the code for QP API, and there
5928d6692cSGeorge Zhang 	 * it is protected by the QP lock.  It
6028d6692cSGeorge Zhang 	 * is also accessed from the context
6128d6692cSGeorge Zhang 	 * clean up path, which does not
6228d6692cSGeorge Zhang 	 * require a lock.  VMCILock is not
6328d6692cSGeorge Zhang 	 * used to protect the QP array field.
6428d6692cSGeorge Zhang 	 */
6528d6692cSGeorge Zhang 	struct vmci_handle_arr *queue_pair_array;
6628d6692cSGeorge Zhang 
6728d6692cSGeorge Zhang 	/* Doorbells created by context. */
6828d6692cSGeorge Zhang 	struct vmci_handle_arr *doorbell_array;
6928d6692cSGeorge Zhang 
7028d6692cSGeorge Zhang 	/* Doorbells pending for context. */
7128d6692cSGeorge Zhang 	struct vmci_handle_arr *pending_doorbell_array;
7228d6692cSGeorge Zhang 
7328d6692cSGeorge Zhang 	/* Contexts current context is subscribing to. */
7428d6692cSGeorge Zhang 	struct list_head notifier_list;
7528d6692cSGeorge Zhang 	unsigned int n_notifiers;
7628d6692cSGeorge Zhang 
7728d6692cSGeorge Zhang 	struct vmci_host host_context;
7828d6692cSGeorge Zhang 	u32 priv_flags;
7928d6692cSGeorge Zhang 
8028d6692cSGeorge Zhang 	const struct cred *cred;
8128d6692cSGeorge Zhang 	bool *notify;		/* Notify flag pointer - hosted only. */
8228d6692cSGeorge Zhang 	struct page *notify_page;	/* Page backing the notify UVA. */
8328d6692cSGeorge Zhang };
8428d6692cSGeorge Zhang 
8528d6692cSGeorge Zhang /* VMCINotifyAddRemoveInfo: Used to add/remove remote context notifications. */
8628d6692cSGeorge Zhang struct vmci_ctx_info {
8728d6692cSGeorge Zhang 	u32 remote_cid;
8828d6692cSGeorge Zhang 	int result;
8928d6692cSGeorge Zhang };
9028d6692cSGeorge Zhang 
9128d6692cSGeorge Zhang /* VMCICptBufInfo: Used to set/get current context's checkpoint state. */
9228d6692cSGeorge Zhang struct vmci_ctx_chkpt_buf_info {
9328d6692cSGeorge Zhang 	u64 cpt_buf;
9428d6692cSGeorge Zhang 	u32 cpt_type;
9528d6692cSGeorge Zhang 	u32 buf_size;
9628d6692cSGeorge Zhang 	s32 result;
9728d6692cSGeorge Zhang 	u32 _pad;
9828d6692cSGeorge Zhang };
9928d6692cSGeorge Zhang 
10028d6692cSGeorge Zhang /*
10128d6692cSGeorge Zhang  * VMCINotificationReceiveInfo: Used to recieve pending notifications
10228d6692cSGeorge Zhang  * for doorbells and queue pairs.
10328d6692cSGeorge Zhang  */
10428d6692cSGeorge Zhang struct vmci_ctx_notify_recv_info {
10528d6692cSGeorge Zhang 	u64 db_handle_buf_uva;
10628d6692cSGeorge Zhang 	u64 db_handle_buf_size;
10728d6692cSGeorge Zhang 	u64 qp_handle_buf_uva;
10828d6692cSGeorge Zhang 	u64 qp_handle_buf_size;
10928d6692cSGeorge Zhang 	s32 result;
11028d6692cSGeorge Zhang 	u32 _pad;
11128d6692cSGeorge Zhang };
11228d6692cSGeorge Zhang 
11328d6692cSGeorge Zhang /*
11428d6692cSGeorge Zhang  * Utilility function that checks whether two entities are allowed
11528d6692cSGeorge Zhang  * to interact. If one of them is restricted, the other one must
11628d6692cSGeorge Zhang  * be trusted.
11728d6692cSGeorge Zhang  */
vmci_deny_interaction(u32 part_one,u32 part_two)11828d6692cSGeorge Zhang static inline bool vmci_deny_interaction(u32 part_one, u32 part_two)
11928d6692cSGeorge Zhang {
12028d6692cSGeorge Zhang 	return ((part_one & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
12128d6692cSGeorge Zhang 		!(part_two & VMCI_PRIVILEGE_FLAG_TRUSTED)) ||
12228d6692cSGeorge Zhang 	       ((part_two & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
12328d6692cSGeorge Zhang 		!(part_one & VMCI_PRIVILEGE_FLAG_TRUSTED));
12428d6692cSGeorge Zhang }
12528d6692cSGeorge Zhang 
12628d6692cSGeorge Zhang struct vmci_ctx *vmci_ctx_create(u32 cid, u32 flags,
12728d6692cSGeorge Zhang 				 uintptr_t event_hnd, int version,
12828d6692cSGeorge Zhang 				 const struct cred *cred);
12928d6692cSGeorge Zhang void vmci_ctx_destroy(struct vmci_ctx *context);
13028d6692cSGeorge Zhang 
13128d6692cSGeorge Zhang bool vmci_ctx_supports_host_qp(struct vmci_ctx *context);
13228d6692cSGeorge Zhang int vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg);
13328d6692cSGeorge Zhang int vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
13428d6692cSGeorge Zhang 			      size_t *max_size, struct vmci_datagram **dg);
13528d6692cSGeorge Zhang int vmci_ctx_pending_datagrams(u32 cid, u32 *pending);
13628d6692cSGeorge Zhang struct vmci_ctx *vmci_ctx_get(u32 cid);
13728d6692cSGeorge Zhang void vmci_ctx_put(struct vmci_ctx *context);
13828d6692cSGeorge Zhang bool vmci_ctx_exists(u32 cid);
13928d6692cSGeorge Zhang 
14028d6692cSGeorge Zhang int vmci_ctx_add_notification(u32 context_id, u32 remote_cid);
14128d6692cSGeorge Zhang int vmci_ctx_remove_notification(u32 context_id, u32 remote_cid);
14228d6692cSGeorge Zhang int vmci_ctx_get_chkpt_state(u32 context_id, u32 cpt_type,
14328d6692cSGeorge Zhang 			     u32 *num_cids, void **cpt_buf_ptr);
14428d6692cSGeorge Zhang int vmci_ctx_set_chkpt_state(u32 context_id, u32 cpt_type,
14528d6692cSGeorge Zhang 			     u32 num_cids, void *cpt_buf);
14628d6692cSGeorge Zhang 
14728d6692cSGeorge Zhang int vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle);
14828d6692cSGeorge Zhang int vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle);
14928d6692cSGeorge Zhang bool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle);
15028d6692cSGeorge Zhang 
15128d6692cSGeorge Zhang void vmci_ctx_check_signal_notify(struct vmci_ctx *context);
15228d6692cSGeorge Zhang void vmci_ctx_unset_notify(struct vmci_ctx *context);
15328d6692cSGeorge Zhang 
15428d6692cSGeorge Zhang int vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle);
15528d6692cSGeorge Zhang int vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle);
15628d6692cSGeorge Zhang int vmci_ctx_dbell_destroy_all(u32 context_id);
15728d6692cSGeorge Zhang int vmci_ctx_notify_dbell(u32 cid, struct vmci_handle handle,
15828d6692cSGeorge Zhang 			  u32 src_priv_flags);
15928d6692cSGeorge Zhang 
16028d6692cSGeorge Zhang int vmci_ctx_rcv_notifications_get(u32 context_id, struct vmci_handle_arr
16128d6692cSGeorge Zhang 				   **db_handle_array, struct vmci_handle_arr
16228d6692cSGeorge Zhang 				   **qp_handle_array);
16328d6692cSGeorge Zhang void vmci_ctx_rcv_notifications_release(u32 context_id, struct vmci_handle_arr
16428d6692cSGeorge Zhang 					*db_handle_array, struct vmci_handle_arr
16528d6692cSGeorge Zhang 					*qp_handle_array, bool success);
16628d6692cSGeorge Zhang 
vmci_ctx_get_id(struct vmci_ctx * context)16728d6692cSGeorge Zhang static inline u32 vmci_ctx_get_id(struct vmci_ctx *context)
16828d6692cSGeorge Zhang {
16928d6692cSGeorge Zhang 	if (!context)
17028d6692cSGeorge Zhang 		return VMCI_INVALID_ID;
17128d6692cSGeorge Zhang 	return context->cid;
17228d6692cSGeorge Zhang }
17328d6692cSGeorge Zhang 
17428d6692cSGeorge Zhang #endif /* _VMCI_CONTEXT_H_ */
175