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