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