xref: /freebsd/sys/dev/hyperv/utilities/vmbus_ic.c (revision 2a58b312)
1 /*-
2  * Copyright (c) 2014,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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34 #include <sys/sysctl.h>
35 
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>
40 
41 #include "vmbus_if.h"
42 
43 #define VMBUS_IC_BRSIZE		(4 * PAGE_SIZE)
44 
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);
49 
50 static int	vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS);
51 static int	vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS);
52 
53 int
54 vmbus_ic_negomsg(struct vmbus_ic_softc *sc, void *data, int *dlen0,
55     uint32_t fw_ver, uint32_t msg_ver)
56 {
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;
61 
62 	has_fw_ver = false;
63 	has_msg_ver = false;
64 
65 	/*
66 	 * Preliminary message verification.
67 	 */
68 	if (dlen < sizeof(*nego)) {
69 		device_printf(sc->ic_dev, "truncated ic negotiate, len %d\n",
70 		    dlen);
71 		return (EINVAL);
72 	}
73 	nego = data;
74 
75 	if (nego->ic_fwver_cnt == 0) {
76 		device_printf(sc->ic_dev, "ic negotiate does not contain "
77 		    "framework version %u\n", nego->ic_fwver_cnt);
78 		return (EINVAL);
79 	}
80 	if (nego->ic_msgver_cnt == 0) {
81 		device_printf(sc->ic_dev, "ic negotiate does not contain "
82 		    "message version %u\n", nego->ic_msgver_cnt);
83 		return (EINVAL);
84 	}
85 
86 	cnt = nego->ic_fwver_cnt + nego->ic_msgver_cnt;
87 	if (dlen < __offsetof(struct vmbus_icmsg_negotiate, ic_ver[cnt])) {
88 		device_printf(sc->ic_dev, "ic negotiate does not contain "
89 		    "versions %d\n", dlen);
90 		return (EINVAL);
91 	}
92 
93 	error = EOPNOTSUPP;
94 
95 	/*
96 	 * Find the best match framework version.
97 	 */
98 	for (i = 0; i < nego->ic_fwver_cnt; ++i) {
99 		if (VMBUS_ICVER_LE(nego->ic_ver[i], fw_ver)) {
100 			if (!has_fw_ver) {
101 				sel_fw_ver = nego->ic_ver[i];
102 				has_fw_ver = true;
103 			} else if (VMBUS_ICVER_GT(nego->ic_ver[i],
104 			    sel_fw_ver)) {
105 				sel_fw_ver = nego->ic_ver[i];
106 			}
107 		}
108 	}
109 	if (!has_fw_ver) {
110 		device_printf(sc->ic_dev, "failed to select framework "
111 		    "version\n");
112 		goto done;
113 	}
114 
115 	/*
116 	 * Find the best match message version.
117 	 */
118 	for (i = nego->ic_fwver_cnt;
119 	    i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; ++i) {
120 		if (VMBUS_ICVER_LE(nego->ic_ver[i], msg_ver)) {
121 			if (!has_msg_ver) {
122 				sel_msg_ver = nego->ic_ver[i];
123 				has_msg_ver = true;
124 			} else if (VMBUS_ICVER_GT(nego->ic_ver[i],
125 			    sel_msg_ver)) {
126 				sel_msg_ver = nego->ic_ver[i];
127 			}
128 		}
129 	}
130 	if (!has_msg_ver) {
131 		device_printf(sc->ic_dev, "failed to select message "
132 		    "version\n");
133 		goto done;
134 	}
135 
136 	error = 0;
137 done:
138 	if (bootverbose || !has_fw_ver || !has_msg_ver) {
139 		if (has_fw_ver) {
140 			device_printf(sc->ic_dev, "sel framework version: "
141 			    "%u.%u\n",
142 			    VMBUS_ICVER_MAJOR(sel_fw_ver),
143 			    VMBUS_ICVER_MINOR(sel_fw_ver));
144 		}
145 		for (i = 0; i < nego->ic_fwver_cnt; i++) {
146 			device_printf(sc->ic_dev, "supp framework version: "
147 			    "%u.%u\n",
148 			    VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
149 			    VMBUS_ICVER_MINOR(nego->ic_ver[i]));
150 		}
151 
152 		if (has_msg_ver) {
153 			device_printf(sc->ic_dev, "sel message version: "
154 			    "%u.%u\n",
155 			    VMBUS_ICVER_MAJOR(sel_msg_ver),
156 			    VMBUS_ICVER_MINOR(sel_msg_ver));
157 		}
158 		for (i = nego->ic_fwver_cnt;
159 		    i < nego->ic_fwver_cnt + nego->ic_msgver_cnt; i++) {
160 			device_printf(sc->ic_dev, "supp message version: "
161 			    "%u.%u\n",
162 			    VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
163 			    VMBUS_ICVER_MINOR(nego->ic_ver[i]));
164 		}
165 	}
166 	if (error)
167 		return (error);
168 
169 	/* Record the selected versions. */
170 	sc->ic_fwver = sel_fw_ver;
171 	sc->ic_msgver = sel_msg_ver;
172 
173 	/* One framework version. */
174 	nego->ic_fwver_cnt = 1;
175 	nego->ic_ver[0] = sel_fw_ver;
176 
177 	/* One message version. */
178 	nego->ic_msgver_cnt = 1;
179 	nego->ic_ver[1] = sel_msg_ver;
180 
181 	/* Update data size. */
182 	nego->ic_hdr.ic_dsize = VMBUS_IC_NEGOSZ -
183 	    sizeof(struct vmbus_icmsg_hdr);
184 
185 	/* Update total size, if necessary. */
186 	if (dlen < VMBUS_IC_NEGOSZ)
187 		*dlen0 = VMBUS_IC_NEGOSZ;
188 
189 	return (0);
190 }
191 
192 int
193 vmbus_ic_probe(device_t dev, const struct vmbus_ic_desc descs[])
194 {
195 	device_t bus = device_get_parent(dev);
196 	const struct vmbus_ic_desc *d;
197 
198 	if (resource_disabled(device_get_name(dev), 0))
199 		return (ENXIO);
200 
201 	for (d = descs; d->ic_desc != NULL; ++d) {
202 		if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) {
203 			device_set_desc(dev, d->ic_desc);
204 			return (BUS_PROBE_DEFAULT);
205 		}
206 	}
207 	return (ENXIO);
208 }
209 
210 int
211 vmbus_ic_attach(device_t dev, vmbus_chan_callback_t cb)
212 {
213 	struct vmbus_ic_softc *sc = device_get_softc(dev);
214 	struct vmbus_channel *chan = vmbus_get_channel(dev);
215 	struct sysctl_oid_list *child;
216 	struct sysctl_ctx_list *ctx;
217 	int error;
218 
219 	sc->ic_dev = dev;
220 	sc->ic_buflen = VMBUS_IC_BRSIZE;
221 	sc->ic_buf = malloc(VMBUS_IC_BRSIZE, M_DEVBUF, M_WAITOK | M_ZERO);
222 
223 	/*
224 	 * These services are not performance critical and do not need
225 	 * batched reading. Furthermore, some services such as KVP can
226 	 * only handle one message from the host at a time.
227 	 * Turn off batched reading for all util drivers before we open the
228 	 * channel.
229 	 */
230 	vmbus_chan_set_readbatch(chan, false);
231 
232 	error = vmbus_chan_open(chan, VMBUS_IC_BRSIZE, VMBUS_IC_BRSIZE, NULL, 0,
233 	    cb, sc);
234 	if (error) {
235 		free(sc->ic_buf, M_DEVBUF);
236 		return (error);
237 	}
238 
239 	ctx = device_get_sysctl_ctx(dev);
240 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
241 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "fw_version",
242 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
243 	    vmbus_ic_fwver_sysctl, "A", "framework version");
244 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "msg_version",
245 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
246 	    vmbus_ic_msgver_sysctl, "A", "message version");
247 
248 	return (0);
249 }
250 
251 static int
252 vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS)
253 {
254 	struct vmbus_ic_softc *sc = arg1;
255 	char verstr[16];
256 
257 	snprintf(verstr, sizeof(verstr), "%u.%u",
258 	    VMBUS_ICVER_MAJOR(sc->ic_fwver), VMBUS_ICVER_MINOR(sc->ic_fwver));
259 	return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
260 }
261 
262 static int
263 vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS)
264 {
265 	struct vmbus_ic_softc *sc = arg1;
266 	char verstr[16];
267 
268 	snprintf(verstr, sizeof(verstr), "%u.%u",
269 	    VMBUS_ICVER_MAJOR(sc->ic_msgver), VMBUS_ICVER_MINOR(sc->ic_msgver));
270 	return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
271 }
272 
273 int
274 vmbus_ic_detach(device_t dev)
275 {
276 	struct vmbus_ic_softc *sc = device_get_softc(dev);
277 
278 	vmbus_chan_close(vmbus_get_channel(dev));
279 	free(sc->ic_buf, M_DEVBUF);
280 
281 	return (0);
282 }
283 
284 int
285 vmbus_ic_sendresp(struct vmbus_ic_softc *sc, struct vmbus_channel *chan,
286     void *data, int dlen, uint64_t xactid)
287 {
288 	struct vmbus_icmsg_hdr *hdr;
289 	int error;
290 
291 	KASSERT(dlen >= sizeof(*hdr), ("invalid data length %d", dlen));
292 	hdr = data;
293 
294 	hdr->ic_flags = VMBUS_ICMSG_FLAG_XACT | VMBUS_ICMSG_FLAG_RESP;
295 	error = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
296 	    data, dlen, xactid);
297 	if (error)
298 		device_printf(sc->ic_dev, "resp send failed: %d\n", error);
299 	return (error);
300 }
301