/* SPDX-License-Identifier: GPL-2.0-only */ /* * VMware VMCI Driver * * Copyright (C) 2012 VMware, Inc. All rights reserved. */ #ifndef _VMW_VMCI_DEF_H_ #define _VMW_VMCI_DEF_H_ #include <linux/atomic.h> #include <linux/bits.h> /* Register offsets. */ #define VMCI_STATUS_ADDR … #define VMCI_CONTROL_ADDR … #define VMCI_ICR_ADDR … #define VMCI_IMR_ADDR … #define VMCI_DATA_OUT_ADDR … #define VMCI_DATA_IN_ADDR … #define VMCI_CAPS_ADDR … #define VMCI_RESULT_LOW_ADDR … #define VMCI_RESULT_HIGH_ADDR … #define VMCI_DATA_OUT_LOW_ADDR … #define VMCI_DATA_OUT_HIGH_ADDR … #define VMCI_DATA_IN_LOW_ADDR … #define VMCI_DATA_IN_HIGH_ADDR … #define VMCI_GUEST_PAGE_SHIFT … /* Max number of devices. */ #define VMCI_MAX_DEVICES … /* Status register bits. */ #define VMCI_STATUS_INT_ON … /* Control register bits. */ #define VMCI_CONTROL_RESET … #define VMCI_CONTROL_INT_ENABLE … #define VMCI_CONTROL_INT_DISABLE … /* Capabilities register bits. */ #define VMCI_CAPS_HYPERCALL … #define VMCI_CAPS_GUESTCALL … #define VMCI_CAPS_DATAGRAM … #define VMCI_CAPS_NOTIFICATIONS … #define VMCI_CAPS_PPN64 … #define VMCI_CAPS_DMA_DATAGRAM … /* Interrupt Cause register bits. */ #define VMCI_ICR_DATAGRAM … #define VMCI_ICR_NOTIFICATION … #define VMCI_ICR_DMA_DATAGRAM … /* Interrupt Mask register bits. */ #define VMCI_IMR_DATAGRAM … #define VMCI_IMR_NOTIFICATION … #define VMCI_IMR_DMA_DATAGRAM … /* * Maximum MSI/MSI-X interrupt vectors in the device. * If VMCI_CAPS_DMA_DATAGRAM is supported by the device, * VMCI_MAX_INTRS_DMA_DATAGRAM vectors are available, * otherwise only VMCI_MAX_INTRS_NOTIFICATION. */ #define VMCI_MAX_INTRS_NOTIFICATION … #define VMCI_MAX_INTRS_DMA_DATAGRAM … #define VMCI_MAX_INTRS … /* * Supported interrupt vectors. There is one for each ICR value above, * but here they indicate the position in the vector array/message ID. */ enum { … }; /* * A single VMCI device has an upper limit of 128MB on the amount of * memory that can be used for queue pairs. Since each queue pair * consists of at least two pages, the memory limit also dictates the * number of queue pairs a guest can create. */ #define VMCI_MAX_GUEST_QP_MEMORY … #define VMCI_MAX_GUEST_QP_COUNT … /* * There can be at most PAGE_SIZE doorbells since there is one doorbell * per byte in the doorbell bitmap page. */ #define VMCI_MAX_GUEST_DOORBELL_COUNT … /* * Queues with pre-mapped data pages must be small, so that we don't pin * too much kernel memory (especially on vmkernel). We limit a queuepair to * 32 KB, or 16 KB per queue for symmetrical pairs. */ #define VMCI_MAX_PINNED_QP_MEMORY … /* * The version of the VMCI device that supports MMIO access to registers * requests 256KB for BAR1 whereas the version of VMCI that supports * MSI/MSI-X only requests 8KB. The layout of the larger 256KB region is: * - the first 128KB are used for MSI/MSI-X. * - the following 64KB are used for MMIO register access. * - the remaining 64KB are unused. */ #define VMCI_WITH_MMIO_ACCESS_BAR_SIZE … #define VMCI_MMIO_ACCESS_OFFSET … #define VMCI_MMIO_ACCESS_SIZE … /* * For VMCI devices supporting the VMCI_CAPS_DMA_DATAGRAM capability, the * sending and receiving of datagrams can be performed using DMA to/from * a driver allocated buffer. * Sending and receiving will be handled as follows: * - when sending datagrams, the driver initializes the buffer where the * data part will refer to the outgoing VMCI datagram, sets the busy flag * to 1 and writes the address of the buffer to VMCI_DATA_OUT_HIGH_ADDR * and VMCI_DATA_OUT_LOW_ADDR. Writing to VMCI_DATA_OUT_LOW_ADDR triggers * the device processing of the buffer. When the device has processed the * buffer, it will write the result value to the buffer and then clear the * busy flag. * - when receiving datagrams, the driver initializes the buffer where the * data part will describe the receive buffer, clears the busy flag and * writes the address of the buffer to VMCI_DATA_IN_HIGH_ADDR and * VMCI_DATA_IN_LOW_ADDR. Writing to VMCI_DATA_IN_LOW_ADDR triggers the * device processing of the buffer. The device will copy as many available * datagrams into the buffer as possible, and then sets the busy flag. * When the busy flag is set, the driver will process the datagrams in the * buffer. */ struct vmci_data_in_out_header { … }; struct vmci_sg_elem { … }; /* * We have a fixed set of resource IDs available in the VMX. * This allows us to have a very simple implementation since we statically * know how many will create datagram handles. If a new caller arrives and * we have run out of slots we can manually increment the maximum size of * available resource IDs. * * VMCI reserved hypervisor datagram resource IDs. */ enum { … }; /* * struct vmci_handle - Ownership information structure * @context: The VMX context ID. * @resource: The resource ID (used for locating in resource hash). * * The vmci_handle structure is used to track resources used within * vmw_vmci. */ struct vmci_handle { … }; #define vmci_make_handle(_cid, _rid) … static inline bool vmci_handle_is_equal(struct vmci_handle h1, struct vmci_handle h2) { … } #define VMCI_INVALID_ID … static const struct vmci_handle VMCI_INVALID_HANDLE = …; static inline bool vmci_handle_is_invalid(struct vmci_handle h) { … } /* * The below defines can be used to send anonymous requests. * This also indicates that no response is expected. */ #define VMCI_ANON_SRC_CONTEXT_ID … #define VMCI_ANON_SRC_RESOURCE_ID … static const struct vmci_handle __maybe_unused VMCI_ANON_SRC_HANDLE = …; /* The lowest 16 context ids are reserved for internal use. */ #define VMCI_RESERVED_CID_LIMIT … /* * Hypervisor context id, used for calling into hypervisor * supplied services from the VM. */ #define VMCI_HYPERVISOR_CONTEXT_ID … /* * Well-known context id, a logical context that contains a set of * well-known services. This context ID is now obsolete. */ #define VMCI_WELL_KNOWN_CONTEXT_ID … /* * Context ID used by host endpoints. */ #define VMCI_HOST_CONTEXT_ID … #define VMCI_CONTEXT_IS_VM(_cid) … /* * The VMCI_CONTEXT_RESOURCE_ID is used together with vmci_make_handle to make * handles that refer to a specific context. */ #define VMCI_CONTEXT_RESOURCE_ID … /* * VMCI error codes. */ enum { … }; /* VMCI reserved events. */ enum { … }; /* * Of the above events, a few are reserved for use in the VMX, and * other endpoints (guest and host kernel) should not use them. For * the rest of the events, we allow both host and guest endpoints to * subscribe to them, to maintain the same API for host and guest * endpoints. */ #define VMCI_EVENT_VALID_VMX(_event) … #define VMCI_EVENT_VALID(_event) … /* Reserved guest datagram resource ids. */ #define VMCI_EVENT_HANDLER … /* * VMCI coarse-grained privileges (per context or host * process/endpoint. An entity with the restricted flag is only * allowed to interact with the hypervisor and trusted entities. */ enum { … }; /* 0 through VMCI_RESERVED_RESOURCE_ID_MAX are reserved. */ #define VMCI_RESERVED_RESOURCE_ID_MAX … /* * Driver version. * * Increment major version when you make an incompatible change. * Compatibility goes both ways (old driver with new executable * as well as new driver with old executable). */ /* Never change VMCI_VERSION_SHIFT_WIDTH */ #define VMCI_VERSION_SHIFT_WIDTH … #define VMCI_MAKE_VERSION(_major, _minor) … #define VMCI_VERSION_MAJOR(v) … #define VMCI_VERSION_MINOR(v) … /* * VMCI_VERSION is always the current version. Subsequently listed * versions are ways of detecting previous versions of the connecting * application (i.e., VMX). * * VMCI_VERSION_NOVMVM: This version removed support for VM to VM * communication. * * VMCI_VERSION_NOTIFY: This version introduced doorbell notification * support. * * VMCI_VERSION_HOSTQP: This version introduced host end point support * for hosted products. * * VMCI_VERSION_PREHOSTQP: This is the version prior to the adoption of * support for host end-points. * * VMCI_VERSION_PREVERS2: This fictional version number is intended to * represent the version of a VMX which doesn't call into the driver * with ioctl VERSION2 and thus doesn't establish its version with the * driver. */ #define VMCI_VERSION … #define VMCI_VERSION_NOVMVM … #define VMCI_VERSION_NOTIFY … #define VMCI_VERSION_HOSTQP … #define VMCI_VERSION_PREHOSTQP … #define VMCI_VERSION_PREVERS2 … #define VMCI_SOCKETS_MAKE_VERSION(_p) … /* * The VMCI IOCTLs. We use identity code 7, as noted in ioctl-number.h, and * we start at sequence 9f. This gives us the same values that our shipping * products use, starting at 1951, provided we leave out the direction and * structure size. Note that VMMon occupies the block following us, starting * at 2001. */ #define IOCTL_VMCI_VERSION … #define IOCTL_VMCI_INIT_CONTEXT … #define IOCTL_VMCI_QUEUEPAIR_SETVA … #define IOCTL_VMCI_NOTIFY_RESOURCE … #define IOCTL_VMCI_NOTIFICATIONS_RECEIVE … #define IOCTL_VMCI_VERSION2 … #define IOCTL_VMCI_QUEUEPAIR_ALLOC … #define IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE … #define IOCTL_VMCI_QUEUEPAIR_DETACH … #define IOCTL_VMCI_DATAGRAM_SEND … #define IOCTL_VMCI_DATAGRAM_RECEIVE … #define IOCTL_VMCI_CTX_ADD_NOTIFICATION … #define IOCTL_VMCI_CTX_REMOVE_NOTIFICATION … #define IOCTL_VMCI_CTX_GET_CPT_STATE … #define IOCTL_VMCI_CTX_SET_CPT_STATE … #define IOCTL_VMCI_GET_CONTEXT_ID … #define IOCTL_VMCI_SOCKETS_VERSION … #define IOCTL_VMCI_SOCKETS_GET_AF_VALUE … #define IOCTL_VMCI_SOCKETS_GET_LOCAL_CID … #define IOCTL_VMCI_SET_NOTIFY … /*IOCTL_VMMON_START _IO(7, 0xd1)*/ /* 2001 */ /* * struct vmci_queue_header - VMCI Queue Header information. * * A Queue cannot stand by itself as designed. Each Queue's header * contains a pointer into itself (the producer_tail) and into its peer * (consumer_head). The reason for the separation is one of * accessibility: Each end-point can modify two things: where the next * location to enqueue is within its produce_q (producer_tail); and * where the next dequeue location is in its consume_q (consumer_head). * * An end-point cannot modify the pointers of its peer (guest to * guest; NOTE that in the host both queue headers are mapped r/w). * But, each end-point needs read access to both Queue header * structures in order to determine how much space is used (or left) * in the Queue. This is because for an end-point to know how full * its produce_q is, it needs to use the consumer_head that points into * the produce_q but -that- consumer_head is in the Queue header for * that end-points consume_q. * * Thoroughly confused? Sorry. * * producer_tail: the point to enqueue new entrants. When you approach * a line in a store, for example, you walk up to the tail. * * consumer_head: the point in the queue from which the next element is * dequeued. In other words, who is next in line is he who is at the * head of the line. * * Also, producer_tail points to an empty byte in the Queue, whereas * consumer_head points to a valid byte of data (unless producer_tail == * consumer_head in which case consumer_head does not point to a valid * byte of data). * * For a queue of buffer 'size' bytes, the tail and head pointers will be in * the range [0, size-1]. * * If produce_q_header->producer_tail == consume_q_header->consumer_head * then the produce_q is empty. */ struct vmci_queue_header { … }; /* * struct vmci_datagram - Base struct for vmci datagrams. * @dst: A vmci_handle that tracks the destination of the datagram. * @src: A vmci_handle that tracks the source of the datagram. * @payload_size: The size of the payload. * * vmci_datagram structs are used when sending vmci datagrams. They include * the necessary source and destination information to properly route * the information along with the size of the package. */ struct vmci_datagram { … }; /* * Second flag is for creating a well-known handle instead of a per context * handle. Next flag is for deferring datagram delivery, so that the * datagram callback is invoked in a delayed context (not interrupt context). */ #define VMCI_FLAG_DG_NONE … #define VMCI_FLAG_WELLKNOWN_DG_HND … #define VMCI_FLAG_ANYCID_DG_HND … #define VMCI_FLAG_DG_DELAYED_CB … /* * Maximum supported size of a VMCI datagram for routable datagrams. * Datagrams going to the hypervisor are allowed to be larger. */ #define VMCI_MAX_DG_SIZE … #define VMCI_MAX_DG_PAYLOAD_SIZE … #define VMCI_DG_PAYLOAD(_dg) … #define VMCI_DG_HEADERSIZE … #define VMCI_DG_SIZE(_dg) … #define VMCI_DG_SIZE_ALIGNED(_dg) … #define VMCI_MAX_DATAGRAM_QUEUE_SIZE … struct vmci_event_payload_qp { … }; /* Flags for VMCI queue_pair API. */ enum { … }; /* * We allow at least 1024 more event datagrams from the hypervisor past the * normally allowed datagrams pending for a given context. We define this * limit on event datagrams from the hypervisor to guard against DoS attack * from a malicious VM which could repeatedly attach to and detach from a queue * pair, causing events to be queued at the destination VM. However, the rate * at which such events can be generated is small since it requires a VM exit * and handling of queue pair attach/detach call at the hypervisor. Event * datagrams may be queued up at the destination VM if it has interrupts * disabled or if it is not draining events for some other reason. 1024 * datagrams is a grossly conservative estimate of the time for which * interrupts may be disabled in the destination VM, but at the same time does * not exacerbate the memory pressure problem on the host by much (size of each * event datagram is small). */ #define VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE … /* * Struct used for querying, via VMCI_RESOURCES_QUERY, the availability of * hypervisor resources. Struct size is 16 bytes. All fields in struct are * aligned to their natural alignment. */ struct vmci_resource_query_hdr { … }; /* * Convenience struct for negotiating vectors. Must match layout of * VMCIResourceQueryHdr minus the struct vmci_datagram header. */ struct vmci_resource_query_msg { … }; /* * The maximum number of resources that can be queried using * VMCI_RESOURCE_QUERY is 31, as the result is encoded in the lower 31 * bits of a positive return value. Negative values are reserved for * errors. */ #define VMCI_RESOURCE_QUERY_MAX_NUM … /* Maximum size for the VMCI_RESOURCE_QUERY request. */ #define VMCI_RESOURCE_QUERY_MAX_SIZE … /* * Struct used for setting the notification bitmap. All fields in * struct are aligned to their natural alignment. */ struct vmci_notify_bm_set_msg { … }; /* * Struct used for linking a doorbell handle with an index in the * notify bitmap. All fields in struct are aligned to their natural * alignment. */ struct vmci_doorbell_link_msg { … }; /* * Struct used for unlinking a doorbell handle from an index in the * notify bitmap. All fields in struct are aligned to their natural * alignment. */ struct vmci_doorbell_unlink_msg { … }; /* * Struct used for generating a notification on a doorbell handle. All * fields in struct are aligned to their natural alignment. */ struct vmci_doorbell_notify_msg { … }; /* * This struct is used to contain data for events. Size of this struct is a * multiple of 8 bytes, and all fields are aligned to their natural alignment. */ struct vmci_event_data { … }; /* * Define the different VMCI_EVENT payload data types here. All structs must * be a multiple of 8 bytes, and fields must be aligned to their natural * alignment. */ struct vmci_event_payld_ctx { … }; struct vmci_event_payld_qp { … }; /* * We define the following struct to get the size of the maximum event * data the hypervisor may send to the guest. If adding a new event * payload type above, add it to the following struct too (inside the * union). */ struct vmci_event_data_max { … }; /* * Struct used for VMCI_EVENT_SUBSCRIBE/UNSUBSCRIBE and * VMCI_EVENT_HANDLER messages. Struct size is 32 bytes. All fields * in struct are aligned to their natural alignment. */ struct vmci_event_msg { … }; /* Event with context payload. */ struct vmci_event_ctx { … }; /* Event with QP payload. */ struct vmci_event_qp { … }; /* * Structs used for queue_pair alloc and detach messages. We align fields of * these structs to 64bit boundaries. */ struct vmci_qp_alloc_msg { … }; struct vmci_qp_detach_msg { … }; /* VMCI Doorbell API. */ #define VMCI_FLAG_DELAYED_CB … vmci_callback; /* * struct vmci_qp - A vmw_vmci queue pair handle. * * This structure is used as a handle to a queue pair created by * VMCI. It is intentionally left opaque to clients. */ struct vmci_qp; /* Callback needed for correctly waiting on events. */ vmci_datagram_recv_cb; /* VMCI Event API. */ vmci_event_cb; /* * We use the following inline function to access the payload data * associated with an event data. */ static inline const void * vmci_event_data_const_payload(const struct vmci_event_data *ev_data) { … } static inline void *vmci_event_data_payload(struct vmci_event_data *ev_data) { … } /* * Helper to read a value from a head or tail pointer. For X86_32, the * pointer is treated as a 32bit value, since the pointer value * never exceeds a 32bit value in this case. Also, doing an * atomic64_read on X86_32 uniprocessor systems may be implemented * as a non locked cmpxchg8b, that may end up overwriting updates done * by the VMCI device to the memory location. On 32bit SMP, the lock * prefix will be used, so correctness isn't an issue, but using a * 64bit operation still adds unnecessary overhead. */ static inline u64 vmci_q_read_pointer(u64 *var) { … } /* * Helper to set the value of a head or tail pointer. For X86_32, the * pointer is treated as a 32bit value, since the pointer value * never exceeds a 32bit value in this case. On 32bit SMP, using a * locked cmpxchg8b adds unnecessary overhead. */ static inline void vmci_q_set_pointer(u64 *var, u64 new_val) { … } /* * Helper to add a given offset to a head or tail pointer. Wraps the * value of the pointer around the max size of the queue. */ static inline void vmci_qp_add_pointer(u64 *var, size_t add, u64 size) { … } /* * Helper routine to get the Producer Tail from the supplied queue. */ static inline u64 vmci_q_header_producer_tail(const struct vmci_queue_header *q_header) { … } /* * Helper routine to get the Consumer Head from the supplied queue. */ static inline u64 vmci_q_header_consumer_head(const struct vmci_queue_header *q_header) { … } /* * Helper routine to increment the Producer Tail. Fundamentally, * vmci_qp_add_pointer() is used to manipulate the tail itself. */ static inline void vmci_q_header_add_producer_tail(struct vmci_queue_header *q_header, size_t add, u64 queue_size) { … } /* * Helper routine to increment the Consumer Head. Fundamentally, * vmci_qp_add_pointer() is used to manipulate the head itself. */ static inline void vmci_q_header_add_consumer_head(struct vmci_queue_header *q_header, size_t add, u64 queue_size) { … } /* * Helper routine for getting the head and the tail pointer for a queue. * Both the VMCIQueues are needed to get both the pointers for one queue. */ 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) { … } static inline void vmci_q_header_init(struct vmci_queue_header *q_header, const struct vmci_handle handle) { … } /* * Finds available free space in a produce queue to enqueue more * data or reports an error if queue pair corruption is detected. */ 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) { … } /* * vmci_q_header_free_space() does all the heavy lifting of * determing the number of free bytes in a Queue. This routine, * then subtracts that size from the full size of the Queue so * the caller knows how many bytes are ready to be dequeued. * Results: * On success, available data size in bytes (up to MAX_INT64). * On failure, appropriate error code. */ 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) { … } #endif /* _VMW_VMCI_DEF_H_ */