2 * Copyright (c) 2014 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
29 #include <sys/param.h>
30 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/timetc.h>
35 #include <sys/syscallsubr.h>
37 #include <dev/hyperv/include/hyperv.h>
40 /* Heartbeat Service */
41 static hv_guid service_guid = { .data =
42 {0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
43 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d} };
46 * Process heartbeat message
49 hv_heartbeat_cb(void *context)
52 hv_vmbus_channel* channel;
57 struct hv_vmbus_heartbeat_msg_data* heartbeat_msg;
58 struct hv_vmbus_icmsg_hdr* icmsghdrp;
61 softc = (hv_util_sc*)context;
62 buf = softc->receive_buffer;;
63 channel = softc->hv_dev->channel;
65 ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE, &recvlen,
68 if ((ret == 0) && recvlen > 0) {
70 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
71 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
73 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
74 hv_negotiate_version(icmsghdrp, NULL, buf);
78 (struct hv_vmbus_heartbeat_msg_data *)
79 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
80 sizeof(struct hv_vmbus_icmsg_hdr)];
82 heartbeat_msg->seq_num += 1;
85 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
86 HV_ICMSGHDRFLAG_RESPONSE;
88 hv_vmbus_channel_send_packet(channel, buf, recvlen, requestid,
89 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
94 hv_heartbeat_probe(device_t dev)
96 const char *p = vmbus_get_type(dev);
97 if (!memcmp(p, &service_guid, sizeof(hv_guid))) {
98 device_set_desc(dev, "Hyper-V Heartbeat Service");
99 return BUS_PROBE_DEFAULT;
106 hv_heartbeat_attach(device_t dev)
108 hv_util_sc *softc = (hv_util_sc*)device_get_softc(dev);
110 softc->callback = hv_heartbeat_cb;
112 return hv_util_attach(dev);
115 static device_method_t heartbeat_methods[] = {
116 /* Device interface */
117 DEVMETHOD(device_probe, hv_heartbeat_probe),
118 DEVMETHOD(device_attach, hv_heartbeat_attach),
119 DEVMETHOD(device_detach, hv_util_detach),
123 static driver_t heartbeat_driver = { "hvheartbeat", heartbeat_methods, sizeof(hv_util_sc)};
125 static devclass_t heartbeat_devclass;
127 DRIVER_MODULE(hv_heartbeat, vmbus, heartbeat_driver, heartbeat_devclass, NULL, NULL);
128 MODULE_VERSION(hv_heartbeat, 1);
129 MODULE_DEPEND(hv_heartbeat, vmbus, 1, 1, 1);