channel_mgmt.c 23 KB

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