hyperv/ic: Update total message size if negotiate message size grows.
[freebsd.git] / sys / dev / hyperv / utilities / hv_timesync.c
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  * $FreeBSD$
27  */
28
29 /*
30  * A common driver for all hyper-V util services.
31  */
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/reboot.h>
39 #include <sys/timetc.h>
40 #include <sys/syscallsubr.h>
41 #include <sys/systm.h>
42 #include <sys/taskqueue.h>
43
44 #include <dev/hyperv/include/hyperv.h>
45 #include <dev/hyperv/include/vmbus.h>
46 #include <dev/hyperv/utilities/hv_utilreg.h>
47 #include "hv_util.h"
48 #include "vmbus_if.h"
49
50 #define HV_WLTIMEDELTA              116444736000000000L     /* in 100ns unit */
51 #define HV_ICTIMESYNCFLAG_PROBE     0
52 #define HV_ICTIMESYNCFLAG_SYNC      1
53 #define HV_ICTIMESYNCFLAG_SAMPLE    2
54 #define HV_NANO_SEC_PER_SEC         1000000000
55
56 /* Time Sync data */
57 typedef struct {
58         uint64_t data;
59 } time_sync_data;
60
61 static const struct vmbus_ic_desc vmbus_timesync_descs[] = {
62         {
63                 .ic_guid = { .hv_guid = {
64                     0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
65                     0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } },
66                 .ic_desc = "Hyper-V Timesync"
67         },
68         VMBUS_IC_DESC_END
69 };
70
71 struct hv_ictimesync_data {
72         uint64_t    parenttime;
73         uint64_t    childtime;
74         uint64_t    roundtriptime;
75         uint8_t     flags;
76 } __packed;
77
78 typedef struct hv_timesync_sc {
79         hv_util_sc      util_sc;
80         struct task     task;
81         time_sync_data  time_msg;
82 } hv_timesync_sc;
83
84 /**
85  * Set host time based on time sync message from host
86  */
87 static void
88 hv_set_host_time(void *context, int pending)
89 {
90         hv_timesync_sc *softc = (hv_timesync_sc*)context;
91         uint64_t hosttime = softc->time_msg.data;
92         struct timespec guest_ts, host_ts;
93         uint64_t host_tns;
94         int64_t diff;
95         int error;
96
97         host_tns = (hosttime - HV_WLTIMEDELTA) * 100;
98         host_ts.tv_sec = (time_t)(host_tns/HV_NANO_SEC_PER_SEC);
99         host_ts.tv_nsec = (long)(host_tns%HV_NANO_SEC_PER_SEC);
100
101         nanotime(&guest_ts);
102
103         diff = (int64_t)host_ts.tv_sec - (int64_t)guest_ts.tv_sec;
104
105         /*
106          * If host differs by 5 seconds then make the guest catch up
107          */
108         if (diff > 5 || diff < -5) {
109                 error = kern_clock_settime(curthread, CLOCK_REALTIME,
110                     &host_ts);
111         }
112 }
113
114 /**
115  * @brief Synchronize time with host after reboot, restore, etc.
116  *
117  * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
118  * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
119  * message after the timesync channel is opened. Since the hv_utils module is
120  * loaded after hv_vmbus, the first message is usually missed. The other
121  * thing is, systime is automatically set to emulated hardware clock which may
122  * not be UTC time or in the same time zone. So, to override these effects, we
123  * use the first 50 time samples for initial system time setting.
124  */
125 static inline
126 void hv_adj_guesttime(hv_timesync_sc *sc, uint64_t hosttime, uint8_t flags)
127 {
128         sc->time_msg.data = hosttime;
129
130         if (((flags & HV_ICTIMESYNCFLAG_SYNC) != 0) ||
131                 ((flags & HV_ICTIMESYNCFLAG_SAMPLE) != 0)) {
132                 taskqueue_enqueue(taskqueue_thread, &sc->task);
133         }
134 }
135
136 /**
137  * Time Sync Channel message handler
138  */
139 static void
140 hv_timesync_cb(struct vmbus_channel *channel, void *context)
141 {
142         hv_vmbus_icmsg_hdr*     icmsghdrp;
143         uint32_t                recvlen;
144         uint64_t                requestId;
145         int                     ret;
146         uint8_t*                time_buf;
147         struct hv_ictimesync_data* timedatap;
148         hv_timesync_sc          *softc;
149
150         softc = (hv_timesync_sc*)context;
151         time_buf = softc->util_sc.receive_buffer;
152
153         recvlen = softc->util_sc.ic_buflen;
154         ret = vmbus_chan_recv(channel, time_buf, &recvlen, &requestId);
155         KASSERT(ret != ENOBUFS, ("hvtimesync recvbuf is not large enough"));
156         /* XXX check recvlen to make sure that it contains enough data */
157
158         if ((ret == 0) && recvlen > 0) {
159             icmsghdrp = (struct hv_vmbus_icmsg_hdr *) &time_buf[
160                 sizeof(struct hv_vmbus_pipe_hdr)];
161
162             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
163                 int error;
164
165                 error = vmbus_ic_negomsg(&softc->util_sc, time_buf, &recvlen);
166                 if (error)
167                         return;
168             } else {
169                 timedatap = (struct hv_ictimesync_data *) &time_buf[
170                     sizeof(struct hv_vmbus_pipe_hdr) +
171                         sizeof(struct hv_vmbus_icmsg_hdr)];
172                 hv_adj_guesttime(softc, timedatap->parenttime, timedatap->flags);
173             }
174
175             icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION
176                 | HV_ICMSGHDRFLAG_RESPONSE;
177
178             vmbus_chan_send(channel, VMBUS_CHANPKT_TYPE_INBAND, 0,
179                 time_buf, recvlen, requestId);
180         }
181 }
182
183 static int
184 hv_timesync_probe(device_t dev)
185 {
186
187         return (vmbus_ic_probe(dev, vmbus_timesync_descs));
188 }
189
190 static int
191 hv_timesync_attach(device_t dev)
192 {
193         hv_timesync_sc *softc = device_get_softc(dev);
194
195         TASK_INIT(&softc->task, 1, hv_set_host_time, softc);
196         return hv_util_attach(dev, hv_timesync_cb);
197 }
198
199 static int
200 hv_timesync_detach(device_t dev)
201 {
202         hv_timesync_sc *softc = device_get_softc(dev);
203
204         taskqueue_drain(taskqueue_thread, &softc->task);
205         return hv_util_detach(dev);
206 }
207
208 static device_method_t timesync_methods[] = {
209         /* Device interface */
210         DEVMETHOD(device_probe, hv_timesync_probe),
211         DEVMETHOD(device_attach, hv_timesync_attach),
212         DEVMETHOD(device_detach, hv_timesync_detach),
213         { 0, 0 }
214 };
215
216 static driver_t timesync_driver = { "hvtimesync", timesync_methods, sizeof(hv_timesync_sc)};
217
218 static devclass_t timesync_devclass;
219
220 DRIVER_MODULE(hv_timesync, vmbus, timesync_driver, timesync_devclass, NULL, NULL);
221 MODULE_VERSION(hv_timesync, 1);
222 MODULE_DEPEND(hv_timesync, vmbus, 1, 1, 1);