channel_mgmt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/wait.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/list.h>
  28. #include <linux/module.h>
  29. #include <linux/completion.h>
  30. #include <linux/hyperv.h>
  31. #include "hyperv_vmbus.h"
  32. struct vmbus_channel_message_table_entry {
  33. enum vmbus_channel_message_type message_type;
  34. void (*message_handler)(struct vmbus_channel_message_header *msg);
  35. };
  36. #define MAX_MSG_TYPES 4
  37. #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 8
  38. static const uuid_le
  39. supported_device_classes[MAX_NUM_DEVICE_CLASSES_SUPPORTED] = {
  40. /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
  41. /* Storage - SCSI */
  42. {
  43. .b = {
  44. 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
  45. 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
  46. }
  47. },
  48. /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
  49. /* Network */
  50. {
  51. .b = {
  52. 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
  53. 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
  54. }
  55. },
  56. /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
  57. /* Input */
  58. {
  59. .b = {
  60. 0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
  61. 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A
  62. }
  63. },
  64. /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
  65. /* IDE */
  66. {
  67. .b = {
  68. 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
  69. 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5
  70. }
  71. },
  72. /* 0E0B6031-5213-4934-818B-38D90CED39DB */
  73. /* Shutdown */
  74. {
  75. .b = {
  76. 0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
  77. 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB
  78. }
  79. },
  80. /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */
  81. /* TimeSync */
  82. {
  83. .b = {
  84. 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
  85. 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf
  86. }
  87. },
  88. /* {57164f39-9115-4e78-ab55-382f3bd5422d} */
  89. /* Heartbeat */
  90. {
  91. .b = {
  92. 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
  93. 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d
  94. }
  95. },
  96. /* {A9A0F4E7-5A45-4d96-B827-8A841E8C03E6} */
  97. /* KVP */
  98. {
  99. .b = {
  100. 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
  101. 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6
  102. }
  103. },
  104. };
  105. /**
  106. * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
  107. * @icmsghdrp: Pointer to msg header structure
  108. * @icmsg_negotiate: Pointer to negotiate message structure
  109. * @buf: Raw buffer channel data
  110. *
  111. * @icmsghdrp is of type &struct icmsg_hdr.
  112. * @negop is of type &struct icmsg_negotiate.
  113. * Set up and fill in default negotiate response message. This response can
  114. * come from both the vmbus driver and the hv_utils driver. The current api
  115. * will respond properly to both Windows 2008 and Windows 2008-R2 operating
  116. * systems.
  117. *
  118. * Mainly used by Hyper-V drivers.
  119. */
  120. void vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
  121. struct icmsg_negotiate *negop, u8 *buf)
  122. {
  123. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  124. icmsghdrp->icmsgsize = 0x10;
  125. negop = (struct icmsg_negotiate *)&buf[
  126. sizeof(struct vmbuspipe_hdr) +
  127. sizeof(struct icmsg_hdr)];
  128. if (negop->icframe_vercnt == 2 &&
  129. negop->icversion_data[1].major == 3) {
  130. negop->icversion_data[0].major = 3;
  131. negop->icversion_data[0].minor = 0;
  132. negop->icversion_data[1].major = 3;
  133. negop->icversion_data[1].minor = 0;
  134. } else {
  135. negop->icversion_data[0].major = 1;
  136. negop->icversion_data[0].minor = 0;
  137. negop->icversion_data[1].major = 1;
  138. negop->icversion_data[1].minor = 0;
  139. }
  140. negop->icframe_vercnt = 1;
  141. negop->icmsg_vercnt = 1;
  142. }
  143. }
  144. EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
  145. /*
  146. * alloc_channel - Allocate and initialize a vmbus channel object
  147. */
  148. static struct vmbus_channel *alloc_channel(void)
  149. {
  150. struct vmbus_channel *channel;
  151. channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
  152. if (!channel)
  153. return NULL;
  154. spin_lock_init(&channel->inbound_lock);
  155. channel->controlwq = create_workqueue("hv_vmbus_ctl");
  156. if (!channel->controlwq) {
  157. kfree(channel);
  158. return NULL;
  159. }
  160. return channel;
  161. }
  162. /*
  163. * release_hannel - Release the vmbus channel object itself
  164. */
  165. static void release_channel(struct work_struct *work)
  166. {
  167. struct vmbus_channel *channel = container_of(work,
  168. struct vmbus_channel,
  169. work);
  170. destroy_workqueue(channel->controlwq);
  171. kfree(channel);
  172. }
  173. /*
  174. * free_channel - Release the resources used by the vmbus channel object
  175. */
  176. static void free_channel(struct vmbus_channel *channel)
  177. {
  178. /*
  179. * We have to release the channel's workqueue/thread in the vmbus's
  180. * workqueue/thread context
  181. * ie we can't destroy ourselves.
  182. */
  183. INIT_WORK(&channel->work, release_channel);
  184. queue_work(vmbus_connection.work_queue, &channel->work);
  185. }
  186. /*
  187. * vmbus_process_rescind_offer -
  188. * Rescind the offer by initiating a device removal
  189. */
  190. static void vmbus_process_rescind_offer(struct work_struct *work)
  191. {
  192. struct vmbus_channel *channel = container_of(work,
  193. struct vmbus_channel,
  194. work);
  195. vmbus_device_unregister(channel->device_obj);
  196. }
  197. /*
  198. * vmbus_process_offer - Process the offer by creating a channel/device
  199. * associated with this offer
  200. */
  201. static void vmbus_process_offer(struct work_struct *work)
  202. {
  203. struct vmbus_channel *newchannel = container_of(work,
  204. struct vmbus_channel,
  205. work);
  206. struct vmbus_channel *channel;
  207. bool fnew = true;
  208. int ret;
  209. unsigned long flags;
  210. /* The next possible work is rescind handling */
  211. INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
  212. /* Make sure this is a new offer */
  213. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  214. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  215. if (!uuid_le_cmp(channel->offermsg.offer.if_type,
  216. newchannel->offermsg.offer.if_type) &&
  217. !uuid_le_cmp(channel->offermsg.offer.if_instance,
  218. newchannel->offermsg.offer.if_instance)) {
  219. fnew = false;
  220. break;
  221. }
  222. }
  223. if (fnew)
  224. list_add_tail(&newchannel->listentry,
  225. &vmbus_connection.chn_list);
  226. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  227. if (!fnew) {
  228. free_channel(newchannel);
  229. return;
  230. }
  231. /*
  232. * Start the process of binding this offer to the driver
  233. * We need to set the DeviceObject field before calling
  234. * vmbus_child_dev_add()
  235. */
  236. newchannel->device_obj = vmbus_device_create(
  237. &newchannel->offermsg.offer.if_type,
  238. &newchannel->offermsg.offer.if_instance,
  239. newchannel);
  240. /*
  241. * Add the new device to the bus. This will kick off device-driver
  242. * binding which eventually invokes the device driver's AddDevice()
  243. * method.
  244. */
  245. ret = vmbus_device_register(newchannel->device_obj);
  246. if (ret != 0) {
  247. pr_err("unable to add child device object (relid %d)\n",
  248. newchannel->offermsg.child_relid);
  249. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  250. list_del(&newchannel->listentry);
  251. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  252. free_channel(newchannel);
  253. } else {
  254. /*
  255. * This state is used to indicate a successful open
  256. * so that when we do close the channel normally, we
  257. * can cleanup properly
  258. */
  259. newchannel->state = CHANNEL_OPEN_STATE;
  260. }
  261. }
  262. /*
  263. * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
  264. *
  265. */
  266. static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
  267. {
  268. struct vmbus_channel_offer_channel *offer;
  269. struct vmbus_channel *newchannel;
  270. uuid_le *guidtype;
  271. uuid_le *guidinstance;
  272. int i;
  273. int fsupported = 0;
  274. offer = (struct vmbus_channel_offer_channel *)hdr;
  275. for (i = 0; i < MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++) {
  276. if (!uuid_le_cmp(offer->offer.if_type,
  277. supported_device_classes[i])) {
  278. fsupported = 1;
  279. break;
  280. }
  281. }
  282. if (!fsupported)
  283. return;
  284. guidtype = &offer->offer.if_type;
  285. guidinstance = &offer->offer.if_instance;
  286. /* Allocate the channel object and save this offer. */
  287. newchannel = alloc_channel();
  288. if (!newchannel) {
  289. pr_err("Unable to allocate channel object\n");
  290. return;
  291. }
  292. memcpy(&newchannel->offermsg, offer,
  293. sizeof(struct vmbus_channel_offer_channel));
  294. newchannel->monitor_grp = (u8)offer->monitorid / 32;
  295. newchannel->monitor_bit = (u8)offer->monitorid % 32;
  296. INIT_WORK(&newchannel->work, vmbus_process_offer);
  297. queue_work(newchannel->controlwq, &newchannel->work);
  298. }
  299. /*
  300. * vmbus_onoffer_rescind - Rescind offer handler.
  301. *
  302. * We queue a work item to process this offer synchronously
  303. */
  304. static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
  305. {
  306. struct vmbus_channel_rescind_offer *rescind;
  307. struct vmbus_channel *channel;
  308. rescind = (struct vmbus_channel_rescind_offer *)hdr;
  309. channel = relid2channel(rescind->child_relid);
  310. if (channel == NULL)
  311. /* Just return here, no channel found */
  312. return;
  313. /* work is initialized for vmbus_process_rescind_offer() from
  314. * vmbus_process_offer() where the channel got created */
  315. queue_work(channel->controlwq, &channel->work);
  316. }
  317. /*
  318. * vmbus_onoffers_delivered -
  319. * This is invoked when all offers have been delivered.
  320. *
  321. * Nothing to do here.
  322. */
  323. static void vmbus_onoffers_delivered(
  324. struct vmbus_channel_message_header *hdr)
  325. {
  326. }
  327. /*
  328. * vmbus_onopen_result - Open result handler.
  329. *
  330. * This is invoked when we received a response to our channel open request.
  331. * Find the matching request, copy the response and signal the requesting
  332. * thread.
  333. */
  334. static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
  335. {
  336. struct vmbus_channel_open_result *result;
  337. struct vmbus_channel_msginfo *msginfo;
  338. struct vmbus_channel_message_header *requestheader;
  339. struct vmbus_channel_open_channel *openmsg;
  340. unsigned long flags;
  341. result = (struct vmbus_channel_open_result *)hdr;
  342. /*
  343. * Find the open msg, copy the result and signal/unblock the wait event
  344. */
  345. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  346. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  347. msglistentry) {
  348. requestheader =
  349. (struct vmbus_channel_message_header *)msginfo->msg;
  350. if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
  351. openmsg =
  352. (struct vmbus_channel_open_channel *)msginfo->msg;
  353. if (openmsg->child_relid == result->child_relid &&
  354. openmsg->openid == result->openid) {
  355. memcpy(&msginfo->response.open_result,
  356. result,
  357. sizeof(
  358. struct vmbus_channel_open_result));
  359. complete(&msginfo->waitevent);
  360. break;
  361. }
  362. }
  363. }
  364. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  365. }
  366. /*
  367. * vmbus_ongpadl_created - GPADL created handler.
  368. *
  369. * This is invoked when we received a response to our gpadl create request.
  370. * Find the matching request, copy the response and signal the requesting
  371. * thread.
  372. */
  373. static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
  374. {
  375. struct vmbus_channel_gpadl_created *gpadlcreated;
  376. struct vmbus_channel_msginfo *msginfo;
  377. struct vmbus_channel_message_header *requestheader;
  378. struct vmbus_channel_gpadl_header *gpadlheader;
  379. unsigned long flags;
  380. gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
  381. /*
  382. * Find the establish msg, copy the result and signal/unblock the wait
  383. * event
  384. */
  385. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  386. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  387. msglistentry) {
  388. requestheader =
  389. (struct vmbus_channel_message_header *)msginfo->msg;
  390. if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
  391. gpadlheader =
  392. (struct vmbus_channel_gpadl_header *)requestheader;
  393. if ((gpadlcreated->child_relid ==
  394. gpadlheader->child_relid) &&
  395. (gpadlcreated->gpadl == gpadlheader->gpadl)) {
  396. memcpy(&msginfo->response.gpadl_created,
  397. gpadlcreated,
  398. sizeof(
  399. struct vmbus_channel_gpadl_created));
  400. complete(&msginfo->waitevent);
  401. break;
  402. }
  403. }
  404. }
  405. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  406. }
  407. /*
  408. * vmbus_ongpadl_torndown - GPADL torndown handler.
  409. *
  410. * This is invoked when we received a response to our gpadl teardown request.
  411. * Find the matching request, copy the response and signal the requesting
  412. * thread.
  413. */
  414. static void vmbus_ongpadl_torndown(
  415. struct vmbus_channel_message_header *hdr)
  416. {
  417. struct vmbus_channel_gpadl_torndown *gpadl_torndown;
  418. struct vmbus_channel_msginfo *msginfo;
  419. struct vmbus_channel_message_header *requestheader;
  420. struct vmbus_channel_gpadl_teardown *gpadl_teardown;
  421. unsigned long flags;
  422. gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
  423. /*
  424. * Find the open msg, copy the result and signal/unblock the wait event
  425. */
  426. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  427. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  428. msglistentry) {
  429. requestheader =
  430. (struct vmbus_channel_message_header *)msginfo->msg;
  431. if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
  432. gpadl_teardown =
  433. (struct vmbus_channel_gpadl_teardown *)requestheader;
  434. if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
  435. memcpy(&msginfo->response.gpadl_torndown,
  436. gpadl_torndown,
  437. sizeof(
  438. struct vmbus_channel_gpadl_torndown));
  439. complete(&msginfo->waitevent);
  440. break;
  441. }
  442. }
  443. }
  444. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  445. }
  446. /*
  447. * vmbus_onversion_response - Version response handler
  448. *
  449. * This is invoked when we received a response to our initiate contact request.
  450. * Find the matching request, copy the response and signal the requesting
  451. * thread.
  452. */
  453. static void vmbus_onversion_response(
  454. struct vmbus_channel_message_header *hdr)
  455. {
  456. struct vmbus_channel_msginfo *msginfo;
  457. struct vmbus_channel_message_header *requestheader;
  458. struct vmbus_channel_initiate_contact *initiate;
  459. struct vmbus_channel_version_response *version_response;
  460. unsigned long flags;
  461. version_response = (struct vmbus_channel_version_response *)hdr;
  462. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  463. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  464. msglistentry) {
  465. requestheader =
  466. (struct vmbus_channel_message_header *)msginfo->msg;
  467. if (requestheader->msgtype ==
  468. CHANNELMSG_INITIATE_CONTACT) {
  469. initiate =
  470. (struct vmbus_channel_initiate_contact *)requestheader;
  471. memcpy(&msginfo->response.version_response,
  472. version_response,
  473. sizeof(struct vmbus_channel_version_response));
  474. complete(&msginfo->waitevent);
  475. }
  476. }
  477. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  478. }
  479. /* Channel message dispatch table */
  480. static struct vmbus_channel_message_table_entry
  481. channel_message_table[CHANNELMSG_COUNT] = {
  482. {CHANNELMSG_INVALID, NULL},
  483. {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
  484. {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
  485. {CHANNELMSG_REQUESTOFFERS, NULL},
  486. {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
  487. {CHANNELMSG_OPENCHANNEL, NULL},
  488. {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
  489. {CHANNELMSG_CLOSECHANNEL, NULL},
  490. {CHANNELMSG_GPADL_HEADER, NULL},
  491. {CHANNELMSG_GPADL_BODY, NULL},
  492. {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
  493. {CHANNELMSG_GPADL_TEARDOWN, NULL},
  494. {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
  495. {CHANNELMSG_RELID_RELEASED, NULL},
  496. {CHANNELMSG_INITIATE_CONTACT, NULL},
  497. {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
  498. {CHANNELMSG_UNLOAD, NULL},
  499. };
  500. /*
  501. * vmbus_onmessage - Handler for channel protocol messages.
  502. *
  503. * This is invoked in the vmbus worker thread context.
  504. */
  505. void vmbus_onmessage(void *context)
  506. {
  507. struct hv_message *msg = context;
  508. struct vmbus_channel_message_header *hdr;
  509. int size;
  510. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  511. size = msg->header.payload_size;
  512. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  513. pr_err("Received invalid channel message type %d size %d\n",
  514. hdr->msgtype, size);
  515. print_hex_dump_bytes("", DUMP_PREFIX_NONE,
  516. (unsigned char *)msg->u.payload, size);
  517. return;
  518. }
  519. if (channel_message_table[hdr->msgtype].message_handler)
  520. channel_message_table[hdr->msgtype].message_handler(hdr);
  521. else
  522. pr_err("Unhandled channel message type %d\n", hdr->msgtype);
  523. }
  524. /*
  525. * vmbus_request_offers - Send a request to get all our pending offers.
  526. */
  527. int vmbus_request_offers(void)
  528. {
  529. struct vmbus_channel_message_header *msg;
  530. struct vmbus_channel_msginfo *msginfo;
  531. int ret, t;
  532. msginfo = kmalloc(sizeof(*msginfo) +
  533. sizeof(struct vmbus_channel_message_header),
  534. GFP_KERNEL);
  535. if (!msginfo)
  536. return -ENOMEM;
  537. init_completion(&msginfo->waitevent);
  538. msg = (struct vmbus_channel_message_header *)msginfo->msg;
  539. msg->msgtype = CHANNELMSG_REQUESTOFFERS;
  540. ret = vmbus_post_msg(msg,
  541. sizeof(struct vmbus_channel_message_header));
  542. if (ret != 0) {
  543. pr_err("Unable to request offers - %d\n", ret);
  544. goto cleanup;
  545. }
  546. t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
  547. if (t == 0) {
  548. ret = -ETIMEDOUT;
  549. goto cleanup;
  550. }
  551. cleanup:
  552. kfree(msginfo);
  553. return ret;
  554. }
  555. /* eof */