hv_kvp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. int rc;
  298. msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
  299. if (!msg)
  300. return;
  301. msg->id.idx = CN_KVP_IDX;
  302. msg->id.val = CN_KVP_VAL;
  303. message = (struct hv_kvp_msg *)msg->data;
  304. message->kvp_hdr.operation = operation;
  305. message->kvp_hdr.pool = pool;
  306. in_msg = kvp_transaction.kvp_msg;
  307. /*
  308. * The key/value strings sent from the host are encoded in
  309. * in utf16; convert it to utf8 strings.
  310. * The host assures us that the utf16 strings will not exceed
  311. * the max lengths specified. We will however, reserve room
  312. * for the string terminating character - in the utf16s_utf8s()
  313. * function we limit the size of the buffer where the converted
  314. * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
  315. * that the strings can be properly terminated!
  316. */
  317. switch (message->kvp_hdr.operation) {
  318. case KVP_OP_SET_IP_INFO:
  319. process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
  320. break;
  321. case KVP_OP_GET_IP_INFO:
  322. process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
  323. break;
  324. case KVP_OP_SET:
  325. switch (in_msg->body.kvp_set.data.value_type) {
  326. case REG_SZ:
  327. /*
  328. * The value is a string - utf16 encoding.
  329. */
  330. message->body.kvp_set.data.value_size =
  331. utf16s_to_utf8s(
  332. (wchar_t *)in_msg->body.kvp_set.data.value,
  333. in_msg->body.kvp_set.data.value_size,
  334. UTF16_LITTLE_ENDIAN,
  335. message->body.kvp_set.data.value,
  336. HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
  337. break;
  338. case REG_U32:
  339. /*
  340. * The value is a 32 bit scalar.
  341. * We save this as a utf8 string.
  342. */
  343. val32 = in_msg->body.kvp_set.data.value_u32;
  344. message->body.kvp_set.data.value_size =
  345. sprintf(message->body.kvp_set.data.value,
  346. "%d", val32) + 1;
  347. break;
  348. case REG_U64:
  349. /*
  350. * The value is a 64 bit scalar.
  351. * We save this as a utf8 string.
  352. */
  353. val64 = in_msg->body.kvp_set.data.value_u64;
  354. message->body.kvp_set.data.value_size =
  355. sprintf(message->body.kvp_set.data.value,
  356. "%llu", val64) + 1;
  357. break;
  358. }
  359. case KVP_OP_GET:
  360. message->body.kvp_set.data.key_size =
  361. utf16s_to_utf8s(
  362. (wchar_t *)in_msg->body.kvp_set.data.key,
  363. in_msg->body.kvp_set.data.key_size,
  364. UTF16_LITTLE_ENDIAN,
  365. message->body.kvp_set.data.key,
  366. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  367. break;
  368. case KVP_OP_DELETE:
  369. message->body.kvp_delete.key_size =
  370. utf16s_to_utf8s(
  371. (wchar_t *)in_msg->body.kvp_delete.key,
  372. in_msg->body.kvp_delete.key_size,
  373. UTF16_LITTLE_ENDIAN,
  374. message->body.kvp_delete.key,
  375. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  376. break;
  377. case KVP_OP_ENUMERATE:
  378. message->body.kvp_enum_data.index =
  379. in_msg->body.kvp_enum_data.index;
  380. break;
  381. }
  382. msg->len = sizeof(struct hv_kvp_msg);
  383. rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
  384. if (rc) {
  385. pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
  386. if (cancel_delayed_work_sync(&kvp_work))
  387. kvp_respond_to_host(message, HV_E_FAIL);
  388. }
  389. kfree(msg);
  390. return;
  391. }
  392. /*
  393. * Send a response back to the host.
  394. */
  395. static void
  396. kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
  397. {
  398. struct hv_kvp_msg *kvp_msg;
  399. struct hv_kvp_exchg_msg_value *kvp_data;
  400. char *key_name;
  401. char *value;
  402. struct icmsg_hdr *icmsghdrp;
  403. int keylen = 0;
  404. int valuelen = 0;
  405. u32 buf_len;
  406. struct vmbus_channel *channel;
  407. u64 req_id;
  408. int ret;
  409. /*
  410. * If a transaction is not active; log and return.
  411. */
  412. if (!kvp_transaction.active) {
  413. /*
  414. * This is a spurious call!
  415. */
  416. pr_warn("KVP: Transaction not active\n");
  417. return;
  418. }
  419. /*
  420. * Copy the global state for completing the transaction. Note that
  421. * only one transaction can be active at a time.
  422. */
  423. buf_len = kvp_transaction.recv_len;
  424. channel = kvp_transaction.recv_channel;
  425. req_id = kvp_transaction.recv_req_id;
  426. kvp_transaction.active = false;
  427. icmsghdrp = (struct icmsg_hdr *)
  428. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  429. if (channel->onchannel_callback == NULL)
  430. /*
  431. * We have raced with util driver being unloaded;
  432. * silently return.
  433. */
  434. return;
  435. icmsghdrp->status = error;
  436. /*
  437. * If the error parameter is set, terminate the host's enumeration
  438. * on this pool.
  439. */
  440. if (error) {
  441. /*
  442. * Something failed or we have timedout;
  443. * terminate the current host-side iteration.
  444. */
  445. goto response_done;
  446. }
  447. kvp_msg = (struct hv_kvp_msg *)
  448. &recv_buffer[sizeof(struct vmbuspipe_hdr) +
  449. sizeof(struct icmsg_hdr)];
  450. switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
  451. case KVP_OP_GET_IP_INFO:
  452. ret = process_ob_ipinfo(msg_to_host,
  453. (struct hv_kvp_ip_msg *)kvp_msg,
  454. KVP_OP_GET_IP_INFO);
  455. if (ret < 0)
  456. icmsghdrp->status = HV_E_FAIL;
  457. goto response_done;
  458. case KVP_OP_SET_IP_INFO:
  459. goto response_done;
  460. case KVP_OP_GET:
  461. kvp_data = &kvp_msg->body.kvp_get.data;
  462. goto copy_value;
  463. case KVP_OP_SET:
  464. case KVP_OP_DELETE:
  465. goto response_done;
  466. default:
  467. break;
  468. }
  469. kvp_data = &kvp_msg->body.kvp_enum_data.data;
  470. key_name = msg_to_host->body.kvp_enum_data.data.key;
  471. /*
  472. * The windows host expects the key/value pair to be encoded
  473. * in utf16. Ensure that the key/value size reported to the host
  474. * will be less than or equal to the MAX size (including the
  475. * terminating character).
  476. */
  477. keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
  478. (wchar_t *) kvp_data->key,
  479. (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
  480. kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
  481. copy_value:
  482. value = msg_to_host->body.kvp_enum_data.data.value;
  483. valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
  484. (wchar_t *) kvp_data->value,
  485. (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
  486. kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
  487. /*
  488. * If the utf8s to utf16s conversion failed; notify host
  489. * of the error.
  490. */
  491. if ((keylen < 0) || (valuelen < 0))
  492. icmsghdrp->status = HV_E_FAIL;
  493. kvp_data->value_type = REG_SZ; /* all our values are strings */
  494. response_done:
  495. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  496. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  497. VM_PKT_DATA_INBAND, 0);
  498. poll_channel(channel);
  499. }
  500. /*
  501. * This callback is invoked when we get a KVP message from the host.
  502. * The host ensures that only one KVP transaction can be active at a time.
  503. * KVP implementation in Linux needs to forward the key to a user-mde
  504. * component to retrive the corresponding value. Consequently, we cannot
  505. * respond to the host in the conext of this callback. Since the host
  506. * guarantees that at most only one transaction can be active at a time,
  507. * we stash away the transaction state in a set of global variables.
  508. */
  509. void hv_kvp_onchannelcallback(void *context)
  510. {
  511. struct vmbus_channel *channel = context;
  512. u32 recvlen;
  513. u64 requestid;
  514. struct hv_kvp_msg *kvp_msg;
  515. struct icmsg_hdr *icmsghdrp;
  516. struct icmsg_negotiate *negop = NULL;
  517. int util_fw_version;
  518. int kvp_srv_version;
  519. if (kvp_transaction.active) {
  520. /*
  521. * We will defer processing this callback once
  522. * the current transaction is complete.
  523. */
  524. kvp_transaction.kvp_context = context;
  525. return;
  526. }
  527. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
  528. &requestid);
  529. if (recvlen > 0) {
  530. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
  531. sizeof(struct vmbuspipe_hdr)];
  532. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  533. /*
  534. * Based on the host, select appropriate
  535. * framework and service versions we will
  536. * negotiate.
  537. */
  538. switch (vmbus_proto_version) {
  539. case (VERSION_WS2008):
  540. util_fw_version = UTIL_WS2K8_FW_VERSION;
  541. kvp_srv_version = WS2008_SRV_VERSION;
  542. break;
  543. case (VERSION_WIN7):
  544. util_fw_version = UTIL_FW_VERSION;
  545. kvp_srv_version = WIN7_SRV_VERSION;
  546. break;
  547. default:
  548. util_fw_version = UTIL_FW_VERSION;
  549. kvp_srv_version = WIN8_SRV_VERSION;
  550. }
  551. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  552. recv_buffer, util_fw_version,
  553. kvp_srv_version);
  554. } else {
  555. kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
  556. sizeof(struct vmbuspipe_hdr) +
  557. sizeof(struct icmsg_hdr)];
  558. /*
  559. * Stash away this global state for completing the
  560. * transaction; note transactions are serialized.
  561. */
  562. kvp_transaction.recv_len = recvlen;
  563. kvp_transaction.recv_channel = channel;
  564. kvp_transaction.recv_req_id = requestid;
  565. kvp_transaction.active = true;
  566. kvp_transaction.kvp_msg = kvp_msg;
  567. /*
  568. * Get the information from the
  569. * user-mode component.
  570. * component. This transaction will be
  571. * completed when we get the value from
  572. * the user-mode component.
  573. * Set a timeout to deal with
  574. * user-mode not responding.
  575. */
  576. schedule_work(&kvp_sendkey_work);
  577. schedule_delayed_work(&kvp_work, 5*HZ);
  578. return;
  579. }
  580. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  581. | ICMSGHDRFLAG_RESPONSE;
  582. vmbus_sendpacket(channel, recv_buffer,
  583. recvlen, requestid,
  584. VM_PKT_DATA_INBAND, 0);
  585. }
  586. }
  587. int
  588. hv_kvp_init(struct hv_util_service *srv)
  589. {
  590. int err;
  591. err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
  592. if (err)
  593. return err;
  594. recv_buffer = srv->recv_buffer;
  595. /*
  596. * When this driver loads, the user level daemon that
  597. * processes the host requests may not yet be running.
  598. * Defer processing channel callbacks until the daemon
  599. * has registered.
  600. */
  601. kvp_transaction.active = true;
  602. return 0;
  603. }
  604. void hv_kvp_deinit(void)
  605. {
  606. cn_del_callback(&kvp_id);
  607. cancel_delayed_work_sync(&kvp_work);
  608. cancel_work_sync(&kvp_sendkey_work);
  609. }