channel_mgmt.c 24 KB

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