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