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