xref: /aosp_15_r20/external/wmediumd/wmediumd/inc/usfstl/vhost.h (revision 621120a22a0cd8ba80b131fe8bcb37c86ff453e3)
1 /*
2  * Copyright (C) 2019 - 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #ifndef _USFSTL_VHOST_H_
7 #define _USFSTL_VHOST_H_
8 
9 #include "list.h"
10 #include "sched.h"
11 #include "schedctrl.h"
12 #include "vhostproto.h"
13 
14 struct usfstl_vhost_user_buf {
15 	unsigned int n_in_sg, n_out_sg;
16 	struct iovec *in_sg, *out_sg;
17 	size_t written;
18 	unsigned int idx;
19 	bool allocated;
20 };
21 
22 struct usfstl_vhost_user_dev {
23 	uint64_t features, protocol_features;
24 	struct usfstl_vhost_user_server *server;
25 	void *data;
26 };
27 
28 struct usfstl_vhost_user_ops {
29 	void (*connected)(struct usfstl_vhost_user_dev *dev);
30 	void (*handle)(struct usfstl_vhost_user_dev *dev,
31 		       struct usfstl_vhost_user_buf *buf,
32 		       unsigned int vring);
33 	void (*disconnected)(struct usfstl_vhost_user_dev *dev);
34 	// Called to initiate a snapshot or restore. Implementor should spawn a
35 	// thread that reads or writes state to `fd` and then closes `fd`.
36 	//
37 	// Attempting to read/write to `fd` directly from `start_data_transfer`
38 	// will likely cause a deadlock.
39 	//
40 	// `transfer_direction == 0` => snapshot (should write to the fd)
41 	// `transfer_direction == 1` => restore (should read from the fd)
42 	void (*start_data_transfer)(struct usfstl_vhost_user_dev *dev, uint32_t transfer_direction, int fd);
43 	// Called to check if the work spawn by `start_data_transfer`
44 	// succeeded. Should block until the work completes and abort if
45 	// something went wrong.
46 	void (*check_data_transfer)(struct usfstl_vhost_user_dev *dev);
47 };
48 
49 /**
50  * struct usfstl_vhost_user_server: vhost-user device server
51  */
52 struct usfstl_vhost_user_server {
53 	/**
54 	 * @ops: various ops for the server/devices
55 	 */
56 	const struct usfstl_vhost_user_ops *ops;
57 
58 	/**
59 	 * @name: socket name to use
60 	 */
61 	char *socket;
62 
63 	/**
64 	 * @interrupt_latency: interrupt latency to model, in
65 	 *	scheduler ticks (actual time then depends on
66 	 *	the scheduler unit)
67 	 */
68 	unsigned int interrupt_latency;
69 
70 	/**
71 	 * @max_queues: max number of virt queues supported
72 	 */
73 	unsigned int max_queues;
74 
75 	/**
76 	 * @input_queues: bitmap of input queues (where to handle interrupts)
77 	 */
78 	uint64_t input_queues;
79 
80 	/**
81 	 * @scheduler: the scheduler to integrate with,
82 	 *	may be %NULL
83 	 */
84 	struct usfstl_scheduler *scheduler;
85 
86 	/**
87 	 * @ctrl: external scheduler control to integrate with,
88 	 *	may be %NULL
89 	 */
90 	struct usfstl_sched_ctrl *ctrl;
91 
92 	/**
93 	 * @features: user features
94 	 */
95 	uint64_t features;
96 
97 	/**
98 	 * @protocol_features: protocol features
99 	 */
100 	uint64_t protocol_features;
101 
102 	/**
103 	 * @config: config data, if supported
104 	 */
105 	const void *config;
106 
107 	/**
108 	 * @config_len: length of config data
109 	 */
110 	size_t config_len;
111 
112 	/**
113 	 * @data: arbitrary user data
114 	 */
115 	void *data;
116 };
117 
118 /**
119  * usfstl_vhost_user_server_start - start the server
120  */
121 void usfstl_vhost_user_server_start(struct usfstl_vhost_user_server *server);
122 
123 /**
124  * usfstl_vhost_user_server_stop - stop the server
125  *
126  * Note that this doesn't stop the existing devices, and it thus
127  * may be used from the connected callback, if only one connection
128  * is allowed.
129  */
130 void usfstl_vhost_user_server_stop(struct usfstl_vhost_user_server *server);
131 
132 /**
133  * usfstl_vhost_user_dev_notify - send a message on a vring
134  * @dev: device to send to
135  * @vring: vring index to send on
136  * @buf: buffer to send
137  * @buflen: length of the buffer
138  */
139 void usfstl_vhost_user_dev_notify(struct usfstl_vhost_user_dev *dev,
140 				  unsigned int vring,
141 				  const uint8_t *buf, size_t buflen);
142 
143 /**
144  * usfstl_vhost_user_config_changed - notify host of a config change event
145  * @dev: device to send to
146  */
147 void usfstl_vhost_user_config_changed(struct usfstl_vhost_user_dev *dev);
148 
149 /**
150  * usfstl_vhost_user_to_va - translate address
151  * @dev: device to translate address for
152  * @addr: guest-side virtual addr
153  */
154 void *usfstl_vhost_user_to_va(struct usfstl_vhost_user_dev *dev, uint64_t addr);
155 
156 /**
157  * usfstl_vhost_phys_to_va - translate address
158  * @dev: device to translate address for
159  * @addr: guest-side physical addr
160  */
161 void *usfstl_vhost_phys_to_va(struct usfstl_vhost_user_dev *dev, uint64_t addr);
162 
163 /* also some IOV helpers */
164 size_t iov_len(struct iovec *sg, unsigned int nsg);
165 size_t iov_fill(struct iovec *sg, unsigned int nsg,
166 		const void *buf, size_t buflen);
167 size_t iov_read(void *buf, size_t buflen,
168 		struct iovec *sg, unsigned int nsg);
169 
170 #endif // _USFSTL_VHOST_H_
171