xref: /freebsd/sys/dev/hyperv/include/vmbus.h (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * Copyright (c) 2016 Microsoft Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _VMBUS_H_
30 #define _VMBUS_H_
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/_iovec.h>
35 
36 /*
37  * VMBUS version is 32 bit, upper 16 bit for major_number and lower
38  * 16 bit for minor_number.
39  *
40  * 0.13  --  Windows Server 2008
41  * 1.1   --  Windows 7
42  * 2.4   --  Windows 8
43  * 3.0   --  Windows 8.1
44  * 4.0   --  Windows 10
45  * 5.0   --  Newer Windows 10
46  */
47 #define VMBUS_VERSION_WS2008		((0 << 16) | (13))
48 #define VMBUS_VERSION_WIN7		((1 << 16) | (1))
49 #define VMBUS_VERSION_WIN8		((2 << 16) | (4))
50 #define VMBUS_VERSION_WIN8_1		((3 << 16) | (0))
51 #define VMBUS_VERSION_WIN10		((4 << 16) | (0))
52 #define VMBUS_VERSION_WIN10_V5		((5 << 16) | (0))
53 
54 #define VMBUS_VERSION_MAJOR(ver)	(((uint32_t)(ver)) >> 16)
55 #define VMBUS_VERSION_MINOR(ver)	(((uint32_t)(ver)) & 0xffff)
56 
57 #define VMBUS_CHAN_POLLHZ_MIN		100	/* 10ms interval */
58 #define VMBUS_CHAN_POLLHZ_MAX		1000000	/* 1us interval */
59 
60 /*
61  * GPA stuffs.
62  */
63 struct vmbus_gpa_range {
64 	uint32_t	gpa_len;
65 	uint32_t	gpa_ofs;
66 	uint64_t	gpa_page[0];
67 } __packed;
68 
69 /* This is actually vmbus_gpa_range.gpa_page[1] */
70 struct vmbus_gpa {
71 	uint32_t	gpa_len;
72 	uint32_t	gpa_ofs;
73 	uint64_t	gpa_page;
74 } __packed;
75 
76 #define VMBUS_CHANPKT_SIZE_SHIFT	3
77 
78 #define VMBUS_CHANPKT_GETLEN(pktlen)	\
79 	(((int)(pktlen)) << VMBUS_CHANPKT_SIZE_SHIFT)
80 
81 struct vmbus_chanpkt_hdr {
82 	uint16_t	cph_type;	/* VMBUS_CHANPKT_TYPE_ */
83 	uint16_t	cph_hlen;	/* header len, in 8 bytes */
84 	uint16_t	cph_tlen;	/* total len, in 8 bytes */
85 	uint16_t	cph_flags;	/* VMBUS_CHANPKT_FLAG_ */
86 	uint64_t	cph_xactid;
87 } __packed;
88 
89 #define VMBUS_CHANPKT_TYPE_INBAND	0x0006
90 #define VMBUS_CHANPKT_TYPE_RXBUF	0x0007
91 #define VMBUS_CHANPKT_TYPE_GPA		0x0009
92 #define VMBUS_CHANPKT_TYPE_COMP		0x000b
93 
94 #define VMBUS_CHANPKT_FLAG_NONE		0
95 #define VMBUS_CHANPKT_FLAG_RC		0x0001	/* report completion */
96 
97 #define VMBUS_CHANPKT_CONST_DATA(pkt)		\
98 	(const void *)((const uint8_t *)(pkt) +	\
99 	VMBUS_CHANPKT_GETLEN((pkt)->cph_hlen))
100 
101 /* Include padding */
102 #define VMBUS_CHANPKT_DATALEN(pkt)		\
103 	(VMBUS_CHANPKT_GETLEN((pkt)->cph_tlen) -\
104 	 VMBUS_CHANPKT_GETLEN((pkt)->cph_hlen))
105 
106 struct vmbus_rxbuf_desc {
107 	uint32_t	rb_len;
108 	uint32_t	rb_ofs;
109 } __packed;
110 
111 struct vmbus_chanpkt_rxbuf {
112 	struct vmbus_chanpkt_hdr cp_hdr;
113 	uint16_t	cp_rxbuf_id;
114 	uint16_t	cp_rsvd;
115 	uint32_t	cp_rxbuf_cnt;
116 	struct vmbus_rxbuf_desc cp_rxbuf[];
117 } __packed;
118 
119 struct vmbus_chan_br {
120 	void		*cbr;
121 	bus_addr_t	cbr_paddr;
122 	int		cbr_txsz;
123 	int		cbr_rxsz;
124 };
125 
126 struct vmbus_channel;
127 struct vmbus_xact;
128 struct vmbus_xact_ctx;
129 struct hyperv_guid;
130 struct task;
131 struct taskqueue;
132 
133 typedef void	(*vmbus_chan_callback_t)(struct vmbus_channel *, void *);
134 typedef int	(*vmbus_br_copy_callback_t)(void *, int, void *);
135 
136 static __inline struct vmbus_channel *
137 vmbus_get_channel(device_t dev)
138 {
139 	return device_get_ivars(dev);
140 }
141 
142 /*
143  * vmbus_chan_open_br()
144  *
145  * Return values:
146  * 0			Succeeded.
147  * EISCONN		Failed, and the memory passed through 'br' is still
148  *			connected.  Callers must _not_ free the memory
149  *			passed through 'br', if this error happens.
150  * other values		Failed.  The memory passed through 'br' is no longer
151  *			connected.  Callers are free to do anything with the
152  *			memory passed through 'br'.
153  *
154  *
155  *
156  * vmbus_chan_close_direct()
157  *
158  * NOTE:
159  * Callers of this function _must_ make sure to close all sub-channels before
160  * closing the primary channel.
161  *
162  * Return values:
163  * 0			Succeeded.
164  * EISCONN		Failed, and the memory associated with the bufring
165  *			is still connected.  Callers must _not_ free the the
166  *			memory associated with the bufring, if this error
167  *			happens.
168  * other values		Failed.  The memory associated with the bufring is
169  *			no longer connected.  Callers are free to do anything
170  *			with the memory associated with the bufring.
171  */
172 int		vmbus_chan_open(struct vmbus_channel *chan,
173 		    int txbr_size, int rxbr_size, const void *udata, int udlen,
174 		    vmbus_chan_callback_t cb, void *cbarg);
175 int		vmbus_chan_open_br(struct vmbus_channel *chan,
176 		    const struct vmbus_chan_br *cbr, const void *udata,
177 		    int udlen, vmbus_chan_callback_t cb, void *cbarg);
178 void		vmbus_chan_close(struct vmbus_channel *chan);
179 int		vmbus_chan_close_direct(struct vmbus_channel *chan);
180 void		vmbus_chan_intr_drain(struct vmbus_channel *chan);
181 void		vmbus_chan_run_task(struct vmbus_channel *chan,
182 		    struct task *task);
183 void		vmbus_chan_set_orphan(struct vmbus_channel *chan,
184 		    struct vmbus_xact_ctx *);
185 void		vmbus_chan_unset_orphan(struct vmbus_channel *chan);
186 const void	*vmbus_chan_xact_wait(const struct vmbus_channel *chan,
187 		    struct vmbus_xact *xact, size_t *resp_len, bool can_sleep);
188 
189 int		vmbus_chan_gpadl_connect(struct vmbus_channel *chan,
190 		    bus_addr_t paddr, int size, uint32_t *gpadl);
191 int		vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan,
192 		    uint32_t gpadl);
193 
194 void		vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu);
195 void		vmbus_chan_cpu_rr(struct vmbus_channel *chan);
196 void		vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on);
197 
198 struct vmbus_channel **
199 		vmbus_subchan_get(struct vmbus_channel *pri_chan,
200 		    int subchan_cnt);
201 void		vmbus_subchan_rel(struct vmbus_channel **subchan,
202 		    int subchan_cnt);
203 void		vmbus_subchan_drain(struct vmbus_channel *pri_chan);
204 
205 int		vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen,
206 		    uint64_t *xactid);
207 int		vmbus_chan_recv_pkt(struct vmbus_channel *chan,
208 		    struct vmbus_chanpkt_hdr *pkt, int *pktlen);
209 
210 int		vmbus_chan_recv_idxadv(struct vmbus_channel *chan,
211 		    uint32_t advance);
212 int		vmbus_chan_recv_peek(struct vmbus_channel *chan,
213 		    void *data, int data_len, uint32_t advance);
214 int		vmbus_chan_recv_peek_call(struct vmbus_channel *chan,
215 		    int data_len, uint32_t skip,
216 		    vmbus_br_copy_callback_t cb, void *cbarg);
217 
218 int		vmbus_chan_send(struct vmbus_channel *chan, uint16_t type,
219 		    uint16_t flags, void *data, int dlen, uint64_t xactid);
220 int		vmbus_chan_send_sglist(struct vmbus_channel *chan,
221 		    struct vmbus_gpa sg[], int sglen, void *data, int dlen,
222 		    uint64_t xactid);
223 int		vmbus_chan_send_prplist(struct vmbus_channel *chan,
224 		    struct vmbus_gpa_range *prp, int prp_cnt, void *data,
225 		    int dlen, uint64_t xactid);
226 int		vmbus_chan_iov_send(struct vmbus_channel *chan,
227 		    const struct iovec iov[], int iovlen,
228 		    vmbus_br_copy_callback_t cb, void *cbarg);
229 uint32_t	vmbus_chan_write_available(struct vmbus_channel *chan);
230 uint32_t	vmbus_chan_read_available(struct vmbus_channel *chan);
231 bool		vmbus_chan_write_signal(struct vmbus_channel *chan,
232 		    int32_t min_signal_size);
233 void		vmbus_chan_set_pending_send_size(struct vmbus_channel *chan,
234 		    uint32_t size);
235 
236 uint32_t	vmbus_chan_id(const struct vmbus_channel *chan);
237 uint32_t	vmbus_chan_subidx(const struct vmbus_channel *chan);
238 bool		vmbus_chan_is_primary(const struct vmbus_channel *chan);
239 bool		vmbus_chan_is_revoked(const struct vmbus_channel *chan);
240 bool		vmbus_chan_is_hvs(const struct vmbus_channel *chan);
241 bool		vmbus_chan_is_hvs_conn_from_host(
242 		    const struct vmbus_channel *chan);
243 int		vmbus_req_tl_connect(struct hyperv_guid *,
244 		    struct hyperv_guid *);
245 
246 struct hyperv_guid *
247 		vmbus_chan_guid_type(struct vmbus_channel *chan);
248 struct hyperv_guid *
249 		vmbus_chan_guid_inst(struct vmbus_channel *chan);
250 int		vmbus_chan_prplist_nelem(int br_size, int prpcnt_max,
251 		    int dlen_max);
252 bool		vmbus_chan_rx_empty(const struct vmbus_channel *chan);
253 bool		vmbus_chan_tx_empty(const struct vmbus_channel *chan);
254 struct taskqueue *
255 		vmbus_chan_mgmt_tq(const struct vmbus_channel *chan);
256 
257 void		vmbus_chan_poll_enable(struct vmbus_channel *chan,
258 		    u_int pollhz);
259 void		vmbus_chan_poll_disable(struct vmbus_channel *chan);
260 
261 #endif	/* !_VMBUS_H_ */
262