Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
| 17 | * Authors: |
| 18 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 19 | * Hank Janssen <hjanssen@microsoft.com> |
| 20 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/sysctl.h> |
Greg Kroah-Hartman | 9e62907 | 2010-05-04 08:26:23 -0700 | [diff] [blame] | 28 | #include <linux/reboot.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 29 | #include <linux/hyperv.h> |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 30 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 31 | |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 32 | #define SD_MAJOR 3 |
| 33 | #define SD_MINOR 0 |
| 34 | #define SD_VERSION (SD_MAJOR << 16 | SD_MINOR) |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 35 | |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 36 | #define SD_WS2008_MAJOR 1 |
| 37 | #define SD_WS2008_VERSION (SD_WS2008_MAJOR << 16 | SD_MINOR) |
| 38 | |
| 39 | #define TS_MAJOR 3 |
| 40 | #define TS_MINOR 0 |
| 41 | #define TS_VERSION (TS_MAJOR << 16 | TS_MINOR) |
| 42 | |
| 43 | #define TS_WS2008_MAJOR 1 |
| 44 | #define TS_WS2008_VERSION (TS_WS2008_MAJOR << 16 | TS_MINOR) |
| 45 | |
| 46 | #define HB_MAJOR 3 |
| 47 | #define HB_MINOR 0 |
| 48 | #define HB_VERSION (HB_MAJOR << 16 | HB_MINOR) |
| 49 | |
| 50 | #define HB_WS2008_MAJOR 1 |
| 51 | #define HB_WS2008_VERSION (HB_WS2008_MAJOR << 16 | HB_MINOR) |
| 52 | |
| 53 | static int sd_srv_version; |
| 54 | static int ts_srv_version; |
| 55 | static int hb_srv_version; |
| 56 | static int util_fw_version; |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 57 | |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 58 | static void shutdown_onchannelcallback(void *context); |
| 59 | static struct hv_util_service util_shutdown = { |
| 60 | .util_cb = shutdown_onchannelcallback, |
| 61 | }; |
| 62 | |
| 63 | static void timesync_onchannelcallback(void *context); |
| 64 | static struct hv_util_service util_timesynch = { |
| 65 | .util_cb = timesync_onchannelcallback, |
| 66 | }; |
| 67 | |
| 68 | static void heartbeat_onchannelcallback(void *context); |
| 69 | static struct hv_util_service util_heartbeat = { |
| 70 | .util_cb = heartbeat_onchannelcallback, |
| 71 | }; |
| 72 | |
| 73 | static struct hv_util_service util_kvp = { |
| 74 | .util_cb = hv_kvp_onchannelcallback, |
| 75 | .util_init = hv_kvp_init, |
| 76 | .util_deinit = hv_kvp_deinit, |
| 77 | }; |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 78 | |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 79 | static struct hv_util_service util_vss = { |
| 80 | .util_cb = hv_vss_onchannelcallback, |
| 81 | .util_init = hv_vss_init, |
| 82 | .util_deinit = hv_vss_deinit, |
| 83 | }; |
| 84 | |
K. Y. Srinivasan | 3dd6cb4 | 2013-01-23 17:42:45 -0800 | [diff] [blame] | 85 | static void perform_shutdown(struct work_struct *dummy) |
| 86 | { |
| 87 | orderly_poweroff(true); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Perform the shutdown operation in a thread context. |
| 92 | */ |
| 93 | static DECLARE_WORK(shutdown_work, perform_shutdown); |
| 94 | |
Greg Kroah-Hartman | 6610944 | 2010-05-04 14:31:18 -0700 | [diff] [blame] | 95 | static void shutdown_onchannelcallback(void *context) |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 96 | { |
| 97 | struct vmbus_channel *channel = context; |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 98 | u32 recvlen; |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 99 | u64 requestid; |
Peter Senna Tschudin | 3173463 | 2013-09-22 00:27:34 +0200 | [diff] [blame] | 100 | bool execute_shutdown = false; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 101 | u8 *shut_txf_buf = util_shutdown.recv_buffer; |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 102 | |
| 103 | struct shutdown_msg_data *shutdown_msg; |
| 104 | |
| 105 | struct icmsg_hdr *icmsghdrp; |
| 106 | struct icmsg_negotiate *negop = NULL; |
| 107 | |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 108 | vmbus_recvpacket(channel, shut_txf_buf, |
| 109 | PAGE_SIZE, &recvlen, &requestid); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 110 | |
| 111 | if (recvlen > 0) { |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 112 | icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[ |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 113 | sizeof(struct vmbuspipe_hdr)]; |
| 114 | |
| 115 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 116 | vmbus_prep_negotiate_resp(icmsghdrp, negop, |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 117 | shut_txf_buf, util_fw_version, |
| 118 | sd_srv_version); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 119 | } else { |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 120 | shutdown_msg = |
| 121 | (struct shutdown_msg_data *)&shut_txf_buf[ |
| 122 | sizeof(struct vmbuspipe_hdr) + |
| 123 | sizeof(struct icmsg_hdr)]; |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 124 | |
| 125 | switch (shutdown_msg->flags) { |
| 126 | case 0: |
| 127 | case 1: |
| 128 | icmsghdrp->status = HV_S_OK; |
| 129 | execute_shutdown = true; |
| 130 | |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 131 | pr_info("Shutdown request received -" |
Greg Kroah-Hartman | 32235b0 | 2011-04-13 12:14:05 -0700 | [diff] [blame] | 132 | " graceful shutdown initiated\n"); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 133 | break; |
| 134 | default: |
| 135 | icmsghdrp->status = HV_E_FAIL; |
| 136 | execute_shutdown = false; |
| 137 | |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 138 | pr_info("Shutdown request received -" |
| 139 | " Invalid request\n"); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 140 | break; |
Joe Perches | 95cd17c | 2011-04-10 14:31:35 -0700 | [diff] [blame] | 141 | } |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 145 | | ICMSGHDRFLAG_RESPONSE; |
| 146 | |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 147 | vmbus_sendpacket(channel, shut_txf_buf, |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 148 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 149 | VM_PKT_DATA_INBAND, 0); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 152 | if (execute_shutdown == true) |
K. Y. Srinivasan | 3dd6cb4 | 2013-01-23 17:42:45 -0800 | [diff] [blame] | 153 | schedule_work(&shutdown_work); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 156 | /* |
Haiyang Zhang | e9ec360 | 2010-05-11 15:11:24 +0000 | [diff] [blame] | 157 | * Set guest time to host UTC time. |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 158 | */ |
Haiyang Zhang | e9ec360 | 2010-05-11 15:11:24 +0000 | [diff] [blame] | 159 | static inline void do_adj_guesttime(u64 hosttime) |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 160 | { |
| 161 | s64 host_tns; |
| 162 | struct timespec host_ts; |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 163 | |
| 164 | host_tns = (hosttime - WLTIMEDELTA) * 100; |
| 165 | host_ts = ns_to_timespec(host_tns); |
| 166 | |
Haiyang Zhang | e9ec360 | 2010-05-11 15:11:24 +0000 | [diff] [blame] | 167 | do_settimeofday(&host_ts); |
| 168 | } |
| 169 | |
| 170 | /* |
K. Y. Srinivasan | 95ff7cd | 2011-08-27 11:31:31 -0700 | [diff] [blame] | 171 | * Set the host time in a process context. |
| 172 | */ |
| 173 | |
| 174 | struct adj_time_work { |
| 175 | struct work_struct work; |
| 176 | u64 host_time; |
| 177 | }; |
| 178 | |
| 179 | static void hv_set_host_time(struct work_struct *work) |
| 180 | { |
| 181 | struct adj_time_work *wrk; |
| 182 | |
| 183 | wrk = container_of(work, struct adj_time_work, work); |
| 184 | do_adj_guesttime(wrk->host_time); |
| 185 | kfree(wrk); |
| 186 | } |
| 187 | |
| 188 | /* |
Haiyang Zhang | e9ec360 | 2010-05-11 15:11:24 +0000 | [diff] [blame] | 189 | * Synchronize time with host after reboot, restore, etc. |
| 190 | * |
| 191 | * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM. |
| 192 | * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time |
| 193 | * message after the timesync channel is opened. Since the hv_utils module is |
| 194 | * loaded after hv_vmbus, the first message is usually missed. The other |
| 195 | * thing is, systime is automatically set to emulated hardware clock which may |
| 196 | * not be UTC time or in the same time zone. So, to override these effects, we |
| 197 | * use the first 50 time samples for initial system time setting. |
| 198 | */ |
| 199 | static inline void adj_guesttime(u64 hosttime, u8 flags) |
| 200 | { |
K. Y. Srinivasan | 95ff7cd | 2011-08-27 11:31:31 -0700 | [diff] [blame] | 201 | struct adj_time_work *wrk; |
Haiyang Zhang | e9ec360 | 2010-05-11 15:11:24 +0000 | [diff] [blame] | 202 | static s32 scnt = 50; |
| 203 | |
K. Y. Srinivasan | 95ff7cd | 2011-08-27 11:31:31 -0700 | [diff] [blame] | 204 | wrk = kmalloc(sizeof(struct adj_time_work), GFP_ATOMIC); |
| 205 | if (wrk == NULL) |
| 206 | return; |
| 207 | |
| 208 | wrk->host_time = hosttime; |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 209 | if ((flags & ICTIMESYNCFLAG_SYNC) != 0) { |
K. Y. Srinivasan | 95ff7cd | 2011-08-27 11:31:31 -0700 | [diff] [blame] | 210 | INIT_WORK(&wrk->work, hv_set_host_time); |
| 211 | schedule_work(&wrk->work); |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | |
Haiyang Zhang | e9ec360 | 2010-05-11 15:11:24 +0000 | [diff] [blame] | 215 | if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) { |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 216 | scnt--; |
K. Y. Srinivasan | 95ff7cd | 2011-08-27 11:31:31 -0700 | [diff] [blame] | 217 | INIT_WORK(&wrk->work, hv_set_host_time); |
| 218 | schedule_work(&wrk->work); |
| 219 | } else |
| 220 | kfree(wrk); |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Time Sync Channel message handler. |
| 225 | */ |
| 226 | static void timesync_onchannelcallback(void *context) |
| 227 | { |
| 228 | struct vmbus_channel *channel = context; |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 229 | u32 recvlen; |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 230 | u64 requestid; |
| 231 | struct icmsg_hdr *icmsghdrp; |
| 232 | struct ictimesync_data *timedatap; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 233 | u8 *time_txf_buf = util_timesynch.recv_buffer; |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 234 | struct icmsg_negotiate *negop = NULL; |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 235 | |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 236 | vmbus_recvpacket(channel, time_txf_buf, |
| 237 | PAGE_SIZE, &recvlen, &requestid); |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 238 | |
| 239 | if (recvlen > 0) { |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 240 | icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[ |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 241 | sizeof(struct vmbuspipe_hdr)]; |
| 242 | |
| 243 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 244 | vmbus_prep_negotiate_resp(icmsghdrp, negop, |
| 245 | time_txf_buf, |
| 246 | util_fw_version, |
| 247 | ts_srv_version); |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 248 | } else { |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 249 | timedatap = (struct ictimesync_data *)&time_txf_buf[ |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 250 | sizeof(struct vmbuspipe_hdr) + |
| 251 | sizeof(struct icmsg_hdr)]; |
| 252 | adj_guesttime(timedatap->parenttime, timedatap->flags); |
| 253 | } |
| 254 | |
| 255 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 256 | | ICMSGHDRFLAG_RESPONSE; |
| 257 | |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 258 | vmbus_sendpacket(channel, time_txf_buf, |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 259 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 260 | VM_PKT_DATA_INBAND, 0); |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 261 | } |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 264 | /* |
| 265 | * Heartbeat functionality. |
| 266 | * Every two seconds, Hyper-V send us a heartbeat request message. |
| 267 | * we respond to this message, and Hyper-V knows we are alive. |
| 268 | */ |
| 269 | static void heartbeat_onchannelcallback(void *context) |
| 270 | { |
| 271 | struct vmbus_channel *channel = context; |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 272 | u32 recvlen; |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 273 | u64 requestid; |
| 274 | struct icmsg_hdr *icmsghdrp; |
| 275 | struct heartbeat_msg_data *heartbeat_msg; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 276 | u8 *hbeat_txf_buf = util_heartbeat.recv_buffer; |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 277 | struct icmsg_negotiate *negop = NULL; |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 278 | |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 279 | vmbus_recvpacket(channel, hbeat_txf_buf, |
| 280 | PAGE_SIZE, &recvlen, &requestid); |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 281 | |
| 282 | if (recvlen > 0) { |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 283 | icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[ |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 284 | sizeof(struct vmbuspipe_hdr)]; |
| 285 | |
| 286 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 287 | vmbus_prep_negotiate_resp(icmsghdrp, negop, |
| 288 | hbeat_txf_buf, util_fw_version, |
| 289 | hb_srv_version); |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 290 | } else { |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 291 | heartbeat_msg = |
| 292 | (struct heartbeat_msg_data *)&hbeat_txf_buf[ |
| 293 | sizeof(struct vmbuspipe_hdr) + |
| 294 | sizeof(struct icmsg_hdr)]; |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 295 | |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 296 | heartbeat_msg->seq_num += 1; |
| 297 | } |
| 298 | |
| 299 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 300 | | ICMSGHDRFLAG_RESPONSE; |
| 301 | |
Hank Janssen | 45241e5 | 2010-12-13 16:23:36 -0800 | [diff] [blame] | 302 | vmbus_sendpacket(channel, hbeat_txf_buf, |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 303 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 304 | VM_PKT_DATA_INBAND, 0); |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 305 | } |
Hank Janssen | 9153f7b | 2010-05-15 14:39:58 -0700 | [diff] [blame] | 306 | } |
Haiyang Zhang | 39c4e9c | 2010-05-05 19:23:46 +0000 | [diff] [blame] | 307 | |
K. Y. Srinivasan | 8494689 | 2011-09-13 10:59:38 -0700 | [diff] [blame] | 308 | static int util_probe(struct hv_device *dev, |
| 309 | const struct hv_vmbus_device_id *dev_id) |
K. Y. Srinivasan | 283f212 | 2011-08-25 09:48:36 -0700 | [diff] [blame] | 310 | { |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 311 | struct hv_util_service *srv = |
| 312 | (struct hv_util_service *)dev_id->driver_data; |
| 313 | int ret; |
| 314 | |
K. Y. Srinivasan | 9b595780 | 2012-08-13 10:06:51 -0700 | [diff] [blame] | 315 | srv->recv_buffer = kmalloc(PAGE_SIZE * 2, GFP_KERNEL); |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 316 | if (!srv->recv_buffer) |
| 317 | return -ENOMEM; |
| 318 | if (srv->util_init) { |
| 319 | ret = srv->util_init(srv); |
| 320 | if (ret) { |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 321 | ret = -ENODEV; |
| 322 | goto error1; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
K. Y. Srinivasan | 7ae3e03 | 2012-12-01 06:46:34 -0800 | [diff] [blame] | 326 | /* |
| 327 | * The set of services managed by the util driver are not performance |
| 328 | * critical and do not need batched reading. Furthermore, some services |
| 329 | * such as KVP can only handle one message from the host at a time. |
| 330 | * Turn off batched reading for all util drivers before we open the |
| 331 | * channel. |
| 332 | */ |
| 333 | |
| 334 | set_channel_read_state(dev->channel, false); |
| 335 | |
K. Y. Srinivasan | 9b595780 | 2012-08-13 10:06:51 -0700 | [diff] [blame] | 336 | ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0, |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 337 | srv->util_cb, dev->channel); |
| 338 | if (ret) |
| 339 | goto error; |
| 340 | |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 341 | hv_set_drvdata(dev, srv); |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 342 | /* |
| 343 | * Based on the host; initialize the framework and |
| 344 | * service version numbers we will negotiate. |
| 345 | */ |
| 346 | switch (vmbus_proto_version) { |
| 347 | case (VERSION_WS2008): |
| 348 | util_fw_version = UTIL_WS2K8_FW_VERSION; |
| 349 | sd_srv_version = SD_WS2008_VERSION; |
| 350 | ts_srv_version = TS_WS2008_VERSION; |
| 351 | hb_srv_version = HB_WS2008_VERSION; |
| 352 | break; |
| 353 | |
| 354 | default: |
| 355 | util_fw_version = UTIL_FW_VERSION; |
| 356 | sd_srv_version = SD_VERSION; |
| 357 | ts_srv_version = TS_VERSION; |
| 358 | hb_srv_version = HB_VERSION; |
| 359 | } |
| 360 | |
K. Y. Srinivasan | 283f212 | 2011-08-25 09:48:36 -0700 | [diff] [blame] | 361 | return 0; |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 362 | |
| 363 | error: |
| 364 | if (srv->util_deinit) |
| 365 | srv->util_deinit(); |
| 366 | error1: |
| 367 | kfree(srv->recv_buffer); |
| 368 | return ret; |
K. Y. Srinivasan | 283f212 | 2011-08-25 09:48:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static int util_remove(struct hv_device *dev) |
| 372 | { |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 373 | struct hv_util_service *srv = hv_get_drvdata(dev); |
| 374 | |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 375 | vmbus_close(dev->channel); |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 376 | if (srv->util_deinit) |
| 377 | srv->util_deinit(); |
| 378 | kfree(srv->recv_buffer); |
| 379 | |
K. Y. Srinivasan | 283f212 | 2011-08-25 09:48:36 -0700 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 384 | /* Shutdown guid */ |
K. Y. Srinivasan | d13984e | 2013-01-23 17:42:41 -0800 | [diff] [blame] | 385 | { HV_SHUTDOWN_GUID, |
| 386 | .driver_data = (unsigned long)&util_shutdown |
| 387 | }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 388 | /* Time synch guid */ |
K. Y. Srinivasan | d13984e | 2013-01-23 17:42:41 -0800 | [diff] [blame] | 389 | { HV_TS_GUID, |
| 390 | .driver_data = (unsigned long)&util_timesynch |
| 391 | }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 392 | /* Heartbeat guid */ |
K. Y. Srinivasan | d13984e | 2013-01-23 17:42:41 -0800 | [diff] [blame] | 393 | { HV_HEART_BEAT_GUID, |
| 394 | .driver_data = (unsigned long)&util_heartbeat |
| 395 | }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 396 | /* KVP guid */ |
K. Y. Srinivasan | d13984e | 2013-01-23 17:42:41 -0800 | [diff] [blame] | 397 | { HV_KVP_GUID, |
| 398 | .driver_data = (unsigned long)&util_kvp |
| 399 | }, |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 400 | /* VSS GUID */ |
| 401 | { HV_VSS_GUID, |
| 402 | .driver_data = (unsigned long)&util_vss |
| 403 | }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 404 | { }, |
K. Y. Srinivasan | 283f212 | 2011-08-25 09:48:36 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | MODULE_DEVICE_TABLE(vmbus, id_table); |
| 408 | |
| 409 | /* The one and only one */ |
| 410 | static struct hv_driver util_drv = { |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 411 | .name = "hv_util", |
K. Y. Srinivasan | 283f212 | 2011-08-25 09:48:36 -0700 | [diff] [blame] | 412 | .id_table = id_table, |
| 413 | .probe = util_probe, |
| 414 | .remove = util_remove, |
| 415 | }; |
| 416 | |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 417 | static int __init init_hyperv_utils(void) |
| 418 | { |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 419 | pr_info("Registering HyperV Utility Driver\n"); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 420 | |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 421 | return vmbus_driver_register(&util_drv); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static void exit_hyperv_utils(void) |
| 425 | { |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 426 | pr_info("De-Registered HyperV Utility Driver\n"); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 427 | |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 428 | vmbus_driver_unregister(&util_drv); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | module_init(init_hyperv_utils); |
| 432 | module_exit(exit_hyperv_utils); |
| 433 | |
| 434 | MODULE_DESCRIPTION("Hyper-V Utilities"); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 435 | MODULE_LICENSE("GPL"); |