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