1 /* $NetBSD: hvshutdown.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: hvshutdown.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_shutdown.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/sysmon/sysmonvar.h>
44 #include <dev/sysmon/sysmon_taskq.h>
45
46 #include <dev/hyperv/vmbusvar.h>
47 #include <dev/hyperv/vmbusicreg.h>
48 #include <dev/hyperv/vmbusicvar.h>
49
50 #define VMBUS_SHUTDOWN_FWVER_MAJOR 3
51 #define VMBUS_SHUTDOWN_FWVER \
52 VMBUS_IC_VERSION(VMBUS_SHUTDOWN_FWVER_MAJOR, 0)
53
54 #define VMBUS_SHUTDOWN_MSGVER_MAJOR 3
55 #define VMBUS_SHUTDOWN_MSGVER \
56 VMBUS_IC_VERSION(VMBUS_SHUTDOWN_MSGVER_MAJOR, 0)
57
58 static int hvshutdown_match(device_t, cfdata_t, void *);
59 static void hvshutdown_attach(device_t, device_t, void *);
60 static int hvshutdown_detach(device_t, int);
61
62 static void hvshutdown_channel_cb(void *);
63
64 struct hvshutdown_softc {
65 struct vmbusic_softc sc_vmbusic;
66
67 struct sysmon_pswitch sc_smpsw;
68 };
69
70 CFATTACH_DECL_NEW(hvshutdown, sizeof(struct hvshutdown_softc),
71 hvshutdown_match, hvshutdown_attach, hvshutdown_detach, NULL);
72
73 static int
hvshutdown_match(device_t parent,cfdata_t cf,void * aux)74 hvshutdown_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 struct vmbus_attach_args *aa = aux;
77
78 return vmbusic_probe(aa, &hyperv_guid_shutdown);
79 }
80
81 static void
hvshutdown_attach(device_t parent,device_t self,void * aux)82 hvshutdown_attach(device_t parent, device_t self, void *aux)
83 {
84 struct hvshutdown_softc *sc = device_private(self);
85 struct vmbus_attach_args *aa = aux;
86 int error;
87
88 aprint_naive("\n");
89 aprint_normal(": Hyper-V Guest Shutdown Service\n");
90
91 error = vmbusic_attach(self, aa, hvshutdown_channel_cb);
92 if (error)
93 return;
94
95 (void) pmf_device_register(self, NULL, NULL);
96
97 sysmon_task_queue_init();
98
99 sc->sc_smpsw.smpsw_name = device_xname(self);
100 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
101 (void) sysmon_pswitch_register(&sc->sc_smpsw);
102 }
103
104 static int
hvshutdown_detach(device_t self,int flags)105 hvshutdown_detach(device_t self, int flags)
106 {
107 struct hvshutdown_softc *sc = device_private(self);
108 int error;
109
110 error = vmbusic_detach(self, flags);
111 if (error)
112 return error;
113
114 pmf_device_deregister(self);
115 sysmon_pswitch_unregister(&sc->sc_smpsw);
116
117 return 0;
118 }
119
120 static void
hvshutdown_do_shutdown(void * arg)121 hvshutdown_do_shutdown(void *arg)
122 {
123 struct hvshutdown_softc *sc = arg;
124
125 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
126 }
127
128 static void
hvshutdown_channel_cb(void * arg)129 hvshutdown_channel_cb(void *arg)
130 {
131 struct hvshutdown_softc *sc = arg;
132 struct vmbusic_softc *vsc = &sc->sc_vmbusic;
133 struct vmbus_channel *ch = vsc->sc_chan;
134 struct vmbus_icmsg_hdr *hdr;
135 struct vmbus_icmsg_shutdown *msg;
136 uint64_t rid;
137 uint32_t rlen;
138 int error;
139 bool do_shutdown = false;
140
141 error = vmbus_channel_recv(ch, vsc->sc_buf, vsc->sc_buflen,
142 &rlen, &rid, 0);
143 if (error || rlen == 0) {
144 if (error != EAGAIN) {
145 DPRINTF("%s: shutdown error=%d len=%u\n",
146 device_xname(vsc->sc_dev), error, rlen);
147 }
148 return;
149 }
150 if (rlen < sizeof(*hdr)) {
151 DPRINTF("%s: shutdown short read len=%u\n",
152 device_xname(vsc->sc_dev), rlen);
153 return;
154 }
155
156 hdr = (struct vmbus_icmsg_hdr *)vsc->sc_buf;
157 switch (hdr->ic_type) {
158 case VMBUS_ICMSG_TYPE_NEGOTIATE:
159 error = vmbusic_negotiate(vsc, hdr, &rlen, VMBUS_SHUTDOWN_FWVER,
160 VMBUS_SHUTDOWN_MSGVER);
161 if (error)
162 return;
163 break;
164
165 case VMBUS_ICMSG_TYPE_SHUTDOWN:
166 if (rlen < VMBUS_ICMSG_SHUTDOWN_SIZE_MIN) {
167 DPRINTF("%s: invalid shutdown len=%u\n",
168 device_xname(vsc->sc_dev), rlen);
169 return;
170 }
171
172 msg = (struct vmbus_icmsg_shutdown *)hdr;
173 if (msg->ic_haltflags == 0 || msg->ic_haltflags == 1) {
174 device_printf(vsc->sc_dev, "shutdown requested\n");
175 hdr->ic_status = VMBUS_ICMSG_STATUS_OK;
176 do_shutdown = true;
177 } else {
178 device_printf(vsc->sc_dev,
179 "unknown shutdown flags 0x%08x\n",
180 msg->ic_haltflags);
181 hdr->ic_status = VMBUS_ICMSG_STATUS_FAIL;
182 }
183 break;
184
185 default:
186 device_printf(vsc->sc_dev,
187 "unhandled shutdown message type %u\n", hdr->ic_type);
188 return;
189 }
190
191 (void) vmbusic_sendresp(vsc, ch, vsc->sc_buf, rlen, rid);
192
193 if (do_shutdown)
194 sysmon_task_queue_sched(0, hvshutdown_do_shutdown, sc);
195 }
196
197 MODULE(MODULE_CLASS_DRIVER, hvshutdown, "vmbus");
198
199 #ifdef _MODULE
200 #include "ioconf.c"
201 #endif
202
203 static int
hvshutdown_modcmd(modcmd_t cmd,void * aux)204 hvshutdown_modcmd(modcmd_t cmd, void *aux)
205 {
206 int error = 0;
207
208 switch (cmd) {
209 case MODULE_CMD_INIT:
210 #ifdef _MODULE
211 error = config_init_component(cfdriver_ioconf_hvshutdown,
212 cfattach_ioconf_hvshutdown, cfdata_ioconf_hvshutdown);
213 #endif
214 break;
215
216 case MODULE_CMD_FINI:
217 #ifdef _MODULE
218 error = config_fini_component(cfdriver_ioconf_hvshutdown,
219 cfattach_ioconf_hvshutdown, cfdata_ioconf_hvshutdown);
220 #endif
221 break;
222
223 case MODULE_CMD_AUTOUNLOAD:
224 error = EBUSY;
225 break;
226
227 default:
228 error = ENOTTY;
229 break;
230 }
231
232 return error;
233 }
234