hv_kvp.c 21 KB

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