hv_kvp.c 18 KB

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