channel_mgmt.c 25 KB

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