1 /*-
2  * Copyright (c) 2014,2016-2017 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/kernel.h>
33 #include <sys/module.h>
34 #include <sys/syscallsubr.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
37 
38 #include <dev/hyperv/include/hyperv.h>
39 #include <dev/hyperv/include/vmbus.h>
40 #include <dev/hyperv/utilities/vmbus_icreg.h>
41 #include <dev/hyperv/utilities/vmbus_icvar.h>
42 
43 #define VMBUS_TIMESYNC_FWVER_MAJOR	3
44 #define VMBUS_TIMESYNC_FWVER		\
45 	VMBUS_IC_VERSION(VMBUS_TIMESYNC_FWVER_MAJOR, 0)
46 
47 #define VMBUS_TIMESYNC_MSGVER_MAJOR	4
48 #define VMBUS_TIMESYNC_MSGVER		\
49 	VMBUS_IC_VERSION(VMBUS_TIMESYNC_MSGVER_MAJOR, 0)
50 
51 #define VMBUS_TIMESYNC_MSGVER4(sc)	\
52 	VMBUS_ICVER_LE(VMBUS_IC_VERSION(4, 0), (sc)->ic_msgver)
53 
54 #define VMBUS_TIMESYNC_DORTT(sc)	\
55 	(VMBUS_TIMESYNC_MSGVER4((sc)) && hyperv_tc64 != NULL)
56 
57 static int			vmbus_timesync_probe(device_t);
58 static int			vmbus_timesync_attach(device_t);
59 
60 static const struct vmbus_ic_desc vmbus_timesync_descs[] = {
61 	{
62 		.ic_guid = { .hv_guid = {
63 		    0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
64 		    0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } },
65 		.ic_desc = "Hyper-V Timesync"
66 	},
67 	VMBUS_IC_DESC_END
68 };
69 
70 static device_method_t vmbus_timesync_methods[] = {
71 	/* Device interface */
72 	DEVMETHOD(device_probe,		vmbus_timesync_probe),
73 	DEVMETHOD(device_attach,	vmbus_timesync_attach),
74 	DEVMETHOD(device_detach,	vmbus_ic_detach),
75 	DEVMETHOD_END
76 };
77 
78 static driver_t vmbus_timesync_driver = {
79 	"hvtimesync",
80 	vmbus_timesync_methods,
81 	sizeof(struct vmbus_ic_softc)
82 };
83 
84 static devclass_t vmbus_timesync_devclass;
85 
86 DRIVER_MODULE(hv_timesync, vmbus, vmbus_timesync_driver,
87     vmbus_timesync_devclass, NULL, NULL);
88 MODULE_VERSION(hv_timesync, 1);
89 MODULE_DEPEND(hv_timesync, vmbus, 1, 1, 1);
90 
91 SYSCTL_NODE(_hw, OID_AUTO, hvtimesync, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
92     "Hyper-V timesync interface");
93 
94 static int vmbus_ts_ignore_sync = 0;
95 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, ignore_sync, CTLFLAG_RWTUN,
96     &vmbus_ts_ignore_sync, 0, "Ignore the sync request.");
97 
98 /*
99  * Trigger sample sync when drift exceeds threshold (ms).
100  * Ignore the sample request when set to 0.
101  */
102 static int vmbus_ts_sample_thresh = 100;
103 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_thresh, CTLFLAG_RWTUN,
104     &vmbus_ts_sample_thresh, 0,
105     "Threshold that makes sample request trigger the sync (unit: ms).");
106 
107 static int vmbus_ts_sample_verbose = 0;
108 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_verbose, CTLFLAG_RWTUN,
109     &vmbus_ts_sample_verbose, 0, "Increase sample request verbosity.");
110 
111 static void
112 vmbus_timesync(struct vmbus_ic_softc *sc, uint64_t hvtime, uint64_t sent_tc,
113     uint8_t tsflags)
114 {
115 	struct timespec vm_ts;
116 	uint64_t hv_ns, vm_ns, rtt = 0;
117 
118 	if (VMBUS_TIMESYNC_DORTT(sc))
119 		rtt = hyperv_tc64() - sent_tc;
120 
121 	hv_ns = (hvtime - VMBUS_ICMSG_TS_BASE + rtt) * HYPERV_TIMER_NS_FACTOR;
122 	nanotime(&vm_ts);
123 	vm_ns = (vm_ts.tv_sec * NANOSEC) + vm_ts.tv_nsec;
124 
125 	if ((tsflags & VMBUS_ICMSG_TS_FLAG_SYNC) && !vmbus_ts_ignore_sync) {
126 		struct timespec hv_ts;
127 
128 		if (bootverbose) {
129 			device_printf(sc->ic_dev, "apply sync request, "
130 			    "hv: %ju, vm: %ju\n",
131 			    (uintmax_t)hv_ns, (uintmax_t)vm_ns);
132 		}
133 		hv_ts.tv_sec = hv_ns / NANOSEC;
134 		hv_ts.tv_nsec = hv_ns % NANOSEC;
135 		kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
136 		/* Done! */
137 		return;
138 	}
139 
140 	if ((tsflags & VMBUS_ICMSG_TS_FLAG_SAMPLE) &&
141 	    vmbus_ts_sample_thresh >= 0) {
142 		int64_t diff;
143 
144 		if (vmbus_ts_sample_verbose) {
145 			device_printf(sc->ic_dev, "sample request, "
146 			    "hv: %ju, vm: %ju\n",
147 			    (uintmax_t)hv_ns, (uintmax_t)vm_ns);
148 		}
149 
150 		if (hv_ns > vm_ns)
151 			diff = hv_ns - vm_ns;
152 		else
153 			diff = vm_ns - hv_ns;
154 		/* nanosec -> millisec */
155 		diff /= 1000000;
156 
157 		if (diff > vmbus_ts_sample_thresh) {
158 			struct timespec hv_ts;
159 
160 			if (bootverbose) {
161 				device_printf(sc->ic_dev,
162 				    "apply sample request, hv: %ju, vm: %ju\n",
163 				    (uintmax_t)hv_ns, (uintmax_t)vm_ns);
164 			}
165 			hv_ts.tv_sec = hv_ns / NANOSEC;
166 			hv_ts.tv_nsec = hv_ns % NANOSEC;
167 			kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
168 		}
169 		/* Done */
170 		return;
171 	}
172 }
173 
174 static void
175 vmbus_timesync_cb(struct vmbus_channel *chan, void *xsc)
176 {
177 	struct vmbus_ic_softc *sc = xsc;
178 	struct vmbus_icmsg_hdr *hdr;
179 	int dlen, error;
180 	uint64_t xactid;
181 	void *data;
182 
183 	/*
184 	 * Receive request.
185 	 */
186 	data = sc->ic_buf;
187 	dlen = sc->ic_buflen;
188 	error = vmbus_chan_recv(chan, data, &dlen, &xactid);
189 	KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
190 	if (error)
191 		return;
192 
193 	if (dlen < sizeof(*hdr)) {
194 		device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
195 		return;
196 	}
197 	hdr = data;
198 
199 	/*
200 	 * Update request, which will be echoed back as response.
201 	 */
202 	switch (hdr->ic_type) {
203 	case VMBUS_ICMSG_TYPE_NEGOTIATE:
204 		error = vmbus_ic_negomsg(sc, data, &dlen,
205 		    VMBUS_TIMESYNC_FWVER, VMBUS_TIMESYNC_MSGVER);
206 		if (error)
207 			return;
208 		if (VMBUS_TIMESYNC_DORTT(sc))
209 			device_printf(sc->ic_dev, "RTT\n");
210 		break;
211 
212 	case VMBUS_ICMSG_TYPE_TIMESYNC:
213 		if (VMBUS_TIMESYNC_MSGVER4(sc)) {
214 			const struct vmbus_icmsg_timesync4 *msg4;
215 
216 			if (dlen < sizeof(*msg4)) {
217 				device_printf(sc->ic_dev, "invalid timesync4 "
218 				    "len %d\n", dlen);
219 				return;
220 			}
221 			msg4 = data;
222 			vmbus_timesync(sc, msg4->ic_hvtime, msg4->ic_sent_tc,
223 			    msg4->ic_tsflags);
224 		} else {
225 			const struct vmbus_icmsg_timesync *msg;
226 
227 			if (dlen < sizeof(*msg)) {
228 				device_printf(sc->ic_dev, "invalid timesync "
229 				    "len %d\n", dlen);
230 				return;
231 			}
232 			msg = data;
233 			vmbus_timesync(sc, msg->ic_hvtime, 0, msg->ic_tsflags);
234 		}
235 		break;
236 
237 	default:
238 		device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
239 		break;
240 	}
241 
242 	/*
243 	 * Send response by echoing the request back.
244 	 */
245 	vmbus_ic_sendresp(sc, chan, data, dlen, xactid);
246 }
247 
248 static int
249 vmbus_timesync_probe(device_t dev)
250 {
251 
252 	return (vmbus_ic_probe(dev, vmbus_timesync_descs));
253 }
254 
255 static int
256 vmbus_timesync_attach(device_t dev)
257 {
258 
259 	return (vmbus_ic_attach(dev, vmbus_timesync_cb));
260 }
261