xref: /freebsd/sys/dev/hyperv/utilities/hv_kvp.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2014,2016-2017 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 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/conf.h>
37 #include <sys/uio.h>
38 #include <sys/bus.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/reboot.h>
43 #include <sys/lock.h>
44 #include <sys/taskqueue.h>
45 #include <sys/selinfo.h>
46 #include <sys/sysctl.h>
47 #include <sys/poll.h>
48 #include <sys/proc.h>
49 #include <sys/kthread.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/sysproto.h>
52 #include <sys/un.h>
53 #include <sys/endian.h>
54 #include <sys/_null.h>
55 #include <sys/sema.h>
56 #include <sys/signal.h>
57 #include <sys/syslog.h>
58 #include <sys/systm.h>
59 #include <sys/mutex.h>
60 
61 #include <dev/hyperv/include/hyperv.h>
62 #include <dev/hyperv/include/vmbus.h>
63 #include <dev/hyperv/utilities/hv_utilreg.h>
64 #include <dev/hyperv/utilities/vmbus_icreg.h>
65 #include <dev/hyperv/utilities/vmbus_icvar.h>
66 
67 #include "unicode.h"
68 #include "hv_kvp.h"
69 #include "vmbus_if.h"
70 
71 /* hv_kvp defines */
72 #define BUFFERSIZE	sizeof(struct hv_kvp_msg)
73 #define kvp_hdr		hdr.kvp_hdr
74 
75 #define KVP_FWVER_MAJOR		3
76 #define KVP_FWVER		VMBUS_IC_VERSION(KVP_FWVER_MAJOR, 0)
77 
78 #define KVP_MSGVER_MAJOR	4
79 #define KVP_MSGVER		VMBUS_IC_VERSION(KVP_MSGVER_MAJOR, 0)
80 
81 /* hv_kvp debug control */
82 static int hv_kvp_log = 0;
83 
84 #define	hv_kvp_log_error(...)	do {				\
85 	if (hv_kvp_log > 0)				\
86 		log(LOG_ERR, "hv_kvp: " __VA_ARGS__);	\
87 } while (0)
88 
89 #define	hv_kvp_log_info(...) do {				\
90 	if (hv_kvp_log > 1)				\
91 		log(LOG_INFO, "hv_kvp: " __VA_ARGS__);		\
92 } while (0)
93 
94 static const struct vmbus_ic_desc vmbus_kvp_descs[] = {
95 	{
96 		.ic_guid = { .hv_guid = {
97 		    0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
98 		    0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3,  0xe6 } },
99 		.ic_desc = "Hyper-V KVP"
100 	},
101 	VMBUS_IC_DESC_END
102 };
103 
104 /* character device prototypes */
105 static d_open_t		hv_kvp_dev_open;
106 static d_close_t	hv_kvp_dev_close;
107 static d_read_t		hv_kvp_dev_daemon_read;
108 static d_write_t	hv_kvp_dev_daemon_write;
109 static d_poll_t		hv_kvp_dev_daemon_poll;
110 
111 /* hv_kvp character device structure */
112 static struct cdevsw hv_kvp_cdevsw =
113 {
114 	.d_version	= D_VERSION,
115 	.d_open		= hv_kvp_dev_open,
116 	.d_close	= hv_kvp_dev_close,
117 	.d_read		= hv_kvp_dev_daemon_read,
118 	.d_write	= hv_kvp_dev_daemon_write,
119 	.d_poll		= hv_kvp_dev_daemon_poll,
120 	.d_name		= "hv_kvp_dev",
121 };
122 
123 
124 /*
125  * Global state to track and synchronize multiple
126  * KVP transaction requests from the host.
127  */
128 typedef struct hv_kvp_sc {
129 	struct vmbus_ic_softc	util_sc;
130 	device_t		dev;
131 
132 	/* Unless specified the pending mutex should be
133 	 * used to alter the values of the following parameters:
134 	 * 1. req_in_progress
135 	 * 2. req_timed_out
136 	 */
137 	struct mtx		pending_mutex;
138 
139 	struct task		task;
140 
141 	/* To track if transaction is active or not */
142 	boolean_t		req_in_progress;
143 	/* Tracks if daemon did not reply back in time */
144 	boolean_t		req_timed_out;
145 	/* Tracks if daemon is serving a request currently */
146 	boolean_t		daemon_busy;
147 
148 	/* Length of host message */
149 	uint32_t		host_msg_len;
150 
151 	/* Host message id */
152 	uint64_t		host_msg_id;
153 
154 	/* Current kvp message from the host */
155 	struct hv_kvp_msg	*host_kvp_msg;
156 
157 	 /* Current kvp message for daemon */
158 	struct hv_kvp_msg	daemon_kvp_msg;
159 
160 	/* Rcv buffer for communicating with the host*/
161 	uint8_t			*rcv_buf;
162 
163 	/* Device semaphore to control communication */
164 	struct sema		dev_sema;
165 
166 	/* Indicates if daemon registered with driver */
167 	boolean_t		register_done;
168 
169 	/* Character device status */
170 	boolean_t		dev_accessed;
171 
172 	struct cdev *hv_kvp_dev;
173 
174 	struct proc *daemon_task;
175 
176 	struct selinfo hv_kvp_selinfo;
177 } hv_kvp_sc;
178 
179 /* hv_kvp prototypes */
180 static int	hv_kvp_req_in_progress(hv_kvp_sc *sc);
181 static void	hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t, uint64_t, uint8_t *);
182 static void	hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc);
183 static void	hv_kvp_process_request(void *context, int pending);
184 
185 /*
186  * hv_kvp low level functions
187  */
188 
189 /*
190  * Check if kvp transaction is in progres
191  */
192 static int
193 hv_kvp_req_in_progress(hv_kvp_sc *sc)
194 {
195 
196 	return (sc->req_in_progress);
197 }
198 
199 
200 /*
201  * This routine is called whenever a message is received from the host
202  */
203 static void
204 hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t rcv_len,
205 			uint64_t request_id, uint8_t *rcv_buf)
206 {
207 
208 	/* Store all the relevant message details in the global structure */
209 	/* Do not need to use mutex for req_in_progress here */
210 	sc->req_in_progress = true;
211 	sc->host_msg_len = rcv_len;
212 	sc->host_msg_id = request_id;
213 	sc->rcv_buf = rcv_buf;
214 	sc->host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
215 	    sizeof(struct hv_vmbus_pipe_hdr) +
216 	    sizeof(struct hv_vmbus_icmsg_hdr)];
217 }
218 
219 /*
220  * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
221  */
222 static int
223 hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
224 				    struct hv_kvp_ip_msg *host_ip_msg)
225 {
226 	int err_ip, err_subnet, err_gway, err_dns, err_adap;
227 	int UNUSED_FLAG = 1;
228 
229 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
230 	    MAX_IP_ADDR_SIZE,
231 	    (char *)umsg->body.kvp_ip_val.ip_addr,
232 	    strlen((char *)umsg->body.kvp_ip_val.ip_addr),
233 	    UNUSED_FLAG,
234 	    &err_ip);
235 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
236 	    MAX_IP_ADDR_SIZE,
237 	    (char *)umsg->body.kvp_ip_val.sub_net,
238 	    strlen((char *)umsg->body.kvp_ip_val.sub_net),
239 	    UNUSED_FLAG,
240 	    &err_subnet);
241 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
242 	    MAX_GATEWAY_SIZE,
243 	    (char *)umsg->body.kvp_ip_val.gate_way,
244 	    strlen((char *)umsg->body.kvp_ip_val.gate_way),
245 	    UNUSED_FLAG,
246 	    &err_gway);
247 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
248 	    MAX_IP_ADDR_SIZE,
249 	    (char *)umsg->body.kvp_ip_val.dns_addr,
250 	    strlen((char *)umsg->body.kvp_ip_val.dns_addr),
251 	    UNUSED_FLAG,
252 	    &err_dns);
253 	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
254 	    MAX_ADAPTER_ID_SIZE,
255 	    (char *)umsg->body.kvp_ip_val.adapter_id,
256 	    strlen((char *)umsg->body.kvp_ip_val.adapter_id),
257 	    UNUSED_FLAG,
258 	    &err_adap);
259 
260 	host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
261 	host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
262 
263 	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
264 }
265 
266 
267 /*
268  * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
269  */
270 static int
271 hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
272 				    struct hv_kvp_msg *umsg)
273 {
274 	int err_ip, err_subnet, err_gway, err_dns, err_adap;
275 	int UNUSED_FLAG = 1;
276 	device_t *devs;
277 	int devcnt;
278 
279 	/* IP Address */
280 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
281 	    MAX_IP_ADDR_SIZE,
282 	    (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
283 	    MAX_IP_ADDR_SIZE,
284 	    UNUSED_FLAG,
285 	    &err_ip);
286 
287 	/* Adapter ID : GUID */
288 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
289 	    MAX_ADAPTER_ID_SIZE,
290 	    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
291 	    MAX_ADAPTER_ID_SIZE,
292 	    UNUSED_FLAG,
293 	    &err_adap);
294 
295 	if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
296 		for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
297 			device_t dev = devs[devcnt];
298 			struct vmbus_channel *chan;
299 			char buf[HYPERV_GUID_STRLEN];
300 			int n;
301 
302 			chan = vmbus_get_channel(dev);
303 			n = hyperv_guid2str(vmbus_chan_guid_inst(chan), buf,
304 			    sizeof(buf));
305 
306 			/*
307 			 * The string in the 'kvp_ip_val.adapter_id' has
308 			 * braces around the GUID; skip the leading brace
309 			 * in 'kvp_ip_val.adapter_id'.
310 			 */
311 			if (strncmp(buf,
312 			    ((char *)&umsg->body.kvp_ip_val.adapter_id) + 1,
313 			    n) == 0) {
314 				strlcpy((char *)umsg->body.kvp_ip_val.adapter_id,
315 				    device_get_nameunit(dev), MAX_ADAPTER_ID_SIZE);
316 				break;
317 			}
318 		}
319 		free(devs, M_TEMP);
320 	}
321 
322 	/* Address Family , DHCP , SUBNET, Gateway, DNS */
323 	umsg->kvp_hdr.operation = host_ip_msg->operation;
324 	umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
325 	umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
326 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
327 	    (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
328 	    MAX_IP_ADDR_SIZE,
329 	    UNUSED_FLAG,
330 	    &err_subnet);
331 
332 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
333 	    (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
334 	    MAX_GATEWAY_SIZE,
335 	    UNUSED_FLAG,
336 	    &err_gway);
337 
338 	utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
339 	    (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
340 	    MAX_IP_ADDR_SIZE,
341 	    UNUSED_FLAG,
342 	    &err_dns);
343 
344 	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
345 }
346 
347 
348 /*
349  * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
350  * Ensure utf16_utf8 takes care of the additional string terminating char!!
351  */
352 static void
353 hv_kvp_convert_hostmsg_to_usermsg(struct hv_kvp_msg *hmsg, struct hv_kvp_msg *umsg)
354 {
355 	int utf_err = 0;
356 	uint32_t value_type;
357 	struct hv_kvp_ip_msg *host_ip_msg;
358 
359 	host_ip_msg = (struct hv_kvp_ip_msg*)hmsg;
360 	memset(umsg, 0, sizeof(struct hv_kvp_msg));
361 
362 	umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
363 	umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
364 
365 	switch (umsg->kvp_hdr.operation) {
366 	case HV_KVP_OP_SET_IP_INFO:
367 		hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
368 		break;
369 
370 	case HV_KVP_OP_GET_IP_INFO:
371 		utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
372 		    MAX_ADAPTER_ID_SIZE,
373 		    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
374 		    MAX_ADAPTER_ID_SIZE, 1, &utf_err);
375 
376 		umsg->body.kvp_ip_val.addr_family =
377 		    host_ip_msg->kvp_ip_val.addr_family;
378 		break;
379 
380 	case HV_KVP_OP_SET:
381 		value_type = hmsg->body.kvp_set.data.value_type;
382 
383 		switch (value_type) {
384 		case HV_REG_SZ:
385 			umsg->body.kvp_set.data.value_size =
386 			    utf16_to_utf8(
387 				(char *)umsg->body.kvp_set.data.msg_value.value,
388 				HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
389 				(uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
390 				hmsg->body.kvp_set.data.value_size,
391 				1, &utf_err);
392 			/* utf8 encoding */
393 			umsg->body.kvp_set.data.value_size =
394 			    umsg->body.kvp_set.data.value_size / 2;
395 			break;
396 
397 		case HV_REG_U32:
398 			umsg->body.kvp_set.data.value_size =
399 			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
400 				hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
401 			break;
402 
403 		case HV_REG_U64:
404 			umsg->body.kvp_set.data.value_size =
405 			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
406 				(unsigned long long)
407 				hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
408 			break;
409 		}
410 
411 		umsg->body.kvp_set.data.key_size =
412 		    utf16_to_utf8(
413 			umsg->body.kvp_set.data.key,
414 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
415 			(uint16_t *)hmsg->body.kvp_set.data.key,
416 			hmsg->body.kvp_set.data.key_size,
417 			1, &utf_err);
418 
419 		/* utf8 encoding */
420 		umsg->body.kvp_set.data.key_size =
421 		    umsg->body.kvp_set.data.key_size / 2;
422 		break;
423 
424 	case HV_KVP_OP_GET:
425 		umsg->body.kvp_get.data.key_size =
426 		    utf16_to_utf8(umsg->body.kvp_get.data.key,
427 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
428 			(uint16_t *)hmsg->body.kvp_get.data.key,
429 			hmsg->body.kvp_get.data.key_size,
430 			1, &utf_err);
431 		/* utf8 encoding */
432 		umsg->body.kvp_get.data.key_size =
433 		    umsg->body.kvp_get.data.key_size / 2;
434 		break;
435 
436 	case HV_KVP_OP_DELETE:
437 		umsg->body.kvp_delete.key_size =
438 		    utf16_to_utf8(umsg->body.kvp_delete.key,
439 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
440 			(uint16_t *)hmsg->body.kvp_delete.key,
441 			hmsg->body.kvp_delete.key_size,
442 			1, &utf_err);
443 		/* utf8 encoding */
444 		umsg->body.kvp_delete.key_size =
445 		    umsg->body.kvp_delete.key_size / 2;
446 		break;
447 
448 	case HV_KVP_OP_ENUMERATE:
449 		umsg->body.kvp_enum_data.index =
450 		    hmsg->body.kvp_enum_data.index;
451 		break;
452 
453 	default:
454 		hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
455 		    __func__, umsg->kvp_hdr.operation);
456 	}
457 }
458 
459 
460 /*
461  * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
462  */
463 static int
464 hv_kvp_convert_usermsg_to_hostmsg(struct hv_kvp_msg *umsg, struct hv_kvp_msg *hmsg)
465 {
466 	int hkey_len = 0, hvalue_len = 0, utf_err = 0;
467 	struct hv_kvp_exchg_msg_value *host_exchg_data;
468 	char *key_name, *value;
469 
470 	struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
471 
472 	switch (hmsg->kvp_hdr.operation) {
473 	case HV_KVP_OP_GET_IP_INFO:
474 		return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
475 
476 	case HV_KVP_OP_SET_IP_INFO:
477 	case HV_KVP_OP_SET:
478 	case HV_KVP_OP_DELETE:
479 		return (0);
480 
481 	case HV_KVP_OP_ENUMERATE:
482 		host_exchg_data = &hmsg->body.kvp_enum_data.data;
483 		key_name = umsg->body.kvp_enum_data.data.key;
484 		hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
485 				((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
486 				key_name, strlen(key_name),
487 				1, &utf_err);
488 		/* utf16 encoding */
489 		host_exchg_data->key_size = 2 * (hkey_len + 1);
490 		value = umsg->body.kvp_enum_data.data.msg_value.value;
491 		hvalue_len = utf8_to_utf16(
492 				(uint16_t *)host_exchg_data->msg_value.value,
493 				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
494 				value, strlen(value),
495 				1, &utf_err);
496 		host_exchg_data->value_size = 2 * (hvalue_len + 1);
497 		host_exchg_data->value_type = HV_REG_SZ;
498 
499 		if ((hkey_len < 0) || (hvalue_len < 0))
500 			return (EINVAL);
501 
502 		return (0);
503 
504 	case HV_KVP_OP_GET:
505 		host_exchg_data = &hmsg->body.kvp_get.data;
506 		value = umsg->body.kvp_get.data.msg_value.value;
507 		hvalue_len = utf8_to_utf16(
508 				(uint16_t *)host_exchg_data->msg_value.value,
509 				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
510 				value, strlen(value),
511 				1, &utf_err);
512 		/* Convert value size to uft16 */
513 		host_exchg_data->value_size = 2 * (hvalue_len + 1);
514 		/* Use values by string */
515 		host_exchg_data->value_type = HV_REG_SZ;
516 
517 		if (hvalue_len < 0)
518 			return (EINVAL);
519 
520 		return (0);
521 
522 	default:
523 		return (EINVAL);
524 	}
525 }
526 
527 
528 /*
529  * Send the response back to the host.
530  */
531 static void
532 hv_kvp_respond_host(hv_kvp_sc *sc, uint32_t error)
533 {
534 	struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
535 
536 	hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
537 	    &sc->rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
538 
539 	hv_icmsg_hdrp->status = error;
540 	hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
541 	    HV_ICMSGHDRFLAG_RESPONSE;
542 
543 	error = vmbus_chan_send(vmbus_get_channel(sc->dev),
544 	    VMBUS_CHANPKT_TYPE_INBAND, 0, sc->rcv_buf, sc->host_msg_len,
545 	    sc->host_msg_id);
546 	if (error)
547 		hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
548 			__func__, error);
549 }
550 
551 
552 /*
553  * This is the main kvp kernel process that interacts with both user daemon
554  * and the host
555  */
556 static void
557 hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc)
558 {
559 	struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
560 	struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
561 
562 	/* Prepare kvp_msg to be sent to user */
563 	hv_kvp_convert_hostmsg_to_usermsg(hmsg, umsg);
564 
565 	/* Send the msg to user via function deamon_read - setting sema */
566 	sema_post(&sc->dev_sema);
567 
568 	/* We should wake up the daemon, in case it's doing poll() */
569 	selwakeup(&sc->hv_kvp_selinfo);
570 }
571 
572 
573 /*
574  * Function to read the kvp request buffer from host
575  * and interact with daemon
576  */
577 static void
578 hv_kvp_process_request(void *context, int pending)
579 {
580 	uint8_t *kvp_buf;
581 	struct vmbus_channel *channel;
582 	uint32_t recvlen = 0;
583 	uint64_t requestid;
584 	struct hv_vmbus_icmsg_hdr *icmsghdrp;
585 	int ret = 0, error;
586 	hv_kvp_sc *sc;
587 
588 	hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
589 
590 	sc = (hv_kvp_sc*)context;
591 	kvp_buf = sc->util_sc.ic_buf;
592 	channel = vmbus_get_channel(sc->dev);
593 
594 	recvlen = sc->util_sc.ic_buflen;
595 	ret = vmbus_chan_recv(channel, kvp_buf, &recvlen, &requestid);
596 	KASSERT(ret != ENOBUFS, ("hvkvp recvbuf is not large enough"));
597 	/* XXX check recvlen to make sure that it contains enough data */
598 
599 	while ((ret == 0) && (recvlen > 0)) {
600 		icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
601 		    &kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
602 
603 		hv_kvp_transaction_init(sc, recvlen, requestid, kvp_buf);
604 		if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
605 			error = vmbus_ic_negomsg(&sc->util_sc,
606 			    kvp_buf, &recvlen, KVP_FWVER, KVP_MSGVER);
607 			/* XXX handle vmbus_ic_negomsg failure. */
608 			if (!error)
609 				hv_kvp_respond_host(sc, HV_S_OK);
610 			else
611 				hv_kvp_respond_host(sc, HV_E_FAIL);
612 			/*
613 			 * It is ok to not acquire the mutex before setting
614 			 * req_in_progress here because negotiation is the
615 			 * first thing that happens and hence there is no
616 			 * chance of a race condition.
617 			 */
618 
619 			sc->req_in_progress = false;
620 			hv_kvp_log_info("%s :version negotiated\n", __func__);
621 
622 		} else {
623 			if (!sc->daemon_busy) {
624 
625 				hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
626 				mtx_lock(&sc->pending_mutex);
627 				sc->req_timed_out = false;
628 				sc->daemon_busy = true;
629 				mtx_unlock(&sc->pending_mutex);
630 
631 				hv_kvp_send_msg_to_daemon(sc);
632 				hv_kvp_log_info("%s: waiting for daemon\n", __func__);
633 			}
634 
635 			/* Wait 5 seconds for daemon to respond back */
636 			tsleep(sc, 0, "kvpworkitem", 5 * hz);
637 			hv_kvp_log_info("%s: came out of wait\n", __func__);
638 		}
639 
640 		mtx_lock(&sc->pending_mutex);
641 
642 		/* Notice that once req_timed_out is set to true
643 		 * it will remain true until the next request is
644 		 * sent to the daemon. The response from daemon
645 		 * is forwarded to host only when this flag is
646 		 * false.
647 		 */
648 		sc->req_timed_out = true;
649 
650 		/*
651 		 * Cancel request if so need be.
652 		 */
653 		if (hv_kvp_req_in_progress(sc)) {
654 			hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
655 			hv_kvp_respond_host(sc, HV_E_FAIL);
656 			sc->req_in_progress = false;
657 		}
658 
659 		mtx_unlock(&sc->pending_mutex);
660 
661 		/*
662 		 * Try reading next buffer
663 		 */
664 		recvlen = sc->util_sc.ic_buflen;
665 		ret = vmbus_chan_recv(channel, kvp_buf, &recvlen, &requestid);
666 		KASSERT(ret != ENOBUFS, ("hvkvp recvbuf is not large enough"));
667 		/* XXX check recvlen to make sure that it contains enough data */
668 
669 		hv_kvp_log_info("%s: read: context %p, ret =%d, recvlen=%d\n",
670 			__func__, context, ret, recvlen);
671 	}
672 }
673 
674 
675 /*
676  * Callback routine that gets called whenever there is a message from host
677  */
678 static void
679 hv_kvp_callback(struct vmbus_channel *chan __unused, void *context)
680 {
681 	hv_kvp_sc *sc = (hv_kvp_sc*)context;
682 	/*
683 	 The first request from host will not be handled until daemon is registered.
684 	 when callback is triggered without a registered daemon, callback just return.
685 	 When a new daemon gets regsitered, this callbcak is trigged from _write op.
686 	*/
687 	if (sc->register_done) {
688 		hv_kvp_log_info("%s: Queuing work item\n", __func__);
689 		taskqueue_enqueue(taskqueue_thread, &sc->task);
690 	}
691 }
692 
693 static int
694 hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
695 				struct thread *td)
696 {
697 	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
698 
699 	hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
700 	if (sc->dev_accessed)
701 		return (-EBUSY);
702 
703 	sc->daemon_task = curproc;
704 	sc->dev_accessed = true;
705 	sc->daemon_busy = false;
706 	return (0);
707 }
708 
709 
710 static int
711 hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
712 				 struct thread *td __unused)
713 {
714 	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
715 
716 	hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
717 	sc->dev_accessed = false;
718 	sc->register_done = false;
719 	return (0);
720 }
721 
722 
723 /*
724  * hv_kvp_daemon read invokes this function
725  * acts as a send to daemon
726  */
727 static int
728 hv_kvp_dev_daemon_read(struct cdev *dev, struct uio *uio, int ioflag __unused)
729 {
730 	size_t amt;
731 	int error = 0;
732 	struct hv_kvp_msg *hv_kvp_dev_buf;
733 	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
734 
735 	/* Read is not allowed util registering is done. */
736 	if (!sc->register_done)
737 		return (EPERM);
738 
739 	sema_wait(&sc->dev_sema);
740 
741 	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
742 	memcpy(hv_kvp_dev_buf, &sc->daemon_kvp_msg, sizeof(struct hv_kvp_msg));
743 
744 	amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
745 		BUFFERSIZE + 1 - uio->uio_offset);
746 
747 	if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
748 		hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
749 
750 	free(hv_kvp_dev_buf, M_TEMP);
751 	return (error);
752 }
753 
754 
755 /*
756  * hv_kvp_daemon write invokes this function
757  * acts as a receive from daemon
758  */
759 static int
760 hv_kvp_dev_daemon_write(struct cdev *dev, struct uio *uio, int ioflag __unused)
761 {
762 	size_t amt;
763 	int error = 0;
764 	struct hv_kvp_msg *hv_kvp_dev_buf;
765 	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
766 
767 	uio->uio_offset = 0;
768 	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
769 
770 	amt = MIN(uio->uio_resid, BUFFERSIZE);
771 	error = uiomove(hv_kvp_dev_buf, amt, uio);
772 
773 	if (error != 0) {
774 		free(hv_kvp_dev_buf, M_TEMP);
775 		return (error);
776 	}
777 	memcpy(&sc->daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
778 
779 	free(hv_kvp_dev_buf, M_TEMP);
780 	if (sc->register_done == false) {
781 		if (sc->daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
782 			sc->register_done = true;
783 			hv_kvp_callback(vmbus_get_channel(sc->dev), dev->si_drv1);
784 		}
785 		else {
786 			hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
787 			return (EINVAL);
788 		}
789 	} else {
790 
791 		mtx_lock(&sc->pending_mutex);
792 
793 		if(!sc->req_timed_out) {
794 			struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
795 			struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
796 
797 			error = hv_kvp_convert_usermsg_to_hostmsg(umsg, hmsg);
798 			hv_kvp_respond_host(sc, umsg->hdr.error);
799 			wakeup(sc);
800 			sc->req_in_progress = false;
801 			if (umsg->hdr.error != HV_S_OK)
802 				hv_kvp_log_info("%s, Error 0x%x from daemon\n",
803 				    __func__, umsg->hdr.error);
804 			if (error)
805 				hv_kvp_log_info("%s, Error from convert\n", __func__);
806 		}
807 
808 		sc->daemon_busy = false;
809 		mtx_unlock(&sc->pending_mutex);
810 	}
811 
812 	return (error);
813 }
814 
815 
816 /*
817  * hv_kvp_daemon poll invokes this function to check if data is available
818  * for daemon to read.
819  */
820 static int
821 hv_kvp_dev_daemon_poll(struct cdev *dev, int events, struct thread *td)
822 {
823 	int revents = 0;
824 	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
825 
826 	mtx_lock(&sc->pending_mutex);
827 	/*
828 	 * We check global flag daemon_busy for the data availiability for
829 	 * userland to read. Deamon_busy is set to true before driver has data
830 	 * for daemon to read. It is set to false after daemon sends
831 	 * then response back to driver.
832 	 */
833 	if (sc->daemon_busy == true)
834 		revents = POLLIN;
835 	else
836 		selrecord(td, &sc->hv_kvp_selinfo);
837 
838 	mtx_unlock(&sc->pending_mutex);
839 
840 	return (revents);
841 }
842 
843 static int
844 hv_kvp_probe(device_t dev)
845 {
846 
847 	return (vmbus_ic_probe(dev, vmbus_kvp_descs));
848 }
849 
850 static int
851 hv_kvp_attach(device_t dev)
852 {
853 	int error;
854 	struct sysctl_oid_list *child;
855 	struct sysctl_ctx_list *ctx;
856 
857 	hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
858 
859 	sc->dev = dev;
860 	sema_init(&sc->dev_sema, 0, "hv_kvp device semaphore");
861 	mtx_init(&sc->pending_mutex, "hv-kvp pending mutex",
862 		NULL, MTX_DEF);
863 
864 	ctx = device_get_sysctl_ctx(dev);
865 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
866 
867 	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "hv_kvp_log",
868 	    CTLFLAG_RWTUN, &hv_kvp_log, 0, "Hyperv KVP service log level");
869 
870 	TASK_INIT(&sc->task, 0, hv_kvp_process_request, sc);
871 
872 	/* create character device */
873 	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
874 			&sc->hv_kvp_dev,
875 			&hv_kvp_cdevsw,
876 			0,
877 			UID_ROOT,
878 			GID_WHEEL,
879 			0640,
880 			"hv_kvp_dev");
881 
882 	if (error != 0)
883 		return (error);
884 	sc->hv_kvp_dev->si_drv1 = sc;
885 
886 	return (vmbus_ic_attach(dev, hv_kvp_callback));
887 }
888 
889 static int
890 hv_kvp_detach(device_t dev)
891 {
892 	hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
893 
894 	if (sc->daemon_task != NULL) {
895 		PROC_LOCK(sc->daemon_task);
896 		kern_psignal(sc->daemon_task, SIGKILL);
897 		PROC_UNLOCK(sc->daemon_task);
898 	}
899 
900 	destroy_dev(sc->hv_kvp_dev);
901 	return (vmbus_ic_detach(dev));
902 }
903 
904 static device_method_t kvp_methods[] = {
905 	/* Device interface */
906 	DEVMETHOD(device_probe, hv_kvp_probe),
907 	DEVMETHOD(device_attach, hv_kvp_attach),
908 	DEVMETHOD(device_detach, hv_kvp_detach),
909 	{ 0, 0 }
910 };
911 
912 static driver_t kvp_driver = { "hvkvp", kvp_methods, sizeof(hv_kvp_sc)};
913 
914 DRIVER_MODULE(hv_kvp, vmbus, kvp_driver, NULL, NULL);
915 MODULE_VERSION(hv_kvp, 1);
916 MODULE_DEPEND(hv_kvp, vmbus, 1, 1, 1);
917