hv_kvp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/net.h>
  25. #include <linux/nls.h>
  26. #include <linux/connector.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/hyperv.h>
  29. #include "hyperv_vmbus.h"
  30. #include "hv_utils_transport.h"
  31. /*
  32. * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
  33. */
  34. #define WS2008_SRV_MAJOR 1
  35. #define WS2008_SRV_MINOR 0
  36. #define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
  37. #define WIN7_SRV_MAJOR 3
  38. #define WIN7_SRV_MINOR 0
  39. #define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
  40. #define WIN8_SRV_MAJOR 4
  41. #define WIN8_SRV_MINOR 0
  42. #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
  43. /*
  44. * Global state maintained for transaction that is being processed. For a class
  45. * of integration services, including the "KVP service", the specified protocol
  46. * is a "request/response" protocol which means that there can only be single
  47. * outstanding transaction from the host at any given point in time. We use
  48. * this to simplify memory management in this driver - we cache and process
  49. * only one message at a time.
  50. *
  51. * While the request/response protocol is guaranteed by the host, we further
  52. * ensure this by serializing packet processing in this driver - we do not
  53. * read additional packets from the VMBUs until the current packet is fully
  54. * handled.
  55. */
  56. static struct {
  57. int state; /* hvutil_device_state */
  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;
  64. /*
  65. * This state maintains the version number registered by the daemon.
  66. */
  67. static int dm_reg_value;
  68. static void kvp_send_key(struct work_struct *dummy);
  69. static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
  70. static void kvp_timeout_func(struct work_struct *dummy);
  71. static void kvp_register(int);
  72. static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
  73. static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
  74. static const char kvp_devname[] = "vmbus/hv_kvp";
  75. static u8 *recv_buffer;
  76. static struct hvutil_transport *hvt;
  77. /*
  78. * Register the kernel component with the user-level daemon.
  79. * As part of this registration, pass the LIC version number.
  80. * This number has no meaning, it satisfies the registration protocol.
  81. */
  82. #define HV_DRV_VERSION "3.1"
  83. static void
  84. kvp_register(int reg_value)
  85. {
  86. struct hv_kvp_msg *kvp_msg;
  87. char *version;
  88. kvp_msg = kzalloc(sizeof(*kvp_msg), GFP_KERNEL);
  89. if (kvp_msg) {
  90. version = kvp_msg->body.kvp_register.version;
  91. kvp_msg->kvp_hdr.operation = reg_value;
  92. strcpy(version, HV_DRV_VERSION);
  93. hvutil_transport_send(hvt, kvp_msg, sizeof(*kvp_msg));
  94. kfree(kvp_msg);
  95. }
  96. }
  97. static void kvp_timeout_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(NULL, HV_E_FAIL);
  104. /* Transaction is finished, reset the state. */
  105. if (kvp_transaction.state > HVUTIL_READY)
  106. kvp_transaction.state = HVUTIL_READY;
  107. hv_poll_channel(kvp_transaction.kvp_context,
  108. hv_kvp_onchannelcallback);
  109. }
  110. static int kvp_handle_handshake(struct hv_kvp_msg *msg)
  111. {
  112. switch (msg->kvp_hdr.operation) {
  113. case KVP_OP_REGISTER:
  114. dm_reg_value = KVP_OP_REGISTER;
  115. pr_info("KVP: IP injection functionality not available\n");
  116. pr_info("KVP: Upgrade the KVP daemon\n");
  117. break;
  118. case KVP_OP_REGISTER1:
  119. dm_reg_value = KVP_OP_REGISTER1;
  120. break;
  121. default:
  122. pr_info("KVP: incompatible daemon\n");
  123. pr_info("KVP: KVP version: %d, Daemon version: %d\n",
  124. KVP_OP_REGISTER1, msg->kvp_hdr.operation);
  125. return -EINVAL;
  126. }
  127. /*
  128. * We have a compatible daemon; complete the handshake.
  129. */
  130. pr_debug("KVP: userspace daemon ver. %d registered\n",
  131. KVP_OP_REGISTER);
  132. kvp_register(dm_reg_value);
  133. kvp_transaction.state = HVUTIL_READY;
  134. return 0;
  135. }
  136. /*
  137. * Callback when data is received from user mode.
  138. */
  139. static int kvp_on_msg(void *msg, int len)
  140. {
  141. struct hv_kvp_msg *message = (struct hv_kvp_msg *)msg;
  142. struct hv_kvp_msg_enumerate *data;
  143. int error = 0;
  144. if (len < sizeof(*message))
  145. return -EINVAL;
  146. /*
  147. * If we are negotiating the version information
  148. * with the daemon; handle that first.
  149. */
  150. if (kvp_transaction.state < HVUTIL_READY) {
  151. return kvp_handle_handshake(message);
  152. }
  153. /* We didn't send anything to userspace so the reply is spurious */
  154. if (kvp_transaction.state < HVUTIL_USERSPACE_REQ)
  155. return -EINVAL;
  156. kvp_transaction.state = HVUTIL_USERSPACE_RECV;
  157. /*
  158. * Based on the version of the daemon, we propagate errors from the
  159. * daemon differently.
  160. */
  161. data = &message->body.kvp_enum_data;
  162. switch (dm_reg_value) {
  163. case KVP_OP_REGISTER:
  164. /*
  165. * Null string is used to pass back error condition.
  166. */
  167. if (data->data.key[0] == 0)
  168. error = HV_S_CONT;
  169. break;
  170. case KVP_OP_REGISTER1:
  171. /*
  172. * We use the message header information from
  173. * the user level daemon to transmit errors.
  174. */
  175. error = message->error;
  176. break;
  177. }
  178. /*
  179. * Complete the transaction by forwarding the key value
  180. * to the host. But first, cancel the timeout.
  181. */
  182. if (cancel_delayed_work_sync(&kvp_timeout_work)) {
  183. kvp_respond_to_host(message, error);
  184. kvp_transaction.state = HVUTIL_READY;
  185. hv_poll_channel(kvp_transaction.kvp_context,
  186. hv_kvp_onchannelcallback);
  187. }
  188. return 0;
  189. }
  190. static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
  191. {
  192. struct hv_kvp_msg *in = in_msg;
  193. struct hv_kvp_ip_msg *out = out_msg;
  194. int len;
  195. switch (op) {
  196. case KVP_OP_GET_IP_INFO:
  197. /*
  198. * Transform all parameters into utf16 encoding.
  199. */
  200. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
  201. strlen((char *)in->body.kvp_ip_val.ip_addr),
  202. UTF16_HOST_ENDIAN,
  203. (wchar_t *)out->kvp_ip_val.ip_addr,
  204. MAX_IP_ADDR_SIZE);
  205. if (len < 0)
  206. return len;
  207. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
  208. strlen((char *)in->body.kvp_ip_val.sub_net),
  209. UTF16_HOST_ENDIAN,
  210. (wchar_t *)out->kvp_ip_val.sub_net,
  211. MAX_IP_ADDR_SIZE);
  212. if (len < 0)
  213. return len;
  214. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
  215. strlen((char *)in->body.kvp_ip_val.gate_way),
  216. UTF16_HOST_ENDIAN,
  217. (wchar_t *)out->kvp_ip_val.gate_way,
  218. MAX_GATEWAY_SIZE);
  219. if (len < 0)
  220. return len;
  221. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
  222. strlen((char *)in->body.kvp_ip_val.dns_addr),
  223. UTF16_HOST_ENDIAN,
  224. (wchar_t *)out->kvp_ip_val.dns_addr,
  225. MAX_IP_ADDR_SIZE);
  226. if (len < 0)
  227. return len;
  228. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
  229. strlen((char *)in->body.kvp_ip_val.adapter_id),
  230. UTF16_HOST_ENDIAN,
  231. (wchar_t *)out->kvp_ip_val.adapter_id,
  232. MAX_IP_ADDR_SIZE);
  233. if (len < 0)
  234. return len;
  235. out->kvp_ip_val.dhcp_enabled =
  236. in->body.kvp_ip_val.dhcp_enabled;
  237. out->kvp_ip_val.addr_family =
  238. in->body.kvp_ip_val.addr_family;
  239. }
  240. return 0;
  241. }
  242. static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
  243. {
  244. struct hv_kvp_ip_msg *in = in_msg;
  245. struct hv_kvp_msg *out = out_msg;
  246. switch (op) {
  247. case KVP_OP_SET_IP_INFO:
  248. /*
  249. * Transform all parameters into utf8 encoding.
  250. */
  251. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
  252. MAX_IP_ADDR_SIZE,
  253. UTF16_LITTLE_ENDIAN,
  254. (__u8 *)out->body.kvp_ip_val.ip_addr,
  255. MAX_IP_ADDR_SIZE);
  256. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
  257. MAX_IP_ADDR_SIZE,
  258. UTF16_LITTLE_ENDIAN,
  259. (__u8 *)out->body.kvp_ip_val.sub_net,
  260. MAX_IP_ADDR_SIZE);
  261. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
  262. MAX_GATEWAY_SIZE,
  263. UTF16_LITTLE_ENDIAN,
  264. (__u8 *)out->body.kvp_ip_val.gate_way,
  265. MAX_GATEWAY_SIZE);
  266. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
  267. MAX_IP_ADDR_SIZE,
  268. UTF16_LITTLE_ENDIAN,
  269. (__u8 *)out->body.kvp_ip_val.dns_addr,
  270. MAX_IP_ADDR_SIZE);
  271. out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
  272. default:
  273. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
  274. MAX_ADAPTER_ID_SIZE,
  275. UTF16_LITTLE_ENDIAN,
  276. (__u8 *)out->body.kvp_ip_val.adapter_id,
  277. MAX_ADAPTER_ID_SIZE);
  278. out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
  279. }
  280. }
  281. static void
  282. kvp_send_key(struct work_struct *dummy)
  283. {
  284. struct hv_kvp_msg *message;
  285. struct hv_kvp_msg *in_msg;
  286. __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
  287. __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
  288. __u32 val32;
  289. __u64 val64;
  290. int rc;
  291. /* The transaction state is wrong. */
  292. if (kvp_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
  293. return;
  294. message = kzalloc(sizeof(*message), GFP_KERNEL);
  295. message->kvp_hdr.operation = operation;
  296. message->kvp_hdr.pool = pool;
  297. in_msg = kvp_transaction.kvp_msg;
  298. /*
  299. * The key/value strings sent from the host are encoded in
  300. * in utf16; convert it to utf8 strings.
  301. * The host assures us that the utf16 strings will not exceed
  302. * the max lengths specified. We will however, reserve room
  303. * for the string terminating character - in the utf16s_utf8s()
  304. * function we limit the size of the buffer where the converted
  305. * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
  306. * that the strings can be properly terminated!
  307. */
  308. switch (message->kvp_hdr.operation) {
  309. case KVP_OP_SET_IP_INFO:
  310. process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
  311. break;
  312. case KVP_OP_GET_IP_INFO:
  313. process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
  314. break;
  315. case KVP_OP_SET:
  316. switch (in_msg->body.kvp_set.data.value_type) {
  317. case REG_SZ:
  318. /*
  319. * The value is a string - utf16 encoding.
  320. */
  321. message->body.kvp_set.data.value_size =
  322. utf16s_to_utf8s(
  323. (wchar_t *)in_msg->body.kvp_set.data.value,
  324. in_msg->body.kvp_set.data.value_size,
  325. UTF16_LITTLE_ENDIAN,
  326. message->body.kvp_set.data.value,
  327. HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
  328. break;
  329. case REG_U32:
  330. /*
  331. * The value is a 32 bit scalar.
  332. * We save this as a utf8 string.
  333. */
  334. val32 = in_msg->body.kvp_set.data.value_u32;
  335. message->body.kvp_set.data.value_size =
  336. sprintf(message->body.kvp_set.data.value,
  337. "%d", val32) + 1;
  338. break;
  339. case REG_U64:
  340. /*
  341. * The value is a 64 bit scalar.
  342. * We save this as a utf8 string.
  343. */
  344. val64 = in_msg->body.kvp_set.data.value_u64;
  345. message->body.kvp_set.data.value_size =
  346. sprintf(message->body.kvp_set.data.value,
  347. "%llu", val64) + 1;
  348. break;
  349. }
  350. case KVP_OP_GET:
  351. message->body.kvp_set.data.key_size =
  352. utf16s_to_utf8s(
  353. (wchar_t *)in_msg->body.kvp_set.data.key,
  354. in_msg->body.kvp_set.data.key_size,
  355. UTF16_LITTLE_ENDIAN,
  356. message->body.kvp_set.data.key,
  357. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  358. break;
  359. case KVP_OP_DELETE:
  360. message->body.kvp_delete.key_size =
  361. utf16s_to_utf8s(
  362. (wchar_t *)in_msg->body.kvp_delete.key,
  363. in_msg->body.kvp_delete.key_size,
  364. UTF16_LITTLE_ENDIAN,
  365. message->body.kvp_delete.key,
  366. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  367. break;
  368. case KVP_OP_ENUMERATE:
  369. message->body.kvp_enum_data.index =
  370. in_msg->body.kvp_enum_data.index;
  371. break;
  372. }
  373. kvp_transaction.state = HVUTIL_USERSPACE_REQ;
  374. rc = hvutil_transport_send(hvt, message, sizeof(*message));
  375. if (rc) {
  376. pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
  377. if (cancel_delayed_work_sync(&kvp_timeout_work)) {
  378. kvp_respond_to_host(message, HV_E_FAIL);
  379. kvp_transaction.state = HVUTIL_READY;
  380. }
  381. }
  382. kfree(message);
  383. return;
  384. }
  385. /*
  386. * Send a response back to the host.
  387. */
  388. static void
  389. kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
  390. {
  391. struct hv_kvp_msg *kvp_msg;
  392. struct hv_kvp_exchg_msg_value *kvp_data;
  393. char *key_name;
  394. char *value;
  395. struct icmsg_hdr *icmsghdrp;
  396. int keylen = 0;
  397. int valuelen = 0;
  398. u32 buf_len;
  399. struct vmbus_channel *channel;
  400. u64 req_id;
  401. int ret;
  402. /*
  403. * Copy the global state for completing the transaction. Note that
  404. * only one transaction can be active at a time.
  405. */
  406. buf_len = kvp_transaction.recv_len;
  407. channel = kvp_transaction.recv_channel;
  408. req_id = kvp_transaction.recv_req_id;
  409. icmsghdrp = (struct icmsg_hdr *)
  410. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  411. if (channel->onchannel_callback == NULL)
  412. /*
  413. * We have raced with util driver being unloaded;
  414. * silently return.
  415. */
  416. return;
  417. icmsghdrp->status = error;
  418. /*
  419. * If the error parameter is set, terminate the host's enumeration
  420. * on this pool.
  421. */
  422. if (error) {
  423. /*
  424. * Something failed or we have timedout;
  425. * terminate the current host-side iteration.
  426. */
  427. goto response_done;
  428. }
  429. kvp_msg = (struct hv_kvp_msg *)
  430. &recv_buffer[sizeof(struct vmbuspipe_hdr) +
  431. sizeof(struct icmsg_hdr)];
  432. switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
  433. case KVP_OP_GET_IP_INFO:
  434. ret = process_ob_ipinfo(msg_to_host,
  435. (struct hv_kvp_ip_msg *)kvp_msg,
  436. KVP_OP_GET_IP_INFO);
  437. if (ret < 0)
  438. icmsghdrp->status = HV_E_FAIL;
  439. goto response_done;
  440. case KVP_OP_SET_IP_INFO:
  441. goto response_done;
  442. case KVP_OP_GET:
  443. kvp_data = &kvp_msg->body.kvp_get.data;
  444. goto copy_value;
  445. case KVP_OP_SET:
  446. case KVP_OP_DELETE:
  447. goto response_done;
  448. default:
  449. break;
  450. }
  451. kvp_data = &kvp_msg->body.kvp_enum_data.data;
  452. key_name = msg_to_host->body.kvp_enum_data.data.key;
  453. /*
  454. * The windows host expects the key/value pair to be encoded
  455. * in utf16. Ensure that the key/value size reported to the host
  456. * will be less than or equal to the MAX size (including the
  457. * terminating character).
  458. */
  459. keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
  460. (wchar_t *) kvp_data->key,
  461. (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
  462. kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
  463. copy_value:
  464. value = msg_to_host->body.kvp_enum_data.data.value;
  465. valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
  466. (wchar_t *) kvp_data->value,
  467. (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
  468. kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
  469. /*
  470. * If the utf8s to utf16s conversion failed; notify host
  471. * of the error.
  472. */
  473. if ((keylen < 0) || (valuelen < 0))
  474. icmsghdrp->status = HV_E_FAIL;
  475. kvp_data->value_type = REG_SZ; /* all our values are strings */
  476. response_done:
  477. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  478. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  479. VM_PKT_DATA_INBAND, 0);
  480. }
  481. /*
  482. * This callback is invoked when we get a KVP message from the host.
  483. * The host ensures that only one KVP transaction can be active at a time.
  484. * KVP implementation in Linux needs to forward the key to a user-mde
  485. * component to retrive the corresponding value. Consequently, we cannot
  486. * respond to the host in the conext of this callback. Since the host
  487. * guarantees that at most only one transaction can be active at a time,
  488. * we stash away the transaction state in a set of global variables.
  489. */
  490. void hv_kvp_onchannelcallback(void *context)
  491. {
  492. struct vmbus_channel *channel = context;
  493. u32 recvlen;
  494. u64 requestid;
  495. struct hv_kvp_msg *kvp_msg;
  496. struct icmsg_hdr *icmsghdrp;
  497. struct icmsg_negotiate *negop = NULL;
  498. int util_fw_version;
  499. int kvp_srv_version;
  500. if (kvp_transaction.state > HVUTIL_READY) {
  501. /*
  502. * We will defer processing this callback once
  503. * the current transaction is complete.
  504. */
  505. kvp_transaction.kvp_context = context;
  506. return;
  507. }
  508. kvp_transaction.kvp_context = NULL;
  509. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
  510. &requestid);
  511. if (recvlen > 0) {
  512. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
  513. sizeof(struct vmbuspipe_hdr)];
  514. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  515. /*
  516. * Based on the host, select appropriate
  517. * framework and service versions we will
  518. * negotiate.
  519. */
  520. switch (vmbus_proto_version) {
  521. case (VERSION_WS2008):
  522. util_fw_version = UTIL_WS2K8_FW_VERSION;
  523. kvp_srv_version = WS2008_SRV_VERSION;
  524. break;
  525. case (VERSION_WIN7):
  526. util_fw_version = UTIL_FW_VERSION;
  527. kvp_srv_version = WIN7_SRV_VERSION;
  528. break;
  529. default:
  530. util_fw_version = UTIL_FW_VERSION;
  531. kvp_srv_version = WIN8_SRV_VERSION;
  532. }
  533. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  534. recv_buffer, util_fw_version,
  535. kvp_srv_version);
  536. } else {
  537. kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
  538. sizeof(struct vmbuspipe_hdr) +
  539. sizeof(struct icmsg_hdr)];
  540. /*
  541. * Stash away this global state for completing the
  542. * transaction; note transactions are serialized.
  543. */
  544. kvp_transaction.recv_len = recvlen;
  545. kvp_transaction.recv_channel = channel;
  546. kvp_transaction.recv_req_id = requestid;
  547. kvp_transaction.kvp_msg = kvp_msg;
  548. if (kvp_transaction.state < HVUTIL_READY) {
  549. /* Userspace is not registered yet */
  550. kvp_respond_to_host(NULL, HV_E_FAIL);
  551. return;
  552. }
  553. kvp_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
  554. /*
  555. * Get the information from the
  556. * user-mode component.
  557. * component. This transaction will be
  558. * completed when we get the value from
  559. * the user-mode component.
  560. * Set a timeout to deal with
  561. * user-mode not responding.
  562. */
  563. schedule_work(&kvp_sendkey_work);
  564. schedule_delayed_work(&kvp_timeout_work, 5*HZ);
  565. return;
  566. }
  567. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  568. | ICMSGHDRFLAG_RESPONSE;
  569. vmbus_sendpacket(channel, recv_buffer,
  570. recvlen, requestid,
  571. VM_PKT_DATA_INBAND, 0);
  572. }
  573. }
  574. static void kvp_on_reset(void)
  575. {
  576. if (cancel_delayed_work_sync(&kvp_timeout_work))
  577. kvp_respond_to_host(NULL, HV_E_FAIL);
  578. kvp_transaction.state = HVUTIL_DEVICE_INIT;
  579. }
  580. int
  581. hv_kvp_init(struct hv_util_service *srv)
  582. {
  583. recv_buffer = srv->recv_buffer;
  584. /*
  585. * When this driver loads, the user level daemon that
  586. * processes the host requests may not yet be running.
  587. * Defer processing channel callbacks until the daemon
  588. * has registered.
  589. */
  590. kvp_transaction.state = HVUTIL_DEVICE_INIT;
  591. hvt = hvutil_transport_init(kvp_devname, CN_KVP_IDX, CN_KVP_VAL,
  592. kvp_on_msg, kvp_on_reset);
  593. if (!hvt)
  594. return -EFAULT;
  595. return 0;
  596. }
  597. void hv_kvp_deinit(void)
  598. {
  599. kvp_transaction.state = HVUTIL_DEVICE_DYING;
  600. cancel_delayed_work_sync(&kvp_timeout_work);
  601. cancel_work_sync(&kvp_sendkey_work);
  602. hvutil_transport_destroy(hvt);
  603. }