]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_shutdown.c
MFC 302733,302737,302801-302806
[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
42 #include <dev/hyperv/include/hyperv.h>
43 #include "hv_util.h"
44 #include "vmbus_if.h"
45
46 static const struct hyperv_guid service_guid = { .hv_guid =
47         {0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
48         0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB} };
49
50 /**
51  * Shutdown
52  */
53 static void
54 hv_shutdown_cb(void *context)
55 {
56         uint8_t*                        buf;
57         hv_vmbus_channel*               channel;
58         uint8_t                         execute_shutdown = 0;
59         hv_vmbus_icmsg_hdr*             icmsghdrp;
60         uint32_t                        recv_len;
61         uint64_t                        request_id;
62         int                             ret;
63         hv_vmbus_shutdown_msg_data*     shutdown_msg;
64         hv_util_sc                      *softc;
65
66         softc = (hv_util_sc*)context;
67         buf = softc->receive_buffer;;
68         channel = softc->channel;
69         ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE,
70                                             &recv_len, &request_id);
71
72         if ((ret == 0) && recv_len > 0) {
73
74             icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
75                 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
76
77             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
78                 hv_negotiate_version(icmsghdrp, NULL, buf);
79
80             } else {
81                 shutdown_msg =
82                     (struct hv_vmbus_shutdown_msg_data *)
83                     &buf[sizeof(struct hv_vmbus_pipe_hdr) +
84                         sizeof(struct hv_vmbus_icmsg_hdr)];
85
86                 switch (shutdown_msg->flags) {
87                     case 0:
88                     case 1:
89                         icmsghdrp->status = HV_S_OK;
90                         execute_shutdown = 1;
91                         if(bootverbose)
92                             printf("Shutdown request received -"
93                                     " graceful shutdown initiated\n");
94                         break;
95                     default:
96                         icmsghdrp->status = HV_E_FAIL;
97                         execute_shutdown = 0;
98                         printf("Shutdown request received -"
99                             " Invalid request\n");
100                         break;
101                     }
102             }
103
104         icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
105                                  HV_ICMSGHDRFLAG_RESPONSE;
106
107             hv_vmbus_channel_send_packet(channel, buf,
108                                         recv_len, request_id,
109                                         HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
110         }
111
112         if (execute_shutdown)
113             shutdown_nice(RB_POWEROFF);
114 }
115
116 static int
117 hv_shutdown_probe(device_t dev)
118 {
119         if (resource_disabled("hvshutdown", 0))
120                 return ENXIO;
121
122         if (VMBUS_PROBE_GUID(device_get_parent(dev), dev, &service_guid) == 0) {
123                 device_set_desc(dev, "Hyper-V Shutdown Service");
124                 return BUS_PROBE_DEFAULT;
125         }
126         return ENXIO;
127 }
128
129 static int
130 hv_shutdown_attach(device_t dev)
131 {
132         hv_util_sc *softc = (hv_util_sc*)device_get_softc(dev);
133
134         softc->callback = hv_shutdown_cb;
135
136         return hv_util_attach(dev);
137 }
138
139 static device_method_t shutdown_methods[] = {
140         /* Device interface */
141         DEVMETHOD(device_probe, hv_shutdown_probe),
142         DEVMETHOD(device_attach, hv_shutdown_attach),
143         DEVMETHOD(device_detach, hv_util_detach),
144         { 0, 0 }
145 };
146
147 static driver_t shutdown_driver = { "hvshutdown", shutdown_methods, sizeof(hv_util_sc)};
148
149 static devclass_t shutdown_devclass;
150
151 DRIVER_MODULE(hv_shutdown, vmbus, shutdown_driver, shutdown_devclass, NULL, NULL);
152 MODULE_VERSION(hv_shutdown, 1);
153 MODULE_DEPEND(hv_shutdown, vmbus, 1, 1, 1);