xref: /linux/drivers/misc/vmw_vmci/vmci_context.h (revision bfd5bb6f90af092aa345b15cd78143956a13c2a8)
1 /*
2  * VMware VMCI driver (vmciContext.h)
3  *
4  * Copyright (C) 2012 VMware, Inc. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation version 2 and no later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  */
15 
16 #ifndef _VMCI_CONTEXT_H_
17 #define _VMCI_CONTEXT_H_
18 
19 #include <linux/vmw_vmci_defs.h>
20 #include <linux/atomic.h>
21 #include <linux/kref.h>
22 #include <linux/types.h>
23 #include <linux/wait.h>
24 
25 #include "vmci_handle_array.h"
26 #include "vmci_datagram.h"
27 
28 /* Used to determine what checkpoint state to get and set. */
29 enum {
30 	VMCI_NOTIFICATION_CPT_STATE = 1,
31 	VMCI_WELLKNOWN_CPT_STATE    = 2,
32 	VMCI_DG_OUT_STATE           = 3,
33 	VMCI_DG_IN_STATE            = 4,
34 	VMCI_DG_IN_SIZE_STATE       = 5,
35 	VMCI_DOORBELL_CPT_STATE     = 6,
36 };
37 
38 /* Host specific struct used for signalling */
39 struct vmci_host {
40 	wait_queue_head_t wait_queue;
41 };
42 
43 struct vmci_handle_list {
44 	struct list_head node;
45 	struct vmci_handle handle;
46 };
47 
48 struct vmci_ctx {
49 	struct list_head list_item;       /* For global VMCI list. */
50 	u32 cid;
51 	struct kref kref;
52 	struct list_head datagram_queue;  /* Head of per VM queue. */
53 	u32 pending_datagrams;
54 	size_t datagram_queue_size;	  /* Size of datagram queue in bytes. */
55 
56 	/*
57 	 * Version of the code that created
58 	 * this context; e.g., VMX.
59 	 */
60 	int user_version;
61 	spinlock_t lock;  /* Locks callQueue and handle_arrays. */
62 
63 	/*
64 	 * queue_pairs attached to.  The array of
65 	 * handles for queue pairs is accessed
66 	 * from the code for QP API, and there
67 	 * it is protected by the QP lock.  It
68 	 * is also accessed from the context
69 	 * clean up path, which does not
70 	 * require a lock.  VMCILock is not
71 	 * used to protect the QP array field.
72 	 */
73 	struct vmci_handle_arr *queue_pair_array;
74 
75 	/* Doorbells created by context. */
76 	struct vmci_handle_arr *doorbell_array;
77 
78 	/* Doorbells pending for context. */
79 	struct vmci_handle_arr *pending_doorbell_array;
80 
81 	/* Contexts current context is subscribing to. */
82 	struct list_head notifier_list;
83 	unsigned int n_notifiers;
84 
85 	struct vmci_host host_context;
86 	u32 priv_flags;
87 
88 	const struct cred *cred;
89 	bool *notify;		/* Notify flag pointer - hosted only. */
90 	struct page *notify_page;	/* Page backing the notify UVA. */
91 };
92 
93 /* VMCINotifyAddRemoveInfo: Used to add/remove remote context notifications. */
94 struct vmci_ctx_info {
95 	u32 remote_cid;
96 	int result;
97 };
98 
99 /* VMCICptBufInfo: Used to set/get current context's checkpoint state. */
100 struct vmci_ctx_chkpt_buf_info {
101 	u64 cpt_buf;
102 	u32 cpt_type;
103 	u32 buf_size;
104 	s32 result;
105 	u32 _pad;
106 };
107 
108 /*
109  * VMCINotificationReceiveInfo: Used to recieve pending notifications
110  * for doorbells and queue pairs.
111  */
112 struct vmci_ctx_notify_recv_info {
113 	u64 db_handle_buf_uva;
114 	u64 db_handle_buf_size;
115 	u64 qp_handle_buf_uva;
116 	u64 qp_handle_buf_size;
117 	s32 result;
118 	u32 _pad;
119 };
120 
121 /*
122  * Utilility function that checks whether two entities are allowed
123  * to interact. If one of them is restricted, the other one must
124  * be trusted.
125  */
126 static inline bool vmci_deny_interaction(u32 part_one, u32 part_two)
127 {
128 	return ((part_one & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
129 		!(part_two & VMCI_PRIVILEGE_FLAG_TRUSTED)) ||
130 	       ((part_two & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
131 		!(part_one & VMCI_PRIVILEGE_FLAG_TRUSTED));
132 }
133 
134 struct vmci_ctx *vmci_ctx_create(u32 cid, u32 flags,
135 				 uintptr_t event_hnd, int version,
136 				 const struct cred *cred);
137 void vmci_ctx_destroy(struct vmci_ctx *context);
138 
139 bool vmci_ctx_supports_host_qp(struct vmci_ctx *context);
140 int vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg);
141 int vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
142 			      size_t *max_size, struct vmci_datagram **dg);
143 int vmci_ctx_pending_datagrams(u32 cid, u32 *pending);
144 struct vmci_ctx *vmci_ctx_get(u32 cid);
145 void vmci_ctx_put(struct vmci_ctx *context);
146 bool vmci_ctx_exists(u32 cid);
147 
148 int vmci_ctx_add_notification(u32 context_id, u32 remote_cid);
149 int vmci_ctx_remove_notification(u32 context_id, u32 remote_cid);
150 int vmci_ctx_get_chkpt_state(u32 context_id, u32 cpt_type,
151 			     u32 *num_cids, void **cpt_buf_ptr);
152 int vmci_ctx_set_chkpt_state(u32 context_id, u32 cpt_type,
153 			     u32 num_cids, void *cpt_buf);
154 
155 int vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle);
156 int vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle);
157 bool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle);
158 
159 void vmci_ctx_check_signal_notify(struct vmci_ctx *context);
160 void vmci_ctx_unset_notify(struct vmci_ctx *context);
161 
162 int vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle);
163 int vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle);
164 int vmci_ctx_dbell_destroy_all(u32 context_id);
165 int vmci_ctx_notify_dbell(u32 cid, struct vmci_handle handle,
166 			  u32 src_priv_flags);
167 
168 int vmci_ctx_rcv_notifications_get(u32 context_id, struct vmci_handle_arr
169 				   **db_handle_array, struct vmci_handle_arr
170 				   **qp_handle_array);
171 void vmci_ctx_rcv_notifications_release(u32 context_id, struct vmci_handle_arr
172 					*db_handle_array, struct vmci_handle_arr
173 					*qp_handle_array, bool success);
174 
175 static inline u32 vmci_ctx_get_id(struct vmci_ctx *context)
176 {
177 	if (!context)
178 		return VMCI_INVALID_ID;
179 	return context->cid;
180 }
181 
182 #endif /* _VMCI_CONTEXT_H_ */
183