channel_mgmt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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 fw_version specifies the framework version that
  47. * we can support and srv_version specifies the service
  48. * version we can support.
  49. *
  50. * Mainly used by Hyper-V drivers.
  51. */
  52. bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
  53. struct icmsg_negotiate *negop, u8 *buf,
  54. int fw_version, int srv_version)
  55. {
  56. int icframe_major, icframe_minor;
  57. int icmsg_major, icmsg_minor;
  58. int fw_major, fw_minor;
  59. int srv_major, srv_minor;
  60. int i;
  61. bool found_match = false;
  62. icmsghdrp->icmsgsize = 0x10;
  63. fw_major = (fw_version >> 16);
  64. fw_minor = (fw_version & 0xFFFF);
  65. srv_major = (srv_version >> 16);
  66. srv_minor = (srv_version & 0xFFFF);
  67. negop = (struct icmsg_negotiate *)&buf[
  68. sizeof(struct vmbuspipe_hdr) +
  69. sizeof(struct icmsg_hdr)];
  70. icframe_major = negop->icframe_vercnt;
  71. icframe_minor = 0;
  72. icmsg_major = negop->icmsg_vercnt;
  73. icmsg_minor = 0;
  74. /*
  75. * Select the framework version number we will
  76. * support.
  77. */
  78. for (i = 0; i < negop->icframe_vercnt; i++) {
  79. if ((negop->icversion_data[i].major == fw_major) &&
  80. (negop->icversion_data[i].minor == fw_minor)) {
  81. icframe_major = negop->icversion_data[i].major;
  82. icframe_minor = negop->icversion_data[i].minor;
  83. found_match = true;
  84. }
  85. }
  86. if (!found_match)
  87. goto fw_error;
  88. found_match = false;
  89. for (i = negop->icframe_vercnt;
  90. (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
  91. if ((negop->icversion_data[i].major == srv_major) &&
  92. (negop->icversion_data[i].minor == srv_minor)) {
  93. icmsg_major = negop->icversion_data[i].major;
  94. icmsg_minor = negop->icversion_data[i].minor;
  95. found_match = true;
  96. }
  97. }
  98. /*
  99. * Respond with the framework and service
  100. * version numbers we can support.
  101. */
  102. fw_error:
  103. if (!found_match) {
  104. negop->icframe_vercnt = 0;
  105. negop->icmsg_vercnt = 0;
  106. } else {
  107. negop->icframe_vercnt = 1;
  108. negop->icmsg_vercnt = 1;
  109. }
  110. negop->icversion_data[0].major = icframe_major;
  111. negop->icversion_data[0].minor = icframe_minor;
  112. negop->icversion_data[1].major = icmsg_major;
  113. negop->icversion_data[1].minor = icmsg_minor;
  114. return found_match;
  115. }
  116. EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
  117. /*
  118. * alloc_channel - Allocate and initialize a vmbus channel object
  119. */
  120. static struct vmbus_channel *alloc_channel(void)
  121. {
  122. struct vmbus_channel *channel;
  123. channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
  124. if (!channel)
  125. return NULL;
  126. spin_lock_init(&channel->inbound_lock);
  127. spin_lock_init(&channel->sc_lock);
  128. INIT_LIST_HEAD(&channel->sc_list);
  129. channel->controlwq = create_workqueue("hv_vmbus_ctl");
  130. if (!channel->controlwq) {
  131. kfree(channel);
  132. return NULL;
  133. }
  134. return channel;
  135. }
  136. /*
  137. * release_hannel - Release the vmbus channel object itself
  138. */
  139. static void release_channel(struct work_struct *work)
  140. {
  141. struct vmbus_channel *channel = container_of(work,
  142. struct vmbus_channel,
  143. work);
  144. destroy_workqueue(channel->controlwq);
  145. kfree(channel);
  146. }
  147. /*
  148. * free_channel - Release the resources used by the vmbus channel object
  149. */
  150. static void free_channel(struct vmbus_channel *channel)
  151. {
  152. /*
  153. * We have to release the channel's workqueue/thread in the vmbus's
  154. * workqueue/thread context
  155. * ie we can't destroy ourselves.
  156. */
  157. INIT_WORK(&channel->work, release_channel);
  158. queue_work(vmbus_connection.work_queue, &channel->work);
  159. }
  160. /*
  161. * vmbus_process_rescind_offer -
  162. * Rescind the offer by initiating a device removal
  163. */
  164. static void vmbus_process_rescind_offer(struct work_struct *work)
  165. {
  166. struct vmbus_channel *channel = container_of(work,
  167. struct vmbus_channel,
  168. work);
  169. unsigned long flags;
  170. struct vmbus_channel *primary_channel;
  171. struct vmbus_channel_relid_released msg;
  172. vmbus_device_unregister(channel->device_obj);
  173. memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
  174. msg.child_relid = channel->offermsg.child_relid;
  175. msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
  176. vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
  177. if (channel->primary_channel == NULL) {
  178. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  179. list_del(&channel->listentry);
  180. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  181. } else {
  182. primary_channel = channel->primary_channel;
  183. spin_lock_irqsave(&primary_channel->sc_lock, flags);
  184. list_del(&channel->listentry);
  185. spin_unlock_irqrestore(&primary_channel->sc_lock, flags);
  186. }
  187. free_channel(channel);
  188. }
  189. void vmbus_free_channels(void)
  190. {
  191. struct vmbus_channel *channel;
  192. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  193. vmbus_device_unregister(channel->device_obj);
  194. kfree(channel->device_obj);
  195. free_channel(channel);
  196. }
  197. }
  198. /*
  199. * vmbus_process_offer - Process the offer by creating a channel/device
  200. * associated with this offer
  201. */
  202. static void vmbus_process_offer(struct work_struct *work)
  203. {
  204. struct vmbus_channel *newchannel = container_of(work,
  205. struct vmbus_channel,
  206. work);
  207. struct vmbus_channel *channel;
  208. bool fnew = true;
  209. int ret;
  210. unsigned long flags;
  211. /* The next possible work is rescind handling */
  212. INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
  213. /* Make sure this is a new offer */
  214. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  215. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  216. if (!uuid_le_cmp(channel->offermsg.offer.if_type,
  217. newchannel->offermsg.offer.if_type) &&
  218. !uuid_le_cmp(channel->offermsg.offer.if_instance,
  219. newchannel->offermsg.offer.if_instance)) {
  220. fnew = false;
  221. break;
  222. }
  223. }
  224. if (fnew)
  225. list_add_tail(&newchannel->listentry,
  226. &vmbus_connection.chn_list);
  227. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  228. if (!fnew) {
  229. /*
  230. * Check to see if this is a sub-channel.
  231. */
  232. if (newchannel->offermsg.offer.sub_channel_index != 0) {
  233. /*
  234. * Process the sub-channel.
  235. */
  236. newchannel->primary_channel = channel;
  237. spin_lock_irqsave(&channel->sc_lock, flags);
  238. list_add_tail(&newchannel->sc_list, &channel->sc_list);
  239. spin_unlock_irqrestore(&channel->sc_lock, flags);
  240. newchannel->state = CHANNEL_OPEN_STATE;
  241. if (channel->sc_creation_callback != NULL)
  242. channel->sc_creation_callback(newchannel);
  243. return;
  244. }
  245. free_channel(newchannel);
  246. return;
  247. }
  248. /*
  249. * This state is used to indicate a successful open
  250. * so that when we do close the channel normally, we
  251. * can cleanup properly
  252. */
  253. newchannel->state = CHANNEL_OPEN_STATE;
  254. /*
  255. * Start the process of binding this offer to the driver
  256. * We need to set the DeviceObject field before calling
  257. * vmbus_child_dev_add()
  258. */
  259. newchannel->device_obj = vmbus_device_create(
  260. &newchannel->offermsg.offer.if_type,
  261. &newchannel->offermsg.offer.if_instance,
  262. newchannel);
  263. /*
  264. * Add the new device to the bus. This will kick off device-driver
  265. * binding which eventually invokes the device driver's AddDevice()
  266. * method.
  267. */
  268. ret = vmbus_device_register(newchannel->device_obj);
  269. if (ret != 0) {
  270. pr_err("unable to add child device object (relid %d)\n",
  271. newchannel->offermsg.child_relid);
  272. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  273. list_del(&newchannel->listentry);
  274. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  275. kfree(newchannel->device_obj);
  276. free_channel(newchannel);
  277. }
  278. }
  279. enum {
  280. IDE = 0,
  281. SCSI,
  282. NIC,
  283. MAX_PERF_CHN,
  284. };
  285. /*
  286. * This is an array of device_ids (device types) that are performance critical.
  287. * We attempt to distribute the interrupt load for these devices across
  288. * all available CPUs.
  289. */
  290. static const struct hv_vmbus_device_id hp_devs[] = {
  291. /* IDE */
  292. { HV_IDE_GUID, },
  293. /* Storage - SCSI */
  294. { HV_SCSI_GUID, },
  295. /* Network */
  296. { HV_NIC_GUID, },
  297. };
  298. /*
  299. * We use this state to statically distribute the channel interrupt load.
  300. */
  301. static u32 next_vp;
  302. /*
  303. * Starting with Win8, we can statically distribute the incoming
  304. * channel interrupt load by binding a channel to VCPU. We
  305. * implement here a simple round robin scheme for distributing
  306. * the interrupt load.
  307. * We will bind channels that are not performance critical to cpu 0 and
  308. * performance critical channels (IDE, SCSI and Network) will be uniformly
  309. * distributed across all available CPUs.
  310. */
  311. static u32 get_vp_index(uuid_le *type_guid)
  312. {
  313. u32 cur_cpu;
  314. int i;
  315. bool perf_chn = false;
  316. u32 max_cpus = num_online_cpus();
  317. for (i = IDE; i < MAX_PERF_CHN; i++) {
  318. if (!memcmp(type_guid->b, hp_devs[i].guid,
  319. sizeof(uuid_le))) {
  320. perf_chn = true;
  321. break;
  322. }
  323. }
  324. if ((vmbus_proto_version == VERSION_WS2008) ||
  325. (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
  326. /*
  327. * Prior to win8, all channel interrupts are
  328. * delivered on cpu 0.
  329. * Also if the channel is not a performance critical
  330. * channel, bind it to cpu 0.
  331. */
  332. return 0;
  333. }
  334. cur_cpu = (++next_vp % max_cpus);
  335. return hv_context.vp_index[cur_cpu];
  336. }
  337. /*
  338. * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
  339. *
  340. */
  341. static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
  342. {
  343. struct vmbus_channel_offer_channel *offer;
  344. struct vmbus_channel *newchannel;
  345. offer = (struct vmbus_channel_offer_channel *)hdr;
  346. /* Allocate the channel object and save this offer. */
  347. newchannel = alloc_channel();
  348. if (!newchannel) {
  349. pr_err("Unable to allocate channel object\n");
  350. return;
  351. }
  352. /*
  353. * By default we setup state to enable batched
  354. * reading. A specific service can choose to
  355. * disable this prior to opening the channel.
  356. */
  357. newchannel->batched_reading = true;
  358. /*
  359. * Setup state for signalling the host.
  360. */
  361. newchannel->sig_event = (struct hv_input_signal_event *)
  362. (ALIGN((unsigned long)
  363. &newchannel->sig_buf,
  364. HV_HYPERCALL_PARAM_ALIGN));
  365. newchannel->sig_event->connectionid.asu32 = 0;
  366. newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
  367. newchannel->sig_event->flag_number = 0;
  368. newchannel->sig_event->rsvdz = 0;
  369. if (vmbus_proto_version != VERSION_WS2008) {
  370. newchannel->is_dedicated_interrupt =
  371. (offer->is_dedicated_interrupt != 0);
  372. newchannel->sig_event->connectionid.u.id =
  373. offer->connection_id;
  374. }
  375. newchannel->target_vp = get_vp_index(&offer->offer.if_type);
  376. memcpy(&newchannel->offermsg, offer,
  377. sizeof(struct vmbus_channel_offer_channel));
  378. newchannel->monitor_grp = (u8)offer->monitorid / 32;
  379. newchannel->monitor_bit = (u8)offer->monitorid % 32;
  380. INIT_WORK(&newchannel->work, vmbus_process_offer);
  381. queue_work(newchannel->controlwq, &newchannel->work);
  382. }
  383. /*
  384. * vmbus_onoffer_rescind - Rescind offer handler.
  385. *
  386. * We queue a work item to process this offer synchronously
  387. */
  388. static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
  389. {
  390. struct vmbus_channel_rescind_offer *rescind;
  391. struct vmbus_channel *channel;
  392. rescind = (struct vmbus_channel_rescind_offer *)hdr;
  393. channel = relid2channel(rescind->child_relid);
  394. if (channel == NULL)
  395. /* Just return here, no channel found */
  396. return;
  397. /* work is initialized for vmbus_process_rescind_offer() from
  398. * vmbus_process_offer() where the channel got created */
  399. queue_work(channel->controlwq, &channel->work);
  400. }
  401. /*
  402. * vmbus_onoffers_delivered -
  403. * This is invoked when all offers have been delivered.
  404. *
  405. * Nothing to do here.
  406. */
  407. static void vmbus_onoffers_delivered(
  408. struct vmbus_channel_message_header *hdr)
  409. {
  410. }
  411. /*
  412. * vmbus_onopen_result - Open result handler.
  413. *
  414. * This is invoked when we received a response to our channel open request.
  415. * Find the matching request, copy the response and signal the requesting
  416. * thread.
  417. */
  418. static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
  419. {
  420. struct vmbus_channel_open_result *result;
  421. struct vmbus_channel_msginfo *msginfo;
  422. struct vmbus_channel_message_header *requestheader;
  423. struct vmbus_channel_open_channel *openmsg;
  424. unsigned long flags;
  425. result = (struct vmbus_channel_open_result *)hdr;
  426. /*
  427. * Find the open msg, copy the result and signal/unblock the wait event
  428. */
  429. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  430. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  431. msglistentry) {
  432. requestheader =
  433. (struct vmbus_channel_message_header *)msginfo->msg;
  434. if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
  435. openmsg =
  436. (struct vmbus_channel_open_channel *)msginfo->msg;
  437. if (openmsg->child_relid == result->child_relid &&
  438. openmsg->openid == result->openid) {
  439. memcpy(&msginfo->response.open_result,
  440. result,
  441. sizeof(
  442. struct vmbus_channel_open_result));
  443. complete(&msginfo->waitevent);
  444. break;
  445. }
  446. }
  447. }
  448. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  449. }
  450. /*
  451. * vmbus_ongpadl_created - GPADL created handler.
  452. *
  453. * This is invoked when we received a response to our gpadl create request.
  454. * Find the matching request, copy the response and signal the requesting
  455. * thread.
  456. */
  457. static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
  458. {
  459. struct vmbus_channel_gpadl_created *gpadlcreated;
  460. struct vmbus_channel_msginfo *msginfo;
  461. struct vmbus_channel_message_header *requestheader;
  462. struct vmbus_channel_gpadl_header *gpadlheader;
  463. unsigned long flags;
  464. gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
  465. /*
  466. * Find the establish msg, copy the result and signal/unblock the wait
  467. * event
  468. */
  469. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  470. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  471. msglistentry) {
  472. requestheader =
  473. (struct vmbus_channel_message_header *)msginfo->msg;
  474. if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
  475. gpadlheader =
  476. (struct vmbus_channel_gpadl_header *)requestheader;
  477. if ((gpadlcreated->child_relid ==
  478. gpadlheader->child_relid) &&
  479. (gpadlcreated->gpadl == gpadlheader->gpadl)) {
  480. memcpy(&msginfo->response.gpadl_created,
  481. gpadlcreated,
  482. sizeof(
  483. struct vmbus_channel_gpadl_created));
  484. complete(&msginfo->waitevent);
  485. break;
  486. }
  487. }
  488. }
  489. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  490. }
  491. /*
  492. * vmbus_ongpadl_torndown - GPADL torndown handler.
  493. *
  494. * This is invoked when we received a response to our gpadl teardown request.
  495. * Find the matching request, copy the response and signal the requesting
  496. * thread.
  497. */
  498. static void vmbus_ongpadl_torndown(
  499. struct vmbus_channel_message_header *hdr)
  500. {
  501. struct vmbus_channel_gpadl_torndown *gpadl_torndown;
  502. struct vmbus_channel_msginfo *msginfo;
  503. struct vmbus_channel_message_header *requestheader;
  504. struct vmbus_channel_gpadl_teardown *gpadl_teardown;
  505. unsigned long flags;
  506. gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
  507. /*
  508. * Find the open msg, copy the result and signal/unblock the wait event
  509. */
  510. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  511. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  512. msglistentry) {
  513. requestheader =
  514. (struct vmbus_channel_message_header *)msginfo->msg;
  515. if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
  516. gpadl_teardown =
  517. (struct vmbus_channel_gpadl_teardown *)requestheader;
  518. if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
  519. memcpy(&msginfo->response.gpadl_torndown,
  520. gpadl_torndown,
  521. sizeof(
  522. struct vmbus_channel_gpadl_torndown));
  523. complete(&msginfo->waitevent);
  524. break;
  525. }
  526. }
  527. }
  528. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  529. }
  530. /*
  531. * vmbus_onversion_response - Version response handler
  532. *
  533. * This is invoked when we received a response to our initiate contact request.
  534. * Find the matching request, copy the response and signal the requesting
  535. * thread.
  536. */
  537. static void vmbus_onversion_response(
  538. struct vmbus_channel_message_header *hdr)
  539. {
  540. struct vmbus_channel_msginfo *msginfo;
  541. struct vmbus_channel_message_header *requestheader;
  542. struct vmbus_channel_version_response *version_response;
  543. unsigned long flags;
  544. version_response = (struct vmbus_channel_version_response *)hdr;
  545. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  546. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  547. msglistentry) {
  548. requestheader =
  549. (struct vmbus_channel_message_header *)msginfo->msg;
  550. if (requestheader->msgtype ==
  551. CHANNELMSG_INITIATE_CONTACT) {
  552. memcpy(&msginfo->response.version_response,
  553. version_response,
  554. sizeof(struct vmbus_channel_version_response));
  555. complete(&msginfo->waitevent);
  556. }
  557. }
  558. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  559. }
  560. /* Channel message dispatch table */
  561. static struct vmbus_channel_message_table_entry
  562. channel_message_table[CHANNELMSG_COUNT] = {
  563. {CHANNELMSG_INVALID, NULL},
  564. {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
  565. {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
  566. {CHANNELMSG_REQUESTOFFERS, NULL},
  567. {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
  568. {CHANNELMSG_OPENCHANNEL, NULL},
  569. {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
  570. {CHANNELMSG_CLOSECHANNEL, NULL},
  571. {CHANNELMSG_GPADL_HEADER, NULL},
  572. {CHANNELMSG_GPADL_BODY, NULL},
  573. {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
  574. {CHANNELMSG_GPADL_TEARDOWN, NULL},
  575. {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
  576. {CHANNELMSG_RELID_RELEASED, NULL},
  577. {CHANNELMSG_INITIATE_CONTACT, NULL},
  578. {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
  579. {CHANNELMSG_UNLOAD, NULL},
  580. };
  581. /*
  582. * vmbus_onmessage - Handler for channel protocol messages.
  583. *
  584. * This is invoked in the vmbus worker thread context.
  585. */
  586. void vmbus_onmessage(void *context)
  587. {
  588. struct hv_message *msg = context;
  589. struct vmbus_channel_message_header *hdr;
  590. int size;
  591. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  592. size = msg->header.payload_size;
  593. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  594. pr_err("Received invalid channel message type %d size %d\n",
  595. hdr->msgtype, size);
  596. print_hex_dump_bytes("", DUMP_PREFIX_NONE,
  597. (unsigned char *)msg->u.payload, size);
  598. return;
  599. }
  600. if (channel_message_table[hdr->msgtype].message_handler)
  601. channel_message_table[hdr->msgtype].message_handler(hdr);
  602. else
  603. pr_err("Unhandled channel message type %d\n", hdr->msgtype);
  604. }
  605. /*
  606. * vmbus_request_offers - Send a request to get all our pending offers.
  607. */
  608. int vmbus_request_offers(void)
  609. {
  610. struct vmbus_channel_message_header *msg;
  611. struct vmbus_channel_msginfo *msginfo;
  612. int ret, t;
  613. msginfo = kmalloc(sizeof(*msginfo) +
  614. sizeof(struct vmbus_channel_message_header),
  615. GFP_KERNEL);
  616. if (!msginfo)
  617. return -ENOMEM;
  618. init_completion(&msginfo->waitevent);
  619. msg = (struct vmbus_channel_message_header *)msginfo->msg;
  620. msg->msgtype = CHANNELMSG_REQUESTOFFERS;
  621. ret = vmbus_post_msg(msg,
  622. sizeof(struct vmbus_channel_message_header));
  623. if (ret != 0) {
  624. pr_err("Unable to request offers - %d\n", ret);
  625. goto cleanup;
  626. }
  627. t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
  628. if (t == 0) {
  629. ret = -ETIMEDOUT;
  630. goto cleanup;
  631. }
  632. cleanup:
  633. kfree(msginfo);
  634. return ret;
  635. }
  636. /*
  637. * Retrieve the (sub) channel on which to send an outgoing request.
  638. * When a primary channel has multiple sub-channels, we choose a
  639. * channel whose VCPU binding is closest to the VCPU on which
  640. * this call is being made.
  641. */
  642. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
  643. {
  644. struct list_head *cur, *tmp;
  645. int cur_cpu = hv_context.vp_index[smp_processor_id()];
  646. struct vmbus_channel *cur_channel;
  647. struct vmbus_channel *outgoing_channel = primary;
  648. int cpu_distance, new_cpu_distance;
  649. if (list_empty(&primary->sc_list))
  650. return outgoing_channel;
  651. list_for_each_safe(cur, tmp, &primary->sc_list) {
  652. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  653. if (cur_channel->state != CHANNEL_OPENED_STATE)
  654. continue;
  655. if (cur_channel->target_vp == cur_cpu)
  656. return cur_channel;
  657. cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ?
  658. (outgoing_channel->target_vp - cur_cpu) :
  659. (cur_cpu - outgoing_channel->target_vp));
  660. new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ?
  661. (cur_channel->target_vp - cur_cpu) :
  662. (cur_cpu - cur_channel->target_vp));
  663. if (cpu_distance < new_cpu_distance)
  664. continue;
  665. outgoing_channel = cur_channel;
  666. }
  667. return outgoing_channel;
  668. }
  669. EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
  670. static void invoke_sc_cb(struct vmbus_channel *primary_channel)
  671. {
  672. struct list_head *cur, *tmp;
  673. struct vmbus_channel *cur_channel;
  674. if (primary_channel->sc_creation_callback == NULL)
  675. return;
  676. list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
  677. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  678. primary_channel->sc_creation_callback(cur_channel);
  679. }
  680. }
  681. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  682. void (*sc_cr_cb)(struct vmbus_channel *new_sc))
  683. {
  684. primary_channel->sc_creation_callback = sc_cr_cb;
  685. }
  686. EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
  687. bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
  688. {
  689. bool ret;
  690. ret = !list_empty(&primary->sc_list);
  691. if (ret) {
  692. /*
  693. * Invoke the callback on sub-channel creation.
  694. * This will present a uniform interface to the
  695. * clients.
  696. */
  697. invoke_sc_cb(primary);
  698. }
  699. return ret;
  700. }
  701. EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);