staging: brcm80211: remove brcms_b_dotxstatus wrapper function
[zen-stable.git] / drivers / staging / hv / hv_kvp.c
blob9aa9ede0ee8735e4e6b5dbf14620fc8ca97957a2
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>
30 #include "hyperv.h"
31 #include "hv_kvp.h"
36 * Global state maintained for transaction that is being processed.
37 * Note that only one transaction can be active at any point in time.
39 * This state is set when we receive a request from the host; we
40 * cleanup this state when the transaction is completed - when we respond
41 * to the host with the key value.
44 static struct {
45 bool active; /* transaction status - active or not */
46 int recv_len; /* number of bytes received. */
47 struct vmbus_channel *recv_channel; /* chn we got the request */
48 u64 recv_req_id; /* request ID. */
49 } kvp_transaction;
51 static int kvp_send_key(int index);
53 static void kvp_respond_to_host(char *key, char *value, int error);
54 static void kvp_work_func(struct work_struct *dummy);
55 static void kvp_register(void);
57 static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
59 static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
60 static const char kvp_name[] = "kvp_kernel_module";
61 static int timeout_fired;
62 static u8 *recv_buffer;
64 * Register the kernel component with the user-level daemon.
65 * As part of this registration, pass the LIC version number.
68 static void
69 kvp_register(void)
72 struct cn_msg *msg;
74 msg = kzalloc(sizeof(*msg) + strlen(HV_DRV_VERSION) + 1 , GFP_ATOMIC);
76 if (msg) {
77 msg->id.idx = CN_KVP_IDX;
78 msg->id.val = CN_KVP_VAL;
79 msg->seq = KVP_REGISTER;
80 strcpy(msg->data, HV_DRV_VERSION);
81 msg->len = strlen(HV_DRV_VERSION) + 1;
82 cn_netlink_send(msg, 0, GFP_ATOMIC);
83 kfree(msg);
86 static void
87 kvp_work_func(struct work_struct *dummy)
90 * If the timer fires, the user-mode component has not responded;
91 * process the pending transaction.
93 kvp_respond_to_host("Unknown key", "Guest timed out", timeout_fired);
94 timeout_fired = 1;
98 * Callback when data is received from user mode.
101 static void
102 kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
104 struct hv_ku_msg *message;
106 message = (struct hv_ku_msg *)msg->data;
107 if (msg->seq == KVP_REGISTER) {
108 pr_info("KVP: user-mode registering done.\n");
109 kvp_register();
112 if (msg->seq == KVP_USER_SET) {
114 * Complete the transaction by forwarding the key value
115 * to the host. But first, cancel the timeout.
117 if (cancel_delayed_work_sync(&kvp_work))
118 kvp_respond_to_host(message->kvp_key,
119 message->kvp_value,
120 !strlen(message->kvp_key));
124 static int
125 kvp_send_key(int index)
127 struct cn_msg *msg;
129 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
131 if (msg) {
132 msg->id.idx = CN_KVP_IDX;
133 msg->id.val = CN_KVP_VAL;
134 msg->seq = KVP_KERNEL_GET;
135 ((struct hv_ku_msg *)msg->data)->kvp_index = index;
136 msg->len = sizeof(struct hv_ku_msg);
137 cn_netlink_send(msg, 0, GFP_ATOMIC);
138 kfree(msg);
139 return 0;
141 return 1;
145 * Send a response back to the host.
148 static void
149 kvp_respond_to_host(char *key, char *value, int error)
151 struct hv_kvp_msg *kvp_msg;
152 struct hv_kvp_msg_enumerate *kvp_data;
153 char *key_name;
154 struct icmsg_hdr *icmsghdrp;
155 int keylen, valuelen;
156 u32 buf_len;
157 struct vmbus_channel *channel;
158 u64 req_id;
161 * If a transaction is not active; log and return.
164 if (!kvp_transaction.active) {
166 * This is a spurious call!
168 pr_warn("KVP: Transaction not active\n");
169 return;
172 * Copy the global state for completing the transaction. Note that
173 * only one transaction can be active at a time.
176 buf_len = kvp_transaction.recv_len;
177 channel = kvp_transaction.recv_channel;
178 req_id = kvp_transaction.recv_req_id;
180 if (channel->onchannel_callback == NULL)
182 * We have raced with util driver being unloaded;
183 * silently return.
185 return;
187 icmsghdrp = (struct icmsg_hdr *)
188 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
189 kvp_msg = (struct hv_kvp_msg *)
190 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
191 sizeof(struct icmsg_hdr)];
192 kvp_data = &kvp_msg->kvp_data;
193 key_name = key;
196 * If the error parameter is set, terminate the host's enumeration.
198 if (error) {
200 * We don't support this index or the we have timedout;
201 * terminate the host-side iteration by returning an error.
203 icmsghdrp->status = HV_E_FAIL;
204 goto response_done;
208 * The windows host expects the key/value pair to be encoded
209 * in utf16.
211 keylen = utf8s_to_utf16s(key_name, strlen(key_name),
212 (wchar_t *)kvp_data->data.key);
213 kvp_data->data.key_size = 2*(keylen + 1); /* utf16 encoding */
214 valuelen = utf8s_to_utf16s(value, strlen(value),
215 (wchar_t *)kvp_data->data.value);
216 kvp_data->data.value_size = 2*(valuelen + 1); /* utf16 encoding */
218 kvp_data->data.value_type = REG_SZ; /* all our values are strings */
219 icmsghdrp->status = HV_S_OK;
221 response_done:
222 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
224 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
225 VM_PKT_DATA_INBAND, 0);
227 kvp_transaction.active = false;
231 * This callback is invoked when we get a KVP message from the host.
232 * The host ensures that only one KVP transaction can be active at a time.
233 * KVP implementation in Linux needs to forward the key to a user-mde
234 * component to retrive the corresponding value. Consequently, we cannot
235 * respond to the host in the conext of this callback. Since the host
236 * guarantees that at most only one transaction can be active at a time,
237 * we stash away the transaction state in a set of global variables.
240 void hv_kvp_onchannelcallback(void *context)
242 struct vmbus_channel *channel = context;
243 u32 recvlen;
244 u64 requestid;
246 struct hv_kvp_msg *kvp_msg;
247 struct hv_kvp_msg_enumerate *kvp_data;
249 struct icmsg_hdr *icmsghdrp;
250 struct icmsg_negotiate *negop = NULL;
253 if (kvp_transaction.active)
254 return;
257 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
259 if (recvlen > 0) {
260 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
261 sizeof(struct vmbuspipe_hdr)];
263 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
264 prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
265 } else {
266 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
267 sizeof(struct vmbuspipe_hdr) +
268 sizeof(struct icmsg_hdr)];
270 kvp_data = &kvp_msg->kvp_data;
273 * We only support the "get" operation on
274 * "KVP_POOL_AUTO" pool.
277 if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) ||
278 (kvp_msg->kvp_hdr.operation !=
279 KVP_OP_ENUMERATE)) {
280 icmsghdrp->status = HV_E_FAIL;
281 goto callback_done;
285 * Stash away this global state for completing the
286 * transaction; note transactions are serialized.
288 kvp_transaction.recv_len = recvlen;
289 kvp_transaction.recv_channel = channel;
290 kvp_transaction.recv_req_id = requestid;
291 kvp_transaction.active = true;
294 * Get the information from the
295 * user-mode component.
296 * component. This transaction will be
297 * completed when we get the value from
298 * the user-mode component.
299 * Set a timeout to deal with
300 * user-mode not responding.
302 kvp_send_key(kvp_data->index);
303 schedule_delayed_work(&kvp_work, 100);
305 return;
309 callback_done:
311 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
312 | ICMSGHDRFLAG_RESPONSE;
314 vmbus_sendpacket(channel, recv_buffer,
315 recvlen, requestid,
316 VM_PKT_DATA_INBAND, 0);
322 hv_kvp_init(struct hv_util_service *srv)
324 int err;
326 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
327 if (err)
328 return err;
329 recv_buffer = srv->recv_buffer;
331 return 0;
334 void hv_kvp_deinit(void)
336 cn_del_callback(&kvp_id);
337 cancel_delayed_work_sync(&kvp_work);