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