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.
30 * A common driver for all hyper-V util services.
33 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/reboot.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 #include <sys/timetc.h>
43 #include <dev/hyperv/include/hyperv.h>
44 #include <dev/hyperv/include/vmbus.h>
45 #include <dev/hyperv/utilities/hv_util.h>
46 #include <dev/hyperv/utilities/vmbus_icreg.h>
50 #define VMBUS_IC_BRSIZE (4 * PAGE_SIZE)
52 #define VMBUS_IC_VERCNT 2
53 #define VMBUS_IC_NEGOSZ \
54 __offsetof(struct vmbus_icmsg_negotiate, ic_ver[VMBUS_IC_VERCNT])
55 CTASSERT(VMBUS_IC_NEGOSZ < VMBUS_IC_BRSIZE);
57 static int vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS);
58 static int vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS);
61 vmbus_ic_negomsg(struct hv_util_sc *sc, void *data, int *dlen0,
62 uint32_t fw_ver, uint32_t msg_ver)
64 struct vmbus_icmsg_negotiate *nego;
65 int i, cnt, dlen = *dlen0, error;
66 uint32_t sel_fw_ver, sel_msg_ver;
67 bool has_fw_ver, has_msg_ver;
70 * Preliminary message verification.
72 if (dlen < sizeof(*nego)) {
73 device_printf(sc->ic_dev, "truncated ic negotiate, len %d\n",
79 if (nego->ic_fwver_cnt == 0) {
80 device_printf(sc->ic_dev, "ic negotiate does not contain "
81 "framework version %u\n", nego->ic_fwver_cnt);
84 if (nego->ic_msgver_cnt == 0) {
85 device_printf(sc->ic_dev, "ic negotiate does not contain "
86 "message version %u\n", nego->ic_msgver_cnt);
90 cnt = nego->ic_fwver_cnt + nego->ic_msgver_cnt;
91 if (dlen < __offsetof(struct vmbus_icmsg_negotiate, ic_ver[cnt])) {
92 device_printf(sc->ic_dev, "ic negotiate does not contain "
93 "versions %d\n", dlen);
100 * Find the best match framework version.
103 for (i = 0; i < nego->ic_fwver_cnt; ++i) {
104 if (VMBUS_ICVER_LE(nego->ic_ver[i], fw_ver)) {
106 sel_fw_ver = nego->ic_ver[i];
108 } else if (VMBUS_ICVER_GT(nego->ic_ver[i],
110 sel_fw_ver = nego->ic_ver[i];
115 device_printf(sc->ic_dev, "failed to select framework "
121 * Fine the best match message version.
124 for (i = nego->ic_fwver_cnt;
125 i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; ++i) {
126 if (VMBUS_ICVER_LE(nego->ic_ver[i], msg_ver)) {
128 sel_msg_ver = nego->ic_ver[i];
130 } else if (VMBUS_ICVER_GT(nego->ic_ver[i],
132 sel_msg_ver = nego->ic_ver[i];
137 device_printf(sc->ic_dev, "failed to select message "
144 if (bootverbose || !has_fw_ver || !has_msg_ver) {
146 device_printf(sc->ic_dev, "sel framework version: "
148 VMBUS_ICVER_MAJOR(sel_fw_ver),
149 VMBUS_ICVER_MINOR(sel_fw_ver));
151 for (i = 0; i < nego->ic_fwver_cnt; i++) {
152 device_printf(sc->ic_dev, "supp framework version: "
154 VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
155 VMBUS_ICVER_MINOR(nego->ic_ver[i]));
159 device_printf(sc->ic_dev, "sel message version: "
161 VMBUS_ICVER_MAJOR(sel_msg_ver),
162 VMBUS_ICVER_MINOR(sel_msg_ver));
164 for (i = nego->ic_fwver_cnt;
165 i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; i++) {
166 device_printf(sc->ic_dev, "supp message version: "
168 VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
169 VMBUS_ICVER_MINOR(nego->ic_ver[i]));
175 /* Record the selected versions. */
176 sc->ic_fwver = sel_fw_ver;
177 sc->ic_msgver = sel_msg_ver;
179 /* One framework version. */
180 nego->ic_fwver_cnt = 1;
181 nego->ic_ver[0] = sel_fw_ver;
183 /* One message version. */
184 nego->ic_msgver_cnt = 1;
185 nego->ic_ver[1] = sel_msg_ver;
187 /* Update data size. */
188 nego->ic_hdr.ic_dsize = VMBUS_IC_NEGOSZ -
189 sizeof(struct vmbus_icmsg_hdr);
191 /* Update total size, if necessary. */
192 if (dlen < VMBUS_IC_NEGOSZ)
193 *dlen0 = VMBUS_IC_NEGOSZ;
199 vmbus_ic_probe(device_t dev, const struct vmbus_ic_desc descs[])
201 device_t bus = device_get_parent(dev);
202 const struct vmbus_ic_desc *d;
204 if (resource_disabled(device_get_name(dev), 0))
207 for (d = descs; d->ic_desc != NULL; ++d) {
208 if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) {
209 device_set_desc(dev, d->ic_desc);
210 return (BUS_PROBE_DEFAULT);
217 hv_util_attach(device_t dev, vmbus_chan_callback_t cb)
219 struct hv_util_sc *sc = device_get_softc(dev);
220 struct vmbus_channel *chan = vmbus_get_channel(dev);
221 struct sysctl_oid_list *child;
222 struct sysctl_ctx_list *ctx;
226 sc->ic_buflen = VMBUS_IC_BRSIZE;
227 sc->receive_buffer = malloc(VMBUS_IC_BRSIZE, M_DEVBUF,
231 * These services are not performance critical and do not need
232 * batched reading. Furthermore, some services such as KVP can
233 * only handle one message from the host at a time.
234 * Turn off batched reading for all util drivers before we open the
237 vmbus_chan_set_readbatch(chan, false);
239 error = vmbus_chan_open(chan, VMBUS_IC_BRSIZE, VMBUS_IC_BRSIZE, NULL, 0,
242 free(sc->receive_buffer, M_DEVBUF);
246 ctx = device_get_sysctl_ctx(dev);
247 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
248 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "fw_version",
249 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
250 vmbus_ic_fwver_sysctl, "A", "framework version");
251 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "msg_version",
252 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
253 vmbus_ic_msgver_sysctl, "A", "message version");
259 vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS)
261 struct hv_util_sc *sc = arg1;
264 snprintf(verstr, sizeof(verstr), "%u.%u",
265 VMBUS_ICVER_MAJOR(sc->ic_fwver), VMBUS_ICVER_MINOR(sc->ic_fwver));
266 return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
270 vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS)
272 struct hv_util_sc *sc = arg1;
275 snprintf(verstr, sizeof(verstr), "%u.%u",
276 VMBUS_ICVER_MAJOR(sc->ic_msgver), VMBUS_ICVER_MINOR(sc->ic_msgver));
277 return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
281 hv_util_detach(device_t dev)
283 struct hv_util_sc *sc = device_get_softc(dev);
285 vmbus_chan_close(vmbus_get_channel(dev));
286 free(sc->receive_buffer, M_DEVBUF);