channel_mgmt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. /**
  37. * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
  38. * @icmsghdrp: Pointer to msg header structure
  39. * @icmsg_negotiate: Pointer to negotiate message structure
  40. * @buf: Raw buffer channel data
  41. *
  42. * @icmsghdrp is of type &struct icmsg_hdr.
  43. * @negop is of type &struct icmsg_negotiate.
  44. * Set up and fill in default negotiate response message.
  45. *
  46. * The max_fw_version specifies the maximum framework version that
  47. * we can support and max _srv_version specifies the maximum service
  48. * version we can support. A special value MAX_SRV_VER can be
  49. * specified to indicate that we can handle the maximum version
  50. * exposed by the host.
  51. *
  52. * Mainly used by Hyper-V drivers.
  53. */
  54. void vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
  55. struct icmsg_negotiate *negop, u8 *buf,
  56. int max_fw_version, int max_srv_version)
  57. {
  58. int icframe_vercnt;
  59. int icmsg_vercnt;
  60. int i;
  61. icmsghdrp->icmsgsize = 0x10;
  62. negop = (struct icmsg_negotiate *)&buf[
  63. sizeof(struct vmbuspipe_hdr) +
  64. sizeof(struct icmsg_hdr)];
  65. icframe_vercnt = negop->icframe_vercnt;
  66. icmsg_vercnt = negop->icmsg_vercnt;
  67. /*
  68. * Select the framework version number we will
  69. * support.
  70. */
  71. for (i = 0; i < negop->icframe_vercnt; i++) {
  72. if (negop->icversion_data[i].major <= max_fw_version)
  73. icframe_vercnt = negop->icversion_data[i].major;
  74. }
  75. for (i = negop->icframe_vercnt;
  76. (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
  77. if (negop->icversion_data[i].major <= max_srv_version)
  78. icmsg_vercnt = negop->icversion_data[i].major;
  79. }
  80. /*
  81. * Respond with the maximum framework and service
  82. * version numbers we can support.
  83. */
  84. negop->icframe_vercnt = 1;
  85. negop->icmsg_vercnt = 1;
  86. negop->icversion_data[0].major = icframe_vercnt;
  87. negop->icversion_data[0].minor = 0;
  88. negop->icversion_data[1].major = icmsg_vercnt;
  89. negop->icversion_data[1].minor = 0;
  90. }
  91. EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
  92. /*
  93. * alloc_channel - Allocate and initialize a vmbus channel object
  94. */
  95. static struct vmbus_channel *alloc_channel(void)
  96. {
  97. struct vmbus_channel *channel;
  98. channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
  99. if (!channel)
  100. return NULL;
  101. spin_lock_init(&channel->inbound_lock);
  102. channel->controlwq = create_workqueue("hv_vmbus_ctl");
  103. if (!channel->controlwq) {
  104. kfree(channel);
  105. return NULL;
  106. }
  107. return channel;
  108. }
  109. /*
  110. * release_hannel - Release the vmbus channel object itself
  111. */
  112. static void release_channel(struct work_struct *work)
  113. {
  114. struct vmbus_channel *channel = container_of(work,
  115. struct vmbus_channel,
  116. work);
  117. destroy_workqueue(channel->controlwq);
  118. kfree(channel);
  119. }
  120. /*
  121. * free_channel - Release the resources used by the vmbus channel object
  122. */
  123. static void free_channel(struct vmbus_channel *channel)
  124. {
  125. /*
  126. * We have to release the channel's workqueue/thread in the vmbus's
  127. * workqueue/thread context
  128. * ie we can't destroy ourselves.
  129. */
  130. INIT_WORK(&channel->work, release_channel);
  131. queue_work(vmbus_connection.work_queue, &channel->work);
  132. }
  133. /*
  134. * vmbus_process_rescind_offer -
  135. * Rescind the offer by initiating a device removal
  136. */
  137. static void vmbus_process_rescind_offer(struct work_struct *work)
  138. {
  139. struct vmbus_channel *channel = container_of(work,
  140. struct vmbus_channel,
  141. work);
  142. vmbus_device_unregister(channel->device_obj);
  143. }
  144. void vmbus_free_channels(void)
  145. {
  146. struct vmbus_channel *channel;
  147. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  148. vmbus_device_unregister(channel->device_obj);
  149. kfree(channel->device_obj);
  150. free_channel(channel);
  151. }
  152. }
  153. /*
  154. * vmbus_process_offer - Process the offer by creating a channel/device
  155. * associated with this offer
  156. */
  157. static void vmbus_process_offer(struct work_struct *work)
  158. {
  159. struct vmbus_channel *newchannel = container_of(work,
  160. struct vmbus_channel,
  161. work);
  162. struct vmbus_channel *channel;
  163. bool fnew = true;
  164. int ret;
  165. unsigned long flags;
  166. /* The next possible work is rescind handling */
  167. INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
  168. /* Make sure this is a new offer */
  169. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  170. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  171. if (!uuid_le_cmp(channel->offermsg.offer.if_type,
  172. newchannel->offermsg.offer.if_type) &&
  173. !uuid_le_cmp(channel->offermsg.offer.if_instance,
  174. newchannel->offermsg.offer.if_instance)) {
  175. fnew = false;
  176. break;
  177. }
  178. }
  179. if (fnew)
  180. list_add_tail(&newchannel->listentry,
  181. &vmbus_connection.chn_list);
  182. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  183. if (!fnew) {
  184. free_channel(newchannel);
  185. return;
  186. }
  187. /*
  188. * Start the process of binding this offer to the driver
  189. * We need to set the DeviceObject field before calling
  190. * vmbus_child_dev_add()
  191. */
  192. newchannel->device_obj = vmbus_device_create(
  193. &newchannel->offermsg.offer.if_type,
  194. &newchannel->offermsg.offer.if_instance,
  195. newchannel);
  196. /*
  197. * Add the new device to the bus. This will kick off device-driver
  198. * binding which eventually invokes the device driver's AddDevice()
  199. * method.
  200. */
  201. ret = vmbus_device_register(newchannel->device_obj);
  202. if (ret != 0) {
  203. pr_err("unable to add child device object (relid %d)\n",
  204. newchannel->offermsg.child_relid);
  205. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  206. list_del(&newchannel->listentry);
  207. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  208. kfree(newchannel->device_obj);
  209. free_channel(newchannel);
  210. } else {
  211. /*
  212. * This state is used to indicate a successful open
  213. * so that when we do close the channel normally, we
  214. * can cleanup properly
  215. */
  216. newchannel->state = CHANNEL_OPEN_STATE;
  217. }
  218. }
  219. enum {
  220. IDE = 0,
  221. SCSI,
  222. NIC,
  223. MAX_PERF_CHN,
  224. };
  225. /*
  226. * This is an array of channels (devices) that are performance critical.
  227. * We attempt to distribute the interrupt load for these devices across
  228. * all available CPUs.
  229. */
  230. static const uuid_le hp_devs[] = {
  231. /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
  232. /* IDE */
  233. {
  234. .b = {
  235. 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
  236. 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5
  237. }
  238. },
  239. /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
  240. /* Storage - SCSI */
  241. {
  242. .b = {
  243. 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
  244. 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
  245. }
  246. },
  247. /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
  248. /* Network */
  249. {
  250. .b = {
  251. 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
  252. 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
  253. }
  254. },
  255. };
  256. /*
  257. * We use this state to statically distribute the channel interrupt load.
  258. */
  259. static u32 next_vp;
  260. /*
  261. * Starting with Win8, we can statically distribute the incoming
  262. * channel interrupt load by binding a channel to VCPU. We
  263. * implement here a simple round robin scheme for distributing
  264. * the interrupt load.
  265. * We will bind channels that are not performance critical to cpu 0 and
  266. * performance critical channels (IDE, SCSI and Network) will be uniformly
  267. * distributed across all available CPUs.
  268. */
  269. static u32 get_vp_index(uuid_le *type_guid)
  270. {
  271. u32 cur_cpu;
  272. int i;
  273. bool perf_chn = false;
  274. u32 max_cpus = num_online_cpus();
  275. for (i = IDE; i < MAX_PERF_CHN; i++) {
  276. if (!memcmp(type_guid->b, hp_devs[i].b,
  277. sizeof(uuid_le))) {
  278. perf_chn = true;
  279. break;
  280. }
  281. }
  282. if ((vmbus_proto_version == VERSION_WS2008) ||
  283. (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
  284. /*
  285. * Prior to win8, all channel interrupts are
  286. * delivered on cpu 0.
  287. * Also if the channel is not a performance critical
  288. * channel, bind it to cpu 0.
  289. */
  290. return 0;
  291. }
  292. cur_cpu = (++next_vp % max_cpus);
  293. return hv_context.vp_index[cur_cpu];
  294. }
  295. /*
  296. * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
  297. *
  298. */
  299. static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
  300. {
  301. struct vmbus_channel_offer_channel *offer;
  302. struct vmbus_channel *newchannel;
  303. offer = (struct vmbus_channel_offer_channel *)hdr;
  304. /* Allocate the channel object and save this offer. */
  305. newchannel = alloc_channel();
  306. if (!newchannel) {
  307. pr_err("Unable to allocate channel object\n");
  308. return;
  309. }
  310. /*
  311. * By default we setup state to enable batched
  312. * reading. A specific service can choose to
  313. * disable this prior to opening the channel.
  314. */
  315. newchannel->batched_reading = true;
  316. /*
  317. * Setup state for signalling the host.
  318. */
  319. newchannel->sig_event = (struct hv_input_signal_event *)
  320. (ALIGN((unsigned long)
  321. &newchannel->sig_buf,
  322. HV_HYPERCALL_PARAM_ALIGN));
  323. newchannel->sig_event->connectionid.asu32 = 0;
  324. newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
  325. newchannel->sig_event->flag_number = 0;
  326. newchannel->sig_event->rsvdz = 0;
  327. if (vmbus_proto_version != VERSION_WS2008) {
  328. newchannel->is_dedicated_interrupt =
  329. (offer->is_dedicated_interrupt != 0);
  330. newchannel->sig_event->connectionid.u.id =
  331. offer->connection_id;
  332. }
  333. newchannel->target_vp = get_vp_index(&offer->offer.if_type);
  334. memcpy(&newchannel->offermsg, offer,
  335. sizeof(struct vmbus_channel_offer_channel));
  336. newchannel->monitor_grp = (u8)offer->monitorid / 32;
  337. newchannel->monitor_bit = (u8)offer->monitorid % 32;
  338. INIT_WORK(&newchannel->work, vmbus_process_offer);
  339. queue_work(newchannel->controlwq, &newchannel->work);
  340. }
  341. /*
  342. * vmbus_onoffer_rescind - Rescind offer handler.
  343. *
  344. * We queue a work item to process this offer synchronously
  345. */
  346. static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
  347. {
  348. struct vmbus_channel_rescind_offer *rescind;
  349. struct vmbus_channel *channel;
  350. rescind = (struct vmbus_channel_rescind_offer *)hdr;
  351. channel = relid2channel(rescind->child_relid);
  352. if (channel == NULL)
  353. /* Just return here, no channel found */
  354. return;
  355. /* work is initialized for vmbus_process_rescind_offer() from
  356. * vmbus_process_offer() where the channel got created */
  357. queue_work(channel->controlwq, &channel->work);
  358. }
  359. /*
  360. * vmbus_onoffers_delivered -
  361. * This is invoked when all offers have been delivered.
  362. *
  363. * Nothing to do here.
  364. */
  365. static void vmbus_onoffers_delivered(
  366. struct vmbus_channel_message_header *hdr)
  367. {
  368. }
  369. /*
  370. * vmbus_onopen_result - Open result handler.
  371. *
  372. * This is invoked when we received a response to our channel open request.
  373. * Find the matching request, copy the response and signal the requesting
  374. * thread.
  375. */
  376. static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
  377. {
  378. struct vmbus_channel_open_result *result;
  379. struct vmbus_channel_msginfo *msginfo;
  380. struct vmbus_channel_message_header *requestheader;
  381. struct vmbus_channel_open_channel *openmsg;
  382. unsigned long flags;
  383. result = (struct vmbus_channel_open_result *)hdr;
  384. /*
  385. * Find the open msg, copy the result and signal/unblock the wait event
  386. */
  387. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  388. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  389. msglistentry) {
  390. requestheader =
  391. (struct vmbus_channel_message_header *)msginfo->msg;
  392. if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
  393. openmsg =
  394. (struct vmbus_channel_open_channel *)msginfo->msg;
  395. if (openmsg->child_relid == result->child_relid &&
  396. openmsg->openid == result->openid) {
  397. memcpy(&msginfo->response.open_result,
  398. result,
  399. sizeof(
  400. struct vmbus_channel_open_result));
  401. complete(&msginfo->waitevent);
  402. break;
  403. }
  404. }
  405. }
  406. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  407. }
  408. /*
  409. * vmbus_ongpadl_created - GPADL created handler.
  410. *
  411. * This is invoked when we received a response to our gpadl create request.
  412. * Find the matching request, copy the response and signal the requesting
  413. * thread.
  414. */
  415. static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
  416. {
  417. struct vmbus_channel_gpadl_created *gpadlcreated;
  418. struct vmbus_channel_msginfo *msginfo;
  419. struct vmbus_channel_message_header *requestheader;
  420. struct vmbus_channel_gpadl_header *gpadlheader;
  421. unsigned long flags;
  422. gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
  423. /*
  424. * Find the establish msg, copy the result and signal/unblock the wait
  425. * event
  426. */
  427. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  428. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  429. msglistentry) {
  430. requestheader =
  431. (struct vmbus_channel_message_header *)msginfo->msg;
  432. if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
  433. gpadlheader =
  434. (struct vmbus_channel_gpadl_header *)requestheader;
  435. if ((gpadlcreated->child_relid ==
  436. gpadlheader->child_relid) &&
  437. (gpadlcreated->gpadl == gpadlheader->gpadl)) {
  438. memcpy(&msginfo->response.gpadl_created,
  439. gpadlcreated,
  440. sizeof(
  441. struct vmbus_channel_gpadl_created));
  442. complete(&msginfo->waitevent);
  443. break;
  444. }
  445. }
  446. }
  447. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  448. }
  449. /*
  450. * vmbus_ongpadl_torndown - GPADL torndown handler.
  451. *
  452. * This is invoked when we received a response to our gpadl teardown request.
  453. * Find the matching request, copy the response and signal the requesting
  454. * thread.
  455. */
  456. static void vmbus_ongpadl_torndown(
  457. struct vmbus_channel_message_header *hdr)
  458. {
  459. struct vmbus_channel_gpadl_torndown *gpadl_torndown;
  460. struct vmbus_channel_msginfo *msginfo;
  461. struct vmbus_channel_message_header *requestheader;
  462. struct vmbus_channel_gpadl_teardown *gpadl_teardown;
  463. unsigned long flags;
  464. gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
  465. /*
  466. * Find the open msg, copy the result and signal/unblock the wait event
  467. */
  468. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  469. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  470. msglistentry) {
  471. requestheader =
  472. (struct vmbus_channel_message_header *)msginfo->msg;
  473. if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
  474. gpadl_teardown =
  475. (struct vmbus_channel_gpadl_teardown *)requestheader;
  476. if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
  477. memcpy(&msginfo->response.gpadl_torndown,
  478. gpadl_torndown,
  479. sizeof(
  480. struct vmbus_channel_gpadl_torndown));
  481. complete(&msginfo->waitevent);
  482. break;
  483. }
  484. }
  485. }
  486. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  487. }
  488. /*
  489. * vmbus_onversion_response - Version response handler
  490. *
  491. * This is invoked when we received a response to our initiate contact request.
  492. * Find the matching request, copy the response and signal the requesting
  493. * thread.
  494. */
  495. static void vmbus_onversion_response(
  496. struct vmbus_channel_message_header *hdr)
  497. {
  498. struct vmbus_channel_msginfo *msginfo;
  499. struct vmbus_channel_message_header *requestheader;
  500. struct vmbus_channel_version_response *version_response;
  501. unsigned long flags;
  502. version_response = (struct vmbus_channel_version_response *)hdr;
  503. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  504. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  505. msglistentry) {
  506. requestheader =
  507. (struct vmbus_channel_message_header *)msginfo->msg;
  508. if (requestheader->msgtype ==
  509. CHANNELMSG_INITIATE_CONTACT) {
  510. memcpy(&msginfo->response.version_response,
  511. version_response,
  512. sizeof(struct vmbus_channel_version_response));
  513. complete(&msginfo->waitevent);
  514. }
  515. }
  516. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  517. }
  518. /* Channel message dispatch table */
  519. static struct vmbus_channel_message_table_entry
  520. channel_message_table[CHANNELMSG_COUNT] = {
  521. {CHANNELMSG_INVALID, NULL},
  522. {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
  523. {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
  524. {CHANNELMSG_REQUESTOFFERS, NULL},
  525. {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
  526. {CHANNELMSG_OPENCHANNEL, NULL},
  527. {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
  528. {CHANNELMSG_CLOSECHANNEL, NULL},
  529. {CHANNELMSG_GPADL_HEADER, NULL},
  530. {CHANNELMSG_GPADL_BODY, NULL},
  531. {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
  532. {CHANNELMSG_GPADL_TEARDOWN, NULL},
  533. {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
  534. {CHANNELMSG_RELID_RELEASED, NULL},
  535. {CHANNELMSG_INITIATE_CONTACT, NULL},
  536. {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
  537. {CHANNELMSG_UNLOAD, NULL},
  538. };
  539. /*
  540. * vmbus_onmessage - Handler for channel protocol messages.
  541. *
  542. * This is invoked in the vmbus worker thread context.
  543. */
  544. void vmbus_onmessage(void *context)
  545. {
  546. struct hv_message *msg = context;
  547. struct vmbus_channel_message_header *hdr;
  548. int size;
  549. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  550. size = msg->header.payload_size;
  551. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  552. pr_err("Received invalid channel message type %d size %d\n",
  553. hdr->msgtype, size);
  554. print_hex_dump_bytes("", DUMP_PREFIX_NONE,
  555. (unsigned char *)msg->u.payload, size);
  556. return;
  557. }
  558. if (channel_message_table[hdr->msgtype].message_handler)
  559. channel_message_table[hdr->msgtype].message_handler(hdr);
  560. else
  561. pr_err("Unhandled channel message type %d\n", hdr->msgtype);
  562. }
  563. /*
  564. * vmbus_request_offers - Send a request to get all our pending offers.
  565. */
  566. int vmbus_request_offers(void)
  567. {
  568. struct vmbus_channel_message_header *msg;
  569. struct vmbus_channel_msginfo *msginfo;
  570. int ret, t;
  571. msginfo = kmalloc(sizeof(*msginfo) +
  572. sizeof(struct vmbus_channel_message_header),
  573. GFP_KERNEL);
  574. if (!msginfo)
  575. return -ENOMEM;
  576. init_completion(&msginfo->waitevent);
  577. msg = (struct vmbus_channel_message_header *)msginfo->msg;
  578. msg->msgtype = CHANNELMSG_REQUESTOFFERS;
  579. ret = vmbus_post_msg(msg,
  580. sizeof(struct vmbus_channel_message_header));
  581. if (ret != 0) {
  582. pr_err("Unable to request offers - %d\n", ret);
  583. goto cleanup;
  584. }
  585. t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
  586. if (t == 0) {
  587. ret = -ETIMEDOUT;
  588. goto cleanup;
  589. }
  590. cleanup:
  591. kfree(msginfo);
  592. return ret;
  593. }
  594. /* eof */