channel_mgmt.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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/delay.h>
  31. #include <linux/hyperv.h>
  32. #include "hyperv_vmbus.h"
  33. static void init_vp_index(struct vmbus_channel *channel,
  34. const uuid_le *type_guid);
  35. /**
  36. * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
  37. * @icmsghdrp: Pointer to msg header structure
  38. * @icmsg_negotiate: Pointer to negotiate message structure
  39. * @buf: Raw buffer channel data
  40. *
  41. * @icmsghdrp is of type &struct icmsg_hdr.
  42. * @negop is of type &struct icmsg_negotiate.
  43. * Set up and fill in default negotiate response message.
  44. *
  45. * The fw_version specifies the framework version that
  46. * we can support and srv_version specifies the service
  47. * version we can support.
  48. *
  49. * Mainly used by Hyper-V drivers.
  50. */
  51. bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
  52. struct icmsg_negotiate *negop, u8 *buf,
  53. int fw_version, int srv_version)
  54. {
  55. int icframe_major, icframe_minor;
  56. int icmsg_major, icmsg_minor;
  57. int fw_major, fw_minor;
  58. int srv_major, srv_minor;
  59. int i;
  60. bool found_match = false;
  61. icmsghdrp->icmsgsize = 0x10;
  62. fw_major = (fw_version >> 16);
  63. fw_minor = (fw_version & 0xFFFF);
  64. srv_major = (srv_version >> 16);
  65. srv_minor = (srv_version & 0xFFFF);
  66. negop = (struct icmsg_negotiate *)&buf[
  67. sizeof(struct vmbuspipe_hdr) +
  68. sizeof(struct icmsg_hdr)];
  69. icframe_major = negop->icframe_vercnt;
  70. icframe_minor = 0;
  71. icmsg_major = negop->icmsg_vercnt;
  72. icmsg_minor = 0;
  73. /*
  74. * Select the framework version number we will
  75. * support.
  76. */
  77. for (i = 0; i < negop->icframe_vercnt; i++) {
  78. if ((negop->icversion_data[i].major == fw_major) &&
  79. (negop->icversion_data[i].minor == fw_minor)) {
  80. icframe_major = negop->icversion_data[i].major;
  81. icframe_minor = negop->icversion_data[i].minor;
  82. found_match = true;
  83. }
  84. }
  85. if (!found_match)
  86. goto fw_error;
  87. found_match = false;
  88. for (i = negop->icframe_vercnt;
  89. (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
  90. if ((negop->icversion_data[i].major == srv_major) &&
  91. (negop->icversion_data[i].minor == srv_minor)) {
  92. icmsg_major = negop->icversion_data[i].major;
  93. icmsg_minor = negop->icversion_data[i].minor;
  94. found_match = true;
  95. }
  96. }
  97. /*
  98. * Respond with the framework and service
  99. * version numbers we can support.
  100. */
  101. fw_error:
  102. if (!found_match) {
  103. negop->icframe_vercnt = 0;
  104. negop->icmsg_vercnt = 0;
  105. } else {
  106. negop->icframe_vercnt = 1;
  107. negop->icmsg_vercnt = 1;
  108. }
  109. negop->icversion_data[0].major = icframe_major;
  110. negop->icversion_data[0].minor = icframe_minor;
  111. negop->icversion_data[1].major = icmsg_major;
  112. negop->icversion_data[1].minor = icmsg_minor;
  113. return found_match;
  114. }
  115. EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
  116. /*
  117. * alloc_channel - Allocate and initialize a vmbus channel object
  118. */
  119. static struct vmbus_channel *alloc_channel(void)
  120. {
  121. static atomic_t chan_num = ATOMIC_INIT(0);
  122. struct vmbus_channel *channel;
  123. channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
  124. if (!channel)
  125. return NULL;
  126. channel->id = atomic_inc_return(&chan_num);
  127. spin_lock_init(&channel->inbound_lock);
  128. spin_lock_init(&channel->lock);
  129. INIT_LIST_HEAD(&channel->sc_list);
  130. INIT_LIST_HEAD(&channel->percpu_list);
  131. return channel;
  132. }
  133. /*
  134. * free_channel - Release the resources used by the vmbus channel object
  135. */
  136. static void free_channel(struct vmbus_channel *channel)
  137. {
  138. kfree(channel);
  139. }
  140. static void percpu_channel_enq(void *arg)
  141. {
  142. struct vmbus_channel *channel = arg;
  143. int cpu = smp_processor_id();
  144. list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
  145. }
  146. static void percpu_channel_deq(void *arg)
  147. {
  148. struct vmbus_channel *channel = arg;
  149. list_del(&channel->percpu_list);
  150. }
  151. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
  152. {
  153. struct vmbus_channel_relid_released msg;
  154. unsigned long flags;
  155. struct vmbus_channel *primary_channel;
  156. memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
  157. msg.child_relid = relid;
  158. msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
  159. vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
  160. if (channel == NULL)
  161. return;
  162. BUG_ON(!channel->rescind);
  163. if (channel->target_cpu != get_cpu()) {
  164. put_cpu();
  165. smp_call_function_single(channel->target_cpu,
  166. percpu_channel_deq, channel, true);
  167. } else {
  168. percpu_channel_deq(channel);
  169. put_cpu();
  170. }
  171. if (channel->primary_channel == NULL) {
  172. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  173. list_del(&channel->listentry);
  174. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  175. primary_channel = channel;
  176. } else {
  177. primary_channel = channel->primary_channel;
  178. spin_lock_irqsave(&primary_channel->lock, flags);
  179. list_del(&channel->sc_list);
  180. primary_channel->num_sc--;
  181. spin_unlock_irqrestore(&primary_channel->lock, flags);
  182. }
  183. /*
  184. * We need to free the bit for init_vp_index() to work in the case
  185. * of sub-channel, when we reload drivers like hv_netvsc.
  186. */
  187. cpumask_clear_cpu(channel->target_cpu,
  188. &primary_channel->alloced_cpus_in_node);
  189. free_channel(channel);
  190. }
  191. void vmbus_free_channels(void)
  192. {
  193. struct vmbus_channel *channel, *tmp;
  194. list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
  195. listentry) {
  196. /* hv_process_channel_removal() needs this */
  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. /*
  398. * Normally Hyper-V host doesn't create more subchannels than there
  399. * are VCPUs on the node but it is possible when not all present VCPUs
  400. * on the node are initialized by guest. Clear the alloced_cpus_in_node
  401. * to start over.
  402. */
  403. if (cpumask_equal(&primary->alloced_cpus_in_node,
  404. cpumask_of_node(primary->numa_node)))
  405. cpumask_clear(&primary->alloced_cpus_in_node);
  406. while (true) {
  407. cur_cpu = cpumask_next(cur_cpu, &available_mask);
  408. if (cur_cpu >= nr_cpu_ids) {
  409. cur_cpu = -1;
  410. cpumask_copy(&available_mask,
  411. cpumask_of_node(primary->numa_node));
  412. continue;
  413. }
  414. /*
  415. * NOTE: in the case of sub-channel, we clear the sub-channel
  416. * related bit(s) in primary->alloced_cpus_in_node in
  417. * hv_process_channel_removal(), so when we reload drivers
  418. * like hv_netvsc in SMP guest, here we're able to re-allocate
  419. * bit from primary->alloced_cpus_in_node.
  420. */
  421. if (!cpumask_test_cpu(cur_cpu,
  422. &primary->alloced_cpus_in_node)) {
  423. cpumask_set_cpu(cur_cpu,
  424. &primary->alloced_cpus_in_node);
  425. cpumask_set_cpu(cur_cpu, alloced_mask);
  426. break;
  427. }
  428. }
  429. channel->target_cpu = cur_cpu;
  430. channel->target_vp = hv_context.vp_index[cur_cpu];
  431. }
  432. static void vmbus_wait_for_unload(void)
  433. {
  434. int cpu = smp_processor_id();
  435. void *page_addr = hv_context.synic_message_page[cpu];
  436. struct hv_message *msg = (struct hv_message *)page_addr +
  437. VMBUS_MESSAGE_SINT;
  438. struct vmbus_channel_message_header *hdr;
  439. bool unloaded = false;
  440. while (1) {
  441. if (msg->header.message_type == HVMSG_NONE) {
  442. mdelay(10);
  443. continue;
  444. }
  445. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  446. if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
  447. unloaded = true;
  448. msg->header.message_type = HVMSG_NONE;
  449. /*
  450. * header.message_type needs to be written before we do
  451. * wrmsrl() below.
  452. */
  453. mb();
  454. if (msg->header.message_flags.msg_pending)
  455. wrmsrl(HV_X64_MSR_EOM, 0);
  456. if (unloaded)
  457. break;
  458. }
  459. }
  460. /*
  461. * vmbus_unload_response - Handler for the unload response.
  462. */
  463. static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
  464. {
  465. /*
  466. * This is a global event; just wakeup the waiting thread.
  467. * Once we successfully unload, we can cleanup the monitor state.
  468. */
  469. complete(&vmbus_connection.unload_event);
  470. }
  471. void vmbus_initiate_unload(void)
  472. {
  473. struct vmbus_channel_message_header hdr;
  474. /* Pre-Win2012R2 hosts don't support reconnect */
  475. if (vmbus_proto_version < VERSION_WIN8_1)
  476. return;
  477. init_completion(&vmbus_connection.unload_event);
  478. memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
  479. hdr.msgtype = CHANNELMSG_UNLOAD;
  480. vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header));
  481. /*
  482. * vmbus_initiate_unload() is also called on crash and the crash can be
  483. * happening in an interrupt context, where scheduling is impossible.
  484. */
  485. if (!in_interrupt())
  486. wait_for_completion(&vmbus_connection.unload_event);
  487. else
  488. vmbus_wait_for_unload();
  489. }
  490. /*
  491. * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
  492. *
  493. */
  494. static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
  495. {
  496. struct vmbus_channel_offer_channel *offer;
  497. struct vmbus_channel *newchannel;
  498. offer = (struct vmbus_channel_offer_channel *)hdr;
  499. /* Allocate the channel object and save this offer. */
  500. newchannel = alloc_channel();
  501. if (!newchannel) {
  502. pr_err("Unable to allocate channel object\n");
  503. return;
  504. }
  505. /*
  506. * By default we setup state to enable batched
  507. * reading. A specific service can choose to
  508. * disable this prior to opening the channel.
  509. */
  510. newchannel->batched_reading = true;
  511. /*
  512. * Setup state for signalling the host.
  513. */
  514. newchannel->sig_event = (struct hv_input_signal_event *)
  515. (ALIGN((unsigned long)
  516. &newchannel->sig_buf,
  517. HV_HYPERCALL_PARAM_ALIGN));
  518. newchannel->sig_event->connectionid.asu32 = 0;
  519. newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
  520. newchannel->sig_event->flag_number = 0;
  521. newchannel->sig_event->rsvdz = 0;
  522. if (vmbus_proto_version != VERSION_WS2008) {
  523. newchannel->is_dedicated_interrupt =
  524. (offer->is_dedicated_interrupt != 0);
  525. newchannel->sig_event->connectionid.u.id =
  526. offer->connection_id;
  527. }
  528. memcpy(&newchannel->offermsg, offer,
  529. sizeof(struct vmbus_channel_offer_channel));
  530. newchannel->monitor_grp = (u8)offer->monitorid / 32;
  531. newchannel->monitor_bit = (u8)offer->monitorid % 32;
  532. vmbus_process_offer(newchannel);
  533. }
  534. /*
  535. * vmbus_onoffer_rescind - Rescind offer handler.
  536. *
  537. * We queue a work item to process this offer synchronously
  538. */
  539. static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
  540. {
  541. struct vmbus_channel_rescind_offer *rescind;
  542. struct vmbus_channel *channel;
  543. unsigned long flags;
  544. struct device *dev;
  545. rescind = (struct vmbus_channel_rescind_offer *)hdr;
  546. channel = relid2channel(rescind->child_relid);
  547. if (channel == NULL) {
  548. hv_process_channel_removal(NULL, rescind->child_relid);
  549. return;
  550. }
  551. spin_lock_irqsave(&channel->lock, flags);
  552. channel->rescind = true;
  553. spin_unlock_irqrestore(&channel->lock, flags);
  554. if (channel->device_obj) {
  555. /*
  556. * We will have to unregister this device from the
  557. * driver core.
  558. */
  559. dev = get_device(&channel->device_obj->device);
  560. if (dev) {
  561. vmbus_device_unregister(channel->device_obj);
  562. put_device(dev);
  563. }
  564. } else {
  565. hv_process_channel_removal(channel,
  566. channel->offermsg.child_relid);
  567. }
  568. }
  569. /*
  570. * vmbus_onoffers_delivered -
  571. * This is invoked when all offers have been delivered.
  572. *
  573. * Nothing to do here.
  574. */
  575. static void vmbus_onoffers_delivered(
  576. struct vmbus_channel_message_header *hdr)
  577. {
  578. }
  579. /*
  580. * vmbus_onopen_result - Open result handler.
  581. *
  582. * This is invoked when we received a response to our channel open request.
  583. * Find the matching request, copy the response and signal the requesting
  584. * thread.
  585. */
  586. static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
  587. {
  588. struct vmbus_channel_open_result *result;
  589. struct vmbus_channel_msginfo *msginfo;
  590. struct vmbus_channel_message_header *requestheader;
  591. struct vmbus_channel_open_channel *openmsg;
  592. unsigned long flags;
  593. result = (struct vmbus_channel_open_result *)hdr;
  594. /*
  595. * Find the open msg, copy the result and signal/unblock the wait event
  596. */
  597. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  598. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  599. msglistentry) {
  600. requestheader =
  601. (struct vmbus_channel_message_header *)msginfo->msg;
  602. if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
  603. openmsg =
  604. (struct vmbus_channel_open_channel *)msginfo->msg;
  605. if (openmsg->child_relid == result->child_relid &&
  606. openmsg->openid == result->openid) {
  607. memcpy(&msginfo->response.open_result,
  608. result,
  609. sizeof(
  610. struct vmbus_channel_open_result));
  611. complete(&msginfo->waitevent);
  612. break;
  613. }
  614. }
  615. }
  616. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  617. }
  618. /*
  619. * vmbus_ongpadl_created - GPADL created handler.
  620. *
  621. * This is invoked when we received a response to our gpadl create request.
  622. * Find the matching request, copy the response and signal the requesting
  623. * thread.
  624. */
  625. static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
  626. {
  627. struct vmbus_channel_gpadl_created *gpadlcreated;
  628. struct vmbus_channel_msginfo *msginfo;
  629. struct vmbus_channel_message_header *requestheader;
  630. struct vmbus_channel_gpadl_header *gpadlheader;
  631. unsigned long flags;
  632. gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
  633. /*
  634. * Find the establish msg, copy the result and signal/unblock the wait
  635. * event
  636. */
  637. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  638. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  639. msglistentry) {
  640. requestheader =
  641. (struct vmbus_channel_message_header *)msginfo->msg;
  642. if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
  643. gpadlheader =
  644. (struct vmbus_channel_gpadl_header *)requestheader;
  645. if ((gpadlcreated->child_relid ==
  646. gpadlheader->child_relid) &&
  647. (gpadlcreated->gpadl == gpadlheader->gpadl)) {
  648. memcpy(&msginfo->response.gpadl_created,
  649. gpadlcreated,
  650. sizeof(
  651. struct vmbus_channel_gpadl_created));
  652. complete(&msginfo->waitevent);
  653. break;
  654. }
  655. }
  656. }
  657. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  658. }
  659. /*
  660. * vmbus_ongpadl_torndown - GPADL torndown handler.
  661. *
  662. * This is invoked when we received a response to our gpadl teardown request.
  663. * Find the matching request, copy the response and signal the requesting
  664. * thread.
  665. */
  666. static void vmbus_ongpadl_torndown(
  667. struct vmbus_channel_message_header *hdr)
  668. {
  669. struct vmbus_channel_gpadl_torndown *gpadl_torndown;
  670. struct vmbus_channel_msginfo *msginfo;
  671. struct vmbus_channel_message_header *requestheader;
  672. struct vmbus_channel_gpadl_teardown *gpadl_teardown;
  673. unsigned long flags;
  674. gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
  675. /*
  676. * Find the open msg, copy the result and signal/unblock the wait event
  677. */
  678. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  679. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  680. msglistentry) {
  681. requestheader =
  682. (struct vmbus_channel_message_header *)msginfo->msg;
  683. if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
  684. gpadl_teardown =
  685. (struct vmbus_channel_gpadl_teardown *)requestheader;
  686. if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
  687. memcpy(&msginfo->response.gpadl_torndown,
  688. gpadl_torndown,
  689. sizeof(
  690. struct vmbus_channel_gpadl_torndown));
  691. complete(&msginfo->waitevent);
  692. break;
  693. }
  694. }
  695. }
  696. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  697. }
  698. /*
  699. * vmbus_onversion_response - Version response handler
  700. *
  701. * This is invoked when we received a response to our initiate contact request.
  702. * Find the matching request, copy the response and signal the requesting
  703. * thread.
  704. */
  705. static void vmbus_onversion_response(
  706. struct vmbus_channel_message_header *hdr)
  707. {
  708. struct vmbus_channel_msginfo *msginfo;
  709. struct vmbus_channel_message_header *requestheader;
  710. struct vmbus_channel_version_response *version_response;
  711. unsigned long flags;
  712. version_response = (struct vmbus_channel_version_response *)hdr;
  713. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  714. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  715. msglistentry) {
  716. requestheader =
  717. (struct vmbus_channel_message_header *)msginfo->msg;
  718. if (requestheader->msgtype ==
  719. CHANNELMSG_INITIATE_CONTACT) {
  720. memcpy(&msginfo->response.version_response,
  721. version_response,
  722. sizeof(struct vmbus_channel_version_response));
  723. complete(&msginfo->waitevent);
  724. }
  725. }
  726. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  727. }
  728. /* Channel message dispatch table */
  729. struct vmbus_channel_message_table_entry
  730. channel_message_table[CHANNELMSG_COUNT] = {
  731. {CHANNELMSG_INVALID, 0, NULL},
  732. {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
  733. {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
  734. {CHANNELMSG_REQUESTOFFERS, 0, NULL},
  735. {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
  736. {CHANNELMSG_OPENCHANNEL, 0, NULL},
  737. {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
  738. {CHANNELMSG_CLOSECHANNEL, 0, NULL},
  739. {CHANNELMSG_GPADL_HEADER, 0, NULL},
  740. {CHANNELMSG_GPADL_BODY, 0, NULL},
  741. {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
  742. {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
  743. {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
  744. {CHANNELMSG_RELID_RELEASED, 0, NULL},
  745. {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
  746. {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
  747. {CHANNELMSG_UNLOAD, 0, NULL},
  748. {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
  749. };
  750. /*
  751. * vmbus_onmessage - Handler for channel protocol messages.
  752. *
  753. * This is invoked in the vmbus worker thread context.
  754. */
  755. void vmbus_onmessage(void *context)
  756. {
  757. struct hv_message *msg = context;
  758. struct vmbus_channel_message_header *hdr;
  759. int size;
  760. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  761. size = msg->header.payload_size;
  762. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  763. pr_err("Received invalid channel message type %d size %d\n",
  764. hdr->msgtype, size);
  765. print_hex_dump_bytes("", DUMP_PREFIX_NONE,
  766. (unsigned char *)msg->u.payload, size);
  767. return;
  768. }
  769. if (channel_message_table[hdr->msgtype].message_handler)
  770. channel_message_table[hdr->msgtype].message_handler(hdr);
  771. else
  772. pr_err("Unhandled channel message type %d\n", hdr->msgtype);
  773. }
  774. /*
  775. * vmbus_request_offers - Send a request to get all our pending offers.
  776. */
  777. int vmbus_request_offers(void)
  778. {
  779. struct vmbus_channel_message_header *msg;
  780. struct vmbus_channel_msginfo *msginfo;
  781. int ret;
  782. msginfo = kmalloc(sizeof(*msginfo) +
  783. sizeof(struct vmbus_channel_message_header),
  784. GFP_KERNEL);
  785. if (!msginfo)
  786. return -ENOMEM;
  787. msg = (struct vmbus_channel_message_header *)msginfo->msg;
  788. msg->msgtype = CHANNELMSG_REQUESTOFFERS;
  789. ret = vmbus_post_msg(msg,
  790. sizeof(struct vmbus_channel_message_header));
  791. if (ret != 0) {
  792. pr_err("Unable to request offers - %d\n", ret);
  793. goto cleanup;
  794. }
  795. cleanup:
  796. kfree(msginfo);
  797. return ret;
  798. }
  799. /*
  800. * Retrieve the (sub) channel on which to send an outgoing request.
  801. * When a primary channel has multiple sub-channels, we try to
  802. * distribute the load equally amongst all available channels.
  803. */
  804. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
  805. {
  806. struct list_head *cur, *tmp;
  807. int cur_cpu;
  808. struct vmbus_channel *cur_channel;
  809. struct vmbus_channel *outgoing_channel = primary;
  810. int next_channel;
  811. int i = 1;
  812. if (list_empty(&primary->sc_list))
  813. return outgoing_channel;
  814. next_channel = primary->next_oc++;
  815. if (next_channel > (primary->num_sc)) {
  816. primary->next_oc = 0;
  817. return outgoing_channel;
  818. }
  819. cur_cpu = hv_context.vp_index[get_cpu()];
  820. put_cpu();
  821. list_for_each_safe(cur, tmp, &primary->sc_list) {
  822. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  823. if (cur_channel->state != CHANNEL_OPENED_STATE)
  824. continue;
  825. if (cur_channel->target_vp == cur_cpu)
  826. return cur_channel;
  827. if (i == next_channel)
  828. return cur_channel;
  829. i++;
  830. }
  831. return outgoing_channel;
  832. }
  833. EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
  834. static void invoke_sc_cb(struct vmbus_channel *primary_channel)
  835. {
  836. struct list_head *cur, *tmp;
  837. struct vmbus_channel *cur_channel;
  838. if (primary_channel->sc_creation_callback == NULL)
  839. return;
  840. list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
  841. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  842. primary_channel->sc_creation_callback(cur_channel);
  843. }
  844. }
  845. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  846. void (*sc_cr_cb)(struct vmbus_channel *new_sc))
  847. {
  848. primary_channel->sc_creation_callback = sc_cr_cb;
  849. }
  850. EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
  851. bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
  852. {
  853. bool ret;
  854. ret = !list_empty(&primary->sc_list);
  855. if (ret) {
  856. /*
  857. * Invoke the callback on sub-channel creation.
  858. * This will present a uniform interface to the
  859. * clients.
  860. */
  861. invoke_sc_cb(primary);
  862. }
  863. return ret;
  864. }
  865. EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);