2 * Copyright (c) 2014,2016 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34 #include <sys/sysctl.h>
36 #include <dev/hyperv/include/hyperv.h>
37 #include <dev/hyperv/include/vmbus.h>
38 #include <dev/hyperv/utilities/vmbus_icreg.h>
39 #include <dev/hyperv/utilities/vmbus_icvar.h>
43 #define VMBUS_IC_BRSIZE (4 * PAGE_SIZE)
45 #define VMBUS_IC_VERCNT 2
46 #define VMBUS_IC_NEGOSZ \
47 __offsetof(struct vmbus_icmsg_negotiate, ic_ver[VMBUS_IC_VERCNT])
48 CTASSERT(VMBUS_IC_NEGOSZ < VMBUS_IC_BRSIZE);
50 static int vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS);
51 static int vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS);
54 vmbus_ic_negomsg(struct vmbus_ic_softc *sc, void *data, int *dlen0,
55 uint32_t fw_ver, uint32_t msg_ver)
57 struct vmbus_icmsg_negotiate *nego;
58 int i, cnt, dlen = *dlen0, error;
59 uint32_t sel_fw_ver, sel_msg_ver;
60 bool has_fw_ver, has_msg_ver;
63 * Preliminary message verification.
65 if (dlen < sizeof(*nego)) {
66 device_printf(sc->ic_dev, "truncated ic negotiate, len %d\n",
72 if (nego->ic_fwver_cnt == 0) {
73 device_printf(sc->ic_dev, "ic negotiate does not contain "
74 "framework version %u\n", nego->ic_fwver_cnt);
77 if (nego->ic_msgver_cnt == 0) {
78 device_printf(sc->ic_dev, "ic negotiate does not contain "
79 "message version %u\n", nego->ic_msgver_cnt);
83 cnt = nego->ic_fwver_cnt + nego->ic_msgver_cnt;
84 if (dlen < __offsetof(struct vmbus_icmsg_negotiate, ic_ver[cnt])) {
85 device_printf(sc->ic_dev, "ic negotiate does not contain "
86 "versions %d\n", dlen);
93 * Find the best match framework version.
96 for (i = 0; i < nego->ic_fwver_cnt; ++i) {
97 if (VMBUS_ICVER_LE(nego->ic_ver[i], fw_ver)) {
99 sel_fw_ver = nego->ic_ver[i];
101 } else if (VMBUS_ICVER_GT(nego->ic_ver[i],
103 sel_fw_ver = nego->ic_ver[i];
108 device_printf(sc->ic_dev, "failed to select framework "
114 * Fine the best match message version.
117 for (i = nego->ic_fwver_cnt;
118 i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; ++i) {
119 if (VMBUS_ICVER_LE(nego->ic_ver[i], msg_ver)) {
121 sel_msg_ver = nego->ic_ver[i];
123 } else if (VMBUS_ICVER_GT(nego->ic_ver[i],
125 sel_msg_ver = nego->ic_ver[i];
130 device_printf(sc->ic_dev, "failed to select message "
137 if (bootverbose || !has_fw_ver || !has_msg_ver) {
139 device_printf(sc->ic_dev, "sel framework version: "
141 VMBUS_ICVER_MAJOR(sel_fw_ver),
142 VMBUS_ICVER_MINOR(sel_fw_ver));
144 for (i = 0; i < nego->ic_fwver_cnt; i++) {
145 device_printf(sc->ic_dev, "supp framework version: "
147 VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
148 VMBUS_ICVER_MINOR(nego->ic_ver[i]));
152 device_printf(sc->ic_dev, "sel message version: "
154 VMBUS_ICVER_MAJOR(sel_msg_ver),
155 VMBUS_ICVER_MINOR(sel_msg_ver));
157 for (i = nego->ic_fwver_cnt;
158 i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; i++) {
159 device_printf(sc->ic_dev, "supp message version: "
161 VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
162 VMBUS_ICVER_MINOR(nego->ic_ver[i]));
168 /* Record the selected versions. */
169 sc->ic_fwver = sel_fw_ver;
170 sc->ic_msgver = sel_msg_ver;
172 /* One framework version. */
173 nego->ic_fwver_cnt = 1;
174 nego->ic_ver[0] = sel_fw_ver;
176 /* One message version. */
177 nego->ic_msgver_cnt = 1;
178 nego->ic_ver[1] = sel_msg_ver;
180 /* Update data size. */
181 nego->ic_hdr.ic_dsize = VMBUS_IC_NEGOSZ -
182 sizeof(struct vmbus_icmsg_hdr);
184 /* Update total size, if necessary. */
185 if (dlen < VMBUS_IC_NEGOSZ)
186 *dlen0 = VMBUS_IC_NEGOSZ;
192 vmbus_ic_probe(device_t dev, const struct vmbus_ic_desc descs[])
194 device_t bus = device_get_parent(dev);
195 const struct vmbus_ic_desc *d;
197 if (resource_disabled(device_get_name(dev), 0))
200 for (d = descs; d->ic_desc != NULL; ++d) {
201 if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) {
202 device_set_desc(dev, d->ic_desc);
203 return (BUS_PROBE_DEFAULT);
210 vmbus_ic_attach(device_t dev, vmbus_chan_callback_t cb)
212 struct vmbus_ic_softc *sc = device_get_softc(dev);
213 struct vmbus_channel *chan = vmbus_get_channel(dev);
214 struct sysctl_oid_list *child;
215 struct sysctl_ctx_list *ctx;
219 sc->ic_buflen = VMBUS_IC_BRSIZE;
220 sc->ic_buf = malloc(VMBUS_IC_BRSIZE, M_DEVBUF, M_WAITOK | M_ZERO);
223 * These services are not performance critical and do not need
224 * batched reading. Furthermore, some services such as KVP can
225 * only handle one message from the host at a time.
226 * Turn off batched reading for all util drivers before we open the
229 vmbus_chan_set_readbatch(chan, false);
231 error = vmbus_chan_open(chan, VMBUS_IC_BRSIZE, VMBUS_IC_BRSIZE, NULL, 0,
234 free(sc->ic_buf, M_DEVBUF);
238 ctx = device_get_sysctl_ctx(dev);
239 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
240 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "fw_version",
241 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
242 vmbus_ic_fwver_sysctl, "A", "framework version");
243 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "msg_version",
244 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
245 vmbus_ic_msgver_sysctl, "A", "message version");
251 vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS)
253 struct vmbus_ic_softc *sc = arg1;
256 snprintf(verstr, sizeof(verstr), "%u.%u",
257 VMBUS_ICVER_MAJOR(sc->ic_fwver), VMBUS_ICVER_MINOR(sc->ic_fwver));
258 return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
262 vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS)
264 struct vmbus_ic_softc *sc = arg1;
267 snprintf(verstr, sizeof(verstr), "%u.%u",
268 VMBUS_ICVER_MAJOR(sc->ic_msgver), VMBUS_ICVER_MINOR(sc->ic_msgver));
269 return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
273 vmbus_ic_detach(device_t dev)
275 struct vmbus_ic_softc *sc = device_get_softc(dev);
277 vmbus_chan_close(vmbus_get_channel(dev));
278 free(sc->ic_buf, M_DEVBUF);
284 vmbus_ic_sendresp(struct vmbus_ic_softc *sc, struct vmbus_channel *chan,
285 void *data, int dlen, uint64_t xactid)
287 struct vmbus_icmsg_hdr *hdr;
290 KASSERT(dlen >= sizeof(*hdr), ("invalid data length %d", dlen));
293 hdr->ic_flags = VMBUS_ICMSG_FLAG_XACT | VMBUS_ICMSG_FLAG_RESP;
294 error = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
297 device_printf(sc->ic_dev, "resp send failed: %d\n", error);