]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_util.c
MFC 303066-303072,303127-303129,303131
[FreeBSD/stable/10.git] / sys / dev / hyperv / utilities / hv_util.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 /*
30  * A common driver for all hyper-V util services.
31  */
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/reboot.h>
39 #include <sys/timetc.h>
40 #include <sys/syscallsubr.h>
41
42 #include <dev/hyperv/include/hyperv.h>
43 #include <dev/hyperv/include/vmbus.h>
44 #include <dev/hyperv/utilities/hv_utilreg.h>
45 #include "hv_util.h"
46
47 void
48 hv_negotiate_version(
49         struct hv_vmbus_icmsg_hdr*              icmsghdrp,
50         struct hv_vmbus_icmsg_negotiate*        negop,
51         uint8_t*                                buf)
52 {
53         icmsghdrp->icmsgsize = 0x10;
54
55         negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
56                 sizeof(struct hv_vmbus_pipe_hdr) +
57                 sizeof(struct hv_vmbus_icmsg_hdr)];
58
59         if (negop->icframe_vercnt >= 2 &&
60             negop->icversion_data[1].major == 3) {
61                 negop->icversion_data[0].major = 3;
62                 negop->icversion_data[0].minor = 0;
63                 negop->icversion_data[1].major = 3;
64                 negop->icversion_data[1].minor = 0;
65         } else {
66                 negop->icversion_data[0].major = 1;
67                 negop->icversion_data[0].minor = 0;
68                 negop->icversion_data[1].major = 1;
69                 negop->icversion_data[1].minor = 0;
70         }
71
72         negop->icframe_vercnt = 1;
73         negop->icmsg_vercnt = 1;
74 }
75
76 int
77 hv_util_attach(device_t dev)
78 {
79         struct hv_util_sc*      softc;
80         struct vmbus_channel *chan;
81         int                     ret;
82
83         softc = device_get_softc(dev);
84         softc->receive_buffer =
85                 malloc(4 * PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
86         chan = vmbus_get_channel(dev);
87
88         /*
89          * These services are not performance critical and do not need
90          * batched reading. Furthermore, some services such as KVP can
91          * only handle one message from the host at a time.
92          * Turn off batched reading for all util drivers before we open the
93          * channel.
94          */
95         vmbus_chan_set_readbatch(chan, false);
96
97         ret = vmbus_chan_open(chan, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
98             softc->callback, softc);
99
100         if (ret)
101                 goto error0;
102
103         return (0);
104
105 error0:
106         free(softc->receive_buffer, M_DEVBUF);
107         return (ret);
108 }
109
110 int
111 hv_util_detach(device_t dev)
112 {
113         struct hv_util_sc *sc = device_get_softc(dev);
114
115         vmbus_chan_close(vmbus_get_channel(dev));
116         free(sc->receive_buffer, M_DEVBUF);
117
118         return (0);
119 }