Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 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 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/net.h> |
| 26 | #include <linux/nls.h> |
| 27 | #include <linux/connector.h> |
| 28 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 29 | #include <linux/hyperv.h> |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 30 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /* |
| 34 | * Global state maintained for transaction that is being processed. |
| 35 | * Note that only one transaction can be active at any point in time. |
| 36 | * |
| 37 | * This state is set when we receive a request from the host; we |
| 38 | * cleanup this state when the transaction is completed - when we respond |
| 39 | * to the host with the key value. |
| 40 | */ |
| 41 | |
| 42 | static struct { |
| 43 | bool active; /* transaction status - active or not */ |
| 44 | int recv_len; /* number of bytes received. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 45 | struct hv_kvp_msg *kvp_msg; /* current message */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 46 | struct vmbus_channel *recv_channel; /* chn we got the request */ |
| 47 | u64 recv_req_id; /* request ID. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 48 | void *kvp_context; /* for the channel callback */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 49 | } kvp_transaction; |
| 50 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 51 | static void kvp_send_key(struct work_struct *dummy); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 52 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 53 | #define TIMEOUT_FIRED 1 |
| 54 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 55 | static void kvp_respond_to_host(char *key, char *value, int error); |
| 56 | static void kvp_work_func(struct work_struct *dummy); |
| 57 | static void kvp_register(void); |
| 58 | |
| 59 | static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 60 | static DECLARE_WORK(kvp_sendkey_work, kvp_send_key); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 61 | |
| 62 | static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL }; |
| 63 | static const char kvp_name[] = "kvp_kernel_module"; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 64 | static u8 *recv_buffer; |
| 65 | /* |
| 66 | * Register the kernel component with the user-level daemon. |
| 67 | * As part of this registration, pass the LIC version number. |
| 68 | */ |
| 69 | |
| 70 | static void |
| 71 | kvp_register(void) |
| 72 | { |
| 73 | |
| 74 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 75 | struct hv_kvp_msg *kvp_msg; |
| 76 | char *version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 77 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 78 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 79 | |
| 80 | if (msg) { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 81 | kvp_msg = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame] | 82 | version = kvp_msg->body.kvp_register.version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 83 | msg->id.idx = CN_KVP_IDX; |
| 84 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 85 | |
| 86 | kvp_msg->kvp_hdr.operation = KVP_OP_REGISTER; |
| 87 | strcpy(version, HV_DRV_VERSION); |
| 88 | msg->len = sizeof(struct hv_kvp_msg); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 89 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 90 | kfree(msg); |
| 91 | } |
| 92 | } |
| 93 | static void |
| 94 | kvp_work_func(struct work_struct *dummy) |
| 95 | { |
| 96 | /* |
| 97 | * If the timer fires, the user-mode component has not responded; |
| 98 | * process the pending transaction. |
| 99 | */ |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 100 | kvp_respond_to_host("Unknown key", "Guest timed out", TIMEOUT_FIRED); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Callback when data is received from user mode. |
| 105 | */ |
| 106 | |
| 107 | static void |
| 108 | kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
| 109 | { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 110 | struct hv_kvp_msg *message; |
| 111 | struct hv_kvp_msg_enumerate *data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 112 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 113 | message = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 114 | switch (message->kvp_hdr.operation) { |
| 115 | case KVP_OP_REGISTER: |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 116 | pr_info("KVP: user-mode registering done.\n"); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 117 | kvp_register(); |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 118 | kvp_transaction.active = false; |
| 119 | hv_kvp_onchannelcallback(kvp_transaction.kvp_context); |
| 120 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 121 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 122 | default: |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 123 | data = &message->body.kvp_enum_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Complete the transaction by forwarding the key value |
| 126 | * to the host. But first, cancel the timeout. |
| 127 | */ |
| 128 | if (cancel_delayed_work_sync(&kvp_work)) |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame] | 129 | kvp_respond_to_host(data->data.key, |
| 130 | data->data.value, |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 131 | !strlen(data->data.key)); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 135 | static void |
| 136 | kvp_send_key(struct work_struct *dummy) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 137 | { |
| 138 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 139 | struct hv_kvp_msg *message; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 140 | struct hv_kvp_msg *in_msg; |
| 141 | __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation; |
| 142 | __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool; |
| 143 | __u32 val32; |
| 144 | __u64 val64; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 145 | |
| 146 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC); |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 147 | if (!msg) |
| 148 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 149 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 150 | msg->id.idx = CN_KVP_IDX; |
| 151 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 152 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 153 | message = (struct hv_kvp_msg *)msg->data; |
| 154 | message->kvp_hdr.operation = operation; |
| 155 | message->kvp_hdr.pool = pool; |
| 156 | in_msg = kvp_transaction.kvp_msg; |
| 157 | |
| 158 | /* |
| 159 | * The key/value strings sent from the host are encoded in |
| 160 | * in utf16; convert it to utf8 strings. |
| 161 | * The host assures us that the utf16 strings will not exceed |
| 162 | * the max lengths specified. We will however, reserve room |
| 163 | * for the string terminating character - in the utf16s_utf8s() |
| 164 | * function we limit the size of the buffer where the converted |
| 165 | * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee |
| 166 | * that the strings can be properly terminated! |
| 167 | */ |
| 168 | |
| 169 | switch (message->kvp_hdr.operation) { |
| 170 | case KVP_OP_SET: |
| 171 | switch (in_msg->body.kvp_set.data.value_type) { |
| 172 | case REG_SZ: |
| 173 | /* |
| 174 | * The value is a string - utf16 encoding. |
| 175 | */ |
| 176 | message->body.kvp_set.data.value_size = |
| 177 | utf16s_to_utf8s( |
| 178 | (wchar_t *)in_msg->body.kvp_set.data.value, |
| 179 | in_msg->body.kvp_set.data.value_size, |
| 180 | UTF16_LITTLE_ENDIAN, |
| 181 | message->body.kvp_set.data.value, |
| 182 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1; |
| 183 | break; |
| 184 | |
| 185 | case REG_U32: |
| 186 | /* |
| 187 | * The value is a 32 bit scalar. |
| 188 | * We save this as a utf8 string. |
| 189 | */ |
| 190 | val32 = in_msg->body.kvp_set.data.value_u32; |
| 191 | message->body.kvp_set.data.value_size = |
| 192 | sprintf(message->body.kvp_set.data.value, |
| 193 | "%d", val32) + 1; |
| 194 | break; |
| 195 | |
| 196 | case REG_U64: |
| 197 | /* |
| 198 | * The value is a 64 bit scalar. |
| 199 | * We save this as a utf8 string. |
| 200 | */ |
| 201 | val64 = in_msg->body.kvp_set.data.value_u64; |
| 202 | message->body.kvp_set.data.value_size = |
| 203 | sprintf(message->body.kvp_set.data.value, |
| 204 | "%llu", val64) + 1; |
| 205 | break; |
| 206 | |
| 207 | } |
| 208 | case KVP_OP_GET: |
| 209 | message->body.kvp_set.data.key_size = |
| 210 | utf16s_to_utf8s( |
| 211 | (wchar_t *)in_msg->body.kvp_set.data.key, |
| 212 | in_msg->body.kvp_set.data.key_size, |
| 213 | UTF16_LITTLE_ENDIAN, |
| 214 | message->body.kvp_set.data.key, |
| 215 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 216 | break; |
| 217 | |
| 218 | case KVP_OP_DELETE: |
| 219 | message->body.kvp_delete.key_size = |
| 220 | utf16s_to_utf8s( |
| 221 | (wchar_t *)in_msg->body.kvp_delete.key, |
| 222 | in_msg->body.kvp_delete.key_size, |
| 223 | UTF16_LITTLE_ENDIAN, |
| 224 | message->body.kvp_delete.key, |
| 225 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 226 | break; |
| 227 | |
| 228 | case KVP_OP_ENUMERATE: |
| 229 | message->body.kvp_enum_data.index = |
| 230 | in_msg->body.kvp_enum_data.index; |
| 231 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 232 | } |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 233 | |
| 234 | msg->len = sizeof(struct hv_kvp_msg); |
| 235 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 236 | kfree(msg); |
| 237 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 238 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Send a response back to the host. |
| 243 | */ |
| 244 | |
| 245 | static void |
| 246 | kvp_respond_to_host(char *key, char *value, int error) |
| 247 | { |
| 248 | struct hv_kvp_msg *kvp_msg; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 249 | struct hv_kvp_exchg_msg_value *kvp_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 250 | char *key_name; |
| 251 | struct icmsg_hdr *icmsghdrp; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 252 | int keylen = 0; |
| 253 | int valuelen = 0; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 254 | u32 buf_len; |
| 255 | struct vmbus_channel *channel; |
| 256 | u64 req_id; |
| 257 | |
| 258 | /* |
| 259 | * If a transaction is not active; log and return. |
| 260 | */ |
| 261 | |
| 262 | if (!kvp_transaction.active) { |
| 263 | /* |
| 264 | * This is a spurious call! |
| 265 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 266 | pr_warn("KVP: Transaction not active\n"); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | /* |
| 270 | * Copy the global state for completing the transaction. Note that |
| 271 | * only one transaction can be active at a time. |
| 272 | */ |
| 273 | |
| 274 | buf_len = kvp_transaction.recv_len; |
| 275 | channel = kvp_transaction.recv_channel; |
| 276 | req_id = kvp_transaction.recv_req_id; |
| 277 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 278 | kvp_transaction.active = false; |
| 279 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 280 | icmsghdrp = (struct icmsg_hdr *) |
| 281 | &recv_buffer[sizeof(struct vmbuspipe_hdr)]; |
| 282 | |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 283 | if (channel->onchannel_callback == NULL) |
| 284 | /* |
| 285 | * We have raced with util driver being unloaded; |
| 286 | * silently return. |
| 287 | */ |
| 288 | return; |
| 289 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 290 | |
| 291 | /* |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 292 | * If the error parameter is set, terminate the host's enumeration |
| 293 | * on this pool. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 294 | */ |
| 295 | if (error) { |
| 296 | /* |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 297 | * Something failed or the we have timedout; |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 298 | * terminate the current host-side iteration. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 299 | */ |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 300 | icmsghdrp->status = HV_S_CONT; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 301 | goto response_done; |
| 302 | } |
| 303 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 304 | icmsghdrp->status = HV_S_OK; |
| 305 | |
| 306 | kvp_msg = (struct hv_kvp_msg *) |
| 307 | &recv_buffer[sizeof(struct vmbuspipe_hdr) + |
| 308 | sizeof(struct icmsg_hdr)]; |
| 309 | |
| 310 | switch (kvp_transaction.kvp_msg->kvp_hdr.operation) { |
| 311 | case KVP_OP_GET: |
| 312 | kvp_data = &kvp_msg->body.kvp_get.data; |
| 313 | goto copy_value; |
| 314 | |
| 315 | case KVP_OP_SET: |
| 316 | case KVP_OP_DELETE: |
| 317 | goto response_done; |
| 318 | |
| 319 | default: |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | kvp_data = &kvp_msg->body.kvp_enum_data.data; |
| 324 | key_name = key; |
| 325 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 326 | /* |
| 327 | * The windows host expects the key/value pair to be encoded |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 328 | * in utf16. Ensure that the key/value size reported to the host |
| 329 | * will be less than or equal to the MAX size (including the |
| 330 | * terminating character). |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 331 | */ |
Alan Stern | 0720a06 | 2011-11-17 16:42:19 -0500 | [diff] [blame] | 332 | keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN, |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 333 | (wchar_t *) kvp_data->key, |
| 334 | (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2); |
| 335 | kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 336 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 337 | copy_value: |
| 338 | valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN, |
| 339 | (wchar_t *) kvp_data->value, |
| 340 | (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2); |
| 341 | kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */ |
| 342 | |
| 343 | /* |
| 344 | * If the utf8s to utf16s conversion failed; notify host |
| 345 | * of the error. |
| 346 | */ |
| 347 | if ((keylen < 0) || (valuelen < 0)) |
| 348 | icmsghdrp->status = HV_E_FAIL; |
| 349 | |
| 350 | kvp_data->value_type = REG_SZ; /* all our values are strings */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 351 | |
| 352 | response_done: |
| 353 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; |
| 354 | |
| 355 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 356 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 357 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * This callback is invoked when we get a KVP message from the host. |
| 362 | * The host ensures that only one KVP transaction can be active at a time. |
| 363 | * KVP implementation in Linux needs to forward the key to a user-mde |
| 364 | * component to retrive the corresponding value. Consequently, we cannot |
| 365 | * respond to the host in the conext of this callback. Since the host |
| 366 | * guarantees that at most only one transaction can be active at a time, |
| 367 | * we stash away the transaction state in a set of global variables. |
| 368 | */ |
| 369 | |
| 370 | void hv_kvp_onchannelcallback(void *context) |
| 371 | { |
| 372 | struct vmbus_channel *channel = context; |
| 373 | u32 recvlen; |
| 374 | u64 requestid; |
| 375 | |
| 376 | struct hv_kvp_msg *kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 377 | |
| 378 | struct icmsg_hdr *icmsghdrp; |
| 379 | struct icmsg_negotiate *negop = NULL; |
| 380 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 381 | if (kvp_transaction.active) { |
| 382 | /* |
| 383 | * We will defer processing this callback once |
| 384 | * the current transaction is complete. |
| 385 | */ |
| 386 | kvp_transaction.kvp_context = context; |
| 387 | return; |
| 388 | } |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 389 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 390 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid); |
| 391 | |
| 392 | if (recvlen > 0) { |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 393 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ |
| 394 | sizeof(struct vmbuspipe_hdr)]; |
| 395 | |
| 396 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
Greg Kroah-Hartman | da0e963 | 2011-10-11 08:42:28 -0600 | [diff] [blame] | 397 | vmbus_prep_negotiate_resp(icmsghdrp, negop, recv_buffer); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 398 | } else { |
| 399 | kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ |
| 400 | sizeof(struct vmbuspipe_hdr) + |
| 401 | sizeof(struct icmsg_hdr)]; |
| 402 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 403 | /* |
| 404 | * Stash away this global state for completing the |
| 405 | * transaction; note transactions are serialized. |
| 406 | */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 407 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 408 | kvp_transaction.recv_len = recvlen; |
| 409 | kvp_transaction.recv_channel = channel; |
| 410 | kvp_transaction.recv_req_id = requestid; |
| 411 | kvp_transaction.active = true; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 412 | kvp_transaction.kvp_msg = kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * Get the information from the |
| 416 | * user-mode component. |
| 417 | * component. This transaction will be |
| 418 | * completed when we get the value from |
| 419 | * the user-mode component. |
| 420 | * Set a timeout to deal with |
| 421 | * user-mode not responding. |
| 422 | */ |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 423 | schedule_work(&kvp_sendkey_work); |
| 424 | schedule_delayed_work(&kvp_work, 5*HZ); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 425 | |
| 426 | return; |
| 427 | |
| 428 | } |
| 429 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 430 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 431 | | ICMSGHDRFLAG_RESPONSE; |
| 432 | |
| 433 | vmbus_sendpacket(channel, recv_buffer, |
| 434 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 435 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | } |
| 439 | |
| 440 | int |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 441 | hv_kvp_init(struct hv_util_service *srv) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 442 | { |
| 443 | int err; |
| 444 | |
| 445 | err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback); |
| 446 | if (err) |
| 447 | return err; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 448 | recv_buffer = srv->recv_buffer; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 449 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 450 | /* |
| 451 | * When this driver loads, the user level daemon that |
| 452 | * processes the host requests may not yet be running. |
| 453 | * Defer processing channel callbacks until the daemon |
| 454 | * has registered. |
| 455 | */ |
| 456 | kvp_transaction.active = true; |
| 457 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | void hv_kvp_deinit(void) |
| 462 | { |
| 463 | cn_del_callback(&kvp_id); |
| 464 | cancel_delayed_work_sync(&kvp_work); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 465 | cancel_work_sync(&kvp_sendkey_work); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 466 | } |