vmbusic.c revision 1.1.6.3 1 1.1.6.3 martin /* $NetBSD: vmbusic.c,v 1.1.6.3 2020/04/13 08:04:20 martin Exp $ */
2 1.1.6.2 christos /*-
3 1.1.6.2 christos * Copyright (c) 2014,2016 Microsoft Corp.
4 1.1.6.2 christos * All rights reserved.
5 1.1.6.2 christos *
6 1.1.6.2 christos * Redistribution and use in source and binary forms, with or without
7 1.1.6.2 christos * modification, are permitted provided that the following conditions
8 1.1.6.2 christos * are met:
9 1.1.6.2 christos * 1. Redistributions of source code must retain the above copyright
10 1.1.6.2 christos * notice unmodified, this list of conditions, and the following
11 1.1.6.2 christos * disclaimer.
12 1.1.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 christos * documentation and/or other materials provided with the distribution.
15 1.1.6.2 christos *
16 1.1.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.6.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.6.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.6.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.6.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.6.2 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.6.2 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.6.2 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.6.2 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.6.2 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.6.2 christos */
27 1.1.6.2 christos
28 1.1.6.2 christos #include <sys/cdefs.h>
29 1.1.6.2 christos #ifdef __KERNEL_RCSID
30 1.1.6.3 martin __KERNEL_RCSID(0, "$NetBSD: vmbusic.c,v 1.1.6.3 2020/04/13 08:04:20 martin Exp $");
31 1.1.6.2 christos #endif
32 1.1.6.2 christos #ifdef __FBSDID
33 1.1.6.2 christos __FBSDID("$FreeBSD: head/sys/dev/hyperv/utilities/vmbus_ic.c 310317 2016-12-20 07:14:24Z sephe $");
34 1.1.6.2 christos #endif
35 1.1.6.2 christos
36 1.1.6.2 christos #include <sys/param.h>
37 1.1.6.2 christos #include <sys/systm.h>
38 1.1.6.2 christos #include <sys/kernel.h>
39 1.1.6.2 christos #include <sys/device.h>
40 1.1.6.2 christos #include <sys/kmem.h>
41 1.1.6.2 christos #include <sys/reboot.h>
42 1.1.6.2 christos
43 1.1.6.2 christos #include <dev/hyperv/vmbusvar.h>
44 1.1.6.2 christos #include <dev/hyperv/vmbusicreg.h>
45 1.1.6.2 christos #include <dev/hyperv/vmbusicvar.h>
46 1.1.6.2 christos
47 1.1.6.2 christos #define VMBUS_IC_BRSIZE (4 * PAGE_SIZE)
48 1.1.6.2 christos
49 1.1.6.2 christos #define VMBUS_IC_VERCNT 2
50 1.1.6.2 christos #define VMBUS_IC_NEGOSZ \
51 1.1.6.2 christos offsetof(struct vmbus_icmsg_negotiate, ic_ver[VMBUS_IC_VERCNT])
52 1.1.6.2 christos __CTASSERT(VMBUS_IC_NEGOSZ < VMBUS_IC_BRSIZE);
53 1.1.6.2 christos
54 1.1.6.2 christos
55 1.1.6.2 christos int
56 1.1.6.2 christos vmbusic_probe(struct vmbus_attach_args *aa, const struct hyperv_guid *guid)
57 1.1.6.2 christos {
58 1.1.6.2 christos
59 1.1.6.2 christos if (memcmp(aa->aa_type, guid, sizeof(*aa->aa_type)) != 0)
60 1.1.6.2 christos return 0;
61 1.1.6.2 christos return 1;
62 1.1.6.2 christos }
63 1.1.6.2 christos
64 1.1.6.2 christos int
65 1.1.6.2 christos vmbusic_attach(device_t dv, struct vmbus_attach_args *aa,
66 1.1.6.2 christos vmbus_channel_callback_t cb)
67 1.1.6.2 christos {
68 1.1.6.2 christos struct vmbusic_softc *sc = device_private(dv);
69 1.1.6.2 christos
70 1.1.6.2 christos sc->sc_dev = dv;
71 1.1.6.2 christos sc->sc_chan = aa->aa_chan;
72 1.1.6.2 christos
73 1.1.6.2 christos sc->sc_buflen = VMBUS_IC_BRSIZE;
74 1.1.6.3 martin sc->sc_buf = kmem_alloc(sc->sc_buflen, KM_SLEEP);
75 1.1.6.2 christos
76 1.1.6.2 christos /*
77 1.1.6.2 christos * These services are not performance critical and do not need
78 1.1.6.2 christos * batched reading. Furthermore, some services such as KVP can
79 1.1.6.2 christos * only handle one message from the host at a time.
80 1.1.6.2 christos * Turn off batched reading for all util drivers before we open the
81 1.1.6.2 christos * channel.
82 1.1.6.2 christos */
83 1.1.6.2 christos sc->sc_chan->ch_flags &= ~CHF_BATCHED;
84 1.1.6.2 christos
85 1.1.6.2 christos if (vmbus_channel_open(sc->sc_chan, sc->sc_buflen, NULL, 0, cb, sc)) {
86 1.1.6.2 christos aprint_error_dev(dv, "failed to open channel\n");
87 1.1.6.2 christos kmem_free(sc->sc_buf, sc->sc_buflen);
88 1.1.6.2 christos sc->sc_buf = NULL;
89 1.1.6.2 christos return ENXIO;
90 1.1.6.2 christos }
91 1.1.6.2 christos
92 1.1.6.2 christos return 0;
93 1.1.6.2 christos }
94 1.1.6.2 christos
95 1.1.6.2 christos int
96 1.1.6.2 christos vmbusic_detach(device_t dv, int flags)
97 1.1.6.2 christos {
98 1.1.6.2 christos struct vmbusic_softc *sc = device_private(dv);
99 1.1.6.2 christos int error;
100 1.1.6.2 christos
101 1.1.6.2 christos error = vmbus_channel_close(sc->sc_chan);
102 1.1.6.2 christos if (error != 0)
103 1.1.6.2 christos return error;
104 1.1.6.2 christos
105 1.1.6.2 christos if (sc->sc_buf != NULL) {
106 1.1.6.2 christos kmem_free(sc->sc_buf, sc->sc_buflen);
107 1.1.6.2 christos sc->sc_buf = NULL;
108 1.1.6.2 christos }
109 1.1.6.2 christos
110 1.1.6.2 christos if (sc->sc_log != NULL) {
111 1.1.6.2 christos sysctl_teardown(&sc->sc_log);
112 1.1.6.2 christos sc->sc_log = NULL;
113 1.1.6.2 christos }
114 1.1.6.2 christos
115 1.1.6.2 christos return 0;
116 1.1.6.2 christos }
117 1.1.6.2 christos
118 1.1.6.2 christos int
119 1.1.6.2 christos vmbusic_negotiate(struct vmbusic_softc *sc, void *data, uint32_t *dlen0,
120 1.1.6.2 christos uint32_t fw_ver, uint32_t msg_ver)
121 1.1.6.2 christos {
122 1.1.6.2 christos struct vmbus_icmsg_negotiate *nego;
123 1.1.6.2 christos uint32_t sel_fw_ver = 0, sel_msg_ver = 0;
124 1.1.6.2 christos int i, cnt, dlen = *dlen0, error;
125 1.1.6.2 christos bool has_fw_ver, has_msg_ver = false;
126 1.1.6.2 christos
127 1.1.6.2 christos /*
128 1.1.6.2 christos * Preliminary message verification.
129 1.1.6.2 christos */
130 1.1.6.2 christos if (dlen < sizeof(*nego)) {
131 1.1.6.2 christos device_printf(sc->sc_dev, "truncated ic negotiate, len %d\n",
132 1.1.6.2 christos dlen);
133 1.1.6.2 christos return EINVAL;
134 1.1.6.2 christos }
135 1.1.6.2 christos nego = data;
136 1.1.6.2 christos
137 1.1.6.2 christos if (nego->ic_fwver_cnt == 0) {
138 1.1.6.2 christos device_printf(sc->sc_dev, "ic negotiate does not contain "
139 1.1.6.2 christos "framework version %u\n", nego->ic_fwver_cnt);
140 1.1.6.2 christos return EINVAL;
141 1.1.6.2 christos }
142 1.1.6.2 christos if (nego->ic_msgver_cnt == 0) {
143 1.1.6.2 christos device_printf(sc->sc_dev, "ic negotiate does not contain "
144 1.1.6.2 christos "message version %u\n", nego->ic_msgver_cnt);
145 1.1.6.2 christos return EINVAL;
146 1.1.6.2 christos }
147 1.1.6.2 christos
148 1.1.6.2 christos cnt = nego->ic_fwver_cnt + nego->ic_msgver_cnt;
149 1.1.6.2 christos if (dlen < offsetof(struct vmbus_icmsg_negotiate, ic_ver[cnt])) {
150 1.1.6.2 christos device_printf(sc->sc_dev, "ic negotiate does not contain "
151 1.1.6.2 christos "versions %d\n", dlen);
152 1.1.6.2 christos return EINVAL;
153 1.1.6.2 christos }
154 1.1.6.2 christos
155 1.1.6.2 christos error = EOPNOTSUPP;
156 1.1.6.2 christos
157 1.1.6.2 christos /*
158 1.1.6.2 christos * Find the best match framework version.
159 1.1.6.2 christos */
160 1.1.6.2 christos has_fw_ver = false;
161 1.1.6.2 christos for (i = 0; i < nego->ic_fwver_cnt; ++i) {
162 1.1.6.2 christos if (VMBUS_ICVER_LE(nego->ic_ver[i], fw_ver)) {
163 1.1.6.2 christos if (!has_fw_ver) {
164 1.1.6.2 christos sel_fw_ver = nego->ic_ver[i];
165 1.1.6.2 christos has_fw_ver = true;
166 1.1.6.2 christos } else if (VMBUS_ICVER_GT(nego->ic_ver[i],
167 1.1.6.2 christos sel_fw_ver)) {
168 1.1.6.2 christos sel_fw_ver = nego->ic_ver[i];
169 1.1.6.2 christos }
170 1.1.6.2 christos }
171 1.1.6.2 christos }
172 1.1.6.2 christos if (!has_fw_ver) {
173 1.1.6.2 christos device_printf(sc->sc_dev, "failed to select framework "
174 1.1.6.2 christos "version\n");
175 1.1.6.2 christos goto done;
176 1.1.6.2 christos }
177 1.1.6.2 christos
178 1.1.6.2 christos /*
179 1.1.6.2 christos * Fine the best match message version.
180 1.1.6.2 christos */
181 1.1.6.2 christos has_msg_ver = false;
182 1.1.6.2 christos for (i = nego->ic_fwver_cnt;
183 1.1.6.2 christos i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; ++i) {
184 1.1.6.2 christos if (VMBUS_ICVER_LE(nego->ic_ver[i], msg_ver)) {
185 1.1.6.2 christos if (!has_msg_ver) {
186 1.1.6.2 christos sel_msg_ver = nego->ic_ver[i];
187 1.1.6.2 christos has_msg_ver = true;
188 1.1.6.2 christos } else if (VMBUS_ICVER_GT(nego->ic_ver[i],
189 1.1.6.2 christos sel_msg_ver)) {
190 1.1.6.2 christos sel_msg_ver = nego->ic_ver[i];
191 1.1.6.2 christos }
192 1.1.6.2 christos }
193 1.1.6.2 christos }
194 1.1.6.2 christos if (!has_msg_ver) {
195 1.1.6.2 christos device_printf(sc->sc_dev, "failed to select message version\n");
196 1.1.6.2 christos goto done;
197 1.1.6.2 christos }
198 1.1.6.2 christos
199 1.1.6.2 christos error = 0;
200 1.1.6.2 christos done:
201 1.1.6.2 christos if (bootverbose || !has_fw_ver || !has_msg_ver) {
202 1.1.6.2 christos if (has_fw_ver) {
203 1.1.6.2 christos device_printf(sc->sc_dev,
204 1.1.6.2 christos "sel framework version: %u.%u\n",
205 1.1.6.2 christos VMBUS_ICVER_MAJOR(sel_fw_ver),
206 1.1.6.2 christos VMBUS_ICVER_MINOR(sel_fw_ver));
207 1.1.6.2 christos }
208 1.1.6.2 christos for (i = 0; i < nego->ic_fwver_cnt; i++) {
209 1.1.6.2 christos device_printf(sc->sc_dev,
210 1.1.6.2 christos "supp framework version: %u.%u\n",
211 1.1.6.2 christos VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
212 1.1.6.2 christos VMBUS_ICVER_MINOR(nego->ic_ver[i]));
213 1.1.6.2 christos }
214 1.1.6.2 christos
215 1.1.6.2 christos if (has_msg_ver) {
216 1.1.6.2 christos device_printf(sc->sc_dev,
217 1.1.6.2 christos "sel message version: %u.%u\n",
218 1.1.6.2 christos VMBUS_ICVER_MAJOR(sel_msg_ver),
219 1.1.6.2 christos VMBUS_ICVER_MINOR(sel_msg_ver));
220 1.1.6.2 christos }
221 1.1.6.2 christos for (i = nego->ic_fwver_cnt;
222 1.1.6.2 christos i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; i++) {
223 1.1.6.2 christos device_printf(sc->sc_dev,
224 1.1.6.2 christos "supp message version: %u.%u\n",
225 1.1.6.2 christos VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
226 1.1.6.2 christos VMBUS_ICVER_MINOR(nego->ic_ver[i]));
227 1.1.6.2 christos }
228 1.1.6.2 christos }
229 1.1.6.2 christos if (error)
230 1.1.6.2 christos return error;
231 1.1.6.2 christos
232 1.1.6.2 christos /* Record the selected versions. */
233 1.1.6.2 christos sc->sc_fwver = sel_fw_ver;
234 1.1.6.2 christos sc->sc_msgver = sel_msg_ver;
235 1.1.6.2 christos
236 1.1.6.2 christos /* One framework version. */
237 1.1.6.2 christos nego->ic_fwver_cnt = 1;
238 1.1.6.2 christos nego->ic_ver[0] = sel_fw_ver;
239 1.1.6.2 christos
240 1.1.6.2 christos /* One message version. */
241 1.1.6.2 christos nego->ic_msgver_cnt = 1;
242 1.1.6.2 christos nego->ic_ver[1] = sel_msg_ver;
243 1.1.6.2 christos
244 1.1.6.2 christos /* Update data size. */
245 1.1.6.2 christos nego->ic_hdr.ic_dsize = VMBUS_IC_NEGOSZ -
246 1.1.6.2 christos sizeof(struct vmbus_icmsg_hdr);
247 1.1.6.2 christos
248 1.1.6.2 christos /* Update total size, if necessary. */
249 1.1.6.2 christos if (dlen < VMBUS_IC_NEGOSZ)
250 1.1.6.2 christos *dlen0 = VMBUS_IC_NEGOSZ;
251 1.1.6.2 christos
252 1.1.6.2 christos return 0;
253 1.1.6.2 christos }
254 1.1.6.2 christos
255 1.1.6.2 christos int
256 1.1.6.2 christos vmbusic_sendresp(struct vmbusic_softc *sc, struct vmbus_channel *chan,
257 1.1.6.2 christos void *data, uint32_t dlen, uint64_t rid)
258 1.1.6.2 christos {
259 1.1.6.2 christos struct vmbus_icmsg_hdr *hdr;
260 1.1.6.2 christos int error;
261 1.1.6.2 christos
262 1.1.6.2 christos KASSERTMSG(dlen >= sizeof(*hdr), "invalid data length %d", dlen);
263 1.1.6.2 christos hdr = data;
264 1.1.6.2 christos
265 1.1.6.2 christos hdr->ic_flags = VMBUS_ICMSG_FLAG_TRANSACTION|VMBUS_ICMSG_FLAG_RESPONSE;
266 1.1.6.2 christos error = vmbus_channel_send(chan, data, dlen, rid,
267 1.1.6.2 christos VMBUS_CHANPKT_TYPE_INBAND, 0);
268 1.1.6.2 christos if (error != 0)
269 1.1.6.2 christos device_printf(sc->sc_dev, "resp send failed: %d\n", error);
270 1.1.6.2 christos return error;
271 1.1.6.2 christos }
272