blob: a414c8397f8840902a87581d080887a6aa6e0cba [file] [log] [blame]
Ky Srinivasan245ba562010-12-16 18:54:16 -07001/*
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 Janssenaf7a5b62011-03-29 13:58:49 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Ky Srinivasan245ba562010-12-16 18:54:16 -070024
25#include <linux/net.h>
26#include <linux/nls.h>
27#include <linux/connector.h>
28#include <linux/workqueue.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
Ky Srinivasan245ba562010-12-16 18:54:16 -070030
Vitaly Kuznetsov3647a832015-04-11 18:07:39 -070031#include "hyperv_vmbus.h"
Ky Srinivasan245ba562010-12-16 18:54:16 -070032
K. Y. Srinivasan67413352013-07-02 10:31:30 -070033/*
34 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
35 */
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070036#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. Srinivasan67413352013-07-02 10:31:30 -070040#define WIN7_SRV_MAJOR 3
41#define WIN7_SRV_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070042#define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
K. Y. Srinivasan67413352013-07-02 10:31:30 -070043
44#define WIN8_SRV_MAJOR 4
45#define WIN8_SRV_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070046#define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
Ky Srinivasan245ba562010-12-16 18:54:16 -070047
48/*
49 * Global state maintained for transaction that is being processed.
50 * Note that only one transaction can be active at any point in time.
51 *
52 * This state is set when we receive a request from the host; we
53 * cleanup this state when the transaction is completed - when we respond
54 * to the host with the key value.
55 */
56
57static struct {
58 bool active; /* transaction status - active or not */
59 int recv_len; /* number of bytes received. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070060 struct hv_kvp_msg *kvp_msg; /* current message */
Ky Srinivasan245ba562010-12-16 18:54:16 -070061 struct vmbus_channel *recv_channel; /* chn we got the request */
62 u64 recv_req_id; /* request ID. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070063 void *kvp_context; /* for the channel callback */
Ky Srinivasan245ba562010-12-16 18:54:16 -070064} kvp_transaction;
65
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070066/*
67 * Before we can accept KVP messages from the host, we need
68 * to handshake with the user level daemon. This state tracks
69 * if we are in the handshake phase.
70 */
71static bool in_hand_shake = true;
72
73/*
74 * This state maintains the version number registered by the daemon.
75 */
76static int dm_reg_value;
77
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070078static void kvp_send_key(struct work_struct *dummy);
Ky Srinivasan245ba562010-12-16 18:54:16 -070079
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -070080
K. Y. Srinivasan03db7722012-08-16 18:32:12 -070081static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
Ky Srinivasan245ba562010-12-16 18:54:16 -070082static void kvp_work_func(struct work_struct *dummy);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070083static void kvp_register(int);
Ky Srinivasan245ba562010-12-16 18:54:16 -070084
85static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070086static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
Ky Srinivasan245ba562010-12-16 18:54:16 -070087
88static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
89static const char kvp_name[] = "kvp_kernel_module";
Ky Srinivasan245ba562010-12-16 18:54:16 -070090static u8 *recv_buffer;
91/*
92 * Register the kernel component with the user-level daemon.
93 * As part of this registration, pass the LIC version number.
Olaf Heringcfc25992013-05-29 11:29:07 +020094 * This number has no meaning, it satisfies the registration protocol.
Ky Srinivasan245ba562010-12-16 18:54:16 -070095 */
Olaf Heringcfc25992013-05-29 11:29:07 +020096#define HV_DRV_VERSION "3.1"
Ky Srinivasan245ba562010-12-16 18:54:16 -070097
98static void
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070099kvp_register(int reg_value)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700100{
101
102 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800103 struct hv_kvp_msg *kvp_msg;
104 char *version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700105
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800106 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700107
108 if (msg) {
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800109 kvp_msg = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800110 version = kvp_msg->body.kvp_register.version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700111 msg->id.idx = CN_KVP_IDX;
112 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800113
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700114 kvp_msg->kvp_hdr.operation = reg_value;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800115 strcpy(version, HV_DRV_VERSION);
116 msg->len = sizeof(struct hv_kvp_msg);
David Friesac8f7332014-01-15 22:29:19 -0600117 cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700118 kfree(msg);
119 }
120}
121static void
122kvp_work_func(struct work_struct *dummy)
123{
124 /*
125 * If the timer fires, the user-mode component has not responded;
126 * process the pending transaction.
127 */
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700128 kvp_respond_to_host(NULL, HV_E_FAIL);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700129}
130
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700131static void poll_channel(struct vmbus_channel *channel)
132{
133 if (channel->target_cpu != smp_processor_id())
134 smp_call_function_single(channel->target_cpu,
135 hv_kvp_onchannelcallback,
136 channel, true);
137 else
138 hv_kvp_onchannelcallback(channel);
139}
140
141
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700142static int kvp_handle_handshake(struct hv_kvp_msg *msg)
143{
144 int ret = 1;
145
146 switch (msg->kvp_hdr.operation) {
147 case KVP_OP_REGISTER:
148 dm_reg_value = KVP_OP_REGISTER;
149 pr_info("KVP: IP injection functionality not available\n");
150 pr_info("KVP: Upgrade the KVP daemon\n");
151 break;
152 case KVP_OP_REGISTER1:
153 dm_reg_value = KVP_OP_REGISTER1;
154 break;
155 default:
156 pr_info("KVP: incompatible daemon\n");
157 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
158 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
159 ret = 0;
160 }
161
162 if (ret) {
163 /*
164 * We have a compatible daemon; complete the handshake.
165 */
166 pr_info("KVP: user-mode registering done.\n");
167 kvp_register(dm_reg_value);
168 kvp_transaction.active = false;
169 if (kvp_transaction.kvp_context)
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700170 poll_channel(kvp_transaction.kvp_context);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700171 }
172 return ret;
173}
174
175
Ky Srinivasan245ba562010-12-16 18:54:16 -0700176/*
177 * Callback when data is received from user mode.
178 */
179
180static void
181kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
182{
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800183 struct hv_kvp_msg *message;
184 struct hv_kvp_msg_enumerate *data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700185 int error = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700186
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800187 message = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700188
189 /*
190 * If we are negotiating the version information
191 * with the daemon; handle that first.
192 */
193
194 if (in_hand_shake) {
195 if (kvp_handle_handshake(message))
196 in_hand_shake = false;
197 return;
198 }
199
200 /*
201 * Based on the version of the daemon, we propagate errors from the
202 * daemon differently.
203 */
204
205 data = &message->body.kvp_enum_data;
206
207 switch (dm_reg_value) {
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700208 case KVP_OP_REGISTER:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700209 /*
210 * Null string is used to pass back error condition.
211 */
212 if (data->data.key[0] == 0)
213 error = HV_S_CONT;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700214 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700215
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700216 case KVP_OP_REGISTER1:
Ky Srinivasan245ba562010-12-16 18:54:16 -0700217 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700218 * We use the message header information from
219 * the user level daemon to transmit errors.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700220 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700221 error = message->error;
222 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700223 }
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700224
225 /*
226 * Complete the transaction by forwarding the key value
227 * to the host. But first, cancel the timeout.
228 */
229 if (cancel_delayed_work_sync(&kvp_work))
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700230 kvp_respond_to_host(message, error);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700231}
232
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700233
234static 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. Srinivasana500e0e2012-09-04 17:54:13 -0700287 out->kvp_ip_val.addr_family =
288 in->body.kvp_ip_val.addr_family;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700289 }
290
291 return 0;
292}
293
294static 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. Srinivasane2bb6532011-10-09 19:42:28 -0700344static void
345kvp_send_key(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700346{
347 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800348 struct hv_kvp_msg *message;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700349 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 Kuznetsov8d9560e2014-11-06 18:21:25 +0100354 int rc;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700355
356 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700357 if (!msg)
358 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700359
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700360 msg->id.idx = CN_KVP_IDX;
361 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800362
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700363 message = (struct hv_kvp_msg *)msg->data;
364 message->kvp_hdr.operation = operation;
365 message->kvp_hdr.pool = pool;
366 in_msg = kvp_transaction.kvp_msg;
367
368 /*
369 * The key/value strings sent from the host are encoded in
370 * in utf16; convert it to utf8 strings.
371 * The host assures us that the utf16 strings will not exceed
372 * the max lengths specified. We will however, reserve room
373 * for the string terminating character - in the utf16s_utf8s()
374 * function we limit the size of the buffer where the converted
375 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
376 * that the strings can be properly terminated!
377 */
378
379 switch (message->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700380 case KVP_OP_SET_IP_INFO:
381 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
382 break;
383 case KVP_OP_GET_IP_INFO:
384 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
385 break;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700386 case KVP_OP_SET:
387 switch (in_msg->body.kvp_set.data.value_type) {
388 case REG_SZ:
389 /*
390 * The value is a string - utf16 encoding.
391 */
392 message->body.kvp_set.data.value_size =
393 utf16s_to_utf8s(
394 (wchar_t *)in_msg->body.kvp_set.data.value,
395 in_msg->body.kvp_set.data.value_size,
396 UTF16_LITTLE_ENDIAN,
397 message->body.kvp_set.data.value,
398 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
399 break;
400
401 case REG_U32:
402 /*
403 * The value is a 32 bit scalar.
404 * We save this as a utf8 string.
405 */
406 val32 = in_msg->body.kvp_set.data.value_u32;
407 message->body.kvp_set.data.value_size =
408 sprintf(message->body.kvp_set.data.value,
409 "%d", val32) + 1;
410 break;
411
412 case REG_U64:
413 /*
414 * The value is a 64 bit scalar.
415 * We save this as a utf8 string.
416 */
417 val64 = in_msg->body.kvp_set.data.value_u64;
418 message->body.kvp_set.data.value_size =
419 sprintf(message->body.kvp_set.data.value,
420 "%llu", val64) + 1;
421 break;
422
423 }
424 case KVP_OP_GET:
425 message->body.kvp_set.data.key_size =
426 utf16s_to_utf8s(
427 (wchar_t *)in_msg->body.kvp_set.data.key,
428 in_msg->body.kvp_set.data.key_size,
429 UTF16_LITTLE_ENDIAN,
430 message->body.kvp_set.data.key,
431 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
432 break;
433
434 case KVP_OP_DELETE:
435 message->body.kvp_delete.key_size =
436 utf16s_to_utf8s(
437 (wchar_t *)in_msg->body.kvp_delete.key,
438 in_msg->body.kvp_delete.key_size,
439 UTF16_LITTLE_ENDIAN,
440 message->body.kvp_delete.key,
441 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
442 break;
443
444 case KVP_OP_ENUMERATE:
445 message->body.kvp_enum_data.index =
446 in_msg->body.kvp_enum_data.index;
447 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700448 }
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700449
450 msg->len = sizeof(struct hv_kvp_msg);
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100451 rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
452 if (rc) {
453 pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
454 if (cancel_delayed_work_sync(&kvp_work))
455 kvp_respond_to_host(message, HV_E_FAIL);
456 }
457
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700458 kfree(msg);
459
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700460 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700461}
462
463/*
464 * Send a response back to the host.
465 */
466
467static void
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700468kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700469{
470 struct hv_kvp_msg *kvp_msg;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700471 struct hv_kvp_exchg_msg_value *kvp_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700472 char *key_name;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700473 char *value;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700474 struct icmsg_hdr *icmsghdrp;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700475 int keylen = 0;
476 int valuelen = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700477 u32 buf_len;
478 struct vmbus_channel *channel;
479 u64 req_id;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700480 int ret;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700481
482 /*
483 * If a transaction is not active; log and return.
484 */
485
486 if (!kvp_transaction.active) {
487 /*
488 * This is a spurious call!
489 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700490 pr_warn("KVP: Transaction not active\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700491 return;
492 }
493 /*
494 * Copy the global state for completing the transaction. Note that
495 * only one transaction can be active at a time.
496 */
497
498 buf_len = kvp_transaction.recv_len;
499 channel = kvp_transaction.recv_channel;
500 req_id = kvp_transaction.recv_req_id;
501
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -0700502 kvp_transaction.active = false;
503
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700504 icmsghdrp = (struct icmsg_hdr *)
505 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
506
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700507 if (channel->onchannel_callback == NULL)
508 /*
509 * We have raced with util driver being unloaded;
510 * silently return.
511 */
512 return;
513
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700514 icmsghdrp->status = error;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700515
516 /*
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700517 * If the error parameter is set, terminate the host's enumeration
518 * on this pool.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700519 */
520 if (error) {
521 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700522 * Something failed or we have timedout;
523 * terminate the current host-side iteration.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700524 */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700525 goto response_done;
526 }
527
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700528 kvp_msg = (struct hv_kvp_msg *)
529 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
530 sizeof(struct icmsg_hdr)];
531
532 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700533 case KVP_OP_GET_IP_INFO:
534 ret = process_ob_ipinfo(msg_to_host,
535 (struct hv_kvp_ip_msg *)kvp_msg,
536 KVP_OP_GET_IP_INFO);
537 if (ret < 0)
538 icmsghdrp->status = HV_E_FAIL;
539
540 goto response_done;
541 case KVP_OP_SET_IP_INFO:
542 goto response_done;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700543 case KVP_OP_GET:
544 kvp_data = &kvp_msg->body.kvp_get.data;
545 goto copy_value;
546
547 case KVP_OP_SET:
548 case KVP_OP_DELETE:
549 goto response_done;
550
551 default:
552 break;
553 }
554
555 kvp_data = &kvp_msg->body.kvp_enum_data.data;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700556 key_name = msg_to_host->body.kvp_enum_data.data.key;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700557
Ky Srinivasan245ba562010-12-16 18:54:16 -0700558 /*
559 * The windows host expects the key/value pair to be encoded
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700560 * in utf16. Ensure that the key/value size reported to the host
561 * will be less than or equal to the MAX size (including the
562 * terminating character).
Ky Srinivasan245ba562010-12-16 18:54:16 -0700563 */
Alan Stern0720a062011-11-17 16:42:19 -0500564 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700565 (wchar_t *) kvp_data->key,
566 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
567 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700568
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700569copy_value:
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700570 value = msg_to_host->body.kvp_enum_data.data.value;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700571 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
572 (wchar_t *) kvp_data->value,
573 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
574 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
575
576 /*
577 * If the utf8s to utf16s conversion failed; notify host
578 * of the error.
579 */
580 if ((keylen < 0) || (valuelen < 0))
581 icmsghdrp->status = HV_E_FAIL;
582
583 kvp_data->value_type = REG_SZ; /* all our values are strings */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700584
585response_done:
586 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
587
588 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800589 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700590 poll_channel(channel);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700591}
592
593/*
594 * This callback is invoked when we get a KVP message from the host.
595 * The host ensures that only one KVP transaction can be active at a time.
596 * KVP implementation in Linux needs to forward the key to a user-mde
597 * component to retrive the corresponding value. Consequently, we cannot
598 * respond to the host in the conext of this callback. Since the host
599 * guarantees that at most only one transaction can be active at a time,
600 * we stash away the transaction state in a set of global variables.
601 */
602
603void hv_kvp_onchannelcallback(void *context)
604{
605 struct vmbus_channel *channel = context;
606 u32 recvlen;
607 u64 requestid;
608
609 struct hv_kvp_msg *kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700610
611 struct icmsg_hdr *icmsghdrp;
612 struct icmsg_negotiate *negop = NULL;
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700613 int util_fw_version;
614 int kvp_srv_version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700615
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700616 if (kvp_transaction.active) {
617 /*
618 * We will defer processing this callback once
619 * the current transaction is complete.
620 */
621 kvp_transaction.kvp_context = context;
622 return;
623 }
Ky Srinivasan245ba562010-12-16 18:54:16 -0700624
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700625 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700626 &requestid);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700627
628 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700629 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
630 sizeof(struct vmbuspipe_hdr)];
631
632 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700633 /*
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700634 * Based on the host, select appropriate
635 * framework and service versions we will
636 * negotiate.
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700637 */
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700638 switch (vmbus_proto_version) {
639 case (VERSION_WS2008):
640 util_fw_version = UTIL_WS2K8_FW_VERSION;
641 kvp_srv_version = WS2008_SRV_VERSION;
642 break;
643 case (VERSION_WIN7):
644 util_fw_version = UTIL_FW_VERSION;
645 kvp_srv_version = WIN7_SRV_VERSION;
646 break;
647 default:
648 util_fw_version = UTIL_FW_VERSION;
649 kvp_srv_version = WIN8_SRV_VERSION;
650 }
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700651 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700652 recv_buffer, util_fw_version,
653 kvp_srv_version);
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700654
Ky Srinivasan245ba562010-12-16 18:54:16 -0700655 } else {
656 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
657 sizeof(struct vmbuspipe_hdr) +
658 sizeof(struct icmsg_hdr)];
659
Ky Srinivasan245ba562010-12-16 18:54:16 -0700660 /*
661 * Stash away this global state for completing the
662 * transaction; note transactions are serialized.
663 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700664
Ky Srinivasan245ba562010-12-16 18:54:16 -0700665 kvp_transaction.recv_len = recvlen;
666 kvp_transaction.recv_channel = channel;
667 kvp_transaction.recv_req_id = requestid;
668 kvp_transaction.active = true;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700669 kvp_transaction.kvp_msg = kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700670
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. Srinivasane2bb6532011-10-09 19:42:28 -0700680 schedule_work(&kvp_sendkey_work);
681 schedule_delayed_work(&kvp_work, 5*HZ);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700682
683 return;
684
685 }
686
Ky Srinivasan245ba562010-12-16 18:54:16 -0700687 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
688 | ICMSGHDRFLAG_RESPONSE;
689
690 vmbus_sendpacket(channel, recv_buffer,
691 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800692 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700693 }
694
695}
696
697int
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700698hv_kvp_init(struct hv_util_service *srv)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700699{
700 int err;
701
702 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
703 if (err)
704 return err;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700705 recv_buffer = srv->recv_buffer;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700706
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700707 /*
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 */
713 kvp_transaction.active = true;
714
Ky Srinivasan245ba562010-12-16 18:54:16 -0700715 return 0;
716}
717
718void hv_kvp_deinit(void)
719{
720 cn_del_callback(&kvp_id);
721 cancel_delayed_work_sync(&kvp_work);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700722 cancel_work_sync(&kvp_sendkey_work);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700723}