162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * VMware VMCI Driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2012 VMware, Inc. All rights reserved.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef _VMCI_QUEUE_PAIR_H_
962306a36Sopenharmony_ci#define _VMCI_QUEUE_PAIR_H_
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/vmw_vmci_defs.h>
1262306a36Sopenharmony_ci#include <linux/types.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "vmci_context.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/* Callback needed for correctly waiting on events. */
1762306a36Sopenharmony_citypedef int (*vmci_event_release_cb) (void *client_data);
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/* Guest device port I/O. */
2062306a36Sopenharmony_cistruct ppn_set {
2162306a36Sopenharmony_ci	u64 num_produce_pages;
2262306a36Sopenharmony_ci	u64 num_consume_pages;
2362306a36Sopenharmony_ci	u64 *produce_ppns;
2462306a36Sopenharmony_ci	u64 *consume_ppns;
2562306a36Sopenharmony_ci	bool initialized;
2662306a36Sopenharmony_ci};
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/* VMCIqueue_pairAllocInfo */
2962306a36Sopenharmony_cistruct vmci_qp_alloc_info {
3062306a36Sopenharmony_ci	struct vmci_handle handle;
3162306a36Sopenharmony_ci	u32 peer;
3262306a36Sopenharmony_ci	u32 flags;
3362306a36Sopenharmony_ci	u64 produce_size;
3462306a36Sopenharmony_ci	u64 consume_size;
3562306a36Sopenharmony_ci	u64 ppn_va;	/* Start VA of queue pair PPNs. */
3662306a36Sopenharmony_ci	u64 num_ppns;
3762306a36Sopenharmony_ci	s32 result;
3862306a36Sopenharmony_ci	u32 version;
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* VMCIqueue_pairSetVAInfo */
4262306a36Sopenharmony_cistruct vmci_qp_set_va_info {
4362306a36Sopenharmony_ci	struct vmci_handle handle;
4462306a36Sopenharmony_ci	u64 va;		/* Start VA of queue pair PPNs. */
4562306a36Sopenharmony_ci	u64 num_ppns;
4662306a36Sopenharmony_ci	u32 version;
4762306a36Sopenharmony_ci	s32 result;
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/*
5162306a36Sopenharmony_ci * For backwards compatibility, here is a version of the
5262306a36Sopenharmony_ci * VMCIqueue_pairPageFileInfo before host support end-points was added.
5362306a36Sopenharmony_ci * Note that the current version of that structure requires VMX to
5462306a36Sopenharmony_ci * pass down the VA of the mapped file.  Before host support was added
5562306a36Sopenharmony_ci * there was nothing of the sort.  So, when the driver sees the ioctl
5662306a36Sopenharmony_ci * with a parameter that is the sizeof
5762306a36Sopenharmony_ci * VMCIqueue_pairPageFileInfo_NoHostQP then it can infer that the version
5862306a36Sopenharmony_ci * of VMX running can't attach to host end points because it doesn't
5962306a36Sopenharmony_ci * provide the VA of the mapped files.
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * The Linux driver doesn't get an indication of the size of the
6262306a36Sopenharmony_ci * structure passed down from user space.  So, to fix a long standing
6362306a36Sopenharmony_ci * but unfiled bug, the _pad field has been renamed to version.
6462306a36Sopenharmony_ci * Existing versions of VMX always initialize the PageFileInfo
6562306a36Sopenharmony_ci * structure so that _pad, er, version is set to 0.
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci * A version value of 1 indicates that the size of the structure has
6862306a36Sopenharmony_ci * been increased to include two UVA's: produce_uva and consume_uva.
6962306a36Sopenharmony_ci * These UVA's are of the mmap()'d queue contents backing files.
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * In addition, if when VMX is sending down the
7262306a36Sopenharmony_ci * VMCIqueue_pairPageFileInfo structure it gets an error then it will
7362306a36Sopenharmony_ci * try again with the _NoHostQP version of the file to see if an older
7462306a36Sopenharmony_ci * VMCI kernel module is running.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/* VMCIqueue_pairPageFileInfo */
7862306a36Sopenharmony_cistruct vmci_qp_page_file_info {
7962306a36Sopenharmony_ci	struct vmci_handle handle;
8062306a36Sopenharmony_ci	u64 produce_page_file;	  /* User VA. */
8162306a36Sopenharmony_ci	u64 consume_page_file;	  /* User VA. */
8262306a36Sopenharmony_ci	u64 produce_page_file_size;  /* Size of the file name array. */
8362306a36Sopenharmony_ci	u64 consume_page_file_size;  /* Size of the file name array. */
8462306a36Sopenharmony_ci	s32 result;
8562306a36Sopenharmony_ci	u32 version;	/* Was _pad. */
8662306a36Sopenharmony_ci	u64 produce_va;	/* User VA of the mapped file. */
8762306a36Sopenharmony_ci	u64 consume_va;	/* User VA of the mapped file. */
8862306a36Sopenharmony_ci};
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/* vmci queuepair detach info */
9162306a36Sopenharmony_cistruct vmci_qp_dtch_info {
9262306a36Sopenharmony_ci	struct vmci_handle handle;
9362306a36Sopenharmony_ci	s32 result;
9462306a36Sopenharmony_ci	u32 _pad;
9562306a36Sopenharmony_ci};
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/*
9862306a36Sopenharmony_ci * struct vmci_qp_page_store describes how the memory of a given queue pair
9962306a36Sopenharmony_ci * is backed. When the queue pair is between the host and a guest, the
10062306a36Sopenharmony_ci * page store consists of references to the guest pages. On vmkernel,
10162306a36Sopenharmony_ci * this is a list of PPNs, and on hosted, it is a user VA where the
10262306a36Sopenharmony_ci * queue pair is mapped into the VMX address space.
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_cistruct vmci_qp_page_store {
10562306a36Sopenharmony_ci	/* Reference to pages backing the queue pair. */
10662306a36Sopenharmony_ci	u64 pages;
10762306a36Sopenharmony_ci	/* Length of pageList/virtual address range (in pages). */
10862306a36Sopenharmony_ci	u32 len;
10962306a36Sopenharmony_ci};
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/*
11262306a36Sopenharmony_ci * This data type contains the information about a queue.
11362306a36Sopenharmony_ci * There are two queues (hence, queue pairs) per transaction model between a
11462306a36Sopenharmony_ci * pair of end points, A & B.  One queue is used by end point A to transmit
11562306a36Sopenharmony_ci * commands and responses to B.  The other queue is used by B to transmit
11662306a36Sopenharmony_ci * commands and responses.
11762306a36Sopenharmony_ci *
11862306a36Sopenharmony_ci * struct vmci_queue_kern_if is a per-OS defined Queue structure.  It contains
11962306a36Sopenharmony_ci * either a direct pointer to the linear address of the buffer contents or a
12062306a36Sopenharmony_ci * pointer to structures which help the OS locate those data pages.  See
12162306a36Sopenharmony_ci * vmciKernelIf.c for each platform for its definition.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_cistruct vmci_queue {
12462306a36Sopenharmony_ci	struct vmci_queue_header *q_header;
12562306a36Sopenharmony_ci	struct vmci_queue_header *saved_header;
12662306a36Sopenharmony_ci	struct vmci_queue_kern_if *kernel_if;
12762306a36Sopenharmony_ci};
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci/*
13062306a36Sopenharmony_ci * Utility function that checks whether the fields of the page
13162306a36Sopenharmony_ci * store contain valid values.
13262306a36Sopenharmony_ci * Result:
13362306a36Sopenharmony_ci * true if the page store is wellformed. false otherwise.
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_cistatic inline bool
13662306a36Sopenharmony_ciVMCI_QP_PAGESTORE_IS_WELLFORMED(struct vmci_qp_page_store *page_store)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	return page_store->len >= 2;
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_civoid vmci_qp_broker_exit(void);
14262306a36Sopenharmony_ciint vmci_qp_broker_alloc(struct vmci_handle handle, u32 peer,
14362306a36Sopenharmony_ci			 u32 flags, u32 priv_flags,
14462306a36Sopenharmony_ci			 u64 produce_size, u64 consume_size,
14562306a36Sopenharmony_ci			 struct vmci_qp_page_store *page_store,
14662306a36Sopenharmony_ci			 struct vmci_ctx *context);
14762306a36Sopenharmony_ciint vmci_qp_broker_set_page_store(struct vmci_handle handle,
14862306a36Sopenharmony_ci				  u64 produce_uva, u64 consume_uva,
14962306a36Sopenharmony_ci				  struct vmci_ctx *context);
15062306a36Sopenharmony_ciint vmci_qp_broker_detach(struct vmci_handle handle, struct vmci_ctx *context);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_civoid vmci_qp_guest_endpoints_exit(void);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ciint vmci_qp_alloc(struct vmci_handle *handle,
15562306a36Sopenharmony_ci		  struct vmci_queue **produce_q, u64 produce_size,
15662306a36Sopenharmony_ci		  struct vmci_queue **consume_q, u64 consume_size,
15762306a36Sopenharmony_ci		  u32 peer, u32 flags, u32 priv_flags,
15862306a36Sopenharmony_ci		  bool guest_endpoint, vmci_event_release_cb wakeup_cb,
15962306a36Sopenharmony_ci		  void *client_data);
16062306a36Sopenharmony_ciint vmci_qp_broker_map(struct vmci_handle handle,
16162306a36Sopenharmony_ci		       struct vmci_ctx *context, u64 guest_mem);
16262306a36Sopenharmony_ciint vmci_qp_broker_unmap(struct vmci_handle handle,
16362306a36Sopenharmony_ci			 struct vmci_ctx *context, u32 gid);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci#endif /* _VMCI_QUEUE_PAIR_H_ */
166