mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / hv / hv_kvp.c
blob816782a65488cb60b12effb04f237f4b0bc0bd07
1 /*
2 * An implementation of key value pair (KVP) functionality for Linux.
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
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.
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.
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.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/net.h>
26 #include <linux/nls.h>
27 #include <linux/connector.h>
28 #include <linux/workqueue.h>
29 #include <linux/hyperv.h>
33 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
35 #define WS2008_SRV_MAJOR 1
36 #define WS2008_SRV_MINOR 0
37 #define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
39 #define WIN7_SRV_MAJOR 3
40 #define WIN7_SRV_MINOR 0
41 #define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
43 #define WIN8_SRV_MAJOR 4
44 #define WIN8_SRV_MINOR 0
45 #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
48 * Global state maintained for transaction that is being processed.
49 * Note that only one transaction can be active at any point in time.
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.
56 static struct {
57 bool active; /* transaction status - active or not */
58 int recv_len; /* number of bytes received. */
59 struct hv_kvp_msg *kvp_msg; /* current message */
60 struct vmbus_channel *recv_channel; /* chn we got the request */
61 u64 recv_req_id; /* request ID. */
62 void *kvp_context; /* for the channel callback */
63 } kvp_transaction;
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.
70 static bool in_hand_shake = true;
73 * This state maintains the version number registered by the daemon.
75 static int dm_reg_value;
77 static void kvp_send_key(struct work_struct *dummy);
80 static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
81 static void kvp_work_func(struct work_struct *dummy);
82 static void kvp_register(int);
84 static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
85 static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
87 static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
88 static const char kvp_name[] = "kvp_kernel_module";
89 static u8 *recv_buffer;
91 * Register the kernel component with the user-level daemon.
92 * As part of this registration, pass the LIC version number.
93 * This number has no meaning, it satisfies the registration protocol.
95 #define HV_DRV_VERSION "3.1"
97 static void
98 kvp_register(int reg_value)
101 struct cn_msg *msg;
102 struct hv_kvp_msg *kvp_msg;
103 char *version;
105 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
107 if (msg) {
108 kvp_msg = (struct hv_kvp_msg *)msg->data;
109 version = kvp_msg->body.kvp_register.version;
110 msg->id.idx = CN_KVP_IDX;
111 msg->id.val = CN_KVP_VAL;
113 kvp_msg->kvp_hdr.operation = reg_value;
114 strcpy(version, HV_DRV_VERSION);
115 msg->len = sizeof(struct hv_kvp_msg);
116 cn_netlink_send(msg, 0, GFP_ATOMIC);
117 kfree(msg);
120 static void
121 kvp_work_func(struct work_struct *dummy)
124 * If the timer fires, the user-mode component has not responded;
125 * process the pending transaction.
127 kvp_respond_to_host(NULL, HV_E_FAIL);
130 static void poll_channel(struct vmbus_channel *channel)
132 unsigned long flags;
134 spin_lock_irqsave(&channel->inbound_lock, flags);
135 hv_kvp_onchannelcallback(channel);
136 spin_unlock_irqrestore(&channel->inbound_lock, flags);
139 static int kvp_handle_handshake(struct hv_kvp_msg *msg)
141 int ret = 1;
143 switch (msg->kvp_hdr.operation) {
144 case KVP_OP_REGISTER:
145 dm_reg_value = KVP_OP_REGISTER;
146 pr_info("KVP: IP injection functionality not available\n");
147 pr_info("KVP: Upgrade the KVP daemon\n");
148 break;
149 case KVP_OP_REGISTER1:
150 dm_reg_value = KVP_OP_REGISTER1;
151 break;
152 default:
153 pr_info("KVP: incompatible daemon\n");
154 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
155 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
156 ret = 0;
159 if (ret) {
161 * We have a compatible daemon; complete the handshake.
163 pr_info("KVP: user-mode registering done.\n");
164 kvp_register(dm_reg_value);
165 kvp_transaction.active = false;
166 if (kvp_transaction.kvp_context)
167 poll_channel(kvp_transaction.kvp_context);
169 return ret;
174 * Callback when data is received from user mode.
177 static void
178 kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
180 struct hv_kvp_msg *message;
181 struct hv_kvp_msg_enumerate *data;
182 int error = 0;
184 message = (struct hv_kvp_msg *)msg->data;
187 * If we are negotiating the version information
188 * with the daemon; handle that first.
191 if (in_hand_shake) {
192 if (kvp_handle_handshake(message))
193 in_hand_shake = false;
194 return;
198 * Based on the version of the daemon, we propagate errors from the
199 * daemon differently.
202 data = &message->body.kvp_enum_data;
204 switch (dm_reg_value) {
205 case KVP_OP_REGISTER:
207 * Null string is used to pass back error condition.
209 if (data->data.key[0] == 0)
210 error = HV_S_CONT;
211 break;
213 case KVP_OP_REGISTER1:
215 * We use the message header information from
216 * the user level daemon to transmit errors.
218 error = message->error;
219 break;
223 * Complete the transaction by forwarding the key value
224 * to the host. But first, cancel the timeout.
226 if (cancel_delayed_work_sync(&kvp_work))
227 kvp_respond_to_host(message, error);
231 static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
233 struct hv_kvp_msg *in = in_msg;
234 struct hv_kvp_ip_msg *out = out_msg;
235 int len;
237 switch (op) {
238 case KVP_OP_GET_IP_INFO:
240 * Transform all parameters into utf16 encoding.
242 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
243 strlen((char *)in->body.kvp_ip_val.ip_addr),
244 UTF16_HOST_ENDIAN,
245 (wchar_t *)out->kvp_ip_val.ip_addr,
246 MAX_IP_ADDR_SIZE);
247 if (len < 0)
248 return len;
250 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
251 strlen((char *)in->body.kvp_ip_val.sub_net),
252 UTF16_HOST_ENDIAN,
253 (wchar_t *)out->kvp_ip_val.sub_net,
254 MAX_IP_ADDR_SIZE);
255 if (len < 0)
256 return len;
258 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
259 strlen((char *)in->body.kvp_ip_val.gate_way),
260 UTF16_HOST_ENDIAN,
261 (wchar_t *)out->kvp_ip_val.gate_way,
262 MAX_GATEWAY_SIZE);
263 if (len < 0)
264 return len;
266 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
267 strlen((char *)in->body.kvp_ip_val.dns_addr),
268 UTF16_HOST_ENDIAN,
269 (wchar_t *)out->kvp_ip_val.dns_addr,
270 MAX_IP_ADDR_SIZE);
271 if (len < 0)
272 return len;
274 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
275 strlen((char *)in->body.kvp_ip_val.adapter_id),
276 UTF16_HOST_ENDIAN,
277 (wchar_t *)out->kvp_ip_val.adapter_id,
278 MAX_IP_ADDR_SIZE);
279 if (len < 0)
280 return len;
282 out->kvp_ip_val.dhcp_enabled =
283 in->body.kvp_ip_val.dhcp_enabled;
284 out->kvp_ip_val.addr_family =
285 in->body.kvp_ip_val.addr_family;
288 return 0;
291 static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
293 struct hv_kvp_ip_msg *in = in_msg;
294 struct hv_kvp_msg *out = out_msg;
296 switch (op) {
297 case KVP_OP_SET_IP_INFO:
299 * Transform all parameters into utf8 encoding.
301 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
302 MAX_IP_ADDR_SIZE,
303 UTF16_LITTLE_ENDIAN,
304 (__u8 *)out->body.kvp_ip_val.ip_addr,
305 MAX_IP_ADDR_SIZE);
307 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
308 MAX_IP_ADDR_SIZE,
309 UTF16_LITTLE_ENDIAN,
310 (__u8 *)out->body.kvp_ip_val.sub_net,
311 MAX_IP_ADDR_SIZE);
313 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
314 MAX_GATEWAY_SIZE,
315 UTF16_LITTLE_ENDIAN,
316 (__u8 *)out->body.kvp_ip_val.gate_way,
317 MAX_GATEWAY_SIZE);
319 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
320 MAX_IP_ADDR_SIZE,
321 UTF16_LITTLE_ENDIAN,
322 (__u8 *)out->body.kvp_ip_val.dns_addr,
323 MAX_IP_ADDR_SIZE);
325 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
327 default:
328 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
329 MAX_ADAPTER_ID_SIZE,
330 UTF16_LITTLE_ENDIAN,
331 (__u8 *)out->body.kvp_ip_val.adapter_id,
332 MAX_ADAPTER_ID_SIZE);
334 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
341 static void
342 kvp_send_key(struct work_struct *dummy)
344 struct cn_msg *msg;
345 struct hv_kvp_msg *message;
346 struct hv_kvp_msg *in_msg;
347 __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
348 __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
349 __u32 val32;
350 __u64 val64;
352 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
353 if (!msg)
354 return;
356 msg->id.idx = CN_KVP_IDX;
357 msg->id.val = CN_KVP_VAL;
359 message = (struct hv_kvp_msg *)msg->data;
360 message->kvp_hdr.operation = operation;
361 message->kvp_hdr.pool = pool;
362 in_msg = kvp_transaction.kvp_msg;
365 * The key/value strings sent from the host are encoded in
366 * in utf16; convert it to utf8 strings.
367 * The host assures us that the utf16 strings will not exceed
368 * the max lengths specified. We will however, reserve room
369 * for the string terminating character - in the utf16s_utf8s()
370 * function we limit the size of the buffer where the converted
371 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
372 * that the strings can be properly terminated!
375 switch (message->kvp_hdr.operation) {
376 case KVP_OP_SET_IP_INFO:
377 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
378 break;
379 case KVP_OP_GET_IP_INFO:
380 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
381 break;
382 case KVP_OP_SET:
383 switch (in_msg->body.kvp_set.data.value_type) {
384 case REG_SZ:
386 * The value is a string - utf16 encoding.
388 message->body.kvp_set.data.value_size =
389 utf16s_to_utf8s(
390 (wchar_t *)in_msg->body.kvp_set.data.value,
391 in_msg->body.kvp_set.data.value_size,
392 UTF16_LITTLE_ENDIAN,
393 message->body.kvp_set.data.value,
394 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
395 break;
397 case REG_U32:
399 * The value is a 32 bit scalar.
400 * We save this as a utf8 string.
402 val32 = in_msg->body.kvp_set.data.value_u32;
403 message->body.kvp_set.data.value_size =
404 sprintf(message->body.kvp_set.data.value,
405 "%d", val32) + 1;
406 break;
408 case REG_U64:
410 * The value is a 64 bit scalar.
411 * We save this as a utf8 string.
413 val64 = in_msg->body.kvp_set.data.value_u64;
414 message->body.kvp_set.data.value_size =
415 sprintf(message->body.kvp_set.data.value,
416 "%llu", val64) + 1;
417 break;
420 case KVP_OP_GET:
421 message->body.kvp_set.data.key_size =
422 utf16s_to_utf8s(
423 (wchar_t *)in_msg->body.kvp_set.data.key,
424 in_msg->body.kvp_set.data.key_size,
425 UTF16_LITTLE_ENDIAN,
426 message->body.kvp_set.data.key,
427 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
428 break;
430 case KVP_OP_DELETE:
431 message->body.kvp_delete.key_size =
432 utf16s_to_utf8s(
433 (wchar_t *)in_msg->body.kvp_delete.key,
434 in_msg->body.kvp_delete.key_size,
435 UTF16_LITTLE_ENDIAN,
436 message->body.kvp_delete.key,
437 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
438 break;
440 case KVP_OP_ENUMERATE:
441 message->body.kvp_enum_data.index =
442 in_msg->body.kvp_enum_data.index;
443 break;
446 msg->len = sizeof(struct hv_kvp_msg);
447 cn_netlink_send(msg, 0, GFP_ATOMIC);
448 kfree(msg);
450 return;
454 * Send a response back to the host.
457 static void
458 kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
460 struct hv_kvp_msg *kvp_msg;
461 struct hv_kvp_exchg_msg_value *kvp_data;
462 char *key_name;
463 char *value;
464 struct icmsg_hdr *icmsghdrp;
465 int keylen = 0;
466 int valuelen = 0;
467 u32 buf_len;
468 struct vmbus_channel *channel;
469 u64 req_id;
470 int ret;
473 * If a transaction is not active; log and return.
476 if (!kvp_transaction.active) {
478 * This is a spurious call!
480 pr_warn("KVP: Transaction not active\n");
481 return;
484 * Copy the global state for completing the transaction. Note that
485 * only one transaction can be active at a time.
488 buf_len = kvp_transaction.recv_len;
489 channel = kvp_transaction.recv_channel;
490 req_id = kvp_transaction.recv_req_id;
492 kvp_transaction.active = false;
494 icmsghdrp = (struct icmsg_hdr *)
495 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
497 if (channel->onchannel_callback == NULL)
499 * We have raced with util driver being unloaded;
500 * silently return.
502 return;
504 icmsghdrp->status = error;
507 * If the error parameter is set, terminate the host's enumeration
508 * on this pool.
510 if (error) {
512 * Something failed or we have timedout;
513 * terminate the current host-side iteration.
515 goto response_done;
518 kvp_msg = (struct hv_kvp_msg *)
519 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
520 sizeof(struct icmsg_hdr)];
522 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
523 case KVP_OP_GET_IP_INFO:
524 ret = process_ob_ipinfo(msg_to_host,
525 (struct hv_kvp_ip_msg *)kvp_msg,
526 KVP_OP_GET_IP_INFO);
527 if (ret < 0)
528 icmsghdrp->status = HV_E_FAIL;
530 goto response_done;
531 case KVP_OP_SET_IP_INFO:
532 goto response_done;
533 case KVP_OP_GET:
534 kvp_data = &kvp_msg->body.kvp_get.data;
535 goto copy_value;
537 case KVP_OP_SET:
538 case KVP_OP_DELETE:
539 goto response_done;
541 default:
542 break;
545 kvp_data = &kvp_msg->body.kvp_enum_data.data;
546 key_name = msg_to_host->body.kvp_enum_data.data.key;
549 * The windows host expects the key/value pair to be encoded
550 * in utf16. Ensure that the key/value size reported to the host
551 * will be less than or equal to the MAX size (including the
552 * terminating character).
554 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
555 (wchar_t *) kvp_data->key,
556 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
557 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
559 copy_value:
560 value = msg_to_host->body.kvp_enum_data.data.value;
561 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
562 (wchar_t *) kvp_data->value,
563 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
564 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
567 * If the utf8s to utf16s conversion failed; notify host
568 * of the error.
570 if ((keylen < 0) || (valuelen < 0))
571 icmsghdrp->status = HV_E_FAIL;
573 kvp_data->value_type = REG_SZ; /* all our values are strings */
575 response_done:
576 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
578 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
579 VM_PKT_DATA_INBAND, 0);
580 poll_channel(channel);
585 * This callback is invoked when we get a KVP message from the host.
586 * The host ensures that only one KVP transaction can be active at a time.
587 * KVP implementation in Linux needs to forward the key to a user-mde
588 * component to retrive the corresponding value. Consequently, we cannot
589 * respond to the host in the conext of this callback. Since the host
590 * guarantees that at most only one transaction can be active at a time,
591 * we stash away the transaction state in a set of global variables.
594 void hv_kvp_onchannelcallback(void *context)
596 struct vmbus_channel *channel = context;
597 u32 recvlen;
598 u64 requestid;
600 struct hv_kvp_msg *kvp_msg;
602 struct icmsg_hdr *icmsghdrp;
603 struct icmsg_negotiate *negop = NULL;
604 int util_fw_version;
605 int kvp_srv_version;
607 if (kvp_transaction.active) {
609 * We will defer processing this callback once
610 * the current transaction is complete.
612 kvp_transaction.kvp_context = context;
613 return;
616 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
617 &requestid);
619 if (recvlen > 0) {
620 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
621 sizeof(struct vmbuspipe_hdr)];
623 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
625 * Based on the host, select appropriate
626 * framework and service versions we will
627 * negotiate.
629 switch (vmbus_proto_version) {
630 case (VERSION_WS2008):
631 util_fw_version = UTIL_WS2K8_FW_VERSION;
632 kvp_srv_version = WS2008_SRV_VERSION;
633 break;
634 case (VERSION_WIN7):
635 util_fw_version = UTIL_FW_VERSION;
636 kvp_srv_version = WIN7_SRV_VERSION;
637 break;
638 default:
639 util_fw_version = UTIL_FW_VERSION;
640 kvp_srv_version = WIN8_SRV_VERSION;
642 vmbus_prep_negotiate_resp(icmsghdrp, negop,
643 recv_buffer, util_fw_version,
644 kvp_srv_version);
646 } else {
647 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
648 sizeof(struct vmbuspipe_hdr) +
649 sizeof(struct icmsg_hdr)];
652 * Stash away this global state for completing the
653 * transaction; note transactions are serialized.
656 kvp_transaction.recv_len = recvlen;
657 kvp_transaction.recv_channel = channel;
658 kvp_transaction.recv_req_id = requestid;
659 kvp_transaction.active = true;
660 kvp_transaction.kvp_msg = kvp_msg;
663 * Get the information from the
664 * user-mode component.
665 * component. This transaction will be
666 * completed when we get the value from
667 * the user-mode component.
668 * Set a timeout to deal with
669 * user-mode not responding.
671 schedule_work(&kvp_sendkey_work);
672 schedule_delayed_work(&kvp_work, 5*HZ);
674 return;
678 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
679 | ICMSGHDRFLAG_RESPONSE;
681 vmbus_sendpacket(channel, recv_buffer,
682 recvlen, requestid,
683 VM_PKT_DATA_INBAND, 0);
689 hv_kvp_init(struct hv_util_service *srv)
691 int err;
693 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
694 if (err)
695 return err;
696 recv_buffer = srv->recv_buffer;
699 * When this driver loads, the user level daemon that
700 * processes the host requests may not yet be running.
701 * Defer processing channel callbacks until the daemon
702 * has registered.
704 kvp_transaction.active = true;
706 return 0;
709 void hv_kvp_deinit(void)
711 cn_del_callback(&kvp_id);
712 cancel_delayed_work_sync(&kvp_work);
713 cancel_work_sync(&kvp_sendkey_work);