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
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.
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>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/completion.h>
31 #include <linux/hyperv.h>
33 #include "hyperv_vmbus.h"
35 static void init_vp_index(struct vmbus_channel *channel, u16 dev_type);
37 static const struct vmbus_device vmbus_devs[] = {
45 { .dev_type = HV_SCSI,
69 { .dev_type = HV_PCIE,
74 /* Synthetic Frame Buffer */
80 /* Synthetic Keyboard */
87 { .dev_type = HV_MOUSE,
101 .perf_device = false,
107 .perf_device = false,
111 { .dev_type = HV_SHUTDOWN,
113 .perf_device = false,
117 { .dev_type = HV_FCOPY,
119 .perf_device = false,
123 { .dev_type = HV_BACKUP,
125 .perf_device = false,
131 .perf_device = false,
135 { .dev_type = HV_UNKOWN,
136 .perf_device = false,
140 static u16 hv_get_dev_type(const uuid_le *guid)
144 for (i = HV_IDE; i < HV_UNKOWN; i++) {
145 if (!uuid_le_cmp(*guid, vmbus_devs[i].guid))
148 pr_info("Unknown GUID: %pUl\n", guid);
153 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
154 * @icmsghdrp: Pointer to msg header structure
155 * @icmsg_negotiate: Pointer to negotiate message structure
156 * @buf: Raw buffer channel data
158 * @icmsghdrp is of type &struct icmsg_hdr.
159 * @negop is of type &struct icmsg_negotiate.
160 * Set up and fill in default negotiate response message.
162 * The fw_version specifies the framework version that
163 * we can support and srv_version specifies the service
164 * version we can support.
166 * Mainly used by Hyper-V drivers.
168 bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
169 struct icmsg_negotiate *negop, u8 *buf,
170 int fw_version, int srv_version)
172 int icframe_major, icframe_minor;
173 int icmsg_major, icmsg_minor;
174 int fw_major, fw_minor;
175 int srv_major, srv_minor;
177 bool found_match = false;
179 icmsghdrp->icmsgsize = 0x10;
180 fw_major = (fw_version >> 16);
181 fw_minor = (fw_version & 0xFFFF);
183 srv_major = (srv_version >> 16);
184 srv_minor = (srv_version & 0xFFFF);
186 negop = (struct icmsg_negotiate *)&buf[
187 sizeof(struct vmbuspipe_hdr) +
188 sizeof(struct icmsg_hdr)];
190 icframe_major = negop->icframe_vercnt;
193 icmsg_major = negop->icmsg_vercnt;
197 * Select the framework version number we will
201 for (i = 0; i < negop->icframe_vercnt; i++) {
202 if ((negop->icversion_data[i].major == fw_major) &&
203 (negop->icversion_data[i].minor == fw_minor)) {
204 icframe_major = negop->icversion_data[i].major;
205 icframe_minor = negop->icversion_data[i].minor;
215 for (i = negop->icframe_vercnt;
216 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
217 if ((negop->icversion_data[i].major == srv_major) &&
218 (negop->icversion_data[i].minor == srv_minor)) {
219 icmsg_major = negop->icversion_data[i].major;
220 icmsg_minor = negop->icversion_data[i].minor;
226 * Respond with the framework and service
227 * version numbers we can support.
232 negop->icframe_vercnt = 0;
233 negop->icmsg_vercnt = 0;
235 negop->icframe_vercnt = 1;
236 negop->icmsg_vercnt = 1;
239 negop->icversion_data[0].major = icframe_major;
240 negop->icversion_data[0].minor = icframe_minor;
241 negop->icversion_data[1].major = icmsg_major;
242 negop->icversion_data[1].minor = icmsg_minor;
246 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
249 * alloc_channel - Allocate and initialize a vmbus channel object
251 static struct vmbus_channel *alloc_channel(void)
253 static atomic_t chan_num = ATOMIC_INIT(0);
254 struct vmbus_channel *channel;
256 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
260 channel->id = atomic_inc_return(&chan_num);
261 spin_lock_init(&channel->inbound_lock);
262 spin_lock_init(&channel->lock);
264 INIT_LIST_HEAD(&channel->sc_list);
265 INIT_LIST_HEAD(&channel->percpu_list);
271 * free_channel - Release the resources used by the vmbus channel object
273 static void free_channel(struct vmbus_channel *channel)
278 static void percpu_channel_enq(void *arg)
280 struct vmbus_channel *channel = arg;
281 int cpu = smp_processor_id();
283 list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
286 static void percpu_channel_deq(void *arg)
288 struct vmbus_channel *channel = arg;
290 list_del(&channel->percpu_list);
294 static void vmbus_release_relid(u32 relid)
296 struct vmbus_channel_relid_released msg;
298 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
299 msg.child_relid = relid;
300 msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
301 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
304 void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
307 struct vmbus_channel *primary_channel;
309 vmbus_release_relid(relid);
311 BUG_ON(!channel->rescind);
313 if (channel->target_cpu != get_cpu()) {
315 smp_call_function_single(channel->target_cpu,
316 percpu_channel_deq, channel, true);
318 percpu_channel_deq(channel);
322 if (channel->primary_channel == NULL) {
323 mutex_lock(&vmbus_connection.channel_mutex);
324 list_del(&channel->listentry);
325 mutex_unlock(&vmbus_connection.channel_mutex);
327 primary_channel = channel;
329 primary_channel = channel->primary_channel;
330 spin_lock_irqsave(&primary_channel->lock, flags);
331 list_del(&channel->sc_list);
332 primary_channel->num_sc--;
333 spin_unlock_irqrestore(&primary_channel->lock, flags);
337 * We need to free the bit for init_vp_index() to work in the case
338 * of sub-channel, when we reload drivers like hv_netvsc.
340 cpumask_clear_cpu(channel->target_cpu,
341 &primary_channel->alloced_cpus_in_node);
343 free_channel(channel);
346 void vmbus_free_channels(void)
348 struct vmbus_channel *channel, *tmp;
350 list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
352 /* hv_process_channel_removal() needs this */
353 channel->rescind = true;
355 vmbus_device_unregister(channel->device_obj);
360 * vmbus_process_offer - Process the offer by creating a channel/device
361 * associated with this offer
363 static void vmbus_process_offer(struct vmbus_channel *newchannel)
365 struct vmbus_channel *channel;
370 /* Make sure this is a new offer */
371 mutex_lock(&vmbus_connection.channel_mutex);
373 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
374 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
375 newchannel->offermsg.offer.if_type) &&
376 !uuid_le_cmp(channel->offermsg.offer.if_instance,
377 newchannel->offermsg.offer.if_instance)) {
384 list_add_tail(&newchannel->listentry,
385 &vmbus_connection.chn_list);
387 mutex_unlock(&vmbus_connection.channel_mutex);
391 * Check to see if this is a sub-channel.
393 if (newchannel->offermsg.offer.sub_channel_index != 0) {
395 * Process the sub-channel.
397 newchannel->primary_channel = channel;
398 spin_lock_irqsave(&channel->lock, flags);
399 list_add_tail(&newchannel->sc_list, &channel->sc_list);
401 spin_unlock_irqrestore(&channel->lock, flags);
406 dev_type = hv_get_dev_type(&newchannel->offermsg.offer.if_type);
408 init_vp_index(newchannel, dev_type);
410 if (newchannel->target_cpu != get_cpu()) {
412 smp_call_function_single(newchannel->target_cpu,
416 percpu_channel_enq(newchannel);
421 * This state is used to indicate a successful open
422 * so that when we do close the channel normally, we
423 * can cleanup properly
425 newchannel->state = CHANNEL_OPEN_STATE;
428 if (channel->sc_creation_callback != NULL)
429 channel->sc_creation_callback(newchannel);
434 * Start the process of binding this offer to the driver
435 * We need to set the DeviceObject field before calling
436 * vmbus_child_dev_add()
438 newchannel->device_obj = vmbus_device_create(
439 &newchannel->offermsg.offer.if_type,
440 &newchannel->offermsg.offer.if_instance,
442 if (!newchannel->device_obj)
445 newchannel->device_obj->device_id = dev_type;
447 * Add the new device to the bus. This will kick off device-driver
448 * binding which eventually invokes the device driver's AddDevice()
451 if (vmbus_device_register(newchannel->device_obj) != 0) {
452 pr_err("unable to add child device object (relid %d)\n",
453 newchannel->offermsg.child_relid);
454 kfree(newchannel->device_obj);
460 vmbus_release_relid(newchannel->offermsg.child_relid);
462 mutex_lock(&vmbus_connection.channel_mutex);
463 list_del(&newchannel->listentry);
464 mutex_unlock(&vmbus_connection.channel_mutex);
466 if (newchannel->target_cpu != get_cpu()) {
468 smp_call_function_single(newchannel->target_cpu,
469 percpu_channel_deq, newchannel, true);
471 percpu_channel_deq(newchannel);
476 free_channel(newchannel);
480 * We use this state to statically distribute the channel interrupt load.
482 static int next_numa_node_id;
485 * Starting with Win8, we can statically distribute the incoming
486 * channel interrupt load by binding a channel to VCPU.
487 * We do this in a hierarchical fashion:
488 * First distribute the primary channels across available NUMA nodes
489 * and then distribute the subchannels amongst the CPUs in the NUMA
490 * node assigned to the primary channel.
492 * For pre-win8 hosts or non-performance critical channels we assign the
493 * first CPU in the first NUMA node.
495 static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
498 bool perf_chn = vmbus_devs[dev_type].perf_device;
499 struct vmbus_channel *primary = channel->primary_channel;
501 struct cpumask available_mask;
502 struct cpumask *alloced_mask;
504 if ((vmbus_proto_version == VERSION_WS2008) ||
505 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
507 * Prior to win8, all channel interrupts are
508 * delivered on cpu 0.
509 * Also if the channel is not a performance critical
510 * channel, bind it to cpu 0.
512 channel->numa_node = 0;
513 channel->target_cpu = 0;
514 channel->target_vp = hv_context.vp_index[0];
519 * We distribute primary channels evenly across all the available
520 * NUMA nodes and within the assigned NUMA node we will assign the
521 * first available CPU to the primary channel.
522 * The sub-channels will be assigned to the CPUs available in the
527 next_node = next_numa_node_id++;
528 if (next_node == nr_node_ids)
529 next_node = next_numa_node_id = 0;
530 if (cpumask_empty(cpumask_of_node(next_node)))
534 channel->numa_node = next_node;
537 alloced_mask = &hv_context.hv_numa_map[primary->numa_node];
539 if (cpumask_weight(alloced_mask) ==
540 cpumask_weight(cpumask_of_node(primary->numa_node))) {
542 * We have cycled through all the CPUs in the node;
543 * reset the alloced map.
545 cpumask_clear(alloced_mask);
548 cpumask_xor(&available_mask, alloced_mask,
549 cpumask_of_node(primary->numa_node));
554 * Normally Hyper-V host doesn't create more subchannels than there
555 * are VCPUs on the node but it is possible when not all present VCPUs
556 * on the node are initialized by guest. Clear the alloced_cpus_in_node
559 if (cpumask_equal(&primary->alloced_cpus_in_node,
560 cpumask_of_node(primary->numa_node)))
561 cpumask_clear(&primary->alloced_cpus_in_node);
564 cur_cpu = cpumask_next(cur_cpu, &available_mask);
565 if (cur_cpu >= nr_cpu_ids) {
567 cpumask_copy(&available_mask,
568 cpumask_of_node(primary->numa_node));
573 * NOTE: in the case of sub-channel, we clear the sub-channel
574 * related bit(s) in primary->alloced_cpus_in_node in
575 * hv_process_channel_removal(), so when we reload drivers
576 * like hv_netvsc in SMP guest, here we're able to re-allocate
577 * bit from primary->alloced_cpus_in_node.
579 if (!cpumask_test_cpu(cur_cpu,
580 &primary->alloced_cpus_in_node)) {
581 cpumask_set_cpu(cur_cpu,
582 &primary->alloced_cpus_in_node);
583 cpumask_set_cpu(cur_cpu, alloced_mask);
588 channel->target_cpu = cur_cpu;
589 channel->target_vp = hv_context.vp_index[cur_cpu];
593 * vmbus_unload_response - Handler for the unload response.
595 static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
598 * This is a global event; just wakeup the waiting thread.
599 * Once we successfully unload, we can cleanup the monitor state.
601 complete(&vmbus_connection.unload_event);
604 void vmbus_initiate_unload(void)
606 struct vmbus_channel_message_header hdr;
608 /* Pre-Win2012R2 hosts don't support reconnect */
609 if (vmbus_proto_version < VERSION_WIN8_1)
612 init_completion(&vmbus_connection.unload_event);
613 memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
614 hdr.msgtype = CHANNELMSG_UNLOAD;
615 vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header));
617 wait_for_completion(&vmbus_connection.unload_event);
621 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
624 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
626 struct vmbus_channel_offer_channel *offer;
627 struct vmbus_channel *newchannel;
629 offer = (struct vmbus_channel_offer_channel *)hdr;
631 /* Allocate the channel object and save this offer. */
632 newchannel = alloc_channel();
634 pr_err("Unable to allocate channel object\n");
639 * By default we setup state to enable batched
640 * reading. A specific service can choose to
641 * disable this prior to opening the channel.
643 newchannel->batched_reading = true;
646 * Setup state for signalling the host.
648 newchannel->sig_event = (struct hv_input_signal_event *)
649 (ALIGN((unsigned long)
650 &newchannel->sig_buf,
651 HV_HYPERCALL_PARAM_ALIGN));
653 newchannel->sig_event->connectionid.asu32 = 0;
654 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
655 newchannel->sig_event->flag_number = 0;
656 newchannel->sig_event->rsvdz = 0;
658 if (vmbus_proto_version != VERSION_WS2008) {
659 newchannel->is_dedicated_interrupt =
660 (offer->is_dedicated_interrupt != 0);
661 newchannel->sig_event->connectionid.u.id =
662 offer->connection_id;
665 memcpy(&newchannel->offermsg, offer,
666 sizeof(struct vmbus_channel_offer_channel));
667 newchannel->monitor_grp = (u8)offer->monitorid / 32;
668 newchannel->monitor_bit = (u8)offer->monitorid % 32;
670 vmbus_process_offer(newchannel);
674 * vmbus_onoffer_rescind - Rescind offer handler.
676 * We queue a work item to process this offer synchronously
678 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
680 struct vmbus_channel_rescind_offer *rescind;
681 struct vmbus_channel *channel;
685 rescind = (struct vmbus_channel_rescind_offer *)hdr;
686 channel = relid2channel(rescind->child_relid);
688 if (channel == NULL) {
690 * This is very impossible, because in
691 * vmbus_process_offer(), we have already invoked
692 * vmbus_release_relid() on error.
697 spin_lock_irqsave(&channel->lock, flags);
698 channel->rescind = true;
699 spin_unlock_irqrestore(&channel->lock, flags);
701 if (channel->device_obj) {
703 * We will have to unregister this device from the
706 dev = get_device(&channel->device_obj->device);
708 vmbus_device_unregister(channel->device_obj);
712 hv_process_channel_removal(channel,
713 channel->offermsg.child_relid);
718 * vmbus_onoffers_delivered -
719 * This is invoked when all offers have been delivered.
721 * Nothing to do here.
723 static void vmbus_onoffers_delivered(
724 struct vmbus_channel_message_header *hdr)
729 * vmbus_onopen_result - Open result handler.
731 * This is invoked when we received a response to our channel open request.
732 * Find the matching request, copy the response and signal the requesting
735 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
737 struct vmbus_channel_open_result *result;
738 struct vmbus_channel_msginfo *msginfo;
739 struct vmbus_channel_message_header *requestheader;
740 struct vmbus_channel_open_channel *openmsg;
743 result = (struct vmbus_channel_open_result *)hdr;
746 * Find the open msg, copy the result and signal/unblock the wait event
748 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
750 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
753 (struct vmbus_channel_message_header *)msginfo->msg;
755 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
757 (struct vmbus_channel_open_channel *)msginfo->msg;
758 if (openmsg->child_relid == result->child_relid &&
759 openmsg->openid == result->openid) {
760 memcpy(&msginfo->response.open_result,
763 struct vmbus_channel_open_result));
764 complete(&msginfo->waitevent);
769 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
773 * vmbus_ongpadl_created - GPADL created handler.
775 * This is invoked when we received a response to our gpadl create request.
776 * Find the matching request, copy the response and signal the requesting
779 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
781 struct vmbus_channel_gpadl_created *gpadlcreated;
782 struct vmbus_channel_msginfo *msginfo;
783 struct vmbus_channel_message_header *requestheader;
784 struct vmbus_channel_gpadl_header *gpadlheader;
787 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
790 * Find the establish msg, copy the result and signal/unblock the wait
793 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
795 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
798 (struct vmbus_channel_message_header *)msginfo->msg;
800 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
802 (struct vmbus_channel_gpadl_header *)requestheader;
804 if ((gpadlcreated->child_relid ==
805 gpadlheader->child_relid) &&
806 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
807 memcpy(&msginfo->response.gpadl_created,
810 struct vmbus_channel_gpadl_created));
811 complete(&msginfo->waitevent);
816 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
820 * vmbus_ongpadl_torndown - GPADL torndown handler.
822 * This is invoked when we received a response to our gpadl teardown request.
823 * Find the matching request, copy the response and signal the requesting
826 static void vmbus_ongpadl_torndown(
827 struct vmbus_channel_message_header *hdr)
829 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
830 struct vmbus_channel_msginfo *msginfo;
831 struct vmbus_channel_message_header *requestheader;
832 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
835 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
838 * Find the open msg, copy the result and signal/unblock the wait event
840 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
842 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
845 (struct vmbus_channel_message_header *)msginfo->msg;
847 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
849 (struct vmbus_channel_gpadl_teardown *)requestheader;
851 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
852 memcpy(&msginfo->response.gpadl_torndown,
855 struct vmbus_channel_gpadl_torndown));
856 complete(&msginfo->waitevent);
861 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
865 * vmbus_onversion_response - Version response handler
867 * This is invoked when we received a response to our initiate contact request.
868 * Find the matching request, copy the response and signal the requesting
871 static void vmbus_onversion_response(
872 struct vmbus_channel_message_header *hdr)
874 struct vmbus_channel_msginfo *msginfo;
875 struct vmbus_channel_message_header *requestheader;
876 struct vmbus_channel_version_response *version_response;
879 version_response = (struct vmbus_channel_version_response *)hdr;
880 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
882 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
885 (struct vmbus_channel_message_header *)msginfo->msg;
887 if (requestheader->msgtype ==
888 CHANNELMSG_INITIATE_CONTACT) {
889 memcpy(&msginfo->response.version_response,
891 sizeof(struct vmbus_channel_version_response));
892 complete(&msginfo->waitevent);
895 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
898 /* Channel message dispatch table */
899 struct vmbus_channel_message_table_entry
900 channel_message_table[CHANNELMSG_COUNT] = {
901 {CHANNELMSG_INVALID, 0, NULL},
902 {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
903 {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
904 {CHANNELMSG_REQUESTOFFERS, 0, NULL},
905 {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
906 {CHANNELMSG_OPENCHANNEL, 0, NULL},
907 {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
908 {CHANNELMSG_CLOSECHANNEL, 0, NULL},
909 {CHANNELMSG_GPADL_HEADER, 0, NULL},
910 {CHANNELMSG_GPADL_BODY, 0, NULL},
911 {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
912 {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
913 {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
914 {CHANNELMSG_RELID_RELEASED, 0, NULL},
915 {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
916 {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
917 {CHANNELMSG_UNLOAD, 0, NULL},
918 {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
922 * vmbus_onmessage - Handler for channel protocol messages.
924 * This is invoked in the vmbus worker thread context.
926 void vmbus_onmessage(void *context)
928 struct hv_message *msg = context;
929 struct vmbus_channel_message_header *hdr;
932 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
933 size = msg->header.payload_size;
935 if (hdr->msgtype >= CHANNELMSG_COUNT) {
936 pr_err("Received invalid channel message type %d size %d\n",
938 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
939 (unsigned char *)msg->u.payload, size);
943 if (channel_message_table[hdr->msgtype].message_handler)
944 channel_message_table[hdr->msgtype].message_handler(hdr);
946 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
950 * vmbus_request_offers - Send a request to get all our pending offers.
952 int vmbus_request_offers(void)
954 struct vmbus_channel_message_header *msg;
955 struct vmbus_channel_msginfo *msginfo;
958 msginfo = kmalloc(sizeof(*msginfo) +
959 sizeof(struct vmbus_channel_message_header),
964 msg = (struct vmbus_channel_message_header *)msginfo->msg;
966 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
969 ret = vmbus_post_msg(msg,
970 sizeof(struct vmbus_channel_message_header));
972 pr_err("Unable to request offers - %d\n", ret);
984 * Retrieve the (sub) channel on which to send an outgoing request.
985 * When a primary channel has multiple sub-channels, we try to
986 * distribute the load equally amongst all available channels.
988 struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
990 struct list_head *cur, *tmp;
992 struct vmbus_channel *cur_channel;
993 struct vmbus_channel *outgoing_channel = primary;
997 if (list_empty(&primary->sc_list))
998 return outgoing_channel;
1000 next_channel = primary->next_oc++;
1002 if (next_channel > (primary->num_sc)) {
1003 primary->next_oc = 0;
1004 return outgoing_channel;
1007 cur_cpu = hv_context.vp_index[get_cpu()];
1009 list_for_each_safe(cur, tmp, &primary->sc_list) {
1010 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1011 if (cur_channel->state != CHANNEL_OPENED_STATE)
1014 if (cur_channel->target_vp == cur_cpu)
1017 if (i == next_channel)
1023 return outgoing_channel;
1025 EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
1027 static void invoke_sc_cb(struct vmbus_channel *primary_channel)
1029 struct list_head *cur, *tmp;
1030 struct vmbus_channel *cur_channel;
1032 if (primary_channel->sc_creation_callback == NULL)
1035 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
1036 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
1038 primary_channel->sc_creation_callback(cur_channel);
1042 void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
1043 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
1045 primary_channel->sc_creation_callback = sc_cr_cb;
1047 EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
1049 bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
1053 ret = !list_empty(&primary->sc_list);
1057 * Invoke the callback on sub-channel creation.
1058 * This will present a uniform interface to the
1061 invoke_sc_cb(primary);
1066 EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);