Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An implementation of key value pair (KVP) functionality for Linux. |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2010, Novell, Inc. |
| 6 | * Author : K. Y. Srinivasan <ksrinivasan@novell.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 15 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 16 | * details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/net.h> |
| 26 | #include <linux/nls.h> |
| 27 | #include <linux/connector.h> |
| 28 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 29 | #include <linux/hyperv.h> |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 30 | |
Vitaly Kuznetsov | 3647a83 | 2015-04-11 18:07:39 -0700 | [diff] [blame] | 31 | #include "hyperv_vmbus.h" |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 32 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7) |
| 35 | */ |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 36 | #define WS2008_SRV_MAJOR 1 |
| 37 | #define WS2008_SRV_MINOR 0 |
| 38 | #define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR) |
| 39 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 40 | #define WIN7_SRV_MAJOR 3 |
| 41 | #define WIN7_SRV_MINOR 0 |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 42 | #define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR) |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 43 | |
| 44 | #define WIN8_SRV_MAJOR 4 |
| 45 | #define WIN8_SRV_MINOR 0 |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 46 | #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 49 | * Global state maintained for transaction that is being processed. For a class |
| 50 | * of integration services, including the "KVP service", the specified protocol |
| 51 | * is a "request/response" protocol which means that there can only be single |
| 52 | * outstanding transaction from the host at any given point in time. We use |
| 53 | * this to simplify memory management in this driver - we cache and process |
| 54 | * only one message at a time. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 55 | * |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 56 | * While the request/response protocol is guaranteed by the host, we further |
| 57 | * ensure this by serializing packet processing in this driver - we do not |
| 58 | * read additional packets from the VMBUs until the current packet is fully |
| 59 | * handled. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | static struct { |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 63 | int state; /* hvutil_device_state */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 64 | int recv_len; /* number of bytes received. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 65 | struct hv_kvp_msg *kvp_msg; /* current message */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 66 | struct vmbus_channel *recv_channel; /* chn we got the request */ |
| 67 | u64 recv_req_id; /* request ID. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 68 | void *kvp_context; /* for the channel callback */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 69 | } kvp_transaction; |
| 70 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 71 | /* |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 72 | * This state maintains the version number registered by the daemon. |
| 73 | */ |
| 74 | static int dm_reg_value; |
| 75 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 76 | static void kvp_send_key(struct work_struct *dummy); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 77 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 78 | |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 79 | static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error); |
Vitaly Kuznetsov | 68c8b39 | 2015-04-11 18:07:44 -0700 | [diff] [blame] | 80 | static void kvp_timeout_func(struct work_struct *dummy); |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 81 | static void kvp_register(int); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 82 | |
Vitaly Kuznetsov | 68c8b39 | 2015-04-11 18:07:44 -0700 | [diff] [blame] | 83 | static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 84 | static DECLARE_WORK(kvp_sendkey_work, kvp_send_key); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 85 | |
| 86 | static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL }; |
| 87 | static const char kvp_name[] = "kvp_kernel_module"; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 88 | static u8 *recv_buffer; |
| 89 | /* |
| 90 | * Register the kernel component with the user-level daemon. |
| 91 | * As part of this registration, pass the LIC version number. |
Olaf Hering | cfc2599 | 2013-05-29 11:29:07 +0200 | [diff] [blame] | 92 | * This number has no meaning, it satisfies the registration protocol. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 93 | */ |
Olaf Hering | cfc2599 | 2013-05-29 11:29:07 +0200 | [diff] [blame] | 94 | #define HV_DRV_VERSION "3.1" |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 95 | |
| 96 | static void |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 97 | kvp_register(int reg_value) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 98 | { |
| 99 | |
| 100 | struct cn_msg *msg; |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 101 | struct hv_kvp_msg *kvp_msg; |
| 102 | char *version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 103 | |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 104 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 105 | |
| 106 | if (msg) { |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 107 | kvp_msg = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame] | 108 | version = kvp_msg->body.kvp_register.version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 109 | msg->id.idx = CN_KVP_IDX; |
| 110 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 111 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 112 | kvp_msg->kvp_hdr.operation = reg_value; |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 113 | strcpy(version, HV_DRV_VERSION); |
| 114 | msg->len = sizeof(struct hv_kvp_msg); |
David Fries | ac8f733 | 2014-01-15 22:29:19 -0600 | [diff] [blame] | 115 | cn_netlink_send(msg, 0, 0, GFP_ATOMIC); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 116 | kfree(msg); |
| 117 | } |
| 118 | } |
Vitaly Kuznetsov | 68c8b39 | 2015-04-11 18:07:44 -0700 | [diff] [blame] | 119 | |
| 120 | static void kvp_timeout_func(struct work_struct *dummy) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 121 | { |
| 122 | /* |
| 123 | * If the timer fires, the user-mode component has not responded; |
| 124 | * process the pending transaction. |
| 125 | */ |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 126 | kvp_respond_to_host(NULL, HV_E_FAIL); |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 127 | |
| 128 | /* Transaction is finished, reset the state. */ |
| 129 | if (kvp_transaction.state > HVUTIL_READY) |
| 130 | kvp_transaction.state = HVUTIL_READY; |
| 131 | |
| 132 | hv_poll_channel(kvp_transaction.kvp_context, |
| 133 | hv_kvp_onchannelcallback); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 134 | } |
| 135 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 136 | static int kvp_handle_handshake(struct hv_kvp_msg *msg) |
| 137 | { |
| 138 | int ret = 1; |
| 139 | |
| 140 | switch (msg->kvp_hdr.operation) { |
| 141 | case KVP_OP_REGISTER: |
| 142 | dm_reg_value = KVP_OP_REGISTER; |
| 143 | pr_info("KVP: IP injection functionality not available\n"); |
| 144 | pr_info("KVP: Upgrade the KVP daemon\n"); |
| 145 | break; |
| 146 | case KVP_OP_REGISTER1: |
| 147 | dm_reg_value = KVP_OP_REGISTER1; |
| 148 | break; |
| 149 | default: |
| 150 | pr_info("KVP: incompatible daemon\n"); |
| 151 | pr_info("KVP: KVP version: %d, Daemon version: %d\n", |
| 152 | KVP_OP_REGISTER1, msg->kvp_hdr.operation); |
| 153 | ret = 0; |
| 154 | } |
| 155 | |
| 156 | if (ret) { |
| 157 | /* |
| 158 | * We have a compatible daemon; complete the handshake. |
| 159 | */ |
| 160 | pr_info("KVP: user-mode registering done.\n"); |
| 161 | kvp_register(dm_reg_value); |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 162 | kvp_transaction.state = HVUTIL_READY; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 163 | } |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 168 | /* |
| 169 | * Callback when data is received from user mode. |
| 170 | */ |
| 171 | |
| 172 | static void |
| 173 | kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
| 174 | { |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 175 | struct hv_kvp_msg *message; |
| 176 | struct hv_kvp_msg_enumerate *data; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 177 | int error = 0; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 178 | |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 179 | message = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * If we are negotiating the version information |
| 183 | * with the daemon; handle that first. |
| 184 | */ |
| 185 | |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 186 | if (kvp_transaction.state < HVUTIL_READY) { |
| 187 | kvp_handle_handshake(message); |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 191 | /* We didn't send anything to userspace so the reply is spurious */ |
| 192 | if (kvp_transaction.state < HVUTIL_USERSPACE_REQ) |
| 193 | return; |
| 194 | kvp_transaction.state = HVUTIL_USERSPACE_RECV; |
| 195 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 196 | /* |
| 197 | * Based on the version of the daemon, we propagate errors from the |
| 198 | * daemon differently. |
| 199 | */ |
| 200 | |
| 201 | data = &message->body.kvp_enum_data; |
| 202 | |
| 203 | switch (dm_reg_value) { |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 204 | case KVP_OP_REGISTER: |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 205 | /* |
| 206 | * Null string is used to pass back error condition. |
| 207 | */ |
| 208 | if (data->data.key[0] == 0) |
| 209 | error = HV_S_CONT; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 210 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 211 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 212 | case KVP_OP_REGISTER1: |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 213 | /* |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 214 | * We use the message header information from |
| 215 | * the user level daemon to transmit errors. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 216 | */ |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 217 | error = message->error; |
| 218 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 219 | } |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * Complete the transaction by forwarding the key value |
| 223 | * to the host. But first, cancel the timeout. |
| 224 | */ |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 225 | if (cancel_delayed_work_sync(&kvp_timeout_work)) { |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 226 | kvp_respond_to_host(message, error); |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 227 | kvp_transaction.state = HVUTIL_READY; |
| 228 | hv_poll_channel(kvp_transaction.kvp_context, |
| 229 | hv_kvp_onchannelcallback); |
| 230 | } |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 231 | } |
| 232 | |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 233 | |
| 234 | static int process_ob_ipinfo(void *in_msg, void *out_msg, int op) |
| 235 | { |
| 236 | struct hv_kvp_msg *in = in_msg; |
| 237 | struct hv_kvp_ip_msg *out = out_msg; |
| 238 | int len; |
| 239 | |
| 240 | switch (op) { |
| 241 | case KVP_OP_GET_IP_INFO: |
| 242 | /* |
| 243 | * Transform all parameters into utf16 encoding. |
| 244 | */ |
| 245 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr, |
| 246 | strlen((char *)in->body.kvp_ip_val.ip_addr), |
| 247 | UTF16_HOST_ENDIAN, |
| 248 | (wchar_t *)out->kvp_ip_val.ip_addr, |
| 249 | MAX_IP_ADDR_SIZE); |
| 250 | if (len < 0) |
| 251 | return len; |
| 252 | |
| 253 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net, |
| 254 | strlen((char *)in->body.kvp_ip_val.sub_net), |
| 255 | UTF16_HOST_ENDIAN, |
| 256 | (wchar_t *)out->kvp_ip_val.sub_net, |
| 257 | MAX_IP_ADDR_SIZE); |
| 258 | if (len < 0) |
| 259 | return len; |
| 260 | |
| 261 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way, |
| 262 | strlen((char *)in->body.kvp_ip_val.gate_way), |
| 263 | UTF16_HOST_ENDIAN, |
| 264 | (wchar_t *)out->kvp_ip_val.gate_way, |
| 265 | MAX_GATEWAY_SIZE); |
| 266 | if (len < 0) |
| 267 | return len; |
| 268 | |
| 269 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr, |
| 270 | strlen((char *)in->body.kvp_ip_val.dns_addr), |
| 271 | UTF16_HOST_ENDIAN, |
| 272 | (wchar_t *)out->kvp_ip_val.dns_addr, |
| 273 | MAX_IP_ADDR_SIZE); |
| 274 | if (len < 0) |
| 275 | return len; |
| 276 | |
| 277 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id, |
| 278 | strlen((char *)in->body.kvp_ip_val.adapter_id), |
| 279 | UTF16_HOST_ENDIAN, |
| 280 | (wchar_t *)out->kvp_ip_val.adapter_id, |
| 281 | MAX_IP_ADDR_SIZE); |
| 282 | if (len < 0) |
| 283 | return len; |
| 284 | |
| 285 | out->kvp_ip_val.dhcp_enabled = |
| 286 | in->body.kvp_ip_val.dhcp_enabled; |
K. Y. Srinivasan | a500e0e | 2012-09-04 17:54:13 -0700 | [diff] [blame] | 287 | out->kvp_ip_val.addr_family = |
| 288 | in->body.kvp_ip_val.addr_family; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static void process_ib_ipinfo(void *in_msg, void *out_msg, int op) |
| 295 | { |
| 296 | struct hv_kvp_ip_msg *in = in_msg; |
| 297 | struct hv_kvp_msg *out = out_msg; |
| 298 | |
| 299 | switch (op) { |
| 300 | case KVP_OP_SET_IP_INFO: |
| 301 | /* |
| 302 | * Transform all parameters into utf8 encoding. |
| 303 | */ |
| 304 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr, |
| 305 | MAX_IP_ADDR_SIZE, |
| 306 | UTF16_LITTLE_ENDIAN, |
| 307 | (__u8 *)out->body.kvp_ip_val.ip_addr, |
| 308 | MAX_IP_ADDR_SIZE); |
| 309 | |
| 310 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net, |
| 311 | MAX_IP_ADDR_SIZE, |
| 312 | UTF16_LITTLE_ENDIAN, |
| 313 | (__u8 *)out->body.kvp_ip_val.sub_net, |
| 314 | MAX_IP_ADDR_SIZE); |
| 315 | |
| 316 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way, |
| 317 | MAX_GATEWAY_SIZE, |
| 318 | UTF16_LITTLE_ENDIAN, |
| 319 | (__u8 *)out->body.kvp_ip_val.gate_way, |
| 320 | MAX_GATEWAY_SIZE); |
| 321 | |
| 322 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr, |
| 323 | MAX_IP_ADDR_SIZE, |
| 324 | UTF16_LITTLE_ENDIAN, |
| 325 | (__u8 *)out->body.kvp_ip_val.dns_addr, |
| 326 | MAX_IP_ADDR_SIZE); |
| 327 | |
| 328 | out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled; |
| 329 | |
| 330 | default: |
| 331 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id, |
| 332 | MAX_ADAPTER_ID_SIZE, |
| 333 | UTF16_LITTLE_ENDIAN, |
| 334 | (__u8 *)out->body.kvp_ip_val.adapter_id, |
| 335 | MAX_ADAPTER_ID_SIZE); |
| 336 | |
| 337 | out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 344 | static void |
| 345 | kvp_send_key(struct work_struct *dummy) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 346 | { |
| 347 | struct cn_msg *msg; |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 348 | struct hv_kvp_msg *message; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 349 | struct hv_kvp_msg *in_msg; |
| 350 | __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation; |
| 351 | __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool; |
| 352 | __u32 val32; |
| 353 | __u64 val64; |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 354 | int rc; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 355 | |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 356 | /* The transaction state is wrong. */ |
| 357 | if (kvp_transaction.state != HVUTIL_HOSTMSG_RECEIVED) |
| 358 | return; |
| 359 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 360 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC); |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 361 | if (!msg) |
| 362 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 363 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 364 | msg->id.idx = CN_KVP_IDX; |
| 365 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 264033543 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 366 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 367 | message = (struct hv_kvp_msg *)msg->data; |
| 368 | message->kvp_hdr.operation = operation; |
| 369 | message->kvp_hdr.pool = pool; |
| 370 | in_msg = kvp_transaction.kvp_msg; |
| 371 | |
| 372 | /* |
| 373 | * The key/value strings sent from the host are encoded in |
| 374 | * in utf16; convert it to utf8 strings. |
| 375 | * The host assures us that the utf16 strings will not exceed |
| 376 | * the max lengths specified. We will however, reserve room |
| 377 | * for the string terminating character - in the utf16s_utf8s() |
| 378 | * function we limit the size of the buffer where the converted |
| 379 | * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee |
| 380 | * that the strings can be properly terminated! |
| 381 | */ |
| 382 | |
| 383 | switch (message->kvp_hdr.operation) { |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 384 | case KVP_OP_SET_IP_INFO: |
| 385 | process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO); |
| 386 | break; |
| 387 | case KVP_OP_GET_IP_INFO: |
| 388 | process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO); |
| 389 | break; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 390 | case KVP_OP_SET: |
| 391 | switch (in_msg->body.kvp_set.data.value_type) { |
| 392 | case REG_SZ: |
| 393 | /* |
| 394 | * The value is a string - utf16 encoding. |
| 395 | */ |
| 396 | message->body.kvp_set.data.value_size = |
| 397 | utf16s_to_utf8s( |
| 398 | (wchar_t *)in_msg->body.kvp_set.data.value, |
| 399 | in_msg->body.kvp_set.data.value_size, |
| 400 | UTF16_LITTLE_ENDIAN, |
| 401 | message->body.kvp_set.data.value, |
| 402 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1; |
| 403 | break; |
| 404 | |
| 405 | case REG_U32: |
| 406 | /* |
| 407 | * The value is a 32 bit scalar. |
| 408 | * We save this as a utf8 string. |
| 409 | */ |
| 410 | val32 = in_msg->body.kvp_set.data.value_u32; |
| 411 | message->body.kvp_set.data.value_size = |
| 412 | sprintf(message->body.kvp_set.data.value, |
| 413 | "%d", val32) + 1; |
| 414 | break; |
| 415 | |
| 416 | case REG_U64: |
| 417 | /* |
| 418 | * The value is a 64 bit scalar. |
| 419 | * We save this as a utf8 string. |
| 420 | */ |
| 421 | val64 = in_msg->body.kvp_set.data.value_u64; |
| 422 | message->body.kvp_set.data.value_size = |
| 423 | sprintf(message->body.kvp_set.data.value, |
| 424 | "%llu", val64) + 1; |
| 425 | break; |
| 426 | |
| 427 | } |
| 428 | case KVP_OP_GET: |
| 429 | message->body.kvp_set.data.key_size = |
| 430 | utf16s_to_utf8s( |
| 431 | (wchar_t *)in_msg->body.kvp_set.data.key, |
| 432 | in_msg->body.kvp_set.data.key_size, |
| 433 | UTF16_LITTLE_ENDIAN, |
| 434 | message->body.kvp_set.data.key, |
| 435 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 436 | break; |
| 437 | |
| 438 | case KVP_OP_DELETE: |
| 439 | message->body.kvp_delete.key_size = |
| 440 | utf16s_to_utf8s( |
| 441 | (wchar_t *)in_msg->body.kvp_delete.key, |
| 442 | in_msg->body.kvp_delete.key_size, |
| 443 | UTF16_LITTLE_ENDIAN, |
| 444 | message->body.kvp_delete.key, |
| 445 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 446 | break; |
| 447 | |
| 448 | case KVP_OP_ENUMERATE: |
| 449 | message->body.kvp_enum_data.index = |
| 450 | in_msg->body.kvp_enum_data.index; |
| 451 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 452 | } |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 453 | |
| 454 | msg->len = sizeof(struct hv_kvp_msg); |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 455 | kvp_transaction.state = HVUTIL_USERSPACE_REQ; |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 456 | rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC); |
| 457 | if (rc) { |
| 458 | pr_debug("KVP: failed to communicate to the daemon: %d\n", rc); |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 459 | if (cancel_delayed_work_sync(&kvp_timeout_work)) { |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 460 | kvp_respond_to_host(message, HV_E_FAIL); |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 461 | kvp_transaction.state = HVUTIL_READY; |
| 462 | } |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 463 | } |
| 464 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 465 | kfree(msg); |
| 466 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 467 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Send a response back to the host. |
| 472 | */ |
| 473 | |
| 474 | static void |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 475 | kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 476 | { |
| 477 | struct hv_kvp_msg *kvp_msg; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 478 | struct hv_kvp_exchg_msg_value *kvp_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 479 | char *key_name; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 480 | char *value; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 481 | struct icmsg_hdr *icmsghdrp; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 482 | int keylen = 0; |
| 483 | int valuelen = 0; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 484 | u32 buf_len; |
| 485 | struct vmbus_channel *channel; |
| 486 | u64 req_id; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 487 | int ret; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 488 | |
| 489 | /* |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 490 | * Copy the global state for completing the transaction. Note that |
| 491 | * only one transaction can be active at a time. |
| 492 | */ |
| 493 | |
| 494 | buf_len = kvp_transaction.recv_len; |
| 495 | channel = kvp_transaction.recv_channel; |
| 496 | req_id = kvp_transaction.recv_req_id; |
| 497 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 498 | icmsghdrp = (struct icmsg_hdr *) |
| 499 | &recv_buffer[sizeof(struct vmbuspipe_hdr)]; |
| 500 | |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 501 | if (channel->onchannel_callback == NULL) |
| 502 | /* |
| 503 | * We have raced with util driver being unloaded; |
| 504 | * silently return. |
| 505 | */ |
| 506 | return; |
| 507 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 508 | icmsghdrp->status = error; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 509 | |
| 510 | /* |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 511 | * If the error parameter is set, terminate the host's enumeration |
| 512 | * on this pool. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 513 | */ |
| 514 | if (error) { |
| 515 | /* |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 516 | * Something failed or we have timedout; |
| 517 | * terminate the current host-side iteration. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 518 | */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 519 | goto response_done; |
| 520 | } |
| 521 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 522 | kvp_msg = (struct hv_kvp_msg *) |
| 523 | &recv_buffer[sizeof(struct vmbuspipe_hdr) + |
| 524 | sizeof(struct icmsg_hdr)]; |
| 525 | |
| 526 | switch (kvp_transaction.kvp_msg->kvp_hdr.operation) { |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 527 | case KVP_OP_GET_IP_INFO: |
| 528 | ret = process_ob_ipinfo(msg_to_host, |
| 529 | (struct hv_kvp_ip_msg *)kvp_msg, |
| 530 | KVP_OP_GET_IP_INFO); |
| 531 | if (ret < 0) |
| 532 | icmsghdrp->status = HV_E_FAIL; |
| 533 | |
| 534 | goto response_done; |
| 535 | case KVP_OP_SET_IP_INFO: |
| 536 | goto response_done; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 537 | case KVP_OP_GET: |
| 538 | kvp_data = &kvp_msg->body.kvp_get.data; |
| 539 | goto copy_value; |
| 540 | |
| 541 | case KVP_OP_SET: |
| 542 | case KVP_OP_DELETE: |
| 543 | goto response_done; |
| 544 | |
| 545 | default: |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | kvp_data = &kvp_msg->body.kvp_enum_data.data; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 550 | key_name = msg_to_host->body.kvp_enum_data.data.key; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 551 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 552 | /* |
| 553 | * The windows host expects the key/value pair to be encoded |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 554 | * in utf16. Ensure that the key/value size reported to the host |
| 555 | * will be less than or equal to the MAX size (including the |
| 556 | * terminating character). |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 557 | */ |
Alan Stern | 0720a06 | 2011-11-17 16:42:19 -0500 | [diff] [blame] | 558 | keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN, |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 559 | (wchar_t *) kvp_data->key, |
| 560 | (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2); |
| 561 | kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 562 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 563 | copy_value: |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 564 | value = msg_to_host->body.kvp_enum_data.data.value; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 565 | valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN, |
| 566 | (wchar_t *) kvp_data->value, |
| 567 | (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2); |
| 568 | kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */ |
| 569 | |
| 570 | /* |
| 571 | * If the utf8s to utf16s conversion failed; notify host |
| 572 | * of the error. |
| 573 | */ |
| 574 | if ((keylen < 0) || (valuelen < 0)) |
| 575 | icmsghdrp->status = HV_E_FAIL; |
| 576 | |
| 577 | kvp_data->value_type = REG_SZ; /* all our values are strings */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 578 | |
| 579 | response_done: |
| 580 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; |
| 581 | |
| 582 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 583 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* |
| 587 | * This callback is invoked when we get a KVP message from the host. |
| 588 | * The host ensures that only one KVP transaction can be active at a time. |
| 589 | * KVP implementation in Linux needs to forward the key to a user-mde |
| 590 | * component to retrive the corresponding value. Consequently, we cannot |
| 591 | * respond to the host in the conext of this callback. Since the host |
| 592 | * guarantees that at most only one transaction can be active at a time, |
| 593 | * we stash away the transaction state in a set of global variables. |
| 594 | */ |
| 595 | |
| 596 | void hv_kvp_onchannelcallback(void *context) |
| 597 | { |
| 598 | struct vmbus_channel *channel = context; |
| 599 | u32 recvlen; |
| 600 | u64 requestid; |
| 601 | |
| 602 | struct hv_kvp_msg *kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 603 | |
| 604 | struct icmsg_hdr *icmsghdrp; |
| 605 | struct icmsg_negotiate *negop = NULL; |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 606 | int util_fw_version; |
| 607 | int kvp_srv_version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 608 | |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 609 | if (kvp_transaction.state > HVUTIL_READY) { |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 610 | /* |
| 611 | * We will defer processing this callback once |
| 612 | * the current transaction is complete. |
| 613 | */ |
| 614 | kvp_transaction.kvp_context = context; |
| 615 | return; |
| 616 | } |
Vitaly Kuznetsov | 5fa9748 | 2015-04-11 18:07:40 -0700 | [diff] [blame] | 617 | kvp_transaction.kvp_context = NULL; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 618 | |
K. Y. Srinivasan | 9bd2d0d | 2014-07-07 16:34:25 -0700 | [diff] [blame] | 619 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen, |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 620 | &requestid); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 621 | |
| 622 | if (recvlen > 0) { |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 623 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ |
| 624 | sizeof(struct vmbuspipe_hdr)]; |
| 625 | |
| 626 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 627 | /* |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 628 | * Based on the host, select appropriate |
| 629 | * framework and service versions we will |
| 630 | * negotiate. |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 631 | */ |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 632 | switch (vmbus_proto_version) { |
| 633 | case (VERSION_WS2008): |
| 634 | util_fw_version = UTIL_WS2K8_FW_VERSION; |
| 635 | kvp_srv_version = WS2008_SRV_VERSION; |
| 636 | break; |
| 637 | case (VERSION_WIN7): |
| 638 | util_fw_version = UTIL_FW_VERSION; |
| 639 | kvp_srv_version = WIN7_SRV_VERSION; |
| 640 | break; |
| 641 | default: |
| 642 | util_fw_version = UTIL_FW_VERSION; |
| 643 | kvp_srv_version = WIN8_SRV_VERSION; |
| 644 | } |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 645 | vmbus_prep_negotiate_resp(icmsghdrp, negop, |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 646 | recv_buffer, util_fw_version, |
| 647 | kvp_srv_version); |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 648 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 649 | } else { |
| 650 | kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ |
| 651 | sizeof(struct vmbuspipe_hdr) + |
| 652 | sizeof(struct icmsg_hdr)]; |
| 653 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 654 | /* |
| 655 | * Stash away this global state for completing the |
| 656 | * transaction; note transactions are serialized. |
| 657 | */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 658 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 659 | kvp_transaction.recv_len = recvlen; |
| 660 | kvp_transaction.recv_channel = channel; |
| 661 | kvp_transaction.recv_req_id = requestid; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 662 | kvp_transaction.kvp_msg = kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 663 | |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 664 | if (kvp_transaction.state < HVUTIL_READY) { |
| 665 | /* Userspace is not registered yet */ |
| 666 | kvp_respond_to_host(NULL, HV_E_FAIL); |
| 667 | return; |
| 668 | } |
| 669 | kvp_transaction.state = HVUTIL_HOSTMSG_RECEIVED; |
| 670 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 671 | /* |
| 672 | * Get the information from the |
| 673 | * user-mode component. |
| 674 | * component. This transaction will be |
| 675 | * completed when we get the value from |
| 676 | * the user-mode component. |
| 677 | * Set a timeout to deal with |
| 678 | * user-mode not responding. |
| 679 | */ |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 680 | schedule_work(&kvp_sendkey_work); |
Vitaly Kuznetsov | 68c8b39 | 2015-04-11 18:07:44 -0700 | [diff] [blame] | 681 | schedule_delayed_work(&kvp_timeout_work, 5*HZ); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 682 | |
| 683 | return; |
| 684 | |
| 685 | } |
| 686 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 687 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 688 | | ICMSGHDRFLAG_RESPONSE; |
| 689 | |
| 690 | vmbus_sendpacket(channel, recv_buffer, |
| 691 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 692 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | } |
| 696 | |
| 697 | int |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 698 | hv_kvp_init(struct hv_util_service *srv) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 699 | { |
| 700 | int err; |
| 701 | |
| 702 | err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback); |
| 703 | if (err) |
| 704 | return err; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 705 | recv_buffer = srv->recv_buffer; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 706 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 707 | /* |
| 708 | * When this driver loads, the user level daemon that |
| 709 | * processes the host requests may not yet be running. |
| 710 | * Defer processing channel callbacks until the daemon |
| 711 | * has registered. |
| 712 | */ |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 713 | kvp_transaction.state = HVUTIL_DEVICE_INIT; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 714 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | void hv_kvp_deinit(void) |
| 719 | { |
Vitaly Kuznetsov | 97bf16c | 2015-04-11 18:07:47 -0700 | [diff] [blame^] | 720 | kvp_transaction.state = HVUTIL_DEVICE_DYING; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 721 | cn_del_callback(&kvp_id); |
Vitaly Kuznetsov | 68c8b39 | 2015-04-11 18:07:44 -0700 | [diff] [blame] | 722 | cancel_delayed_work_sync(&kvp_timeout_work); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 723 | cancel_work_sync(&kvp_sendkey_work); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 724 | } |