channel_mgmt.c 24 KB

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