channel_mgmt.c 22 KB

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