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
27 #ifndef _VMBUS_H_
28 #define _VMBUS_H_
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/_iovec.h>
33
34 /*
35 * VMBUS version is 32 bit, upper 16 bit for major_number and lower
36 * 16 bit for minor_number.
37 *
38 * 0.13 -- Windows Server 2008
39 * 1.1 -- Windows 7
40 * 2.4 -- Windows 8
41 * 3.0 -- Windows 8.1
42 * 4.0 -- Windows 10
43 * 5.0 -- Newer Windows 10
44 */
45 #define VMBUS_VERSION_WS2008 ((0 << 16) | (13))
46 #define VMBUS_VERSION_WIN7 ((1 << 16) | (1))
47 #define VMBUS_VERSION_WIN8 ((2 << 16) | (4))
48 #define VMBUS_VERSION_WIN8_1 ((3 << 16) | (0))
49 #define VMBUS_VERSION_WIN10 ((4 << 16) | (0))
50 #define VMBUS_VERSION_WIN10_V5 ((5 << 16) | (0))
51
52 #define VMBUS_VERSION_MAJOR(ver) (((uint32_t)(ver)) >> 16)
53 #define VMBUS_VERSION_MINOR(ver) (((uint32_t)(ver)) & 0xffff)
54
55 #define VMBUS_CHAN_POLLHZ_MIN 100 /* 10ms interval */
56 #define VMBUS_CHAN_POLLHZ_MAX 1000000 /* 1us interval */
57
58 /*
59 * GPA stuffs.
60 */
61 struct vmbus_gpa_range {
62 uint32_t gpa_len;
63 uint32_t gpa_ofs;
64 uint64_t gpa_page[0];
65 } __packed;
66
67 /* This is actually vmbus_gpa_range.gpa_page[1] */
68 struct vmbus_gpa {
69 uint32_t gpa_len;
70 uint32_t gpa_ofs;
71 uint64_t gpa_page;
72 } __packed;
73
74 #define VMBUS_CHANPKT_SIZE_SHIFT 3
75
76 #define VMBUS_CHANPKT_GETLEN(pktlen) \
77 (((int)(pktlen)) << VMBUS_CHANPKT_SIZE_SHIFT)
78
79 struct vmbus_chanpkt_hdr {
80 uint16_t cph_type; /* VMBUS_CHANPKT_TYPE_ */
81 uint16_t cph_hlen; /* header len, in 8 bytes */
82 uint16_t cph_tlen; /* total len, in 8 bytes */
83 uint16_t cph_flags; /* VMBUS_CHANPKT_FLAG_ */
84 uint64_t cph_xactid;
85 } __packed;
86
87 #define VMBUS_CHANPKT_TYPE_INBAND 0x0006
88 #define VMBUS_CHANPKT_TYPE_RXBUF 0x0007
89 #define VMBUS_CHANPKT_TYPE_GPA 0x0009
90 #define VMBUS_CHANPKT_TYPE_COMP 0x000b
91
92 #define VMBUS_CHANPKT_FLAG_NONE 0
93 #define VMBUS_CHANPKT_FLAG_RC 0x0001 /* report completion */
94
95 #define VMBUS_CHANPKT_CONST_DATA(pkt) \
96 (const void *)((const uint8_t *)(pkt) + \
97 VMBUS_CHANPKT_GETLEN((pkt)->cph_hlen))
98
99 /* Include padding */
100 #define VMBUS_CHANPKT_DATALEN(pkt) \
101 (VMBUS_CHANPKT_GETLEN((pkt)->cph_tlen) -\
102 VMBUS_CHANPKT_GETLEN((pkt)->cph_hlen))
103
104 struct vmbus_rxbuf_desc {
105 uint32_t rb_len;
106 uint32_t rb_ofs;
107 } __packed;
108
109 struct vmbus_chanpkt_rxbuf {
110 struct vmbus_chanpkt_hdr cp_hdr;
111 uint16_t cp_rxbuf_id;
112 uint16_t cp_rsvd;
113 uint32_t cp_rxbuf_cnt;
114 struct vmbus_rxbuf_desc cp_rxbuf[];
115 } __packed;
116
117 struct vmbus_chan_br {
118 void *cbr;
119 bus_addr_t cbr_paddr;
120 int cbr_txsz;
121 int cbr_rxsz;
122 };
123
124 struct vmbus_channel;
125 struct vmbus_xact;
126 struct vmbus_xact_ctx;
127 struct hyperv_guid;
128 struct task;
129 struct taskqueue;
130
131 typedef void (*vmbus_chan_callback_t)(struct vmbus_channel *, void *);
132 typedef int (*vmbus_br_copy_callback_t)(void *, int, void *);
133
134 static __inline struct vmbus_channel *
vmbus_get_channel(device_t dev)135 vmbus_get_channel(device_t dev)
136 {
137 return device_get_ivars(dev);
138 }
139
140 /*
141 * vmbus_chan_open_br()
142 *
143 * Return values:
144 * 0 Succeeded.
145 * EISCONN Failed, and the memory passed through 'br' is still
146 * connected. Callers must _not_ free the memory
147 * passed through 'br', if this error happens.
148 * other values Failed. The memory passed through 'br' is no longer
149 * connected. Callers are free to do anything with the
150 * memory passed through 'br'.
151 *
152 *
153 *
154 * vmbus_chan_close_direct()
155 *
156 * NOTE:
157 * Callers of this function _must_ make sure to close all sub-channels before
158 * closing the primary channel.
159 *
160 * Return values:
161 * 0 Succeeded.
162 * EISCONN Failed, and the memory associated with the bufring
163 * is still connected. Callers must _not_ free the the
164 * memory associated with the bufring, if this error
165 * happens.
166 * other values Failed. The memory associated with the bufring is
167 * no longer connected. Callers are free to do anything
168 * with the memory associated with the bufring.
169 */
170 int vmbus_chan_open(struct vmbus_channel *chan,
171 int txbr_size, int rxbr_size, const void *udata, int udlen,
172 vmbus_chan_callback_t cb, void *cbarg);
173 int vmbus_chan_open_br(struct vmbus_channel *chan,
174 const struct vmbus_chan_br *cbr, const void *udata,
175 int udlen, vmbus_chan_callback_t cb, void *cbarg);
176 void vmbus_chan_close(struct vmbus_channel *chan);
177 int vmbus_chan_close_direct(struct vmbus_channel *chan);
178 void vmbus_chan_intr_drain(struct vmbus_channel *chan);
179 void vmbus_chan_run_task(struct vmbus_channel *chan,
180 struct task *task);
181 void vmbus_chan_set_orphan(struct vmbus_channel *chan,
182 struct vmbus_xact_ctx *);
183 void vmbus_chan_unset_orphan(struct vmbus_channel *chan);
184 const void *vmbus_chan_xact_wait(const struct vmbus_channel *chan,
185 struct vmbus_xact *xact, size_t *resp_len, bool can_sleep);
186
187 int vmbus_chan_gpadl_connect(struct vmbus_channel *chan,
188 bus_addr_t paddr, int size, uint32_t *gpadl);
189 int vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan,
190 uint32_t gpadl);
191
192 void vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu);
193 void vmbus_chan_cpu_rr(struct vmbus_channel *chan);
194 void vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on);
195
196 struct vmbus_channel **
197 vmbus_subchan_get(struct vmbus_channel *pri_chan,
198 int subchan_cnt);
199 void vmbus_subchan_rel(struct vmbus_channel **subchan,
200 int subchan_cnt);
201 void vmbus_subchan_drain(struct vmbus_channel *pri_chan);
202
203 int vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen,
204 uint64_t *xactid);
205 int vmbus_chan_recv_pkt(struct vmbus_channel *chan,
206 struct vmbus_chanpkt_hdr *pkt, int *pktlen);
207
208 int vmbus_chan_recv_idxadv(struct vmbus_channel *chan,
209 uint32_t advance);
210 int vmbus_chan_recv_peek(struct vmbus_channel *chan,
211 void *data, int data_len, uint32_t advance);
212 int vmbus_chan_recv_peek_call(struct vmbus_channel *chan,
213 int data_len, uint32_t skip,
214 vmbus_br_copy_callback_t cb, void *cbarg);
215
216 int vmbus_chan_send(struct vmbus_channel *chan, uint16_t type,
217 uint16_t flags, void *data, int dlen, uint64_t xactid);
218 int vmbus_chan_send_sglist(struct vmbus_channel *chan,
219 struct vmbus_gpa sg[], int sglen, void *data, int dlen,
220 uint64_t xactid);
221 int vmbus_chan_send_prplist(struct vmbus_channel *chan,
222 struct vmbus_gpa_range *prp, int prp_cnt, void *data,
223 int dlen, uint64_t xactid);
224 int vmbus_chan_iov_send(struct vmbus_channel *chan,
225 const struct iovec iov[], int iovlen,
226 vmbus_br_copy_callback_t cb, void *cbarg);
227 uint32_t vmbus_chan_write_available(struct vmbus_channel *chan);
228 uint32_t vmbus_chan_read_available(struct vmbus_channel *chan);
229 bool vmbus_chan_write_signal(struct vmbus_channel *chan,
230 int32_t min_signal_size);
231 void vmbus_chan_set_pending_send_size(struct vmbus_channel *chan,
232 uint32_t size);
233
234 uint32_t vmbus_chan_id(const struct vmbus_channel *chan);
235 uint32_t vmbus_chan_subidx(const struct vmbus_channel *chan);
236 bool vmbus_chan_is_primary(const struct vmbus_channel *chan);
237 bool vmbus_chan_is_revoked(const struct vmbus_channel *chan);
238 bool vmbus_chan_is_hvs(const struct vmbus_channel *chan);
239 bool vmbus_chan_is_hvs_conn_from_host(
240 const struct vmbus_channel *chan);
241 int vmbus_req_tl_connect(struct hyperv_guid *,
242 struct hyperv_guid *);
243
244 struct hyperv_guid *
245 vmbus_chan_guid_type(struct vmbus_channel *chan);
246 struct hyperv_guid *
247 vmbus_chan_guid_inst(struct vmbus_channel *chan);
248 int vmbus_chan_prplist_nelem(int br_size, int prpcnt_max,
249 int dlen_max);
250 bool vmbus_chan_rx_empty(const struct vmbus_channel *chan);
251 bool vmbus_chan_tx_empty(const struct vmbus_channel *chan);
252 struct taskqueue *
253 vmbus_chan_mgmt_tq(const struct vmbus_channel *chan);
254
255 void vmbus_chan_poll_enable(struct vmbus_channel *chan,
256 u_int pollhz);
257 void vmbus_chan_poll_disable(struct vmbus_channel *chan);
258
259 #endif /* !_VMBUS_H_ */
260