Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * VMware VMCI Driver
0004  *
0005  * Copyright (C) 2012 VMware, Inc. All rights reserved.
0006  */
0007 
0008 #ifndef _VMW_VMCI_DEF_H_
0009 #define _VMW_VMCI_DEF_H_
0010 
0011 #include <linux/atomic.h>
0012 #include <linux/bits.h>
0013 
0014 /* Register offsets. */
0015 #define VMCI_STATUS_ADDR        0x00
0016 #define VMCI_CONTROL_ADDR       0x04
0017 #define VMCI_ICR_ADDR           0x08
0018 #define VMCI_IMR_ADDR           0x0c
0019 #define VMCI_DATA_OUT_ADDR      0x10
0020 #define VMCI_DATA_IN_ADDR       0x14
0021 #define VMCI_CAPS_ADDR          0x18
0022 #define VMCI_RESULT_LOW_ADDR    0x1c
0023 #define VMCI_RESULT_HIGH_ADDR   0x20
0024 #define VMCI_DATA_OUT_LOW_ADDR  0x24
0025 #define VMCI_DATA_OUT_HIGH_ADDR 0x28
0026 #define VMCI_DATA_IN_LOW_ADDR   0x2c
0027 #define VMCI_DATA_IN_HIGH_ADDR  0x30
0028 #define VMCI_GUEST_PAGE_SHIFT   0x34
0029 
0030 /* Max number of devices. */
0031 #define VMCI_MAX_DEVICES 1
0032 
0033 /* Status register bits. */
0034 #define VMCI_STATUS_INT_ON     BIT(0)
0035 
0036 /* Control register bits. */
0037 #define VMCI_CONTROL_RESET        BIT(0)
0038 #define VMCI_CONTROL_INT_ENABLE   BIT(1)
0039 #define VMCI_CONTROL_INT_DISABLE  BIT(2)
0040 
0041 /* Capabilities register bits. */
0042 #define VMCI_CAPS_HYPERCALL     BIT(0)
0043 #define VMCI_CAPS_GUESTCALL     BIT(1)
0044 #define VMCI_CAPS_DATAGRAM      BIT(2)
0045 #define VMCI_CAPS_NOTIFICATIONS BIT(3)
0046 #define VMCI_CAPS_PPN64         BIT(4)
0047 #define VMCI_CAPS_DMA_DATAGRAM  BIT(5)
0048 
0049 /* Interrupt Cause register bits. */
0050 #define VMCI_ICR_DATAGRAM      BIT(0)
0051 #define VMCI_ICR_NOTIFICATION  BIT(1)
0052 #define VMCI_ICR_DMA_DATAGRAM  BIT(2)
0053 
0054 /* Interrupt Mask register bits. */
0055 #define VMCI_IMR_DATAGRAM      BIT(0)
0056 #define VMCI_IMR_NOTIFICATION  BIT(1)
0057 #define VMCI_IMR_DMA_DATAGRAM  BIT(2)
0058 
0059 /*
0060  * Maximum MSI/MSI-X interrupt vectors in the device.
0061  * If VMCI_CAPS_DMA_DATAGRAM is supported by the device,
0062  * VMCI_MAX_INTRS_DMA_DATAGRAM vectors are available,
0063  * otherwise only VMCI_MAX_INTRS_NOTIFICATION.
0064  */
0065 #define VMCI_MAX_INTRS_NOTIFICATION 2
0066 #define VMCI_MAX_INTRS_DMA_DATAGRAM 3
0067 #define VMCI_MAX_INTRS              VMCI_MAX_INTRS_DMA_DATAGRAM
0068 
0069 /*
0070  * Supported interrupt vectors.  There is one for each ICR value above,
0071  * but here they indicate the position in the vector array/message ID.
0072  */
0073 enum {
0074     VMCI_INTR_DATAGRAM = 0,
0075     VMCI_INTR_NOTIFICATION = 1,
0076     VMCI_INTR_DMA_DATAGRAM = 2,
0077 };
0078 
0079 /*
0080  * A single VMCI device has an upper limit of 128MB on the amount of
0081  * memory that can be used for queue pairs. Since each queue pair
0082  * consists of at least two pages, the memory limit also dictates the
0083  * number of queue pairs a guest can create.
0084  */
0085 #define VMCI_MAX_GUEST_QP_MEMORY ((size_t)(128 * 1024 * 1024))
0086 #define VMCI_MAX_GUEST_QP_COUNT  (VMCI_MAX_GUEST_QP_MEMORY / PAGE_SIZE / 2)
0087 
0088 /*
0089  * There can be at most PAGE_SIZE doorbells since there is one doorbell
0090  * per byte in the doorbell bitmap page.
0091  */
0092 #define VMCI_MAX_GUEST_DOORBELL_COUNT PAGE_SIZE
0093 
0094 /*
0095  * Queues with pre-mapped data pages must be small, so that we don't pin
0096  * too much kernel memory (especially on vmkernel).  We limit a queuepair to
0097  * 32 KB, or 16 KB per queue for symmetrical pairs.
0098  */
0099 #define VMCI_MAX_PINNED_QP_MEMORY ((size_t)(32 * 1024))
0100 
0101 /*
0102  * The version of the VMCI device that supports MMIO access to registers
0103  * requests 256KB for BAR1 whereas the version of VMCI that supports
0104  * MSI/MSI-X only requests 8KB. The layout of the larger 256KB region is:
0105  * - the first 128KB are used for MSI/MSI-X.
0106  * - the following 64KB are used for MMIO register access.
0107  * - the remaining 64KB are unused.
0108  */
0109 #define VMCI_WITH_MMIO_ACCESS_BAR_SIZE ((size_t)(256 * 1024))
0110 #define VMCI_MMIO_ACCESS_OFFSET        ((size_t)(128 * 1024))
0111 #define VMCI_MMIO_ACCESS_SIZE          ((size_t)(64 * 1024))
0112 
0113 /*
0114  * For VMCI devices supporting the VMCI_CAPS_DMA_DATAGRAM capability, the
0115  * sending and receiving of datagrams can be performed using DMA to/from
0116  * a driver allocated buffer.
0117  * Sending and receiving will be handled as follows:
0118  * - when sending datagrams, the driver initializes the buffer where the
0119  *   data part will refer to the outgoing VMCI datagram, sets the busy flag
0120  *   to 1 and writes the address of the buffer to VMCI_DATA_OUT_HIGH_ADDR
0121  *   and VMCI_DATA_OUT_LOW_ADDR. Writing to VMCI_DATA_OUT_LOW_ADDR triggers
0122  *   the device processing of the buffer. When the device has processed the
0123  *   buffer, it will write the result value to the buffer and then clear the
0124  *   busy flag.
0125  * - when receiving datagrams, the driver initializes the buffer where the
0126  *   data part will describe the receive buffer, clears the busy flag and
0127  *   writes the address of the buffer to VMCI_DATA_IN_HIGH_ADDR and
0128  *   VMCI_DATA_IN_LOW_ADDR. Writing to VMCI_DATA_IN_LOW_ADDR triggers the
0129  *   device processing of the buffer. The device will copy as many available
0130  *   datagrams into the buffer as possible, and then sets the busy flag.
0131  *   When the busy flag is set, the driver will process the datagrams in the
0132  *   buffer.
0133  */
0134 struct vmci_data_in_out_header {
0135     uint32_t busy;
0136     uint32_t opcode;
0137     uint32_t size;
0138     uint32_t rsvd;
0139     uint64_t result;
0140 };
0141 
0142 struct vmci_sg_elem {
0143     uint64_t addr;
0144     uint64_t size;
0145 };
0146 
0147 /*
0148  * We have a fixed set of resource IDs available in the VMX.
0149  * This allows us to have a very simple implementation since we statically
0150  * know how many will create datagram handles. If a new caller arrives and
0151  * we have run out of slots we can manually increment the maximum size of
0152  * available resource IDs.
0153  *
0154  * VMCI reserved hypervisor datagram resource IDs.
0155  */
0156 enum {
0157     VMCI_RESOURCES_QUERY = 0,
0158     VMCI_GET_CONTEXT_ID = 1,
0159     VMCI_SET_NOTIFY_BITMAP = 2,
0160     VMCI_DOORBELL_LINK = 3,
0161     VMCI_DOORBELL_UNLINK = 4,
0162     VMCI_DOORBELL_NOTIFY = 5,
0163     /*
0164      * VMCI_DATAGRAM_REQUEST_MAP and VMCI_DATAGRAM_REMOVE_MAP are
0165      * obsoleted by the removal of VM to VM communication.
0166      */
0167     VMCI_DATAGRAM_REQUEST_MAP = 6,
0168     VMCI_DATAGRAM_REMOVE_MAP = 7,
0169     VMCI_EVENT_SUBSCRIBE = 8,
0170     VMCI_EVENT_UNSUBSCRIBE = 9,
0171     VMCI_QUEUEPAIR_ALLOC = 10,
0172     VMCI_QUEUEPAIR_DETACH = 11,
0173 
0174     /*
0175      * VMCI_VSOCK_VMX_LOOKUP was assigned to 12 for Fusion 3.0/3.1,
0176      * WS 7.0/7.1 and ESX 4.1
0177      */
0178     VMCI_HGFS_TRANSPORT = 13,
0179     VMCI_UNITY_PBRPC_REGISTER = 14,
0180     VMCI_RPC_PRIVILEGED = 15,
0181     VMCI_RPC_UNPRIVILEGED = 16,
0182     VMCI_RESOURCE_MAX = 17,
0183 };
0184 
0185 /*
0186  * struct vmci_handle - Ownership information structure
0187  * @context:    The VMX context ID.
0188  * @resource:   The resource ID (used for locating in resource hash).
0189  *
0190  * The vmci_handle structure is used to track resources used within
0191  * vmw_vmci.
0192  */
0193 struct vmci_handle {
0194     u32 context;
0195     u32 resource;
0196 };
0197 
0198 #define vmci_make_handle(_cid, _rid) \
0199     (struct vmci_handle){ .context = _cid, .resource = _rid }
0200 
0201 static inline bool vmci_handle_is_equal(struct vmci_handle h1,
0202                     struct vmci_handle h2)
0203 {
0204     return h1.context == h2.context && h1.resource == h2.resource;
0205 }
0206 
0207 #define VMCI_INVALID_ID ~0
0208 static const struct vmci_handle VMCI_INVALID_HANDLE = {
0209     .context = VMCI_INVALID_ID,
0210     .resource = VMCI_INVALID_ID
0211 };
0212 
0213 static inline bool vmci_handle_is_invalid(struct vmci_handle h)
0214 {
0215     return vmci_handle_is_equal(h, VMCI_INVALID_HANDLE);
0216 }
0217 
0218 /*
0219  * The below defines can be used to send anonymous requests.
0220  * This also indicates that no response is expected.
0221  */
0222 #define VMCI_ANON_SRC_CONTEXT_ID   VMCI_INVALID_ID
0223 #define VMCI_ANON_SRC_RESOURCE_ID  VMCI_INVALID_ID
0224 static const struct vmci_handle __maybe_unused VMCI_ANON_SRC_HANDLE = {
0225     .context = VMCI_ANON_SRC_CONTEXT_ID,
0226     .resource = VMCI_ANON_SRC_RESOURCE_ID
0227 };
0228 
0229 /* The lowest 16 context ids are reserved for internal use. */
0230 #define VMCI_RESERVED_CID_LIMIT ((u32) 16)
0231 
0232 /*
0233  * Hypervisor context id, used for calling into hypervisor
0234  * supplied services from the VM.
0235  */
0236 #define VMCI_HYPERVISOR_CONTEXT_ID 0
0237 
0238 /*
0239  * Well-known context id, a logical context that contains a set of
0240  * well-known services. This context ID is now obsolete.
0241  */
0242 #define VMCI_WELL_KNOWN_CONTEXT_ID 1
0243 
0244 /*
0245  * Context ID used by host endpoints.
0246  */
0247 #define VMCI_HOST_CONTEXT_ID  2
0248 
0249 #define VMCI_CONTEXT_IS_VM(_cid) (VMCI_INVALID_ID != (_cid) &&      \
0250                   (_cid) > VMCI_HOST_CONTEXT_ID)
0251 
0252 /*
0253  * The VMCI_CONTEXT_RESOURCE_ID is used together with vmci_make_handle to make
0254  * handles that refer to a specific context.
0255  */
0256 #define VMCI_CONTEXT_RESOURCE_ID 0
0257 
0258 /*
0259  * VMCI error codes.
0260  */
0261 enum {
0262     VMCI_SUCCESS_QUEUEPAIR_ATTACH   = 5,
0263     VMCI_SUCCESS_QUEUEPAIR_CREATE   = 4,
0264     VMCI_SUCCESS_LAST_DETACH    = 3,
0265     VMCI_SUCCESS_ACCESS_GRANTED = 2,
0266     VMCI_SUCCESS_ENTRY_DEAD     = 1,
0267     VMCI_SUCCESS             = 0,
0268     VMCI_ERROR_INVALID_RESOURCE  = (-1),
0269     VMCI_ERROR_INVALID_ARGS      = (-2),
0270     VMCI_ERROR_NO_MEM        = (-3),
0271     VMCI_ERROR_DATAGRAM_FAILED   = (-4),
0272     VMCI_ERROR_MORE_DATA         = (-5),
0273     VMCI_ERROR_NO_MORE_DATAGRAMS     = (-6),
0274     VMCI_ERROR_NO_ACCESS         = (-7),
0275     VMCI_ERROR_NO_HANDLE         = (-8),
0276     VMCI_ERROR_DUPLICATE_ENTRY   = (-9),
0277     VMCI_ERROR_DST_UNREACHABLE   = (-10),
0278     VMCI_ERROR_PAYLOAD_TOO_LARGE     = (-11),
0279     VMCI_ERROR_INVALID_PRIV      = (-12),
0280     VMCI_ERROR_GENERIC       = (-13),
0281     VMCI_ERROR_PAGE_ALREADY_SHARED   = (-14),
0282     VMCI_ERROR_CANNOT_SHARE_PAGE     = (-15),
0283     VMCI_ERROR_CANNOT_UNSHARE_PAGE   = (-16),
0284     VMCI_ERROR_NO_PROCESS        = (-17),
0285     VMCI_ERROR_NO_DATAGRAM       = (-18),
0286     VMCI_ERROR_NO_RESOURCES      = (-19),
0287     VMCI_ERROR_UNAVAILABLE       = (-20),
0288     VMCI_ERROR_NOT_FOUND         = (-21),
0289     VMCI_ERROR_ALREADY_EXISTS    = (-22),
0290     VMCI_ERROR_NOT_PAGE_ALIGNED  = (-23),
0291     VMCI_ERROR_INVALID_SIZE      = (-24),
0292     VMCI_ERROR_REGION_ALREADY_SHARED = (-25),
0293     VMCI_ERROR_TIMEOUT       = (-26),
0294     VMCI_ERROR_DATAGRAM_INCOMPLETE   = (-27),
0295     VMCI_ERROR_INCORRECT_IRQL    = (-28),
0296     VMCI_ERROR_EVENT_UNKNOWN     = (-29),
0297     VMCI_ERROR_OBSOLETE      = (-30),
0298     VMCI_ERROR_QUEUEPAIR_MISMATCH    = (-31),
0299     VMCI_ERROR_QUEUEPAIR_NOTSET  = (-32),
0300     VMCI_ERROR_QUEUEPAIR_NOTOWNER    = (-33),
0301     VMCI_ERROR_QUEUEPAIR_NOTATTACHED = (-34),
0302     VMCI_ERROR_QUEUEPAIR_NOSPACE     = (-35),
0303     VMCI_ERROR_QUEUEPAIR_NODATA  = (-36),
0304     VMCI_ERROR_BUSMEM_INVALIDATION   = (-37),
0305     VMCI_ERROR_MODULE_NOT_LOADED     = (-38),
0306     VMCI_ERROR_DEVICE_NOT_FOUND  = (-39),
0307     VMCI_ERROR_QUEUEPAIR_NOT_READY   = (-40),
0308     VMCI_ERROR_WOULD_BLOCK       = (-41),
0309 
0310     /* VMCI clients should return error code within this range */
0311     VMCI_ERROR_CLIENT_MIN        = (-500),
0312     VMCI_ERROR_CLIENT_MAX        = (-550),
0313 
0314     /* Internal error codes. */
0315     VMCI_SHAREDMEM_ERROR_BAD_CONTEXT = (-1000),
0316 };
0317 
0318 /* VMCI reserved events. */
0319 enum {
0320     /* Only applicable to guest endpoints */
0321     VMCI_EVENT_CTX_ID_UPDATE  = 0,
0322 
0323     /* Applicable to guest and host */
0324     VMCI_EVENT_CTX_REMOVED    = 1,
0325 
0326     /* Only applicable to guest endpoints */
0327     VMCI_EVENT_QP_RESUMED     = 2,
0328 
0329     /* Applicable to guest and host */
0330     VMCI_EVENT_QP_PEER_ATTACH = 3,
0331 
0332     /* Applicable to guest and host */
0333     VMCI_EVENT_QP_PEER_DETACH = 4,
0334 
0335     /*
0336      * Applicable to VMX and vmk.  On vmk,
0337      * this event has the Context payload type.
0338      */
0339     VMCI_EVENT_MEM_ACCESS_ON  = 5,
0340 
0341     /*
0342      * Applicable to VMX and vmk.  Same as
0343      * above for the payload type.
0344      */
0345     VMCI_EVENT_MEM_ACCESS_OFF = 6,
0346     VMCI_EVENT_MAX        = 7,
0347 };
0348 
0349 /*
0350  * Of the above events, a few are reserved for use in the VMX, and
0351  * other endpoints (guest and host kernel) should not use them. For
0352  * the rest of the events, we allow both host and guest endpoints to
0353  * subscribe to them, to maintain the same API for host and guest
0354  * endpoints.
0355  */
0356 #define VMCI_EVENT_VALID_VMX(_event) ((_event) == VMCI_EVENT_MEM_ACCESS_ON || \
0357                       (_event) == VMCI_EVENT_MEM_ACCESS_OFF)
0358 
0359 #define VMCI_EVENT_VALID(_event) ((_event) < VMCI_EVENT_MAX &&      \
0360                   !VMCI_EVENT_VALID_VMX(_event))
0361 
0362 /* Reserved guest datagram resource ids. */
0363 #define VMCI_EVENT_HANDLER 0
0364 
0365 /*
0366  * VMCI coarse-grained privileges (per context or host
0367  * process/endpoint. An entity with the restricted flag is only
0368  * allowed to interact with the hypervisor and trusted entities.
0369  */
0370 enum {
0371     VMCI_NO_PRIVILEGE_FLAGS = 0,
0372     VMCI_PRIVILEGE_FLAG_RESTRICTED = 1,
0373     VMCI_PRIVILEGE_FLAG_TRUSTED = 2,
0374     VMCI_PRIVILEGE_ALL_FLAGS = (VMCI_PRIVILEGE_FLAG_RESTRICTED |
0375                     VMCI_PRIVILEGE_FLAG_TRUSTED),
0376     VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS = VMCI_NO_PRIVILEGE_FLAGS,
0377     VMCI_LEAST_PRIVILEGE_FLAGS = VMCI_PRIVILEGE_FLAG_RESTRICTED,
0378     VMCI_MAX_PRIVILEGE_FLAGS = VMCI_PRIVILEGE_FLAG_TRUSTED,
0379 };
0380 
0381 /* 0 through VMCI_RESERVED_RESOURCE_ID_MAX are reserved. */
0382 #define VMCI_RESERVED_RESOURCE_ID_MAX 1023
0383 
0384 /*
0385  * Driver version.
0386  *
0387  * Increment major version when you make an incompatible change.
0388  * Compatibility goes both ways (old driver with new executable
0389  * as well as new driver with old executable).
0390  */
0391 
0392 /* Never change VMCI_VERSION_SHIFT_WIDTH */
0393 #define VMCI_VERSION_SHIFT_WIDTH 16
0394 #define VMCI_MAKE_VERSION(_major, _minor)           \
0395     ((_major) << VMCI_VERSION_SHIFT_WIDTH | (u16) (_minor))
0396 
0397 #define VMCI_VERSION_MAJOR(v)  ((u32) (v) >> VMCI_VERSION_SHIFT_WIDTH)
0398 #define VMCI_VERSION_MINOR(v)  ((u16) (v))
0399 
0400 /*
0401  * VMCI_VERSION is always the current version.  Subsequently listed
0402  * versions are ways of detecting previous versions of the connecting
0403  * application (i.e., VMX).
0404  *
0405  * VMCI_VERSION_NOVMVM: This version removed support for VM to VM
0406  * communication.
0407  *
0408  * VMCI_VERSION_NOTIFY: This version introduced doorbell notification
0409  * support.
0410  *
0411  * VMCI_VERSION_HOSTQP: This version introduced host end point support
0412  * for hosted products.
0413  *
0414  * VMCI_VERSION_PREHOSTQP: This is the version prior to the adoption of
0415  * support for host end-points.
0416  *
0417  * VMCI_VERSION_PREVERS2: This fictional version number is intended to
0418  * represent the version of a VMX which doesn't call into the driver
0419  * with ioctl VERSION2 and thus doesn't establish its version with the
0420  * driver.
0421  */
0422 
0423 #define VMCI_VERSION                VMCI_VERSION_NOVMVM
0424 #define VMCI_VERSION_NOVMVM         VMCI_MAKE_VERSION(11, 0)
0425 #define VMCI_VERSION_NOTIFY         VMCI_MAKE_VERSION(10, 0)
0426 #define VMCI_VERSION_HOSTQP         VMCI_MAKE_VERSION(9, 0)
0427 #define VMCI_VERSION_PREHOSTQP      VMCI_MAKE_VERSION(8, 0)
0428 #define VMCI_VERSION_PREVERS2       VMCI_MAKE_VERSION(1, 0)
0429 
0430 #define VMCI_SOCKETS_MAKE_VERSION(_p)                   \
0431     ((((_p)[0] & 0xFF) << 24) | (((_p)[1] & 0xFF) << 16) | ((_p)[2]))
0432 
0433 /*
0434  * The VMCI IOCTLs.  We use identity code 7, as noted in ioctl-number.h, and
0435  * we start at sequence 9f.  This gives us the same values that our shipping
0436  * products use, starting at 1951, provided we leave out the direction and
0437  * structure size.  Note that VMMon occupies the block following us, starting
0438  * at 2001.
0439  */
0440 #define IOCTL_VMCI_VERSION          _IO(7, 0x9f)    /* 1951 */
0441 #define IOCTL_VMCI_INIT_CONTEXT         _IO(7, 0xa0)
0442 #define IOCTL_VMCI_QUEUEPAIR_SETVA      _IO(7, 0xa4)
0443 #define IOCTL_VMCI_NOTIFY_RESOURCE      _IO(7, 0xa5)
0444 #define IOCTL_VMCI_NOTIFICATIONS_RECEIVE    _IO(7, 0xa6)
0445 #define IOCTL_VMCI_VERSION2         _IO(7, 0xa7)
0446 #define IOCTL_VMCI_QUEUEPAIR_ALLOC      _IO(7, 0xa8)
0447 #define IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE    _IO(7, 0xa9)
0448 #define IOCTL_VMCI_QUEUEPAIR_DETACH     _IO(7, 0xaa)
0449 #define IOCTL_VMCI_DATAGRAM_SEND        _IO(7, 0xab)
0450 #define IOCTL_VMCI_DATAGRAM_RECEIVE     _IO(7, 0xac)
0451 #define IOCTL_VMCI_CTX_ADD_NOTIFICATION     _IO(7, 0xaf)
0452 #define IOCTL_VMCI_CTX_REMOVE_NOTIFICATION  _IO(7, 0xb0)
0453 #define IOCTL_VMCI_CTX_GET_CPT_STATE        _IO(7, 0xb1)
0454 #define IOCTL_VMCI_CTX_SET_CPT_STATE        _IO(7, 0xb2)
0455 #define IOCTL_VMCI_GET_CONTEXT_ID       _IO(7, 0xb3)
0456 #define IOCTL_VMCI_SOCKETS_VERSION      _IO(7, 0xb4)
0457 #define IOCTL_VMCI_SOCKETS_GET_AF_VALUE     _IO(7, 0xb8)
0458 #define IOCTL_VMCI_SOCKETS_GET_LOCAL_CID    _IO(7, 0xb9)
0459 #define IOCTL_VMCI_SET_NOTIFY           _IO(7, 0xcb)    /* 1995 */
0460 /*IOCTL_VMMON_START             _IO(7, 0xd1)*/  /* 2001 */
0461 
0462 /*
0463  * struct vmci_queue_header - VMCI Queue Header information.
0464  *
0465  * A Queue cannot stand by itself as designed.  Each Queue's header
0466  * contains a pointer into itself (the producer_tail) and into its peer
0467  * (consumer_head).  The reason for the separation is one of
0468  * accessibility: Each end-point can modify two things: where the next
0469  * location to enqueue is within its produce_q (producer_tail); and
0470  * where the next dequeue location is in its consume_q (consumer_head).
0471  *
0472  * An end-point cannot modify the pointers of its peer (guest to
0473  * guest; NOTE that in the host both queue headers are mapped r/w).
0474  * But, each end-point needs read access to both Queue header
0475  * structures in order to determine how much space is used (or left)
0476  * in the Queue.  This is because for an end-point to know how full
0477  * its produce_q is, it needs to use the consumer_head that points into
0478  * the produce_q but -that- consumer_head is in the Queue header for
0479  * that end-points consume_q.
0480  *
0481  * Thoroughly confused?  Sorry.
0482  *
0483  * producer_tail: the point to enqueue new entrants.  When you approach
0484  * a line in a store, for example, you walk up to the tail.
0485  *
0486  * consumer_head: the point in the queue from which the next element is
0487  * dequeued.  In other words, who is next in line is he who is at the
0488  * head of the line.
0489  *
0490  * Also, producer_tail points to an empty byte in the Queue, whereas
0491  * consumer_head points to a valid byte of data (unless producer_tail ==
0492  * consumer_head in which case consumer_head does not point to a valid
0493  * byte of data).
0494  *
0495  * For a queue of buffer 'size' bytes, the tail and head pointers will be in
0496  * the range [0, size-1].
0497  *
0498  * If produce_q_header->producer_tail == consume_q_header->consumer_head
0499  * then the produce_q is empty.
0500  */
0501 struct vmci_queue_header {
0502     /* All fields are 64bit and aligned. */
0503     struct vmci_handle handle;  /* Identifier. */
0504     u64 producer_tail;  /* Offset in this queue. */
0505     u64 consumer_head;  /* Offset in peer queue. */
0506 };
0507 
0508 /*
0509  * struct vmci_datagram - Base struct for vmci datagrams.
0510  * @dst:        A vmci_handle that tracks the destination of the datagram.
0511  * @src:        A vmci_handle that tracks the source of the datagram.
0512  * @payload_size:       The size of the payload.
0513  *
0514  * vmci_datagram structs are used when sending vmci datagrams.  They include
0515  * the necessary source and destination information to properly route
0516  * the information along with the size of the package.
0517  */
0518 struct vmci_datagram {
0519     struct vmci_handle dst;
0520     struct vmci_handle src;
0521     u64 payload_size;
0522 };
0523 
0524 /*
0525  * Second flag is for creating a well-known handle instead of a per context
0526  * handle.  Next flag is for deferring datagram delivery, so that the
0527  * datagram callback is invoked in a delayed context (not interrupt context).
0528  */
0529 #define VMCI_FLAG_DG_NONE          0
0530 #define VMCI_FLAG_WELLKNOWN_DG_HND BIT(0)
0531 #define VMCI_FLAG_ANYCID_DG_HND    BIT(1)
0532 #define VMCI_FLAG_DG_DELAYED_CB    BIT(2)
0533 
0534 /*
0535  * Maximum supported size of a VMCI datagram for routable datagrams.
0536  * Datagrams going to the hypervisor are allowed to be larger.
0537  */
0538 #define VMCI_MAX_DG_SIZE (17 * 4096)
0539 #define VMCI_MAX_DG_PAYLOAD_SIZE (VMCI_MAX_DG_SIZE - \
0540                   sizeof(struct vmci_datagram))
0541 #define VMCI_DG_PAYLOAD(_dg) (void *)((char *)(_dg) +           \
0542                       sizeof(struct vmci_datagram))
0543 #define VMCI_DG_HEADERSIZE sizeof(struct vmci_datagram)
0544 #define VMCI_DG_SIZE(_dg) (VMCI_DG_HEADERSIZE + (size_t)(_dg)->payload_size)
0545 #define VMCI_DG_SIZE_ALIGNED(_dg) ((VMCI_DG_SIZE(_dg) + 7) & (~((size_t) 0x7)))
0546 #define VMCI_MAX_DATAGRAM_QUEUE_SIZE (VMCI_MAX_DG_SIZE * 2)
0547 
0548 struct vmci_event_payload_qp {
0549     struct vmci_handle handle;  /* queue_pair handle. */
0550     u32 peer_id;            /* Context id of attaching/detaching VM. */
0551     u32 _pad;
0552 };
0553 
0554 /* Flags for VMCI queue_pair API. */
0555 enum {
0556     /* Fail alloc if QP not created by peer. */
0557     VMCI_QPFLAG_ATTACH_ONLY = 1 << 0,
0558 
0559     /* Only allow attaches from local context. */
0560     VMCI_QPFLAG_LOCAL = 1 << 1,
0561 
0562     /* Host won't block when guest is quiesced. */
0563     VMCI_QPFLAG_NONBLOCK = 1 << 2,
0564 
0565     /* Pin data pages in ESX.  Used with NONBLOCK */
0566     VMCI_QPFLAG_PINNED = 1 << 3,
0567 
0568     /* Update the following flag when adding new flags. */
0569     VMCI_QP_ALL_FLAGS = (VMCI_QPFLAG_ATTACH_ONLY | VMCI_QPFLAG_LOCAL |
0570                  VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED),
0571 
0572     /* Convenience flags */
0573     VMCI_QP_ASYMM = (VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED),
0574     VMCI_QP_ASYMM_PEER = (VMCI_QPFLAG_ATTACH_ONLY | VMCI_QP_ASYMM),
0575 };
0576 
0577 /*
0578  * We allow at least 1024 more event datagrams from the hypervisor past the
0579  * normally allowed datagrams pending for a given context.  We define this
0580  * limit on event datagrams from the hypervisor to guard against DoS attack
0581  * from a malicious VM which could repeatedly attach to and detach from a queue
0582  * pair, causing events to be queued at the destination VM.  However, the rate
0583  * at which such events can be generated is small since it requires a VM exit
0584  * and handling of queue pair attach/detach call at the hypervisor.  Event
0585  * datagrams may be queued up at the destination VM if it has interrupts
0586  * disabled or if it is not draining events for some other reason.  1024
0587  * datagrams is a grossly conservative estimate of the time for which
0588  * interrupts may be disabled in the destination VM, but at the same time does
0589  * not exacerbate the memory pressure problem on the host by much (size of each
0590  * event datagram is small).
0591  */
0592 #define VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE              \
0593     (VMCI_MAX_DATAGRAM_QUEUE_SIZE +                 \
0594      1024 * (sizeof(struct vmci_datagram) +             \
0595          sizeof(struct vmci_event_data_max)))
0596 
0597 /*
0598  * Struct used for querying, via VMCI_RESOURCES_QUERY, the availability of
0599  * hypervisor resources.  Struct size is 16 bytes. All fields in struct are
0600  * aligned to their natural alignment.
0601  */
0602 struct vmci_resource_query_hdr {
0603     struct vmci_datagram hdr;
0604     u32 num_resources;
0605     u32 _padding;
0606 };
0607 
0608 /*
0609  * Convenience struct for negotiating vectors. Must match layout of
0610  * VMCIResourceQueryHdr minus the struct vmci_datagram header.
0611  */
0612 struct vmci_resource_query_msg {
0613     u32 num_resources;
0614     u32 _padding;
0615     u32 resources[1];
0616 };
0617 
0618 /*
0619  * The maximum number of resources that can be queried using
0620  * VMCI_RESOURCE_QUERY is 31, as the result is encoded in the lower 31
0621  * bits of a positive return value. Negative values are reserved for
0622  * errors.
0623  */
0624 #define VMCI_RESOURCE_QUERY_MAX_NUM 31
0625 
0626 /* Maximum size for the VMCI_RESOURCE_QUERY request. */
0627 #define VMCI_RESOURCE_QUERY_MAX_SIZE                \
0628     (sizeof(struct vmci_resource_query_hdr) +       \
0629      sizeof(u32) * VMCI_RESOURCE_QUERY_MAX_NUM)
0630 
0631 /*
0632  * Struct used for setting the notification bitmap.  All fields in
0633  * struct are aligned to their natural alignment.
0634  */
0635 struct vmci_notify_bm_set_msg {
0636     struct vmci_datagram hdr;
0637     union {
0638         u32 bitmap_ppn32;
0639         u64 bitmap_ppn64;
0640     };
0641 };
0642 
0643 /*
0644  * Struct used for linking a doorbell handle with an index in the
0645  * notify bitmap. All fields in struct are aligned to their natural
0646  * alignment.
0647  */
0648 struct vmci_doorbell_link_msg {
0649     struct vmci_datagram hdr;
0650     struct vmci_handle handle;
0651     u64 notify_idx;
0652 };
0653 
0654 /*
0655  * Struct used for unlinking a doorbell handle from an index in the
0656  * notify bitmap. All fields in struct are aligned to their natural
0657  * alignment.
0658  */
0659 struct vmci_doorbell_unlink_msg {
0660     struct vmci_datagram hdr;
0661     struct vmci_handle handle;
0662 };
0663 
0664 /*
0665  * Struct used for generating a notification on a doorbell handle. All
0666  * fields in struct are aligned to their natural alignment.
0667  */
0668 struct vmci_doorbell_notify_msg {
0669     struct vmci_datagram hdr;
0670     struct vmci_handle handle;
0671 };
0672 
0673 /*
0674  * This struct is used to contain data for events.  Size of this struct is a
0675  * multiple of 8 bytes, and all fields are aligned to their natural alignment.
0676  */
0677 struct vmci_event_data {
0678     u32 event;      /* 4 bytes. */
0679     u32 _pad;
0680     /* Event payload is put here. */
0681 };
0682 
0683 /*
0684  * Define the different VMCI_EVENT payload data types here.  All structs must
0685  * be a multiple of 8 bytes, and fields must be aligned to their natural
0686  * alignment.
0687  */
0688 struct vmci_event_payld_ctx {
0689     u32 context_id; /* 4 bytes. */
0690     u32 _pad;
0691 };
0692 
0693 struct vmci_event_payld_qp {
0694     struct vmci_handle handle;  /* queue_pair handle. */
0695     u32 peer_id;        /* Context id of attaching/detaching VM. */
0696     u32 _pad;
0697 };
0698 
0699 /*
0700  * We define the following struct to get the size of the maximum event
0701  * data the hypervisor may send to the guest.  If adding a new event
0702  * payload type above, add it to the following struct too (inside the
0703  * union).
0704  */
0705 struct vmci_event_data_max {
0706     struct vmci_event_data event_data;
0707     union {
0708         struct vmci_event_payld_ctx context_payload;
0709         struct vmci_event_payld_qp qp_payload;
0710     } ev_data_payload;
0711 };
0712 
0713 /*
0714  * Struct used for VMCI_EVENT_SUBSCRIBE/UNSUBSCRIBE and
0715  * VMCI_EVENT_HANDLER messages.  Struct size is 32 bytes.  All fields
0716  * in struct are aligned to their natural alignment.
0717  */
0718 struct vmci_event_msg {
0719     struct vmci_datagram hdr;
0720 
0721     /* Has event type and payload. */
0722     struct vmci_event_data event_data;
0723 
0724     /* Payload gets put here. */
0725 };
0726 
0727 /* Event with context payload. */
0728 struct vmci_event_ctx {
0729     struct vmci_event_msg msg;
0730     struct vmci_event_payld_ctx payload;
0731 };
0732 
0733 /* Event with QP payload. */
0734 struct vmci_event_qp {
0735     struct vmci_event_msg msg;
0736     struct vmci_event_payld_qp payload;
0737 };
0738 
0739 /*
0740  * Structs used for queue_pair alloc and detach messages.  We align fields of
0741  * these structs to 64bit boundaries.
0742  */
0743 struct vmci_qp_alloc_msg {
0744     struct vmci_datagram hdr;
0745     struct vmci_handle handle;
0746     u32 peer;
0747     u32 flags;
0748     u64 produce_size;
0749     u64 consume_size;
0750     u64 num_ppns;
0751 
0752     /* List of PPNs placed here. */
0753 };
0754 
0755 struct vmci_qp_detach_msg {
0756     struct vmci_datagram hdr;
0757     struct vmci_handle handle;
0758 };
0759 
0760 /* VMCI Doorbell API. */
0761 #define VMCI_FLAG_DELAYED_CB BIT(0)
0762 
0763 typedef void (*vmci_callback) (void *client_data);
0764 
0765 /*
0766  * struct vmci_qp - A vmw_vmci queue pair handle.
0767  *
0768  * This structure is used as a handle to a queue pair created by
0769  * VMCI.  It is intentionally left opaque to clients.
0770  */
0771 struct vmci_qp;
0772 
0773 /* Callback needed for correctly waiting on events. */
0774 typedef int (*vmci_datagram_recv_cb) (void *client_data,
0775                       struct vmci_datagram *msg);
0776 
0777 /* VMCI Event API. */
0778 typedef void (*vmci_event_cb) (u32 sub_id, const struct vmci_event_data *ed,
0779                    void *client_data);
0780 
0781 /*
0782  * We use the following inline function to access the payload data
0783  * associated with an event data.
0784  */
0785 static inline const void *
0786 vmci_event_data_const_payload(const struct vmci_event_data *ev_data)
0787 {
0788     return (const char *)ev_data + sizeof(*ev_data);
0789 }
0790 
0791 static inline void *vmci_event_data_payload(struct vmci_event_data *ev_data)
0792 {
0793     return (void *)vmci_event_data_const_payload(ev_data);
0794 }
0795 
0796 /*
0797  * Helper to read a value from a head or tail pointer. For X86_32, the
0798  * pointer is treated as a 32bit value, since the pointer value
0799  * never exceeds a 32bit value in this case. Also, doing an
0800  * atomic64_read on X86_32 uniprocessor systems may be implemented
0801  * as a non locked cmpxchg8b, that may end up overwriting updates done
0802  * by the VMCI device to the memory location. On 32bit SMP, the lock
0803  * prefix will be used, so correctness isn't an issue, but using a
0804  * 64bit operation still adds unnecessary overhead.
0805  */
0806 static inline u64 vmci_q_read_pointer(u64 *var)
0807 {
0808     return READ_ONCE(*(unsigned long *)var);
0809 }
0810 
0811 /*
0812  * Helper to set the value of a head or tail pointer. For X86_32, the
0813  * pointer is treated as a 32bit value, since the pointer value
0814  * never exceeds a 32bit value in this case. On 32bit SMP, using a
0815  * locked cmpxchg8b adds unnecessary overhead.
0816  */
0817 static inline void vmci_q_set_pointer(u64 *var, u64 new_val)
0818 {
0819     /* XXX buggered on big-endian */
0820     WRITE_ONCE(*(unsigned long *)var, (unsigned long)new_val);
0821 }
0822 
0823 /*
0824  * Helper to add a given offset to a head or tail pointer. Wraps the
0825  * value of the pointer around the max size of the queue.
0826  */
0827 static inline void vmci_qp_add_pointer(u64 *var, size_t add, u64 size)
0828 {
0829     u64 new_val = vmci_q_read_pointer(var);
0830 
0831     if (new_val >= size - add)
0832         new_val -= size;
0833 
0834     new_val += add;
0835 
0836     vmci_q_set_pointer(var, new_val);
0837 }
0838 
0839 /*
0840  * Helper routine to get the Producer Tail from the supplied queue.
0841  */
0842 static inline u64
0843 vmci_q_header_producer_tail(const struct vmci_queue_header *q_header)
0844 {
0845     struct vmci_queue_header *qh = (struct vmci_queue_header *)q_header;
0846     return vmci_q_read_pointer(&qh->producer_tail);
0847 }
0848 
0849 /*
0850  * Helper routine to get the Consumer Head from the supplied queue.
0851  */
0852 static inline u64
0853 vmci_q_header_consumer_head(const struct vmci_queue_header *q_header)
0854 {
0855     struct vmci_queue_header *qh = (struct vmci_queue_header *)q_header;
0856     return vmci_q_read_pointer(&qh->consumer_head);
0857 }
0858 
0859 /*
0860  * Helper routine to increment the Producer Tail.  Fundamentally,
0861  * vmci_qp_add_pointer() is used to manipulate the tail itself.
0862  */
0863 static inline void
0864 vmci_q_header_add_producer_tail(struct vmci_queue_header *q_header,
0865                 size_t add,
0866                 u64 queue_size)
0867 {
0868     vmci_qp_add_pointer(&q_header->producer_tail, add, queue_size);
0869 }
0870 
0871 /*
0872  * Helper routine to increment the Consumer Head.  Fundamentally,
0873  * vmci_qp_add_pointer() is used to manipulate the head itself.
0874  */
0875 static inline void
0876 vmci_q_header_add_consumer_head(struct vmci_queue_header *q_header,
0877                 size_t add,
0878                 u64 queue_size)
0879 {
0880     vmci_qp_add_pointer(&q_header->consumer_head, add, queue_size);
0881 }
0882 
0883 /*
0884  * Helper routine for getting the head and the tail pointer for a queue.
0885  * Both the VMCIQueues are needed to get both the pointers for one queue.
0886  */
0887 static inline void
0888 vmci_q_header_get_pointers(const struct vmci_queue_header *produce_q_header,
0889                const struct vmci_queue_header *consume_q_header,
0890                u64 *producer_tail,
0891                u64 *consumer_head)
0892 {
0893     if (producer_tail)
0894         *producer_tail = vmci_q_header_producer_tail(produce_q_header);
0895 
0896     if (consumer_head)
0897         *consumer_head = vmci_q_header_consumer_head(consume_q_header);
0898 }
0899 
0900 static inline void vmci_q_header_init(struct vmci_queue_header *q_header,
0901                       const struct vmci_handle handle)
0902 {
0903     q_header->handle = handle;
0904     q_header->producer_tail = 0;
0905     q_header->consumer_head = 0;
0906 }
0907 
0908 /*
0909  * Finds available free space in a produce queue to enqueue more
0910  * data or reports an error if queue pair corruption is detected.
0911  */
0912 static s64
0913 vmci_q_header_free_space(const struct vmci_queue_header *produce_q_header,
0914              const struct vmci_queue_header *consume_q_header,
0915              const u64 produce_q_size)
0916 {
0917     u64 tail;
0918     u64 head;
0919     u64 free_space;
0920 
0921     tail = vmci_q_header_producer_tail(produce_q_header);
0922     head = vmci_q_header_consumer_head(consume_q_header);
0923 
0924     if (tail >= produce_q_size || head >= produce_q_size)
0925         return VMCI_ERROR_INVALID_SIZE;
0926 
0927     /*
0928      * Deduct 1 to avoid tail becoming equal to head which causes
0929      * ambiguity. If head and tail are equal it means that the
0930      * queue is empty.
0931      */
0932     if (tail >= head)
0933         free_space = produce_q_size - (tail - head) - 1;
0934     else
0935         free_space = head - tail - 1;
0936 
0937     return free_space;
0938 }
0939 
0940 /*
0941  * vmci_q_header_free_space() does all the heavy lifting of
0942  * determing the number of free bytes in a Queue.  This routine,
0943  * then subtracts that size from the full size of the Queue so
0944  * the caller knows how many bytes are ready to be dequeued.
0945  * Results:
0946  * On success, available data size in bytes (up to MAX_INT64).
0947  * On failure, appropriate error code.
0948  */
0949 static inline s64
0950 vmci_q_header_buf_ready(const struct vmci_queue_header *consume_q_header,
0951             const struct vmci_queue_header *produce_q_header,
0952             const u64 consume_q_size)
0953 {
0954     s64 free_space;
0955 
0956     free_space = vmci_q_header_free_space(consume_q_header,
0957                           produce_q_header, consume_q_size);
0958     if (free_space < VMCI_SUCCESS)
0959         return free_space;
0960 
0961     return consume_q_size - free_space - 1;
0962 }
0963 
0964 
0965 #endif /* _VMW_VMCI_DEF_H_ */