Home | History | Annotate | Line # | Download | only in hyperv
hvheartbeat.c revision 1.2.6.2
      1  1.2.6.2  christos /*	$NetBSD: hvheartbeat.c,v 1.2.6.2 2019/06/10 22:07:09 christos Exp $	*/
      2  1.2.6.2  christos 
      3  1.2.6.2  christos /*-
      4  1.2.6.2  christos  * Copyright (c) 2014,2016 Microsoft Corp.
      5  1.2.6.2  christos  * All rights reserved.
      6  1.2.6.2  christos  *
      7  1.2.6.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.2.6.2  christos  * modification, are permitted provided that the following conditions
      9  1.2.6.2  christos  * are met:
     10  1.2.6.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.2.6.2  christos  *    notice unmodified, this list of conditions, and the following
     12  1.2.6.2  christos  *    disclaimer.
     13  1.2.6.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.2.6.2  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.2.6.2  christos  *    documentation and/or other materials provided with the distribution.
     16  1.2.6.2  christos  *
     17  1.2.6.2  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.2.6.2  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.2.6.2  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.2.6.2  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.2.6.2  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.2.6.2  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.2.6.2  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.2.6.2  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.2.6.2  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.2.6.2  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.2.6.2  christos  */
     28  1.2.6.2  christos 
     29  1.2.6.2  christos #include <sys/cdefs.h>
     30  1.2.6.2  christos #ifdef __KERNEL_RCSID
     31  1.2.6.2  christos __KERNEL_RCSID(0, "$NetBSD: hvheartbeat.c,v 1.2.6.2 2019/06/10 22:07:09 christos Exp $");
     32  1.2.6.2  christos #endif
     33  1.2.6.2  christos #ifdef __FBSDID
     34  1.2.6.2  christos __FBSDID("$FreeBSD: head/sys/dev/hyperv/utilities/vmbus_heartbeat.c 310324 2016-12-20 09:46:14Z sephe $");
     35  1.2.6.2  christos #endif
     36  1.2.6.2  christos 
     37  1.2.6.2  christos #include <sys/param.h>
     38  1.2.6.2  christos #include <sys/systm.h>
     39  1.2.6.2  christos #include <sys/device.h>
     40  1.2.6.2  christos #include <sys/module.h>
     41  1.2.6.2  christos #include <sys/pmf.h>
     42  1.2.6.2  christos 
     43  1.2.6.2  christos #include <dev/hyperv/vmbusvar.h>
     44  1.2.6.2  christos #include <dev/hyperv/vmbusicreg.h>
     45  1.2.6.2  christos #include <dev/hyperv/vmbusicvar.h>
     46  1.2.6.2  christos 
     47  1.2.6.2  christos #define VMBUS_HEARTBEAT_FWVER_MAJOR	3
     48  1.2.6.2  christos #define VMBUS_HEARTBEAT_FWVER		\
     49  1.2.6.2  christos 	    VMBUS_IC_VERSION(VMBUS_HEARTBEAT_FWVER_MAJOR, 0)
     50  1.2.6.2  christos 
     51  1.2.6.2  christos #define VMBUS_HEARTBEAT_MSGVER_MAJOR	3
     52  1.2.6.2  christos #define VMBUS_HEARTBEAT_MSGVER		\
     53  1.2.6.2  christos 	    VMBUS_IC_VERSION(VMBUS_HEARTBEAT_MSGVER_MAJOR, 0)
     54  1.2.6.2  christos 
     55  1.2.6.2  christos static int	hvheartbeat_match(device_t, cfdata_t, void *);
     56  1.2.6.2  christos static void	hvheartbeat_attach(device_t, device_t, void *);
     57  1.2.6.2  christos static int	hvheartbeat_detach(device_t, int);
     58  1.2.6.2  christos 
     59  1.2.6.2  christos static void	hvheartbeat_channel_cb(void *);
     60  1.2.6.2  christos 
     61  1.2.6.2  christos struct hvheartbeat_softc {
     62  1.2.6.2  christos 	struct vmbusic_softc	sc_vmbusic;
     63  1.2.6.2  christos };
     64  1.2.6.2  christos 
     65  1.2.6.2  christos CFATTACH_DECL_NEW(hvheartbeat, sizeof(struct hvheartbeat_softc),
     66  1.2.6.2  christos     hvheartbeat_match, hvheartbeat_attach, hvheartbeat_detach, NULL);
     67  1.2.6.2  christos 
     68  1.2.6.2  christos static int
     69  1.2.6.2  christos hvheartbeat_match(device_t parent, cfdata_t cf, void *aux)
     70  1.2.6.2  christos {
     71  1.2.6.2  christos 	struct vmbus_attach_args *aa = aux;
     72  1.2.6.2  christos 
     73  1.2.6.2  christos 	return vmbusic_probe(aa, &hyperv_guid_heartbeat);
     74  1.2.6.2  christos }
     75  1.2.6.2  christos 
     76  1.2.6.2  christos static void
     77  1.2.6.2  christos hvheartbeat_attach(device_t parent, device_t self, void *aux)
     78  1.2.6.2  christos {
     79  1.2.6.2  christos 	struct vmbus_attach_args *aa = aux;
     80  1.2.6.2  christos 	int error;
     81  1.2.6.2  christos 
     82  1.2.6.2  christos 	aprint_naive("\n");
     83  1.2.6.2  christos 	aprint_normal(": Hyper-V Heartbeat Service\n");
     84  1.2.6.2  christos 
     85  1.2.6.2  christos 	error = vmbusic_attach(self, aa, hvheartbeat_channel_cb);
     86  1.2.6.2  christos 	if (error)
     87  1.2.6.2  christos 		return;
     88  1.2.6.2  christos 
     89  1.2.6.2  christos 	(void) pmf_device_register(self, NULL, NULL);
     90  1.2.6.2  christos }
     91  1.2.6.2  christos 
     92  1.2.6.2  christos static int
     93  1.2.6.2  christos hvheartbeat_detach(device_t self, int flags)
     94  1.2.6.2  christos {
     95  1.2.6.2  christos 	int error;
     96  1.2.6.2  christos 
     97  1.2.6.2  christos 	error = vmbusic_detach(self, flags);
     98  1.2.6.2  christos 	if (error)
     99  1.2.6.2  christos 		return error;
    100  1.2.6.2  christos 
    101  1.2.6.2  christos 	pmf_device_deregister(self);
    102  1.2.6.2  christos 
    103  1.2.6.2  christos 	return 0;
    104  1.2.6.2  christos }
    105  1.2.6.2  christos 
    106  1.2.6.2  christos static void
    107  1.2.6.2  christos hvheartbeat_channel_cb(void *arg)
    108  1.2.6.2  christos {
    109  1.2.6.2  christos 	struct hvheartbeat_softc *sc = arg;
    110  1.2.6.2  christos 	struct vmbusic_softc *vsc = &sc->sc_vmbusic;
    111  1.2.6.2  christos 	struct vmbus_channel *ch = vsc->sc_chan;
    112  1.2.6.2  christos 	struct vmbus_icmsg_hdr *hdr;
    113  1.2.6.2  christos 	struct vmbus_icmsg_heartbeat *msg;
    114  1.2.6.2  christos 	uint64_t rid;
    115  1.2.6.2  christos 	uint32_t rlen;
    116  1.2.6.2  christos 	int error;
    117  1.2.6.2  christos 
    118  1.2.6.2  christos 	error = vmbus_channel_recv(ch, vsc->sc_buf, vsc->sc_buflen,
    119  1.2.6.2  christos 	    &rlen, &rid, 0);
    120  1.2.6.2  christos 	if (error || rlen == 0) {
    121  1.2.6.2  christos 		if (error != EAGAIN) {
    122  1.2.6.2  christos 			DPRINTF("%s: heartbeat error=%d len=%u\n",
    123  1.2.6.2  christos 			    device_xname(vsc->sc_dev), error, rlen);
    124  1.2.6.2  christos 		}
    125  1.2.6.2  christos 		return;
    126  1.2.6.2  christos 	}
    127  1.2.6.2  christos 	if (rlen < sizeof(*hdr)) {
    128  1.2.6.2  christos 		DPRINTF("%s: heartbeat short read len=%u\n",
    129  1.2.6.2  christos 		    device_xname(vsc->sc_dev), rlen);
    130  1.2.6.2  christos 		return;
    131  1.2.6.2  christos 	}
    132  1.2.6.2  christos 
    133  1.2.6.2  christos 	hdr = (struct vmbus_icmsg_hdr *)vsc->sc_buf;
    134  1.2.6.2  christos 	switch (hdr->ic_type) {
    135  1.2.6.2  christos 	case VMBUS_ICMSG_TYPE_NEGOTIATE:
    136  1.2.6.2  christos 		error = vmbusic_negotiate(vsc, hdr, &rlen,
    137  1.2.6.2  christos 		    VMBUS_HEARTBEAT_FWVER, VMBUS_HEARTBEAT_MSGVER);
    138  1.2.6.2  christos 		if (error)
    139  1.2.6.2  christos 			return;
    140  1.2.6.2  christos 		break;
    141  1.2.6.2  christos 
    142  1.2.6.2  christos 	case VMBUS_ICMSG_TYPE_HEARTBEAT:
    143  1.2.6.2  christos 		if (rlen < VMBUS_ICMSG_HEARTBEAT_SIZE_MIN) {
    144  1.2.6.2  christos 			DPRINTF("%s: invalid heartbeat len=%u\n",
    145  1.2.6.2  christos 			    device_xname(vsc->sc_dev), rlen);
    146  1.2.6.2  christos 			return;
    147  1.2.6.2  christos 		}
    148  1.2.6.2  christos 
    149  1.2.6.2  christos 		msg = (struct vmbus_icmsg_heartbeat *)hdr;
    150  1.2.6.2  christos 		msg->ic_seq++;
    151  1.2.6.2  christos 		break;
    152  1.2.6.2  christos 
    153  1.2.6.2  christos 	default:
    154  1.2.6.2  christos 		device_printf(vsc->sc_dev,
    155  1.2.6.2  christos 		    "unhandled heartbeat message type %u\n", hdr->ic_type);
    156  1.2.6.2  christos 		return;
    157  1.2.6.2  christos 	}
    158  1.2.6.2  christos 
    159  1.2.6.2  christos 	(void) vmbusic_sendresp(vsc, ch, vsc->sc_buf, rlen, rid);
    160  1.2.6.2  christos }
    161  1.2.6.2  christos 
    162  1.2.6.2  christos MODULE(MODULE_CLASS_DRIVER, hvheartbeat, "vmbus");
    163  1.2.6.2  christos 
    164  1.2.6.2  christos #ifdef _MODULE
    165  1.2.6.2  christos #include "ioconf.c"
    166  1.2.6.2  christos #endif
    167  1.2.6.2  christos 
    168  1.2.6.2  christos static int
    169  1.2.6.2  christos hvheartbeat_modcmd(modcmd_t cmd, void *aux)
    170  1.2.6.2  christos {
    171  1.2.6.2  christos 	int error = 0;
    172  1.2.6.2  christos 
    173  1.2.6.2  christos 	switch (cmd) {
    174  1.2.6.2  christos 	case MODULE_CMD_INIT:
    175  1.2.6.2  christos #ifdef _MODULE
    176  1.2.6.2  christos 		error = config_init_component(cfdriver_ioconf_hvheartbeat,
    177  1.2.6.2  christos 		    cfattach_ioconf_hvheartbeat, cfdata_ioconf_hvheartbeat);
    178  1.2.6.2  christos #endif
    179  1.2.6.2  christos 		break;
    180  1.2.6.2  christos 
    181  1.2.6.2  christos 	case MODULE_CMD_FINI:
    182  1.2.6.2  christos #ifdef _MODULE
    183  1.2.6.2  christos 		error = config_fini_component(cfdriver_ioconf_hvheartbeat,
    184  1.2.6.2  christos 		    cfattach_ioconf_hvheartbeat, cfdata_ioconf_hvheartbeat);
    185  1.2.6.2  christos #endif
    186  1.2.6.2  christos 		break;
    187  1.2.6.2  christos 
    188  1.2.6.2  christos 	case MODULE_CMD_AUTOUNLOAD:
    189  1.2.6.2  christos 		error = EBUSY;
    190  1.2.6.2  christos 		break;
    191  1.2.6.2  christos 
    192  1.2.6.2  christos 	default:
    193  1.2.6.2  christos 		error = ENOTTY;
    194  1.2.6.2  christos 		break;
    195  1.2.6.2  christos 	}
    196  1.2.6.2  christos 
    197  1.2.6.2  christos 	return error;
    198  1.2.6.2  christos }
    199