2 * Copyright (c) 2014,2016 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
30 * A common driver for all hyper-V util services.
33 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/reboot.h>
39 #include <sys/timetc.h>
40 #include <sys/syscallsubr.h>
41 #include <sys/systm.h>
43 #include <dev/hyperv/include/hyperv.h>
44 #include <dev/hyperv/include/vmbus.h>
45 #include <dev/hyperv/utilities/hv_utilreg.h>
49 static const struct hyperv_guid service_guid = { .hv_guid =
50 {0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
51 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB} };
57 hv_shutdown_cb(struct vmbus_channel *channel, void *context)
60 uint8_t execute_shutdown = 0;
61 hv_vmbus_icmsg_hdr* icmsghdrp;
65 hv_vmbus_shutdown_msg_data* shutdown_msg;
68 softc = (hv_util_sc*)context;
69 buf = softc->receive_buffer;;
72 ret = vmbus_chan_recv(channel, buf, &recv_len, &request_id);
73 KASSERT(ret != ENOBUFS, ("hvshutdown recvbuf is not large enough"));
74 /* XXX check recv_len to make sure that it contains enough data */
76 if ((ret == 0) && recv_len > 0) {
78 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
79 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
81 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
82 hv_negotiate_version(icmsghdrp, NULL, buf);
86 (struct hv_vmbus_shutdown_msg_data *)
87 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
88 sizeof(struct hv_vmbus_icmsg_hdr)];
90 switch (shutdown_msg->flags) {
93 icmsghdrp->status = HV_S_OK;
96 printf("Shutdown request received -"
97 " graceful shutdown initiated\n");
100 icmsghdrp->status = HV_E_FAIL;
101 execute_shutdown = 0;
102 printf("Shutdown request received -"
103 " Invalid request\n");
108 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
109 HV_ICMSGHDRFLAG_RESPONSE;
111 vmbus_chan_send(channel, VMBUS_CHANPKT_TYPE_INBAND, 0,
112 buf, recv_len, request_id);
115 if (execute_shutdown)
116 shutdown_nice(RB_POWEROFF);
120 hv_shutdown_probe(device_t dev)
122 if (resource_disabled("hvshutdown", 0))
125 if (VMBUS_PROBE_GUID(device_get_parent(dev), dev, &service_guid) == 0) {
126 device_set_desc(dev, "Hyper-V Shutdown Service");
127 return BUS_PROBE_DEFAULT;
133 hv_shutdown_attach(device_t dev)
135 hv_util_sc *softc = (hv_util_sc*)device_get_softc(dev);
137 softc->callback = hv_shutdown_cb;
139 return hv_util_attach(dev);
142 static device_method_t shutdown_methods[] = {
143 /* Device interface */
144 DEVMETHOD(device_probe, hv_shutdown_probe),
145 DEVMETHOD(device_attach, hv_shutdown_attach),
146 DEVMETHOD(device_detach, hv_util_detach),
150 static driver_t shutdown_driver = { "hvshutdown", shutdown_methods, sizeof(hv_util_sc)};
152 static devclass_t shutdown_devclass;
154 DRIVER_MODULE(hv_shutdown, vmbus, shutdown_driver, shutdown_devclass, NULL, NULL);
155 MODULE_VERSION(hv_shutdown, 1);
156 MODULE_DEPEND(hv_shutdown, vmbus, 1, 1, 1);