]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_shutdown.c
MFC 304786,304788
[FreeBSD/stable/10.git] / sys / dev / hyperv / utilities / hv_shutdown.c
1 /*-
2  * Copyright (c) 2014,2016 Microsoft Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * A common driver for all hyper-V util services.
31  */
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.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>
42
43 #include <dev/hyperv/include/hyperv.h>
44 #include <dev/hyperv/include/vmbus.h>
45 #include <dev/hyperv/utilities/hv_utilreg.h>
46 #include "hv_util.h"
47 #include "vmbus_if.h"
48
49 static const struct vmbus_ic_desc vmbus_shutdown_descs[] = {
50         {
51                 .ic_guid = { .hv_guid = {
52                     0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49,
53                     0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb } },
54                 .ic_desc = "Hyper-V Shutdown"
55         },
56         VMBUS_IC_DESC_END
57 };
58
59 /**
60  * Shutdown
61  */
62 static void
63 hv_shutdown_cb(struct vmbus_channel *channel, void *context)
64 {
65         uint8_t*                        buf;
66         uint8_t                         execute_shutdown = 0;
67         hv_vmbus_icmsg_hdr*             icmsghdrp;
68         uint32_t                        recv_len;
69         uint64_t                        request_id;
70         int                             ret;
71         hv_vmbus_shutdown_msg_data*     shutdown_msg;
72         hv_util_sc                      *softc;
73
74         softc = (hv_util_sc*)context;
75         buf = softc->receive_buffer;;
76
77         recv_len = softc->ic_buflen;
78         ret = vmbus_chan_recv(channel, buf, &recv_len, &request_id);
79         KASSERT(ret != ENOBUFS, ("hvshutdown recvbuf is not large enough"));
80         /* XXX check recv_len to make sure that it contains enough data */
81
82         if ((ret == 0) && recv_len > 0) {
83
84             icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
85                 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
86
87             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
88                 int error;
89
90                 error = vmbus_ic_negomsg(softc, buf, &recv_len);
91                 if (error)
92                         return;
93             } else {
94                 shutdown_msg =
95                     (struct hv_vmbus_shutdown_msg_data *)
96                     &buf[sizeof(struct hv_vmbus_pipe_hdr) +
97                         sizeof(struct hv_vmbus_icmsg_hdr)];
98
99                 switch (shutdown_msg->flags) {
100                     case 0:
101                     case 1:
102                         icmsghdrp->status = HV_S_OK;
103                         execute_shutdown = 1;
104                         if(bootverbose)
105                             printf("Shutdown request received -"
106                                     " graceful shutdown initiated\n");
107                         break;
108                     default:
109                         icmsghdrp->status = HV_E_FAIL;
110                         execute_shutdown = 0;
111                         printf("Shutdown request received -"
112                             " Invalid request\n");
113                         break;
114                     }
115             }
116
117         icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
118                                  HV_ICMSGHDRFLAG_RESPONSE;
119
120             vmbus_chan_send(channel, VMBUS_CHANPKT_TYPE_INBAND, 0,
121                 buf, recv_len, request_id);
122         }
123
124         if (execute_shutdown)
125             shutdown_nice(RB_POWEROFF);
126 }
127
128 static int
129 hv_shutdown_probe(device_t dev)
130 {
131
132         return (vmbus_ic_probe(dev, vmbus_shutdown_descs));
133 }
134
135 static int
136 hv_shutdown_attach(device_t dev)
137 {
138         return hv_util_attach(dev, hv_shutdown_cb);
139 }
140
141 static device_method_t shutdown_methods[] = {
142         /* Device interface */
143         DEVMETHOD(device_probe, hv_shutdown_probe),
144         DEVMETHOD(device_attach, hv_shutdown_attach),
145         DEVMETHOD(device_detach, hv_util_detach),
146         { 0, 0 }
147 };
148
149 static driver_t shutdown_driver = { "hvshutdown", shutdown_methods, sizeof(hv_util_sc)};
150
151 static devclass_t shutdown_devclass;
152
153 DRIVER_MODULE(hv_shutdown, vmbus, shutdown_driver, shutdown_devclass, NULL, NULL);
154 MODULE_VERSION(hv_shutdown, 1);
155 MODULE_DEPEND(hv_shutdown, vmbus, 1, 1, 1);