xref: /freebsd/sys/dev/hyperv/utilities/hv_kvp.c (revision 325151a3)
1 /*-
2  * Copyright (c) 2014 Microsoft Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  *	Author:	Sainath Varanasi.
29  *	Date:	4/2012
30  *	Email:	bsdic@microsoft.com
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/conf.h>
39 #include <sys/uio.h>
40 #include <sys/bus.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/reboot.h>
45 #include <sys/lock.h>
46 #include <sys/taskqueue.h>
47 #include <sys/sysctl.h>
48 #include <sys/poll.h>
49 #include <sys/proc.h>
50 #include <sys/kthread.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysproto.h>
53 #include <sys/un.h>
54 #include <sys/endian.h>
55 #include <sys/_null.h>
56 #include <sys/signal.h>
57 #include <sys/syslog.h>
58 #include <sys/systm.h>
59 #include <sys/mutex.h>
60 #include <net/if_arp.h>
61 
62 #include <dev/hyperv/include/hyperv.h>
63 #include <dev/hyperv/netvsc/hv_net_vsc.h>
64 
65 #include "unicode.h"
66 #include "hv_kvp.h"
67 
68 /* hv_kvp defines */
69 #define BUFFERSIZE	sizeof(struct hv_kvp_msg)
70 #define KVP_SUCCESS	0
71 #define KVP_ERROR	1
72 #define kvp_hdr		hdr.kvp_hdr
73 
74 /* hv_kvp debug control */
75 static int hv_kvp_log = 0;
76 SYSCTL_INT(_dev, OID_AUTO, hv_kvp_log, CTLFLAG_RW, &hv_kvp_log, 0,
77 	"hv_kvp log");
78 
79 #define	hv_kvp_log_error(...)	do {				\
80 	if (hv_kvp_log > 0)				\
81 		log(LOG_ERR, "hv_kvp: " __VA_ARGS__);	\
82 } while (0)
83 
84 #define	hv_kvp_log_info(...) do {				\
85 	if (hv_kvp_log > 1)				\
86 		log(LOG_INFO, "hv_kvp: " __VA_ARGS__);		\
87 } while (0)
88 
89 /* character device prototypes */
90 static d_open_t		hv_kvp_dev_open;
91 static d_close_t	hv_kvp_dev_close;
92 static d_read_t		hv_kvp_dev_daemon_read;
93 static d_write_t	hv_kvp_dev_daemon_write;
94 static d_poll_t		hv_kvp_dev_daemon_poll;
95 
96 /* hv_kvp prototypes */
97 static int	hv_kvp_req_in_progress(void);
98 static void	hv_kvp_transaction_init(uint32_t, hv_vmbus_channel *, uint64_t, uint8_t *);
99 static void	hv_kvp_send_msg_to_daemon(void);
100 static void	hv_kvp_process_request(void *context);
101 
102 /* hv_kvp character device structure */
103 static struct cdevsw hv_kvp_cdevsw =
104 {
105 	.d_version	= D_VERSION,
106 	.d_open		= hv_kvp_dev_open,
107 	.d_close	= hv_kvp_dev_close,
108 	.d_read		= hv_kvp_dev_daemon_read,
109 	.d_write	= hv_kvp_dev_daemon_write,
110 	.d_poll		= hv_kvp_dev_daemon_poll,
111 	.d_name		= "hv_kvp_dev",
112 };
113 static struct cdev *hv_kvp_dev;
114 static struct hv_kvp_msg *hv_kvp_dev_buf;
115 struct proc *daemon_task;
116 
117 /*
118  * Global state to track and synchronize multiple
119  * KVP transaction requests from the host.
120  */
121 static struct {
122 
123 	/* Pre-allocated work item for queue */
124 	hv_work_item		work_item;
125 
126 	/* Unless specified the pending mutex should be
127 	 * used to alter the values of the following paramters:
128 	 * 1. req_in_progress
129 	 * 2. req_timed_out
130 	 * 3. pending_reqs.
131 	 */
132 	struct mtx		pending_mutex;
133 
134 	/* To track if transaction is active or not */
135 	boolean_t		req_in_progress;
136 	/* Tracks if daemon did not reply back in time */
137 	boolean_t		req_timed_out;
138 	/* Tracks if daemon is serving a request currently */
139 	boolean_t		daemon_busy;
140 	/* Count of KVP requests from Hyper-V. */
141 	uint64_t		pending_reqs;
142 
143 
144 	/* Length of host message */
145 	uint32_t		host_msg_len;
146 
147 	/* Pointer to channel */
148 	hv_vmbus_channel	*channelp;
149 
150 	/* Host message id */
151 	uint64_t		host_msg_id;
152 
153 	/* Current kvp message from the host */
154 	struct hv_kvp_msg	*host_kvp_msg;
155 
156 	 /* Current kvp message for daemon */
157 	struct hv_kvp_msg	daemon_kvp_msg;
158 
159 	/* Rcv buffer for communicating with the host*/
160 	uint8_t			*rcv_buf;
161 
162 	/* Device semaphore to control communication */
163 	struct sema		dev_sema;
164 
165 	/* Indicates if daemon registered with driver */
166 	boolean_t		register_done;
167 
168 	/* Character device status */
169 	boolean_t		dev_accessed;
170 } kvp_globals;
171 
172 /* global vars */
173 MALLOC_DECLARE(M_HV_KVP_DEV_BUF);
174 MALLOC_DEFINE(M_HV_KVP_DEV_BUF, "hv_kvp_dev buffer", "buffer for hv_kvp_dev module");
175 
176 /*
177  * hv_kvp low level functions
178  */
179 
180 /*
181  * Check if kvp transaction is in progres
182  */
183 static int
184 hv_kvp_req_in_progress(void)
185 {
186 
187 	return (kvp_globals.req_in_progress);
188 }
189 
190 
191 /*
192  * This routine is called whenever a message is received from the host
193  */
194 static void
195 hv_kvp_transaction_init(uint32_t rcv_len, hv_vmbus_channel *rcv_channel,
196 			uint64_t request_id, uint8_t *rcv_buf)
197 {
198 
199 	/* Store all the relevant message details in the global structure */
200 	/* Do not need to use mutex for req_in_progress here */
201 	kvp_globals.req_in_progress = true;
202 	kvp_globals.host_msg_len = rcv_len;
203 	kvp_globals.channelp = rcv_channel;
204 	kvp_globals.host_msg_id = request_id;
205 	kvp_globals.rcv_buf = rcv_buf;
206 	kvp_globals.host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
207 		sizeof(struct hv_vmbus_pipe_hdr) +
208 		sizeof(struct hv_vmbus_icmsg_hdr)];
209 }
210 
211 
212 /*
213  * hv_kvp - version neogtiation function
214  */
215 static void
216 hv_kvp_negotiate_version(struct hv_vmbus_icmsg_hdr *icmsghdrp,
217 			 struct hv_vmbus_icmsg_negotiate *negop,
218 			 uint8_t *buf)
219 {
220 	int icframe_vercnt;
221 	int icmsg_vercnt;
222 
223 	icmsghdrp->icmsgsize = 0x10;
224 
225 	negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
226 		sizeof(struct hv_vmbus_pipe_hdr) +
227 		sizeof(struct hv_vmbus_icmsg_hdr)];
228 	icframe_vercnt = negop->icframe_vercnt;
229 	icmsg_vercnt = negop->icmsg_vercnt;
230 
231 	/*
232 	 * Select the framework version number we will support
233 	 */
234 	if ((icframe_vercnt >= 2) && (negop->icversion_data[1].major == 3)) {
235 		icframe_vercnt = 3;
236 		if (icmsg_vercnt > 2)
237 			icmsg_vercnt = 4;
238 		else
239 			icmsg_vercnt = 3;
240 	} else {
241 		icframe_vercnt = 1;
242 		icmsg_vercnt = 1;
243 	}
244 
245 	negop->icframe_vercnt = 1;
246 	negop->icmsg_vercnt = 1;
247 	negop->icversion_data[0].major = icframe_vercnt;
248 	negop->icversion_data[0].minor = 0;
249 	negop->icversion_data[1].major = icmsg_vercnt;
250 	negop->icversion_data[1].minor = 0;
251 }
252 
253 
254 /*
255  * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
256  */
257 static int
258 hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
259 				    struct hv_kvp_ip_msg *host_ip_msg)
260 {
261 	int err_ip, err_subnet, err_gway, err_dns, err_adap;
262 	int UNUSED_FLAG = 1;
263 
264 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
265 	    MAX_IP_ADDR_SIZE,
266 	    (char *)umsg->body.kvp_ip_val.ip_addr,
267 	    strlen((char *)umsg->body.kvp_ip_val.ip_addr),
268 	    UNUSED_FLAG,
269 	    &err_ip);
270 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
271 	    MAX_IP_ADDR_SIZE,
272 	    (char *)umsg->body.kvp_ip_val.sub_net,
273 	    strlen((char *)umsg->body.kvp_ip_val.sub_net),
274 	    UNUSED_FLAG,
275 	    &err_subnet);
276 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
277 	    MAX_GATEWAY_SIZE,
278 	    (char *)umsg->body.kvp_ip_val.gate_way,
279 	    strlen((char *)umsg->body.kvp_ip_val.gate_way),
280 	    UNUSED_FLAG,
281 	    &err_gway);
282 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
283 	    MAX_IP_ADDR_SIZE,
284 	    (char *)umsg->body.kvp_ip_val.dns_addr,
285 	    strlen((char *)umsg->body.kvp_ip_val.dns_addr),
286 	    UNUSED_FLAG,
287 	    &err_dns);
288 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
289 	    MAX_IP_ADDR_SIZE,
290 	    (char *)umsg->body.kvp_ip_val.adapter_id,
291 	    strlen((char *)umsg->body.kvp_ip_val.adapter_id),
292 	    UNUSED_FLAG,
293 	    &err_adap);
294 
295 	host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
296 	host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
297 
298 	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
299 }
300 
301 
302 /*
303  * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
304  */
305 static int
306 hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
307 				    struct hv_kvp_msg *umsg)
308 {
309 	int err_ip, err_subnet, err_gway, err_dns, err_adap;
310 	int UNUSED_FLAG = 1;
311 	int guid_index;
312 	struct hv_device *hv_dev;       /* GUID Data Structure */
313 	hn_softc_t *sc;                 /* hn softc structure  */
314 	char if_name[4];
315 	unsigned char guid_instance[40];
316 	char *guid_data = NULL;
317 	char buf[39];
318 
319 	struct guid_extract {
320 		char	a1[2];
321 		char	a2[2];
322 		char	a3[2];
323 		char	a4[2];
324 		char	b1[2];
325 		char	b2[2];
326 		char	c1[2];
327 		char	c2[2];
328 		char	d[4];
329 		char	e[12];
330 	};
331 
332 	struct guid_extract *id;
333 	device_t *devs;
334 	int devcnt;
335 
336 	/* IP Address */
337 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
338 	    MAX_IP_ADDR_SIZE,
339 	    (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
340 	    MAX_IP_ADDR_SIZE,
341 	    UNUSED_FLAG,
342 	    &err_ip);
343 
344 	/* Adapter ID : GUID */
345 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
346 	    MAX_ADAPTER_ID_SIZE,
347 	    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
348 	    MAX_ADAPTER_ID_SIZE,
349 	    UNUSED_FLAG,
350 	    &err_adap);
351 
352 	if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
353 		for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
354 			sc = device_get_softc(devs[devcnt]);
355 
356 			/* Trying to find GUID of Network Device */
357 			hv_dev = sc->hn_dev_obj;
358 
359 			for (guid_index = 0; guid_index < 16; guid_index++) {
360 				sprintf(&guid_instance[guid_index * 2], "%02x",
361 				    hv_dev->device_id.data[guid_index]);
362 			}
363 
364 			guid_data = (char *)guid_instance;
365 			id = (struct guid_extract *)guid_data;
366 			snprintf(buf, sizeof(buf), "{%.2s%.2s%.2s%.2s-%.2s%.2s-%.2s%.2s-%.4s-%s}",
367 			    id->a4, id->a3, id->a2, id->a1,
368 			    id->b2, id->b1, id->c2, id->c1, id->d, id->e);
369 			guid_data = NULL;
370 			sprintf(if_name, "%s%d", "hn", device_get_unit(devs[devcnt]));
371 
372 			if (strncmp(buf, (char *)umsg->body.kvp_ip_val.adapter_id, 39) == 0) {
373 				strcpy((char *)umsg->body.kvp_ip_val.adapter_id, if_name);
374 				break;
375 			}
376 		}
377 		free(devs, M_TEMP);
378 	}
379 
380 	/* Address Family , DHCP , SUBNET, Gateway, DNS */
381 	umsg->kvp_hdr.operation = host_ip_msg->operation;
382 	umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
383 	umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
384 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
385 	    (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
386 	    MAX_IP_ADDR_SIZE,
387 	    UNUSED_FLAG,
388 	    &err_subnet);
389 
390 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
391 	    (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
392 	    MAX_GATEWAY_SIZE,
393 	    UNUSED_FLAG,
394 	    &err_gway);
395 
396 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
397 	    (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
398 	    MAX_IP_ADDR_SIZE,
399 	    UNUSED_FLAG,
400 	    &err_dns);
401 
402 	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
403 }
404 
405 
406 /*
407  * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
408  * Ensure utf16_utf8 takes care of the additional string terminating char!!
409  */
410 static void
411 hv_kvp_convert_hostmsg_to_usermsg(void)
412 {
413 	int utf_err = 0;
414 	uint32_t value_type;
415 	struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)
416 		kvp_globals.host_kvp_msg;
417 
418 	struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
419 	struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
420 
421 	memset(umsg, 0, sizeof(struct hv_kvp_msg));
422 
423 	umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
424 	umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
425 
426 	switch (umsg->kvp_hdr.operation) {
427 	case HV_KVP_OP_SET_IP_INFO:
428 		hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
429 		break;
430 
431 	case HV_KVP_OP_GET_IP_INFO:
432 		utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
433 		    MAX_ADAPTER_ID_SIZE,
434 		    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
435 		    MAX_ADAPTER_ID_SIZE, 1, &utf_err);
436 
437 		umsg->body.kvp_ip_val.addr_family =
438 		    host_ip_msg->kvp_ip_val.addr_family;
439 		break;
440 
441 	case HV_KVP_OP_SET:
442 		value_type = hmsg->body.kvp_set.data.value_type;
443 
444 		switch (value_type) {
445 		case HV_REG_SZ:
446 			umsg->body.kvp_set.data.value_size =
447 			    utf16_to_utf8(
448 				(char *)umsg->body.kvp_set.data.msg_value.value,
449 				HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
450 				(uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
451 				hmsg->body.kvp_set.data.value_size,
452 				1, &utf_err);
453 			/* utf8 encoding */
454 			umsg->body.kvp_set.data.value_size =
455 			    umsg->body.kvp_set.data.value_size / 2;
456 			break;
457 
458 		case HV_REG_U32:
459 			umsg->body.kvp_set.data.value_size =
460 			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
461 				hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
462 			break;
463 
464 		case HV_REG_U64:
465 			umsg->body.kvp_set.data.value_size =
466 			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
467 				(unsigned long long)
468 				hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
469 			break;
470 		}
471 
472 		umsg->body.kvp_set.data.key_size =
473 		    utf16_to_utf8(
474 			umsg->body.kvp_set.data.key,
475 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
476 			(uint16_t *)hmsg->body.kvp_set.data.key,
477 			hmsg->body.kvp_set.data.key_size,
478 			1, &utf_err);
479 
480 		/* utf8 encoding */
481 		umsg->body.kvp_set.data.key_size =
482 		    umsg->body.kvp_set.data.key_size / 2;
483 		break;
484 
485 	case HV_KVP_OP_GET:
486 		umsg->body.kvp_get.data.key_size =
487 		    utf16_to_utf8(umsg->body.kvp_get.data.key,
488 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
489 			(uint16_t *)hmsg->body.kvp_get.data.key,
490 			hmsg->body.kvp_get.data.key_size,
491 			1, &utf_err);
492 		/* utf8 encoding */
493 		umsg->body.kvp_get.data.key_size =
494 		    umsg->body.kvp_get.data.key_size / 2;
495 		break;
496 
497 	case HV_KVP_OP_DELETE:
498 		umsg->body.kvp_delete.key_size =
499 		    utf16_to_utf8(umsg->body.kvp_delete.key,
500 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
501 			(uint16_t *)hmsg->body.kvp_delete.key,
502 			hmsg->body.kvp_delete.key_size,
503 			1, &utf_err);
504 		/* utf8 encoding */
505 		umsg->body.kvp_delete.key_size =
506 		    umsg->body.kvp_delete.key_size / 2;
507 		break;
508 
509 	case HV_KVP_OP_ENUMERATE:
510 		umsg->body.kvp_enum_data.index =
511 		    hmsg->body.kvp_enum_data.index;
512 		break;
513 
514 	default:
515 		hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
516 		    __func__, umsg->kvp_hdr.operation);
517 	}
518 }
519 
520 
521 /*
522  * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
523  */
524 static int
525 hv_kvp_convert_usermsg_to_hostmsg(void)
526 {
527 	int hkey_len = 0, hvalue_len = 0, utf_err = 0;
528 	struct hv_kvp_exchg_msg_value *host_exchg_data;
529 	char *key_name, *value;
530 
531 	struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
532 	struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
533 	struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
534 
535 	switch (hmsg->kvp_hdr.operation) {
536 	case HV_KVP_OP_GET_IP_INFO:
537 		return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
538 
539 	case HV_KVP_OP_SET_IP_INFO:
540 	case HV_KVP_OP_SET:
541 	case HV_KVP_OP_DELETE:
542 		return (KVP_SUCCESS);
543 
544 	case HV_KVP_OP_ENUMERATE:
545 		host_exchg_data = &hmsg->body.kvp_enum_data.data;
546 		key_name = umsg->body.kvp_enum_data.data.key;
547 		hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
548 				((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
549 				key_name, strlen(key_name),
550 				1, &utf_err);
551 		/* utf16 encoding */
552 		host_exchg_data->key_size = 2 * (hkey_len + 1);
553 		value = umsg->body.kvp_enum_data.data.msg_value.value;
554 		hvalue_len = utf8_to_utf16(
555 				(uint16_t *)host_exchg_data->msg_value.value,
556 				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
557 				value, strlen(value),
558 				1, &utf_err);
559 		host_exchg_data->value_size = 2 * (hvalue_len + 1);
560 		host_exchg_data->value_type = HV_REG_SZ;
561 
562 		if ((hkey_len < 0) || (hvalue_len < 0))
563 			return (HV_KVP_E_FAIL);
564 
565 		return (KVP_SUCCESS);
566 
567 	case HV_KVP_OP_GET:
568 		host_exchg_data = &hmsg->body.kvp_get.data;
569 		value = umsg->body.kvp_get.data.msg_value.value;
570 		hvalue_len = utf8_to_utf16(
571 				(uint16_t *)host_exchg_data->msg_value.value,
572 				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
573 				value, strlen(value),
574 				1, &utf_err);
575 		/* Convert value size to uft16 */
576 		host_exchg_data->value_size = 2 * (hvalue_len + 1);
577 		/* Use values by string */
578 		host_exchg_data->value_type = HV_REG_SZ;
579 
580 		if ((hkey_len < 0) || (hvalue_len < 0))
581 			return (HV_KVP_E_FAIL);
582 
583 		return (KVP_SUCCESS);
584 
585 	default:
586 		return (HV_KVP_E_FAIL);
587 	}
588 }
589 
590 
591 /*
592  * Send the response back to the host.
593  */
594 static void
595 hv_kvp_respond_host(int error)
596 {
597 	struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
598 
599 	hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
600 	    &kvp_globals.rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
601 
602 	if (error)
603 		error = HV_KVP_E_FAIL;
604 
605 	hv_icmsg_hdrp->status = error;
606 	hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION | HV_ICMSGHDRFLAG_RESPONSE;
607 
608 	error = hv_vmbus_channel_send_packet(kvp_globals.channelp,
609 			kvp_globals.rcv_buf,
610 			kvp_globals.host_msg_len, kvp_globals.host_msg_id,
611 			HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
612 
613 	if (error)
614 		hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
615 			__func__, error);
616 }
617 
618 
619 /*
620  * This is the main kvp kernel process that interacts with both user daemon
621  * and the host
622  */
623 static void
624 hv_kvp_send_msg_to_daemon(void)
625 {
626 	/* Prepare kvp_msg to be sent to user */
627 	hv_kvp_convert_hostmsg_to_usermsg();
628 
629 	/* Send the msg to user via function deamon_read - setting sema */
630 	sema_post(&kvp_globals.dev_sema);
631 }
632 
633 
634 /*
635  * Function to read the kvp request buffer from host
636  * and interact with daemon
637  */
638 static void
639 hv_kvp_process_request(void *context)
640 {
641 	uint8_t *kvp_buf;
642 	hv_vmbus_channel *channel = context;
643 	uint32_t recvlen = 0;
644 	uint64_t requestid;
645 	struct hv_vmbus_icmsg_hdr *icmsghdrp;
646 	int ret = 0;
647 	uint64_t pending_cnt = 1;
648 
649 	hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
650 	kvp_buf = receive_buffer[HV_KVP];
651 	ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
652 		&recvlen, &requestid);
653 
654 	/*
655 	 * We start counting only after the daemon registers
656 	 * and therefore there could be requests pending in
657 	 * the VMBus that are not reflected in pending_cnt.
658 	 * Therefore we continue reading as long as either of
659 	 * the below conditions is true.
660 	 */
661 
662 	while ((pending_cnt>0) || ((ret == 0) && (recvlen > 0))) {
663 
664 		if ((ret == 0) && (recvlen>0)) {
665 
666 			icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
667 					&kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
668 
669 			hv_kvp_transaction_init(recvlen, channel, requestid, kvp_buf);
670 			if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
671 				hv_kvp_negotiate_version(icmsghdrp, NULL, kvp_buf);
672 				hv_kvp_respond_host(ret);
673 
674 				/*
675 				 * It is ok to not acquire the mutex before setting
676 				 * req_in_progress here because negotiation is the
677 				 * first thing that happens and hence there is no
678 				 * chance of a race condition.
679 				 */
680 
681 				kvp_globals.req_in_progress = false;
682 				hv_kvp_log_info("%s :version negotiated\n", __func__);
683 
684 			} else {
685 				if (!kvp_globals.daemon_busy) {
686 
687 					hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
688 					mtx_lock(&kvp_globals.pending_mutex);
689 					kvp_globals.req_timed_out = false;
690 					kvp_globals.daemon_busy = true;
691 					mtx_unlock(&kvp_globals.pending_mutex);
692 
693 					hv_kvp_send_msg_to_daemon();
694 					hv_kvp_log_info("%s: waiting for daemon\n", __func__);
695 				}
696 
697 				/* Wait 5 seconds for daemon to respond back */
698 				tsleep(&kvp_globals, 0, "kvpworkitem", 5 * hz);
699 				hv_kvp_log_info("%s: came out of wait\n", __func__);
700 			}
701 		}
702 
703 		mtx_lock(&kvp_globals.pending_mutex);
704 
705 		/* Notice that once req_timed_out is set to true
706 		 * it will remain true until the next request is
707 		 * sent to the daemon. The response from daemon
708 		 * is forwarded to host only when this flag is
709 		 * false.
710 		 */
711 		kvp_globals.req_timed_out = true;
712 
713 		/*
714 		 * Cancel request if so need be.
715 		 */
716 		if (hv_kvp_req_in_progress()) {
717 			hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
718 			hv_kvp_respond_host(HV_KVP_E_FAIL);
719 			kvp_globals.req_in_progress = false;
720 		}
721 
722 		/*
723 		* Decrement pending request count and
724 		*/
725 		if (kvp_globals.pending_reqs>0) {
726 			kvp_globals.pending_reqs = kvp_globals.pending_reqs - 1;
727 		}
728 		pending_cnt = kvp_globals.pending_reqs;
729 
730 		mtx_unlock(&kvp_globals.pending_mutex);
731 
732 		/*
733 		 * Try reading next buffer
734 		 */
735 		recvlen = 0;
736 		ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
737 			&recvlen, &requestid);
738 		hv_kvp_log_info("%s: read: context %p, pending_cnt %llu ret =%d, recvlen=%d\n",
739 			__func__, context, (unsigned long long)pending_cnt, ret, recvlen);
740 	}
741 }
742 
743 
744 /*
745  * Callback routine that gets called whenever there is a message from host
746  */
747 void
748 hv_kvp_callback(void *context)
749 {
750 	uint64_t pending_cnt = 0;
751 
752 	if (kvp_globals.register_done == false) {
753 
754 		kvp_globals.channelp = context;
755 	} else {
756 
757 		mtx_lock(&kvp_globals.pending_mutex);
758 		kvp_globals.pending_reqs = kvp_globals.pending_reqs + 1;
759 		pending_cnt = kvp_globals.pending_reqs;
760 		mtx_unlock(&kvp_globals.pending_mutex);
761 		if (pending_cnt == 1) {
762 			hv_kvp_log_info("%s: Queuing work item\n", __func__);
763 			hv_queue_work_item(
764 					service_table[HV_KVP].work_queue,
765 					hv_kvp_process_request,
766 					context
767 					);
768 		}
769 	}
770 }
771 
772 
773 /*
774  * This function is called by the hv_kvp_init -
775  * creates character device hv_kvp_dev
776  * allocates memory to hv_kvp_dev_buf
777  *
778  */
779 static int
780 hv_kvp_dev_init(void)
781 {
782 	int error = 0;
783 
784 	/* initialize semaphore */
785 	sema_init(&kvp_globals.dev_sema, 0, "hv_kvp device semaphore");
786 	/* create character device */
787 	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
788 			&hv_kvp_dev,
789 			&hv_kvp_cdevsw,
790 			0,
791 			UID_ROOT,
792 			GID_WHEEL,
793 			0640,
794 			"hv_kvp_dev");
795 
796 	if (error != 0)
797 		return (error);
798 
799 	/*
800 	 * Malloc with M_WAITOK flag will never fail.
801 	 */
802 	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_HV_KVP_DEV_BUF, M_WAITOK |
803 				M_ZERO);
804 
805 	return (0);
806 }
807 
808 
809 /*
810  * This function is called by the hv_kvp_deinit -
811  * destroy character device
812  */
813 static void
814 hv_kvp_dev_destroy(void)
815 {
816 
817 	if (daemon_task != NULL) {
818 		PROC_LOCK(daemon_task);
819 		kern_psignal(daemon_task, SIGKILL);
820 		PROC_UNLOCK(daemon_task);
821 	}
822 
823 	destroy_dev(hv_kvp_dev);
824 	free(hv_kvp_dev_buf, M_HV_KVP_DEV_BUF);
825 	return;
826 }
827 
828 
829 static int
830 hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
831 				struct thread *td)
832 {
833 
834 	hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
835 	if (kvp_globals.dev_accessed)
836 		return (-EBUSY);
837 
838 	daemon_task = curproc;
839 	kvp_globals.dev_accessed = true;
840 	kvp_globals.daemon_busy = false;
841 	return (0);
842 }
843 
844 
845 static int
846 hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
847 				 struct thread *td __unused)
848 {
849 
850 	hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
851 	kvp_globals.dev_accessed = false;
852 	kvp_globals.register_done = false;
853 	return (0);
854 }
855 
856 
857 /*
858  * hv_kvp_daemon read invokes this function
859  * acts as a send to daemon
860  */
861 static int
862 hv_kvp_dev_daemon_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
863 {
864 	size_t amt;
865 	int error = 0;
866 
867 	/* Check hv_kvp daemon registration status*/
868 	if (!kvp_globals.register_done)
869 		return (KVP_ERROR);
870 
871 	sema_wait(&kvp_globals.dev_sema);
872 
873 	memcpy(hv_kvp_dev_buf, &kvp_globals.daemon_kvp_msg, sizeof(struct hv_kvp_msg));
874 
875 	amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
876 		BUFFERSIZE + 1 - uio->uio_offset);
877 
878 	if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
879 		hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
880 
881 	return (error);
882 }
883 
884 
885 /*
886  * hv_kvp_daemon write invokes this function
887  * acts as a recieve from daemon
888  */
889 static int
890 hv_kvp_dev_daemon_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
891 {
892 	size_t amt;
893 	int error = 0;
894 
895 	uio->uio_offset = 0;
896 
897 	amt = MIN(uio->uio_resid, BUFFERSIZE);
898 	error = uiomove(hv_kvp_dev_buf, amt, uio);
899 
900 	if (error != 0)
901 		return (error);
902 
903 	memcpy(&kvp_globals.daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
904 
905 	if (kvp_globals.register_done == false) {
906 		if (kvp_globals.daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
907 
908 			kvp_globals.register_done = true;
909 			if (kvp_globals.channelp) {
910 
911 				hv_kvp_callback(kvp_globals.channelp);
912 			}
913 		}
914 		else {
915 			hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
916 			return (KVP_ERROR);
917 		}
918 	} else {
919 
920 		mtx_lock(&kvp_globals.pending_mutex);
921 
922 		if(!kvp_globals.req_timed_out) {
923 
924 			hv_kvp_convert_usermsg_to_hostmsg();
925 			hv_kvp_respond_host(KVP_SUCCESS);
926 			wakeup(&kvp_globals);
927 			kvp_globals.req_in_progress = false;
928 		}
929 
930 		kvp_globals.daemon_busy = false;
931 		mtx_unlock(&kvp_globals.pending_mutex);
932 	}
933 
934 	return (error);
935 }
936 
937 
938 /*
939  * hv_kvp_daemon poll invokes this function to check if data is available
940  * for daemon to read.
941  */
942 static int
943 hv_kvp_dev_daemon_poll(struct cdev *dev __unused, int events, struct thread *td  __unused)
944 {
945 	int revents = 0;
946 
947 	mtx_lock(&kvp_globals.pending_mutex);
948 	/*
949 	 * We check global flag daemon_busy for the data availiability for
950 	 * userland to read. Deamon_busy is set to true before driver has data
951 	 * for daemon to read. It is set to false after daemon sends
952 	 * then response back to driver.
953 	 */
954 	if (kvp_globals.daemon_busy == true)
955 		revents = POLLIN;
956 	mtx_unlock(&kvp_globals.pending_mutex);
957 
958 	return (revents);
959 }
960 
961 
962 /*
963  * hv_kvp initialization function
964  * called from hv_util service.
965  *
966  */
967 int
968 hv_kvp_init(hv_vmbus_service *srv)
969 {
970 	int error = 0;
971 	hv_work_queue *work_queue = NULL;
972 
973 	memset(&kvp_globals, 0, sizeof(kvp_globals));
974 
975 	work_queue = hv_work_queue_create("KVP Service");
976 	if (work_queue == NULL) {
977 		hv_kvp_log_info("%s: Work queue alloc failed\n", __func__);
978 		error = ENOMEM;
979 		hv_kvp_log_error("%s: ENOMEM\n", __func__);
980 		goto Finish;
981 	}
982 	srv->work_queue = work_queue;
983 
984 	error = hv_kvp_dev_init();
985 	mtx_init(&kvp_globals.pending_mutex, "hv-kvp pending mutex",
986 		       	NULL, MTX_DEF);
987 	kvp_globals.pending_reqs = 0;
988 
989 
990 Finish:
991 	return (error);
992 }
993 
994 
995 void
996 hv_kvp_deinit(void)
997 {
998 	hv_kvp_dev_destroy();
999 	mtx_destroy(&kvp_globals.pending_mutex);
1000 
1001 	return;
1002 }
1003