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