xref: /openbmc/linux/drivers/hv/hv_kvp.c (revision 97bf16cd309805ebf82ffcc4063a65e06169651f)
146a97191SGreg Kroah-Hartman /*
246a97191SGreg Kroah-Hartman  * An implementation of key value pair (KVP) functionality for Linux.
346a97191SGreg Kroah-Hartman  *
446a97191SGreg Kroah-Hartman  *
546a97191SGreg Kroah-Hartman  * Copyright (C) 2010, Novell, Inc.
646a97191SGreg Kroah-Hartman  * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
746a97191SGreg Kroah-Hartman  *
846a97191SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify it
946a97191SGreg Kroah-Hartman  * under the terms of the GNU General Public License version 2 as published
1046a97191SGreg Kroah-Hartman  * by the Free Software Foundation.
1146a97191SGreg Kroah-Hartman  *
1246a97191SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful, but
1346a97191SGreg Kroah-Hartman  * WITHOUT ANY WARRANTY; without even the implied warranty of
1446a97191SGreg Kroah-Hartman  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
1546a97191SGreg Kroah-Hartman  * NON INFRINGEMENT.  See the GNU General Public License for more
1646a97191SGreg Kroah-Hartman  * details.
1746a97191SGreg Kroah-Hartman  *
1846a97191SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
1946a97191SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
2046a97191SGreg Kroah-Hartman  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2146a97191SGreg Kroah-Hartman  *
2246a97191SGreg Kroah-Hartman  */
2346a97191SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2446a97191SGreg Kroah-Hartman 
2546a97191SGreg Kroah-Hartman #include <linux/net.h>
2646a97191SGreg Kroah-Hartman #include <linux/nls.h>
2746a97191SGreg Kroah-Hartman #include <linux/connector.h>
2846a97191SGreg Kroah-Hartman #include <linux/workqueue.h>
2946a97191SGreg Kroah-Hartman #include <linux/hyperv.h>
3046a97191SGreg Kroah-Hartman 
313647a83dSVitaly Kuznetsov #include "hyperv_vmbus.h"
3246a97191SGreg Kroah-Hartman 
336741335bSK. Y. Srinivasan /*
346741335bSK. Y. Srinivasan  * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
356741335bSK. Y. Srinivasan  */
363a491605SK. Y. Srinivasan #define WS2008_SRV_MAJOR	1
373a491605SK. Y. Srinivasan #define WS2008_SRV_MINOR	0
383a491605SK. Y. Srinivasan #define WS2008_SRV_VERSION     (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
393a491605SK. Y. Srinivasan 
406741335bSK. Y. Srinivasan #define WIN7_SRV_MAJOR   3
416741335bSK. Y. Srinivasan #define WIN7_SRV_MINOR   0
423a491605SK. Y. Srinivasan #define WIN7_SRV_VERSION     (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
436741335bSK. Y. Srinivasan 
446741335bSK. Y. Srinivasan #define WIN8_SRV_MAJOR   4
456741335bSK. Y. Srinivasan #define WIN8_SRV_MINOR   0
463a491605SK. Y. Srinivasan #define WIN8_SRV_VERSION     (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
4746a97191SGreg Kroah-Hartman 
4846a97191SGreg Kroah-Hartman /*
49*97bf16cdSVitaly Kuznetsov  * Global state maintained for transaction that is being processed. For a class
50*97bf16cdSVitaly Kuznetsov  * of integration services, including the "KVP service", the specified protocol
51*97bf16cdSVitaly Kuznetsov  * is a "request/response" protocol which means that there can only be single
52*97bf16cdSVitaly Kuznetsov  * outstanding transaction from the host at any given point in time. We use
53*97bf16cdSVitaly Kuznetsov  * this to simplify memory management in this driver - we cache and process
54*97bf16cdSVitaly Kuznetsov  * only one message at a time.
5546a97191SGreg Kroah-Hartman  *
56*97bf16cdSVitaly Kuznetsov  * While the request/response protocol is guaranteed by the host, we further
57*97bf16cdSVitaly Kuznetsov  * ensure this by serializing packet processing in this driver - we do not
58*97bf16cdSVitaly Kuznetsov  * read additional packets from the VMBUs until the current packet is fully
59*97bf16cdSVitaly Kuznetsov  * handled.
6046a97191SGreg Kroah-Hartman  */
6146a97191SGreg Kroah-Hartman 
6246a97191SGreg Kroah-Hartman static struct {
63*97bf16cdSVitaly Kuznetsov 	int state;   /* hvutil_device_state */
6446a97191SGreg Kroah-Hartman 	int recv_len; /* number of bytes received. */
65fa3d5b85SK. Y. Srinivasan 	struct hv_kvp_msg  *kvp_msg; /* current message */
6646a97191SGreg Kroah-Hartman 	struct vmbus_channel *recv_channel; /* chn we got the request */
6746a97191SGreg Kroah-Hartman 	u64 recv_req_id; /* request ID. */
68fa3d5b85SK. Y. Srinivasan 	void *kvp_context; /* for the channel callback */
6946a97191SGreg Kroah-Hartman } kvp_transaction;
7046a97191SGreg Kroah-Hartman 
71b47a81dcSK. Y. Srinivasan /*
72b47a81dcSK. Y. Srinivasan  * This state maintains the version number registered by the daemon.
73b47a81dcSK. Y. Srinivasan  */
74b47a81dcSK. Y. Srinivasan static int dm_reg_value;
75b47a81dcSK. Y. Srinivasan 
7646a97191SGreg Kroah-Hartman static void kvp_send_key(struct work_struct *dummy);
7746a97191SGreg Kroah-Hartman 
7846a97191SGreg Kroah-Hartman 
7903db7724SK. Y. Srinivasan static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
8068c8b39aSVitaly Kuznetsov static void kvp_timeout_func(struct work_struct *dummy);
81b47a81dcSK. Y. Srinivasan static void kvp_register(int);
8246a97191SGreg Kroah-Hartman 
8368c8b39aSVitaly Kuznetsov static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
8446a97191SGreg Kroah-Hartman static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
8546a97191SGreg Kroah-Hartman 
8646a97191SGreg Kroah-Hartman static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
8746a97191SGreg Kroah-Hartman static const char kvp_name[] = "kvp_kernel_module";
8846a97191SGreg Kroah-Hartman static u8 *recv_buffer;
8946a97191SGreg Kroah-Hartman /*
9046a97191SGreg Kroah-Hartman  * Register the kernel component with the user-level daemon.
9146a97191SGreg Kroah-Hartman  * As part of this registration, pass the LIC version number.
92cfc25993SOlaf Hering  * This number has no meaning, it satisfies the registration protocol.
9346a97191SGreg Kroah-Hartman  */
94cfc25993SOlaf Hering #define HV_DRV_VERSION           "3.1"
9546a97191SGreg Kroah-Hartman 
9646a97191SGreg Kroah-Hartman static void
97b47a81dcSK. Y. Srinivasan kvp_register(int reg_value)
9846a97191SGreg Kroah-Hartman {
9946a97191SGreg Kroah-Hartman 
10046a97191SGreg Kroah-Hartman 	struct cn_msg *msg;
10126403354SK. Y. Srinivasan 	struct hv_kvp_msg *kvp_msg;
10226403354SK. Y. Srinivasan 	char *version;
10346a97191SGreg Kroah-Hartman 
10426403354SK. Y. Srinivasan 	msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
10546a97191SGreg Kroah-Hartman 
10646a97191SGreg Kroah-Hartman 	if (msg) {
10726403354SK. Y. Srinivasan 		kvp_msg = (struct hv_kvp_msg *)msg->data;
108e485ceacSK. Y. Srinivasan 		version = kvp_msg->body.kvp_register.version;
10946a97191SGreg Kroah-Hartman 		msg->id.idx =  CN_KVP_IDX;
11046a97191SGreg Kroah-Hartman 		msg->id.val = CN_KVP_VAL;
11126403354SK. Y. Srinivasan 
112b47a81dcSK. Y. Srinivasan 		kvp_msg->kvp_hdr.operation = reg_value;
11326403354SK. Y. Srinivasan 		strcpy(version, HV_DRV_VERSION);
11426403354SK. Y. Srinivasan 		msg->len = sizeof(struct hv_kvp_msg);
115ac8f7330SDavid Fries 		cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
11646a97191SGreg Kroah-Hartman 		kfree(msg);
11746a97191SGreg Kroah-Hartman 	}
11846a97191SGreg Kroah-Hartman }
11968c8b39aSVitaly Kuznetsov 
12068c8b39aSVitaly Kuznetsov static void kvp_timeout_func(struct work_struct *dummy)
12146a97191SGreg Kroah-Hartman {
12246a97191SGreg Kroah-Hartman 	/*
12346a97191SGreg Kroah-Hartman 	 * If the timer fires, the user-mode component has not responded;
12446a97191SGreg Kroah-Hartman 	 * process the pending transaction.
12546a97191SGreg Kroah-Hartman 	 */
12603db7724SK. Y. Srinivasan 	kvp_respond_to_host(NULL, HV_E_FAIL);
127*97bf16cdSVitaly Kuznetsov 
128*97bf16cdSVitaly Kuznetsov 	/* Transaction is finished, reset the state. */
129*97bf16cdSVitaly Kuznetsov 	if (kvp_transaction.state > HVUTIL_READY)
130*97bf16cdSVitaly Kuznetsov 		kvp_transaction.state = HVUTIL_READY;
131*97bf16cdSVitaly Kuznetsov 
132*97bf16cdSVitaly Kuznetsov 	hv_poll_channel(kvp_transaction.kvp_context,
133*97bf16cdSVitaly Kuznetsov 			hv_kvp_onchannelcallback);
13446a97191SGreg Kroah-Hartman }
13546a97191SGreg Kroah-Hartman 
136b47a81dcSK. Y. Srinivasan static int kvp_handle_handshake(struct hv_kvp_msg *msg)
137b47a81dcSK. Y. Srinivasan {
138b47a81dcSK. Y. Srinivasan 	int ret = 1;
139b47a81dcSK. Y. Srinivasan 
140b47a81dcSK. Y. Srinivasan 	switch (msg->kvp_hdr.operation) {
141b47a81dcSK. Y. Srinivasan 	case KVP_OP_REGISTER:
142b47a81dcSK. Y. Srinivasan 		dm_reg_value = KVP_OP_REGISTER;
143b47a81dcSK. Y. Srinivasan 		pr_info("KVP: IP injection functionality not available\n");
144b47a81dcSK. Y. Srinivasan 		pr_info("KVP: Upgrade the KVP daemon\n");
145b47a81dcSK. Y. Srinivasan 		break;
146b47a81dcSK. Y. Srinivasan 	case KVP_OP_REGISTER1:
147b47a81dcSK. Y. Srinivasan 		dm_reg_value = KVP_OP_REGISTER1;
148b47a81dcSK. Y. Srinivasan 		break;
149b47a81dcSK. Y. Srinivasan 	default:
150b47a81dcSK. Y. Srinivasan 		pr_info("KVP: incompatible daemon\n");
151b47a81dcSK. Y. Srinivasan 		pr_info("KVP: KVP version: %d, Daemon version: %d\n",
152b47a81dcSK. Y. Srinivasan 			KVP_OP_REGISTER1, msg->kvp_hdr.operation);
153b47a81dcSK. Y. Srinivasan 		ret = 0;
154b47a81dcSK. Y. Srinivasan 	}
155b47a81dcSK. Y. Srinivasan 
156b47a81dcSK. Y. Srinivasan 	if (ret) {
157b47a81dcSK. Y. Srinivasan 		/*
158b47a81dcSK. Y. Srinivasan 		 * We have a compatible daemon; complete the handshake.
159b47a81dcSK. Y. Srinivasan 		 */
160b47a81dcSK. Y. Srinivasan 		pr_info("KVP: user-mode registering done.\n");
161b47a81dcSK. Y. Srinivasan 		kvp_register(dm_reg_value);
162*97bf16cdSVitaly Kuznetsov 		kvp_transaction.state = HVUTIL_READY;
163b47a81dcSK. Y. Srinivasan 	}
164b47a81dcSK. Y. Srinivasan 	return ret;
165b47a81dcSK. Y. Srinivasan }
166b47a81dcSK. Y. Srinivasan 
167b47a81dcSK. Y. Srinivasan 
16846a97191SGreg Kroah-Hartman /*
16946a97191SGreg Kroah-Hartman  * Callback when data is received from user mode.
17046a97191SGreg Kroah-Hartman  */
17146a97191SGreg Kroah-Hartman 
17246a97191SGreg Kroah-Hartman static void
17346a97191SGreg Kroah-Hartman kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
17446a97191SGreg Kroah-Hartman {
17526403354SK. Y. Srinivasan 	struct hv_kvp_msg *message;
17626403354SK. Y. Srinivasan 	struct hv_kvp_msg_enumerate *data;
177b47a81dcSK. Y. Srinivasan 	int	error = 0;
17846a97191SGreg Kroah-Hartman 
17926403354SK. Y. Srinivasan 	message = (struct hv_kvp_msg *)msg->data;
180b47a81dcSK. Y. Srinivasan 
181b47a81dcSK. Y. Srinivasan 	/*
182b47a81dcSK. Y. Srinivasan 	 * If we are negotiating the version information
183b47a81dcSK. Y. Srinivasan 	 * with the daemon; handle that first.
184b47a81dcSK. Y. Srinivasan 	 */
185b47a81dcSK. Y. Srinivasan 
186*97bf16cdSVitaly Kuznetsov 	if (kvp_transaction.state < HVUTIL_READY) {
187*97bf16cdSVitaly Kuznetsov 		kvp_handle_handshake(message);
188b47a81dcSK. Y. Srinivasan 		return;
189b47a81dcSK. Y. Srinivasan 	}
190b47a81dcSK. Y. Srinivasan 
191*97bf16cdSVitaly Kuznetsov 	/* We didn't send anything to userspace so the reply is spurious */
192*97bf16cdSVitaly Kuznetsov 	if (kvp_transaction.state < HVUTIL_USERSPACE_REQ)
193*97bf16cdSVitaly Kuznetsov 		return;
194*97bf16cdSVitaly Kuznetsov 	kvp_transaction.state = HVUTIL_USERSPACE_RECV;
195*97bf16cdSVitaly Kuznetsov 
196b47a81dcSK. Y. Srinivasan 	/*
197b47a81dcSK. Y. Srinivasan 	 * Based on the version of the daemon, we propagate errors from the
198b47a81dcSK. Y. Srinivasan 	 * daemon differently.
199b47a81dcSK. Y. Srinivasan 	 */
200b47a81dcSK. Y. Srinivasan 
201b47a81dcSK. Y. Srinivasan 	data = &message->body.kvp_enum_data;
202b47a81dcSK. Y. Srinivasan 
203b47a81dcSK. Y. Srinivasan 	switch (dm_reg_value) {
204fa3d5b85SK. Y. Srinivasan 	case KVP_OP_REGISTER:
205b47a81dcSK. Y. Srinivasan 		/*
206b47a81dcSK. Y. Srinivasan 		 * Null string is used to pass back error condition.
207b47a81dcSK. Y. Srinivasan 		 */
208b47a81dcSK. Y. Srinivasan 		if (data->data.key[0] == 0)
209b47a81dcSK. Y. Srinivasan 			error = HV_S_CONT;
210fa3d5b85SK. Y. Srinivasan 		break;
21146a97191SGreg Kroah-Hartman 
212b47a81dcSK. Y. Srinivasan 	case KVP_OP_REGISTER1:
213b47a81dcSK. Y. Srinivasan 		/*
214b47a81dcSK. Y. Srinivasan 		 * We use the message header information from
215b47a81dcSK. Y. Srinivasan 		 * the user level daemon to transmit errors.
216b47a81dcSK. Y. Srinivasan 		 */
217b47a81dcSK. Y. Srinivasan 		error = message->error;
218b47a81dcSK. Y. Srinivasan 		break;
219b47a81dcSK. Y. Srinivasan 	}
220b47a81dcSK. Y. Srinivasan 
22146a97191SGreg Kroah-Hartman 	/*
22246a97191SGreg Kroah-Hartman 	 * Complete the transaction by forwarding the key value
22346a97191SGreg Kroah-Hartman 	 * to the host. But first, cancel the timeout.
22446a97191SGreg Kroah-Hartman 	 */
225*97bf16cdSVitaly Kuznetsov 	if (cancel_delayed_work_sync(&kvp_timeout_work)) {
22603db7724SK. Y. Srinivasan 		kvp_respond_to_host(message, error);
227*97bf16cdSVitaly Kuznetsov 		kvp_transaction.state = HVUTIL_READY;
228*97bf16cdSVitaly Kuznetsov 		hv_poll_channel(kvp_transaction.kvp_context,
229*97bf16cdSVitaly Kuznetsov 				hv_kvp_onchannelcallback);
230*97bf16cdSVitaly Kuznetsov 	}
23146a97191SGreg Kroah-Hartman }
23246a97191SGreg Kroah-Hartman 
23303db7724SK. Y. Srinivasan 
23403db7724SK. Y. Srinivasan static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
23503db7724SK. Y. Srinivasan {
23603db7724SK. Y. Srinivasan 	struct hv_kvp_msg *in = in_msg;
23703db7724SK. Y. Srinivasan 	struct hv_kvp_ip_msg *out = out_msg;
23803db7724SK. Y. Srinivasan 	int len;
23903db7724SK. Y. Srinivasan 
24003db7724SK. Y. Srinivasan 	switch (op) {
24103db7724SK. Y. Srinivasan 	case KVP_OP_GET_IP_INFO:
24203db7724SK. Y. Srinivasan 		/*
24303db7724SK. Y. Srinivasan 		 * Transform all parameters into utf16 encoding.
24403db7724SK. Y. Srinivasan 		 */
24503db7724SK. Y. Srinivasan 		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
24603db7724SK. Y. Srinivasan 				strlen((char *)in->body.kvp_ip_val.ip_addr),
24703db7724SK. Y. Srinivasan 				UTF16_HOST_ENDIAN,
24803db7724SK. Y. Srinivasan 				(wchar_t *)out->kvp_ip_val.ip_addr,
24903db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE);
25003db7724SK. Y. Srinivasan 		if (len < 0)
25103db7724SK. Y. Srinivasan 			return len;
25203db7724SK. Y. Srinivasan 
25303db7724SK. Y. Srinivasan 		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
25403db7724SK. Y. Srinivasan 				strlen((char *)in->body.kvp_ip_val.sub_net),
25503db7724SK. Y. Srinivasan 				UTF16_HOST_ENDIAN,
25603db7724SK. Y. Srinivasan 				(wchar_t *)out->kvp_ip_val.sub_net,
25703db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE);
25803db7724SK. Y. Srinivasan 		if (len < 0)
25903db7724SK. Y. Srinivasan 			return len;
26003db7724SK. Y. Srinivasan 
26103db7724SK. Y. Srinivasan 		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
26203db7724SK. Y. Srinivasan 				strlen((char *)in->body.kvp_ip_val.gate_way),
26303db7724SK. Y. Srinivasan 				UTF16_HOST_ENDIAN,
26403db7724SK. Y. Srinivasan 				(wchar_t *)out->kvp_ip_val.gate_way,
26503db7724SK. Y. Srinivasan 				MAX_GATEWAY_SIZE);
26603db7724SK. Y. Srinivasan 		if (len < 0)
26703db7724SK. Y. Srinivasan 			return len;
26803db7724SK. Y. Srinivasan 
26903db7724SK. Y. Srinivasan 		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
27003db7724SK. Y. Srinivasan 				strlen((char *)in->body.kvp_ip_val.dns_addr),
27103db7724SK. Y. Srinivasan 				UTF16_HOST_ENDIAN,
27203db7724SK. Y. Srinivasan 				(wchar_t *)out->kvp_ip_val.dns_addr,
27303db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE);
27403db7724SK. Y. Srinivasan 		if (len < 0)
27503db7724SK. Y. Srinivasan 			return len;
27603db7724SK. Y. Srinivasan 
27703db7724SK. Y. Srinivasan 		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
27803db7724SK. Y. Srinivasan 				strlen((char *)in->body.kvp_ip_val.adapter_id),
27903db7724SK. Y. Srinivasan 				UTF16_HOST_ENDIAN,
28003db7724SK. Y. Srinivasan 				(wchar_t *)out->kvp_ip_val.adapter_id,
28103db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE);
28203db7724SK. Y. Srinivasan 		if (len < 0)
28303db7724SK. Y. Srinivasan 			return len;
28403db7724SK. Y. Srinivasan 
28503db7724SK. Y. Srinivasan 		out->kvp_ip_val.dhcp_enabled =
28603db7724SK. Y. Srinivasan 			in->body.kvp_ip_val.dhcp_enabled;
287a500e0e7SK. Y. Srinivasan 		out->kvp_ip_val.addr_family =
288a500e0e7SK. Y. Srinivasan 			in->body.kvp_ip_val.addr_family;
28903db7724SK. Y. Srinivasan 	}
29003db7724SK. Y. Srinivasan 
29103db7724SK. Y. Srinivasan 	return 0;
29203db7724SK. Y. Srinivasan }
29303db7724SK. Y. Srinivasan 
29403db7724SK. Y. Srinivasan static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
29503db7724SK. Y. Srinivasan {
29603db7724SK. Y. Srinivasan 	struct hv_kvp_ip_msg *in = in_msg;
29703db7724SK. Y. Srinivasan 	struct hv_kvp_msg *out = out_msg;
29803db7724SK. Y. Srinivasan 
29903db7724SK. Y. Srinivasan 	switch (op) {
30003db7724SK. Y. Srinivasan 	case KVP_OP_SET_IP_INFO:
30103db7724SK. Y. Srinivasan 		/*
30203db7724SK. Y. Srinivasan 		 * Transform all parameters into utf8 encoding.
30303db7724SK. Y. Srinivasan 		 */
30403db7724SK. Y. Srinivasan 		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
30503db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE,
30603db7724SK. Y. Srinivasan 				UTF16_LITTLE_ENDIAN,
30703db7724SK. Y. Srinivasan 				(__u8 *)out->body.kvp_ip_val.ip_addr,
30803db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE);
30903db7724SK. Y. Srinivasan 
31003db7724SK. Y. Srinivasan 		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
31103db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE,
31203db7724SK. Y. Srinivasan 				UTF16_LITTLE_ENDIAN,
31303db7724SK. Y. Srinivasan 				(__u8 *)out->body.kvp_ip_val.sub_net,
31403db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE);
31503db7724SK. Y. Srinivasan 
31603db7724SK. Y. Srinivasan 		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
31703db7724SK. Y. Srinivasan 				MAX_GATEWAY_SIZE,
31803db7724SK. Y. Srinivasan 				UTF16_LITTLE_ENDIAN,
31903db7724SK. Y. Srinivasan 				(__u8 *)out->body.kvp_ip_val.gate_way,
32003db7724SK. Y. Srinivasan 				MAX_GATEWAY_SIZE);
32103db7724SK. Y. Srinivasan 
32203db7724SK. Y. Srinivasan 		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
32303db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE,
32403db7724SK. Y. Srinivasan 				UTF16_LITTLE_ENDIAN,
32503db7724SK. Y. Srinivasan 				(__u8 *)out->body.kvp_ip_val.dns_addr,
32603db7724SK. Y. Srinivasan 				MAX_IP_ADDR_SIZE);
32703db7724SK. Y. Srinivasan 
32803db7724SK. Y. Srinivasan 		out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
32903db7724SK. Y. Srinivasan 
33003db7724SK. Y. Srinivasan 	default:
33103db7724SK. Y. Srinivasan 		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
33203db7724SK. Y. Srinivasan 				MAX_ADAPTER_ID_SIZE,
33303db7724SK. Y. Srinivasan 				UTF16_LITTLE_ENDIAN,
33403db7724SK. Y. Srinivasan 				(__u8 *)out->body.kvp_ip_val.adapter_id,
33503db7724SK. Y. Srinivasan 				MAX_ADAPTER_ID_SIZE);
33603db7724SK. Y. Srinivasan 
33703db7724SK. Y. Srinivasan 		out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
33803db7724SK. Y. Srinivasan 	}
33903db7724SK. Y. Srinivasan }
34003db7724SK. Y. Srinivasan 
34103db7724SK. Y. Srinivasan 
34203db7724SK. Y. Srinivasan 
34303db7724SK. Y. Srinivasan 
34446a97191SGreg Kroah-Hartman static void
34546a97191SGreg Kroah-Hartman kvp_send_key(struct work_struct *dummy)
34646a97191SGreg Kroah-Hartman {
34746a97191SGreg Kroah-Hartman 	struct cn_msg *msg;
34826403354SK. Y. Srinivasan 	struct hv_kvp_msg *message;
349fa3d5b85SK. Y. Srinivasan 	struct hv_kvp_msg *in_msg;
350fa3d5b85SK. Y. Srinivasan 	__u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
351fa3d5b85SK. Y. Srinivasan 	__u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
352fa3d5b85SK. Y. Srinivasan 	__u32 val32;
353fa3d5b85SK. Y. Srinivasan 	__u64 val64;
3548d9560ebSVitaly Kuznetsov 	int rc;
35546a97191SGreg Kroah-Hartman 
356*97bf16cdSVitaly Kuznetsov 	/* The transaction state is wrong. */
357*97bf16cdSVitaly Kuznetsov 	if (kvp_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
358*97bf16cdSVitaly Kuznetsov 		return;
359*97bf16cdSVitaly Kuznetsov 
36046a97191SGreg Kroah-Hartman 	msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
361fa3d5b85SK. Y. Srinivasan 	if (!msg)
362fa3d5b85SK. Y. Srinivasan 		return;
36346a97191SGreg Kroah-Hartman 
36446a97191SGreg Kroah-Hartman 	msg->id.idx =  CN_KVP_IDX;
36546a97191SGreg Kroah-Hartman 	msg->id.val = CN_KVP_VAL;
36626403354SK. Y. Srinivasan 
36726403354SK. Y. Srinivasan 	message = (struct hv_kvp_msg *)msg->data;
368fa3d5b85SK. Y. Srinivasan 	message->kvp_hdr.operation = operation;
369fa3d5b85SK. Y. Srinivasan 	message->kvp_hdr.pool = pool;
370fa3d5b85SK. Y. Srinivasan 	in_msg = kvp_transaction.kvp_msg;
371fa3d5b85SK. Y. Srinivasan 
372fa3d5b85SK. Y. Srinivasan 	/*
373fa3d5b85SK. Y. Srinivasan 	 * The key/value strings sent from the host are encoded in
374fa3d5b85SK. Y. Srinivasan 	 * in utf16; convert it to utf8 strings.
375fa3d5b85SK. Y. Srinivasan 	 * The host assures us that the utf16 strings will not exceed
376fa3d5b85SK. Y. Srinivasan 	 * the max lengths specified. We will however, reserve room
377fa3d5b85SK. Y. Srinivasan 	 * for the string terminating character - in the utf16s_utf8s()
378fa3d5b85SK. Y. Srinivasan 	 * function we limit the size of the buffer where the converted
379fa3d5b85SK. Y. Srinivasan 	 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
380fa3d5b85SK. Y. Srinivasan 	 * that the strings can be properly terminated!
381fa3d5b85SK. Y. Srinivasan 	 */
382fa3d5b85SK. Y. Srinivasan 
383fa3d5b85SK. Y. Srinivasan 	switch (message->kvp_hdr.operation) {
38403db7724SK. Y. Srinivasan 	case KVP_OP_SET_IP_INFO:
38503db7724SK. Y. Srinivasan 		process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
38603db7724SK. Y. Srinivasan 		break;
38703db7724SK. Y. Srinivasan 	case KVP_OP_GET_IP_INFO:
38803db7724SK. Y. Srinivasan 		process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
38903db7724SK. Y. Srinivasan 		break;
390fa3d5b85SK. Y. Srinivasan 	case KVP_OP_SET:
391fa3d5b85SK. Y. Srinivasan 		switch (in_msg->body.kvp_set.data.value_type) {
392fa3d5b85SK. Y. Srinivasan 		case REG_SZ:
393fa3d5b85SK. Y. Srinivasan 			/*
394fa3d5b85SK. Y. Srinivasan 			 * The value is a string - utf16 encoding.
395fa3d5b85SK. Y. Srinivasan 			 */
396fa3d5b85SK. Y. Srinivasan 			message->body.kvp_set.data.value_size =
397fa3d5b85SK. Y. Srinivasan 				utf16s_to_utf8s(
398fa3d5b85SK. Y. Srinivasan 				(wchar_t *)in_msg->body.kvp_set.data.value,
399fa3d5b85SK. Y. Srinivasan 				in_msg->body.kvp_set.data.value_size,
400fa3d5b85SK. Y. Srinivasan 				UTF16_LITTLE_ENDIAN,
401fa3d5b85SK. Y. Srinivasan 				message->body.kvp_set.data.value,
402fa3d5b85SK. Y. Srinivasan 				HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
403fa3d5b85SK. Y. Srinivasan 				break;
404fa3d5b85SK. Y. Srinivasan 
405fa3d5b85SK. Y. Srinivasan 		case REG_U32:
406fa3d5b85SK. Y. Srinivasan 			/*
407fa3d5b85SK. Y. Srinivasan 			 * The value is a 32 bit scalar.
408fa3d5b85SK. Y. Srinivasan 			 * We save this as a utf8 string.
409fa3d5b85SK. Y. Srinivasan 			 */
410fa3d5b85SK. Y. Srinivasan 			val32 = in_msg->body.kvp_set.data.value_u32;
411fa3d5b85SK. Y. Srinivasan 			message->body.kvp_set.data.value_size =
412fa3d5b85SK. Y. Srinivasan 				sprintf(message->body.kvp_set.data.value,
413fa3d5b85SK. Y. Srinivasan 					"%d", val32) + 1;
414fa3d5b85SK. Y. Srinivasan 			break;
415fa3d5b85SK. Y. Srinivasan 
416fa3d5b85SK. Y. Srinivasan 		case REG_U64:
417fa3d5b85SK. Y. Srinivasan 			/*
418fa3d5b85SK. Y. Srinivasan 			 * The value is a 64 bit scalar.
419fa3d5b85SK. Y. Srinivasan 			 * We save this as a utf8 string.
420fa3d5b85SK. Y. Srinivasan 			 */
421fa3d5b85SK. Y. Srinivasan 			val64 = in_msg->body.kvp_set.data.value_u64;
422fa3d5b85SK. Y. Srinivasan 			message->body.kvp_set.data.value_size =
423fa3d5b85SK. Y. Srinivasan 				sprintf(message->body.kvp_set.data.value,
424fa3d5b85SK. Y. Srinivasan 					"%llu", val64) + 1;
425fa3d5b85SK. Y. Srinivasan 			break;
426fa3d5b85SK. Y. Srinivasan 
427fa3d5b85SK. Y. Srinivasan 		}
428fa3d5b85SK. Y. Srinivasan 	case KVP_OP_GET:
429fa3d5b85SK. Y. Srinivasan 		message->body.kvp_set.data.key_size =
430fa3d5b85SK. Y. Srinivasan 			utf16s_to_utf8s(
431fa3d5b85SK. Y. Srinivasan 			(wchar_t *)in_msg->body.kvp_set.data.key,
432fa3d5b85SK. Y. Srinivasan 			in_msg->body.kvp_set.data.key_size,
433fa3d5b85SK. Y. Srinivasan 			UTF16_LITTLE_ENDIAN,
434fa3d5b85SK. Y. Srinivasan 			message->body.kvp_set.data.key,
435fa3d5b85SK. Y. Srinivasan 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
436fa3d5b85SK. Y. Srinivasan 			break;
437fa3d5b85SK. Y. Srinivasan 
438fa3d5b85SK. Y. Srinivasan 	case KVP_OP_DELETE:
439fa3d5b85SK. Y. Srinivasan 		message->body.kvp_delete.key_size =
440fa3d5b85SK. Y. Srinivasan 			utf16s_to_utf8s(
441fa3d5b85SK. Y. Srinivasan 			(wchar_t *)in_msg->body.kvp_delete.key,
442fa3d5b85SK. Y. Srinivasan 			in_msg->body.kvp_delete.key_size,
443fa3d5b85SK. Y. Srinivasan 			UTF16_LITTLE_ENDIAN,
444fa3d5b85SK. Y. Srinivasan 			message->body.kvp_delete.key,
445fa3d5b85SK. Y. Srinivasan 			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
446fa3d5b85SK. Y. Srinivasan 			break;
447fa3d5b85SK. Y. Srinivasan 
448fa3d5b85SK. Y. Srinivasan 	case KVP_OP_ENUMERATE:
449fa3d5b85SK. Y. Srinivasan 		message->body.kvp_enum_data.index =
450fa3d5b85SK. Y. Srinivasan 			in_msg->body.kvp_enum_data.index;
451fa3d5b85SK. Y. Srinivasan 			break;
452fa3d5b85SK. Y. Srinivasan 	}
453fa3d5b85SK. Y. Srinivasan 
45426403354SK. Y. Srinivasan 	msg->len = sizeof(struct hv_kvp_msg);
455*97bf16cdSVitaly Kuznetsov 	kvp_transaction.state = HVUTIL_USERSPACE_REQ;
4568d9560ebSVitaly Kuznetsov 	rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
4578d9560ebSVitaly Kuznetsov 	if (rc) {
4588d9560ebSVitaly Kuznetsov 		pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
459*97bf16cdSVitaly Kuznetsov 		if (cancel_delayed_work_sync(&kvp_timeout_work)) {
4608d9560ebSVitaly Kuznetsov 			kvp_respond_to_host(message, HV_E_FAIL);
461*97bf16cdSVitaly Kuznetsov 			kvp_transaction.state = HVUTIL_READY;
462*97bf16cdSVitaly Kuznetsov 		}
4638d9560ebSVitaly Kuznetsov 	}
4648d9560ebSVitaly Kuznetsov 
46546a97191SGreg Kroah-Hartman 	kfree(msg);
466fa3d5b85SK. Y. Srinivasan 
46746a97191SGreg Kroah-Hartman 	return;
46846a97191SGreg Kroah-Hartman }
46946a97191SGreg Kroah-Hartman 
47046a97191SGreg Kroah-Hartman /*
47146a97191SGreg Kroah-Hartman  * Send a response back to the host.
47246a97191SGreg Kroah-Hartman  */
47346a97191SGreg Kroah-Hartman 
47446a97191SGreg Kroah-Hartman static void
47503db7724SK. Y. Srinivasan kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
47646a97191SGreg Kroah-Hartman {
47746a97191SGreg Kroah-Hartman 	struct hv_kvp_msg  *kvp_msg;
478fa3d5b85SK. Y. Srinivasan 	struct hv_kvp_exchg_msg_value  *kvp_data;
47946a97191SGreg Kroah-Hartman 	char	*key_name;
48003db7724SK. Y. Srinivasan 	char	*value;
48146a97191SGreg Kroah-Hartman 	struct icmsg_hdr *icmsghdrp;
482fa3d5b85SK. Y. Srinivasan 	int	keylen = 0;
483fa3d5b85SK. Y. Srinivasan 	int	valuelen = 0;
48446a97191SGreg Kroah-Hartman 	u32	buf_len;
48546a97191SGreg Kroah-Hartman 	struct vmbus_channel *channel;
48646a97191SGreg Kroah-Hartman 	u64	req_id;
48703db7724SK. Y. Srinivasan 	int ret;
48846a97191SGreg Kroah-Hartman 
48946a97191SGreg Kroah-Hartman 	/*
49046a97191SGreg Kroah-Hartman 	 * Copy the global state for completing the transaction. Note that
49146a97191SGreg Kroah-Hartman 	 * only one transaction can be active at a time.
49246a97191SGreg Kroah-Hartman 	 */
49346a97191SGreg Kroah-Hartman 
49446a97191SGreg Kroah-Hartman 	buf_len = kvp_transaction.recv_len;
49546a97191SGreg Kroah-Hartman 	channel = kvp_transaction.recv_channel;
49646a97191SGreg Kroah-Hartman 	req_id = kvp_transaction.recv_req_id;
49746a97191SGreg Kroah-Hartman 
498fa3d5b85SK. Y. Srinivasan 	icmsghdrp = (struct icmsg_hdr *)
499fa3d5b85SK. Y. Srinivasan 			&recv_buffer[sizeof(struct vmbuspipe_hdr)];
500fa3d5b85SK. Y. Srinivasan 
50146a97191SGreg Kroah-Hartman 	if (channel->onchannel_callback == NULL)
50246a97191SGreg Kroah-Hartman 		/*
50346a97191SGreg Kroah-Hartman 		 * We have raced with util driver being unloaded;
50446a97191SGreg Kroah-Hartman 		 * silently return.
50546a97191SGreg Kroah-Hartman 		 */
50646a97191SGreg Kroah-Hartman 		return;
50746a97191SGreg Kroah-Hartman 
508b47a81dcSK. Y. Srinivasan 	icmsghdrp->status = error;
50946a97191SGreg Kroah-Hartman 
51046a97191SGreg Kroah-Hartman 	/*
511adc80ae6SK. Y. Srinivasan 	 * If the error parameter is set, terminate the host's enumeration
512adc80ae6SK. Y. Srinivasan 	 * on this pool.
51346a97191SGreg Kroah-Hartman 	 */
51446a97191SGreg Kroah-Hartman 	if (error) {
51546a97191SGreg Kroah-Hartman 		/*
516b47a81dcSK. Y. Srinivasan 		 * Something failed or we have timedout;
517adc80ae6SK. Y. Srinivasan 		 * terminate the current host-side iteration.
51846a97191SGreg Kroah-Hartman 		 */
51946a97191SGreg Kroah-Hartman 		goto response_done;
52046a97191SGreg Kroah-Hartman 	}
52146a97191SGreg Kroah-Hartman 
522fa3d5b85SK. Y. Srinivasan 	kvp_msg = (struct hv_kvp_msg *)
523fa3d5b85SK. Y. Srinivasan 			&recv_buffer[sizeof(struct vmbuspipe_hdr) +
524fa3d5b85SK. Y. Srinivasan 			sizeof(struct icmsg_hdr)];
525fa3d5b85SK. Y. Srinivasan 
526fa3d5b85SK. Y. Srinivasan 	switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
52703db7724SK. Y. Srinivasan 	case KVP_OP_GET_IP_INFO:
52803db7724SK. Y. Srinivasan 		ret = process_ob_ipinfo(msg_to_host,
52903db7724SK. Y. Srinivasan 				 (struct hv_kvp_ip_msg *)kvp_msg,
53003db7724SK. Y. Srinivasan 				 KVP_OP_GET_IP_INFO);
53103db7724SK. Y. Srinivasan 		if (ret < 0)
53203db7724SK. Y. Srinivasan 			icmsghdrp->status = HV_E_FAIL;
53303db7724SK. Y. Srinivasan 
53403db7724SK. Y. Srinivasan 		goto response_done;
53503db7724SK. Y. Srinivasan 	case KVP_OP_SET_IP_INFO:
53603db7724SK. Y. Srinivasan 		goto response_done;
537fa3d5b85SK. Y. Srinivasan 	case KVP_OP_GET:
538fa3d5b85SK. Y. Srinivasan 		kvp_data = &kvp_msg->body.kvp_get.data;
539fa3d5b85SK. Y. Srinivasan 		goto copy_value;
540fa3d5b85SK. Y. Srinivasan 
541fa3d5b85SK. Y. Srinivasan 	case KVP_OP_SET:
542fa3d5b85SK. Y. Srinivasan 	case KVP_OP_DELETE:
543fa3d5b85SK. Y. Srinivasan 		goto response_done;
544fa3d5b85SK. Y. Srinivasan 
545fa3d5b85SK. Y. Srinivasan 	default:
546fa3d5b85SK. Y. Srinivasan 		break;
547fa3d5b85SK. Y. Srinivasan 	}
548fa3d5b85SK. Y. Srinivasan 
549fa3d5b85SK. Y. Srinivasan 	kvp_data = &kvp_msg->body.kvp_enum_data.data;
55003db7724SK. Y. Srinivasan 	key_name = msg_to_host->body.kvp_enum_data.data.key;
551fa3d5b85SK. Y. Srinivasan 
55246a97191SGreg Kroah-Hartman 	/*
55346a97191SGreg Kroah-Hartman 	 * The windows host expects the key/value pair to be encoded
554fa3d5b85SK. Y. Srinivasan 	 * in utf16. Ensure that the key/value size reported to the host
555fa3d5b85SK. Y. Srinivasan 	 * will be less than or equal to the MAX size (including the
556fa3d5b85SK. Y. Srinivasan 	 * terminating character).
55746a97191SGreg Kroah-Hartman 	 */
5580720a06aSAlan Stern 	keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
559fa3d5b85SK. Y. Srinivasan 				(wchar_t *) kvp_data->key,
560fa3d5b85SK. Y. Srinivasan 				(HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
561fa3d5b85SK. Y. Srinivasan 	kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
56246a97191SGreg Kroah-Hartman 
563fa3d5b85SK. Y. Srinivasan copy_value:
56403db7724SK. Y. Srinivasan 	value = msg_to_host->body.kvp_enum_data.data.value;
565fa3d5b85SK. Y. Srinivasan 	valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
566fa3d5b85SK. Y. Srinivasan 				(wchar_t *) kvp_data->value,
567fa3d5b85SK. Y. Srinivasan 				(HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
568fa3d5b85SK. Y. Srinivasan 	kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
569fa3d5b85SK. Y. Srinivasan 
570fa3d5b85SK. Y. Srinivasan 	/*
571fa3d5b85SK. Y. Srinivasan 	 * If the utf8s to utf16s conversion failed; notify host
572fa3d5b85SK. Y. Srinivasan 	 * of the error.
573fa3d5b85SK. Y. Srinivasan 	 */
574fa3d5b85SK. Y. Srinivasan 	if ((keylen < 0) || (valuelen < 0))
575fa3d5b85SK. Y. Srinivasan 		icmsghdrp->status = HV_E_FAIL;
576fa3d5b85SK. Y. Srinivasan 
577fa3d5b85SK. Y. Srinivasan 	kvp_data->value_type = REG_SZ; /* all our values are strings */
57846a97191SGreg Kroah-Hartman 
57946a97191SGreg Kroah-Hartman response_done:
58046a97191SGreg Kroah-Hartman 	icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
58146a97191SGreg Kroah-Hartman 
58246a97191SGreg Kroah-Hartman 	vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
58346a97191SGreg Kroah-Hartman 				VM_PKT_DATA_INBAND, 0);
58446a97191SGreg Kroah-Hartman }
58546a97191SGreg Kroah-Hartman 
58646a97191SGreg Kroah-Hartman /*
58746a97191SGreg Kroah-Hartman  * This callback is invoked when we get a KVP message from the host.
58846a97191SGreg Kroah-Hartman  * The host ensures that only one KVP transaction can be active at a time.
58946a97191SGreg Kroah-Hartman  * KVP implementation in Linux needs to forward the key to a user-mde
59046a97191SGreg Kroah-Hartman  * component to retrive the corresponding value. Consequently, we cannot
59146a97191SGreg Kroah-Hartman  * respond to the host in the conext of this callback. Since the host
59246a97191SGreg Kroah-Hartman  * guarantees that at most only one transaction can be active at a time,
59346a97191SGreg Kroah-Hartman  * we stash away the transaction state in a set of global variables.
59446a97191SGreg Kroah-Hartman  */
59546a97191SGreg Kroah-Hartman 
59646a97191SGreg Kroah-Hartman void hv_kvp_onchannelcallback(void *context)
59746a97191SGreg Kroah-Hartman {
59846a97191SGreg Kroah-Hartman 	struct vmbus_channel *channel = context;
59946a97191SGreg Kroah-Hartman 	u32 recvlen;
60046a97191SGreg Kroah-Hartman 	u64 requestid;
60146a97191SGreg Kroah-Hartman 
60246a97191SGreg Kroah-Hartman 	struct hv_kvp_msg *kvp_msg;
60346a97191SGreg Kroah-Hartman 
60446a97191SGreg Kroah-Hartman 	struct icmsg_hdr *icmsghdrp;
60546a97191SGreg Kroah-Hartman 	struct icmsg_negotiate *negop = NULL;
6063a491605SK. Y. Srinivasan 	int util_fw_version;
6073a491605SK. Y. Srinivasan 	int kvp_srv_version;
60846a97191SGreg Kroah-Hartman 
609*97bf16cdSVitaly Kuznetsov 	if (kvp_transaction.state > HVUTIL_READY) {
610fa3d5b85SK. Y. Srinivasan 		/*
611fa3d5b85SK. Y. Srinivasan 		 * We will defer processing this callback once
612fa3d5b85SK. Y. Srinivasan 		 * the current transaction is complete.
613fa3d5b85SK. Y. Srinivasan 		 */
614fa3d5b85SK. Y. Srinivasan 		kvp_transaction.kvp_context = context;
615fa3d5b85SK. Y. Srinivasan 		return;
616fa3d5b85SK. Y. Srinivasan 	}
6175fa97480SVitaly Kuznetsov 	kvp_transaction.kvp_context = NULL;
61846a97191SGreg Kroah-Hartman 
6199bd2d0dfSK. Y. Srinivasan 	vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
62003db7724SK. Y. Srinivasan 			 &requestid);
62146a97191SGreg Kroah-Hartman 
62246a97191SGreg Kroah-Hartman 	if (recvlen > 0) {
62346a97191SGreg Kroah-Hartman 		icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
62446a97191SGreg Kroah-Hartman 			sizeof(struct vmbuspipe_hdr)];
62546a97191SGreg Kroah-Hartman 
62646a97191SGreg Kroah-Hartman 		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
6276741335bSK. Y. Srinivasan 			/*
6283a491605SK. Y. Srinivasan 			 * Based on the host, select appropriate
6293a491605SK. Y. Srinivasan 			 * framework and service versions we will
6303a491605SK. Y. Srinivasan 			 * negotiate.
6316741335bSK. Y. Srinivasan 			 */
6323a491605SK. Y. Srinivasan 			switch (vmbus_proto_version) {
6333a491605SK. Y. Srinivasan 			case (VERSION_WS2008):
6343a491605SK. Y. Srinivasan 				util_fw_version = UTIL_WS2K8_FW_VERSION;
6353a491605SK. Y. Srinivasan 				kvp_srv_version = WS2008_SRV_VERSION;
6363a491605SK. Y. Srinivasan 				break;
6373a491605SK. Y. Srinivasan 			case (VERSION_WIN7):
6383a491605SK. Y. Srinivasan 				util_fw_version = UTIL_FW_VERSION;
6393a491605SK. Y. Srinivasan 				kvp_srv_version = WIN7_SRV_VERSION;
6403a491605SK. Y. Srinivasan 				break;
6413a491605SK. Y. Srinivasan 			default:
6423a491605SK. Y. Srinivasan 				util_fw_version = UTIL_FW_VERSION;
6433a491605SK. Y. Srinivasan 				kvp_srv_version = WIN8_SRV_VERSION;
6443a491605SK. Y. Srinivasan 			}
645c836d0abSK. Y. Srinivasan 			vmbus_prep_negotiate_resp(icmsghdrp, negop,
6463a491605SK. Y. Srinivasan 				 recv_buffer, util_fw_version,
6473a491605SK. Y. Srinivasan 				 kvp_srv_version);
6486741335bSK. Y. Srinivasan 
64946a97191SGreg Kroah-Hartman 		} else {
65046a97191SGreg Kroah-Hartman 			kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
65146a97191SGreg Kroah-Hartman 				sizeof(struct vmbuspipe_hdr) +
65246a97191SGreg Kroah-Hartman 				sizeof(struct icmsg_hdr)];
65346a97191SGreg Kroah-Hartman 
65446a97191SGreg Kroah-Hartman 			/*
65546a97191SGreg Kroah-Hartman 			 * Stash away this global state for completing the
65646a97191SGreg Kroah-Hartman 			 * transaction; note transactions are serialized.
65746a97191SGreg Kroah-Hartman 			 */
658fa3d5b85SK. Y. Srinivasan 
65946a97191SGreg Kroah-Hartman 			kvp_transaction.recv_len = recvlen;
66046a97191SGreg Kroah-Hartman 			kvp_transaction.recv_channel = channel;
66146a97191SGreg Kroah-Hartman 			kvp_transaction.recv_req_id = requestid;
662fa3d5b85SK. Y. Srinivasan 			kvp_transaction.kvp_msg = kvp_msg;
66346a97191SGreg Kroah-Hartman 
664*97bf16cdSVitaly Kuznetsov 			if (kvp_transaction.state < HVUTIL_READY) {
665*97bf16cdSVitaly Kuznetsov 				/* Userspace is not registered yet */
666*97bf16cdSVitaly Kuznetsov 				kvp_respond_to_host(NULL, HV_E_FAIL);
667*97bf16cdSVitaly Kuznetsov 				return;
668*97bf16cdSVitaly Kuznetsov 			}
669*97bf16cdSVitaly Kuznetsov 			kvp_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
670*97bf16cdSVitaly Kuznetsov 
67146a97191SGreg Kroah-Hartman 			/*
67246a97191SGreg Kroah-Hartman 			 * Get the information from the
67346a97191SGreg Kroah-Hartman 			 * user-mode component.
67446a97191SGreg Kroah-Hartman 			 * component. This transaction will be
67546a97191SGreg Kroah-Hartman 			 * completed when we get the value from
67646a97191SGreg Kroah-Hartman 			 * the user-mode component.
67746a97191SGreg Kroah-Hartman 			 * Set a timeout to deal with
67846a97191SGreg Kroah-Hartman 			 * user-mode not responding.
67946a97191SGreg Kroah-Hartman 			 */
68046a97191SGreg Kroah-Hartman 			schedule_work(&kvp_sendkey_work);
68168c8b39aSVitaly Kuznetsov 			schedule_delayed_work(&kvp_timeout_work, 5*HZ);
68246a97191SGreg Kroah-Hartman 
68346a97191SGreg Kroah-Hartman 			return;
68446a97191SGreg Kroah-Hartman 
68546a97191SGreg Kroah-Hartman 		}
68646a97191SGreg Kroah-Hartman 
68746a97191SGreg Kroah-Hartman 		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
68846a97191SGreg Kroah-Hartman 			| ICMSGHDRFLAG_RESPONSE;
68946a97191SGreg Kroah-Hartman 
69046a97191SGreg Kroah-Hartman 		vmbus_sendpacket(channel, recv_buffer,
69146a97191SGreg Kroah-Hartman 				       recvlen, requestid,
69246a97191SGreg Kroah-Hartman 				       VM_PKT_DATA_INBAND, 0);
69346a97191SGreg Kroah-Hartman 	}
69446a97191SGreg Kroah-Hartman 
69546a97191SGreg Kroah-Hartman }
69646a97191SGreg Kroah-Hartman 
69746a97191SGreg Kroah-Hartman int
69846a97191SGreg Kroah-Hartman hv_kvp_init(struct hv_util_service *srv)
69946a97191SGreg Kroah-Hartman {
70046a97191SGreg Kroah-Hartman 	int err;
70146a97191SGreg Kroah-Hartman 
70246a97191SGreg Kroah-Hartman 	err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
70346a97191SGreg Kroah-Hartman 	if (err)
70446a97191SGreg Kroah-Hartman 		return err;
70546a97191SGreg Kroah-Hartman 	recv_buffer = srv->recv_buffer;
70646a97191SGreg Kroah-Hartman 
707fa3d5b85SK. Y. Srinivasan 	/*
708fa3d5b85SK. Y. Srinivasan 	 * When this driver loads, the user level daemon that
709fa3d5b85SK. Y. Srinivasan 	 * processes the host requests may not yet be running.
710fa3d5b85SK. Y. Srinivasan 	 * Defer processing channel callbacks until the daemon
711fa3d5b85SK. Y. Srinivasan 	 * has registered.
712fa3d5b85SK. Y. Srinivasan 	 */
713*97bf16cdSVitaly Kuznetsov 	kvp_transaction.state = HVUTIL_DEVICE_INIT;
714fa3d5b85SK. Y. Srinivasan 
71546a97191SGreg Kroah-Hartman 	return 0;
71646a97191SGreg Kroah-Hartman }
71746a97191SGreg Kroah-Hartman 
71846a97191SGreg Kroah-Hartman void hv_kvp_deinit(void)
71946a97191SGreg Kroah-Hartman {
720*97bf16cdSVitaly Kuznetsov 	kvp_transaction.state = HVUTIL_DEVICE_DYING;
72146a97191SGreg Kroah-Hartman 	cn_del_callback(&kvp_id);
72268c8b39aSVitaly Kuznetsov 	cancel_delayed_work_sync(&kvp_timeout_work);
72346a97191SGreg Kroah-Hartman 	cancel_work_sync(&kvp_sendkey_work);
72446a97191SGreg Kroah-Hartman }
725