xref: /freebsd-13-stable/sys/dev/hyperv/utilities/vmbus_shutdown.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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 #include <sys/param.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/reboot.h>
33 #include <sys/systm.h>
34 
35 #include <dev/hyperv/include/hyperv.h>
36 #include <dev/hyperv/include/vmbus.h>
37 #include <dev/hyperv/utilities/vmbus_icreg.h>
38 #include <dev/hyperv/utilities/vmbus_icvar.h>
39 
40 #define VMBUS_SHUTDOWN_FWVER_MAJOR	3
41 #define VMBUS_SHUTDOWN_FWVER		\
42 	VMBUS_IC_VERSION(VMBUS_SHUTDOWN_FWVER_MAJOR, 0)
43 
44 #define VMBUS_SHUTDOWN_MSGVER_MAJOR	3
45 #define VMBUS_SHUTDOWN_MSGVER		\
46 	VMBUS_IC_VERSION(VMBUS_SHUTDOWN_MSGVER_MAJOR, 0)
47 
48 static int			vmbus_shutdown_probe(device_t);
49 static int			vmbus_shutdown_attach(device_t);
50 
51 static const struct vmbus_ic_desc vmbus_shutdown_descs[] = {
52 	{
53 		.ic_guid = { .hv_guid = {
54 		    0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49,
55 		    0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb } },
56 		.ic_desc = "Hyper-V Shutdown"
57 	},
58 	VMBUS_IC_DESC_END
59 };
60 
61 static device_method_t vmbus_shutdown_methods[] = {
62 	/* Device interface */
63 	DEVMETHOD(device_probe,		vmbus_shutdown_probe),
64 	DEVMETHOD(device_attach,	vmbus_shutdown_attach),
65 	DEVMETHOD(device_detach,	vmbus_ic_detach),
66 	DEVMETHOD_END
67 };
68 
69 static driver_t vmbus_shutdown_driver = {
70 	"hvshutdown",
71 	vmbus_shutdown_methods,
72 	sizeof(struct vmbus_ic_softc)
73 };
74 
75 static devclass_t vmbus_shutdown_devclass;
76 
77 DRIVER_MODULE(hv_shutdown, vmbus, vmbus_shutdown_driver,
78     vmbus_shutdown_devclass, NULL, NULL);
79 MODULE_VERSION(hv_shutdown, 1);
80 MODULE_DEPEND(hv_shutdown, vmbus, 1, 1, 1);
81 
82 static void
vmbus_shutdown_cb(struct vmbus_channel * chan,void * xsc)83 vmbus_shutdown_cb(struct vmbus_channel *chan, void *xsc)
84 {
85 	struct vmbus_ic_softc *sc = xsc;
86 	struct vmbus_icmsg_hdr *hdr;
87 	struct vmbus_icmsg_shutdown *msg;
88 	int dlen, error, do_shutdown = 0;
89 	uint64_t xactid;
90 	void *data;
91 
92 	/*
93 	 * Receive request.
94 	 */
95 	data = sc->ic_buf;
96 	dlen = sc->ic_buflen;
97 	error = vmbus_chan_recv(chan, data, &dlen, &xactid);
98 	KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
99 	if (error)
100 		return;
101 
102 	if (dlen < sizeof(*hdr)) {
103 		device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
104 		return;
105 	}
106 	hdr = data;
107 
108 	/*
109 	 * Update request, which will be echoed back as response.
110 	 */
111 	switch (hdr->ic_type) {
112 	case VMBUS_ICMSG_TYPE_NEGOTIATE:
113 		error = vmbus_ic_negomsg(sc, data, &dlen,
114 		    VMBUS_SHUTDOWN_FWVER, VMBUS_SHUTDOWN_MSGVER);
115 		if (error)
116 			return;
117 		break;
118 
119 	case VMBUS_ICMSG_TYPE_SHUTDOWN:
120 		if (dlen < VMBUS_ICMSG_SHUTDOWN_SIZE_MIN) {
121 			device_printf(sc->ic_dev, "invalid shutdown len %d\n",
122 			    dlen);
123 			return;
124 		}
125 		msg = data;
126 
127 		/* XXX ic_flags definition? */
128 		if (msg->ic_haltflags == 0 || msg->ic_haltflags == 1) {
129 			device_printf(sc->ic_dev, "shutdown requested\n");
130 			hdr->ic_status = VMBUS_ICMSG_STATUS_OK;
131 			do_shutdown = 1;
132 		} else {
133 			device_printf(sc->ic_dev, "unknown shutdown flags "
134 			    "0x%08x\n", msg->ic_haltflags);
135 			hdr->ic_status = VMBUS_ICMSG_STATUS_FAIL;
136 		}
137 		break;
138 
139 	default:
140 		device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
141 		break;
142 	}
143 
144 	/*
145 	 * Send response by echoing the request back.
146 	 */
147 	vmbus_ic_sendresp(sc, chan, data, dlen, xactid);
148 
149 	if (do_shutdown)
150 		shutdown_nice(RB_POWEROFF);
151 }
152 
153 static int
vmbus_shutdown_probe(device_t dev)154 vmbus_shutdown_probe(device_t dev)
155 {
156 
157 	return (vmbus_ic_probe(dev, vmbus_shutdown_descs));
158 }
159 
160 static int
vmbus_shutdown_attach(device_t dev)161 vmbus_shutdown_attach(device_t dev)
162 {
163 
164 	return (vmbus_ic_attach(dev, vmbus_shutdown_cb));
165 }
166