xref: /freebsd/sys/dev/hyperv/utilities/vmbus_heartbeat.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
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 
27 #include <sys/param.h>
28 #include <sys/bus.h>
29 #include <sys/kernel.h>
30 #include <sys/module.h>
31 #include <sys/systm.h>
32 
33 #include <dev/hyperv/include/hyperv.h>
34 #include <dev/hyperv/include/vmbus.h>
35 #include <dev/hyperv/utilities/vmbus_icreg.h>
36 #include <dev/hyperv/utilities/vmbus_icvar.h>
37 
38 #define VMBUS_HEARTBEAT_FWVER_MAJOR	3
39 #define VMBUS_HEARTBEAT_FWVER		\
40 	VMBUS_IC_VERSION(VMBUS_HEARTBEAT_FWVER_MAJOR, 0)
41 
42 #define VMBUS_HEARTBEAT_MSGVER_MAJOR	3
43 #define VMBUS_HEARTBEAT_MSGVER		\
44 	VMBUS_IC_VERSION(VMBUS_HEARTBEAT_MSGVER_MAJOR, 0)
45 
46 static int			vmbus_heartbeat_probe(device_t);
47 static int			vmbus_heartbeat_attach(device_t);
48 
49 static const struct vmbus_ic_desc vmbus_heartbeat_descs[] = {
50 	{
51 		.ic_guid = { .hv_guid = {
52 		    0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
53 		    0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d} },
54 		.ic_desc = "Hyper-V Heartbeat"
55 	},
56 	VMBUS_IC_DESC_END
57 };
58 
59 static device_method_t vmbus_heartbeat_methods[] = {
60 	/* Device interface */
61 	DEVMETHOD(device_probe,		vmbus_heartbeat_probe),
62 	DEVMETHOD(device_attach,	vmbus_heartbeat_attach),
63 	DEVMETHOD(device_detach,	vmbus_ic_detach),
64 	DEVMETHOD_END
65 };
66 
67 static driver_t vmbus_heartbeat_driver = {
68 	"hvheartbeat",
69 	vmbus_heartbeat_methods,
70 	sizeof(struct vmbus_ic_softc)
71 };
72 
73 DRIVER_MODULE(hv_heartbeat, vmbus, vmbus_heartbeat_driver, NULL, NULL);
74 MODULE_VERSION(hv_heartbeat, 1);
75 MODULE_DEPEND(hv_heartbeat, vmbus, 1, 1, 1);
76 
77 static void
vmbus_heartbeat_cb(struct vmbus_channel * chan,void * xsc)78 vmbus_heartbeat_cb(struct vmbus_channel *chan, void *xsc)
79 {
80 	struct vmbus_ic_softc *sc = xsc;
81 	struct vmbus_icmsg_hdr *hdr;
82 	int dlen, error;
83 	uint64_t xactid;
84 	void *data;
85 
86 	/*
87 	 * Receive request.
88 	 */
89 	data = sc->ic_buf;
90 	dlen = sc->ic_buflen;
91 	error = vmbus_chan_recv(chan, data, &dlen, &xactid);
92 	KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
93 	if (error)
94 		return;
95 
96 	if (dlen < sizeof(*hdr)) {
97 		device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
98 		return;
99 	}
100 	hdr = data;
101 
102 	/*
103 	 * Update request, which will be echoed back as response.
104 	 */
105 	switch (hdr->ic_type) {
106 	case VMBUS_ICMSG_TYPE_NEGOTIATE:
107 		error = vmbus_ic_negomsg(sc, data, &dlen,
108 		    VMBUS_HEARTBEAT_FWVER, VMBUS_HEARTBEAT_MSGVER);
109 		if (error)
110 			return;
111 		break;
112 
113 	case VMBUS_ICMSG_TYPE_HEARTBEAT:
114 		/* Only ic_seq is a must */
115 		if (dlen < VMBUS_ICMSG_HEARTBEAT_SIZE_MIN) {
116 			device_printf(sc->ic_dev, "invalid heartbeat len %d\n",
117 			    dlen);
118 			return;
119 		}
120 		((struct vmbus_icmsg_heartbeat *)data)->ic_seq++;
121 		break;
122 
123 	default:
124 		device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
125 		break;
126 	}
127 
128 	/*
129 	 * Send response by echoing the request back.
130 	 */
131 	vmbus_ic_sendresp(sc, chan, data, dlen, xactid);
132 }
133 
134 static int
vmbus_heartbeat_probe(device_t dev)135 vmbus_heartbeat_probe(device_t dev)
136 {
137 
138 	return (vmbus_ic_probe(dev, vmbus_heartbeat_descs));
139 }
140 
141 static int
vmbus_heartbeat_attach(device_t dev)142 vmbus_heartbeat_attach(device_t dev)
143 {
144 
145 	return (vmbus_ic_attach(dev, vmbus_heartbeat_cb));
146 }
147