channel_mgmt.c 23 KB

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