dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / hv / channel_mgmt.c
blob37238dffd947611b771f1ba394b9bf7fb95671d4
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/completion.h>
31 #include <linux/delay.h>
32 #include <linux/hyperv.h>
34 #include "hyperv_vmbus.h"
36 static void init_vp_index(struct vmbus_channel *channel,
37 const uuid_le *type_guid);
39 /**
40 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
41 * @icmsghdrp: Pointer to msg header structure
42 * @icmsg_negotiate: Pointer to negotiate message structure
43 * @buf: Raw buffer channel data
45 * @icmsghdrp is of type &struct icmsg_hdr.
46 * @negop is of type &struct icmsg_negotiate.
47 * Set up and fill in default negotiate response message.
49 * The fw_version specifies the framework version that
50 * we can support and srv_version specifies the service
51 * version we can support.
53 * Mainly used by Hyper-V drivers.
55 bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
56 struct icmsg_negotiate *negop, u8 *buf,
57 int fw_version, int srv_version)
59 int icframe_major, icframe_minor;
60 int icmsg_major, icmsg_minor;
61 int fw_major, fw_minor;
62 int srv_major, srv_minor;
63 int i;
64 bool found_match = false;
66 icmsghdrp->icmsgsize = 0x10;
67 fw_major = (fw_version >> 16);
68 fw_minor = (fw_version & 0xFFFF);
70 srv_major = (srv_version >> 16);
71 srv_minor = (srv_version & 0xFFFF);
73 negop = (struct icmsg_negotiate *)&buf[
74 sizeof(struct vmbuspipe_hdr) +
75 sizeof(struct icmsg_hdr)];
77 icframe_major = negop->icframe_vercnt;
78 icframe_minor = 0;
80 icmsg_major = negop->icmsg_vercnt;
81 icmsg_minor = 0;
84 * Select the framework version number we will
85 * support.
88 for (i = 0; i < negop->icframe_vercnt; i++) {
89 if ((negop->icversion_data[i].major == fw_major) &&
90 (negop->icversion_data[i].minor == fw_minor)) {
91 icframe_major = negop->icversion_data[i].major;
92 icframe_minor = negop->icversion_data[i].minor;
93 found_match = true;
97 if (!found_match)
98 goto fw_error;
100 found_match = false;
102 for (i = negop->icframe_vercnt;
103 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
104 if ((negop->icversion_data[i].major == srv_major) &&
105 (negop->icversion_data[i].minor == srv_minor)) {
106 icmsg_major = negop->icversion_data[i].major;
107 icmsg_minor = negop->icversion_data[i].minor;
108 found_match = true;
113 * Respond with the framework and service
114 * version numbers we can support.
117 fw_error:
118 if (!found_match) {
119 negop->icframe_vercnt = 0;
120 negop->icmsg_vercnt = 0;
121 } else {
122 negop->icframe_vercnt = 1;
123 negop->icmsg_vercnt = 1;
126 negop->icversion_data[0].major = icframe_major;
127 negop->icversion_data[0].minor = icframe_minor;
128 negop->icversion_data[1].major = icmsg_major;
129 negop->icversion_data[1].minor = icmsg_minor;
130 return found_match;
133 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
136 * alloc_channel - Allocate and initialize a vmbus channel object
138 static struct vmbus_channel *alloc_channel(void)
140 static atomic_t chan_num = ATOMIC_INIT(0);
141 struct vmbus_channel *channel;
143 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
144 if (!channel)
145 return NULL;
147 channel->id = atomic_inc_return(&chan_num);
148 spin_lock_init(&channel->inbound_lock);
149 spin_lock_init(&channel->lock);
151 INIT_LIST_HEAD(&channel->sc_list);
152 INIT_LIST_HEAD(&channel->percpu_list);
154 return channel;
158 * free_channel - Release the resources used by the vmbus channel object
160 static void free_channel(struct vmbus_channel *channel)
162 kfree(channel);
165 static void percpu_channel_enq(void *arg)
167 struct vmbus_channel *channel = arg;
168 int cpu = smp_processor_id();
170 list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
173 static void percpu_channel_deq(void *arg)
175 struct vmbus_channel *channel = arg;
177 list_del(&channel->percpu_list);
181 void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
183 struct vmbus_channel_relid_released msg;
184 unsigned long flags;
185 struct vmbus_channel *primary_channel;
187 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
188 msg.child_relid = relid;
189 msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
190 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
192 if (channel == NULL)
193 return;
195 BUG_ON(!channel->rescind);
197 if (channel->target_cpu != get_cpu()) {
198 put_cpu();
199 smp_call_function_single(channel->target_cpu,
200 percpu_channel_deq, channel, true);
201 } else {
202 percpu_channel_deq(channel);
203 put_cpu();
206 if (channel->primary_channel == NULL) {
207 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
208 list_del(&channel->listentry);
209 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
211 primary_channel = channel;
212 } else {
213 primary_channel = channel->primary_channel;
214 spin_lock_irqsave(&primary_channel->lock, flags);
215 list_del(&channel->sc_list);
216 primary_channel->num_sc--;
217 spin_unlock_irqrestore(&primary_channel->lock, flags);
221 * We need to free the bit for init_vp_index() to work in the case
222 * of sub-channel, when we reload drivers like hv_netvsc.
224 cpumask_clear_cpu(channel->target_cpu,
225 &primary_channel->alloced_cpus_in_node);
227 free_channel(channel);
230 void vmbus_free_channels(void)
232 struct vmbus_channel *channel, *tmp;
234 list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
235 listentry) {
236 /* hv_process_channel_removal() needs this */
237 channel->rescind = true;
239 vmbus_device_unregister(channel->device_obj);
244 * vmbus_process_offer - Process the offer by creating a channel/device
245 * associated with this offer
247 static void vmbus_process_offer(struct vmbus_channel *newchannel)
249 struct vmbus_channel *channel;
250 bool fnew = true;
251 unsigned long flags;
253 /* Make sure this is a new offer */
254 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
256 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
257 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
258 newchannel->offermsg.offer.if_type) &&
259 !uuid_le_cmp(channel->offermsg.offer.if_instance,
260 newchannel->offermsg.offer.if_instance)) {
261 fnew = false;
262 break;
266 if (fnew)
267 list_add_tail(&newchannel->listentry,
268 &vmbus_connection.chn_list);
270 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
272 if (!fnew) {
274 * Check to see if this is a sub-channel.
276 if (newchannel->offermsg.offer.sub_channel_index != 0) {
278 * Process the sub-channel.
280 newchannel->primary_channel = channel;
281 spin_lock_irqsave(&channel->lock, flags);
282 list_add_tail(&newchannel->sc_list, &channel->sc_list);
283 channel->num_sc++;
284 spin_unlock_irqrestore(&channel->lock, flags);
285 } else
286 goto err_free_chan;
289 init_vp_index(newchannel, &newchannel->offermsg.offer.if_type);
291 if (newchannel->target_cpu != get_cpu()) {
292 put_cpu();
293 smp_call_function_single(newchannel->target_cpu,
294 percpu_channel_enq,
295 newchannel, true);
296 } else {
297 percpu_channel_enq(newchannel);
298 put_cpu();
302 * This state is used to indicate a successful open
303 * so that when we do close the channel normally, we
304 * can cleanup properly
306 newchannel->state = CHANNEL_OPEN_STATE;
308 if (!fnew) {
309 if (channel->sc_creation_callback != NULL)
310 channel->sc_creation_callback(newchannel);
311 return;
315 * Start the process of binding this offer to the driver
316 * We need to set the DeviceObject field before calling
317 * vmbus_child_dev_add()
319 newchannel->device_obj = vmbus_device_create(
320 &newchannel->offermsg.offer.if_type,
321 &newchannel->offermsg.offer.if_instance,
322 newchannel);
323 if (!newchannel->device_obj)
324 goto err_deq_chan;
327 * Add the new device to the bus. This will kick off device-driver
328 * binding which eventually invokes the device driver's AddDevice()
329 * method.
331 if (vmbus_device_register(newchannel->device_obj) != 0) {
332 pr_err("unable to add child device object (relid %d)\n",
333 newchannel->offermsg.child_relid);
334 kfree(newchannel->device_obj);
335 goto err_deq_chan;
337 return;
339 err_deq_chan:
340 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
341 list_del(&newchannel->listentry);
342 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
344 if (newchannel->target_cpu != get_cpu()) {
345 put_cpu();
346 smp_call_function_single(newchannel->target_cpu,
347 percpu_channel_deq, newchannel, true);
348 } else {
349 percpu_channel_deq(newchannel);
350 put_cpu();
353 err_free_chan:
354 free_channel(newchannel);
357 enum {
358 IDE = 0,
359 SCSI,
360 NIC,
361 ND_NIC,
362 MAX_PERF_CHN,
366 * This is an array of device_ids (device types) that are performance critical.
367 * We attempt to distribute the interrupt load for these devices across
368 * all available CPUs.
370 static const struct hv_vmbus_device_id hp_devs[] = {
371 /* IDE */
372 { HV_IDE_GUID, },
373 /* Storage - SCSI */
374 { HV_SCSI_GUID, },
375 /* Network */
376 { HV_NIC_GUID, },
377 /* NetworkDirect Guest RDMA */
378 { HV_ND_GUID, },
383 * We use this state to statically distribute the channel interrupt load.
385 static int next_numa_node_id;
388 * Starting with Win8, we can statically distribute the incoming
389 * channel interrupt load by binding a channel to VCPU.
390 * We do this in a hierarchical fashion:
391 * First distribute the primary channels across available NUMA nodes
392 * and then distribute the subchannels amongst the CPUs in the NUMA
393 * node assigned to the primary channel.
395 * For pre-win8 hosts or non-performance critical channels we assign the
396 * first CPU in the first NUMA node.
398 static void init_vp_index(struct vmbus_channel *channel, const uuid_le *type_guid)
400 u32 cur_cpu;
401 int i;
402 bool perf_chn = false;
403 struct vmbus_channel *primary = channel->primary_channel;
404 int next_node;
405 struct cpumask available_mask;
406 struct cpumask *alloced_mask;
408 for (i = IDE; i < MAX_PERF_CHN; i++) {
409 if (!memcmp(type_guid->b, hp_devs[i].guid,
410 sizeof(uuid_le))) {
411 perf_chn = true;
412 break;
415 if ((vmbus_proto_version == VERSION_WS2008) ||
416 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
418 * Prior to win8, all channel interrupts are
419 * delivered on cpu 0.
420 * Also if the channel is not a performance critical
421 * channel, bind it to cpu 0.
423 channel->numa_node = 0;
424 channel->target_cpu = 0;
425 channel->target_vp = hv_context.vp_index[0];
426 return;
430 * We distribute primary channels evenly across all the available
431 * NUMA nodes and within the assigned NUMA node we will assign the
432 * first available CPU to the primary channel.
433 * The sub-channels will be assigned to the CPUs available in the
434 * NUMA node evenly.
436 if (!primary) {
437 while (true) {
438 next_node = next_numa_node_id++;
439 if (next_node == nr_node_ids)
440 next_node = next_numa_node_id = 0;
441 if (cpumask_empty(cpumask_of_node(next_node)))
442 continue;
443 break;
445 channel->numa_node = next_node;
446 primary = channel;
448 alloced_mask = &hv_context.hv_numa_map[primary->numa_node];
450 if (cpumask_weight(alloced_mask) ==
451 cpumask_weight(cpumask_of_node(primary->numa_node))) {
453 * We have cycled through all the CPUs in the node;
454 * reset the alloced map.
456 cpumask_clear(alloced_mask);
459 cpumask_xor(&available_mask, alloced_mask,
460 cpumask_of_node(primary->numa_node));
462 cur_cpu = -1;
465 * Normally Hyper-V host doesn't create more subchannels than there
466 * are VCPUs on the node but it is possible when not all present VCPUs
467 * on the node are initialized by guest. Clear the alloced_cpus_in_node
468 * to start over.
470 if (cpumask_equal(&primary->alloced_cpus_in_node,
471 cpumask_of_node(primary->numa_node)))
472 cpumask_clear(&primary->alloced_cpus_in_node);
474 while (true) {
475 cur_cpu = cpumask_next(cur_cpu, &available_mask);
476 if (cur_cpu >= nr_cpu_ids) {
477 cur_cpu = -1;
478 cpumask_copy(&available_mask,
479 cpumask_of_node(primary->numa_node));
480 continue;
484 * NOTE: in the case of sub-channel, we clear the sub-channel
485 * related bit(s) in primary->alloced_cpus_in_node in
486 * hv_process_channel_removal(), so when we reload drivers
487 * like hv_netvsc in SMP guest, here we're able to re-allocate
488 * bit from primary->alloced_cpus_in_node.
490 if (!cpumask_test_cpu(cur_cpu,
491 &primary->alloced_cpus_in_node)) {
492 cpumask_set_cpu(cur_cpu,
493 &primary->alloced_cpus_in_node);
494 cpumask_set_cpu(cur_cpu, alloced_mask);
495 break;
499 channel->target_cpu = cur_cpu;
500 channel->target_vp = hv_context.vp_index[cur_cpu];
503 static void vmbus_wait_for_unload(void)
505 int cpu = smp_processor_id();
506 void *page_addr = hv_context.synic_message_page[cpu];
507 struct hv_message *msg = (struct hv_message *)page_addr +
508 VMBUS_MESSAGE_SINT;
509 struct vmbus_channel_message_header *hdr;
510 bool unloaded = false;
512 while (1) {
513 if (msg->header.message_type == HVMSG_NONE) {
514 mdelay(10);
515 continue;
518 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
519 if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
520 unloaded = true;
522 msg->header.message_type = HVMSG_NONE;
524 * header.message_type needs to be written before we do
525 * wrmsrl() below.
527 mb();
529 if (msg->header.message_flags.msg_pending)
530 wrmsrl(HV_X64_MSR_EOM, 0);
532 if (unloaded)
533 break;
538 * vmbus_unload_response - Handler for the unload response.
540 static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
543 * This is a global event; just wakeup the waiting thread.
544 * Once we successfully unload, we can cleanup the monitor state.
546 complete(&vmbus_connection.unload_event);
549 void vmbus_initiate_unload(void)
551 struct vmbus_channel_message_header hdr;
553 /* Pre-Win2012R2 hosts don't support reconnect */
554 if (vmbus_proto_version < VERSION_WIN8_1)
555 return;
557 init_completion(&vmbus_connection.unload_event);
558 memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
559 hdr.msgtype = CHANNELMSG_UNLOAD;
560 vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header));
563 * vmbus_initiate_unload() is also called on crash and the crash can be
564 * happening in an interrupt context, where scheduling is impossible.
566 if (!in_interrupt())
567 wait_for_completion(&vmbus_connection.unload_event);
568 else
569 vmbus_wait_for_unload();
573 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
576 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
578 struct vmbus_channel_offer_channel *offer;
579 struct vmbus_channel *newchannel;
581 offer = (struct vmbus_channel_offer_channel *)hdr;
583 /* Allocate the channel object and save this offer. */
584 newchannel = alloc_channel();
585 if (!newchannel) {
586 pr_err("Unable to allocate channel object\n");
587 return;
591 * By default we setup state to enable batched
592 * reading. A specific service can choose to
593 * disable this prior to opening the channel.
595 newchannel->batched_reading = true;
598 * Setup state for signalling the host.
600 newchannel->sig_event = (struct hv_input_signal_event *)
601 (ALIGN((unsigned long)
602 &newchannel->sig_buf,
603 HV_HYPERCALL_PARAM_ALIGN));
605 newchannel->sig_event->connectionid.asu32 = 0;
606 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
607 newchannel->sig_event->flag_number = 0;
608 newchannel->sig_event->rsvdz = 0;
610 if (vmbus_proto_version != VERSION_WS2008) {
611 newchannel->is_dedicated_interrupt =
612 (offer->is_dedicated_interrupt != 0);
613 newchannel->sig_event->connectionid.u.id =
614 offer->connection_id;
617 memcpy(&newchannel->offermsg, offer,
618 sizeof(struct vmbus_channel_offer_channel));
619 newchannel->monitor_grp = (u8)offer->monitorid / 32;
620 newchannel->monitor_bit = (u8)offer->monitorid % 32;
622 vmbus_process_offer(newchannel);
626 * vmbus_onoffer_rescind - Rescind offer handler.
628 * We queue a work item to process this offer synchronously
630 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
632 struct vmbus_channel_rescind_offer *rescind;
633 struct vmbus_channel *channel;
634 unsigned long flags;
635 struct device *dev;
637 rescind = (struct vmbus_channel_rescind_offer *)hdr;
638 channel = relid2channel(rescind->child_relid);
640 if (channel == NULL) {
641 hv_process_channel_removal(NULL, rescind->child_relid);
642 return;
645 spin_lock_irqsave(&channel->lock, flags);
646 channel->rescind = true;
647 spin_unlock_irqrestore(&channel->lock, flags);
649 if (channel->device_obj) {
651 * We will have to unregister this device from the
652 * driver core.
654 dev = get_device(&channel->device_obj->device);
655 if (dev) {
656 vmbus_device_unregister(channel->device_obj);
657 put_device(dev);
659 } else {
660 hv_process_channel_removal(channel,
661 channel->offermsg.child_relid);
666 * vmbus_onoffers_delivered -
667 * This is invoked when all offers have been delivered.
669 * Nothing to do here.
671 static void vmbus_onoffers_delivered(
672 struct vmbus_channel_message_header *hdr)
677 * vmbus_onopen_result - Open result handler.
679 * This is invoked when we received a response to our channel open request.
680 * Find the matching request, copy the response and signal the requesting
681 * thread.
683 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
685 struct vmbus_channel_open_result *result;
686 struct vmbus_channel_msginfo *msginfo;
687 struct vmbus_channel_message_header *requestheader;
688 struct vmbus_channel_open_channel *openmsg;
689 unsigned long flags;
691 result = (struct vmbus_channel_open_result *)hdr;
694 * Find the open msg, copy the result and signal/unblock the wait event
696 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
698 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
699 msglistentry) {
700 requestheader =
701 (struct vmbus_channel_message_header *)msginfo->msg;
703 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
704 openmsg =
705 (struct vmbus_channel_open_channel *)msginfo->msg;
706 if (openmsg->child_relid == result->child_relid &&
707 openmsg->openid == result->openid) {
708 memcpy(&msginfo->response.open_result,
709 result,
710 sizeof(
711 struct vmbus_channel_open_result));
712 complete(&msginfo->waitevent);
713 break;
717 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
721 * vmbus_ongpadl_created - GPADL created handler.
723 * This is invoked when we received a response to our gpadl create request.
724 * Find the matching request, copy the response and signal the requesting
725 * thread.
727 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
729 struct vmbus_channel_gpadl_created *gpadlcreated;
730 struct vmbus_channel_msginfo *msginfo;
731 struct vmbus_channel_message_header *requestheader;
732 struct vmbus_channel_gpadl_header *gpadlheader;
733 unsigned long flags;
735 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
738 * Find the establish msg, copy the result and signal/unblock the wait
739 * event
741 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
743 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
744 msglistentry) {
745 requestheader =
746 (struct vmbus_channel_message_header *)msginfo->msg;
748 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
749 gpadlheader =
750 (struct vmbus_channel_gpadl_header *)requestheader;
752 if ((gpadlcreated->child_relid ==
753 gpadlheader->child_relid) &&
754 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
755 memcpy(&msginfo->response.gpadl_created,
756 gpadlcreated,
757 sizeof(
758 struct vmbus_channel_gpadl_created));
759 complete(&msginfo->waitevent);
760 break;
764 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
768 * vmbus_ongpadl_torndown - GPADL torndown handler.
770 * This is invoked when we received a response to our gpadl teardown request.
771 * Find the matching request, copy the response and signal the requesting
772 * thread.
774 static void vmbus_ongpadl_torndown(
775 struct vmbus_channel_message_header *hdr)
777 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
778 struct vmbus_channel_msginfo *msginfo;
779 struct vmbus_channel_message_header *requestheader;
780 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
781 unsigned long flags;
783 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
786 * Find the open msg, copy the result and signal/unblock the wait event
788 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
790 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
791 msglistentry) {
792 requestheader =
793 (struct vmbus_channel_message_header *)msginfo->msg;
795 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
796 gpadl_teardown =
797 (struct vmbus_channel_gpadl_teardown *)requestheader;
799 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
800 memcpy(&msginfo->response.gpadl_torndown,
801 gpadl_torndown,
802 sizeof(
803 struct vmbus_channel_gpadl_torndown));
804 complete(&msginfo->waitevent);
805 break;
809 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
813 * vmbus_onversion_response - Version response handler
815 * This is invoked when we received a response to our initiate contact request.
816 * Find the matching request, copy the response and signal the requesting
817 * thread.
819 static void vmbus_onversion_response(
820 struct vmbus_channel_message_header *hdr)
822 struct vmbus_channel_msginfo *msginfo;
823 struct vmbus_channel_message_header *requestheader;
824 struct vmbus_channel_version_response *version_response;
825 unsigned long flags;
827 version_response = (struct vmbus_channel_version_response *)hdr;
828 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
830 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
831 msglistentry) {
832 requestheader =
833 (struct vmbus_channel_message_header *)msginfo->msg;
835 if (requestheader->msgtype ==
836 CHANNELMSG_INITIATE_CONTACT) {
837 memcpy(&msginfo->response.version_response,
838 version_response,
839 sizeof(struct vmbus_channel_version_response));
840 complete(&msginfo->waitevent);
843 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
846 /* Channel message dispatch table */
847 struct vmbus_channel_message_table_entry
848 channel_message_table[CHANNELMSG_COUNT] = {
849 {CHANNELMSG_INVALID, 0, NULL},
850 {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
851 {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
852 {CHANNELMSG_REQUESTOFFERS, 0, NULL},
853 {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
854 {CHANNELMSG_OPENCHANNEL, 0, NULL},
855 {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
856 {CHANNELMSG_CLOSECHANNEL, 0, NULL},
857 {CHANNELMSG_GPADL_HEADER, 0, NULL},
858 {CHANNELMSG_GPADL_BODY, 0, NULL},
859 {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
860 {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
861 {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
862 {CHANNELMSG_RELID_RELEASED, 0, NULL},
863 {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
864 {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
865 {CHANNELMSG_UNLOAD, 0, NULL},
866 {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
870 * vmbus_onmessage - Handler for channel protocol messages.
872 * This is invoked in the vmbus worker thread context.
874 void vmbus_onmessage(void *context)
876 struct hv_message *msg = context;
877 struct vmbus_channel_message_header *hdr;
878 int size;
880 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
881 size = msg->header.payload_size;
883 if (hdr->msgtype >= CHANNELMSG_COUNT) {
884 pr_err("Received invalid channel message type %d size %d\n",
885 hdr->msgtype, size);
886 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
887 (unsigned char *)msg->u.payload, size);
888 return;
891 if (channel_message_table[hdr->msgtype].message_handler)
892 channel_message_table[hdr->msgtype].message_handler(hdr);
893 else
894 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
898 * vmbus_request_offers - Send a request to get all our pending offers.
900 int vmbus_request_offers(void)
902 struct vmbus_channel_message_header *msg;
903 struct vmbus_channel_msginfo *msginfo;
904 int ret;
906 msginfo = kmalloc(sizeof(*msginfo) +
907 sizeof(struct vmbus_channel_message_header),
908 GFP_KERNEL);
909 if (!msginfo)
910 return -ENOMEM;
912 msg = (struct vmbus_channel_message_header *)msginfo->msg;
914 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
917 ret = vmbus_post_msg(msg,
918 sizeof(struct vmbus_channel_message_header));
919 if (ret != 0) {
920 pr_err("Unable to request offers - %d\n", ret);
922 goto cleanup;
925 cleanup:
926 kfree(msginfo);
928 return ret;
932 * Retrieve the (sub) channel on which to send an outgoing request.
933 * When a primary channel has multiple sub-channels, we try to
934 * distribute the load equally amongst all available channels.
936 struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
938 struct list_head *cur, *tmp;
939 int cur_cpu;
940 struct vmbus_channel *cur_channel;
941 struct vmbus_channel *outgoing_channel = primary;
942 int next_channel;
943 int i = 1;
945 if (list_empty(&primary->sc_list))
946 return outgoing_channel;
948 next_channel = primary->next_oc++;
950 if (next_channel > (primary->num_sc)) {
951 primary->next_oc = 0;
952 return outgoing_channel;
955 cur_cpu = hv_context.vp_index[get_cpu()];
956 put_cpu();
957 list_for_each_safe(cur, tmp, &primary->sc_list) {
958 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
959 if (cur_channel->state != CHANNEL_OPENED_STATE)
960 continue;
962 if (cur_channel->target_vp == cur_cpu)
963 return cur_channel;
965 if (i == next_channel)
966 return cur_channel;
968 i++;
971 return outgoing_channel;
973 EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
975 static void invoke_sc_cb(struct vmbus_channel *primary_channel)
977 struct list_head *cur, *tmp;
978 struct vmbus_channel *cur_channel;
980 if (primary_channel->sc_creation_callback == NULL)
981 return;
983 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
984 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
986 primary_channel->sc_creation_callback(cur_channel);
990 void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
991 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
993 primary_channel->sc_creation_callback = sc_cr_cb;
995 EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
997 bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
999 bool ret;
1001 ret = !list_empty(&primary->sc_list);
1003 if (ret) {
1005 * Invoke the callback on sub-channel creation.
1006 * This will present a uniform interface to the
1007 * clients.
1009 invoke_sc_cb(primary);
1012 return ret;
1014 EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);