Drivers: hv: kvp: switch to using the hvutil_device_state state machine
[linux-2.6-block.git] / drivers / hv / hv_kvp.c
CommitLineData
245ba56a
KS
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 */
af7a5b6e 23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
245ba56a
KS
24
25#include <linux/net.h>
26#include <linux/nls.h>
27#include <linux/connector.h>
28#include <linux/workqueue.h>
46a97191 29#include <linux/hyperv.h>
245ba56a 30
3647a83d 31#include "hyperv_vmbus.h"
245ba56a 32
6741335b
S
33/*
34 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
35 */
3a491605
S
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
6741335b
S
40#define WIN7_SRV_MAJOR 3
41#define WIN7_SRV_MINOR 0
3a491605 42#define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
6741335b
S
43
44#define WIN8_SRV_MAJOR 4
45#define WIN8_SRV_MINOR 0
3a491605 46#define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
245ba56a
KS
47
48/*
97bf16cd
VK
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.
245ba56a 55 *
97bf16cd
VK
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.
245ba56a
KS
60 */
61
62static struct {
97bf16cd 63 int state; /* hvutil_device_state */
245ba56a 64 int recv_len; /* number of bytes received. */
fa3d5b85 65 struct hv_kvp_msg *kvp_msg; /* current message */
245ba56a
KS
66 struct vmbus_channel *recv_channel; /* chn we got the request */
67 u64 recv_req_id; /* request ID. */
fa3d5b85 68 void *kvp_context; /* for the channel callback */
245ba56a
KS
69} kvp_transaction;
70
b47a81dc
S
71/*
72 * This state maintains the version number registered by the daemon.
73 */
74static int dm_reg_value;
75
e2bb6537 76static void kvp_send_key(struct work_struct *dummy);
245ba56a 77
76e5f813 78
03db7724 79static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
68c8b39a 80static void kvp_timeout_func(struct work_struct *dummy);
b47a81dc 81static void kvp_register(int);
245ba56a 82
68c8b39a 83static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
e2bb6537 84static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
245ba56a
KS
85
86static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
87static const char kvp_name[] = "kvp_kernel_module";
245ba56a
KS
88static 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.
cfc25993 92 * This number has no meaning, it satisfies the registration protocol.
245ba56a 93 */
cfc25993 94#define HV_DRV_VERSION "3.1"
245ba56a
KS
95
96static void
b47a81dc 97kvp_register(int reg_value)
245ba56a
KS
98{
99
100 struct cn_msg *msg;
26403354
S
101 struct hv_kvp_msg *kvp_msg;
102 char *version;
245ba56a 103
26403354 104 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
245ba56a
KS
105
106 if (msg) {
26403354 107 kvp_msg = (struct hv_kvp_msg *)msg->data;
e485ceac 108 version = kvp_msg->body.kvp_register.version;
245ba56a
KS
109 msg->id.idx = CN_KVP_IDX;
110 msg->id.val = CN_KVP_VAL;
26403354 111
b47a81dc 112 kvp_msg->kvp_hdr.operation = reg_value;
26403354
S
113 strcpy(version, HV_DRV_VERSION);
114 msg->len = sizeof(struct hv_kvp_msg);
ac8f7330 115 cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
245ba56a
KS
116 kfree(msg);
117 }
118}
68c8b39a
VK
119
120static void kvp_timeout_func(struct work_struct *dummy)
245ba56a
KS
121{
122 /*
123 * If the timer fires, the user-mode component has not responded;
124 * process the pending transaction.
125 */
03db7724 126 kvp_respond_to_host(NULL, HV_E_FAIL);
97bf16cd
VK
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);
b47a81dc
S
134}
135
136static 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);
97bf16cd 162 kvp_transaction.state = HVUTIL_READY;
b47a81dc
S
163 }
164 return ret;
245ba56a
KS
165}
166
b47a81dc 167
245ba56a
KS
168/*
169 * Callback when data is received from user mode.
170 */
171
172static void
173kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
174{
26403354
S
175 struct hv_kvp_msg *message;
176 struct hv_kvp_msg_enumerate *data;
b47a81dc 177 int error = 0;
245ba56a 178
26403354 179 message = (struct hv_kvp_msg *)msg->data;
b47a81dc
S
180
181 /*
182 * If we are negotiating the version information
183 * with the daemon; handle that first.
184 */
185
97bf16cd
VK
186 if (kvp_transaction.state < HVUTIL_READY) {
187 kvp_handle_handshake(message);
b47a81dc
S
188 return;
189 }
190
97bf16cd
VK
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
b47a81dc
S
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) {
fa3d5b85 204 case KVP_OP_REGISTER:
b47a81dc
S
205 /*
206 * Null string is used to pass back error condition.
207 */
208 if (data->data.key[0] == 0)
209 error = HV_S_CONT;
fa3d5b85 210 break;
245ba56a 211
b47a81dc 212 case KVP_OP_REGISTER1:
245ba56a 213 /*
b47a81dc
S
214 * We use the message header information from
215 * the user level daemon to transmit errors.
245ba56a 216 */
b47a81dc
S
217 error = message->error;
218 break;
245ba56a 219 }
b47a81dc
S
220
221 /*
222 * Complete the transaction by forwarding the key value
223 * to the host. But first, cancel the timeout.
224 */
97bf16cd 225 if (cancel_delayed_work_sync(&kvp_timeout_work)) {
03db7724 226 kvp_respond_to_host(message, error);
97bf16cd
VK
227 kvp_transaction.state = HVUTIL_READY;
228 hv_poll_channel(kvp_transaction.kvp_context,
229 hv_kvp_onchannelcallback);
230 }
245ba56a
KS
231}
232
03db7724
S
233
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;
a500e0e7
S
287 out->kvp_ip_val.addr_family =
288 in->body.kvp_ip_val.addr_family;
03db7724
S
289 }
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
e2bb6537
S
344static void
345kvp_send_key(struct work_struct *dummy)
245ba56a
KS
346{
347 struct cn_msg *msg;
26403354 348 struct hv_kvp_msg *message;
fa3d5b85
S
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;
8d9560eb 354 int rc;
245ba56a 355
97bf16cd
VK
356 /* The transaction state is wrong. */
357 if (kvp_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
358 return;
359
245ba56a 360 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
fa3d5b85
S
361 if (!msg)
362 return;
245ba56a 363
fa3d5b85
S
364 msg->id.idx = CN_KVP_IDX;
365 msg->id.val = CN_KVP_VAL;
26403354 366
fa3d5b85
S
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) {
03db7724
S
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;
fa3d5b85
S
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;
245ba56a 452 }
fa3d5b85
S
453
454 msg->len = sizeof(struct hv_kvp_msg);
97bf16cd 455 kvp_transaction.state = HVUTIL_USERSPACE_REQ;
8d9560eb
VK
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);
97bf16cd 459 if (cancel_delayed_work_sync(&kvp_timeout_work)) {
8d9560eb 460 kvp_respond_to_host(message, HV_E_FAIL);
97bf16cd
VK
461 kvp_transaction.state = HVUTIL_READY;
462 }
8d9560eb
VK
463 }
464
fa3d5b85
S
465 kfree(msg);
466
e2bb6537 467 return;
245ba56a
KS
468}
469
470/*
471 * Send a response back to the host.
472 */
473
474static void
03db7724 475kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
245ba56a
KS
476{
477 struct hv_kvp_msg *kvp_msg;
fa3d5b85 478 struct hv_kvp_exchg_msg_value *kvp_data;
245ba56a 479 char *key_name;
03db7724 480 char *value;
245ba56a 481 struct icmsg_hdr *icmsghdrp;
fa3d5b85
S
482 int keylen = 0;
483 int valuelen = 0;
245ba56a
KS
484 u32 buf_len;
485 struct vmbus_channel *channel;
486 u64 req_id;
03db7724 487 int ret;
245ba56a 488
245ba56a
KS
489 /*
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
fa3d5b85
S
498 icmsghdrp = (struct icmsg_hdr *)
499 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
500
4e65f6e8
S
501 if (channel->onchannel_callback == NULL)
502 /*
503 * We have raced with util driver being unloaded;
504 * silently return.
505 */
506 return;
507
b47a81dc 508 icmsghdrp->status = error;
245ba56a
KS
509
510 /*
adc80ae6
S
511 * If the error parameter is set, terminate the host's enumeration
512 * on this pool.
245ba56a
KS
513 */
514 if (error) {
515 /*
b47a81dc
S
516 * Something failed or we have timedout;
517 * terminate the current host-side iteration.
245ba56a 518 */
245ba56a
KS
519 goto response_done;
520 }
521
fa3d5b85
S
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) {
03db7724
S
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;
fa3d5b85
S
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;
03db7724 550 key_name = msg_to_host->body.kvp_enum_data.data.key;
fa3d5b85 551
245ba56a
KS
552 /*
553 * The windows host expects the key/value pair to be encoded
fa3d5b85
S
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).
245ba56a 557 */
0720a06a 558 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
fa3d5b85
S
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 */
562
563copy_value:
03db7724 564 value = msg_to_host->body.kvp_enum_data.data.value;
0720a06a 565 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
fa3d5b85
S
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 */
245ba56a 569
fa3d5b85
S
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 */
245ba56a
KS
578
579response_done:
580 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
581
582 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
415f2287 583 VM_PKT_DATA_INBAND, 0);
245ba56a
KS
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
596void 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;
245ba56a
KS
603
604 struct icmsg_hdr *icmsghdrp;
605 struct icmsg_negotiate *negop = NULL;
3a491605
S
606 int util_fw_version;
607 int kvp_srv_version;
245ba56a 608
97bf16cd 609 if (kvp_transaction.state > HVUTIL_READY) {
fa3d5b85
S
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 }
5fa97480 617 kvp_transaction.kvp_context = NULL;
245ba56a 618
9bd2d0df 619 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
03db7724 620 &requestid);
245ba56a
KS
621
622 if (recvlen > 0) {
245ba56a
KS
623 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
624 sizeof(struct vmbuspipe_hdr)];
625
626 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
6741335b 627 /*
3a491605
S
628 * Based on the host, select appropriate
629 * framework and service versions we will
630 * negotiate.
6741335b 631 */
3a491605
S
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 }
c836d0ab 645 vmbus_prep_negotiate_resp(icmsghdrp, negop,
3a491605
S
646 recv_buffer, util_fw_version,
647 kvp_srv_version);
6741335b 648
245ba56a
KS
649 } else {
650 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
651 sizeof(struct vmbuspipe_hdr) +
652 sizeof(struct icmsg_hdr)];
653
245ba56a
KS
654 /*
655 * Stash away this global state for completing the
656 * transaction; note transactions are serialized.
657 */
fa3d5b85 658
245ba56a
KS
659 kvp_transaction.recv_len = recvlen;
660 kvp_transaction.recv_channel = channel;
661 kvp_transaction.recv_req_id = requestid;
fa3d5b85 662 kvp_transaction.kvp_msg = kvp_msg;
245ba56a 663
97bf16cd
VK
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
245ba56a
KS
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 */
e2bb6537 680 schedule_work(&kvp_sendkey_work);
68c8b39a 681 schedule_delayed_work(&kvp_timeout_work, 5*HZ);
245ba56a
KS
682
683 return;
684
685 }
686
245ba56a
KS
687 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
688 | ICMSGHDRFLAG_RESPONSE;
689
690 vmbus_sendpacket(channel, recv_buffer,
691 recvlen, requestid,
415f2287 692 VM_PKT_DATA_INBAND, 0);
245ba56a
KS
693 }
694
695}
696
697int
a29b643c 698hv_kvp_init(struct hv_util_service *srv)
245ba56a
KS
699{
700 int err;
701
702 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
703 if (err)
704 return err;
a29b643c 705 recv_buffer = srv->recv_buffer;
245ba56a 706
fa3d5b85
S
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 */
97bf16cd 713 kvp_transaction.state = HVUTIL_DEVICE_INIT;
fa3d5b85 714
245ba56a
KS
715 return 0;
716}
717
718void hv_kvp_deinit(void)
719{
97bf16cd 720 kvp_transaction.state = HVUTIL_DEVICE_DYING;
245ba56a 721 cn_del_callback(&kvp_id);
68c8b39a 722 cancel_delayed_work_sync(&kvp_timeout_work);
e2bb6537 723 cancel_work_sync(&kvp_sendkey_work);
245ba56a 724}