xref: /netbsd-src/sys/dev/hyperv/vmbusvar.h (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: vmbusvar.h,v 1.4 2019/12/10 12:20:20 nonaka Exp $	*/
2 /*	$OpenBSD: hypervvar.h,v 1.13 2017/06/23 19:05:42 mikeb Exp $	*/
3 
4 /*
5  * Copyright (c) 2016 Mike Belopuhov <mike@esdenera.com>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _VMBUSVAR_H_
21 #define _VMBUSVAR_H_
22 
23 #include <sys/param.h>
24 #include <sys/device.h>
25 #include <sys/atomic.h>
26 #include <sys/bus.h>
27 #include <sys/condvar.h>
28 #include <sys/evcnt.h>
29 #include <sys/kcpuset.h>
30 #include <sys/mutex.h>
31 #include <sys/pool.h>
32 
33 #include <dev/hyperv/hypervreg.h>
34 #include <dev/hyperv/hypervvar.h>
35 
36 /* #define HYPERV_DEBUG */
37 
38 #ifdef HYPERV_DEBUG
39 #define DPRINTF(x...)		printf(x)
40 #else
41 #define DPRINTF(x...)
42 #endif
43 
44 typedef void (*vmbus_channel_callback_t)(void *);
45 
46 struct vmbus_softc;
47 struct vmbus_channel;
48 
49 struct vmbus_msg {
50 	uint64_t			msg_flags;
51 #define  MSGF_NOSLEEP			__BIT(0)
52 #define  MSGF_NOQUEUE			__BIT(1)
53 #define  MSGF_ORPHANED			__BIT(2)
54 	struct hyperv_hypercall_postmsg_in msg_req __aligned(8);
55 	void				*msg_rsp;
56 	size_t				msg_rsplen;
57 	TAILQ_ENTRY(vmbus_msg)		msg_entry;
58 };
59 __CTASSERT((offsetof(struct vmbus_msg, msg_req) % 8) == 0);
60 TAILQ_HEAD(vmbus_queue, vmbus_msg);
61 
62 struct vmbus_dev {
63 	int					vd_type;
64 #define VMBUS_DEV_TYPE_ATTACH			0
65 #define VMBUS_DEV_TYPE_DETACH			1
66 	struct vmbus_channel			*vd_chan;
67 	SIMPLEQ_ENTRY(vmbus_dev)		vd_entry;
68 };
69 SIMPLEQ_HEAD(vmbus_devq, vmbus_dev);
70 
71 struct vmbus_ring_data {
72 	struct vmbus_bufring		*rd_ring;
73 	uint32_t			rd_size;
74 	kmutex_t			rd_lock;
75 	uint32_t			rd_prod;
76 	uint32_t			rd_cons;
77 	uint32_t			rd_dsize;
78 };
79 
80 TAILQ_HEAD(vmbus_channels, vmbus_channel);
81 
82 struct vmbus_channel {
83 	struct vmbus_softc		*ch_sc;
84 	device_t			ch_dev;
85 	u_int				ch_refs;
86 
87 	int				ch_state;
88 #define  VMBUS_CHANSTATE_OFFERED	1
89 #define  VMBUS_CHANSTATE_OPENED		2
90 #define  VMBUS_CHANSTATE_CLOSING	3
91 #define  VMBUS_CHANSTATE_CLOSED		4
92 	uint32_t			ch_id;
93 	uint16_t			ch_subidx;
94 
95 	struct hyperv_guid		ch_type;
96 	struct hyperv_guid		ch_inst;
97 	char				ch_ident[38];
98 
99 	void				*ch_ring;
100 	uint32_t			ch_ring_gpadl;
101 	u_long				ch_ring_size;
102 	struct hyperv_dma		ch_ring_dma;
103 
104 	struct vmbus_ring_data		ch_wrd;
105 	struct vmbus_ring_data		ch_rrd;
106 
107 	int				ch_cpuid;
108 	uint32_t			ch_vcpu;
109 
110 	void				(*ch_handler)(void *);
111 	void				*ch_ctx;
112 	struct evcnt			ch_evcnt;
113 	void				*ch_taskq;
114 
115 	uint32_t			ch_flags;
116 #define  CHF_BATCHED			__BIT(0)
117 #define  CHF_MONITOR			__BIT(1)
118 #define  CHF_REVOKED			__BIT(2)
119 
120 	uint8_t				ch_mgroup;
121 	uint8_t				ch_mindex;
122 	struct hyperv_mon_param		*ch_monprm;
123 	struct hyperv_dma		ch_monprm_dma;
124 
125 	TAILQ_ENTRY(vmbus_channel)	ch_entry;
126 
127 	kmutex_t			ch_subchannel_lock;
128 	struct vmbus_channels		ch_subchannels;
129 	u_int				ch_subchannel_count;
130 	TAILQ_ENTRY(vmbus_channel)	ch_subentry;
131 	struct vmbus_channel		*ch_primary_channel;
132 };
133 
134 #define VMBUS_CHAN_ISPRIMARY(chan)	((chan)->ch_subidx == 0)
135 
136 struct vmbus_attach_args {
137 	struct hyperv_guid		*aa_type;
138 	struct hyperv_guid		*aa_inst;
139 	char				*aa_ident;
140 	struct vmbus_channel		*aa_chan;
141 	bus_space_tag_t			aa_iot;
142 	bus_space_tag_t			aa_memt;
143 };
144 
145 struct vmbus_percpu_data {
146 	void			*simp;	/* Synthetic Interrupt Message Page */
147 	void			*siep;	/* Synthetic Interrupt Event Flags Page */
148 
149 	/* Rarely used fields */
150 	struct hyperv_dma	simp_dma;
151 	struct hyperv_dma	siep_dma;
152 } __aligned(CACHE_LINE_SIZE);
153 
154 struct vmbus_softc {
155 	device_t		sc_dev;
156 	bus_space_tag_t		sc_iot;
157 	bus_space_tag_t		sc_memt;
158 	bus_dma_tag_t		sc_dmat;
159 
160 	pool_cache_t		sc_msgpool;
161 
162 	void			*sc_msg_sih;
163 
164 	u_long			*sc_wevents;	/* Write events */
165 	u_long			*sc_revents;	/* Read events */
166 	struct vmbus_mnf	*sc_monitor[2];
167 	struct vmbus_percpu_data sc_percpu[MAXCPUS];
168 
169 	/*
170 	 * Rarely used fields
171 	 */
172 	uint32_t		sc_flags;
173 #define  VMBUS_SCFLAG_SYNIC		__BIT(0)
174 #define  VMBUS_SCFLAG_CONNECTED		__BIT(1)
175 #define  VMBUS_SCFLAG_OFFERS_DELIVERED	__BIT(2)
176 	uint32_t		sc_proto;
177 	int			sc_channel_max;
178 
179 	kcpuset_t		*sc_intr_cpuset;
180 
181 	/* Shared memory for Write/Read events */
182 	void			*sc_events;
183 	struct hyperv_dma	sc_events_dma;
184 
185 	struct hyperv_dma	sc_monitor_dma[2];
186 
187 	struct vmbus_queue 	sc_reqs;	/* Request queue */
188 	kmutex_t		sc_req_lock;
189 	struct vmbus_queue 	sc_rsps;	/* Response queue */
190 	kmutex_t		sc_rsp_lock;
191 
192 	struct vmbus_devq	sc_devq;
193 	kmutex_t		sc_devq_lock;
194 	kcondvar_t		sc_devq_cv;
195 
196 	struct vmbus_channels	sc_channels;
197 	kmutex_t		sc_channel_lock;
198 
199 	volatile uint32_t	sc_handle;
200 };
201 
202 static __inline void
203 clear_bit(u_int b, volatile void *p)
204 {
205 	atomic_and_32(((volatile u_int *)p) + (b >> 5), ~(1 << (b & 0x1f)));
206 }
207 
208 static __inline void
209 set_bit(u_int b, volatile void *p)
210 {
211 	atomic_or_32(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f));
212 }
213 
214 static __inline int
215 test_bit(u_int b, volatile void *p)
216 {
217 	return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f)));
218 }
219 
220 extern const struct hyperv_guid hyperv_guid_network;
221 extern const struct hyperv_guid hyperv_guid_ide;
222 extern const struct hyperv_guid hyperv_guid_scsi;
223 extern const struct hyperv_guid hyperv_guid_shutdown;
224 extern const struct hyperv_guid hyperv_guid_timesync;
225 extern const struct hyperv_guid hyperv_guid_heartbeat;
226 extern const struct hyperv_guid hyperv_guid_kvp;
227 extern const struct hyperv_guid hyperv_guid_vss;
228 extern const struct hyperv_guid hyperv_guid_dynmem;
229 extern const struct hyperv_guid hyperv_guid_mouse;
230 extern const struct hyperv_guid hyperv_guid_kbd;
231 extern const struct hyperv_guid hyperv_guid_video;
232 extern const struct hyperv_guid hyperv_guid_fc;
233 extern const struct hyperv_guid hyperv_guid_fcopy;
234 extern const struct hyperv_guid hyperv_guid_pcie;
235 extern const struct hyperv_guid hyperv_guid_netdir;
236 extern const struct hyperv_guid hyperv_guid_rdesktop;
237 extern const struct hyperv_guid hyperv_guid_avma1;
238 extern const struct hyperv_guid hyperv_guid_avma2;
239 extern const struct hyperv_guid hyperv_guid_avma3;
240 extern const struct hyperv_guid hyperv_guid_avma4;
241 
242 int	vmbus_match(device_t, cfdata_t, void *);
243 int	vmbus_attach(struct vmbus_softc *);
244 int	vmbus_detach(struct vmbus_softc *, int);
245 
246 int	vmbus_handle_alloc(struct vmbus_channel *, const struct hyperv_dma *,
247 	    uint32_t, uint32_t *);
248 void	vmbus_handle_free(struct vmbus_channel *, uint32_t);
249 int	vmbus_channel_open(struct vmbus_channel *, size_t, void *, size_t,
250 	    vmbus_channel_callback_t, void *);
251 int	vmbus_channel_close(struct vmbus_channel *);
252 int	vmbus_channel_close_direct(struct vmbus_channel *);
253 int	vmbus_channel_setdeferred(struct vmbus_channel *, const char *);
254 void	vmbus_channel_schedule(struct vmbus_channel *);
255 int	vmbus_channel_send(struct vmbus_channel *, void *, uint32_t, uint64_t,
256 	    int, uint32_t);
257 int	vmbus_channel_send_sgl(struct vmbus_channel *, struct vmbus_gpa *,
258 	    uint32_t, void *, uint32_t, uint64_t);
259 int	vmbus_channel_send_prpl(struct vmbus_channel *,
260 	    struct vmbus_gpa_range *, uint32_t, void *, uint32_t, uint64_t);
261 int	vmbus_channel_recv(struct vmbus_channel *, void *, uint32_t, uint32_t *,
262 	    uint64_t *, int);
263 void	vmbus_channel_cpu_set(struct vmbus_channel *, int);
264 void	vmbus_channel_cpu_rr(struct vmbus_channel *);
265 bool	vmbus_channel_is_revoked(struct vmbus_channel *);
266 
267 struct vmbus_channel **
268 	vmbus_subchannel_get(struct vmbus_channel *, int);
269 void	vmbus_subchannel_put(struct vmbus_channel **, int);
270 
271 #endif	/* _VMBUSVAR_H_ */
272