Staging: hv: Include the newly created header file in all of the relevant hyperv...
[linux-block.git] / drivers / staging / hv / hv_kvp.c
CommitLineData
245ba56a
KS
1/*
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 */
af7a5b6e 23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
245ba56a
KS
24
25#include <linux/net.h>
26#include <linux/nls.h>
27#include <linux/connector.h>
28#include <linux/workqueue.h>
29
3f335ea2 30#include "hyperv.h"
245ba56a 31#include "logging.h"
e3fe0bb6 32#include "hv_api.h"
245ba56a
KS
33#include "vmbus.h"
34#include "vmbus_packet_format.h"
35#include "vmbus_channel_interface.h"
36#include "version_info.h"
37#include "channel.h"
38#include "vmbus_private.h"
39#include "vmbus_api.h"
40#include "utils.h"
41#include "hv_kvp.h"
42
43
44
45/*
46 * Global state maintained for transaction that is being processed.
47 * Note that only one transaction can be active at any point in time.
48 *
49 * This state is set when we receive a request from the host; we
50 * cleanup this state when the transaction is completed - when we respond
51 * to the host with the key value.
52 */
53
54static struct {
55 bool active; /* transaction status - active or not */
56 int recv_len; /* number of bytes received. */
57 struct vmbus_channel *recv_channel; /* chn we got the request */
58 u64 recv_req_id; /* request ID. */
59} kvp_transaction;
60
61static int kvp_send_key(int index);
62
63static void kvp_respond_to_host(char *key, char *value, int error);
64static void kvp_work_func(struct work_struct *dummy);
65static void kvp_register(void);
66
67static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
68
69static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
70static const char kvp_name[] = "kvp_kernel_module";
71static int timeout_fired;
72static u8 *recv_buffer;
73/*
74 * Register the kernel component with the user-level daemon.
75 * As part of this registration, pass the LIC version number.
76 */
77
78static void
79kvp_register(void)
80{
81
82 struct cn_msg *msg;
83
84 msg = kzalloc(sizeof(*msg) + strlen(HV_DRV_VERSION) + 1 , GFP_ATOMIC);
85
86 if (msg) {
87 msg->id.idx = CN_KVP_IDX;
88 msg->id.val = CN_KVP_VAL;
89 msg->seq = KVP_REGISTER;
90 strcpy(msg->data, HV_DRV_VERSION);
91 msg->len = strlen(HV_DRV_VERSION) + 1;
92 cn_netlink_send(msg, 0, GFP_ATOMIC);
93 kfree(msg);
94 }
95}
96static void
97kvp_work_func(struct work_struct *dummy)
98{
99 /*
100 * If the timer fires, the user-mode component has not responded;
101 * process the pending transaction.
102 */
103 kvp_respond_to_host("Unknown key", "Guest timed out", timeout_fired);
104 timeout_fired = 1;
105}
106
107/*
108 * Callback when data is received from user mode.
109 */
110
111static void
112kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
113{
114 struct hv_ku_msg *message;
115
116 message = (struct hv_ku_msg *)msg->data;
117 if (msg->seq == KVP_REGISTER) {
af7a5b6e 118 pr_info("KVP: user-mode registering done.\n");
245ba56a
KS
119 kvp_register();
120 }
121
122 if (msg->seq == KVP_USER_SET) {
123 /*
124 * Complete the transaction by forwarding the key value
125 * to the host. But first, cancel the timeout.
126 */
127 if (cancel_delayed_work_sync(&kvp_work))
128 kvp_respond_to_host(message->kvp_key,
129 message->kvp_value,
130 !strlen(message->kvp_key));
131 }
132}
133
134static int
135kvp_send_key(int index)
136{
137 struct cn_msg *msg;
138
139 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
140
141 if (msg) {
142 msg->id.idx = CN_KVP_IDX;
143 msg->id.val = CN_KVP_VAL;
144 msg->seq = KVP_KERNEL_GET;
145 ((struct hv_ku_msg *)msg->data)->kvp_index = index;
146 msg->len = sizeof(struct hv_ku_msg);
147 cn_netlink_send(msg, 0, GFP_ATOMIC);
148 kfree(msg);
149 return 0;
150 }
151 return 1;
152}
153
154/*
155 * Send a response back to the host.
156 */
157
158static void
159kvp_respond_to_host(char *key, char *value, int error)
160{
161 struct hv_kvp_msg *kvp_msg;
162 struct hv_kvp_msg_enumerate *kvp_data;
163 char *key_name;
164 struct icmsg_hdr *icmsghdrp;
165 int keylen, valuelen;
166 u32 buf_len;
167 struct vmbus_channel *channel;
168 u64 req_id;
169
170 /*
171 * If a transaction is not active; log and return.
172 */
173
174 if (!kvp_transaction.active) {
175 /*
176 * This is a spurious call!
177 */
af7a5b6e 178 pr_warn("KVP: Transaction not active\n");
245ba56a
KS
179 return;
180 }
181 /*
182 * Copy the global state for completing the transaction. Note that
183 * only one transaction can be active at a time.
184 */
185
186 buf_len = kvp_transaction.recv_len;
187 channel = kvp_transaction.recv_channel;
188 req_id = kvp_transaction.recv_req_id;
189
190 icmsghdrp = (struct icmsg_hdr *)
191 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
192 kvp_msg = (struct hv_kvp_msg *)
193 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
194 sizeof(struct icmsg_hdr)];
195 kvp_data = &kvp_msg->kvp_data;
196 key_name = key;
197
198 /*
199 * If the error parameter is set, terminate the host's enumeration.
200 */
201 if (error) {
202 /*
203 * We don't support this index or the we have timedout;
204 * terminate the host-side iteration by returning an error.
205 */
206 icmsghdrp->status = HV_E_FAIL;
207 goto response_done;
208 }
209
210 /*
211 * The windows host expects the key/value pair to be encoded
212 * in utf16.
213 */
214 keylen = utf8s_to_utf16s(key_name, strlen(key_name),
215 (wchar_t *)kvp_data->data.key);
216 kvp_data->data.key_size = 2*(keylen + 1); /* utf16 encoding */
217 valuelen = utf8s_to_utf16s(value, strlen(value),
218 (wchar_t *)kvp_data->data.value);
219 kvp_data->data.value_size = 2*(valuelen + 1); /* utf16 encoding */
220
221 kvp_data->data.value_type = REG_SZ; /* all our values are strings */
222 icmsghdrp->status = HV_S_OK;
223
224response_done:
225 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
226
227 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
415f2287 228 VM_PKT_DATA_INBAND, 0);
245ba56a
KS
229
230 kvp_transaction.active = false;
231}
232
233/*
234 * This callback is invoked when we get a KVP message from the host.
235 * The host ensures that only one KVP transaction can be active at a time.
236 * KVP implementation in Linux needs to forward the key to a user-mde
237 * component to retrive the corresponding value. Consequently, we cannot
238 * respond to the host in the conext of this callback. Since the host
239 * guarantees that at most only one transaction can be active at a time,
240 * we stash away the transaction state in a set of global variables.
241 */
242
243void hv_kvp_onchannelcallback(void *context)
244{
245 struct vmbus_channel *channel = context;
246 u32 recvlen;
247 u64 requestid;
248
249 struct hv_kvp_msg *kvp_msg;
250 struct hv_kvp_msg_enumerate *kvp_data;
251
252 struct icmsg_hdr *icmsghdrp;
253 struct icmsg_negotiate *negop = NULL;
254
255
256 if (kvp_transaction.active)
257 return;
258
259
260 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
261
262 if (recvlen > 0) {
245ba56a
KS
263 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
264 sizeof(struct vmbuspipe_hdr)];
265
266 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
267 prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
268 } else {
269 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
270 sizeof(struct vmbuspipe_hdr) +
271 sizeof(struct icmsg_hdr)];
272
273 kvp_data = &kvp_msg->kvp_data;
274
275 /*
276 * We only support the "get" operation on
277 * "KVP_POOL_AUTO" pool.
278 */
279
280 if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) ||
281 (kvp_msg->kvp_hdr.operation !=
282 KVP_OP_ENUMERATE)) {
283 icmsghdrp->status = HV_E_FAIL;
284 goto callback_done;
285 }
286
287 /*
288 * Stash away this global state for completing the
289 * transaction; note transactions are serialized.
290 */
291 kvp_transaction.recv_len = recvlen;
292 kvp_transaction.recv_channel = channel;
293 kvp_transaction.recv_req_id = requestid;
294 kvp_transaction.active = true;
295
296 /*
297 * Get the information from the
298 * user-mode component.
299 * component. This transaction will be
300 * completed when we get the value from
301 * the user-mode component.
302 * Set a timeout to deal with
303 * user-mode not responding.
304 */
305 kvp_send_key(kvp_data->index);
306 schedule_delayed_work(&kvp_work, 100);
307
308 return;
309
310 }
311
312callback_done:
313
314 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
315 | ICMSGHDRFLAG_RESPONSE;
316
317 vmbus_sendpacket(channel, recv_buffer,
318 recvlen, requestid,
415f2287 319 VM_PKT_DATA_INBAND, 0);
245ba56a
KS
320 }
321
322}
323
324int
325hv_kvp_init(void)
326{
327 int err;
328
329 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
330 if (err)
331 return err;
332 recv_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
333 if (!recv_buffer)
334 return -ENOMEM;
335
336 return 0;
337}
338
339void hv_kvp_deinit(void)
340{
341 cn_del_callback(&kvp_id);
342 cancel_delayed_work_sync(&kvp_work);
343 kfree(recv_buffer);
344}