hvtimesync.c revision 1.2.6.2 1 1.2.6.2 christos /* $NetBSD: hvtimesync.c,v 1.2.6.2 2019/06/10 22:07:09 christos Exp $ */
2 1.2.6.2 christos
3 1.2.6.2 christos /*-
4 1.2.6.2 christos * Copyright (c) 2014,2016-2017 Microsoft Corp.
5 1.2.6.2 christos * All rights reserved.
6 1.2.6.2 christos *
7 1.2.6.2 christos * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 christos * modification, are permitted provided that the following conditions
9 1.2.6.2 christos * are met:
10 1.2.6.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 christos * notice unmodified, this list of conditions, and the following
12 1.2.6.2 christos * disclaimer.
13 1.2.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.6.2 christos * notice, this list of conditions and the following disclaimer in the
15 1.2.6.2 christos * documentation and/or other materials provided with the distribution.
16 1.2.6.2 christos *
17 1.2.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.2.6.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.2.6.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.2.6.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.2.6.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.2.6.2 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.2.6.2 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.2.6.2 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.2.6.2 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.2.6.2 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.2.6.2 christos */
28 1.2.6.2 christos
29 1.2.6.2 christos #include <sys/cdefs.h>
30 1.2.6.2 christos #ifdef __KERNEL_RCSID
31 1.2.6.2 christos __KERNEL_RCSID(0, "$NetBSD: hvtimesync.c,v 1.2.6.2 2019/06/10 22:07:09 christos Exp $");
32 1.2.6.2 christos #endif
33 1.2.6.2 christos #ifdef __FBSDID
34 1.2.6.2 christos __FBSDID("$FreeBSD: head/sys/dev/hyperv/utilities/vmbus_timesync.c 322488 2017-08-14 06:00:50Z sephe $");
35 1.2.6.2 christos #endif
36 1.2.6.2 christos
37 1.2.6.2 christos #include <sys/param.h>
38 1.2.6.2 christos #include <sys/systm.h>
39 1.2.6.2 christos #include <sys/device.h>
40 1.2.6.2 christos #include <sys/module.h>
41 1.2.6.2 christos #include <sys/pmf.h>
42 1.2.6.2 christos #include <sys/sysctl.h>
43 1.2.6.2 christos #include <sys/timetc.h>
44 1.2.6.2 christos
45 1.2.6.2 christos #include <dev/hyperv/vmbusvar.h>
46 1.2.6.2 christos #include <dev/hyperv/vmbusicreg.h>
47 1.2.6.2 christos #include <dev/hyperv/vmbusicvar.h>
48 1.2.6.2 christos
49 1.2.6.2 christos #define VMBUS_TIMESYNC_FWVER_MAJOR 3
50 1.2.6.2 christos #define VMBUS_TIMESYNC_FWVER \
51 1.2.6.2 christos VMBUS_IC_VERSION(VMBUS_TIMESYNC_FWVER_MAJOR, 0)
52 1.2.6.2 christos
53 1.2.6.2 christos #define VMBUS_TIMESYNC_MSGVER_MAJOR 4
54 1.2.6.2 christos #define VMBUS_TIMESYNC_MSGVER \
55 1.2.6.2 christos VMBUS_IC_VERSION(VMBUS_TIMESYNC_MSGVER_MAJOR, 0)
56 1.2.6.2 christos
57 1.2.6.2 christos #define VMBUS_TIMESYNC_MSGVER4(sc) \
58 1.2.6.2 christos VMBUS_ICVER_LE(VMBUS_IC_VERSION(4, 0), (sc)->sc_vmbusic.sc_msgver)
59 1.2.6.2 christos
60 1.2.6.2 christos #define VMBUS_TIMESYNC_DORTT(sc) \
61 1.2.6.2 christos (VMBUS_TIMESYNC_MSGVER4((sc)) && (hyperv_tc64 != NULL))
62 1.2.6.2 christos
63 1.2.6.2 christos static int hvtimesync_match(device_t, cfdata_t, void *);
64 1.2.6.2 christos static void hvtimesync_attach(device_t, device_t, void *);
65 1.2.6.2 christos static int hvtimesync_detach(device_t, int);
66 1.2.6.2 christos
67 1.2.6.2 christos static void hvtimesync_channel_cb(void *);
68 1.2.6.2 christos static int hvtimesync_sysctl_setup(device_t);
69 1.2.6.2 christos
70 1.2.6.2 christos struct hvtimesync_softc {
71 1.2.6.2 christos struct vmbusic_softc sc_vmbusic;
72 1.2.6.2 christos };
73 1.2.6.2 christos
74 1.2.6.2 christos CFATTACH_DECL_NEW(hvtimesync, sizeof(struct hvtimesync_softc),
75 1.2.6.2 christos hvtimesync_match, hvtimesync_attach, hvtimesync_detach, NULL);
76 1.2.6.2 christos
77 1.2.6.2 christos static int hvtimesync_ignore_sync;
78 1.2.6.2 christos static int hvtimesnyc_sample_verbose;
79 1.2.6.2 christos static int hvtimesync_sample_thresh = -1;
80 1.2.6.2 christos
81 1.2.6.2 christos static int
82 1.2.6.2 christos hvtimesync_match(device_t parent, cfdata_t cf, void *aux)
83 1.2.6.2 christos {
84 1.2.6.2 christos struct vmbus_attach_args *aa = aux;
85 1.2.6.2 christos
86 1.2.6.2 christos return vmbusic_probe(aa, &hyperv_guid_timesync);
87 1.2.6.2 christos }
88 1.2.6.2 christos
89 1.2.6.2 christos static void
90 1.2.6.2 christos hvtimesync_attach(device_t parent, device_t self, void *aux)
91 1.2.6.2 christos {
92 1.2.6.2 christos struct vmbus_attach_args *aa = aux;
93 1.2.6.2 christos int error;
94 1.2.6.2 christos
95 1.2.6.2 christos aprint_naive("\n");
96 1.2.6.2 christos aprint_normal(": Hyper-V Time Synchronization Service\n");
97 1.2.6.2 christos
98 1.2.6.2 christos error = vmbusic_attach(self, aa, hvtimesync_channel_cb);
99 1.2.6.2 christos if (error)
100 1.2.6.2 christos return;
101 1.2.6.2 christos
102 1.2.6.2 christos (void) pmf_device_register(self, NULL, NULL);
103 1.2.6.2 christos
104 1.2.6.2 christos (void) hvtimesync_sysctl_setup(self);
105 1.2.6.2 christos }
106 1.2.6.2 christos
107 1.2.6.2 christos static int
108 1.2.6.2 christos hvtimesync_detach(device_t self, int flags)
109 1.2.6.2 christos {
110 1.2.6.2 christos int error;
111 1.2.6.2 christos
112 1.2.6.2 christos error = vmbusic_detach(self, flags);
113 1.2.6.2 christos if (error)
114 1.2.6.2 christos return error;
115 1.2.6.2 christos
116 1.2.6.2 christos pmf_device_deregister(self);
117 1.2.6.2 christos
118 1.2.6.2 christos return 0;
119 1.2.6.2 christos }
120 1.2.6.2 christos
121 1.2.6.2 christos static void
122 1.2.6.2 christos do_timesync(struct hvtimesync_softc *sc, uint64_t hvtime, uint64_t sent_tc,
123 1.2.6.2 christos uint8_t tsflags)
124 1.2.6.2 christos {
125 1.2.6.2 christos struct vmbusic_softc *vsc = &sc->sc_vmbusic;
126 1.2.6.2 christos struct timespec vm_ts, hv_ts;
127 1.2.6.2 christos uint64_t hv_ns, vm_ns, rtt = 0;
128 1.2.6.2 christos int64_t diff;
129 1.2.6.2 christos
130 1.2.6.2 christos if (VMBUS_TIMESYNC_DORTT(sc))
131 1.2.6.2 christos rtt = hyperv_tc64() - sent_tc;
132 1.2.6.2 christos
133 1.2.6.2 christos hv_ns = (hvtime - VMBUS_ICMSG_TS_BASE + rtt) * HYPERV_TIMER_NS_FACTOR;
134 1.2.6.2 christos nanotime(&vm_ts);
135 1.2.6.2 christos vm_ns = (vm_ts.tv_sec * NANOSECOND) + vm_ts.tv_nsec;
136 1.2.6.2 christos
137 1.2.6.2 christos if ((tsflags & VMBUS_ICMSG_TS_FLAG_SYNC) && !hvtimesync_ignore_sync) {
138 1.2.6.2 christos #if 0
139 1.2.6.2 christos device_printf(vsc->sc_dev,
140 1.2.6.2 christos "apply sync request, hv: %ju, vm: %ju\n",
141 1.2.6.2 christos (uintmax_t)hv_ns, (uintmax_t)vm_ns);
142 1.2.6.2 christos #endif
143 1.2.6.2 christos hv_ts.tv_sec = hv_ns / NANOSECOND;
144 1.2.6.2 christos hv_ts.tv_nsec = hv_ns % NANOSECOND;
145 1.2.6.2 christos tc_setclock(&hv_ts);
146 1.2.6.2 christos /* Done! */
147 1.2.6.2 christos return;
148 1.2.6.2 christos }
149 1.2.6.2 christos
150 1.2.6.2 christos if ((tsflags & VMBUS_ICMSG_TS_FLAG_SAMPLE) &&
151 1.2.6.2 christos hvtimesync_sample_thresh >= 0) {
152 1.2.6.2 christos if (hvtimesnyc_sample_verbose) {
153 1.2.6.2 christos device_printf(vsc->sc_dev,
154 1.2.6.2 christos "sample request, hv: %ju, vm: %ju\n",
155 1.2.6.2 christos (uintmax_t)hv_ns, (uintmax_t)vm_ns);
156 1.2.6.2 christos }
157 1.2.6.2 christos
158 1.2.6.2 christos if (hv_ns > vm_ns)
159 1.2.6.2 christos diff = hv_ns - vm_ns;
160 1.2.6.2 christos else
161 1.2.6.2 christos diff = vm_ns - hv_ns;
162 1.2.6.2 christos /* nanosec -> millisec */
163 1.2.6.2 christos diff /= 1000000;
164 1.2.6.2 christos
165 1.2.6.2 christos if (diff > hvtimesync_sample_thresh) {
166 1.2.6.2 christos device_printf(vsc->sc_dev,
167 1.2.6.2 christos "apply sample request, hv: %ju, vm: %ju\n",
168 1.2.6.2 christos (uintmax_t)hv_ns, (uintmax_t)vm_ns);
169 1.2.6.2 christos hv_ts.tv_sec = hv_ns / NANOSECOND;
170 1.2.6.2 christos hv_ts.tv_nsec = hv_ns % NANOSECOND;
171 1.2.6.2 christos tc_setclock(&hv_ts);
172 1.2.6.2 christos }
173 1.2.6.2 christos /* Done */
174 1.2.6.2 christos return;
175 1.2.6.2 christos }
176 1.2.6.2 christos }
177 1.2.6.2 christos
178 1.2.6.2 christos static void
179 1.2.6.2 christos hvtimesync_channel_cb(void *arg)
180 1.2.6.2 christos {
181 1.2.6.2 christos struct hvtimesync_softc *sc = arg;
182 1.2.6.2 christos struct vmbusic_softc *vsc = &sc->sc_vmbusic;
183 1.2.6.2 christos struct vmbus_channel *ch = vsc->sc_chan;
184 1.2.6.2 christos struct vmbus_icmsg_hdr *hdr;
185 1.2.6.2 christos uint64_t rid;
186 1.2.6.2 christos uint32_t rlen;
187 1.2.6.2 christos int error;
188 1.2.6.2 christos
189 1.2.6.2 christos error = vmbus_channel_recv(ch, vsc->sc_buf, vsc->sc_buflen,
190 1.2.6.2 christos &rlen, &rid, 0);
191 1.2.6.2 christos if (error || rlen == 0) {
192 1.2.6.2 christos if (error != EAGAIN) {
193 1.2.6.2 christos DPRINTF("%s: timesync error=%d len=%u\n",
194 1.2.6.2 christos device_xname(vsc->sc_dev), error, rlen);
195 1.2.6.2 christos }
196 1.2.6.2 christos return;
197 1.2.6.2 christos }
198 1.2.6.2 christos if (rlen < sizeof(*hdr)) {
199 1.2.6.2 christos DPRINTF("%s: hvtimesync short read len=%u\n",
200 1.2.6.2 christos device_xname(vsc->sc_dev), rlen);
201 1.2.6.2 christos return;
202 1.2.6.2 christos }
203 1.2.6.2 christos
204 1.2.6.2 christos hdr = (struct vmbus_icmsg_hdr *)vsc->sc_buf;
205 1.2.6.2 christos switch (hdr->ic_type) {
206 1.2.6.2 christos case VMBUS_ICMSG_TYPE_NEGOTIATE:
207 1.2.6.2 christos error = vmbusic_negotiate(vsc, hdr, &rlen, VMBUS_TIMESYNC_FWVER,
208 1.2.6.2 christos VMBUS_TIMESYNC_MSGVER);
209 1.2.6.2 christos if (error)
210 1.2.6.2 christos return;
211 1.2.6.2 christos if (VMBUS_TIMESYNC_DORTT(sc)) {
212 1.2.6.2 christos DPRINTF("%s: RTT\n", device_xname(vsc->sc_dev));
213 1.2.6.2 christos }
214 1.2.6.2 christos break;
215 1.2.6.2 christos
216 1.2.6.2 christos case VMBUS_ICMSG_TYPE_TIMESYNC:
217 1.2.6.2 christos if (VMBUS_TIMESYNC_MSGVER4(sc)) {
218 1.2.6.2 christos struct vmbus_icmsg_timesync4 *msg4;
219 1.2.6.2 christos
220 1.2.6.2 christos if (rlen < sizeof(*msg4)) {
221 1.2.6.2 christos DPRINTF("%s: invalid timesync4 len=%u\n",
222 1.2.6.2 christos device_xname(vsc->sc_dev), rlen);
223 1.2.6.2 christos return;
224 1.2.6.2 christos }
225 1.2.6.2 christos
226 1.2.6.2 christos msg4 = (struct vmbus_icmsg_timesync4 *)hdr;
227 1.2.6.2 christos do_timesync(sc, msg4->ic_hvtime, msg4->ic_sent_tc,
228 1.2.6.2 christos msg4->ic_tsflags);
229 1.2.6.2 christos } else {
230 1.2.6.2 christos struct vmbus_icmsg_timesync *msg;
231 1.2.6.2 christos
232 1.2.6.2 christos if (rlen < sizeof(*msg)) {
233 1.2.6.2 christos DPRINTF("%s: invalid timesync len=%u\n",
234 1.2.6.2 christos device_xname(vsc->sc_dev), rlen);
235 1.2.6.2 christos return;
236 1.2.6.2 christos }
237 1.2.6.2 christos
238 1.2.6.2 christos msg = (struct vmbus_icmsg_timesync *)hdr;
239 1.2.6.2 christos do_timesync(sc, msg->ic_hvtime, 0, msg->ic_tsflags);
240 1.2.6.2 christos }
241 1.2.6.2 christos break;
242 1.2.6.2 christos
243 1.2.6.2 christos default:
244 1.2.6.2 christos device_printf(vsc->sc_dev,
245 1.2.6.2 christos "unhandled _timesync message type %u\n", hdr->ic_type);
246 1.2.6.2 christos return;
247 1.2.6.2 christos }
248 1.2.6.2 christos
249 1.2.6.2 christos (void) vmbusic_sendresp(vsc, ch, vsc->sc_buf, rlen, rid);
250 1.2.6.2 christos }
251 1.2.6.2 christos
252 1.2.6.2 christos static int
253 1.2.6.2 christos hvtimesync_sysctl_setup(device_t self)
254 1.2.6.2 christos {
255 1.2.6.2 christos struct hvtimesync_softc *sc = device_private(self);
256 1.2.6.2 christos struct vmbusic_softc *vsc = &sc->sc_vmbusic;
257 1.2.6.2 christos const struct sysctlnode *node;
258 1.2.6.2 christos int error;
259 1.2.6.2 christos
260 1.2.6.2 christos error = sysctl_createv(NULL, 0, NULL, &node,
261 1.2.6.2 christos CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
262 1.2.6.2 christos NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
263 1.2.6.2 christos if (error)
264 1.2.6.2 christos return error;
265 1.2.6.2 christos error = sysctl_createv(NULL, 0, &node, &node,
266 1.2.6.2 christos CTLFLAG_PERMANENT, CTLTYPE_NODE, "hyperv", NULL,
267 1.2.6.2 christos NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
268 1.2.6.2 christos if (error)
269 1.2.6.2 christos return error;
270 1.2.6.2 christos
271 1.2.6.2 christos error = sysctl_createv(&vsc->sc_log, 0, &node, &node,
272 1.2.6.2 christos 0, CTLTYPE_NODE, "timesync", NULL,
273 1.2.6.2 christos NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
274 1.2.6.2 christos if (error)
275 1.2.6.2 christos return error;
276 1.2.6.2 christos
277 1.2.6.2 christos error = sysctl_createv(&vsc->sc_log, 0, &node, NULL,
278 1.2.6.2 christos CTLFLAG_READWRITE,
279 1.2.6.2 christos CTLTYPE_INT, "ignore_sync", NULL,
280 1.2.6.2 christos NULL, 0, &hvtimesync_ignore_sync, 0,
281 1.2.6.2 christos CTL_CREATE, CTL_EOL);
282 1.2.6.2 christos if (error)
283 1.2.6.2 christos return error;
284 1.2.6.2 christos error = sysctl_createv(&vsc->sc_log, 0, &node, NULL,
285 1.2.6.2 christos CTLFLAG_READWRITE,
286 1.2.6.2 christos CTLTYPE_INT, "sample_verbose", NULL,
287 1.2.6.2 christos NULL, 0, &hvtimesnyc_sample_verbose, 0,
288 1.2.6.2 christos CTL_CREATE, CTL_EOL);
289 1.2.6.2 christos if (error)
290 1.2.6.2 christos return error;
291 1.2.6.2 christos error = sysctl_createv(&vsc->sc_log, 0, &node, NULL,
292 1.2.6.2 christos CTLFLAG_READWRITE,
293 1.2.6.2 christos CTLTYPE_INT, "sample_thresh", NULL,
294 1.2.6.2 christos NULL, 0, &hvtimesync_sample_thresh, 0,
295 1.2.6.2 christos CTL_CREATE, CTL_EOL);
296 1.2.6.2 christos if (error)
297 1.2.6.2 christos return error;
298 1.2.6.2 christos
299 1.2.6.2 christos return 0;
300 1.2.6.2 christos }
301 1.2.6.2 christos
302 1.2.6.2 christos MODULE(MODULE_CLASS_DRIVER, hvtimesync, "vmbus");
303 1.2.6.2 christos
304 1.2.6.2 christos #ifdef _MODULE
305 1.2.6.2 christos #include "ioconf.c"
306 1.2.6.2 christos #endif
307 1.2.6.2 christos
308 1.2.6.2 christos static int
309 1.2.6.2 christos hvtimesync_modcmd(modcmd_t cmd, void *aux)
310 1.2.6.2 christos {
311 1.2.6.2 christos int error = 0;
312 1.2.6.2 christos
313 1.2.6.2 christos switch (cmd) {
314 1.2.6.2 christos case MODULE_CMD_INIT:
315 1.2.6.2 christos #ifdef _MODULE
316 1.2.6.2 christos error = config_init_component(cfdriver_ioconf_hvtimesync,
317 1.2.6.2 christos cfattach_ioconf_hvtimesync, cfdata_ioconf_hvtimesync);
318 1.2.6.2 christos #endif
319 1.2.6.2 christos break;
320 1.2.6.2 christos
321 1.2.6.2 christos case MODULE_CMD_FINI:
322 1.2.6.2 christos #ifdef _MODULE
323 1.2.6.2 christos error = config_fini_component(cfdriver_ioconf_hvtimesync,
324 1.2.6.2 christos cfattach_ioconf_hvtimesync, cfdata_ioconf_hvtimesync);
325 1.2.6.2 christos #endif
326 1.2.6.2 christos break;
327 1.2.6.2 christos
328 1.2.6.2 christos case MODULE_CMD_AUTOUNLOAD:
329 1.2.6.2 christos error = EBUSY;
330 1.2.6.2 christos break;
331 1.2.6.2 christos
332 1.2.6.2 christos default:
333 1.2.6.2 christos error = ENOTTY;
334 1.2.6.2 christos break;
335 1.2.6.2 christos }
336 1.2.6.2 christos
337 1.2.6.2 christos return error;
338 1.2.6.2 christos }
339