]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_shutdown.c
MFC 305279-305281
[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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/reboot.h>
35 #include <sys/systm.h>
36
37 #include <dev/hyperv/include/hyperv.h>
38 #include <dev/hyperv/include/vmbus.h>
39 #include <dev/hyperv/utilities/hv_util.h>
40 #include <dev/hyperv/utilities/vmbus_icreg.h>
41
42 #include "vmbus_if.h"
43
44 static const struct vmbus_ic_desc vmbus_shutdown_descs[] = {
45         {
46                 .ic_guid = { .hv_guid = {
47                     0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49,
48                     0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb } },
49                 .ic_desc = "Hyper-V Shutdown"
50         },
51         VMBUS_IC_DESC_END
52 };
53
54 static void
55 vmbus_shutdown_cb(struct vmbus_channel *chan, void *xsc)
56 {
57         struct hv_util_sc *sc = xsc;
58         struct vmbus_icmsg_hdr *hdr;
59         struct vmbus_icmsg_shutdown *msg;
60         int dlen, error, do_shutdown = 0;
61         uint64_t xactid;
62         void *data;
63
64         /*
65          * Receive request.
66          */
67         data = sc->receive_buffer;
68         dlen = sc->ic_buflen;
69         error = vmbus_chan_recv(chan, data, &dlen, &xactid);
70         KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
71         if (error)
72                 return;
73
74         if (dlen < sizeof(*hdr)) {
75                 device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
76                 return;
77         }
78         hdr = data;
79
80         /*
81          * Update request, which will be echoed back as response.
82          */
83         switch (hdr->ic_type) {
84         case VMBUS_ICMSG_TYPE_NEGOTIATE:
85                 error = vmbus_ic_negomsg(sc, data, &dlen);
86                 if (error)
87                         return;
88                 break;
89
90         case VMBUS_ICMSG_TYPE_SHUTDOWN:
91                 if (dlen < VMBUS_ICMSG_SHUTDOWN_SIZE_MIN) {
92                         device_printf(sc->ic_dev, "invalid shutdown len %d\n",
93                             dlen);
94                         return;
95                 }
96                 msg = data;
97
98                 /* XXX ic_flags definition? */
99                 if (msg->ic_haltflags == 0 || msg->ic_haltflags == 1) {
100                         device_printf(sc->ic_dev, "shutdown requested\n");
101                         hdr->ic_status = VMBUS_ICMSG_STATUS_OK;
102                         do_shutdown = 1;
103                 } else {
104                         device_printf(sc->ic_dev, "unknown shutdown flags "
105                             "0x%08x\n", msg->ic_haltflags);
106                         hdr->ic_status = VMBUS_ICMSG_STATUS_FAIL;
107                 }
108                 break;
109
110         default:
111                 device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
112                 break;
113         }
114
115         /*
116          * Send response by echoing the updated request back.
117          */
118         hdr->ic_flags = VMBUS_ICMSG_FLAG_XACT | VMBUS_ICMSG_FLAG_RESP;
119         error = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
120             data, dlen, xactid);
121         if (error)
122                 device_printf(sc->ic_dev, "resp send failed: %d\n", error);
123
124         if (do_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
139         return (hv_util_attach(dev, vmbus_shutdown_cb));
140 }
141
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),
147         { 0, 0 }
148 };
149
150 static driver_t shutdown_driver = { "hvshutdown", shutdown_methods, sizeof(hv_util_sc)};
151
152 static devclass_t shutdown_devclass;
153
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);