hv_kvp.c 20 KB

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