]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_heartbeat.c
MFC 305279-305281
[FreeBSD/stable/10.git] / sys / dev / hyperv / utilities / hv_heartbeat.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/systm.h>
35
36 #include <dev/hyperv/include/hyperv.h>
37 #include <dev/hyperv/include/vmbus.h>
38 #include <dev/hyperv/utilities/hv_util.h>
39 #include <dev/hyperv/utilities/vmbus_icreg.h>
40
41 #include "vmbus_if.h"
42
43 static const struct vmbus_ic_desc vmbus_heartbeat_descs[] = {
44         {
45                 .ic_guid = { .hv_guid = {
46                     0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
47                     0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d} },
48                 .ic_desc = "Hyper-V Heartbeat"
49         },
50         VMBUS_IC_DESC_END
51 };
52
53 static void
54 vmbus_heartbeat_cb(struct vmbus_channel *chan, void *xsc)
55 {
56         struct hv_util_sc *sc = xsc;
57         struct vmbus_icmsg_hdr *hdr;
58         int dlen, error;
59         uint64_t xactid;
60         void *data;
61
62         /*
63          * Receive request.
64          */
65         data = sc->receive_buffer;
66         dlen = sc->ic_buflen;
67         error = vmbus_chan_recv(chan, data, &dlen, &xactid);
68         KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
69         if (error)
70                 return;
71
72         if (dlen < sizeof(*hdr)) {
73                 device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
74                 return;
75         }
76         hdr = data;
77
78         /*
79          * Update request, which will be echoed back as response.
80          */
81         switch (hdr->ic_type) {
82         case VMBUS_ICMSG_TYPE_NEGOTIATE:
83                 error = vmbus_ic_negomsg(sc, data, &dlen);
84                 if (error)
85                         return;
86                 break;
87
88         case VMBUS_ICMSG_TYPE_HEARTBEAT:
89                 /* Only ic_seq is a must */
90                 if (dlen < VMBUS_ICMSG_HEARTBEAT_SIZE_MIN) {
91                         device_printf(sc->ic_dev, "invalid heartbeat len %d\n",
92                             dlen);
93                         return;
94                 }
95                 ((struct vmbus_icmsg_heartbeat *)data)->ic_seq++;
96                 break;
97
98         default:
99                 device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
100                 break;
101         }
102
103         /*
104          * Send response by echoing the updated request back.
105          */
106         hdr->ic_flags = VMBUS_ICMSG_FLAG_XACT | VMBUS_ICMSG_FLAG_RESP;
107         error = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
108             data, dlen, xactid);
109         if (error)
110                 device_printf(sc->ic_dev, "resp send failed: %d\n", error);
111 }
112
113 static int
114 hv_heartbeat_probe(device_t dev)
115 {
116
117         return (vmbus_ic_probe(dev, vmbus_heartbeat_descs));
118 }
119
120 static int
121 hv_heartbeat_attach(device_t dev)
122 {
123
124         return (hv_util_attach(dev, vmbus_heartbeat_cb));
125 }
126
127 static device_method_t heartbeat_methods[] = {
128         /* Device interface */
129         DEVMETHOD(device_probe, hv_heartbeat_probe),
130         DEVMETHOD(device_attach, hv_heartbeat_attach),
131         DEVMETHOD(device_detach, hv_util_detach),
132         { 0, 0 }
133 };
134
135 static driver_t heartbeat_driver = { "hvheartbeat", heartbeat_methods, sizeof(hv_util_sc)};
136
137 static devclass_t heartbeat_devclass;
138
139 DRIVER_MODULE(hv_heartbeat, vmbus, heartbeat_driver, heartbeat_devclass, NULL, NULL);
140 MODULE_VERSION(hv_heartbeat, 1);
141 MODULE_DEPEND(hv_heartbeat, vmbus, 1, 1, 1);