]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_heartbeat.c
MFC 304270,304273
[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  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/bus.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/timetc.h>
35 #include <sys/syscallsubr.h>
36 #include <sys/systm.h>
37
38 #include <dev/hyperv/include/hyperv.h>
39 #include <dev/hyperv/include/vmbus.h>
40 #include <dev/hyperv/utilities/hv_utilreg.h>
41 #include "hv_util.h"
42 #include "vmbus_if.h"
43
44 static const struct vmbus_ic_desc vmbus_heartbeat_descs[] = {
45         {
46                 .ic_guid = { .hv_guid = {
47                     0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
48                     0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d} },
49                 .ic_desc = "Hyper-V Heartbeat"
50         },
51         VMBUS_IC_DESC_END
52 };
53
54 /**
55  * Process heartbeat message
56  */
57 static void
58 hv_heartbeat_cb(struct vmbus_channel *channel, void *context)
59 {
60         uint8_t*                buf;
61         int                     recvlen;
62         uint64_t                requestid;
63         int                     ret;
64
65         struct hv_vmbus_heartbeat_msg_data*     heartbeat_msg;
66         struct hv_vmbus_icmsg_hdr*              icmsghdrp;
67         hv_util_sc                      *softc;
68
69         softc = (hv_util_sc*)context;
70         buf = softc->receive_buffer;;
71
72         recvlen = softc->ic_buflen;
73         ret = vmbus_chan_recv(channel, buf, &recvlen, &requestid);
74         KASSERT(ret != ENOBUFS, ("hvheartbeat recvbuf is not large enough"));
75         /* XXX check recvlen to make sure that it contains enough data */
76
77         if ((ret == 0) && recvlen > 0) {
78
79             icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
80                 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
81
82             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
83                 hv_negotiate_version(icmsghdrp, buf);
84             } else {
85                 heartbeat_msg =
86                     (struct hv_vmbus_heartbeat_msg_data *)
87                         &buf[sizeof(struct hv_vmbus_pipe_hdr) +
88                              sizeof(struct hv_vmbus_icmsg_hdr)];
89
90                 heartbeat_msg->seq_num += 1;
91             }
92
93             icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
94                                  HV_ICMSGHDRFLAG_RESPONSE;
95
96             vmbus_chan_send(channel, VMBUS_CHANPKT_TYPE_INBAND, 0,
97                 buf, recvlen, requestid);
98         }
99 }
100
101 static int
102 hv_heartbeat_probe(device_t dev)
103 {
104
105         return (vmbus_ic_probe(dev, vmbus_heartbeat_descs));
106 }
107
108 static int
109 hv_heartbeat_attach(device_t dev)
110 {
111         return hv_util_attach(dev, hv_heartbeat_cb);
112 }
113
114 static device_method_t heartbeat_methods[] = {
115         /* Device interface */
116         DEVMETHOD(device_probe, hv_heartbeat_probe),
117         DEVMETHOD(device_attach, hv_heartbeat_attach),
118         DEVMETHOD(device_detach, hv_util_detach),
119         { 0, 0 }
120 };
121
122 static driver_t heartbeat_driver = { "hvheartbeat", heartbeat_methods, sizeof(hv_util_sc)};
123
124 static devclass_t heartbeat_devclass;
125
126 DRIVER_MODULE(hv_heartbeat, vmbus, heartbeat_driver, heartbeat_devclass, NULL, NULL);
127 MODULE_VERSION(hv_heartbeat, 1);
128 MODULE_DEPEND(hv_heartbeat, vmbus, 1, 1, 1);