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