vmbus_ic.c revision 311251
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])
48CTASSERT(VMBUS_IC_NEGOSZ < VMBUS_IC_BRSIZE);
49
50static int	vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS);
51static int	vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS);
52
53int
54vmbus_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	/*
63	 * Preliminary message verification.
64	 */
65	if (dlen < sizeof(*nego)) {
66		device_printf(sc->ic_dev, "truncated ic negotiate, len %d\n",
67		    dlen);
68		return (EINVAL);
69	}
70	nego = data;
71
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);
75		return (EINVAL);
76	}
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);
80		return (EINVAL);
81	}
82
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);
87		return (EINVAL);
88	}
89
90	error = EOPNOTSUPP;
91
92	/*
93	 * Find the best match framework version.
94	 */
95	has_fw_ver = false;
96	for (i = 0; i < nego->ic_fwver_cnt; ++i) {
97		if (VMBUS_ICVER_LE(nego->ic_ver[i], fw_ver)) {
98			if (!has_fw_ver) {
99				sel_fw_ver = nego->ic_ver[i];
100				has_fw_ver = true;
101			} else if (VMBUS_ICVER_GT(nego->ic_ver[i],
102			    sel_fw_ver)) {
103				sel_fw_ver = nego->ic_ver[i];
104			}
105		}
106	}
107	if (!has_fw_ver) {
108		device_printf(sc->ic_dev, "failed to select framework "
109		    "version\n");
110		goto done;
111	}
112
113	/*
114	 * Fine the best match message version.
115	 */
116	has_msg_ver = false;
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)) {
120			if (!has_msg_ver) {
121				sel_msg_ver = nego->ic_ver[i];
122				has_msg_ver = true;
123			} else if (VMBUS_ICVER_GT(nego->ic_ver[i],
124			    sel_msg_ver)) {
125				sel_msg_ver = nego->ic_ver[i];
126			}
127		}
128	}
129	if (!has_msg_ver) {
130		device_printf(sc->ic_dev, "failed to select message "
131		    "version\n");
132		goto done;
133	}
134
135	error = 0;
136done:
137	if (bootverbose || !has_fw_ver || !has_msg_ver) {
138		if (has_fw_ver) {
139			device_printf(sc->ic_dev, "sel framework version: "
140			    "%u.%u\n",
141			    VMBUS_ICVER_MAJOR(sel_fw_ver),
142			    VMBUS_ICVER_MINOR(sel_fw_ver));
143		}
144		for (i = 0; i < nego->ic_fwver_cnt; i++) {
145			device_printf(sc->ic_dev, "supp framework version: "
146			    "%u.%u\n",
147			    VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
148			    VMBUS_ICVER_MINOR(nego->ic_ver[i]));
149		}
150
151		if (has_msg_ver) {
152			device_printf(sc->ic_dev, "sel message version: "
153			    "%u.%u\n",
154			    VMBUS_ICVER_MAJOR(sel_msg_ver),
155			    VMBUS_ICVER_MINOR(sel_msg_ver));
156		}
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: "
160			    "%u.%u\n",
161			    VMBUS_ICVER_MAJOR(nego->ic_ver[i]),
162			    VMBUS_ICVER_MINOR(nego->ic_ver[i]));
163		}
164	}
165	if (error)
166		return (error);
167
168	/* Record the selected versions. */
169	sc->ic_fwver = sel_fw_ver;
170	sc->ic_msgver = sel_msg_ver;
171
172	/* One framework version. */
173	nego->ic_fwver_cnt = 1;
174	nego->ic_ver[0] = sel_fw_ver;
175
176	/* One message version. */
177	nego->ic_msgver_cnt = 1;
178	nego->ic_ver[1] = sel_msg_ver;
179
180	/* Update data size. */
181	nego->ic_hdr.ic_dsize = VMBUS_IC_NEGOSZ -
182	    sizeof(struct vmbus_icmsg_hdr);
183
184	/* Update total size, if necessary. */
185	if (dlen < VMBUS_IC_NEGOSZ)
186		*dlen0 = VMBUS_IC_NEGOSZ;
187
188	return (0);
189}
190
191int
192vmbus_ic_probe(device_t dev, const struct vmbus_ic_desc descs[])
193{
194	device_t bus = device_get_parent(dev);
195	const struct vmbus_ic_desc *d;
196
197	if (resource_disabled(device_get_name(dev), 0))
198		return (ENXIO);
199
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);
204		}
205	}
206	return (ENXIO);
207}
208
209int
210vmbus_ic_attach(device_t dev, vmbus_chan_callback_t cb)
211{
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;
216	int error;
217
218	sc->ic_dev = dev;
219	sc->ic_buflen = VMBUS_IC_BRSIZE;
220	sc->ic_buf = malloc(VMBUS_IC_BRSIZE, M_DEVBUF, M_WAITOK | M_ZERO);
221
222	/*
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
227	 * channel.
228	 */
229	vmbus_chan_set_readbatch(chan, false);
230
231	error = vmbus_chan_open(chan, VMBUS_IC_BRSIZE, VMBUS_IC_BRSIZE, NULL, 0,
232	    cb, sc);
233	if (error) {
234		free(sc->ic_buf, M_DEVBUF);
235		return (error);
236	}
237
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");
246
247	return (0);
248}
249
250static int
251vmbus_ic_fwver_sysctl(SYSCTL_HANDLER_ARGS)
252{
253	struct vmbus_ic_softc *sc = arg1;
254	char verstr[16];
255
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);
259}
260
261static int
262vmbus_ic_msgver_sysctl(SYSCTL_HANDLER_ARGS)
263{
264	struct vmbus_ic_softc *sc = arg1;
265	char verstr[16];
266
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);
270}
271
272int
273vmbus_ic_detach(device_t dev)
274{
275	struct vmbus_ic_softc *sc = device_get_softc(dev);
276
277	vmbus_chan_close(vmbus_get_channel(dev));
278	free(sc->ic_buf, M_DEVBUF);
279
280	return (0);
281}
282
283int
284vmbus_ic_sendresp(struct vmbus_ic_softc *sc, struct vmbus_channel *chan,
285    void *data, int dlen, uint64_t xactid)
286{
287	struct vmbus_icmsg_hdr *hdr;
288	int error;
289
290	KASSERT(dlen >= sizeof(*hdr), ("invalid data length %d", dlen));
291	hdr = data;
292
293	hdr->ic_flags = VMBUS_ICMSG_FLAG_XACT | VMBUS_ICMSG_FLAG_RESP;
294	error = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
295	    data, dlen, xactid);
296	if (error)
297		device_printf(sc->ic_dev, "resp send failed: %d\n", error);
298	return (error);
299}
300