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/timetc.h>
42 #include <dev/hyperv/include/hyperv.h>
43 #include <dev/hyperv/include/vmbus.h>
44 #include <dev/hyperv/utilities/hv_util.h>
45 #include <dev/hyperv/utilities/vmbus_icreg.h>
49 #define VMBUS_IC_BRSIZE (4 * PAGE_SIZE)
51 #define VMBUS_IC_VERCNT 2
52 #define VMBUS_IC_NEGOSZ \
53 __offsetof(struct vmbus_icmsg_negotiate, ic_ver[VMBUS_IC_VERCNT])
54 CTASSERT(VMBUS_IC_NEGOSZ < VMBUS_IC_BRSIZE);
57 vmbus_ic_negomsg(struct hv_util_sc *sc, void *data, int *dlen0)
59 struct vmbus_icmsg_negotiate *nego;
60 int cnt, major, dlen = *dlen0;
63 * Preliminary message size verification
65 if (dlen < sizeof(*nego)) {
66 device_printf(sc->ic_dev, "truncated ic negotiate, len %d\n",
72 cnt = nego->ic_fwver_cnt + nego->ic_msgver_cnt;
73 if (dlen < __offsetof(struct vmbus_icmsg_negotiate, ic_ver[cnt])) {
74 device_printf(sc->ic_dev, "ic negotiate does not contain "
75 "versions %d\n", dlen);
79 /* Select major version; XXX looks wrong. */
80 if (nego->ic_fwver_cnt >= 2 && VMBUS_ICVER_MAJOR(nego->ic_ver[1]) == 3)
85 /* One framework version */
86 nego->ic_fwver_cnt = 1;
87 nego->ic_ver[0] = VMBUS_IC_VERSION(major, 0);
89 /* One message version */
90 nego->ic_msgver_cnt = 1;
91 nego->ic_ver[1] = VMBUS_IC_VERSION(major, 0);
93 /* Update data size */
94 nego->ic_hdr.ic_dsize = VMBUS_IC_NEGOSZ -
95 sizeof(struct vmbus_icmsg_hdr);
97 /* Update total size, if necessary */
98 if (dlen < VMBUS_IC_NEGOSZ)
99 *dlen0 = VMBUS_IC_NEGOSZ;
105 vmbus_ic_probe(device_t dev, const struct vmbus_ic_desc descs[])
107 device_t bus = device_get_parent(dev);
108 const struct vmbus_ic_desc *d;
110 if (resource_disabled(device_get_name(dev), 0))
113 for (d = descs; d->ic_desc != NULL; ++d) {
114 if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) {
115 device_set_desc(dev, d->ic_desc);
116 return (BUS_PROBE_DEFAULT);
123 hv_util_attach(device_t dev, vmbus_chan_callback_t cb)
125 struct hv_util_sc *sc = device_get_softc(dev);
126 struct vmbus_channel *chan = vmbus_get_channel(dev);
130 sc->ic_buflen = VMBUS_IC_BRSIZE;
131 sc->receive_buffer = malloc(VMBUS_IC_BRSIZE, M_DEVBUF,
135 * These services are not performance critical and do not need
136 * batched reading. Furthermore, some services such as KVP can
137 * only handle one message from the host at a time.
138 * Turn off batched reading for all util drivers before we open the
141 vmbus_chan_set_readbatch(chan, false);
143 error = vmbus_chan_open(chan, VMBUS_IC_BRSIZE, VMBUS_IC_BRSIZE, NULL, 0,
146 free(sc->receive_buffer, M_DEVBUF);
153 hv_util_detach(device_t dev)
155 struct hv_util_sc *sc = device_get_softc(dev);
157 vmbus_chan_close(vmbus_get_channel(dev));
158 free(sc->receive_buffer, M_DEVBUF);