1271493Sdelphij/*-
2324461Ssephe * Copyright (c) 2014,2016-2017 Microsoft Corp.
3271493Sdelphij * All rights reserved.
4271493Sdelphij *
5271493Sdelphij * Redistribution and use in source and binary forms, with or without
6271493Sdelphij * modification, are permitted provided that the following conditions
7271493Sdelphij * are met:
8271493Sdelphij * 1. Redistributions of source code must retain the above copyright
9271493Sdelphij *    notice unmodified, this list of conditions, and the following
10271493Sdelphij *    disclaimer.
11271493Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
12271493Sdelphij *    notice, this list of conditions and the following disclaimer in the
13271493Sdelphij *    documentation and/or other materials provided with the distribution.
14271493Sdelphij *
15271493Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16271493Sdelphij * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17271493Sdelphij * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18271493Sdelphij * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19271493Sdelphij * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20271493Sdelphij * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21271493Sdelphij * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22271493Sdelphij * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23271493Sdelphij * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24271493Sdelphij * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25271493Sdelphij */
26271493Sdelphij
27271493Sdelphij/*
28271493Sdelphij *	Author:	Sainath Varanasi.
29271493Sdelphij *	Date:	4/2012
30271493Sdelphij *	Email:	bsdic@microsoft.com
31271493Sdelphij */
32271493Sdelphij
33271493Sdelphij#include <sys/cdefs.h>
34271493Sdelphij__FBSDID("$FreeBSD: stable/10/sys/dev/hyperv/utilities/hv_kvp.c 324461 2017-10-10 02:22:34Z sephe $");
35271493Sdelphij
36271493Sdelphij#include <sys/param.h>
37271493Sdelphij#include <sys/kernel.h>
38271493Sdelphij#include <sys/conf.h>
39271493Sdelphij#include <sys/uio.h>
40271493Sdelphij#include <sys/bus.h>
41271493Sdelphij#include <sys/malloc.h>
42271493Sdelphij#include <sys/mbuf.h>
43271493Sdelphij#include <sys/module.h>
44271493Sdelphij#include <sys/reboot.h>
45271493Sdelphij#include <sys/lock.h>
46271493Sdelphij#include <sys/taskqueue.h>
47292438Sroyger#include <sys/selinfo.h>
48271493Sdelphij#include <sys/sysctl.h>
49271493Sdelphij#include <sys/poll.h>
50271493Sdelphij#include <sys/proc.h>
51271493Sdelphij#include <sys/kthread.h>
52271493Sdelphij#include <sys/syscallsubr.h>
53271493Sdelphij#include <sys/sysproto.h>
54271493Sdelphij#include <sys/un.h>
55271493Sdelphij#include <sys/endian.h>
56271493Sdelphij#include <sys/_null.h>
57307168Ssephe#include <sys/sema.h>
58271493Sdelphij#include <sys/signal.h>
59271493Sdelphij#include <sys/syslog.h>
60283280Swhu#include <sys/systm.h>
61271493Sdelphij#include <sys/mutex.h>
62302166Ssephe
63271493Sdelphij#include <dev/hyperv/include/hyperv.h>
64308499Ssephe#include <dev/hyperv/include/vmbus.h>
65307096Ssephe#include <dev/hyperv/utilities/hv_utilreg.h>
66308499Ssephe#include <dev/hyperv/utilities/vmbus_icreg.h>
67311230Ssephe#include <dev/hyperv/utilities/vmbus_icvar.h>
68271493Sdelphij
69271493Sdelphij#include "unicode.h"
70271493Sdelphij#include "hv_kvp.h"
71307022Ssephe#include "vmbus_if.h"
72271493Sdelphij
73271493Sdelphij/* hv_kvp defines */
74271493Sdelphij#define BUFFERSIZE	sizeof(struct hv_kvp_msg)
75271493Sdelphij#define kvp_hdr		hdr.kvp_hdr
76271493Sdelphij
77308499Ssephe#define KVP_FWVER_MAJOR		3
78308499Ssephe#define KVP_FWVER		VMBUS_IC_VERSION(KVP_FWVER_MAJOR, 0)
79308499Ssephe
80308499Ssephe#define KVP_MSGVER_MAJOR	4
81308499Ssephe#define KVP_MSGVER		VMBUS_IC_VERSION(KVP_MSGVER_MAJOR, 0)
82308499Ssephe
83271493Sdelphij/* hv_kvp debug control */
84271493Sdelphijstatic int hv_kvp_log = 0;
85271493Sdelphij
86271493Sdelphij#define	hv_kvp_log_error(...)	do {				\
87271493Sdelphij	if (hv_kvp_log > 0)				\
88271493Sdelphij		log(LOG_ERR, "hv_kvp: " __VA_ARGS__);	\
89271493Sdelphij} while (0)
90271493Sdelphij
91271493Sdelphij#define	hv_kvp_log_info(...) do {				\
92271493Sdelphij	if (hv_kvp_log > 1)				\
93271493Sdelphij		log(LOG_INFO, "hv_kvp: " __VA_ARGS__);		\
94271493Sdelphij} while (0)
95271493Sdelphij
96307168Ssephestatic const struct vmbus_ic_desc vmbus_kvp_descs[] = {
97307168Ssephe	{
98307168Ssephe		.ic_guid = { .hv_guid = {
99307168Ssephe		    0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
100307168Ssephe		    0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3,  0xe6 } },
101307168Ssephe		.ic_desc = "Hyper-V KVP"
102307168Ssephe	},
103307168Ssephe	VMBUS_IC_DESC_END
104307168Ssephe};
105301866Ssephe
106271493Sdelphij/* character device prototypes */
107271493Sdelphijstatic d_open_t		hv_kvp_dev_open;
108271493Sdelphijstatic d_close_t	hv_kvp_dev_close;
109271493Sdelphijstatic d_read_t		hv_kvp_dev_daemon_read;
110271493Sdelphijstatic d_write_t	hv_kvp_dev_daemon_write;
111271493Sdelphijstatic d_poll_t		hv_kvp_dev_daemon_poll;
112271493Sdelphij
113271493Sdelphij/* hv_kvp character device structure */
114271493Sdelphijstatic struct cdevsw hv_kvp_cdevsw =
115271493Sdelphij{
116271493Sdelphij	.d_version	= D_VERSION,
117271493Sdelphij	.d_open		= hv_kvp_dev_open,
118271493Sdelphij	.d_close	= hv_kvp_dev_close,
119271493Sdelphij	.d_read		= hv_kvp_dev_daemon_read,
120271493Sdelphij	.d_write	= hv_kvp_dev_daemon_write,
121271493Sdelphij	.d_poll		= hv_kvp_dev_daemon_poll,
122271493Sdelphij	.d_name		= "hv_kvp_dev",
123271493Sdelphij};
124271493Sdelphij
125292438Sroyger
126271493Sdelphij/*
127271493Sdelphij * Global state to track and synchronize multiple
128271493Sdelphij * KVP transaction requests from the host.
129271493Sdelphij */
130301866Ssephetypedef struct hv_kvp_sc {
131311230Ssephe	struct vmbus_ic_softc	util_sc;
132307096Ssephe	device_t		dev;
133271493Sdelphij
134301866Ssephe	/* Unless specified the pending mutex should be
135271493Sdelphij	 * used to alter the values of the following paramters:
136271493Sdelphij	 * 1. req_in_progress
137271493Sdelphij	 * 2. req_timed_out
138271493Sdelphij	 */
139301866Ssephe	struct mtx		pending_mutex;
140301866Ssephe
141301866Ssephe	struct task		task;
142301866Ssephe
143271493Sdelphij	/* To track if transaction is active or not */
144301866Ssephe	boolean_t		req_in_progress;
145271493Sdelphij	/* Tracks if daemon did not reply back in time */
146301866Ssephe	boolean_t		req_timed_out;
147271493Sdelphij	/* Tracks if daemon is serving a request currently */
148271493Sdelphij	boolean_t		daemon_busy;
149301866Ssephe
150271493Sdelphij	/* Length of host message */
151301866Ssephe	uint32_t		host_msg_len;
152271493Sdelphij
153301866Ssephe	/* Host message id */
154301866Ssephe	uint64_t		host_msg_id;
155271493Sdelphij
156271493Sdelphij	/* Current kvp message from the host */
157301866Ssephe	struct hv_kvp_msg	*host_kvp_msg;
158301866Ssephe
159271493Sdelphij	 /* Current kvp message for daemon */
160301866Ssephe	struct hv_kvp_msg	daemon_kvp_msg;
161301866Ssephe
162271493Sdelphij	/* Rcv buffer for communicating with the host*/
163301866Ssephe	uint8_t			*rcv_buf;
164301866Ssephe
165271493Sdelphij	/* Device semaphore to control communication */
166301866Ssephe	struct sema		dev_sema;
167301866Ssephe
168271493Sdelphij	/* Indicates if daemon registered with driver */
169301866Ssephe	boolean_t		register_done;
170301866Ssephe
171271493Sdelphij	/* Character device status */
172301866Ssephe	boolean_t		dev_accessed;
173271493Sdelphij
174301866Ssephe	struct cdev *hv_kvp_dev;
175271493Sdelphij
176301866Ssephe	struct proc *daemon_task;
177301866Ssephe
178301866Ssephe	struct selinfo hv_kvp_selinfo;
179301866Ssephe} hv_kvp_sc;
180301866Ssephe
181301866Ssephe/* hv_kvp prototypes */
182301866Ssephestatic int	hv_kvp_req_in_progress(hv_kvp_sc *sc);
183301866Ssephestatic void	hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t, uint64_t, uint8_t *);
184301866Ssephestatic void	hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc);
185301866Ssephestatic void	hv_kvp_process_request(void *context, int pending);
186301866Ssephe
187271493Sdelphij/*
188271493Sdelphij * hv_kvp low level functions
189271493Sdelphij */
190271493Sdelphij
191271493Sdelphij/*
192271493Sdelphij * Check if kvp transaction is in progres
193271493Sdelphij */
194271493Sdelphijstatic int
195301866Ssephehv_kvp_req_in_progress(hv_kvp_sc *sc)
196271493Sdelphij{
197271493Sdelphij
198301866Ssephe	return (sc->req_in_progress);
199271493Sdelphij}
200271493Sdelphij
201271493Sdelphij
202271493Sdelphij/*
203271493Sdelphij * This routine is called whenever a message is received from the host
204271493Sdelphij */
205271493Sdelphijstatic void
206301866Ssephehv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t rcv_len,
207271493Sdelphij			uint64_t request_id, uint8_t *rcv_buf)
208271493Sdelphij{
209301866Ssephe
210271493Sdelphij	/* Store all the relevant message details in the global structure */
211271493Sdelphij	/* Do not need to use mutex for req_in_progress here */
212301866Ssephe	sc->req_in_progress = true;
213301866Ssephe	sc->host_msg_len = rcv_len;
214301866Ssephe	sc->host_msg_id = request_id;
215301866Ssephe	sc->rcv_buf = rcv_buf;
216301866Ssephe	sc->host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
217308499Ssephe	    sizeof(struct hv_vmbus_pipe_hdr) +
218308499Ssephe	    sizeof(struct hv_vmbus_icmsg_hdr)];
219271493Sdelphij}
220271493Sdelphij
221271493Sdelphij/*
222271493Sdelphij * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
223271493Sdelphij */
224271493Sdelphijstatic int
225301866Ssephehv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
226271493Sdelphij				    struct hv_kvp_ip_msg *host_ip_msg)
227271493Sdelphij{
228271493Sdelphij	int err_ip, err_subnet, err_gway, err_dns, err_adap;
229271493Sdelphij	int UNUSED_FLAG = 1;
230301866Ssephe
231271493Sdelphij	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
232271493Sdelphij	    MAX_IP_ADDR_SIZE,
233271493Sdelphij	    (char *)umsg->body.kvp_ip_val.ip_addr,
234271493Sdelphij	    strlen((char *)umsg->body.kvp_ip_val.ip_addr),
235271493Sdelphij	    UNUSED_FLAG,
236271493Sdelphij	    &err_ip);
237271493Sdelphij	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
238271493Sdelphij	    MAX_IP_ADDR_SIZE,
239271493Sdelphij	    (char *)umsg->body.kvp_ip_val.sub_net,
240271493Sdelphij	    strlen((char *)umsg->body.kvp_ip_val.sub_net),
241271493Sdelphij	    UNUSED_FLAG,
242271493Sdelphij	    &err_subnet);
243271493Sdelphij	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
244271493Sdelphij	    MAX_GATEWAY_SIZE,
245271493Sdelphij	    (char *)umsg->body.kvp_ip_val.gate_way,
246271493Sdelphij	    strlen((char *)umsg->body.kvp_ip_val.gate_way),
247271493Sdelphij	    UNUSED_FLAG,
248271493Sdelphij	    &err_gway);
249271493Sdelphij	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
250271493Sdelphij	    MAX_IP_ADDR_SIZE,
251271493Sdelphij	    (char *)umsg->body.kvp_ip_val.dns_addr,
252271493Sdelphij	    strlen((char *)umsg->body.kvp_ip_val.dns_addr),
253271493Sdelphij	    UNUSED_FLAG,
254271493Sdelphij	    &err_dns);
255271493Sdelphij	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
256322133Ssephe	    MAX_ADAPTER_ID_SIZE,
257271493Sdelphij	    (char *)umsg->body.kvp_ip_val.adapter_id,
258271493Sdelphij	    strlen((char *)umsg->body.kvp_ip_val.adapter_id),
259271493Sdelphij	    UNUSED_FLAG,
260271493Sdelphij	    &err_adap);
261301866Ssephe
262271493Sdelphij	host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
263271493Sdelphij	host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
264271493Sdelphij
265271493Sdelphij	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
266271493Sdelphij}
267271493Sdelphij
268271493Sdelphij
269271493Sdelphij/*
270271493Sdelphij * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
271271493Sdelphij */
272271493Sdelphijstatic int
273271493Sdelphijhv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
274271493Sdelphij				    struct hv_kvp_msg *umsg)
275271493Sdelphij{
276271493Sdelphij	int err_ip, err_subnet, err_gway, err_dns, err_adap;
277271493Sdelphij	int UNUSED_FLAG = 1;
278271493Sdelphij	device_t *devs;
279271493Sdelphij	int devcnt;
280271493Sdelphij
281271493Sdelphij	/* IP Address */
282271493Sdelphij	utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
283271493Sdelphij	    MAX_IP_ADDR_SIZE,
284271493Sdelphij	    (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
285271493Sdelphij	    MAX_IP_ADDR_SIZE,
286271493Sdelphij	    UNUSED_FLAG,
287271493Sdelphij	    &err_ip);
288271493Sdelphij
289271493Sdelphij	/* Adapter ID : GUID */
290271493Sdelphij	utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
291271493Sdelphij	    MAX_ADAPTER_ID_SIZE,
292271493Sdelphij	    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
293271493Sdelphij	    MAX_ADAPTER_ID_SIZE,
294271493Sdelphij	    UNUSED_FLAG,
295271493Sdelphij	    &err_adap);
296271493Sdelphij
297271493Sdelphij	if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
298271493Sdelphij		for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
299307022Ssephe			device_t dev = devs[devcnt];
300307096Ssephe			struct vmbus_channel *chan;
301307022Ssephe			char buf[HYPERV_GUID_STRLEN];
302307201Ssephe			int n;
303271493Sdelphij
304307201Ssephe			chan = vmbus_get_channel(dev);
305307201Ssephe			n = hyperv_guid2str(vmbus_chan_guid_inst(chan), buf,
306307201Ssephe			    sizeof(buf));
307307201Ssephe
308307022Ssephe			/*
309307201Ssephe			 * The string in the 'kvp_ip_val.adapter_id' has
310307201Ssephe			 * braces around the GUID; skip the leading brace
311307201Ssephe			 * in 'kvp_ip_val.adapter_id'.
312307022Ssephe			 */
313307201Ssephe			if (strncmp(buf,
314307201Ssephe			    ((char *)&umsg->body.kvp_ip_val.adapter_id) + 1,
315307201Ssephe			    n) == 0) {
316302166Ssephe				strlcpy((char *)umsg->body.kvp_ip_val.adapter_id,
317307168Ssephe				    device_get_nameunit(dev), MAX_ADAPTER_ID_SIZE);
318271493Sdelphij				break;
319271493Sdelphij			}
320271493Sdelphij		}
321271493Sdelphij		free(devs, M_TEMP);
322271493Sdelphij	}
323271493Sdelphij
324271493Sdelphij	/* Address Family , DHCP , SUBNET, Gateway, DNS */
325271493Sdelphij	umsg->kvp_hdr.operation = host_ip_msg->operation;
326271493Sdelphij	umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
327271493Sdelphij	umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
328271493Sdelphij	utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
329271493Sdelphij	    (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
330271493Sdelphij	    MAX_IP_ADDR_SIZE,
331271493Sdelphij	    UNUSED_FLAG,
332271493Sdelphij	    &err_subnet);
333301866Ssephe
334271493Sdelphij	utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
335271493Sdelphij	    (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
336271493Sdelphij	    MAX_GATEWAY_SIZE,
337271493Sdelphij	    UNUSED_FLAG,
338271493Sdelphij	    &err_gway);
339271493Sdelphij
340271493Sdelphij	utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
341271493Sdelphij	    (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
342271493Sdelphij	    MAX_IP_ADDR_SIZE,
343271493Sdelphij	    UNUSED_FLAG,
344271493Sdelphij	    &err_dns);
345271493Sdelphij
346271493Sdelphij	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
347271493Sdelphij}
348271493Sdelphij
349271493Sdelphij
350271493Sdelphij/*
351271493Sdelphij * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
352271493Sdelphij * Ensure utf16_utf8 takes care of the additional string terminating char!!
353271493Sdelphij */
354271493Sdelphijstatic void
355301866Ssephehv_kvp_convert_hostmsg_to_usermsg(struct hv_kvp_msg *hmsg, struct hv_kvp_msg *umsg)
356271493Sdelphij{
357271493Sdelphij	int utf_err = 0;
358271493Sdelphij	uint32_t value_type;
359301866Ssephe	struct hv_kvp_ip_msg *host_ip_msg;
360271493Sdelphij
361301866Ssephe	host_ip_msg = (struct hv_kvp_ip_msg*)hmsg;
362271493Sdelphij	memset(umsg, 0, sizeof(struct hv_kvp_msg));
363271493Sdelphij
364271493Sdelphij	umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
365271493Sdelphij	umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
366271493Sdelphij
367271493Sdelphij	switch (umsg->kvp_hdr.operation) {
368271493Sdelphij	case HV_KVP_OP_SET_IP_INFO:
369271493Sdelphij		hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
370271493Sdelphij		break;
371271493Sdelphij
372271493Sdelphij	case HV_KVP_OP_GET_IP_INFO:
373271493Sdelphij		utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
374271493Sdelphij		    MAX_ADAPTER_ID_SIZE,
375271493Sdelphij		    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
376271493Sdelphij		    MAX_ADAPTER_ID_SIZE, 1, &utf_err);
377271493Sdelphij
378271493Sdelphij		umsg->body.kvp_ip_val.addr_family =
379271493Sdelphij		    host_ip_msg->kvp_ip_val.addr_family;
380271493Sdelphij		break;
381271493Sdelphij
382271493Sdelphij	case HV_KVP_OP_SET:
383271493Sdelphij		value_type = hmsg->body.kvp_set.data.value_type;
384271493Sdelphij
385271493Sdelphij		switch (value_type) {
386271493Sdelphij		case HV_REG_SZ:
387271493Sdelphij			umsg->body.kvp_set.data.value_size =
388271493Sdelphij			    utf16_to_utf8(
389271493Sdelphij				(char *)umsg->body.kvp_set.data.msg_value.value,
390271493Sdelphij				HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
391271493Sdelphij				(uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
392271493Sdelphij				hmsg->body.kvp_set.data.value_size,
393271493Sdelphij				1, &utf_err);
394271493Sdelphij			/* utf8 encoding */
395271493Sdelphij			umsg->body.kvp_set.data.value_size =
396271493Sdelphij			    umsg->body.kvp_set.data.value_size / 2;
397271493Sdelphij			break;
398271493Sdelphij
399271493Sdelphij		case HV_REG_U32:
400271493Sdelphij			umsg->body.kvp_set.data.value_size =
401271493Sdelphij			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
402271493Sdelphij				hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
403271493Sdelphij			break;
404271493Sdelphij
405271493Sdelphij		case HV_REG_U64:
406271493Sdelphij			umsg->body.kvp_set.data.value_size =
407271493Sdelphij			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
408271493Sdelphij				(unsigned long long)
409271493Sdelphij				hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
410271493Sdelphij			break;
411271493Sdelphij		}
412271493Sdelphij
413271493Sdelphij		umsg->body.kvp_set.data.key_size =
414271493Sdelphij		    utf16_to_utf8(
415271493Sdelphij			umsg->body.kvp_set.data.key,
416271493Sdelphij			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
417271493Sdelphij			(uint16_t *)hmsg->body.kvp_set.data.key,
418271493Sdelphij			hmsg->body.kvp_set.data.key_size,
419271493Sdelphij			1, &utf_err);
420271493Sdelphij
421271493Sdelphij		/* utf8 encoding */
422271493Sdelphij		umsg->body.kvp_set.data.key_size =
423271493Sdelphij		    umsg->body.kvp_set.data.key_size / 2;
424271493Sdelphij		break;
425271493Sdelphij
426271493Sdelphij	case HV_KVP_OP_GET:
427271493Sdelphij		umsg->body.kvp_get.data.key_size =
428271493Sdelphij		    utf16_to_utf8(umsg->body.kvp_get.data.key,
429271493Sdelphij			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
430271493Sdelphij			(uint16_t *)hmsg->body.kvp_get.data.key,
431271493Sdelphij			hmsg->body.kvp_get.data.key_size,
432271493Sdelphij			1, &utf_err);
433271493Sdelphij		/* utf8 encoding */
434271493Sdelphij		umsg->body.kvp_get.data.key_size =
435271493Sdelphij		    umsg->body.kvp_get.data.key_size / 2;
436271493Sdelphij		break;
437271493Sdelphij
438271493Sdelphij	case HV_KVP_OP_DELETE:
439271493Sdelphij		umsg->body.kvp_delete.key_size =
440271493Sdelphij		    utf16_to_utf8(umsg->body.kvp_delete.key,
441271493Sdelphij			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
442271493Sdelphij			(uint16_t *)hmsg->body.kvp_delete.key,
443271493Sdelphij			hmsg->body.kvp_delete.key_size,
444271493Sdelphij			1, &utf_err);
445271493Sdelphij		/* utf8 encoding */
446271493Sdelphij		umsg->body.kvp_delete.key_size =
447271493Sdelphij		    umsg->body.kvp_delete.key_size / 2;
448271493Sdelphij		break;
449271493Sdelphij
450271493Sdelphij	case HV_KVP_OP_ENUMERATE:
451271493Sdelphij		umsg->body.kvp_enum_data.index =
452271493Sdelphij		    hmsg->body.kvp_enum_data.index;
453271493Sdelphij		break;
454271493Sdelphij
455271493Sdelphij	default:
456271493Sdelphij		hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
457271493Sdelphij		    __func__, umsg->kvp_hdr.operation);
458271493Sdelphij	}
459271493Sdelphij}
460271493Sdelphij
461271493Sdelphij
462271493Sdelphij/*
463271493Sdelphij * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
464271493Sdelphij */
465271493Sdelphijstatic int
466301866Ssephehv_kvp_convert_usermsg_to_hostmsg(struct hv_kvp_msg *umsg, struct hv_kvp_msg *hmsg)
467271493Sdelphij{
468271493Sdelphij	int hkey_len = 0, hvalue_len = 0, utf_err = 0;
469271493Sdelphij	struct hv_kvp_exchg_msg_value *host_exchg_data;
470271493Sdelphij	char *key_name, *value;
471271493Sdelphij
472271493Sdelphij	struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
473271493Sdelphij
474271493Sdelphij	switch (hmsg->kvp_hdr.operation) {
475271493Sdelphij	case HV_KVP_OP_GET_IP_INFO:
476271493Sdelphij		return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
477271493Sdelphij
478271493Sdelphij	case HV_KVP_OP_SET_IP_INFO:
479271493Sdelphij	case HV_KVP_OP_SET:
480271493Sdelphij	case HV_KVP_OP_DELETE:
481308516Ssephe		return (0);
482271493Sdelphij
483271493Sdelphij	case HV_KVP_OP_ENUMERATE:
484271493Sdelphij		host_exchg_data = &hmsg->body.kvp_enum_data.data;
485271493Sdelphij		key_name = umsg->body.kvp_enum_data.data.key;
486271493Sdelphij		hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
487271493Sdelphij				((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
488271493Sdelphij				key_name, strlen(key_name),
489271493Sdelphij				1, &utf_err);
490271493Sdelphij		/* utf16 encoding */
491271493Sdelphij		host_exchg_data->key_size = 2 * (hkey_len + 1);
492271493Sdelphij		value = umsg->body.kvp_enum_data.data.msg_value.value;
493271493Sdelphij		hvalue_len = utf8_to_utf16(
494271493Sdelphij				(uint16_t *)host_exchg_data->msg_value.value,
495271493Sdelphij				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
496271493Sdelphij				value, strlen(value),
497271493Sdelphij				1, &utf_err);
498271493Sdelphij		host_exchg_data->value_size = 2 * (hvalue_len + 1);
499271493Sdelphij		host_exchg_data->value_type = HV_REG_SZ;
500271493Sdelphij
501271493Sdelphij		if ((hkey_len < 0) || (hvalue_len < 0))
502308516Ssephe			return (EINVAL);
503301866Ssephe
504308516Ssephe		return (0);
505271493Sdelphij
506271493Sdelphij	case HV_KVP_OP_GET:
507271493Sdelphij		host_exchg_data = &hmsg->body.kvp_get.data;
508271493Sdelphij		value = umsg->body.kvp_get.data.msg_value.value;
509271493Sdelphij		hvalue_len = utf8_to_utf16(
510271493Sdelphij				(uint16_t *)host_exchg_data->msg_value.value,
511271493Sdelphij				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
512271493Sdelphij				value, strlen(value),
513271493Sdelphij				1, &utf_err);
514271493Sdelphij		/* Convert value size to uft16 */
515271493Sdelphij		host_exchg_data->value_size = 2 * (hvalue_len + 1);
516271493Sdelphij		/* Use values by string */
517271493Sdelphij		host_exchg_data->value_type = HV_REG_SZ;
518271493Sdelphij
519317125Ssephe		if (hvalue_len < 0)
520308516Ssephe			return (EINVAL);
521301866Ssephe
522308516Ssephe		return (0);
523271493Sdelphij
524271493Sdelphij	default:
525308516Ssephe		return (EINVAL);
526271493Sdelphij	}
527271493Sdelphij}
528271493Sdelphij
529271493Sdelphij
530271493Sdelphij/*
531271493Sdelphij * Send the response back to the host.
532271493Sdelphij */
533271493Sdelphijstatic void
534308516Ssephehv_kvp_respond_host(hv_kvp_sc *sc, uint32_t error)
535271493Sdelphij{
536271493Sdelphij	struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
537271493Sdelphij
538271493Sdelphij	hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
539301866Ssephe	    &sc->rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
540271493Sdelphij
541271493Sdelphij	hv_icmsg_hdrp->status = error;
542308499Ssephe	hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
543308499Ssephe	    HV_ICMSGHDRFLAG_RESPONSE;
544301866Ssephe
545307096Ssephe	error = vmbus_chan_send(vmbus_get_channel(sc->dev),
546307079Ssephe	    VMBUS_CHANPKT_TYPE_INBAND, 0, sc->rcv_buf, sc->host_msg_len,
547307079Ssephe	    sc->host_msg_id);
548271493Sdelphij	if (error)
549271493Sdelphij		hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
550271493Sdelphij			__func__, error);
551271493Sdelphij}
552271493Sdelphij
553271493Sdelphij
554271493Sdelphij/*
555271493Sdelphij * This is the main kvp kernel process that interacts with both user daemon
556271493Sdelphij * and the host
557271493Sdelphij */
558271493Sdelphijstatic void
559301866Ssephehv_kvp_send_msg_to_daemon(hv_kvp_sc *sc)
560271493Sdelphij{
561301866Ssephe	struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
562301866Ssephe	struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
563301866Ssephe
564271493Sdelphij	/* Prepare kvp_msg to be sent to user */
565301866Ssephe	hv_kvp_convert_hostmsg_to_usermsg(hmsg, umsg);
566271493Sdelphij
567271493Sdelphij	/* Send the msg to user via function deamon_read - setting sema */
568301866Ssephe	sema_post(&sc->dev_sema);
569292438Sroyger
570292438Sroyger	/* We should wake up the daemon, in case it's doing poll() */
571301866Ssephe	selwakeup(&sc->hv_kvp_selinfo);
572271493Sdelphij}
573271493Sdelphij
574271493Sdelphij
575271493Sdelphij/*
576271493Sdelphij * Function to read the kvp request buffer from host
577271493Sdelphij * and interact with daemon
578271493Sdelphij */
579271493Sdelphijstatic void
580301859Ssephehv_kvp_process_request(void *context, int pending)
581271493Sdelphij{
582271493Sdelphij	uint8_t *kvp_buf;
583307096Ssephe	struct vmbus_channel *channel;
584271493Sdelphij	uint32_t recvlen = 0;
585271493Sdelphij	uint64_t requestid;
586271493Sdelphij	struct hv_vmbus_icmsg_hdr *icmsghdrp;
587308499Ssephe	int ret = 0, error;
588308499Ssephe	hv_kvp_sc *sc;
589301866Ssephe
590271493Sdelphij	hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
591301866Ssephe
592301866Ssephe	sc = (hv_kvp_sc*)context;
593311230Ssephe	kvp_buf = sc->util_sc.ic_buf;
594307096Ssephe	channel = vmbus_get_channel(sc->dev);
595301866Ssephe
596307160Ssephe	recvlen = sc->util_sc.ic_buflen;
597307080Ssephe	ret = vmbus_chan_recv(channel, kvp_buf, &recvlen, &requestid);
598307080Ssephe	KASSERT(ret != ENOBUFS, ("hvkvp recvbuf is not large enough"));
599307080Ssephe	/* XXX check recvlen to make sure that it contains enough data */
600271493Sdelphij
601301866Ssephe	while ((ret == 0) && (recvlen > 0)) {
602301866Ssephe		icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
603308499Ssephe		    &kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
604271493Sdelphij
605301866Ssephe		hv_kvp_transaction_init(sc, recvlen, requestid, kvp_buf);
606301866Ssephe		if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
607308499Ssephe			error = vmbus_ic_negomsg(&sc->util_sc,
608308499Ssephe			    kvp_buf, &recvlen, KVP_FWVER, KVP_MSGVER);
609308499Ssephe			/* XXX handle vmbus_ic_negomsg failure. */
610308516Ssephe			if (!error)
611308516Ssephe				hv_kvp_respond_host(sc, HV_S_OK);
612308516Ssephe			else
613308516Ssephe				hv_kvp_respond_host(sc, HV_E_FAIL);
614301866Ssephe			/*
615301866Ssephe			 * It is ok to not acquire the mutex before setting
616301866Ssephe			 * req_in_progress here because negotiation is the
617301866Ssephe			 * first thing that happens and hence there is no
618301866Ssephe			 * chance of a race condition.
619301866Ssephe			 */
620271493Sdelphij
621301866Ssephe			sc->req_in_progress = false;
622301866Ssephe			hv_kvp_log_info("%s :version negotiated\n", __func__);
623271493Sdelphij
624301866Ssephe		} else {
625301866Ssephe			if (!sc->daemon_busy) {
626301866Ssephe
627301866Ssephe				hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
628301866Ssephe				mtx_lock(&sc->pending_mutex);
629301866Ssephe				sc->req_timed_out = false;
630301866Ssephe				sc->daemon_busy = true;
631301866Ssephe				mtx_unlock(&sc->pending_mutex);
632301866Ssephe
633301866Ssephe				hv_kvp_send_msg_to_daemon(sc);
634301866Ssephe				hv_kvp_log_info("%s: waiting for daemon\n", __func__);
635271493Sdelphij			}
636301866Ssephe
637301866Ssephe			/* Wait 5 seconds for daemon to respond back */
638301866Ssephe			tsleep(sc, 0, "kvpworkitem", 5 * hz);
639301866Ssephe			hv_kvp_log_info("%s: came out of wait\n", __func__);
640271493Sdelphij		}
641271493Sdelphij
642301866Ssephe		mtx_lock(&sc->pending_mutex);
643301866Ssephe
644271493Sdelphij		/* Notice that once req_timed_out is set to true
645271493Sdelphij		 * it will remain true until the next request is
646271493Sdelphij		 * sent to the daemon. The response from daemon
647301866Ssephe		 * is forwarded to host only when this flag is
648301866Ssephe		 * false.
649271493Sdelphij		 */
650301866Ssephe		sc->req_timed_out = true;
651271493Sdelphij
652271493Sdelphij		/*
653271493Sdelphij		 * Cancel request if so need be.
654271493Sdelphij		 */
655301866Ssephe		if (hv_kvp_req_in_progress(sc)) {
656271493Sdelphij			hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
657308516Ssephe			hv_kvp_respond_host(sc, HV_E_FAIL);
658301866Ssephe			sc->req_in_progress = false;
659271493Sdelphij		}
660271493Sdelphij
661301866Ssephe		mtx_unlock(&sc->pending_mutex);
662301866Ssephe
663271493Sdelphij		/*
664271493Sdelphij		 * Try reading next buffer
665271493Sdelphij		 */
666307160Ssephe		recvlen = sc->util_sc.ic_buflen;
667307080Ssephe		ret = vmbus_chan_recv(channel, kvp_buf, &recvlen, &requestid);
668307080Ssephe		KASSERT(ret != ENOBUFS, ("hvkvp recvbuf is not large enough"));
669307080Ssephe		/* XXX check recvlen to make sure that it contains enough data */
670307080Ssephe
671301866Ssephe		hv_kvp_log_info("%s: read: context %p, ret =%d, recvlen=%d\n",
672301866Ssephe			__func__, context, ret, recvlen);
673301866Ssephe	}
674271493Sdelphij}
675271493Sdelphij
676271493Sdelphij
677271493Sdelphij/*
678271493Sdelphij * Callback routine that gets called whenever there is a message from host
679271493Sdelphij */
680301866Ssephestatic void
681307096Ssephehv_kvp_callback(struct vmbus_channel *chan __unused, void *context)
682271493Sdelphij{
683301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)context;
684271493Sdelphij	/*
685301866Ssephe	 The first request from host will not be handled until daemon is registered.
686301866Ssephe	 when callback is triggered without a registered daemon, callback just return.
687301866Ssephe	 When a new daemon gets regsitered, this callbcak is trigged from _write op.
688301866Ssephe	*/
689301866Ssephe	if (sc->register_done) {
690301866Ssephe		hv_kvp_log_info("%s: Queuing work item\n", __func__);
691301866Ssephe		taskqueue_enqueue(taskqueue_thread, &sc->task);
692271493Sdelphij	}
693271493Sdelphij}
694271493Sdelphij
695271493Sdelphijstatic int
696271493Sdelphijhv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
697271493Sdelphij				struct thread *td)
698271493Sdelphij{
699301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
700301866Ssephe
701271493Sdelphij	hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
702301866Ssephe	if (sc->dev_accessed)
703271493Sdelphij		return (-EBUSY);
704301866Ssephe
705301866Ssephe	sc->daemon_task = curproc;
706301866Ssephe	sc->dev_accessed = true;
707301866Ssephe	sc->daemon_busy = false;
708271493Sdelphij	return (0);
709271493Sdelphij}
710271493Sdelphij
711271493Sdelphij
712271493Sdelphijstatic int
713271493Sdelphijhv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
714271493Sdelphij				 struct thread *td __unused)
715271493Sdelphij{
716301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
717271493Sdelphij
718271493Sdelphij	hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
719301866Ssephe	sc->dev_accessed = false;
720301866Ssephe	sc->register_done = false;
721271493Sdelphij	return (0);
722271493Sdelphij}
723271493Sdelphij
724271493Sdelphij
725271493Sdelphij/*
726271493Sdelphij * hv_kvp_daemon read invokes this function
727271493Sdelphij * acts as a send to daemon
728271493Sdelphij */
729271493Sdelphijstatic int
730301866Ssephehv_kvp_dev_daemon_read(struct cdev *dev, struct uio *uio, int ioflag __unused)
731271493Sdelphij{
732271493Sdelphij	size_t amt;
733271493Sdelphij	int error = 0;
734301866Ssephe	struct hv_kvp_msg *hv_kvp_dev_buf;
735301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
736271493Sdelphij
737308516Ssephe	/* Read is not allowed util registering is done. */
738301866Ssephe	if (!sc->register_done)
739308516Ssephe		return (EPERM);
740271493Sdelphij
741301866Ssephe	sema_wait(&sc->dev_sema);
742271493Sdelphij
743301866Ssephe	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
744301866Ssephe	memcpy(hv_kvp_dev_buf, &sc->daemon_kvp_msg, sizeof(struct hv_kvp_msg));
745271493Sdelphij
746271493Sdelphij	amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
747271493Sdelphij		BUFFERSIZE + 1 - uio->uio_offset);
748271493Sdelphij
749271493Sdelphij	if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
750271493Sdelphij		hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
751271493Sdelphij
752301866Ssephe	free(hv_kvp_dev_buf, M_TEMP);
753271493Sdelphij	return (error);
754271493Sdelphij}
755271493Sdelphij
756271493Sdelphij
757271493Sdelphij/*
758271493Sdelphij * hv_kvp_daemon write invokes this function
759271493Sdelphij * acts as a recieve from daemon
760271493Sdelphij */
761271493Sdelphijstatic int
762301866Ssephehv_kvp_dev_daemon_write(struct cdev *dev, struct uio *uio, int ioflag __unused)
763271493Sdelphij{
764271493Sdelphij	size_t amt;
765271493Sdelphij	int error = 0;
766301866Ssephe	struct hv_kvp_msg *hv_kvp_dev_buf;
767301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
768271493Sdelphij
769271493Sdelphij	uio->uio_offset = 0;
770301866Ssephe	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
771271493Sdelphij
772271493Sdelphij	amt = MIN(uio->uio_resid, BUFFERSIZE);
773271493Sdelphij	error = uiomove(hv_kvp_dev_buf, amt, uio);
774271493Sdelphij
775301866Ssephe	if (error != 0) {
776301866Ssephe		free(hv_kvp_dev_buf, M_TEMP);
777271493Sdelphij		return (error);
778301866Ssephe	}
779301866Ssephe	memcpy(&sc->daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
780271493Sdelphij
781301866Ssephe	free(hv_kvp_dev_buf, M_TEMP);
782301866Ssephe	if (sc->register_done == false) {
783301866Ssephe		if (sc->daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
784301866Ssephe			sc->register_done = true;
785307096Ssephe			hv_kvp_callback(vmbus_get_channel(sc->dev), dev->si_drv1);
786271493Sdelphij		}
787271493Sdelphij		else {
788271493Sdelphij			hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
789308516Ssephe			return (EINVAL);
790271493Sdelphij		}
791271493Sdelphij	} else {
792271493Sdelphij
793301866Ssephe		mtx_lock(&sc->pending_mutex);
794271493Sdelphij
795301866Ssephe		if(!sc->req_timed_out) {
796301866Ssephe			struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
797301866Ssephe			struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
798271493Sdelphij
799308516Ssephe			error = hv_kvp_convert_usermsg_to_hostmsg(umsg, hmsg);
800308516Ssephe			hv_kvp_respond_host(sc, umsg->hdr.error);
801301866Ssephe			wakeup(sc);
802301866Ssephe			sc->req_in_progress = false;
803308516Ssephe			if (umsg->hdr.error != HV_S_OK)
804308516Ssephe				hv_kvp_log_info("%s, Error 0x%x from daemon\n",
805308516Ssephe				    __func__, umsg->hdr.error);
806308516Ssephe			if (error)
807308516Ssephe				hv_kvp_log_info("%s, Error from convert\n", __func__);
808271493Sdelphij		}
809271493Sdelphij
810301866Ssephe		sc->daemon_busy = false;
811301866Ssephe		mtx_unlock(&sc->pending_mutex);
812271493Sdelphij	}
813271493Sdelphij
814271493Sdelphij	return (error);
815271493Sdelphij}
816271493Sdelphij
817271493Sdelphij
818271493Sdelphij/*
819271493Sdelphij * hv_kvp_daemon poll invokes this function to check if data is available
820271493Sdelphij * for daemon to read.
821271493Sdelphij */
822271493Sdelphijstatic int
823301866Ssephehv_kvp_dev_daemon_poll(struct cdev *dev, int events, struct thread *td)
824271493Sdelphij{
825271493Sdelphij	int revents = 0;
826301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
827271493Sdelphij
828301866Ssephe	mtx_lock(&sc->pending_mutex);
829271493Sdelphij	/*
830271493Sdelphij	 * We check global flag daemon_busy for the data availiability for
831271493Sdelphij	 * userland to read. Deamon_busy is set to true before driver has data
832271493Sdelphij	 * for daemon to read. It is set to false after daemon sends
833271493Sdelphij	 * then response back to driver.
834271493Sdelphij	 */
835301866Ssephe	if (sc->daemon_busy == true)
836271493Sdelphij		revents = POLLIN;
837292438Sroyger	else
838301866Ssephe		selrecord(td, &sc->hv_kvp_selinfo);
839292438Sroyger
840301866Ssephe	mtx_unlock(&sc->pending_mutex);
841271493Sdelphij
842271493Sdelphij	return (revents);
843271493Sdelphij}
844271493Sdelphij
845301866Ssephestatic int
846301866Ssephehv_kvp_probe(device_t dev)
847301866Ssephe{
848301948Ssephe
849307168Ssephe	return (vmbus_ic_probe(dev, vmbus_kvp_descs));
850301866Ssephe}
851301866Ssephe
852301866Ssephestatic int
853301866Ssephehv_kvp_attach(device_t dev)
854271493Sdelphij{
855301866Ssephe	int error;
856301866Ssephe	struct sysctl_oid_list *child;
857301866Ssephe	struct sysctl_ctx_list *ctx;
858301859Ssephe
859301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
860271493Sdelphij
861307096Ssephe	sc->dev = dev;
862301866Ssephe	sema_init(&sc->dev_sema, 0, "hv_kvp device semaphore");
863301866Ssephe	mtx_init(&sc->pending_mutex, "hv-kvp pending mutex",
864301859Ssephe		NULL, MTX_DEF);
865271493Sdelphij
866301866Ssephe	ctx = device_get_sysctl_ctx(dev);
867301866Ssephe	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
868301866Ssephe
869301866Ssephe	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "hv_kvp_log",
870308516Ssephe	    CTLFLAG_RWTUN, &hv_kvp_log, 0, "Hyperv KVP service log level");
871301866Ssephe
872301866Ssephe	TASK_INIT(&sc->task, 0, hv_kvp_process_request, sc);
873301866Ssephe
874301866Ssephe	/* create character device */
875301866Ssephe	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
876301866Ssephe			&sc->hv_kvp_dev,
877301866Ssephe			&hv_kvp_cdevsw,
878301866Ssephe			0,
879301866Ssephe			UID_ROOT,
880301866Ssephe			GID_WHEEL,
881301866Ssephe			0640,
882301866Ssephe			"hv_kvp_dev");
883301866Ssephe
884301866Ssephe	if (error != 0)
885301866Ssephe		return (error);
886301866Ssephe	sc->hv_kvp_dev->si_drv1 = sc;
887301866Ssephe
888311230Ssephe	return (vmbus_ic_attach(dev, hv_kvp_callback));
889271493Sdelphij}
890271493Sdelphij
891301866Ssephestatic int
892301866Ssephehv_kvp_detach(device_t dev)
893271493Sdelphij{
894301866Ssephe	hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
895271493Sdelphij
896301866Ssephe	if (sc->daemon_task != NULL) {
897301866Ssephe		PROC_LOCK(sc->daemon_task);
898301866Ssephe		kern_psignal(sc->daemon_task, SIGKILL);
899301866Ssephe		PROC_UNLOCK(sc->daemon_task);
900301866Ssephe	}
901301866Ssephe
902301866Ssephe	destroy_dev(sc->hv_kvp_dev);
903311230Ssephe	return (vmbus_ic_detach(dev));
904271493Sdelphij}
905301866Ssephe
906301866Ssephestatic device_method_t kvp_methods[] = {
907301866Ssephe	/* Device interface */
908301866Ssephe	DEVMETHOD(device_probe, hv_kvp_probe),
909301866Ssephe	DEVMETHOD(device_attach, hv_kvp_attach),
910301866Ssephe	DEVMETHOD(device_detach, hv_kvp_detach),
911301866Ssephe	{ 0, 0 }
912301866Ssephe};
913301866Ssephe
914301866Ssephestatic driver_t kvp_driver = { "hvkvp", kvp_methods, sizeof(hv_kvp_sc)};
915301866Ssephe
916301866Ssephestatic devclass_t kvp_devclass;
917301866Ssephe
918301866SsepheDRIVER_MODULE(hv_kvp, vmbus, kvp_driver, kvp_devclass, NULL, NULL);
919301866SsepheMODULE_VERSION(hv_kvp, 1);
920301866SsepheMODULE_DEPEND(hv_kvp, vmbus, 1, 1, 1);
921