channel_mgmt.c 22 KB

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