hvshutdown.c revision 1.2.6.2 1 1.2.6.2 christos /* $NetBSD: hvshutdown.c,v 1.2.6.2 2019/06/10 22:07:09 christos Exp $ */
2 1.2.6.2 christos
3 1.2.6.2 christos /*-
4 1.2.6.2 christos * Copyright (c) 2014,2016 Microsoft Corp.
5 1.2.6.2 christos * All rights reserved.
6 1.2.6.2 christos *
7 1.2.6.2 christos * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 christos * modification, are permitted provided that the following conditions
9 1.2.6.2 christos * are met:
10 1.2.6.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 christos * notice unmodified, this list of conditions, and the following
12 1.2.6.2 christos * disclaimer.
13 1.2.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.6.2 christos * notice, this list of conditions and the following disclaimer in the
15 1.2.6.2 christos * documentation and/or other materials provided with the distribution.
16 1.2.6.2 christos *
17 1.2.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.2.6.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.2.6.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.2.6.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.2.6.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.2.6.2 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.2.6.2 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.2.6.2 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.2.6.2 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.2.6.2 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.2.6.2 christos */
28 1.2.6.2 christos
29 1.2.6.2 christos #include <sys/cdefs.h>
30 1.2.6.2 christos #ifdef __KERNEL_RCSID
31 1.2.6.2 christos __KERNEL_RCSID(0, "$NetBSD: hvshutdown.c,v 1.2.6.2 2019/06/10 22:07:09 christos Exp $");
32 1.2.6.2 christos #endif
33 1.2.6.2 christos #ifdef __FBSDID
34 1.2.6.2 christos __FBSDID("$FreeBSD: head/sys/dev/hyperv/utilities/vmbus_shutdown.c 310324 2016-12-20 09:46:14Z sephe $");
35 1.2.6.2 christos #endif
36 1.2.6.2 christos
37 1.2.6.2 christos #include <sys/param.h>
38 1.2.6.2 christos #include <sys/systm.h>
39 1.2.6.2 christos #include <sys/device.h>
40 1.2.6.2 christos #include <sys/module.h>
41 1.2.6.2 christos #include <sys/pmf.h>
42 1.2.6.2 christos
43 1.2.6.2 christos #include <dev/sysmon/sysmonvar.h>
44 1.2.6.2 christos #include <dev/sysmon/sysmon_taskq.h>
45 1.2.6.2 christos
46 1.2.6.2 christos #include <dev/hyperv/vmbusvar.h>
47 1.2.6.2 christos #include <dev/hyperv/vmbusicreg.h>
48 1.2.6.2 christos #include <dev/hyperv/vmbusicvar.h>
49 1.2.6.2 christos
50 1.2.6.2 christos #define VMBUS_SHUTDOWN_FWVER_MAJOR 3
51 1.2.6.2 christos #define VMBUS_SHUTDOWN_FWVER \
52 1.2.6.2 christos VMBUS_IC_VERSION(VMBUS_SHUTDOWN_FWVER_MAJOR, 0)
53 1.2.6.2 christos
54 1.2.6.2 christos #define VMBUS_SHUTDOWN_MSGVER_MAJOR 3
55 1.2.6.2 christos #define VMBUS_SHUTDOWN_MSGVER \
56 1.2.6.2 christos VMBUS_IC_VERSION(VMBUS_SHUTDOWN_MSGVER_MAJOR, 0)
57 1.2.6.2 christos
58 1.2.6.2 christos static int hvshutdown_match(device_t, cfdata_t, void *);
59 1.2.6.2 christos static void hvshutdown_attach(device_t, device_t, void *);
60 1.2.6.2 christos static int hvshutdown_detach(device_t, int);
61 1.2.6.2 christos
62 1.2.6.2 christos static void hvshutdown_channel_cb(void *);
63 1.2.6.2 christos
64 1.2.6.2 christos struct hvshutdown_softc {
65 1.2.6.2 christos struct vmbusic_softc sc_vmbusic;
66 1.2.6.2 christos
67 1.2.6.2 christos struct sysmon_pswitch sc_smpsw;
68 1.2.6.2 christos };
69 1.2.6.2 christos
70 1.2.6.2 christos CFATTACH_DECL_NEW(hvshutdown, sizeof(struct hvshutdown_softc),
71 1.2.6.2 christos hvshutdown_match, hvshutdown_attach, hvshutdown_detach, NULL);
72 1.2.6.2 christos
73 1.2.6.2 christos static int
74 1.2.6.2 christos hvshutdown_match(device_t parent, cfdata_t cf, void *aux)
75 1.2.6.2 christos {
76 1.2.6.2 christos struct vmbus_attach_args *aa = aux;
77 1.2.6.2 christos
78 1.2.6.2 christos return vmbusic_probe(aa, &hyperv_guid_shutdown);
79 1.2.6.2 christos }
80 1.2.6.2 christos
81 1.2.6.2 christos static void
82 1.2.6.2 christos hvshutdown_attach(device_t parent, device_t self, void *aux)
83 1.2.6.2 christos {
84 1.2.6.2 christos struct hvshutdown_softc *sc = device_private(self);
85 1.2.6.2 christos struct vmbus_attach_args *aa = aux;
86 1.2.6.2 christos int error;
87 1.2.6.2 christos
88 1.2.6.2 christos aprint_naive("\n");
89 1.2.6.2 christos aprint_normal(": Hyper-V Guest Shutdown Service\n");
90 1.2.6.2 christos
91 1.2.6.2 christos error = vmbusic_attach(self, aa, hvshutdown_channel_cb);
92 1.2.6.2 christos if (error)
93 1.2.6.2 christos return;
94 1.2.6.2 christos
95 1.2.6.2 christos (void) pmf_device_register(self, NULL, NULL);
96 1.2.6.2 christos
97 1.2.6.2 christos sysmon_task_queue_init();
98 1.2.6.2 christos
99 1.2.6.2 christos sc->sc_smpsw.smpsw_name = device_xname(self);
100 1.2.6.2 christos sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
101 1.2.6.2 christos (void) sysmon_pswitch_register(&sc->sc_smpsw);
102 1.2.6.2 christos }
103 1.2.6.2 christos
104 1.2.6.2 christos static int
105 1.2.6.2 christos hvshutdown_detach(device_t self, int flags)
106 1.2.6.2 christos {
107 1.2.6.2 christos struct hvshutdown_softc *sc = device_private(self);
108 1.2.6.2 christos int error;
109 1.2.6.2 christos
110 1.2.6.2 christos error = vmbusic_detach(self, flags);
111 1.2.6.2 christos if (error)
112 1.2.6.2 christos return error;
113 1.2.6.2 christos
114 1.2.6.2 christos pmf_device_deregister(self);
115 1.2.6.2 christos sysmon_pswitch_unregister(&sc->sc_smpsw);
116 1.2.6.2 christos
117 1.2.6.2 christos return 0;
118 1.2.6.2 christos }
119 1.2.6.2 christos
120 1.2.6.2 christos static void
121 1.2.6.2 christos hvshutdown_do_shutdown(void *arg)
122 1.2.6.2 christos {
123 1.2.6.2 christos struct hvshutdown_softc *sc = arg;
124 1.2.6.2 christos
125 1.2.6.2 christos sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
126 1.2.6.2 christos }
127 1.2.6.2 christos
128 1.2.6.2 christos static void
129 1.2.6.2 christos hvshutdown_channel_cb(void *arg)
130 1.2.6.2 christos {
131 1.2.6.2 christos struct hvshutdown_softc *sc = arg;
132 1.2.6.2 christos struct vmbusic_softc *vsc = &sc->sc_vmbusic;
133 1.2.6.2 christos struct vmbus_channel *ch = vsc->sc_chan;
134 1.2.6.2 christos struct vmbus_icmsg_hdr *hdr;
135 1.2.6.2 christos struct vmbus_icmsg_shutdown *msg;
136 1.2.6.2 christos uint64_t rid;
137 1.2.6.2 christos uint32_t rlen;
138 1.2.6.2 christos int error;
139 1.2.6.2 christos bool do_shutdown = false;
140 1.2.6.2 christos
141 1.2.6.2 christos error = vmbus_channel_recv(ch, vsc->sc_buf, vsc->sc_buflen,
142 1.2.6.2 christos &rlen, &rid, 0);
143 1.2.6.2 christos if (error || rlen == 0) {
144 1.2.6.2 christos if (error != EAGAIN) {
145 1.2.6.2 christos DPRINTF("%s: shutdown error=%d len=%u\n",
146 1.2.6.2 christos device_xname(vsc->sc_dev), error, rlen);
147 1.2.6.2 christos }
148 1.2.6.2 christos return;
149 1.2.6.2 christos }
150 1.2.6.2 christos if (rlen < sizeof(*hdr)) {
151 1.2.6.2 christos DPRINTF("%s: shutdown short read len=%u\n",
152 1.2.6.2 christos device_xname(vsc->sc_dev), rlen);
153 1.2.6.2 christos return;
154 1.2.6.2 christos }
155 1.2.6.2 christos
156 1.2.6.2 christos hdr = (struct vmbus_icmsg_hdr *)vsc->sc_buf;
157 1.2.6.2 christos switch (hdr->ic_type) {
158 1.2.6.2 christos case VMBUS_ICMSG_TYPE_NEGOTIATE:
159 1.2.6.2 christos error = vmbusic_negotiate(vsc, hdr, &rlen, VMBUS_SHUTDOWN_FWVER,
160 1.2.6.2 christos VMBUS_SHUTDOWN_MSGVER);
161 1.2.6.2 christos if (error)
162 1.2.6.2 christos return;
163 1.2.6.2 christos break;
164 1.2.6.2 christos
165 1.2.6.2 christos case VMBUS_ICMSG_TYPE_SHUTDOWN:
166 1.2.6.2 christos if (rlen < VMBUS_ICMSG_SHUTDOWN_SIZE_MIN) {
167 1.2.6.2 christos DPRINTF("%s: invalid shutdown len=%u\n",
168 1.2.6.2 christos device_xname(vsc->sc_dev), rlen);
169 1.2.6.2 christos return;
170 1.2.6.2 christos }
171 1.2.6.2 christos
172 1.2.6.2 christos msg = (struct vmbus_icmsg_shutdown *)hdr;
173 1.2.6.2 christos if (msg->ic_haltflags == 0 || msg->ic_haltflags == 1) {
174 1.2.6.2 christos device_printf(vsc->sc_dev, "shutdown requested\n");
175 1.2.6.2 christos hdr->ic_status = VMBUS_ICMSG_STATUS_OK;
176 1.2.6.2 christos do_shutdown = true;
177 1.2.6.2 christos } else {
178 1.2.6.2 christos device_printf(vsc->sc_dev,
179 1.2.6.2 christos "unknown shutdown flags 0x%08x\n",
180 1.2.6.2 christos msg->ic_haltflags);
181 1.2.6.2 christos hdr->ic_status = VMBUS_ICMSG_STATUS_FAIL;
182 1.2.6.2 christos }
183 1.2.6.2 christos break;
184 1.2.6.2 christos
185 1.2.6.2 christos default:
186 1.2.6.2 christos device_printf(vsc->sc_dev,
187 1.2.6.2 christos "unhandled shutdown message type %u\n", hdr->ic_type);
188 1.2.6.2 christos return;
189 1.2.6.2 christos }
190 1.2.6.2 christos
191 1.2.6.2 christos (void) vmbusic_sendresp(vsc, ch, vsc->sc_buf, rlen, rid);
192 1.2.6.2 christos
193 1.2.6.2 christos if (do_shutdown)
194 1.2.6.2 christos sysmon_task_queue_sched(0, hvshutdown_do_shutdown, sc);
195 1.2.6.2 christos }
196 1.2.6.2 christos
197 1.2.6.2 christos MODULE(MODULE_CLASS_DRIVER, hvshutdown, "vmbus");
198 1.2.6.2 christos
199 1.2.6.2 christos #ifdef _MODULE
200 1.2.6.2 christos #include "ioconf.c"
201 1.2.6.2 christos #endif
202 1.2.6.2 christos
203 1.2.6.2 christos static int
204 1.2.6.2 christos hvshutdown_modcmd(modcmd_t cmd, void *aux)
205 1.2.6.2 christos {
206 1.2.6.2 christos int error = 0;
207 1.2.6.2 christos
208 1.2.6.2 christos switch (cmd) {
209 1.2.6.2 christos case MODULE_CMD_INIT:
210 1.2.6.2 christos #ifdef _MODULE
211 1.2.6.2 christos error = config_init_component(cfdriver_ioconf_hvshutdown,
212 1.2.6.2 christos cfattach_ioconf_hvshutdown, cfdata_ioconf_hvshutdown);
213 1.2.6.2 christos #endif
214 1.2.6.2 christos break;
215 1.2.6.2 christos
216 1.2.6.2 christos case MODULE_CMD_FINI:
217 1.2.6.2 christos #ifdef _MODULE
218 1.2.6.2 christos error = config_fini_component(cfdriver_ioconf_hvshutdown,
219 1.2.6.2 christos cfattach_ioconf_hvshutdown, cfdata_ioconf_hvshutdown);
220 1.2.6.2 christos #endif
221 1.2.6.2 christos break;
222 1.2.6.2 christos
223 1.2.6.2 christos case MODULE_CMD_AUTOUNLOAD:
224 1.2.6.2 christos error = EBUSY;
225 1.2.6.2 christos break;
226 1.2.6.2 christos
227 1.2.6.2 christos default:
228 1.2.6.2 christos error = ENOTTY;
229 1.2.6.2 christos break;
230 1.2.6.2 christos }
231 1.2.6.2 christos
232 1.2.6.2 christos return error;
233 1.2.6.2 christos }
234