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>
30b14d749aSHimadri Pandya #include <asm/hyperv-tlfs.h>
3146a97191SGreg Kroah-Hartman
323647a83dSVitaly Kuznetsov #include "hyperv_vmbus.h"
3311bc3a5fSVitaly Kuznetsov #include "hv_utils_transport.h"
3446a97191SGreg Kroah-Hartman
356741335bSK. Y. Srinivasan /*
366741335bSK. Y. Srinivasan * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
376741335bSK. Y. Srinivasan */
383a491605SK. Y. Srinivasan #define WS2008_SRV_MAJOR 1
393a491605SK. Y. Srinivasan #define WS2008_SRV_MINOR 0
403a491605SK. Y. Srinivasan #define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
413a491605SK. Y. Srinivasan
426741335bSK. Y. Srinivasan #define WIN7_SRV_MAJOR 3
436741335bSK. Y. Srinivasan #define WIN7_SRV_MINOR 0
443a491605SK. Y. Srinivasan #define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
456741335bSK. Y. Srinivasan
466741335bSK. Y. Srinivasan #define WIN8_SRV_MAJOR 4
476741335bSK. Y. Srinivasan #define WIN8_SRV_MINOR 0
483a491605SK. Y. Srinivasan #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
4946a97191SGreg Kroah-Hartman
50a1656454SAlex Ng #define KVP_VER_COUNT 3
51a1656454SAlex Ng static const int kvp_versions[] = {
52a1656454SAlex Ng WIN8_SRV_VERSION,
53a1656454SAlex Ng WIN7_SRV_VERSION,
54a1656454SAlex Ng WS2008_SRV_VERSION
55a1656454SAlex Ng };
56a1656454SAlex Ng
57a1656454SAlex Ng #define FW_VER_COUNT 2
58a1656454SAlex Ng static const int fw_versions[] = {
59a1656454SAlex Ng UTIL_FW_VERSION,
60a1656454SAlex Ng UTIL_WS2K8_FW_VERSION
61a1656454SAlex Ng };
62a1656454SAlex Ng
6346a97191SGreg Kroah-Hartman /*
6497bf16cdSVitaly Kuznetsov * Global state maintained for transaction that is being processed. For a class
6597bf16cdSVitaly Kuznetsov * of integration services, including the "KVP service", the specified protocol
6697bf16cdSVitaly Kuznetsov * is a "request/response" protocol which means that there can only be single
6797bf16cdSVitaly Kuznetsov * outstanding transaction from the host at any given point in time. We use
6897bf16cdSVitaly Kuznetsov * this to simplify memory management in this driver - we cache and process
6997bf16cdSVitaly Kuznetsov * only one message at a time.
7046a97191SGreg Kroah-Hartman *
7197bf16cdSVitaly Kuznetsov * While the request/response protocol is guaranteed by the host, we further
7297bf16cdSVitaly Kuznetsov * ensure this by serializing packet processing in this driver - we do not
73bdc1dd47SStephen Hemminger * read additional packets from the VMBUS until the current packet is fully
7497bf16cdSVitaly Kuznetsov * handled.
7546a97191SGreg Kroah-Hartman */
7646a97191SGreg Kroah-Hartman
7746a97191SGreg Kroah-Hartman static struct {
7897bf16cdSVitaly Kuznetsov int state; /* hvutil_device_state */
7946a97191SGreg Kroah-Hartman int recv_len; /* number of bytes received. */
80fa3d5b85SK. Y. Srinivasan struct hv_kvp_msg *kvp_msg; /* current message */
8146a97191SGreg Kroah-Hartman struct vmbus_channel *recv_channel; /* chn we got the request */
8246a97191SGreg Kroah-Hartman u64 recv_req_id; /* request ID. */
8346a97191SGreg Kroah-Hartman } kvp_transaction;
8446a97191SGreg Kroah-Hartman
85b47a81dcSK. Y. Srinivasan /*
86b47a81dcSK. Y. Srinivasan * This state maintains the version number registered by the daemon.
87b47a81dcSK. Y. Srinivasan */
88b47a81dcSK. Y. Srinivasan static int dm_reg_value;
89b47a81dcSK. Y. Srinivasan
9046a97191SGreg Kroah-Hartman static void kvp_send_key(struct work_struct *dummy);
9146a97191SGreg Kroah-Hartman
9246a97191SGreg Kroah-Hartman
9303db7724SK. Y. Srinivasan static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
9468c8b39aSVitaly Kuznetsov static void kvp_timeout_func(struct work_struct *dummy);
954dbfc2e6SVitaly Kuznetsov static void kvp_host_handshake_func(struct work_struct *dummy);
96b47a81dcSK. Y. Srinivasan static void kvp_register(int);
9746a97191SGreg Kroah-Hartman
9868c8b39aSVitaly Kuznetsov static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
994dbfc2e6SVitaly Kuznetsov static DECLARE_DELAYED_WORK(kvp_host_handshake_work, kvp_host_handshake_func);
10046a97191SGreg Kroah-Hartman static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
10146a97191SGreg Kroah-Hartman
10211bc3a5fSVitaly Kuznetsov static const char kvp_devname[] = "vmbus/hv_kvp";
10346a97191SGreg Kroah-Hartman static u8 *recv_buffer;
10411bc3a5fSVitaly Kuznetsov static struct hvutil_transport *hvt;
10546a97191SGreg Kroah-Hartman /*
10646a97191SGreg Kroah-Hartman * Register the kernel component with the user-level daemon.
10746a97191SGreg Kroah-Hartman * As part of this registration, pass the LIC version number.
108cfc25993SOlaf Hering * This number has no meaning, it satisfies the registration protocol.
10946a97191SGreg Kroah-Hartman */
110cfc25993SOlaf Hering #define HV_DRV_VERSION "3.1"
11146a97191SGreg Kroah-Hartman
kvp_poll_wrapper(void * channel)1123cace4a6SOlaf Hering static void kvp_poll_wrapper(void *channel)
1133cace4a6SOlaf Hering {
1143cace4a6SOlaf Hering /* Transaction is finished, reset the state here to avoid races. */
1153cace4a6SOlaf Hering kvp_transaction.state = HVUTIL_READY;
116a3ade8ccSLong Li tasklet_schedule(&((struct vmbus_channel *)channel)->callback_event);
1173cace4a6SOlaf Hering }
1183cace4a6SOlaf Hering
kvp_register_done(void)119e0fa3e5eSVitaly Kuznetsov static void kvp_register_done(void)
120e0fa3e5eSVitaly Kuznetsov {
121e0fa3e5eSVitaly Kuznetsov /*
122e0fa3e5eSVitaly Kuznetsov * If we're still negotiating with the host cancel the timeout
123e0fa3e5eSVitaly Kuznetsov * work to not poll the channel twice.
124e0fa3e5eSVitaly Kuznetsov */
125e0fa3e5eSVitaly Kuznetsov pr_debug("KVP: userspace daemon registered\n");
126e0fa3e5eSVitaly Kuznetsov cancel_delayed_work_sync(&kvp_host_handshake_work);
127e0fa3e5eSVitaly Kuznetsov hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
128e0fa3e5eSVitaly Kuznetsov }
129e0fa3e5eSVitaly Kuznetsov
13046a97191SGreg Kroah-Hartman static void
kvp_register(int reg_value)131b47a81dcSK. Y. Srinivasan kvp_register(int reg_value)
13246a97191SGreg Kroah-Hartman {
13346a97191SGreg Kroah-Hartman
13426403354SK. Y. Srinivasan struct hv_kvp_msg *kvp_msg;
13526403354SK. Y. Srinivasan char *version;
13646a97191SGreg Kroah-Hartman
13711bc3a5fSVitaly Kuznetsov kvp_msg = kzalloc(sizeof(*kvp_msg), GFP_KERNEL);
13846a97191SGreg Kroah-Hartman
13911bc3a5fSVitaly Kuznetsov if (kvp_msg) {
140e485ceacSK. Y. Srinivasan version = kvp_msg->body.kvp_register.version;
141b47a81dcSK. Y. Srinivasan kvp_msg->kvp_hdr.operation = reg_value;
14226403354SK. Y. Srinivasan strcpy(version, HV_DRV_VERSION);
14311bc3a5fSVitaly Kuznetsov
144e0fa3e5eSVitaly Kuznetsov hvutil_transport_send(hvt, kvp_msg, sizeof(*kvp_msg),
145e0fa3e5eSVitaly Kuznetsov kvp_register_done);
14611bc3a5fSVitaly Kuznetsov kfree(kvp_msg);
14746a97191SGreg Kroah-Hartman }
14846a97191SGreg Kroah-Hartman }
14968c8b39aSVitaly Kuznetsov
kvp_timeout_func(struct work_struct * dummy)15068c8b39aSVitaly Kuznetsov static void kvp_timeout_func(struct work_struct *dummy)
15146a97191SGreg Kroah-Hartman {
15246a97191SGreg Kroah-Hartman /*
15346a97191SGreg Kroah-Hartman * If the timer fires, the user-mode component has not responded;
15446a97191SGreg Kroah-Hartman * process the pending transaction.
15546a97191SGreg Kroah-Hartman */
15603db7724SK. Y. Srinivasan kvp_respond_to_host(NULL, HV_E_FAIL);
15797bf16cdSVitaly Kuznetsov
1583cace4a6SOlaf Hering hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
15946a97191SGreg Kroah-Hartman }
16046a97191SGreg Kroah-Hartman
kvp_host_handshake_func(struct work_struct * dummy)1614dbfc2e6SVitaly Kuznetsov static void kvp_host_handshake_func(struct work_struct *dummy)
1624dbfc2e6SVitaly Kuznetsov {
163a3ade8ccSLong Li tasklet_schedule(&kvp_transaction.recv_channel->callback_event);
1644dbfc2e6SVitaly Kuznetsov }
1654dbfc2e6SVitaly Kuznetsov
kvp_handle_handshake(struct hv_kvp_msg * msg)166b47a81dcSK. Y. Srinivasan static int kvp_handle_handshake(struct hv_kvp_msg *msg)
167b47a81dcSK. Y. Srinivasan {
168b47a81dcSK. Y. Srinivasan switch (msg->kvp_hdr.operation) {
169b47a81dcSK. Y. Srinivasan case KVP_OP_REGISTER:
170b47a81dcSK. Y. Srinivasan dm_reg_value = KVP_OP_REGISTER;
171b47a81dcSK. Y. Srinivasan pr_info("KVP: IP injection functionality not available\n");
172b47a81dcSK. Y. Srinivasan pr_info("KVP: Upgrade the KVP daemon\n");
173b47a81dcSK. Y. Srinivasan break;
174b47a81dcSK. Y. Srinivasan case KVP_OP_REGISTER1:
175b47a81dcSK. Y. Srinivasan dm_reg_value = KVP_OP_REGISTER1;
176b47a81dcSK. Y. Srinivasan break;
177b47a81dcSK. Y. Srinivasan default:
178b47a81dcSK. Y. Srinivasan pr_info("KVP: incompatible daemon\n");
179b47a81dcSK. Y. Srinivasan pr_info("KVP: KVP version: %d, Daemon version: %d\n",
180b47a81dcSK. Y. Srinivasan KVP_OP_REGISTER1, msg->kvp_hdr.operation);
18111bc3a5fSVitaly Kuznetsov return -EINVAL;
182b47a81dcSK. Y. Srinivasan }
183b47a81dcSK. Y. Srinivasan
184b47a81dcSK. Y. Srinivasan /*
185b47a81dcSK. Y. Srinivasan * We have a compatible daemon; complete the handshake.
186b47a81dcSK. Y. Srinivasan */
187e0fa3e5eSVitaly Kuznetsov pr_debug("KVP: userspace daemon ver. %d connected\n",
188e0fa3e5eSVitaly Kuznetsov msg->kvp_hdr.operation);
189b47a81dcSK. Y. Srinivasan kvp_register(dm_reg_value);
1904dbfc2e6SVitaly Kuznetsov
19111bc3a5fSVitaly Kuznetsov return 0;
192b47a81dcSK. Y. Srinivasan }
193b47a81dcSK. Y. Srinivasan
194b47a81dcSK. Y. Srinivasan
19546a97191SGreg Kroah-Hartman /*
19646a97191SGreg Kroah-Hartman * Callback when data is received from user mode.
19746a97191SGreg Kroah-Hartman */
19846a97191SGreg Kroah-Hartman
kvp_on_msg(void * msg,int len)19911bc3a5fSVitaly Kuznetsov static int kvp_on_msg(void *msg, int len)
20046a97191SGreg Kroah-Hartman {
20111bc3a5fSVitaly Kuznetsov struct hv_kvp_msg *message = (struct hv_kvp_msg *)msg;
20226403354SK. Y. Srinivasan struct hv_kvp_msg_enumerate *data;
203b47a81dcSK. Y. Srinivasan int error = 0;
20446a97191SGreg Kroah-Hartman
20511bc3a5fSVitaly Kuznetsov if (len < sizeof(*message))
20611bc3a5fSVitaly Kuznetsov return -EINVAL;
207b47a81dcSK. Y. Srinivasan
208b47a81dcSK. Y. Srinivasan /*
209b47a81dcSK. Y. Srinivasan * If we are negotiating the version information
210b47a81dcSK. Y. Srinivasan * with the daemon; handle that first.
211b47a81dcSK. Y. Srinivasan */
212b47a81dcSK. Y. Srinivasan
21397bf16cdSVitaly Kuznetsov if (kvp_transaction.state < HVUTIL_READY) {
21411bc3a5fSVitaly Kuznetsov return kvp_handle_handshake(message);
215b47a81dcSK. Y. Srinivasan }
216b47a81dcSK. Y. Srinivasan
21797bf16cdSVitaly Kuznetsov /* We didn't send anything to userspace so the reply is spurious */
21897bf16cdSVitaly Kuznetsov if (kvp_transaction.state < HVUTIL_USERSPACE_REQ)
21911bc3a5fSVitaly Kuznetsov return -EINVAL;
22011bc3a5fSVitaly Kuznetsov
22197bf16cdSVitaly Kuznetsov kvp_transaction.state = HVUTIL_USERSPACE_RECV;
22297bf16cdSVitaly Kuznetsov
223b47a81dcSK. Y. Srinivasan /*
224b47a81dcSK. Y. Srinivasan * Based on the version of the daemon, we propagate errors from the
225b47a81dcSK. Y. Srinivasan * daemon differently.
226b47a81dcSK. Y. Srinivasan */
227b47a81dcSK. Y. Srinivasan
228b47a81dcSK. Y. Srinivasan data = &message->body.kvp_enum_data;
229b47a81dcSK. Y. Srinivasan
230b47a81dcSK. Y. Srinivasan switch (dm_reg_value) {
231fa3d5b85SK. Y. Srinivasan case KVP_OP_REGISTER:
232b47a81dcSK. Y. Srinivasan /*
233b47a81dcSK. Y. Srinivasan * Null string is used to pass back error condition.
234b47a81dcSK. Y. Srinivasan */
235b47a81dcSK. Y. Srinivasan if (data->data.key[0] == 0)
236b47a81dcSK. Y. Srinivasan error = HV_S_CONT;
237fa3d5b85SK. Y. Srinivasan break;
23846a97191SGreg Kroah-Hartman
239b47a81dcSK. Y. Srinivasan case KVP_OP_REGISTER1:
240b47a81dcSK. Y. Srinivasan /*
241b47a81dcSK. Y. Srinivasan * We use the message header information from
242b47a81dcSK. Y. Srinivasan * the user level daemon to transmit errors.
243b47a81dcSK. Y. Srinivasan */
244b47a81dcSK. Y. Srinivasan error = message->error;
245b47a81dcSK. Y. Srinivasan break;
246b47a81dcSK. Y. Srinivasan }
247b47a81dcSK. Y. Srinivasan
24846a97191SGreg Kroah-Hartman /*
24946a97191SGreg Kroah-Hartman * Complete the transaction by forwarding the key value
25046a97191SGreg Kroah-Hartman * to the host. But first, cancel the timeout.
25146a97191SGreg Kroah-Hartman */
25297bf16cdSVitaly Kuznetsov if (cancel_delayed_work_sync(&kvp_timeout_work)) {
25303db7724SK. Y. Srinivasan kvp_respond_to_host(message, error);
2543cace4a6SOlaf Hering hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
25597bf16cdSVitaly Kuznetsov }
25611bc3a5fSVitaly Kuznetsov
25711bc3a5fSVitaly Kuznetsov return 0;
25846a97191SGreg Kroah-Hartman }
25946a97191SGreg Kroah-Hartman
26003db7724SK. Y. Srinivasan
process_ob_ipinfo(void * in_msg,void * out_msg,int op)26103db7724SK. Y. Srinivasan static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
26203db7724SK. Y. Srinivasan {
26303db7724SK. Y. Srinivasan struct hv_kvp_msg *in = in_msg;
26403db7724SK. Y. Srinivasan struct hv_kvp_ip_msg *out = out_msg;
26503db7724SK. Y. Srinivasan int len;
26603db7724SK. Y. Srinivasan
26703db7724SK. Y. Srinivasan switch (op) {
26803db7724SK. Y. Srinivasan case KVP_OP_GET_IP_INFO:
26903db7724SK. Y. Srinivasan /*
27003db7724SK. Y. Srinivasan * Transform all parameters into utf16 encoding.
27103db7724SK. Y. Srinivasan */
27203db7724SK. Y. Srinivasan len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
27303db7724SK. Y. Srinivasan strlen((char *)in->body.kvp_ip_val.ip_addr),
27403db7724SK. Y. Srinivasan UTF16_HOST_ENDIAN,
27503db7724SK. Y. Srinivasan (wchar_t *)out->kvp_ip_val.ip_addr,
27603db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE);
27703db7724SK. Y. Srinivasan if (len < 0)
27803db7724SK. Y. Srinivasan return len;
27903db7724SK. Y. Srinivasan
28003db7724SK. Y. Srinivasan len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
28103db7724SK. Y. Srinivasan strlen((char *)in->body.kvp_ip_val.sub_net),
28203db7724SK. Y. Srinivasan UTF16_HOST_ENDIAN,
28303db7724SK. Y. Srinivasan (wchar_t *)out->kvp_ip_val.sub_net,
28403db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE);
28503db7724SK. Y. Srinivasan if (len < 0)
28603db7724SK. Y. Srinivasan return len;
28703db7724SK. Y. Srinivasan
28803db7724SK. Y. Srinivasan len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
28903db7724SK. Y. Srinivasan strlen((char *)in->body.kvp_ip_val.gate_way),
29003db7724SK. Y. Srinivasan UTF16_HOST_ENDIAN,
29103db7724SK. Y. Srinivasan (wchar_t *)out->kvp_ip_val.gate_way,
29203db7724SK. Y. Srinivasan MAX_GATEWAY_SIZE);
29303db7724SK. Y. Srinivasan if (len < 0)
29403db7724SK. Y. Srinivasan return len;
29503db7724SK. Y. Srinivasan
29603db7724SK. Y. Srinivasan len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
29703db7724SK. Y. Srinivasan strlen((char *)in->body.kvp_ip_val.dns_addr),
29803db7724SK. Y. Srinivasan UTF16_HOST_ENDIAN,
29903db7724SK. Y. Srinivasan (wchar_t *)out->kvp_ip_val.dns_addr,
30003db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE);
30103db7724SK. Y. Srinivasan if (len < 0)
30203db7724SK. Y. Srinivasan return len;
30303db7724SK. Y. Srinivasan
30403db7724SK. Y. Srinivasan len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
30503db7724SK. Y. Srinivasan strlen((char *)in->body.kvp_ip_val.adapter_id),
30603db7724SK. Y. Srinivasan UTF16_HOST_ENDIAN,
30703db7724SK. Y. Srinivasan (wchar_t *)out->kvp_ip_val.adapter_id,
308ddce54b6SAlex Ng MAX_ADAPTER_ID_SIZE);
30903db7724SK. Y. Srinivasan if (len < 0)
31003db7724SK. Y. Srinivasan return len;
31103db7724SK. Y. Srinivasan
31203db7724SK. Y. Srinivasan out->kvp_ip_val.dhcp_enabled =
31303db7724SK. Y. Srinivasan in->body.kvp_ip_val.dhcp_enabled;
314a500e0e7SK. Y. Srinivasan out->kvp_ip_val.addr_family =
315a500e0e7SK. Y. Srinivasan in->body.kvp_ip_val.addr_family;
31603db7724SK. Y. Srinivasan }
31703db7724SK. Y. Srinivasan
31803db7724SK. Y. Srinivasan return 0;
31903db7724SK. Y. Srinivasan }
32003db7724SK. Y. Srinivasan
process_ib_ipinfo(void * in_msg,void * out_msg,int op)32103db7724SK. Y. Srinivasan static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
32203db7724SK. Y. Srinivasan {
32303db7724SK. Y. Srinivasan struct hv_kvp_ip_msg *in = in_msg;
32403db7724SK. Y. Srinivasan struct hv_kvp_msg *out = out_msg;
32503db7724SK. Y. Srinivasan
32603db7724SK. Y. Srinivasan switch (op) {
32703db7724SK. Y. Srinivasan case KVP_OP_SET_IP_INFO:
32803db7724SK. Y. Srinivasan /*
32903db7724SK. Y. Srinivasan * Transform all parameters into utf8 encoding.
33003db7724SK. Y. Srinivasan */
33103db7724SK. Y. Srinivasan utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
33203db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE,
33303db7724SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
33403db7724SK. Y. Srinivasan (__u8 *)out->body.kvp_ip_val.ip_addr,
33503db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE);
33603db7724SK. Y. Srinivasan
33703db7724SK. Y. Srinivasan utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
33803db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE,
33903db7724SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
34003db7724SK. Y. Srinivasan (__u8 *)out->body.kvp_ip_val.sub_net,
34103db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE);
34203db7724SK. Y. Srinivasan
34303db7724SK. Y. Srinivasan utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
34403db7724SK. Y. Srinivasan MAX_GATEWAY_SIZE,
34503db7724SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
34603db7724SK. Y. Srinivasan (__u8 *)out->body.kvp_ip_val.gate_way,
34703db7724SK. Y. Srinivasan MAX_GATEWAY_SIZE);
34803db7724SK. Y. Srinivasan
34903db7724SK. Y. Srinivasan utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
35003db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE,
35103db7724SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
35203db7724SK. Y. Srinivasan (__u8 *)out->body.kvp_ip_val.dns_addr,
35303db7724SK. Y. Srinivasan MAX_IP_ADDR_SIZE);
35403db7724SK. Y. Srinivasan
35503db7724SK. Y. Srinivasan out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
35603db7724SK. Y. Srinivasan
357df561f66SGustavo A. R. Silva fallthrough;
358e670de54SDexuan Cui
359e670de54SDexuan Cui case KVP_OP_GET_IP_INFO:
36003db7724SK. Y. Srinivasan utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
36103db7724SK. Y. Srinivasan MAX_ADAPTER_ID_SIZE,
36203db7724SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
36303db7724SK. Y. Srinivasan (__u8 *)out->body.kvp_ip_val.adapter_id,
36403db7724SK. Y. Srinivasan MAX_ADAPTER_ID_SIZE);
36503db7724SK. Y. Srinivasan
36603db7724SK. Y. Srinivasan out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
36703db7724SK. Y. Srinivasan }
36803db7724SK. Y. Srinivasan }
36903db7724SK. Y. Srinivasan
37003db7724SK. Y. Srinivasan
37103db7724SK. Y. Srinivasan
37203db7724SK. Y. Srinivasan
37346a97191SGreg Kroah-Hartman static void
kvp_send_key(struct work_struct * dummy)37446a97191SGreg Kroah-Hartman kvp_send_key(struct work_struct *dummy)
37546a97191SGreg Kroah-Hartman {
37626403354SK. Y. Srinivasan struct hv_kvp_msg *message;
377fa3d5b85SK. Y. Srinivasan struct hv_kvp_msg *in_msg;
378fa3d5b85SK. Y. Srinivasan __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
379fa3d5b85SK. Y. Srinivasan __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
380fa3d5b85SK. Y. Srinivasan __u32 val32;
381fa3d5b85SK. Y. Srinivasan __u64 val64;
3828d9560ebSVitaly Kuznetsov int rc;
38346a97191SGreg Kroah-Hartman
38497bf16cdSVitaly Kuznetsov /* The transaction state is wrong. */
38597bf16cdSVitaly Kuznetsov if (kvp_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
38697bf16cdSVitaly Kuznetsov return;
38797bf16cdSVitaly Kuznetsov
38811bc3a5fSVitaly Kuznetsov message = kzalloc(sizeof(*message), GFP_KERNEL);
389b36fda33SVitaly Kuznetsov if (!message)
390b36fda33SVitaly Kuznetsov return;
391b36fda33SVitaly Kuznetsov
392fa3d5b85SK. Y. Srinivasan message->kvp_hdr.operation = operation;
393fa3d5b85SK. Y. Srinivasan message->kvp_hdr.pool = pool;
394fa3d5b85SK. Y. Srinivasan in_msg = kvp_transaction.kvp_msg;
395fa3d5b85SK. Y. Srinivasan
396fa3d5b85SK. Y. Srinivasan /*
39792ec746bSXiang wangx * The key/value strings sent from the host are encoded
398fa3d5b85SK. Y. Srinivasan * in utf16; convert it to utf8 strings.
399fa3d5b85SK. Y. Srinivasan * The host assures us that the utf16 strings will not exceed
400fa3d5b85SK. Y. Srinivasan * the max lengths specified. We will however, reserve room
401fa3d5b85SK. Y. Srinivasan * for the string terminating character - in the utf16s_utf8s()
402fa3d5b85SK. Y. Srinivasan * function we limit the size of the buffer where the converted
403bdc1dd47SStephen Hemminger * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to guarantee
404fa3d5b85SK. Y. Srinivasan * that the strings can be properly terminated!
405fa3d5b85SK. Y. Srinivasan */
406fa3d5b85SK. Y. Srinivasan
407fa3d5b85SK. Y. Srinivasan switch (message->kvp_hdr.operation) {
40803db7724SK. Y. Srinivasan case KVP_OP_SET_IP_INFO:
40903db7724SK. Y. Srinivasan process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
41003db7724SK. Y. Srinivasan break;
41103db7724SK. Y. Srinivasan case KVP_OP_GET_IP_INFO:
412e670de54SDexuan Cui /*
413e670de54SDexuan Cui * We only need to pass on the info of operation, adapter_id
414e670de54SDexuan Cui * and addr_family to the userland kvp daemon.
415e670de54SDexuan Cui */
416e670de54SDexuan Cui process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
41703db7724SK. Y. Srinivasan break;
418fa3d5b85SK. Y. Srinivasan case KVP_OP_SET:
419fa3d5b85SK. Y. Srinivasan switch (in_msg->body.kvp_set.data.value_type) {
420fa3d5b85SK. Y. Srinivasan case REG_SZ:
421fa3d5b85SK. Y. Srinivasan /*
422fa3d5b85SK. Y. Srinivasan * The value is a string - utf16 encoding.
423fa3d5b85SK. Y. Srinivasan */
424fa3d5b85SK. Y. Srinivasan message->body.kvp_set.data.value_size =
425fa3d5b85SK. Y. Srinivasan utf16s_to_utf8s(
426fa3d5b85SK. Y. Srinivasan (wchar_t *)in_msg->body.kvp_set.data.value,
427fa3d5b85SK. Y. Srinivasan in_msg->body.kvp_set.data.value_size,
428fa3d5b85SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
429fa3d5b85SK. Y. Srinivasan message->body.kvp_set.data.value,
430fa3d5b85SK. Y. Srinivasan HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
431fa3d5b85SK. Y. Srinivasan break;
432fa3d5b85SK. Y. Srinivasan
433fa3d5b85SK. Y. Srinivasan case REG_U32:
434fa3d5b85SK. Y. Srinivasan /*
435fa3d5b85SK. Y. Srinivasan * The value is a 32 bit scalar.
436fa3d5b85SK. Y. Srinivasan * We save this as a utf8 string.
437fa3d5b85SK. Y. Srinivasan */
438fa3d5b85SK. Y. Srinivasan val32 = in_msg->body.kvp_set.data.value_u32;
439fa3d5b85SK. Y. Srinivasan message->body.kvp_set.data.value_size =
440fa3d5b85SK. Y. Srinivasan sprintf(message->body.kvp_set.data.value,
44116d1342bSDexuan Cui "%u", val32) + 1;
442fa3d5b85SK. Y. Srinivasan break;
443fa3d5b85SK. Y. Srinivasan
444fa3d5b85SK. Y. Srinivasan case REG_U64:
445fa3d5b85SK. Y. Srinivasan /*
446fa3d5b85SK. Y. Srinivasan * The value is a 64 bit scalar.
447fa3d5b85SK. Y. Srinivasan * We save this as a utf8 string.
448fa3d5b85SK. Y. Srinivasan */
449fa3d5b85SK. Y. Srinivasan val64 = in_msg->body.kvp_set.data.value_u64;
450fa3d5b85SK. Y. Srinivasan message->body.kvp_set.data.value_size =
451fa3d5b85SK. Y. Srinivasan sprintf(message->body.kvp_set.data.value,
452fa3d5b85SK. Y. Srinivasan "%llu", val64) + 1;
453fa3d5b85SK. Y. Srinivasan break;
454fa3d5b85SK. Y. Srinivasan
455fa3d5b85SK. Y. Srinivasan }
456fc62c3b1SDexuan Cui
457e670de54SDexuan Cui /*
458e670de54SDexuan Cui * The key is always a string - utf16 encoding.
459e670de54SDexuan Cui */
460fa3d5b85SK. Y. Srinivasan message->body.kvp_set.data.key_size =
461fa3d5b85SK. Y. Srinivasan utf16s_to_utf8s(
462fa3d5b85SK. Y. Srinivasan (wchar_t *)in_msg->body.kvp_set.data.key,
463fa3d5b85SK. Y. Srinivasan in_msg->body.kvp_set.data.key_size,
464fa3d5b85SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
465fa3d5b85SK. Y. Srinivasan message->body.kvp_set.data.key,
466fa3d5b85SK. Y. Srinivasan HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
467e670de54SDexuan Cui
468e670de54SDexuan Cui break;
469e670de54SDexuan Cui
470e670de54SDexuan Cui case KVP_OP_GET:
471e670de54SDexuan Cui message->body.kvp_get.data.key_size =
472e670de54SDexuan Cui utf16s_to_utf8s(
473e670de54SDexuan Cui (wchar_t *)in_msg->body.kvp_get.data.key,
474e670de54SDexuan Cui in_msg->body.kvp_get.data.key_size,
475e670de54SDexuan Cui UTF16_LITTLE_ENDIAN,
476e670de54SDexuan Cui message->body.kvp_get.data.key,
477e670de54SDexuan Cui HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
478fa3d5b85SK. Y. Srinivasan break;
479fa3d5b85SK. Y. Srinivasan
480fa3d5b85SK. Y. Srinivasan case KVP_OP_DELETE:
481fa3d5b85SK. Y. Srinivasan message->body.kvp_delete.key_size =
482fa3d5b85SK. Y. Srinivasan utf16s_to_utf8s(
483fa3d5b85SK. Y. Srinivasan (wchar_t *)in_msg->body.kvp_delete.key,
484fa3d5b85SK. Y. Srinivasan in_msg->body.kvp_delete.key_size,
485fa3d5b85SK. Y. Srinivasan UTF16_LITTLE_ENDIAN,
486fa3d5b85SK. Y. Srinivasan message->body.kvp_delete.key,
487fa3d5b85SK. Y. Srinivasan HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
488fa3d5b85SK. Y. Srinivasan break;
489fa3d5b85SK. Y. Srinivasan
490fa3d5b85SK. Y. Srinivasan case KVP_OP_ENUMERATE:
491fa3d5b85SK. Y. Srinivasan message->body.kvp_enum_data.index =
492fa3d5b85SK. Y. Srinivasan in_msg->body.kvp_enum_data.index;
493fa3d5b85SK. Y. Srinivasan break;
494fa3d5b85SK. Y. Srinivasan }
495fa3d5b85SK. Y. Srinivasan
49697bf16cdSVitaly Kuznetsov kvp_transaction.state = HVUTIL_USERSPACE_REQ;
497e0fa3e5eSVitaly Kuznetsov rc = hvutil_transport_send(hvt, message, sizeof(*message), NULL);
4988d9560ebSVitaly Kuznetsov if (rc) {
4998d9560ebSVitaly Kuznetsov pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
50097bf16cdSVitaly Kuznetsov if (cancel_delayed_work_sync(&kvp_timeout_work)) {
5018d9560ebSVitaly Kuznetsov kvp_respond_to_host(message, HV_E_FAIL);
50297bf16cdSVitaly Kuznetsov kvp_transaction.state = HVUTIL_READY;
50397bf16cdSVitaly Kuznetsov }
5048d9560ebSVitaly Kuznetsov }
5058d9560ebSVitaly Kuznetsov
50611bc3a5fSVitaly Kuznetsov kfree(message);
50746a97191SGreg Kroah-Hartman }
50846a97191SGreg Kroah-Hartman
50946a97191SGreg Kroah-Hartman /*
51046a97191SGreg Kroah-Hartman * Send a response back to the host.
51146a97191SGreg Kroah-Hartman */
51246a97191SGreg Kroah-Hartman
51346a97191SGreg Kroah-Hartman static void
kvp_respond_to_host(struct hv_kvp_msg * msg_to_host,int error)51403db7724SK. Y. Srinivasan kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
51546a97191SGreg Kroah-Hartman {
51646a97191SGreg Kroah-Hartman struct hv_kvp_msg *kvp_msg;
517fa3d5b85SK. Y. Srinivasan struct hv_kvp_exchg_msg_value *kvp_data;
51846a97191SGreg Kroah-Hartman char *key_name;
51903db7724SK. Y. Srinivasan char *value;
52046a97191SGreg Kroah-Hartman struct icmsg_hdr *icmsghdrp;
521fa3d5b85SK. Y. Srinivasan int keylen = 0;
522fa3d5b85SK. Y. Srinivasan int valuelen = 0;
52346a97191SGreg Kroah-Hartman u32 buf_len;
52446a97191SGreg Kroah-Hartman struct vmbus_channel *channel;
52546a97191SGreg Kroah-Hartman u64 req_id;
52603db7724SK. Y. Srinivasan int ret;
52746a97191SGreg Kroah-Hartman
52846a97191SGreg Kroah-Hartman /*
52946a97191SGreg Kroah-Hartman * Copy the global state for completing the transaction. Note that
53046a97191SGreg Kroah-Hartman * only one transaction can be active at a time.
53146a97191SGreg Kroah-Hartman */
53246a97191SGreg Kroah-Hartman
53346a97191SGreg Kroah-Hartman buf_len = kvp_transaction.recv_len;
53446a97191SGreg Kroah-Hartman channel = kvp_transaction.recv_channel;
53546a97191SGreg Kroah-Hartman req_id = kvp_transaction.recv_req_id;
53646a97191SGreg Kroah-Hartman
537fa3d5b85SK. Y. Srinivasan icmsghdrp = (struct icmsg_hdr *)
538fa3d5b85SK. Y. Srinivasan &recv_buffer[sizeof(struct vmbuspipe_hdr)];
539fa3d5b85SK. Y. Srinivasan
54046a97191SGreg Kroah-Hartman if (channel->onchannel_callback == NULL)
54146a97191SGreg Kroah-Hartman /*
54246a97191SGreg Kroah-Hartman * We have raced with util driver being unloaded;
54346a97191SGreg Kroah-Hartman * silently return.
54446a97191SGreg Kroah-Hartman */
54546a97191SGreg Kroah-Hartman return;
54646a97191SGreg Kroah-Hartman
547b47a81dcSK. Y. Srinivasan icmsghdrp->status = error;
54846a97191SGreg Kroah-Hartman
54946a97191SGreg Kroah-Hartman /*
550adc80ae6SK. Y. Srinivasan * If the error parameter is set, terminate the host's enumeration
551adc80ae6SK. Y. Srinivasan * on this pool.
55246a97191SGreg Kroah-Hartman */
55346a97191SGreg Kroah-Hartman if (error) {
55446a97191SGreg Kroah-Hartman /*
555b47a81dcSK. Y. Srinivasan * Something failed or we have timed out;
556adc80ae6SK. Y. Srinivasan * terminate the current host-side iteration.
55746a97191SGreg Kroah-Hartman */
55846a97191SGreg Kroah-Hartman goto response_done;
55946a97191SGreg Kroah-Hartman }
56046a97191SGreg Kroah-Hartman
561fa3d5b85SK. Y. Srinivasan kvp_msg = (struct hv_kvp_msg *)
562fa3d5b85SK. Y. Srinivasan &recv_buffer[sizeof(struct vmbuspipe_hdr) +
563fa3d5b85SK. Y. Srinivasan sizeof(struct icmsg_hdr)];
564fa3d5b85SK. Y. Srinivasan
565fa3d5b85SK. Y. Srinivasan switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
56603db7724SK. Y. Srinivasan case KVP_OP_GET_IP_INFO:
56703db7724SK. Y. Srinivasan ret = process_ob_ipinfo(msg_to_host,
56803db7724SK. Y. Srinivasan (struct hv_kvp_ip_msg *)kvp_msg,
56903db7724SK. Y. Srinivasan KVP_OP_GET_IP_INFO);
57003db7724SK. Y. Srinivasan if (ret < 0)
57103db7724SK. Y. Srinivasan icmsghdrp->status = HV_E_FAIL;
57203db7724SK. Y. Srinivasan
57303db7724SK. Y. Srinivasan goto response_done;
57403db7724SK. Y. Srinivasan case KVP_OP_SET_IP_INFO:
57503db7724SK. Y. Srinivasan goto response_done;
576fa3d5b85SK. Y. Srinivasan case KVP_OP_GET:
577fa3d5b85SK. Y. Srinivasan kvp_data = &kvp_msg->body.kvp_get.data;
578fa3d5b85SK. Y. Srinivasan goto copy_value;
579fa3d5b85SK. Y. Srinivasan
580fa3d5b85SK. Y. Srinivasan case KVP_OP_SET:
581fa3d5b85SK. Y. Srinivasan case KVP_OP_DELETE:
582fa3d5b85SK. Y. Srinivasan goto response_done;
583fa3d5b85SK. Y. Srinivasan
584fa3d5b85SK. Y. Srinivasan default:
585fa3d5b85SK. Y. Srinivasan break;
586fa3d5b85SK. Y. Srinivasan }
587fa3d5b85SK. Y. Srinivasan
588fa3d5b85SK. Y. Srinivasan kvp_data = &kvp_msg->body.kvp_enum_data.data;
58903db7724SK. Y. Srinivasan key_name = msg_to_host->body.kvp_enum_data.data.key;
590fa3d5b85SK. Y. Srinivasan
59146a97191SGreg Kroah-Hartman /*
59246a97191SGreg Kroah-Hartman * The windows host expects the key/value pair to be encoded
593fa3d5b85SK. Y. Srinivasan * in utf16. Ensure that the key/value size reported to the host
594fa3d5b85SK. Y. Srinivasan * will be less than or equal to the MAX size (including the
595fa3d5b85SK. Y. Srinivasan * terminating character).
59646a97191SGreg Kroah-Hartman */
5970720a06aSAlan Stern keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
598fa3d5b85SK. Y. Srinivasan (wchar_t *) kvp_data->key,
599fa3d5b85SK. Y. Srinivasan (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
600fa3d5b85SK. Y. Srinivasan kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
60146a97191SGreg Kroah-Hartman
602fa3d5b85SK. Y. Srinivasan copy_value:
60303db7724SK. Y. Srinivasan value = msg_to_host->body.kvp_enum_data.data.value;
604fa3d5b85SK. Y. Srinivasan valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
605fa3d5b85SK. Y. Srinivasan (wchar_t *) kvp_data->value,
606fa3d5b85SK. Y. Srinivasan (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
607fa3d5b85SK. Y. Srinivasan kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
608fa3d5b85SK. Y. Srinivasan
609fa3d5b85SK. Y. Srinivasan /*
610fa3d5b85SK. Y. Srinivasan * If the utf8s to utf16s conversion failed; notify host
611fa3d5b85SK. Y. Srinivasan * of the error.
612fa3d5b85SK. Y. Srinivasan */
613fa3d5b85SK. Y. Srinivasan if ((keylen < 0) || (valuelen < 0))
614fa3d5b85SK. Y. Srinivasan icmsghdrp->status = HV_E_FAIL;
615fa3d5b85SK. Y. Srinivasan
616fa3d5b85SK. Y. Srinivasan kvp_data->value_type = REG_SZ; /* all our values are strings */
61746a97191SGreg Kroah-Hartman
61846a97191SGreg Kroah-Hartman response_done:
61946a97191SGreg Kroah-Hartman icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
62046a97191SGreg Kroah-Hartman
62146a97191SGreg Kroah-Hartman vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
62246a97191SGreg Kroah-Hartman VM_PKT_DATA_INBAND, 0);
62346a97191SGreg Kroah-Hartman }
62446a97191SGreg Kroah-Hartman
62546a97191SGreg Kroah-Hartman /*
62646a97191SGreg Kroah-Hartman * This callback is invoked when we get a KVP message from the host.
62746a97191SGreg Kroah-Hartman * The host ensures that only one KVP transaction can be active at a time.
62846a97191SGreg Kroah-Hartman * KVP implementation in Linux needs to forward the key to a user-mde
629bdc1dd47SStephen Hemminger * component to retrieve the corresponding value. Consequently, we cannot
630bdc1dd47SStephen Hemminger * respond to the host in the context of this callback. Since the host
63146a97191SGreg Kroah-Hartman * guarantees that at most only one transaction can be active at a time,
63246a97191SGreg Kroah-Hartman * we stash away the transaction state in a set of global variables.
63346a97191SGreg Kroah-Hartman */
63446a97191SGreg Kroah-Hartman
hv_kvp_onchannelcallback(void * context)63546a97191SGreg Kroah-Hartman void hv_kvp_onchannelcallback(void *context)
63646a97191SGreg Kroah-Hartman {
63746a97191SGreg Kroah-Hartman struct vmbus_channel *channel = context;
63846a97191SGreg Kroah-Hartman u32 recvlen;
63946a97191SGreg Kroah-Hartman u64 requestid;
64046a97191SGreg Kroah-Hartman
64146a97191SGreg Kroah-Hartman struct hv_kvp_msg *kvp_msg;
64246a97191SGreg Kroah-Hartman
64346a97191SGreg Kroah-Hartman struct icmsg_hdr *icmsghdrp;
6443a491605SK. Y. Srinivasan int kvp_srv_version;
6454dbfc2e6SVitaly Kuznetsov static enum {NEGO_NOT_STARTED,
6464dbfc2e6SVitaly Kuznetsov NEGO_IN_PROGRESS,
6474dbfc2e6SVitaly Kuznetsov NEGO_FINISHED} host_negotiatied = NEGO_NOT_STARTED;
64846a97191SGreg Kroah-Hartman
649a3ade8ccSLong Li if (kvp_transaction.state < HVUTIL_READY) {
6504dbfc2e6SVitaly Kuznetsov /*
6514dbfc2e6SVitaly Kuznetsov * If userspace daemon is not connected and host is asking
6524dbfc2e6SVitaly Kuznetsov * us to negotiate we need to delay to not lose messages.
6534dbfc2e6SVitaly Kuznetsov * This is important for Failover IP setting.
6544dbfc2e6SVitaly Kuznetsov */
655a3ade8ccSLong Li if (host_negotiatied == NEGO_NOT_STARTED) {
6564dbfc2e6SVitaly Kuznetsov host_negotiatied = NEGO_IN_PROGRESS;
6574dbfc2e6SVitaly Kuznetsov schedule_delayed_work(&kvp_host_handshake_work,
6584dbfc2e6SVitaly Kuznetsov HV_UTIL_NEGO_TIMEOUT * HZ);
659a3ade8ccSLong Li }
6604dbfc2e6SVitaly Kuznetsov return;
6614dbfc2e6SVitaly Kuznetsov }
6623cace4a6SOlaf Hering if (kvp_transaction.state > HVUTIL_READY)
663fa3d5b85SK. Y. Srinivasan return;
66446a97191SGreg Kroah-Hartman
66506caa778SAndres Beltran if (vmbus_recvpacket(channel, recv_buffer, HV_HYP_PAGE_SIZE * 4, &recvlen, &requestid)) {
66606caa778SAndres Beltran pr_err_ratelimited("KVP request received. Could not read into recv buf\n");
66706caa778SAndres Beltran return;
66806caa778SAndres Beltran }
66946a97191SGreg Kroah-Hartman
67006caa778SAndres Beltran if (!recvlen)
67106caa778SAndres Beltran return;
67206caa778SAndres Beltran
67306caa778SAndres Beltran /* Ensure recvlen is big enough to read header data */
67406caa778SAndres Beltran if (recvlen < ICMSG_HDR) {
67506caa778SAndres Beltran pr_err_ratelimited("KVP request received. Packet length too small: %d\n",
67606caa778SAndres Beltran recvlen);
67706caa778SAndres Beltran return;
67806caa778SAndres Beltran }
67906caa778SAndres Beltran
68006caa778SAndres Beltran icmsghdrp = (struct icmsg_hdr *)&recv_buffer[sizeof(struct vmbuspipe_hdr)];
68146a97191SGreg Kroah-Hartman
68246a97191SGreg Kroah-Hartman if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
6831274a690SAlex Ng if (vmbus_prep_negotiate_resp(icmsghdrp,
68406caa778SAndres Beltran recv_buffer, recvlen,
68506caa778SAndres Beltran fw_versions, FW_VER_COUNT,
686a1656454SAlex Ng kvp_versions, KVP_VER_COUNT,
6871274a690SAlex Ng NULL, &kvp_srv_version)) {
6881274a690SAlex Ng pr_info("KVP IC version %d.%d\n",
6891274a690SAlex Ng kvp_srv_version >> 16,
6901274a690SAlex Ng kvp_srv_version & 0xFFFF);
6911274a690SAlex Ng }
69206caa778SAndres Beltran } else if (icmsghdrp->icmsgtype == ICMSGTYPE_KVPEXCHANGE) {
69306caa778SAndres Beltran /*
69406caa778SAndres Beltran * recvlen is not checked against sizeof(struct kvp_msg) because kvp_msg contains
69506caa778SAndres Beltran * a union of structs and the msg type received is not known. Code using this
69606caa778SAndres Beltran * struct should provide validation when accessing its fields.
69706caa778SAndres Beltran */
69806caa778SAndres Beltran kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ICMSG_HDR];
69946a97191SGreg Kroah-Hartman
70046a97191SGreg Kroah-Hartman /*
70146a97191SGreg Kroah-Hartman * Stash away this global state for completing the
70246a97191SGreg Kroah-Hartman * transaction; note transactions are serialized.
70346a97191SGreg Kroah-Hartman */
704fa3d5b85SK. Y. Srinivasan
70546a97191SGreg Kroah-Hartman kvp_transaction.recv_len = recvlen;
70646a97191SGreg Kroah-Hartman kvp_transaction.recv_req_id = requestid;
707fa3d5b85SK. Y. Srinivasan kvp_transaction.kvp_msg = kvp_msg;
70846a97191SGreg Kroah-Hartman
70997bf16cdSVitaly Kuznetsov if (kvp_transaction.state < HVUTIL_READY) {
71097bf16cdSVitaly Kuznetsov /* Userspace is not registered yet */
71197bf16cdSVitaly Kuznetsov kvp_respond_to_host(NULL, HV_E_FAIL);
71297bf16cdSVitaly Kuznetsov return;
71397bf16cdSVitaly Kuznetsov }
71497bf16cdSVitaly Kuznetsov kvp_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
71597bf16cdSVitaly Kuznetsov
71646a97191SGreg Kroah-Hartman /*
71746a97191SGreg Kroah-Hartman * Get the information from the
71846a97191SGreg Kroah-Hartman * user-mode component.
71946a97191SGreg Kroah-Hartman * component. This transaction will be
72046a97191SGreg Kroah-Hartman * completed when we get the value from
72146a97191SGreg Kroah-Hartman * the user-mode component.
72246a97191SGreg Kroah-Hartman * Set a timeout to deal with
72346a97191SGreg Kroah-Hartman * user-mode not responding.
72446a97191SGreg Kroah-Hartman */
72546a97191SGreg Kroah-Hartman schedule_work(&kvp_sendkey_work);
726c0b200cfSK. Y. Srinivasan schedule_delayed_work(&kvp_timeout_work,
727c0b200cfSK. Y. Srinivasan HV_UTIL_TIMEOUT * HZ);
72846a97191SGreg Kroah-Hartman
72946a97191SGreg Kroah-Hartman return;
73046a97191SGreg Kroah-Hartman
73106caa778SAndres Beltran } else {
73206caa778SAndres Beltran pr_err_ratelimited("KVP request received. Invalid msg type: %d\n",
73306caa778SAndres Beltran icmsghdrp->icmsgtype);
73406caa778SAndres Beltran return;
73546a97191SGreg Kroah-Hartman }
73646a97191SGreg Kroah-Hartman
73746a97191SGreg Kroah-Hartman icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
73846a97191SGreg Kroah-Hartman | ICMSGHDRFLAG_RESPONSE;
73946a97191SGreg Kroah-Hartman
74046a97191SGreg Kroah-Hartman vmbus_sendpacket(channel, recv_buffer,
74146a97191SGreg Kroah-Hartman recvlen, requestid,
74246a97191SGreg Kroah-Hartman VM_PKT_DATA_INBAND, 0);
7434dbfc2e6SVitaly Kuznetsov
7444dbfc2e6SVitaly Kuznetsov host_negotiatied = NEGO_FINISHED;
745a3ade8ccSLong Li hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
74646a97191SGreg Kroah-Hartman }
74746a97191SGreg Kroah-Hartman
kvp_on_reset(void)74811bc3a5fSVitaly Kuznetsov static void kvp_on_reset(void)
74911bc3a5fSVitaly Kuznetsov {
75011bc3a5fSVitaly Kuznetsov if (cancel_delayed_work_sync(&kvp_timeout_work))
75111bc3a5fSVitaly Kuznetsov kvp_respond_to_host(NULL, HV_E_FAIL);
75211bc3a5fSVitaly Kuznetsov kvp_transaction.state = HVUTIL_DEVICE_INIT;
75311bc3a5fSVitaly Kuznetsov }
75411bc3a5fSVitaly Kuznetsov
75546a97191SGreg Kroah-Hartman int
hv_kvp_init(struct hv_util_service * srv)75646a97191SGreg Kroah-Hartman hv_kvp_init(struct hv_util_service *srv)
75746a97191SGreg Kroah-Hartman {
75846a97191SGreg Kroah-Hartman recv_buffer = srv->recv_buffer;
759b9830d12SK. Y. Srinivasan kvp_transaction.recv_channel = srv->channel;
760adae1e93SAndres Beltran kvp_transaction.recv_channel->max_pkt_size = HV_HYP_PAGE_SIZE * 4;
76146a97191SGreg Kroah-Hartman
762fa3d5b85SK. Y. Srinivasan /*
763fa3d5b85SK. Y. Srinivasan * When this driver loads, the user level daemon that
764fa3d5b85SK. Y. Srinivasan * processes the host requests may not yet be running.
765fa3d5b85SK. Y. Srinivasan * Defer processing channel callbacks until the daemon
766fa3d5b85SK. Y. Srinivasan * has registered.
767fa3d5b85SK. Y. Srinivasan */
76897bf16cdSVitaly Kuznetsov kvp_transaction.state = HVUTIL_DEVICE_INIT;
769fa3d5b85SK. Y. Srinivasan
770*89fcec5eSMichael Kelley return 0;
771*89fcec5eSMichael Kelley }
772*89fcec5eSMichael Kelley
773*89fcec5eSMichael Kelley int
hv_kvp_init_transport(void)774*89fcec5eSMichael Kelley hv_kvp_init_transport(void)
775*89fcec5eSMichael Kelley {
77611bc3a5fSVitaly Kuznetsov hvt = hvutil_transport_init(kvp_devname, CN_KVP_IDX, CN_KVP_VAL,
77711bc3a5fSVitaly Kuznetsov kvp_on_msg, kvp_on_reset);
77811bc3a5fSVitaly Kuznetsov if (!hvt)
77911bc3a5fSVitaly Kuznetsov return -EFAULT;
78011bc3a5fSVitaly Kuznetsov
78146a97191SGreg Kroah-Hartman return 0;
78246a97191SGreg Kroah-Hartman }
78346a97191SGreg Kroah-Hartman
hv_kvp_cancel_work(void)78454e19d34SDexuan Cui static void hv_kvp_cancel_work(void)
78546a97191SGreg Kroah-Hartman {
7864dbfc2e6SVitaly Kuznetsov cancel_delayed_work_sync(&kvp_host_handshake_work);
78768c8b39aSVitaly Kuznetsov cancel_delayed_work_sync(&kvp_timeout_work);
78846a97191SGreg Kroah-Hartman cancel_work_sync(&kvp_sendkey_work);
78954e19d34SDexuan Cui }
79054e19d34SDexuan Cui
hv_kvp_pre_suspend(void)79154e19d34SDexuan Cui int hv_kvp_pre_suspend(void)
79254e19d34SDexuan Cui {
79354e19d34SDexuan Cui struct vmbus_channel *channel = kvp_transaction.recv_channel;
79454e19d34SDexuan Cui
79554e19d34SDexuan Cui tasklet_disable(&channel->callback_event);
79654e19d34SDexuan Cui
79754e19d34SDexuan Cui /*
79854e19d34SDexuan Cui * If there is a pending transtion, it's unnecessary to tell the host
79954e19d34SDexuan Cui * that the transaction will fail, because that is implied when
80054e19d34SDexuan Cui * util_suspend() calls vmbus_close() later.
80154e19d34SDexuan Cui */
80254e19d34SDexuan Cui hv_kvp_cancel_work();
80354e19d34SDexuan Cui
80454e19d34SDexuan Cui /*
80554e19d34SDexuan Cui * Forece the state to READY to handle the ICMSGTYPE_NEGOTIATE message
80654e19d34SDexuan Cui * later. The user space daemon may go out of order and its write()
80754e19d34SDexuan Cui * may fail with EINVAL: this doesn't matter since the daemon will
80854e19d34SDexuan Cui * reset the device by closing and re-opening it.
80954e19d34SDexuan Cui */
81054e19d34SDexuan Cui kvp_transaction.state = HVUTIL_READY;
81154e19d34SDexuan Cui return 0;
81254e19d34SDexuan Cui }
81354e19d34SDexuan Cui
hv_kvp_pre_resume(void)81454e19d34SDexuan Cui int hv_kvp_pre_resume(void)
81554e19d34SDexuan Cui {
81654e19d34SDexuan Cui struct vmbus_channel *channel = kvp_transaction.recv_channel;
81754e19d34SDexuan Cui
81854e19d34SDexuan Cui tasklet_enable(&channel->callback_event);
81954e19d34SDexuan Cui
82054e19d34SDexuan Cui return 0;
82154e19d34SDexuan Cui }
82254e19d34SDexuan Cui
hv_kvp_deinit(void)82354e19d34SDexuan Cui void hv_kvp_deinit(void)
82454e19d34SDexuan Cui {
82554e19d34SDexuan Cui kvp_transaction.state = HVUTIL_DEVICE_DYING;
82654e19d34SDexuan Cui
82754e19d34SDexuan Cui hv_kvp_cancel_work();
82854e19d34SDexuan Cui
82911bc3a5fSVitaly Kuznetsov hvutil_transport_destroy(hvt);
83046a97191SGreg Kroah-Hartman }
831