hv_kvp.c revision 295307
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: head/sys/dev/hyperv/utilities/hv_kvp.c 295307 2016-02-05 07:09:58Z sephe $");
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/selinfo.h>
48#include <sys/sysctl.h>
49#include <sys/poll.h>
50#include <sys/proc.h>
51#include <sys/kthread.h>
52#include <sys/syscallsubr.h>
53#include <sys/sysproto.h>
54#include <sys/un.h>
55#include <sys/endian.h>
56#include <sys/_null.h>
57#include <sys/signal.h>
58#include <sys/syslog.h>
59#include <sys/systm.h>
60#include <sys/mutex.h>
61#include <net/if_arp.h>
62
63#include <dev/hyperv/include/hyperv.h>
64#include <dev/hyperv/netvsc/hv_net_vsc.h>
65
66#include "unicode.h"
67#include "hv_kvp.h"
68
69/* hv_kvp defines */
70#define BUFFERSIZE	sizeof(struct hv_kvp_msg)
71#define KVP_SUCCESS	0
72#define KVP_ERROR	1
73#define kvp_hdr		hdr.kvp_hdr
74
75/* hv_kvp debug control */
76static int hv_kvp_log = 0;
77SYSCTL_INT(_dev, OID_AUTO, hv_kvp_log, CTLFLAG_RW, &hv_kvp_log, 0,
78	"hv_kvp log");
79
80#define	hv_kvp_log_error(...)	do {				\
81	if (hv_kvp_log > 0)				\
82		log(LOG_ERR, "hv_kvp: " __VA_ARGS__);	\
83} while (0)
84
85#define	hv_kvp_log_info(...) do {				\
86	if (hv_kvp_log > 1)				\
87		log(LOG_INFO, "hv_kvp: " __VA_ARGS__);		\
88} while (0)
89
90/* character device prototypes */
91static d_open_t		hv_kvp_dev_open;
92static d_close_t	hv_kvp_dev_close;
93static d_read_t		hv_kvp_dev_daemon_read;
94static d_write_t	hv_kvp_dev_daemon_write;
95static d_poll_t		hv_kvp_dev_daemon_poll;
96
97/* hv_kvp prototypes */
98static int	hv_kvp_req_in_progress(void);
99static void	hv_kvp_transaction_init(uint32_t, hv_vmbus_channel *, uint64_t, uint8_t *);
100static void	hv_kvp_send_msg_to_daemon(void);
101static void	hv_kvp_process_request(void *context, int pending);
102
103/* hv_kvp character device structure */
104static struct cdevsw hv_kvp_cdevsw =
105{
106	.d_version	= D_VERSION,
107	.d_open		= hv_kvp_dev_open,
108	.d_close	= hv_kvp_dev_close,
109	.d_read		= hv_kvp_dev_daemon_read,
110	.d_write	= hv_kvp_dev_daemon_write,
111	.d_poll		= hv_kvp_dev_daemon_poll,
112	.d_name		= "hv_kvp_dev",
113};
114static struct cdev *hv_kvp_dev;
115static struct hv_kvp_msg *hv_kvp_dev_buf;
116struct proc *daemon_task;
117
118static struct selinfo hv_kvp_selinfo;
119
120/*
121 * Global state to track and synchronize multiple
122 * KVP transaction requests from the host.
123 */
124static struct {
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 */
173MALLOC_DECLARE(M_HV_KVP_DEV_BUF);
174MALLOC_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 */
183static int
184hv_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 */
194static void
195hv_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 */
215static void
216hv_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 */
257static int
258hv_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 */
305static int
306hv_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 */
410static void
411hv_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 */
524static int
525hv_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 */
594static void
595hv_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 */
623static void
624hv_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	/* We should wake up the daemon, in case it's doing poll() */
633	selwakeup(&hv_kvp_selinfo);
634}
635
636
637/*
638 * Function to read the kvp request buffer from host
639 * and interact with daemon
640 */
641static void
642hv_kvp_process_request(void *context, int pending)
643{
644	uint8_t *kvp_buf;
645	hv_vmbus_channel *channel = context;
646	uint32_t recvlen = 0;
647	uint64_t requestid;
648	struct hv_vmbus_icmsg_hdr *icmsghdrp;
649	int ret = 0;
650	uint64_t pending_cnt = 1;
651
652	hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
653	kvp_buf = receive_buffer[HV_KVP];
654	ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
655		&recvlen, &requestid);
656
657	/*
658	 * We start counting only after the daemon registers
659	 * and therefore there could be requests pending in
660	 * the VMBus that are not reflected in pending_cnt.
661	 * Therefore we continue reading as long as either of
662	 * the below conditions is true.
663	 */
664
665	while ((pending_cnt>0) || ((ret == 0) && (recvlen > 0))) {
666
667		if ((ret == 0) && (recvlen>0)) {
668
669			icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
670					&kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
671
672			hv_kvp_transaction_init(recvlen, channel, requestid, kvp_buf);
673			if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
674				hv_kvp_negotiate_version(icmsghdrp, NULL, kvp_buf);
675				hv_kvp_respond_host(ret);
676
677				/*
678				 * It is ok to not acquire the mutex before setting
679				 * req_in_progress here because negotiation is the
680				 * first thing that happens and hence there is no
681				 * chance of a race condition.
682				 */
683
684				kvp_globals.req_in_progress = false;
685				hv_kvp_log_info("%s :version negotiated\n", __func__);
686
687			} else {
688				if (!kvp_globals.daemon_busy) {
689
690					hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
691					mtx_lock(&kvp_globals.pending_mutex);
692					kvp_globals.req_timed_out = false;
693					kvp_globals.daemon_busy = true;
694					mtx_unlock(&kvp_globals.pending_mutex);
695
696					hv_kvp_send_msg_to_daemon();
697					hv_kvp_log_info("%s: waiting for daemon\n", __func__);
698				}
699
700				/* Wait 5 seconds for daemon to respond back */
701				tsleep(&kvp_globals, 0, "kvpworkitem", 5 * hz);
702				hv_kvp_log_info("%s: came out of wait\n", __func__);
703			}
704		}
705
706		mtx_lock(&kvp_globals.pending_mutex);
707
708		/* Notice that once req_timed_out is set to true
709		 * it will remain true until the next request is
710		 * sent to the daemon. The response from daemon
711		 * is forwarded to host only when this flag is
712		 * false.
713		 */
714		kvp_globals.req_timed_out = true;
715
716		/*
717		 * Cancel request if so need be.
718		 */
719		if (hv_kvp_req_in_progress()) {
720			hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
721			hv_kvp_respond_host(HV_KVP_E_FAIL);
722			kvp_globals.req_in_progress = false;
723		}
724
725		/*
726		* Decrement pending request count and
727		*/
728		if (kvp_globals.pending_reqs>0) {
729			kvp_globals.pending_reqs = kvp_globals.pending_reqs - 1;
730		}
731		pending_cnt = kvp_globals.pending_reqs;
732
733		mtx_unlock(&kvp_globals.pending_mutex);
734
735		/*
736		 * Try reading next buffer
737		 */
738		recvlen = 0;
739		ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
740			&recvlen, &requestid);
741		hv_kvp_log_info("%s: read: context %p, pending_cnt %llu ret =%d, recvlen=%d\n",
742			__func__, context, (unsigned long long)pending_cnt, ret, recvlen);
743	}
744}
745
746
747/*
748 * Callback routine that gets called whenever there is a message from host
749 */
750void
751hv_kvp_callback(void *context)
752{
753	uint64_t pending_cnt = 0;
754
755	if (kvp_globals.register_done == false) {
756		kvp_globals.channelp = context;
757		TASK_INIT(&service_table[HV_KVP].task, 0, hv_kvp_process_request, context);
758	} else {
759		mtx_lock(&kvp_globals.pending_mutex);
760		kvp_globals.pending_reqs = kvp_globals.pending_reqs + 1;
761		pending_cnt = kvp_globals.pending_reqs;
762		mtx_unlock(&kvp_globals.pending_mutex);
763		if (pending_cnt == 1) {
764			hv_kvp_log_info("%s: Queuing work item\n", __func__);
765			taskqueue_enqueue(taskqueue_thread, &service_table[HV_KVP].task);
766		}
767	}
768}
769
770
771/*
772 * This function is called by the hv_kvp_init -
773 * creates character device hv_kvp_dev
774 * allocates memory to hv_kvp_dev_buf
775 *
776 */
777static int
778hv_kvp_dev_init(void)
779{
780	int error = 0;
781
782	/* initialize semaphore */
783	sema_init(&kvp_globals.dev_sema, 0, "hv_kvp device semaphore");
784	/* create character device */
785	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
786			&hv_kvp_dev,
787			&hv_kvp_cdevsw,
788			0,
789			UID_ROOT,
790			GID_WHEEL,
791			0640,
792			"hv_kvp_dev");
793
794	if (error != 0)
795		return (error);
796
797	/*
798	 * Malloc with M_WAITOK flag will never fail.
799	 */
800	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_HV_KVP_DEV_BUF, M_WAITOK |
801				M_ZERO);
802
803	return (0);
804}
805
806
807/*
808 * This function is called by the hv_kvp_deinit -
809 * destroy character device
810 */
811static void
812hv_kvp_dev_destroy(void)
813{
814
815	if (daemon_task != NULL) {
816		PROC_LOCK(daemon_task);
817		kern_psignal(daemon_task, SIGKILL);
818		PROC_UNLOCK(daemon_task);
819	}
820
821	destroy_dev(hv_kvp_dev);
822	free(hv_kvp_dev_buf, M_HV_KVP_DEV_BUF);
823	return;
824}
825
826
827static int
828hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
829				struct thread *td)
830{
831
832	hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
833	if (kvp_globals.dev_accessed)
834		return (-EBUSY);
835
836	daemon_task = curproc;
837	kvp_globals.dev_accessed = true;
838	kvp_globals.daemon_busy = false;
839	return (0);
840}
841
842
843static int
844hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
845				 struct thread *td __unused)
846{
847
848	hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
849	kvp_globals.dev_accessed = false;
850	kvp_globals.register_done = false;
851	return (0);
852}
853
854
855/*
856 * hv_kvp_daemon read invokes this function
857 * acts as a send to daemon
858 */
859static int
860hv_kvp_dev_daemon_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
861{
862	size_t amt;
863	int error = 0;
864
865	/* Check hv_kvp daemon registration status*/
866	if (!kvp_globals.register_done)
867		return (KVP_ERROR);
868
869	sema_wait(&kvp_globals.dev_sema);
870
871	memcpy(hv_kvp_dev_buf, &kvp_globals.daemon_kvp_msg, sizeof(struct hv_kvp_msg));
872
873	amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
874		BUFFERSIZE + 1 - uio->uio_offset);
875
876	if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
877		hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
878
879	return (error);
880}
881
882
883/*
884 * hv_kvp_daemon write invokes this function
885 * acts as a recieve from daemon
886 */
887static int
888hv_kvp_dev_daemon_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
889{
890	size_t amt;
891	int error = 0;
892
893	uio->uio_offset = 0;
894
895	amt = MIN(uio->uio_resid, BUFFERSIZE);
896	error = uiomove(hv_kvp_dev_buf, amt, uio);
897
898	if (error != 0)
899		return (error);
900
901	memcpy(&kvp_globals.daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
902
903	if (kvp_globals.register_done == false) {
904		if (kvp_globals.daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
905
906			kvp_globals.register_done = true;
907			if (kvp_globals.channelp) {
908
909				hv_kvp_callback(kvp_globals.channelp);
910			}
911		}
912		else {
913			hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
914			return (KVP_ERROR);
915		}
916	} else {
917
918		mtx_lock(&kvp_globals.pending_mutex);
919
920		if(!kvp_globals.req_timed_out) {
921
922			hv_kvp_convert_usermsg_to_hostmsg();
923			hv_kvp_respond_host(KVP_SUCCESS);
924			wakeup(&kvp_globals);
925			kvp_globals.req_in_progress = false;
926		}
927
928		kvp_globals.daemon_busy = false;
929		mtx_unlock(&kvp_globals.pending_mutex);
930	}
931
932	return (error);
933}
934
935
936/*
937 * hv_kvp_daemon poll invokes this function to check if data is available
938 * for daemon to read.
939 */
940static int
941hv_kvp_dev_daemon_poll(struct cdev *dev __unused, int events, struct thread *td)
942{
943	int revents = 0;
944
945	mtx_lock(&kvp_globals.pending_mutex);
946	/*
947	 * We check global flag daemon_busy for the data availiability for
948	 * userland to read. Deamon_busy is set to true before driver has data
949	 * for daemon to read. It is set to false after daemon sends
950	 * then response back to driver.
951	 */
952	if (kvp_globals.daemon_busy == true)
953		revents = POLLIN;
954	else
955		selrecord(td, &hv_kvp_selinfo);
956
957	mtx_unlock(&kvp_globals.pending_mutex);
958
959	return (revents);
960}
961
962
963/*
964 * hv_kvp initialization function
965 * called from hv_util service.
966 *
967 */
968int
969hv_kvp_init(hv_vmbus_service *srv)
970{
971	int error = 0;
972
973	memset(&kvp_globals, 0, sizeof(kvp_globals));
974
975	error = hv_kvp_dev_init();
976	mtx_init(&kvp_globals.pending_mutex, "hv-kvp pending mutex",
977		NULL, MTX_DEF);
978
979	return (error);
980}
981
982
983void
984hv_kvp_deinit(void)
985{
986	hv_kvp_dev_destroy();
987	mtx_destroy(&kvp_globals.pending_mutex);
988
989	return;
990}
991