1 /* $NetBSD: hvheartbeat.c,v 1.2 2019/03/01 08:17:51 nonaka Exp $ */
2
3 /*-
4 * Copyright (c) 2014,2016 Microsoft Corp.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #ifdef __KERNEL_RCSID
31 __KERNEL_RCSID(0, "$NetBSD: hvheartbeat.c,v 1.2 2019/03/01 08:17:51 nonaka Exp $");
32 #endif
33 #ifdef __FBSDID
34 __FBSDID("$FreeBSD: head/sys/dev/hyperv/utilities/vmbus_heartbeat.c 310324 2016-12-20 09:46:14Z sephe $");
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/module.h>
41 #include <sys/pmf.h>
42
43 #include <dev/hyperv/vmbusvar.h>
44 #include <dev/hyperv/vmbusicreg.h>
45 #include <dev/hyperv/vmbusicvar.h>
46
47 #define VMBUS_HEARTBEAT_FWVER_MAJOR 3
48 #define VMBUS_HEARTBEAT_FWVER \
49 VMBUS_IC_VERSION(VMBUS_HEARTBEAT_FWVER_MAJOR, 0)
50
51 #define VMBUS_HEARTBEAT_MSGVER_MAJOR 3
52 #define VMBUS_HEARTBEAT_MSGVER \
53 VMBUS_IC_VERSION(VMBUS_HEARTBEAT_MSGVER_MAJOR, 0)
54
55 static int hvheartbeat_match(device_t, cfdata_t, void *);
56 static void hvheartbeat_attach(device_t, device_t, void *);
57 static int hvheartbeat_detach(device_t, int);
58
59 static void hvheartbeat_channel_cb(void *);
60
61 struct hvheartbeat_softc {
62 struct vmbusic_softc sc_vmbusic;
63 };
64
65 CFATTACH_DECL_NEW(hvheartbeat, sizeof(struct hvheartbeat_softc),
66 hvheartbeat_match, hvheartbeat_attach, hvheartbeat_detach, NULL);
67
68 static int
hvheartbeat_match(device_t parent,cfdata_t cf,void * aux)69 hvheartbeat_match(device_t parent, cfdata_t cf, void *aux)
70 {
71 struct vmbus_attach_args *aa = aux;
72
73 return vmbusic_probe(aa, &hyperv_guid_heartbeat);
74 }
75
76 static void
hvheartbeat_attach(device_t parent,device_t self,void * aux)77 hvheartbeat_attach(device_t parent, device_t self, void *aux)
78 {
79 struct vmbus_attach_args *aa = aux;
80 int error;
81
82 aprint_naive("\n");
83 aprint_normal(": Hyper-V Heartbeat Service\n");
84
85 error = vmbusic_attach(self, aa, hvheartbeat_channel_cb);
86 if (error)
87 return;
88
89 (void) pmf_device_register(self, NULL, NULL);
90 }
91
92 static int
hvheartbeat_detach(device_t self,int flags)93 hvheartbeat_detach(device_t self, int flags)
94 {
95 int error;
96
97 error = vmbusic_detach(self, flags);
98 if (error)
99 return error;
100
101 pmf_device_deregister(self);
102
103 return 0;
104 }
105
106 static void
hvheartbeat_channel_cb(void * arg)107 hvheartbeat_channel_cb(void *arg)
108 {
109 struct hvheartbeat_softc *sc = arg;
110 struct vmbusic_softc *vsc = &sc->sc_vmbusic;
111 struct vmbus_channel *ch = vsc->sc_chan;
112 struct vmbus_icmsg_hdr *hdr;
113 struct vmbus_icmsg_heartbeat *msg;
114 uint64_t rid;
115 uint32_t rlen;
116 int error;
117
118 error = vmbus_channel_recv(ch, vsc->sc_buf, vsc->sc_buflen,
119 &rlen, &rid, 0);
120 if (error || rlen == 0) {
121 if (error != EAGAIN) {
122 DPRINTF("%s: heartbeat error=%d len=%u\n",
123 device_xname(vsc->sc_dev), error, rlen);
124 }
125 return;
126 }
127 if (rlen < sizeof(*hdr)) {
128 DPRINTF("%s: heartbeat short read len=%u\n",
129 device_xname(vsc->sc_dev), rlen);
130 return;
131 }
132
133 hdr = (struct vmbus_icmsg_hdr *)vsc->sc_buf;
134 switch (hdr->ic_type) {
135 case VMBUS_ICMSG_TYPE_NEGOTIATE:
136 error = vmbusic_negotiate(vsc, hdr, &rlen,
137 VMBUS_HEARTBEAT_FWVER, VMBUS_HEARTBEAT_MSGVER);
138 if (error)
139 return;
140 break;
141
142 case VMBUS_ICMSG_TYPE_HEARTBEAT:
143 if (rlen < VMBUS_ICMSG_HEARTBEAT_SIZE_MIN) {
144 DPRINTF("%s: invalid heartbeat len=%u\n",
145 device_xname(vsc->sc_dev), rlen);
146 return;
147 }
148
149 msg = (struct vmbus_icmsg_heartbeat *)hdr;
150 msg->ic_seq++;
151 break;
152
153 default:
154 device_printf(vsc->sc_dev,
155 "unhandled heartbeat message type %u\n", hdr->ic_type);
156 return;
157 }
158
159 (void) vmbusic_sendresp(vsc, ch, vsc->sc_buf, rlen, rid);
160 }
161
162 MODULE(MODULE_CLASS_DRIVER, hvheartbeat, "vmbus");
163
164 #ifdef _MODULE
165 #include "ioconf.c"
166 #endif
167
168 static int
hvheartbeat_modcmd(modcmd_t cmd,void * aux)169 hvheartbeat_modcmd(modcmd_t cmd, void *aux)
170 {
171 int error = 0;
172
173 switch (cmd) {
174 case MODULE_CMD_INIT:
175 #ifdef _MODULE
176 error = config_init_component(cfdriver_ioconf_hvheartbeat,
177 cfattach_ioconf_hvheartbeat, cfdata_ioconf_hvheartbeat);
178 #endif
179 break;
180
181 case MODULE_CMD_FINI:
182 #ifdef _MODULE
183 error = config_fini_component(cfdriver_ioconf_hvheartbeat,
184 cfattach_ioconf_hvheartbeat, cfdata_ioconf_hvheartbeat);
185 #endif
186 break;
187
188 case MODULE_CMD_AUTOUNLOAD:
189 error = EBUSY;
190 break;
191
192 default:
193 error = ENOTTY;
194 break;
195 }
196
197 return error;
198 }
199