1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * VMware VMCI Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 VMware, Inc. All rights reserved.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _VMW_VMCI_DEF_H_
9*4882a593Smuzhiyun #define _VMW_VMCI_DEF_H_
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/atomic.h>
12*4882a593Smuzhiyun #include <linux/bits.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /* Register offsets. */
15*4882a593Smuzhiyun #define VMCI_STATUS_ADDR 0x00
16*4882a593Smuzhiyun #define VMCI_CONTROL_ADDR 0x04
17*4882a593Smuzhiyun #define VMCI_ICR_ADDR 0x08
18*4882a593Smuzhiyun #define VMCI_IMR_ADDR 0x0c
19*4882a593Smuzhiyun #define VMCI_DATA_OUT_ADDR 0x10
20*4882a593Smuzhiyun #define VMCI_DATA_IN_ADDR 0x14
21*4882a593Smuzhiyun #define VMCI_CAPS_ADDR 0x18
22*4882a593Smuzhiyun #define VMCI_RESULT_LOW_ADDR 0x1c
23*4882a593Smuzhiyun #define VMCI_RESULT_HIGH_ADDR 0x20
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /* Max number of devices. */
26*4882a593Smuzhiyun #define VMCI_MAX_DEVICES 1
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* Status register bits. */
29*4882a593Smuzhiyun #define VMCI_STATUS_INT_ON BIT(0)
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* Control register bits. */
32*4882a593Smuzhiyun #define VMCI_CONTROL_RESET BIT(0)
33*4882a593Smuzhiyun #define VMCI_CONTROL_INT_ENABLE BIT(1)
34*4882a593Smuzhiyun #define VMCI_CONTROL_INT_DISABLE BIT(2)
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /* Capabilities register bits. */
37*4882a593Smuzhiyun #define VMCI_CAPS_HYPERCALL BIT(0)
38*4882a593Smuzhiyun #define VMCI_CAPS_GUESTCALL BIT(1)
39*4882a593Smuzhiyun #define VMCI_CAPS_DATAGRAM BIT(2)
40*4882a593Smuzhiyun #define VMCI_CAPS_NOTIFICATIONS BIT(3)
41*4882a593Smuzhiyun #define VMCI_CAPS_PPN64 BIT(4)
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* Interrupt Cause register bits. */
44*4882a593Smuzhiyun #define VMCI_ICR_DATAGRAM BIT(0)
45*4882a593Smuzhiyun #define VMCI_ICR_NOTIFICATION BIT(1)
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* Interrupt Mask register bits. */
48*4882a593Smuzhiyun #define VMCI_IMR_DATAGRAM BIT(0)
49*4882a593Smuzhiyun #define VMCI_IMR_NOTIFICATION BIT(1)
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* Maximum MSI/MSI-X interrupt vectors in the device. */
52*4882a593Smuzhiyun #define VMCI_MAX_INTRS 2
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun * Supported interrupt vectors. There is one for each ICR value above,
56*4882a593Smuzhiyun * but here they indicate the position in the vector array/message ID.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun enum {
59*4882a593Smuzhiyun VMCI_INTR_DATAGRAM = 0,
60*4882a593Smuzhiyun VMCI_INTR_NOTIFICATION = 1,
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun * A single VMCI device has an upper limit of 128MB on the amount of
65*4882a593Smuzhiyun * memory that can be used for queue pairs. Since each queue pair
66*4882a593Smuzhiyun * consists of at least two pages, the memory limit also dictates the
67*4882a593Smuzhiyun * number of queue pairs a guest can create.
68*4882a593Smuzhiyun */
69*4882a593Smuzhiyun #define VMCI_MAX_GUEST_QP_MEMORY (128 * 1024 * 1024)
70*4882a593Smuzhiyun #define VMCI_MAX_GUEST_QP_COUNT (VMCI_MAX_GUEST_QP_MEMORY / PAGE_SIZE / 2)
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * There can be at most PAGE_SIZE doorbells since there is one doorbell
74*4882a593Smuzhiyun * per byte in the doorbell bitmap page.
75*4882a593Smuzhiyun */
76*4882a593Smuzhiyun #define VMCI_MAX_GUEST_DOORBELL_COUNT PAGE_SIZE
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * Queues with pre-mapped data pages must be small, so that we don't pin
80*4882a593Smuzhiyun * too much kernel memory (especially on vmkernel). We limit a queuepair to
81*4882a593Smuzhiyun * 32 KB, or 16 KB per queue for symmetrical pairs.
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun #define VMCI_MAX_PINNED_QP_MEMORY (32 * 1024)
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * We have a fixed set of resource IDs available in the VMX.
87*4882a593Smuzhiyun * This allows us to have a very simple implementation since we statically
88*4882a593Smuzhiyun * know how many will create datagram handles. If a new caller arrives and
89*4882a593Smuzhiyun * we have run out of slots we can manually increment the maximum size of
90*4882a593Smuzhiyun * available resource IDs.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * VMCI reserved hypervisor datagram resource IDs.
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun enum {
95*4882a593Smuzhiyun VMCI_RESOURCES_QUERY = 0,
96*4882a593Smuzhiyun VMCI_GET_CONTEXT_ID = 1,
97*4882a593Smuzhiyun VMCI_SET_NOTIFY_BITMAP = 2,
98*4882a593Smuzhiyun VMCI_DOORBELL_LINK = 3,
99*4882a593Smuzhiyun VMCI_DOORBELL_UNLINK = 4,
100*4882a593Smuzhiyun VMCI_DOORBELL_NOTIFY = 5,
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * VMCI_DATAGRAM_REQUEST_MAP and VMCI_DATAGRAM_REMOVE_MAP are
103*4882a593Smuzhiyun * obsoleted by the removal of VM to VM communication.
104*4882a593Smuzhiyun */
105*4882a593Smuzhiyun VMCI_DATAGRAM_REQUEST_MAP = 6,
106*4882a593Smuzhiyun VMCI_DATAGRAM_REMOVE_MAP = 7,
107*4882a593Smuzhiyun VMCI_EVENT_SUBSCRIBE = 8,
108*4882a593Smuzhiyun VMCI_EVENT_UNSUBSCRIBE = 9,
109*4882a593Smuzhiyun VMCI_QUEUEPAIR_ALLOC = 10,
110*4882a593Smuzhiyun VMCI_QUEUEPAIR_DETACH = 11,
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun * VMCI_VSOCK_VMX_LOOKUP was assigned to 12 for Fusion 3.0/3.1,
114*4882a593Smuzhiyun * WS 7.0/7.1 and ESX 4.1
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun VMCI_HGFS_TRANSPORT = 13,
117*4882a593Smuzhiyun VMCI_UNITY_PBRPC_REGISTER = 14,
118*4882a593Smuzhiyun VMCI_RPC_PRIVILEGED = 15,
119*4882a593Smuzhiyun VMCI_RPC_UNPRIVILEGED = 16,
120*4882a593Smuzhiyun VMCI_RESOURCE_MAX = 17,
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun * struct vmci_handle - Ownership information structure
125*4882a593Smuzhiyun * @context: The VMX context ID.
126*4882a593Smuzhiyun * @resource: The resource ID (used for locating in resource hash).
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * The vmci_handle structure is used to track resources used within
129*4882a593Smuzhiyun * vmw_vmci.
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun struct vmci_handle {
132*4882a593Smuzhiyun u32 context;
133*4882a593Smuzhiyun u32 resource;
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #define vmci_make_handle(_cid, _rid) \
137*4882a593Smuzhiyun (struct vmci_handle){ .context = _cid, .resource = _rid }
138*4882a593Smuzhiyun
vmci_handle_is_equal(struct vmci_handle h1,struct vmci_handle h2)139*4882a593Smuzhiyun static inline bool vmci_handle_is_equal(struct vmci_handle h1,
140*4882a593Smuzhiyun struct vmci_handle h2)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun return h1.context == h2.context && h1.resource == h2.resource;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun #define VMCI_INVALID_ID ~0
146*4882a593Smuzhiyun static const struct vmci_handle VMCI_INVALID_HANDLE = {
147*4882a593Smuzhiyun .context = VMCI_INVALID_ID,
148*4882a593Smuzhiyun .resource = VMCI_INVALID_ID
149*4882a593Smuzhiyun };
150*4882a593Smuzhiyun
vmci_handle_is_invalid(struct vmci_handle h)151*4882a593Smuzhiyun static inline bool vmci_handle_is_invalid(struct vmci_handle h)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun return vmci_handle_is_equal(h, VMCI_INVALID_HANDLE);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /*
157*4882a593Smuzhiyun * The below defines can be used to send anonymous requests.
158*4882a593Smuzhiyun * This also indicates that no response is expected.
159*4882a593Smuzhiyun */
160*4882a593Smuzhiyun #define VMCI_ANON_SRC_CONTEXT_ID VMCI_INVALID_ID
161*4882a593Smuzhiyun #define VMCI_ANON_SRC_RESOURCE_ID VMCI_INVALID_ID
162*4882a593Smuzhiyun static const struct vmci_handle __maybe_unused VMCI_ANON_SRC_HANDLE = {
163*4882a593Smuzhiyun .context = VMCI_ANON_SRC_CONTEXT_ID,
164*4882a593Smuzhiyun .resource = VMCI_ANON_SRC_RESOURCE_ID
165*4882a593Smuzhiyun };
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /* The lowest 16 context ids are reserved for internal use. */
168*4882a593Smuzhiyun #define VMCI_RESERVED_CID_LIMIT ((u32) 16)
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * Hypervisor context id, used for calling into hypervisor
172*4882a593Smuzhiyun * supplied services from the VM.
173*4882a593Smuzhiyun */
174*4882a593Smuzhiyun #define VMCI_HYPERVISOR_CONTEXT_ID 0
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun * Well-known context id, a logical context that contains a set of
178*4882a593Smuzhiyun * well-known services. This context ID is now obsolete.
179*4882a593Smuzhiyun */
180*4882a593Smuzhiyun #define VMCI_WELL_KNOWN_CONTEXT_ID 1
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * Context ID used by host endpoints.
184*4882a593Smuzhiyun */
185*4882a593Smuzhiyun #define VMCI_HOST_CONTEXT_ID 2
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun #define VMCI_CONTEXT_IS_VM(_cid) (VMCI_INVALID_ID != (_cid) && \
188*4882a593Smuzhiyun (_cid) > VMCI_HOST_CONTEXT_ID)
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /*
191*4882a593Smuzhiyun * The VMCI_CONTEXT_RESOURCE_ID is used together with vmci_make_handle to make
192*4882a593Smuzhiyun * handles that refer to a specific context.
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun #define VMCI_CONTEXT_RESOURCE_ID 0
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /*
197*4882a593Smuzhiyun * VMCI error codes.
198*4882a593Smuzhiyun */
199*4882a593Smuzhiyun enum {
200*4882a593Smuzhiyun VMCI_SUCCESS_QUEUEPAIR_ATTACH = 5,
201*4882a593Smuzhiyun VMCI_SUCCESS_QUEUEPAIR_CREATE = 4,
202*4882a593Smuzhiyun VMCI_SUCCESS_LAST_DETACH = 3,
203*4882a593Smuzhiyun VMCI_SUCCESS_ACCESS_GRANTED = 2,
204*4882a593Smuzhiyun VMCI_SUCCESS_ENTRY_DEAD = 1,
205*4882a593Smuzhiyun VMCI_SUCCESS = 0,
206*4882a593Smuzhiyun VMCI_ERROR_INVALID_RESOURCE = (-1),
207*4882a593Smuzhiyun VMCI_ERROR_INVALID_ARGS = (-2),
208*4882a593Smuzhiyun VMCI_ERROR_NO_MEM = (-3),
209*4882a593Smuzhiyun VMCI_ERROR_DATAGRAM_FAILED = (-4),
210*4882a593Smuzhiyun VMCI_ERROR_MORE_DATA = (-5),
211*4882a593Smuzhiyun VMCI_ERROR_NO_MORE_DATAGRAMS = (-6),
212*4882a593Smuzhiyun VMCI_ERROR_NO_ACCESS = (-7),
213*4882a593Smuzhiyun VMCI_ERROR_NO_HANDLE = (-8),
214*4882a593Smuzhiyun VMCI_ERROR_DUPLICATE_ENTRY = (-9),
215*4882a593Smuzhiyun VMCI_ERROR_DST_UNREACHABLE = (-10),
216*4882a593Smuzhiyun VMCI_ERROR_PAYLOAD_TOO_LARGE = (-11),
217*4882a593Smuzhiyun VMCI_ERROR_INVALID_PRIV = (-12),
218*4882a593Smuzhiyun VMCI_ERROR_GENERIC = (-13),
219*4882a593Smuzhiyun VMCI_ERROR_PAGE_ALREADY_SHARED = (-14),
220*4882a593Smuzhiyun VMCI_ERROR_CANNOT_SHARE_PAGE = (-15),
221*4882a593Smuzhiyun VMCI_ERROR_CANNOT_UNSHARE_PAGE = (-16),
222*4882a593Smuzhiyun VMCI_ERROR_NO_PROCESS = (-17),
223*4882a593Smuzhiyun VMCI_ERROR_NO_DATAGRAM = (-18),
224*4882a593Smuzhiyun VMCI_ERROR_NO_RESOURCES = (-19),
225*4882a593Smuzhiyun VMCI_ERROR_UNAVAILABLE = (-20),
226*4882a593Smuzhiyun VMCI_ERROR_NOT_FOUND = (-21),
227*4882a593Smuzhiyun VMCI_ERROR_ALREADY_EXISTS = (-22),
228*4882a593Smuzhiyun VMCI_ERROR_NOT_PAGE_ALIGNED = (-23),
229*4882a593Smuzhiyun VMCI_ERROR_INVALID_SIZE = (-24),
230*4882a593Smuzhiyun VMCI_ERROR_REGION_ALREADY_SHARED = (-25),
231*4882a593Smuzhiyun VMCI_ERROR_TIMEOUT = (-26),
232*4882a593Smuzhiyun VMCI_ERROR_DATAGRAM_INCOMPLETE = (-27),
233*4882a593Smuzhiyun VMCI_ERROR_INCORRECT_IRQL = (-28),
234*4882a593Smuzhiyun VMCI_ERROR_EVENT_UNKNOWN = (-29),
235*4882a593Smuzhiyun VMCI_ERROR_OBSOLETE = (-30),
236*4882a593Smuzhiyun VMCI_ERROR_QUEUEPAIR_MISMATCH = (-31),
237*4882a593Smuzhiyun VMCI_ERROR_QUEUEPAIR_NOTSET = (-32),
238*4882a593Smuzhiyun VMCI_ERROR_QUEUEPAIR_NOTOWNER = (-33),
239*4882a593Smuzhiyun VMCI_ERROR_QUEUEPAIR_NOTATTACHED = (-34),
240*4882a593Smuzhiyun VMCI_ERROR_QUEUEPAIR_NOSPACE = (-35),
241*4882a593Smuzhiyun VMCI_ERROR_QUEUEPAIR_NODATA = (-36),
242*4882a593Smuzhiyun VMCI_ERROR_BUSMEM_INVALIDATION = (-37),
243*4882a593Smuzhiyun VMCI_ERROR_MODULE_NOT_LOADED = (-38),
244*4882a593Smuzhiyun VMCI_ERROR_DEVICE_NOT_FOUND = (-39),
245*4882a593Smuzhiyun VMCI_ERROR_QUEUEPAIR_NOT_READY = (-40),
246*4882a593Smuzhiyun VMCI_ERROR_WOULD_BLOCK = (-41),
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* VMCI clients should return error code within this range */
249*4882a593Smuzhiyun VMCI_ERROR_CLIENT_MIN = (-500),
250*4882a593Smuzhiyun VMCI_ERROR_CLIENT_MAX = (-550),
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* Internal error codes. */
253*4882a593Smuzhiyun VMCI_SHAREDMEM_ERROR_BAD_CONTEXT = (-1000),
254*4882a593Smuzhiyun };
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* VMCI reserved events. */
257*4882a593Smuzhiyun enum {
258*4882a593Smuzhiyun /* Only applicable to guest endpoints */
259*4882a593Smuzhiyun VMCI_EVENT_CTX_ID_UPDATE = 0,
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /* Applicable to guest and host */
262*4882a593Smuzhiyun VMCI_EVENT_CTX_REMOVED = 1,
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /* Only applicable to guest endpoints */
265*4882a593Smuzhiyun VMCI_EVENT_QP_RESUMED = 2,
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /* Applicable to guest and host */
268*4882a593Smuzhiyun VMCI_EVENT_QP_PEER_ATTACH = 3,
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /* Applicable to guest and host */
271*4882a593Smuzhiyun VMCI_EVENT_QP_PEER_DETACH = 4,
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /*
274*4882a593Smuzhiyun * Applicable to VMX and vmk. On vmk,
275*4882a593Smuzhiyun * this event has the Context payload type.
276*4882a593Smuzhiyun */
277*4882a593Smuzhiyun VMCI_EVENT_MEM_ACCESS_ON = 5,
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * Applicable to VMX and vmk. Same as
281*4882a593Smuzhiyun * above for the payload type.
282*4882a593Smuzhiyun */
283*4882a593Smuzhiyun VMCI_EVENT_MEM_ACCESS_OFF = 6,
284*4882a593Smuzhiyun VMCI_EVENT_MAX = 7,
285*4882a593Smuzhiyun };
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * Of the above events, a few are reserved for use in the VMX, and
289*4882a593Smuzhiyun * other endpoints (guest and host kernel) should not use them. For
290*4882a593Smuzhiyun * the rest of the events, we allow both host and guest endpoints to
291*4882a593Smuzhiyun * subscribe to them, to maintain the same API for host and guest
292*4882a593Smuzhiyun * endpoints.
293*4882a593Smuzhiyun */
294*4882a593Smuzhiyun #define VMCI_EVENT_VALID_VMX(_event) ((_event) == VMCI_EVENT_MEM_ACCESS_ON || \
295*4882a593Smuzhiyun (_event) == VMCI_EVENT_MEM_ACCESS_OFF)
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun #define VMCI_EVENT_VALID(_event) ((_event) < VMCI_EVENT_MAX && \
298*4882a593Smuzhiyun !VMCI_EVENT_VALID_VMX(_event))
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /* Reserved guest datagram resource ids. */
301*4882a593Smuzhiyun #define VMCI_EVENT_HANDLER 0
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /*
304*4882a593Smuzhiyun * VMCI coarse-grained privileges (per context or host
305*4882a593Smuzhiyun * process/endpoint. An entity with the restricted flag is only
306*4882a593Smuzhiyun * allowed to interact with the hypervisor and trusted entities.
307*4882a593Smuzhiyun */
308*4882a593Smuzhiyun enum {
309*4882a593Smuzhiyun VMCI_NO_PRIVILEGE_FLAGS = 0,
310*4882a593Smuzhiyun VMCI_PRIVILEGE_FLAG_RESTRICTED = 1,
311*4882a593Smuzhiyun VMCI_PRIVILEGE_FLAG_TRUSTED = 2,
312*4882a593Smuzhiyun VMCI_PRIVILEGE_ALL_FLAGS = (VMCI_PRIVILEGE_FLAG_RESTRICTED |
313*4882a593Smuzhiyun VMCI_PRIVILEGE_FLAG_TRUSTED),
314*4882a593Smuzhiyun VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS = VMCI_NO_PRIVILEGE_FLAGS,
315*4882a593Smuzhiyun VMCI_LEAST_PRIVILEGE_FLAGS = VMCI_PRIVILEGE_FLAG_RESTRICTED,
316*4882a593Smuzhiyun VMCI_MAX_PRIVILEGE_FLAGS = VMCI_PRIVILEGE_FLAG_TRUSTED,
317*4882a593Smuzhiyun };
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /* 0 through VMCI_RESERVED_RESOURCE_ID_MAX are reserved. */
320*4882a593Smuzhiyun #define VMCI_RESERVED_RESOURCE_ID_MAX 1023
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /*
323*4882a593Smuzhiyun * Driver version.
324*4882a593Smuzhiyun *
325*4882a593Smuzhiyun * Increment major version when you make an incompatible change.
326*4882a593Smuzhiyun * Compatibility goes both ways (old driver with new executable
327*4882a593Smuzhiyun * as well as new driver with old executable).
328*4882a593Smuzhiyun */
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /* Never change VMCI_VERSION_SHIFT_WIDTH */
331*4882a593Smuzhiyun #define VMCI_VERSION_SHIFT_WIDTH 16
332*4882a593Smuzhiyun #define VMCI_MAKE_VERSION(_major, _minor) \
333*4882a593Smuzhiyun ((_major) << VMCI_VERSION_SHIFT_WIDTH | (u16) (_minor))
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun #define VMCI_VERSION_MAJOR(v) ((u32) (v) >> VMCI_VERSION_SHIFT_WIDTH)
336*4882a593Smuzhiyun #define VMCI_VERSION_MINOR(v) ((u16) (v))
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun /*
339*4882a593Smuzhiyun * VMCI_VERSION is always the current version. Subsequently listed
340*4882a593Smuzhiyun * versions are ways of detecting previous versions of the connecting
341*4882a593Smuzhiyun * application (i.e., VMX).
342*4882a593Smuzhiyun *
343*4882a593Smuzhiyun * VMCI_VERSION_NOVMVM: This version removed support for VM to VM
344*4882a593Smuzhiyun * communication.
345*4882a593Smuzhiyun *
346*4882a593Smuzhiyun * VMCI_VERSION_NOTIFY: This version introduced doorbell notification
347*4882a593Smuzhiyun * support.
348*4882a593Smuzhiyun *
349*4882a593Smuzhiyun * VMCI_VERSION_HOSTQP: This version introduced host end point support
350*4882a593Smuzhiyun * for hosted products.
351*4882a593Smuzhiyun *
352*4882a593Smuzhiyun * VMCI_VERSION_PREHOSTQP: This is the version prior to the adoption of
353*4882a593Smuzhiyun * support for host end-points.
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * VMCI_VERSION_PREVERS2: This fictional version number is intended to
356*4882a593Smuzhiyun * represent the version of a VMX which doesn't call into the driver
357*4882a593Smuzhiyun * with ioctl VERSION2 and thus doesn't establish its version with the
358*4882a593Smuzhiyun * driver.
359*4882a593Smuzhiyun */
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun #define VMCI_VERSION VMCI_VERSION_NOVMVM
362*4882a593Smuzhiyun #define VMCI_VERSION_NOVMVM VMCI_MAKE_VERSION(11, 0)
363*4882a593Smuzhiyun #define VMCI_VERSION_NOTIFY VMCI_MAKE_VERSION(10, 0)
364*4882a593Smuzhiyun #define VMCI_VERSION_HOSTQP VMCI_MAKE_VERSION(9, 0)
365*4882a593Smuzhiyun #define VMCI_VERSION_PREHOSTQP VMCI_MAKE_VERSION(8, 0)
366*4882a593Smuzhiyun #define VMCI_VERSION_PREVERS2 VMCI_MAKE_VERSION(1, 0)
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun #define VMCI_SOCKETS_MAKE_VERSION(_p) \
369*4882a593Smuzhiyun ((((_p)[0] & 0xFF) << 24) | (((_p)[1] & 0xFF) << 16) | ((_p)[2]))
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /*
372*4882a593Smuzhiyun * The VMCI IOCTLs. We use identity code 7, as noted in ioctl-number.h, and
373*4882a593Smuzhiyun * we start at sequence 9f. This gives us the same values that our shipping
374*4882a593Smuzhiyun * products use, starting at 1951, provided we leave out the direction and
375*4882a593Smuzhiyun * structure size. Note that VMMon occupies the block following us, starting
376*4882a593Smuzhiyun * at 2001.
377*4882a593Smuzhiyun */
378*4882a593Smuzhiyun #define IOCTL_VMCI_VERSION _IO(7, 0x9f) /* 1951 */
379*4882a593Smuzhiyun #define IOCTL_VMCI_INIT_CONTEXT _IO(7, 0xa0)
380*4882a593Smuzhiyun #define IOCTL_VMCI_QUEUEPAIR_SETVA _IO(7, 0xa4)
381*4882a593Smuzhiyun #define IOCTL_VMCI_NOTIFY_RESOURCE _IO(7, 0xa5)
382*4882a593Smuzhiyun #define IOCTL_VMCI_NOTIFICATIONS_RECEIVE _IO(7, 0xa6)
383*4882a593Smuzhiyun #define IOCTL_VMCI_VERSION2 _IO(7, 0xa7)
384*4882a593Smuzhiyun #define IOCTL_VMCI_QUEUEPAIR_ALLOC _IO(7, 0xa8)
385*4882a593Smuzhiyun #define IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE _IO(7, 0xa9)
386*4882a593Smuzhiyun #define IOCTL_VMCI_QUEUEPAIR_DETACH _IO(7, 0xaa)
387*4882a593Smuzhiyun #define IOCTL_VMCI_DATAGRAM_SEND _IO(7, 0xab)
388*4882a593Smuzhiyun #define IOCTL_VMCI_DATAGRAM_RECEIVE _IO(7, 0xac)
389*4882a593Smuzhiyun #define IOCTL_VMCI_CTX_ADD_NOTIFICATION _IO(7, 0xaf)
390*4882a593Smuzhiyun #define IOCTL_VMCI_CTX_REMOVE_NOTIFICATION _IO(7, 0xb0)
391*4882a593Smuzhiyun #define IOCTL_VMCI_CTX_GET_CPT_STATE _IO(7, 0xb1)
392*4882a593Smuzhiyun #define IOCTL_VMCI_CTX_SET_CPT_STATE _IO(7, 0xb2)
393*4882a593Smuzhiyun #define IOCTL_VMCI_GET_CONTEXT_ID _IO(7, 0xb3)
394*4882a593Smuzhiyun #define IOCTL_VMCI_SOCKETS_VERSION _IO(7, 0xb4)
395*4882a593Smuzhiyun #define IOCTL_VMCI_SOCKETS_GET_AF_VALUE _IO(7, 0xb8)
396*4882a593Smuzhiyun #define IOCTL_VMCI_SOCKETS_GET_LOCAL_CID _IO(7, 0xb9)
397*4882a593Smuzhiyun #define IOCTL_VMCI_SET_NOTIFY _IO(7, 0xcb) /* 1995 */
398*4882a593Smuzhiyun /*IOCTL_VMMON_START _IO(7, 0xd1)*/ /* 2001 */
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun /*
401*4882a593Smuzhiyun * struct vmci_queue_header - VMCI Queue Header information.
402*4882a593Smuzhiyun *
403*4882a593Smuzhiyun * A Queue cannot stand by itself as designed. Each Queue's header
404*4882a593Smuzhiyun * contains a pointer into itself (the producer_tail) and into its peer
405*4882a593Smuzhiyun * (consumer_head). The reason for the separation is one of
406*4882a593Smuzhiyun * accessibility: Each end-point can modify two things: where the next
407*4882a593Smuzhiyun * location to enqueue is within its produce_q (producer_tail); and
408*4882a593Smuzhiyun * where the next dequeue location is in its consume_q (consumer_head).
409*4882a593Smuzhiyun *
410*4882a593Smuzhiyun * An end-point cannot modify the pointers of its peer (guest to
411*4882a593Smuzhiyun * guest; NOTE that in the host both queue headers are mapped r/w).
412*4882a593Smuzhiyun * But, each end-point needs read access to both Queue header
413*4882a593Smuzhiyun * structures in order to determine how much space is used (or left)
414*4882a593Smuzhiyun * in the Queue. This is because for an end-point to know how full
415*4882a593Smuzhiyun * its produce_q is, it needs to use the consumer_head that points into
416*4882a593Smuzhiyun * the produce_q but -that- consumer_head is in the Queue header for
417*4882a593Smuzhiyun * that end-points consume_q.
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * Thoroughly confused? Sorry.
420*4882a593Smuzhiyun *
421*4882a593Smuzhiyun * producer_tail: the point to enqueue new entrants. When you approach
422*4882a593Smuzhiyun * a line in a store, for example, you walk up to the tail.
423*4882a593Smuzhiyun *
424*4882a593Smuzhiyun * consumer_head: the point in the queue from which the next element is
425*4882a593Smuzhiyun * dequeued. In other words, who is next in line is he who is at the
426*4882a593Smuzhiyun * head of the line.
427*4882a593Smuzhiyun *
428*4882a593Smuzhiyun * Also, producer_tail points to an empty byte in the Queue, whereas
429*4882a593Smuzhiyun * consumer_head points to a valid byte of data (unless producer_tail ==
430*4882a593Smuzhiyun * consumer_head in which case consumer_head does not point to a valid
431*4882a593Smuzhiyun * byte of data).
432*4882a593Smuzhiyun *
433*4882a593Smuzhiyun * For a queue of buffer 'size' bytes, the tail and head pointers will be in
434*4882a593Smuzhiyun * the range [0, size-1].
435*4882a593Smuzhiyun *
436*4882a593Smuzhiyun * If produce_q_header->producer_tail == consume_q_header->consumer_head
437*4882a593Smuzhiyun * then the produce_q is empty.
438*4882a593Smuzhiyun */
439*4882a593Smuzhiyun struct vmci_queue_header {
440*4882a593Smuzhiyun /* All fields are 64bit and aligned. */
441*4882a593Smuzhiyun struct vmci_handle handle; /* Identifier. */
442*4882a593Smuzhiyun u64 producer_tail; /* Offset in this queue. */
443*4882a593Smuzhiyun u64 consumer_head; /* Offset in peer queue. */
444*4882a593Smuzhiyun };
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun * struct vmci_datagram - Base struct for vmci datagrams.
448*4882a593Smuzhiyun * @dst: A vmci_handle that tracks the destination of the datagram.
449*4882a593Smuzhiyun * @src: A vmci_handle that tracks the source of the datagram.
450*4882a593Smuzhiyun * @payload_size: The size of the payload.
451*4882a593Smuzhiyun *
452*4882a593Smuzhiyun * vmci_datagram structs are used when sending vmci datagrams. They include
453*4882a593Smuzhiyun * the necessary source and destination information to properly route
454*4882a593Smuzhiyun * the information along with the size of the package.
455*4882a593Smuzhiyun */
456*4882a593Smuzhiyun struct vmci_datagram {
457*4882a593Smuzhiyun struct vmci_handle dst;
458*4882a593Smuzhiyun struct vmci_handle src;
459*4882a593Smuzhiyun u64 payload_size;
460*4882a593Smuzhiyun };
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /*
463*4882a593Smuzhiyun * Second flag is for creating a well-known handle instead of a per context
464*4882a593Smuzhiyun * handle. Next flag is for deferring datagram delivery, so that the
465*4882a593Smuzhiyun * datagram callback is invoked in a delayed context (not interrupt context).
466*4882a593Smuzhiyun */
467*4882a593Smuzhiyun #define VMCI_FLAG_DG_NONE 0
468*4882a593Smuzhiyun #define VMCI_FLAG_WELLKNOWN_DG_HND BIT(0)
469*4882a593Smuzhiyun #define VMCI_FLAG_ANYCID_DG_HND BIT(1)
470*4882a593Smuzhiyun #define VMCI_FLAG_DG_DELAYED_CB BIT(2)
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun /*
473*4882a593Smuzhiyun * Maximum supported size of a VMCI datagram for routable datagrams.
474*4882a593Smuzhiyun * Datagrams going to the hypervisor are allowed to be larger.
475*4882a593Smuzhiyun */
476*4882a593Smuzhiyun #define VMCI_MAX_DG_SIZE (17 * 4096)
477*4882a593Smuzhiyun #define VMCI_MAX_DG_PAYLOAD_SIZE (VMCI_MAX_DG_SIZE - \
478*4882a593Smuzhiyun sizeof(struct vmci_datagram))
479*4882a593Smuzhiyun #define VMCI_DG_PAYLOAD(_dg) (void *)((char *)(_dg) + \
480*4882a593Smuzhiyun sizeof(struct vmci_datagram))
481*4882a593Smuzhiyun #define VMCI_DG_HEADERSIZE sizeof(struct vmci_datagram)
482*4882a593Smuzhiyun #define VMCI_DG_SIZE(_dg) (VMCI_DG_HEADERSIZE + (size_t)(_dg)->payload_size)
483*4882a593Smuzhiyun #define VMCI_DG_SIZE_ALIGNED(_dg) ((VMCI_DG_SIZE(_dg) + 7) & (~((size_t) 0x7)))
484*4882a593Smuzhiyun #define VMCI_MAX_DATAGRAM_QUEUE_SIZE (VMCI_MAX_DG_SIZE * 2)
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun struct vmci_event_payload_qp {
487*4882a593Smuzhiyun struct vmci_handle handle; /* queue_pair handle. */
488*4882a593Smuzhiyun u32 peer_id; /* Context id of attaching/detaching VM. */
489*4882a593Smuzhiyun u32 _pad;
490*4882a593Smuzhiyun };
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /* Flags for VMCI queue_pair API. */
493*4882a593Smuzhiyun enum {
494*4882a593Smuzhiyun /* Fail alloc if QP not created by peer. */
495*4882a593Smuzhiyun VMCI_QPFLAG_ATTACH_ONLY = 1 << 0,
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /* Only allow attaches from local context. */
498*4882a593Smuzhiyun VMCI_QPFLAG_LOCAL = 1 << 1,
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /* Host won't block when guest is quiesced. */
501*4882a593Smuzhiyun VMCI_QPFLAG_NONBLOCK = 1 << 2,
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /* Pin data pages in ESX. Used with NONBLOCK */
504*4882a593Smuzhiyun VMCI_QPFLAG_PINNED = 1 << 3,
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /* Update the following flag when adding new flags. */
507*4882a593Smuzhiyun VMCI_QP_ALL_FLAGS = (VMCI_QPFLAG_ATTACH_ONLY | VMCI_QPFLAG_LOCAL |
508*4882a593Smuzhiyun VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED),
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /* Convenience flags */
511*4882a593Smuzhiyun VMCI_QP_ASYMM = (VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED),
512*4882a593Smuzhiyun VMCI_QP_ASYMM_PEER = (VMCI_QPFLAG_ATTACH_ONLY | VMCI_QP_ASYMM),
513*4882a593Smuzhiyun };
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /*
516*4882a593Smuzhiyun * We allow at least 1024 more event datagrams from the hypervisor past the
517*4882a593Smuzhiyun * normally allowed datagrams pending for a given context. We define this
518*4882a593Smuzhiyun * limit on event datagrams from the hypervisor to guard against DoS attack
519*4882a593Smuzhiyun * from a malicious VM which could repeatedly attach to and detach from a queue
520*4882a593Smuzhiyun * pair, causing events to be queued at the destination VM. However, the rate
521*4882a593Smuzhiyun * at which such events can be generated is small since it requires a VM exit
522*4882a593Smuzhiyun * and handling of queue pair attach/detach call at the hypervisor. Event
523*4882a593Smuzhiyun * datagrams may be queued up at the destination VM if it has interrupts
524*4882a593Smuzhiyun * disabled or if it is not draining events for some other reason. 1024
525*4882a593Smuzhiyun * datagrams is a grossly conservative estimate of the time for which
526*4882a593Smuzhiyun * interrupts may be disabled in the destination VM, but at the same time does
527*4882a593Smuzhiyun * not exacerbate the memory pressure problem on the host by much (size of each
528*4882a593Smuzhiyun * event datagram is small).
529*4882a593Smuzhiyun */
530*4882a593Smuzhiyun #define VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE \
531*4882a593Smuzhiyun (VMCI_MAX_DATAGRAM_QUEUE_SIZE + \
532*4882a593Smuzhiyun 1024 * (sizeof(struct vmci_datagram) + \
533*4882a593Smuzhiyun sizeof(struct vmci_event_data_max)))
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun /*
536*4882a593Smuzhiyun * Struct used for querying, via VMCI_RESOURCES_QUERY, the availability of
537*4882a593Smuzhiyun * hypervisor resources. Struct size is 16 bytes. All fields in struct are
538*4882a593Smuzhiyun * aligned to their natural alignment.
539*4882a593Smuzhiyun */
540*4882a593Smuzhiyun struct vmci_resource_query_hdr {
541*4882a593Smuzhiyun struct vmci_datagram hdr;
542*4882a593Smuzhiyun u32 num_resources;
543*4882a593Smuzhiyun u32 _padding;
544*4882a593Smuzhiyun };
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun /*
547*4882a593Smuzhiyun * Convenience struct for negotiating vectors. Must match layout of
548*4882a593Smuzhiyun * VMCIResourceQueryHdr minus the struct vmci_datagram header.
549*4882a593Smuzhiyun */
550*4882a593Smuzhiyun struct vmci_resource_query_msg {
551*4882a593Smuzhiyun u32 num_resources;
552*4882a593Smuzhiyun u32 _padding;
553*4882a593Smuzhiyun u32 resources[1];
554*4882a593Smuzhiyun };
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun /*
557*4882a593Smuzhiyun * The maximum number of resources that can be queried using
558*4882a593Smuzhiyun * VMCI_RESOURCE_QUERY is 31, as the result is encoded in the lower 31
559*4882a593Smuzhiyun * bits of a positive return value. Negative values are reserved for
560*4882a593Smuzhiyun * errors.
561*4882a593Smuzhiyun */
562*4882a593Smuzhiyun #define VMCI_RESOURCE_QUERY_MAX_NUM 31
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun /* Maximum size for the VMCI_RESOURCE_QUERY request. */
565*4882a593Smuzhiyun #define VMCI_RESOURCE_QUERY_MAX_SIZE \
566*4882a593Smuzhiyun (sizeof(struct vmci_resource_query_hdr) + \
567*4882a593Smuzhiyun sizeof(u32) * VMCI_RESOURCE_QUERY_MAX_NUM)
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /*
570*4882a593Smuzhiyun * Struct used for setting the notification bitmap. All fields in
571*4882a593Smuzhiyun * struct are aligned to their natural alignment.
572*4882a593Smuzhiyun */
573*4882a593Smuzhiyun struct vmci_notify_bm_set_msg {
574*4882a593Smuzhiyun struct vmci_datagram hdr;
575*4882a593Smuzhiyun union {
576*4882a593Smuzhiyun u32 bitmap_ppn32;
577*4882a593Smuzhiyun u64 bitmap_ppn64;
578*4882a593Smuzhiyun };
579*4882a593Smuzhiyun };
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /*
582*4882a593Smuzhiyun * Struct used for linking a doorbell handle with an index in the
583*4882a593Smuzhiyun * notify bitmap. All fields in struct are aligned to their natural
584*4882a593Smuzhiyun * alignment.
585*4882a593Smuzhiyun */
586*4882a593Smuzhiyun struct vmci_doorbell_link_msg {
587*4882a593Smuzhiyun struct vmci_datagram hdr;
588*4882a593Smuzhiyun struct vmci_handle handle;
589*4882a593Smuzhiyun u64 notify_idx;
590*4882a593Smuzhiyun };
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /*
593*4882a593Smuzhiyun * Struct used for unlinking a doorbell handle from an index in the
594*4882a593Smuzhiyun * notify bitmap. All fields in struct are aligned to their natural
595*4882a593Smuzhiyun * alignment.
596*4882a593Smuzhiyun */
597*4882a593Smuzhiyun struct vmci_doorbell_unlink_msg {
598*4882a593Smuzhiyun struct vmci_datagram hdr;
599*4882a593Smuzhiyun struct vmci_handle handle;
600*4882a593Smuzhiyun };
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /*
603*4882a593Smuzhiyun * Struct used for generating a notification on a doorbell handle. All
604*4882a593Smuzhiyun * fields in struct are aligned to their natural alignment.
605*4882a593Smuzhiyun */
606*4882a593Smuzhiyun struct vmci_doorbell_notify_msg {
607*4882a593Smuzhiyun struct vmci_datagram hdr;
608*4882a593Smuzhiyun struct vmci_handle handle;
609*4882a593Smuzhiyun };
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun /*
612*4882a593Smuzhiyun * This struct is used to contain data for events. Size of this struct is a
613*4882a593Smuzhiyun * multiple of 8 bytes, and all fields are aligned to their natural alignment.
614*4882a593Smuzhiyun */
615*4882a593Smuzhiyun struct vmci_event_data {
616*4882a593Smuzhiyun u32 event; /* 4 bytes. */
617*4882a593Smuzhiyun u32 _pad;
618*4882a593Smuzhiyun /* Event payload is put here. */
619*4882a593Smuzhiyun };
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun * Define the different VMCI_EVENT payload data types here. All structs must
623*4882a593Smuzhiyun * be a multiple of 8 bytes, and fields must be aligned to their natural
624*4882a593Smuzhiyun * alignment.
625*4882a593Smuzhiyun */
626*4882a593Smuzhiyun struct vmci_event_payld_ctx {
627*4882a593Smuzhiyun u32 context_id; /* 4 bytes. */
628*4882a593Smuzhiyun u32 _pad;
629*4882a593Smuzhiyun };
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun struct vmci_event_payld_qp {
632*4882a593Smuzhiyun struct vmci_handle handle; /* queue_pair handle. */
633*4882a593Smuzhiyun u32 peer_id; /* Context id of attaching/detaching VM. */
634*4882a593Smuzhiyun u32 _pad;
635*4882a593Smuzhiyun };
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun /*
638*4882a593Smuzhiyun * We define the following struct to get the size of the maximum event
639*4882a593Smuzhiyun * data the hypervisor may send to the guest. If adding a new event
640*4882a593Smuzhiyun * payload type above, add it to the following struct too (inside the
641*4882a593Smuzhiyun * union).
642*4882a593Smuzhiyun */
643*4882a593Smuzhiyun struct vmci_event_data_max {
644*4882a593Smuzhiyun struct vmci_event_data event_data;
645*4882a593Smuzhiyun union {
646*4882a593Smuzhiyun struct vmci_event_payld_ctx context_payload;
647*4882a593Smuzhiyun struct vmci_event_payld_qp qp_payload;
648*4882a593Smuzhiyun } ev_data_payload;
649*4882a593Smuzhiyun };
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun /*
652*4882a593Smuzhiyun * Struct used for VMCI_EVENT_SUBSCRIBE/UNSUBSCRIBE and
653*4882a593Smuzhiyun * VMCI_EVENT_HANDLER messages. Struct size is 32 bytes. All fields
654*4882a593Smuzhiyun * in struct are aligned to their natural alignment.
655*4882a593Smuzhiyun */
656*4882a593Smuzhiyun struct vmci_event_msg {
657*4882a593Smuzhiyun struct vmci_datagram hdr;
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun /* Has event type and payload. */
660*4882a593Smuzhiyun struct vmci_event_data event_data;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun /* Payload gets put here. */
663*4882a593Smuzhiyun };
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun /* Event with context payload. */
666*4882a593Smuzhiyun struct vmci_event_ctx {
667*4882a593Smuzhiyun struct vmci_event_msg msg;
668*4882a593Smuzhiyun struct vmci_event_payld_ctx payload;
669*4882a593Smuzhiyun };
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun /* Event with QP payload. */
672*4882a593Smuzhiyun struct vmci_event_qp {
673*4882a593Smuzhiyun struct vmci_event_msg msg;
674*4882a593Smuzhiyun struct vmci_event_payld_qp payload;
675*4882a593Smuzhiyun };
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun /*
678*4882a593Smuzhiyun * Structs used for queue_pair alloc and detach messages. We align fields of
679*4882a593Smuzhiyun * these structs to 64bit boundaries.
680*4882a593Smuzhiyun */
681*4882a593Smuzhiyun struct vmci_qp_alloc_msg {
682*4882a593Smuzhiyun struct vmci_datagram hdr;
683*4882a593Smuzhiyun struct vmci_handle handle;
684*4882a593Smuzhiyun u32 peer;
685*4882a593Smuzhiyun u32 flags;
686*4882a593Smuzhiyun u64 produce_size;
687*4882a593Smuzhiyun u64 consume_size;
688*4882a593Smuzhiyun u64 num_ppns;
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /* List of PPNs placed here. */
691*4882a593Smuzhiyun };
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun struct vmci_qp_detach_msg {
694*4882a593Smuzhiyun struct vmci_datagram hdr;
695*4882a593Smuzhiyun struct vmci_handle handle;
696*4882a593Smuzhiyun };
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun /* VMCI Doorbell API. */
699*4882a593Smuzhiyun #define VMCI_FLAG_DELAYED_CB BIT(0)
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun typedef void (*vmci_callback) (void *client_data);
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /*
704*4882a593Smuzhiyun * struct vmci_qp - A vmw_vmci queue pair handle.
705*4882a593Smuzhiyun *
706*4882a593Smuzhiyun * This structure is used as a handle to a queue pair created by
707*4882a593Smuzhiyun * VMCI. It is intentionally left opaque to clients.
708*4882a593Smuzhiyun */
709*4882a593Smuzhiyun struct vmci_qp;
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun /* Callback needed for correctly waiting on events. */
712*4882a593Smuzhiyun typedef int (*vmci_datagram_recv_cb) (void *client_data,
713*4882a593Smuzhiyun struct vmci_datagram *msg);
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun /* VMCI Event API. */
716*4882a593Smuzhiyun typedef void (*vmci_event_cb) (u32 sub_id, const struct vmci_event_data *ed,
717*4882a593Smuzhiyun void *client_data);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun /*
720*4882a593Smuzhiyun * We use the following inline function to access the payload data
721*4882a593Smuzhiyun * associated with an event data.
722*4882a593Smuzhiyun */
723*4882a593Smuzhiyun static inline const void *
vmci_event_data_const_payload(const struct vmci_event_data * ev_data)724*4882a593Smuzhiyun vmci_event_data_const_payload(const struct vmci_event_data *ev_data)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun return (const char *)ev_data + sizeof(*ev_data);
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun
vmci_event_data_payload(struct vmci_event_data * ev_data)729*4882a593Smuzhiyun static inline void *vmci_event_data_payload(struct vmci_event_data *ev_data)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun return (void *)vmci_event_data_const_payload(ev_data);
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun /*
735*4882a593Smuzhiyun * Helper to read a value from a head or tail pointer. For X86_32, the
736*4882a593Smuzhiyun * pointer is treated as a 32bit value, since the pointer value
737*4882a593Smuzhiyun * never exceeds a 32bit value in this case. Also, doing an
738*4882a593Smuzhiyun * atomic64_read on X86_32 uniprocessor systems may be implemented
739*4882a593Smuzhiyun * as a non locked cmpxchg8b, that may end up overwriting updates done
740*4882a593Smuzhiyun * by the VMCI device to the memory location. On 32bit SMP, the lock
741*4882a593Smuzhiyun * prefix will be used, so correctness isn't an issue, but using a
742*4882a593Smuzhiyun * 64bit operation still adds unnecessary overhead.
743*4882a593Smuzhiyun */
vmci_q_read_pointer(u64 * var)744*4882a593Smuzhiyun static inline u64 vmci_q_read_pointer(u64 *var)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun return READ_ONCE(*(unsigned long *)var);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /*
750*4882a593Smuzhiyun * Helper to set the value of a head or tail pointer. For X86_32, the
751*4882a593Smuzhiyun * pointer is treated as a 32bit value, since the pointer value
752*4882a593Smuzhiyun * never exceeds a 32bit value in this case. On 32bit SMP, using a
753*4882a593Smuzhiyun * locked cmpxchg8b adds unnecessary overhead.
754*4882a593Smuzhiyun */
vmci_q_set_pointer(u64 * var,u64 new_val)755*4882a593Smuzhiyun static inline void vmci_q_set_pointer(u64 *var, u64 new_val)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun /* XXX buggered on big-endian */
758*4882a593Smuzhiyun WRITE_ONCE(*(unsigned long *)var, (unsigned long)new_val);
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun /*
762*4882a593Smuzhiyun * Helper to add a given offset to a head or tail pointer. Wraps the
763*4882a593Smuzhiyun * value of the pointer around the max size of the queue.
764*4882a593Smuzhiyun */
vmci_qp_add_pointer(u64 * var,size_t add,u64 size)765*4882a593Smuzhiyun static inline void vmci_qp_add_pointer(u64 *var, size_t add, u64 size)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun u64 new_val = vmci_q_read_pointer(var);
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun if (new_val >= size - add)
770*4882a593Smuzhiyun new_val -= size;
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun new_val += add;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun vmci_q_set_pointer(var, new_val);
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun /*
778*4882a593Smuzhiyun * Helper routine to get the Producer Tail from the supplied queue.
779*4882a593Smuzhiyun */
780*4882a593Smuzhiyun static inline u64
vmci_q_header_producer_tail(const struct vmci_queue_header * q_header)781*4882a593Smuzhiyun vmci_q_header_producer_tail(const struct vmci_queue_header *q_header)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun struct vmci_queue_header *qh = (struct vmci_queue_header *)q_header;
784*4882a593Smuzhiyun return vmci_q_read_pointer(&qh->producer_tail);
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun /*
788*4882a593Smuzhiyun * Helper routine to get the Consumer Head from the supplied queue.
789*4882a593Smuzhiyun */
790*4882a593Smuzhiyun static inline u64
vmci_q_header_consumer_head(const struct vmci_queue_header * q_header)791*4882a593Smuzhiyun vmci_q_header_consumer_head(const struct vmci_queue_header *q_header)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun struct vmci_queue_header *qh = (struct vmci_queue_header *)q_header;
794*4882a593Smuzhiyun return vmci_q_read_pointer(&qh->consumer_head);
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun /*
798*4882a593Smuzhiyun * Helper routine to increment the Producer Tail. Fundamentally,
799*4882a593Smuzhiyun * vmci_qp_add_pointer() is used to manipulate the tail itself.
800*4882a593Smuzhiyun */
801*4882a593Smuzhiyun static inline void
vmci_q_header_add_producer_tail(struct vmci_queue_header * q_header,size_t add,u64 queue_size)802*4882a593Smuzhiyun vmci_q_header_add_producer_tail(struct vmci_queue_header *q_header,
803*4882a593Smuzhiyun size_t add,
804*4882a593Smuzhiyun u64 queue_size)
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun vmci_qp_add_pointer(&q_header->producer_tail, add, queue_size);
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun /*
810*4882a593Smuzhiyun * Helper routine to increment the Consumer Head. Fundamentally,
811*4882a593Smuzhiyun * vmci_qp_add_pointer() is used to manipulate the head itself.
812*4882a593Smuzhiyun */
813*4882a593Smuzhiyun static inline void
vmci_q_header_add_consumer_head(struct vmci_queue_header * q_header,size_t add,u64 queue_size)814*4882a593Smuzhiyun vmci_q_header_add_consumer_head(struct vmci_queue_header *q_header,
815*4882a593Smuzhiyun size_t add,
816*4882a593Smuzhiyun u64 queue_size)
817*4882a593Smuzhiyun {
818*4882a593Smuzhiyun vmci_qp_add_pointer(&q_header->consumer_head, add, queue_size);
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun /*
822*4882a593Smuzhiyun * Helper routine for getting the head and the tail pointer for a queue.
823*4882a593Smuzhiyun * Both the VMCIQueues are needed to get both the pointers for one queue.
824*4882a593Smuzhiyun */
825*4882a593Smuzhiyun static inline void
vmci_q_header_get_pointers(const struct vmci_queue_header * produce_q_header,const struct vmci_queue_header * consume_q_header,u64 * producer_tail,u64 * consumer_head)826*4882a593Smuzhiyun vmci_q_header_get_pointers(const struct vmci_queue_header *produce_q_header,
827*4882a593Smuzhiyun const struct vmci_queue_header *consume_q_header,
828*4882a593Smuzhiyun u64 *producer_tail,
829*4882a593Smuzhiyun u64 *consumer_head)
830*4882a593Smuzhiyun {
831*4882a593Smuzhiyun if (producer_tail)
832*4882a593Smuzhiyun *producer_tail = vmci_q_header_producer_tail(produce_q_header);
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun if (consumer_head)
835*4882a593Smuzhiyun *consumer_head = vmci_q_header_consumer_head(consume_q_header);
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun
vmci_q_header_init(struct vmci_queue_header * q_header,const struct vmci_handle handle)838*4882a593Smuzhiyun static inline void vmci_q_header_init(struct vmci_queue_header *q_header,
839*4882a593Smuzhiyun const struct vmci_handle handle)
840*4882a593Smuzhiyun {
841*4882a593Smuzhiyun q_header->handle = handle;
842*4882a593Smuzhiyun q_header->producer_tail = 0;
843*4882a593Smuzhiyun q_header->consumer_head = 0;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun /*
847*4882a593Smuzhiyun * Finds available free space in a produce queue to enqueue more
848*4882a593Smuzhiyun * data or reports an error if queue pair corruption is detected.
849*4882a593Smuzhiyun */
850*4882a593Smuzhiyun static s64
vmci_q_header_free_space(const struct vmci_queue_header * produce_q_header,const struct vmci_queue_header * consume_q_header,const u64 produce_q_size)851*4882a593Smuzhiyun vmci_q_header_free_space(const struct vmci_queue_header *produce_q_header,
852*4882a593Smuzhiyun const struct vmci_queue_header *consume_q_header,
853*4882a593Smuzhiyun const u64 produce_q_size)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun u64 tail;
856*4882a593Smuzhiyun u64 head;
857*4882a593Smuzhiyun u64 free_space;
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun tail = vmci_q_header_producer_tail(produce_q_header);
860*4882a593Smuzhiyun head = vmci_q_header_consumer_head(consume_q_header);
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun if (tail >= produce_q_size || head >= produce_q_size)
863*4882a593Smuzhiyun return VMCI_ERROR_INVALID_SIZE;
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /*
866*4882a593Smuzhiyun * Deduct 1 to avoid tail becoming equal to head which causes
867*4882a593Smuzhiyun * ambiguity. If head and tail are equal it means that the
868*4882a593Smuzhiyun * queue is empty.
869*4882a593Smuzhiyun */
870*4882a593Smuzhiyun if (tail >= head)
871*4882a593Smuzhiyun free_space = produce_q_size - (tail - head) - 1;
872*4882a593Smuzhiyun else
873*4882a593Smuzhiyun free_space = head - tail - 1;
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun return free_space;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun /*
879*4882a593Smuzhiyun * vmci_q_header_free_space() does all the heavy lifting of
880*4882a593Smuzhiyun * determing the number of free bytes in a Queue. This routine,
881*4882a593Smuzhiyun * then subtracts that size from the full size of the Queue so
882*4882a593Smuzhiyun * the caller knows how many bytes are ready to be dequeued.
883*4882a593Smuzhiyun * Results:
884*4882a593Smuzhiyun * On success, available data size in bytes (up to MAX_INT64).
885*4882a593Smuzhiyun * On failure, appropriate error code.
886*4882a593Smuzhiyun */
887*4882a593Smuzhiyun static inline s64
vmci_q_header_buf_ready(const struct vmci_queue_header * consume_q_header,const struct vmci_queue_header * produce_q_header,const u64 consume_q_size)888*4882a593Smuzhiyun vmci_q_header_buf_ready(const struct vmci_queue_header *consume_q_header,
889*4882a593Smuzhiyun const struct vmci_queue_header *produce_q_header,
890*4882a593Smuzhiyun const u64 consume_q_size)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun s64 free_space;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun free_space = vmci_q_header_free_space(consume_q_header,
895*4882a593Smuzhiyun produce_q_header, consume_q_size);
896*4882a593Smuzhiyun if (free_space < VMCI_SUCCESS)
897*4882a593Smuzhiyun return free_space;
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun return consume_q_size - free_space - 1;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun #endif /* _VMW_VMCI_DEF_H_ */
904