18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * VMware vSockets Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013 VMware, Inc. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _VMCI_TRANSPORT_H_
98c2ecf20Sopenharmony_ci#define _VMCI_TRANSPORT_H_
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/vmw_vmci_defs.h>
128c2ecf20Sopenharmony_ci#include <linux/vmw_vmci_api.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <net/vsock_addr.h>
158c2ecf20Sopenharmony_ci#include <net/af_vsock.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* If the packet format changes in a release then this should change too. */
188c2ecf20Sopenharmony_ci#define VMCI_TRANSPORT_PACKET_VERSION 1
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* The resource ID on which control packets are sent. */
218c2ecf20Sopenharmony_ci#define VMCI_TRANSPORT_PACKET_RID 1
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* The resource ID on which control packets are sent to the hypervisor. */
248c2ecf20Sopenharmony_ci#define VMCI_TRANSPORT_HYPERVISOR_PACKET_RID 15
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define VSOCK_PROTO_INVALID        0
278c2ecf20Sopenharmony_ci#define VSOCK_PROTO_PKT_ON_NOTIFY (1 << 0)
288c2ecf20Sopenharmony_ci#define VSOCK_PROTO_ALL_SUPPORTED (VSOCK_PROTO_PKT_ON_NOTIFY)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define vmci_trans(_vsk) ((struct vmci_transport *)((_vsk)->trans))
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cienum vmci_transport_packet_type {
338c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_INVALID = 0,
348c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
358c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
368c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_OFFER,
378c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
388c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_WROTE,
398c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_READ,
408c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_RST,
418c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
428c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
438c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
448c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
458c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
468c2ecf20Sopenharmony_ci	VMCI_TRANSPORT_PACKET_TYPE_MAX
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct vmci_transport_waiting_info {
508c2ecf20Sopenharmony_ci	u64 generation;
518c2ecf20Sopenharmony_ci	u64 offset;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* Control packet type for STREAM sockets.  DGRAMs have no control packets nor
558c2ecf20Sopenharmony_ci * special packet header for data packets, they are just raw VMCI DGRAM
568c2ecf20Sopenharmony_ci * messages.  For STREAMs, control packets are sent over the control channel
578c2ecf20Sopenharmony_ci * while data is written and read directly from queue pairs with no packet
588c2ecf20Sopenharmony_ci * format.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cistruct vmci_transport_packet {
618c2ecf20Sopenharmony_ci	struct vmci_datagram dg;
628c2ecf20Sopenharmony_ci	u8 version;
638c2ecf20Sopenharmony_ci	u8 type;
648c2ecf20Sopenharmony_ci	u16 proto;
658c2ecf20Sopenharmony_ci	u32 src_port;
668c2ecf20Sopenharmony_ci	u32 dst_port;
678c2ecf20Sopenharmony_ci	u32 _reserved2;
688c2ecf20Sopenharmony_ci	union {
698c2ecf20Sopenharmony_ci		u64 size;
708c2ecf20Sopenharmony_ci		u64 mode;
718c2ecf20Sopenharmony_ci		struct vmci_handle handle;
728c2ecf20Sopenharmony_ci		struct vmci_transport_waiting_info wait;
738c2ecf20Sopenharmony_ci	} u;
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistruct vmci_transport_notify_pkt {
778c2ecf20Sopenharmony_ci	u64 write_notify_window;
788c2ecf20Sopenharmony_ci	u64 write_notify_min_window;
798c2ecf20Sopenharmony_ci	bool peer_waiting_read;
808c2ecf20Sopenharmony_ci	bool peer_waiting_write;
818c2ecf20Sopenharmony_ci	bool peer_waiting_write_detected;
828c2ecf20Sopenharmony_ci	bool sent_waiting_read;
838c2ecf20Sopenharmony_ci	bool sent_waiting_write;
848c2ecf20Sopenharmony_ci	struct vmci_transport_waiting_info peer_waiting_read_info;
858c2ecf20Sopenharmony_ci	struct vmci_transport_waiting_info peer_waiting_write_info;
868c2ecf20Sopenharmony_ci	u64 produce_q_generation;
878c2ecf20Sopenharmony_ci	u64 consume_q_generation;
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistruct vmci_transport_notify_pkt_q_state {
918c2ecf20Sopenharmony_ci	u64 write_notify_window;
928c2ecf20Sopenharmony_ci	u64 write_notify_min_window;
938c2ecf20Sopenharmony_ci	bool peer_waiting_write;
948c2ecf20Sopenharmony_ci	bool peer_waiting_write_detected;
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciunion vmci_transport_notify {
988c2ecf20Sopenharmony_ci	struct vmci_transport_notify_pkt pkt;
998c2ecf20Sopenharmony_ci	struct vmci_transport_notify_pkt_q_state pkt_q_state;
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* Our transport-specific data. */
1038c2ecf20Sopenharmony_cistruct vmci_transport {
1048c2ecf20Sopenharmony_ci	/* For DGRAMs. */
1058c2ecf20Sopenharmony_ci	struct vmci_handle dg_handle;
1068c2ecf20Sopenharmony_ci	/* For STREAMs. */
1078c2ecf20Sopenharmony_ci	struct vmci_handle qp_handle;
1088c2ecf20Sopenharmony_ci	struct vmci_qp *qpair;
1098c2ecf20Sopenharmony_ci	u64 produce_size;
1108c2ecf20Sopenharmony_ci	u64 consume_size;
1118c2ecf20Sopenharmony_ci	u32 detach_sub_id;
1128c2ecf20Sopenharmony_ci	union vmci_transport_notify notify;
1138c2ecf20Sopenharmony_ci	const struct vmci_transport_notify_ops *notify_ops;
1148c2ecf20Sopenharmony_ci	struct list_head elem;
1158c2ecf20Sopenharmony_ci	struct sock *sk;
1168c2ecf20Sopenharmony_ci	spinlock_t lock; /* protects sk. */
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciint vmci_transport_register(void);
1208c2ecf20Sopenharmony_civoid vmci_transport_unregister(void);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciint vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
1238c2ecf20Sopenharmony_ci				 struct sockaddr_vm *src);
1248c2ecf20Sopenharmony_ciint vmci_transport_send_read_bh(struct sockaddr_vm *dst,
1258c2ecf20Sopenharmony_ci				struct sockaddr_vm *src);
1268c2ecf20Sopenharmony_ciint vmci_transport_send_wrote(struct sock *sk);
1278c2ecf20Sopenharmony_ciint vmci_transport_send_read(struct sock *sk);
1288c2ecf20Sopenharmony_ciint vmci_transport_send_waiting_write(struct sock *sk,
1298c2ecf20Sopenharmony_ci				      struct vmci_transport_waiting_info *wait);
1308c2ecf20Sopenharmony_ciint vmci_transport_send_waiting_read(struct sock *sk,
1318c2ecf20Sopenharmony_ci				     struct vmci_transport_waiting_info *wait);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#endif
134