blob: beb8105c0e7bace32769198fd9f253c59d2fa543 [file] [log] [blame]
Ky Srinivasan245ba562010-12-16 18:54:16 -07001/*
2 * An implementation of key value pair (KVP) functionality for Linux.
3 *
4 *
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
16 * details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Ky Srinivasan245ba562010-12-16 18:54:16 -070024
25#include <linux/net.h>
26#include <linux/nls.h>
27#include <linux/connector.h>
28#include <linux/workqueue.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
Ky Srinivasan245ba562010-12-16 18:54:16 -070030
Ky Srinivasan245ba562010-12-16 18:54:16 -070031
K. Y. Srinivasan67413352013-07-02 10:31:30 -070032/*
33 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
34 */
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070035#define WS2008_SRV_MAJOR 1
36#define WS2008_SRV_MINOR 0
37#define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
38
K. Y. Srinivasan67413352013-07-02 10:31:30 -070039#define WIN7_SRV_MAJOR 3
40#define WIN7_SRV_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070041#define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
K. Y. Srinivasan67413352013-07-02 10:31:30 -070042
43#define WIN8_SRV_MAJOR 4
44#define WIN8_SRV_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070045#define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
Ky Srinivasan245ba562010-12-16 18:54:16 -070046
47/*
48 * Global state maintained for transaction that is being processed.
49 * Note that only one transaction can be active at any point in time.
50 *
51 * This state is set when we receive a request from the host; we
52 * cleanup this state when the transaction is completed - when we respond
53 * to the host with the key value.
54 */
55
56static struct {
57 bool active; /* transaction status - active or not */
58 int recv_len; /* number of bytes received. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070059 struct hv_kvp_msg *kvp_msg; /* current message */
Ky Srinivasan245ba562010-12-16 18:54:16 -070060 struct vmbus_channel *recv_channel; /* chn we got the request */
61 u64 recv_req_id; /* request ID. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070062 void *kvp_context; /* for the channel callback */
Ky Srinivasan245ba562010-12-16 18:54:16 -070063} kvp_transaction;
64
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070065/*
66 * Before we can accept KVP messages from the host, we need
67 * to handshake with the user level daemon. This state tracks
68 * if we are in the handshake phase.
69 */
70static bool in_hand_shake = true;
71
72/*
73 * This state maintains the version number registered by the daemon.
74 */
75static int dm_reg_value;
76
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070077static void kvp_send_key(struct work_struct *dummy);
Ky Srinivasan245ba562010-12-16 18:54:16 -070078
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -070079
K. Y. Srinivasan03db7722012-08-16 18:32:12 -070080static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
Ky Srinivasan245ba562010-12-16 18:54:16 -070081static void kvp_work_func(struct work_struct *dummy);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070082static void kvp_register(int);
Ky Srinivasan245ba562010-12-16 18:54:16 -070083
84static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070085static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
Ky Srinivasan245ba562010-12-16 18:54:16 -070086
87static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
88static const char kvp_name[] = "kvp_kernel_module";
Ky Srinivasan245ba562010-12-16 18:54:16 -070089static u8 *recv_buffer;
90/*
91 * Register the kernel component with the user-level daemon.
92 * As part of this registration, pass the LIC version number.
Olaf Heringcfc25992013-05-29 11:29:07 +020093 * This number has no meaning, it satisfies the registration protocol.
Ky Srinivasan245ba562010-12-16 18:54:16 -070094 */
Olaf Heringcfc25992013-05-29 11:29:07 +020095#define HV_DRV_VERSION "3.1"
Ky Srinivasan245ba562010-12-16 18:54:16 -070096
97static void
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070098kvp_register(int reg_value)
Ky Srinivasan245ba562010-12-16 18:54:16 -070099{
100
101 struct cn_msg *msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800102 struct hv_kvp_msg *kvp_msg;
103 char *version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700104
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800105 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700106
107 if (msg) {
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800108 kvp_msg = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800109 version = kvp_msg->body.kvp_register.version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700110 msg->id.idx = CN_KVP_IDX;
111 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800112
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700113 kvp_msg->kvp_hdr.operation = reg_value;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800114 strcpy(version, HV_DRV_VERSION);
115 msg->len = sizeof(struct hv_kvp_msg);
David Friesac8f7332014-01-15 22:29:19 -0600116 cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700117 kfree(msg);
118 }
119}
120static void
121kvp_work_func(struct work_struct *dummy)
122{
123 /*
124 * If the timer fires, the user-mode component has not responded;
125 * process the pending transaction.
126 */
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700127 kvp_respond_to_host(NULL, HV_E_FAIL);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700128}
129
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700130static void poll_channel(struct vmbus_channel *channel)
131{
132 if (channel->target_cpu != smp_processor_id())
133 smp_call_function_single(channel->target_cpu,
134 hv_kvp_onchannelcallback,
135 channel, true);
136 else
137 hv_kvp_onchannelcallback(channel);
138}
139
140
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700141static int kvp_handle_handshake(struct hv_kvp_msg *msg)
142{
143 int ret = 1;
144
145 switch (msg->kvp_hdr.operation) {
146 case KVP_OP_REGISTER:
147 dm_reg_value = KVP_OP_REGISTER;
148 pr_info("KVP: IP injection functionality not available\n");
149 pr_info("KVP: Upgrade the KVP daemon\n");
150 break;
151 case KVP_OP_REGISTER1:
152 dm_reg_value = KVP_OP_REGISTER1;
153 break;
154 default:
155 pr_info("KVP: incompatible daemon\n");
156 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
157 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
158 ret = 0;
159 }
160
161 if (ret) {
162 /*
163 * We have a compatible daemon; complete the handshake.
164 */
165 pr_info("KVP: user-mode registering done.\n");
166 kvp_register(dm_reg_value);
167 kvp_transaction.active = false;
168 if (kvp_transaction.kvp_context)
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700169 poll_channel(kvp_transaction.kvp_context);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700170 }
171 return ret;
172}
173
174
Ky Srinivasan245ba562010-12-16 18:54:16 -0700175/*
176 * Callback when data is received from user mode.
177 */
178
179static void
180kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
181{
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800182 struct hv_kvp_msg *message;
183 struct hv_kvp_msg_enumerate *data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700184 int error = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700185
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800186 message = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700187
188 /*
189 * If we are negotiating the version information
190 * with the daemon; handle that first.
191 */
192
193 if (in_hand_shake) {
194 if (kvp_handle_handshake(message))
195 in_hand_shake = false;
196 return;
197 }
198
199 /*
200 * Based on the version of the daemon, we propagate errors from the
201 * daemon differently.
202 */
203
204 data = &message->body.kvp_enum_data;
205
206 switch (dm_reg_value) {
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700207 case KVP_OP_REGISTER:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700208 /*
209 * Null string is used to pass back error condition.
210 */
211 if (data->data.key[0] == 0)
212 error = HV_S_CONT;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700213 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700214
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700215 case KVP_OP_REGISTER1:
Ky Srinivasan245ba562010-12-16 18:54:16 -0700216 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700217 * We use the message header information from
218 * the user level daemon to transmit errors.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700219 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700220 error = message->error;
221 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700222 }
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700223
224 /*
225 * Complete the transaction by forwarding the key value
226 * to the host. But first, cancel the timeout.
227 */
228 if (cancel_delayed_work_sync(&kvp_work))
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700229 kvp_respond_to_host(message, error);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700230}
231
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700232
233static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
234{
235 struct hv_kvp_msg *in = in_msg;
236 struct hv_kvp_ip_msg *out = out_msg;
237 int len;
238
239 switch (op) {
240 case KVP_OP_GET_IP_INFO:
241 /*
242 * Transform all parameters into utf16 encoding.
243 */
244 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
245 strlen((char *)in->body.kvp_ip_val.ip_addr),
246 UTF16_HOST_ENDIAN,
247 (wchar_t *)out->kvp_ip_val.ip_addr,
248 MAX_IP_ADDR_SIZE);
249 if (len < 0)
250 return len;
251
252 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
253 strlen((char *)in->body.kvp_ip_val.sub_net),
254 UTF16_HOST_ENDIAN,
255 (wchar_t *)out->kvp_ip_val.sub_net,
256 MAX_IP_ADDR_SIZE);
257 if (len < 0)
258 return len;
259
260 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
261 strlen((char *)in->body.kvp_ip_val.gate_way),
262 UTF16_HOST_ENDIAN,
263 (wchar_t *)out->kvp_ip_val.gate_way,
264 MAX_GATEWAY_SIZE);
265 if (len < 0)
266 return len;
267
268 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
269 strlen((char *)in->body.kvp_ip_val.dns_addr),
270 UTF16_HOST_ENDIAN,
271 (wchar_t *)out->kvp_ip_val.dns_addr,
272 MAX_IP_ADDR_SIZE);
273 if (len < 0)
274 return len;
275
276 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
277 strlen((char *)in->body.kvp_ip_val.adapter_id),
278 UTF16_HOST_ENDIAN,
279 (wchar_t *)out->kvp_ip_val.adapter_id,
280 MAX_IP_ADDR_SIZE);
281 if (len < 0)
282 return len;
283
284 out->kvp_ip_val.dhcp_enabled =
285 in->body.kvp_ip_val.dhcp_enabled;
K. Y. Srinivasana500e0e2012-09-04 17:54:13 -0700286 out->kvp_ip_val.addr_family =
287 in->body.kvp_ip_val.addr_family;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700288 }
289
290 return 0;
291}
292
293static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
294{
295 struct hv_kvp_ip_msg *in = in_msg;
296 struct hv_kvp_msg *out = out_msg;
297
298 switch (op) {
299 case KVP_OP_SET_IP_INFO:
300 /*
301 * Transform all parameters into utf8 encoding.
302 */
303 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
304 MAX_IP_ADDR_SIZE,
305 UTF16_LITTLE_ENDIAN,
306 (__u8 *)out->body.kvp_ip_val.ip_addr,
307 MAX_IP_ADDR_SIZE);
308
309 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
310 MAX_IP_ADDR_SIZE,
311 UTF16_LITTLE_ENDIAN,
312 (__u8 *)out->body.kvp_ip_val.sub_net,
313 MAX_IP_ADDR_SIZE);
314
315 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
316 MAX_GATEWAY_SIZE,
317 UTF16_LITTLE_ENDIAN,
318 (__u8 *)out->body.kvp_ip_val.gate_way,
319 MAX_GATEWAY_SIZE);
320
321 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
322 MAX_IP_ADDR_SIZE,
323 UTF16_LITTLE_ENDIAN,
324 (__u8 *)out->body.kvp_ip_val.dns_addr,
325 MAX_IP_ADDR_SIZE);
326
327 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
328
329 default:
330 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
331 MAX_ADAPTER_ID_SIZE,
332 UTF16_LITTLE_ENDIAN,
333 (__u8 *)out->body.kvp_ip_val.adapter_id,
334 MAX_ADAPTER_ID_SIZE);
335
336 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
337 }
338}
339
340
341
342
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700343static void
344kvp_send_key(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700345{
346 struct cn_msg *msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800347 struct hv_kvp_msg *message;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700348 struct hv_kvp_msg *in_msg;
349 __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
350 __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
351 __u32 val32;
352 __u64 val64;
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100353 int rc;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700354
355 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700356 if (!msg)
357 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700358
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700359 msg->id.idx = CN_KVP_IDX;
360 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800361
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700362 message = (struct hv_kvp_msg *)msg->data;
363 message->kvp_hdr.operation = operation;
364 message->kvp_hdr.pool = pool;
365 in_msg = kvp_transaction.kvp_msg;
366
367 /*
368 * The key/value strings sent from the host are encoded in
369 * in utf16; convert it to utf8 strings.
370 * The host assures us that the utf16 strings will not exceed
371 * the max lengths specified. We will however, reserve room
372 * for the string terminating character - in the utf16s_utf8s()
373 * function we limit the size of the buffer where the converted
374 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
375 * that the strings can be properly terminated!
376 */
377
378 switch (message->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700379 case KVP_OP_SET_IP_INFO:
380 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
381 break;
382 case KVP_OP_GET_IP_INFO:
383 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
384 break;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700385 case KVP_OP_SET:
386 switch (in_msg->body.kvp_set.data.value_type) {
387 case REG_SZ:
388 /*
389 * The value is a string - utf16 encoding.
390 */
391 message->body.kvp_set.data.value_size =
392 utf16s_to_utf8s(
393 (wchar_t *)in_msg->body.kvp_set.data.value,
394 in_msg->body.kvp_set.data.value_size,
395 UTF16_LITTLE_ENDIAN,
396 message->body.kvp_set.data.value,
397 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
398 break;
399
400 case REG_U32:
401 /*
402 * The value is a 32 bit scalar.
403 * We save this as a utf8 string.
404 */
405 val32 = in_msg->body.kvp_set.data.value_u32;
406 message->body.kvp_set.data.value_size =
407 sprintf(message->body.kvp_set.data.value,
408 "%d", val32) + 1;
409 break;
410
411 case REG_U64:
412 /*
413 * The value is a 64 bit scalar.
414 * We save this as a utf8 string.
415 */
416 val64 = in_msg->body.kvp_set.data.value_u64;
417 message->body.kvp_set.data.value_size =
418 sprintf(message->body.kvp_set.data.value,
419 "%llu", val64) + 1;
420 break;
421
422 }
423 case KVP_OP_GET:
424 message->body.kvp_set.data.key_size =
425 utf16s_to_utf8s(
426 (wchar_t *)in_msg->body.kvp_set.data.key,
427 in_msg->body.kvp_set.data.key_size,
428 UTF16_LITTLE_ENDIAN,
429 message->body.kvp_set.data.key,
430 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
431 break;
432
433 case KVP_OP_DELETE:
434 message->body.kvp_delete.key_size =
435 utf16s_to_utf8s(
436 (wchar_t *)in_msg->body.kvp_delete.key,
437 in_msg->body.kvp_delete.key_size,
438 UTF16_LITTLE_ENDIAN,
439 message->body.kvp_delete.key,
440 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
441 break;
442
443 case KVP_OP_ENUMERATE:
444 message->body.kvp_enum_data.index =
445 in_msg->body.kvp_enum_data.index;
446 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700447 }
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700448
449 msg->len = sizeof(struct hv_kvp_msg);
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100450 rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
451 if (rc) {
452 pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
453 if (cancel_delayed_work_sync(&kvp_work))
454 kvp_respond_to_host(message, HV_E_FAIL);
455 }
456
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700457 kfree(msg);
458
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700459 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700460}
461
462/*
463 * Send a response back to the host.
464 */
465
466static void
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700467kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700468{
469 struct hv_kvp_msg *kvp_msg;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700470 struct hv_kvp_exchg_msg_value *kvp_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700471 char *key_name;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700472 char *value;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700473 struct icmsg_hdr *icmsghdrp;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700474 int keylen = 0;
475 int valuelen = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700476 u32 buf_len;
477 struct vmbus_channel *channel;
478 u64 req_id;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700479 int ret;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700480
481 /*
482 * If a transaction is not active; log and return.
483 */
484
485 if (!kvp_transaction.active) {
486 /*
487 * This is a spurious call!
488 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700489 pr_warn("KVP: Transaction not active\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700490 return;
491 }
492 /*
493 * Copy the global state for completing the transaction. Note that
494 * only one transaction can be active at a time.
495 */
496
497 buf_len = kvp_transaction.recv_len;
498 channel = kvp_transaction.recv_channel;
499 req_id = kvp_transaction.recv_req_id;
500
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -0700501 kvp_transaction.active = false;
502
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700503 icmsghdrp = (struct icmsg_hdr *)
504 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
505
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700506 if (channel->onchannel_callback == NULL)
507 /*
508 * We have raced with util driver being unloaded;
509 * silently return.
510 */
511 return;
512
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700513 icmsghdrp->status = error;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700514
515 /*
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700516 * If the error parameter is set, terminate the host's enumeration
517 * on this pool.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700518 */
519 if (error) {
520 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700521 * Something failed or we have timedout;
522 * terminate the current host-side iteration.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700523 */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700524 goto response_done;
525 }
526
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700527 kvp_msg = (struct hv_kvp_msg *)
528 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
529 sizeof(struct icmsg_hdr)];
530
531 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700532 case KVP_OP_GET_IP_INFO:
533 ret = process_ob_ipinfo(msg_to_host,
534 (struct hv_kvp_ip_msg *)kvp_msg,
535 KVP_OP_GET_IP_INFO);
536 if (ret < 0)
537 icmsghdrp->status = HV_E_FAIL;
538
539 goto response_done;
540 case KVP_OP_SET_IP_INFO:
541 goto response_done;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700542 case KVP_OP_GET:
543 kvp_data = &kvp_msg->body.kvp_get.data;
544 goto copy_value;
545
546 case KVP_OP_SET:
547 case KVP_OP_DELETE:
548 goto response_done;
549
550 default:
551 break;
552 }
553
554 kvp_data = &kvp_msg->body.kvp_enum_data.data;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700555 key_name = msg_to_host->body.kvp_enum_data.data.key;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700556
Ky Srinivasan245ba562010-12-16 18:54:16 -0700557 /*
558 * The windows host expects the key/value pair to be encoded
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700559 * in utf16. Ensure that the key/value size reported to the host
560 * will be less than or equal to the MAX size (including the
561 * terminating character).
Ky Srinivasan245ba562010-12-16 18:54:16 -0700562 */
Alan Stern0720a062011-11-17 16:42:19 -0500563 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700564 (wchar_t *) kvp_data->key,
565 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
566 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700567
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700568copy_value:
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700569 value = msg_to_host->body.kvp_enum_data.data.value;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700570 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
571 (wchar_t *) kvp_data->value,
572 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
573 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
574
575 /*
576 * If the utf8s to utf16s conversion failed; notify host
577 * of the error.
578 */
579 if ((keylen < 0) || (valuelen < 0))
580 icmsghdrp->status = HV_E_FAIL;
581
582 kvp_data->value_type = REG_SZ; /* all our values are strings */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700583
584response_done:
585 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
586
587 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800588 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700589 poll_channel(channel);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700590}
591
592/*
593 * This callback is invoked when we get a KVP message from the host.
594 * The host ensures that only one KVP transaction can be active at a time.
595 * KVP implementation in Linux needs to forward the key to a user-mde
596 * component to retrive the corresponding value. Consequently, we cannot
597 * respond to the host in the conext of this callback. Since the host
598 * guarantees that at most only one transaction can be active at a time,
599 * we stash away the transaction state in a set of global variables.
600 */
601
602void hv_kvp_onchannelcallback(void *context)
603{
604 struct vmbus_channel *channel = context;
605 u32 recvlen;
606 u64 requestid;
607
608 struct hv_kvp_msg *kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700609
610 struct icmsg_hdr *icmsghdrp;
611 struct icmsg_negotiate *negop = NULL;
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700612 int util_fw_version;
613 int kvp_srv_version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700614
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700615 if (kvp_transaction.active) {
616 /*
617 * We will defer processing this callback once
618 * the current transaction is complete.
619 */
620 kvp_transaction.kvp_context = context;
621 return;
622 }
Ky Srinivasan245ba562010-12-16 18:54:16 -0700623
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700624 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700625 &requestid);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700626
627 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700628 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
629 sizeof(struct vmbuspipe_hdr)];
630
631 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700632 /*
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700633 * Based on the host, select appropriate
634 * framework and service versions we will
635 * negotiate.
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700636 */
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700637 switch (vmbus_proto_version) {
638 case (VERSION_WS2008):
639 util_fw_version = UTIL_WS2K8_FW_VERSION;
640 kvp_srv_version = WS2008_SRV_VERSION;
641 break;
642 case (VERSION_WIN7):
643 util_fw_version = UTIL_FW_VERSION;
644 kvp_srv_version = WIN7_SRV_VERSION;
645 break;
646 default:
647 util_fw_version = UTIL_FW_VERSION;
648 kvp_srv_version = WIN8_SRV_VERSION;
649 }
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700650 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700651 recv_buffer, util_fw_version,
652 kvp_srv_version);
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700653
Ky Srinivasan245ba562010-12-16 18:54:16 -0700654 } else {
655 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
656 sizeof(struct vmbuspipe_hdr) +
657 sizeof(struct icmsg_hdr)];
658
Ky Srinivasan245ba562010-12-16 18:54:16 -0700659 /*
660 * Stash away this global state for completing the
661 * transaction; note transactions are serialized.
662 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700663
Ky Srinivasan245ba562010-12-16 18:54:16 -0700664 kvp_transaction.recv_len = recvlen;
665 kvp_transaction.recv_channel = channel;
666 kvp_transaction.recv_req_id = requestid;
667 kvp_transaction.active = true;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700668 kvp_transaction.kvp_msg = kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700669
670 /*
671 * Get the information from the
672 * user-mode component.
673 * component. This transaction will be
674 * completed when we get the value from
675 * the user-mode component.
676 * Set a timeout to deal with
677 * user-mode not responding.
678 */
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700679 schedule_work(&kvp_sendkey_work);
680 schedule_delayed_work(&kvp_work, 5*HZ);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700681
682 return;
683
684 }
685
Ky Srinivasan245ba562010-12-16 18:54:16 -0700686 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
687 | ICMSGHDRFLAG_RESPONSE;
688
689 vmbus_sendpacket(channel, recv_buffer,
690 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800691 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700692 }
693
694}
695
696int
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700697hv_kvp_init(struct hv_util_service *srv)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700698{
699 int err;
700
701 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
702 if (err)
703 return err;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700704 recv_buffer = srv->recv_buffer;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700705
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700706 /*
707 * When this driver loads, the user level daemon that
708 * processes the host requests may not yet be running.
709 * Defer processing channel callbacks until the daemon
710 * has registered.
711 */
712 kvp_transaction.active = true;
713
Ky Srinivasan245ba562010-12-16 18:54:16 -0700714 return 0;
715}
716
717void hv_kvp_deinit(void)
718{
719 cn_del_callback(&kvp_id);
720 cancel_delayed_work_sync(&kvp_work);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700721 cancel_work_sync(&kvp_sendkey_work);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700722}