Linux 3.12.39
[linux/fpc-iii.git] / drivers / hv / channel_mgmt.c
blob505fe29c75b039eea635f4851f8494f8a06fcc9b
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/hyperv.h>
33 #include "hyperv_vmbus.h"
35 struct vmbus_channel_message_table_entry {
36 enum vmbus_channel_message_type message_type;
37 void (*message_handler)(struct vmbus_channel_message_header *msg);
41 /**
42 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
43 * @icmsghdrp: Pointer to msg header structure
44 * @icmsg_negotiate: Pointer to negotiate message structure
45 * @buf: Raw buffer channel data
47 * @icmsghdrp is of type &struct icmsg_hdr.
48 * @negop is of type &struct icmsg_negotiate.
49 * Set up and fill in default negotiate response message.
51 * The fw_version specifies the framework version that
52 * we can support and srv_version specifies the service
53 * version we can support.
55 * Mainly used by Hyper-V drivers.
57 bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
58 struct icmsg_negotiate *negop, u8 *buf,
59 int fw_version, int srv_version)
61 int icframe_major, icframe_minor;
62 int icmsg_major, icmsg_minor;
63 int fw_major, fw_minor;
64 int srv_major, srv_minor;
65 int i;
66 bool found_match = false;
68 icmsghdrp->icmsgsize = 0x10;
69 fw_major = (fw_version >> 16);
70 fw_minor = (fw_version & 0xFFFF);
72 srv_major = (srv_version >> 16);
73 srv_minor = (srv_version & 0xFFFF);
75 negop = (struct icmsg_negotiate *)&buf[
76 sizeof(struct vmbuspipe_hdr) +
77 sizeof(struct icmsg_hdr)];
79 icframe_major = negop->icframe_vercnt;
80 icframe_minor = 0;
82 icmsg_major = negop->icmsg_vercnt;
83 icmsg_minor = 0;
86 * Select the framework version number we will
87 * support.
90 for (i = 0; i < negop->icframe_vercnt; i++) {
91 if ((negop->icversion_data[i].major == fw_major) &&
92 (negop->icversion_data[i].minor == fw_minor)) {
93 icframe_major = negop->icversion_data[i].major;
94 icframe_minor = negop->icversion_data[i].minor;
95 found_match = true;
99 if (!found_match)
100 goto fw_error;
102 found_match = false;
104 for (i = negop->icframe_vercnt;
105 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
106 if ((negop->icversion_data[i].major == srv_major) &&
107 (negop->icversion_data[i].minor == srv_minor)) {
108 icmsg_major = negop->icversion_data[i].major;
109 icmsg_minor = negop->icversion_data[i].minor;
110 found_match = true;
115 * Respond with the framework and service
116 * version numbers we can support.
119 fw_error:
120 if (!found_match) {
121 negop->icframe_vercnt = 0;
122 negop->icmsg_vercnt = 0;
123 } else {
124 negop->icframe_vercnt = 1;
125 negop->icmsg_vercnt = 1;
128 negop->icversion_data[0].major = icframe_major;
129 negop->icversion_data[0].minor = icframe_minor;
130 negop->icversion_data[1].major = icmsg_major;
131 negop->icversion_data[1].minor = icmsg_minor;
132 return found_match;
135 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
138 * alloc_channel - Allocate and initialize a vmbus channel object
140 static struct vmbus_channel *alloc_channel(void)
142 struct vmbus_channel *channel;
144 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
145 if (!channel)
146 return NULL;
148 spin_lock_init(&channel->inbound_lock);
149 spin_lock_init(&channel->sc_lock);
151 INIT_LIST_HEAD(&channel->sc_list);
153 channel->controlwq = create_workqueue("hv_vmbus_ctl");
154 if (!channel->controlwq) {
155 kfree(channel);
156 return NULL;
159 return channel;
163 * release_hannel - Release the vmbus channel object itself
165 static void release_channel(struct work_struct *work)
167 struct vmbus_channel *channel = container_of(work,
168 struct vmbus_channel,
169 work);
171 destroy_workqueue(channel->controlwq);
173 kfree(channel);
177 * free_channel - Release the resources used by the vmbus channel object
179 static void free_channel(struct vmbus_channel *channel)
183 * We have to release the channel's workqueue/thread in the vmbus's
184 * workqueue/thread context
185 * ie we can't destroy ourselves.
187 INIT_WORK(&channel->work, release_channel);
188 queue_work(vmbus_connection.work_queue, &channel->work);
194 * vmbus_process_rescind_offer -
195 * Rescind the offer by initiating a device removal
197 static void vmbus_process_rescind_offer(struct work_struct *work)
199 struct vmbus_channel *channel = container_of(work,
200 struct vmbus_channel,
201 work);
202 unsigned long flags;
203 struct vmbus_channel *primary_channel;
204 struct vmbus_channel_relid_released msg;
205 struct device *dev;
207 if (channel->device_obj) {
208 dev = get_device(&channel->device_obj->device);
209 if (dev) {
210 vmbus_device_unregister(channel->device_obj);
211 put_device(dev);
215 memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
216 msg.child_relid = channel->offermsg.child_relid;
217 msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
218 vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
220 if (channel->primary_channel == NULL) {
221 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
222 list_del(&channel->listentry);
223 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
224 } else {
225 primary_channel = channel->primary_channel;
226 spin_lock_irqsave(&primary_channel->sc_lock, flags);
227 list_del(&channel->sc_list);
228 spin_unlock_irqrestore(&primary_channel->sc_lock, flags);
230 free_channel(channel);
233 void vmbus_free_channels(void)
235 struct vmbus_channel *channel;
237 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
238 vmbus_device_unregister(channel->device_obj);
239 kfree(channel->device_obj);
240 free_channel(channel);
245 * vmbus_process_offer - Process the offer by creating a channel/device
246 * associated with this offer
248 static void vmbus_process_offer(struct work_struct *work)
250 struct vmbus_channel *newchannel = container_of(work,
251 struct vmbus_channel,
252 work);
253 struct vmbus_channel *channel;
254 bool fnew = true;
255 int ret;
256 unsigned long flags;
258 /* The next possible work is rescind handling */
259 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
261 /* Make sure this is a new offer */
262 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
264 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
265 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
266 newchannel->offermsg.offer.if_type) &&
267 !uuid_le_cmp(channel->offermsg.offer.if_instance,
268 newchannel->offermsg.offer.if_instance)) {
269 fnew = false;
270 break;
274 if (fnew)
275 list_add_tail(&newchannel->listentry,
276 &vmbus_connection.chn_list);
278 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
280 if (!fnew) {
282 * Check to see if this is a sub-channel.
284 if (newchannel->offermsg.offer.sub_channel_index != 0) {
286 * Process the sub-channel.
288 newchannel->primary_channel = channel;
289 spin_lock_irqsave(&channel->sc_lock, flags);
290 list_add_tail(&newchannel->sc_list, &channel->sc_list);
291 spin_unlock_irqrestore(&channel->sc_lock, flags);
292 newchannel->state = CHANNEL_OPEN_STATE;
293 if (channel->sc_creation_callback != NULL)
294 channel->sc_creation_callback(newchannel);
296 return;
299 free_channel(newchannel);
300 return;
304 * This state is used to indicate a successful open
305 * so that when we do close the channel normally, we
306 * can cleanup properly
308 newchannel->state = CHANNEL_OPEN_STATE;
311 * Start the process of binding this offer to the driver
312 * We need to set the DeviceObject field before calling
313 * vmbus_child_dev_add()
315 newchannel->device_obj = vmbus_device_create(
316 &newchannel->offermsg.offer.if_type,
317 &newchannel->offermsg.offer.if_instance,
318 newchannel);
321 * Add the new device to the bus. This will kick off device-driver
322 * binding which eventually invokes the device driver's AddDevice()
323 * method.
325 ret = vmbus_device_register(newchannel->device_obj);
326 if (ret != 0) {
327 pr_err("unable to add child device object (relid %d)\n",
328 newchannel->offermsg.child_relid);
330 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
331 list_del(&newchannel->listentry);
332 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
333 kfree(newchannel->device_obj);
335 free_channel(newchannel);
339 enum {
340 IDE = 0,
341 SCSI,
342 NIC,
343 MAX_PERF_CHN,
347 * This is an array of device_ids (device types) that are performance critical.
348 * We attempt to distribute the interrupt load for these devices across
349 * all available CPUs.
351 static const struct hv_vmbus_device_id hp_devs[] = {
352 /* IDE */
353 { HV_IDE_GUID, },
354 /* Storage - SCSI */
355 { HV_SCSI_GUID, },
356 /* Network */
357 { HV_NIC_GUID, },
362 * We use this state to statically distribute the channel interrupt load.
364 static u32 next_vp;
367 * Starting with Win8, we can statically distribute the incoming
368 * channel interrupt load by binding a channel to VCPU. We
369 * implement here a simple round robin scheme for distributing
370 * the interrupt load.
371 * We will bind channels that are not performance critical to cpu 0 and
372 * performance critical channels (IDE, SCSI and Network) will be uniformly
373 * distributed across all available CPUs.
375 static u32 get_vp_index(uuid_le *type_guid)
377 u32 cur_cpu;
378 int i;
379 bool perf_chn = false;
380 u32 max_cpus = num_online_cpus();
382 for (i = IDE; i < MAX_PERF_CHN; i++) {
383 if (!memcmp(type_guid->b, hp_devs[i].guid,
384 sizeof(uuid_le))) {
385 perf_chn = true;
386 break;
389 if ((vmbus_proto_version == VERSION_WS2008) ||
390 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
392 * Prior to win8, all channel interrupts are
393 * delivered on cpu 0.
394 * Also if the channel is not a performance critical
395 * channel, bind it to cpu 0.
397 return 0;
399 cur_cpu = (++next_vp % max_cpus);
400 return hv_context.vp_index[cur_cpu];
404 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
407 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
409 struct vmbus_channel_offer_channel *offer;
410 struct vmbus_channel *newchannel;
412 offer = (struct vmbus_channel_offer_channel *)hdr;
414 /* Allocate the channel object and save this offer. */
415 newchannel = alloc_channel();
416 if (!newchannel) {
417 pr_err("Unable to allocate channel object\n");
418 return;
422 * By default we setup state to enable batched
423 * reading. A specific service can choose to
424 * disable this prior to opening the channel.
426 newchannel->batched_reading = true;
429 * Setup state for signalling the host.
431 newchannel->sig_event = (struct hv_input_signal_event *)
432 (ALIGN((unsigned long)
433 &newchannel->sig_buf,
434 HV_HYPERCALL_PARAM_ALIGN));
436 newchannel->sig_event->connectionid.asu32 = 0;
437 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
438 newchannel->sig_event->flag_number = 0;
439 newchannel->sig_event->rsvdz = 0;
441 if (vmbus_proto_version != VERSION_WS2008) {
442 newchannel->is_dedicated_interrupt =
443 (offer->is_dedicated_interrupt != 0);
444 newchannel->sig_event->connectionid.u.id =
445 offer->connection_id;
448 newchannel->target_vp = get_vp_index(&offer->offer.if_type);
450 memcpy(&newchannel->offermsg, offer,
451 sizeof(struct vmbus_channel_offer_channel));
452 newchannel->monitor_grp = (u8)offer->monitorid / 32;
453 newchannel->monitor_bit = (u8)offer->monitorid % 32;
455 INIT_WORK(&newchannel->work, vmbus_process_offer);
456 queue_work(newchannel->controlwq, &newchannel->work);
460 * vmbus_onoffer_rescind - Rescind offer handler.
462 * We queue a work item to process this offer synchronously
464 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
466 struct vmbus_channel_rescind_offer *rescind;
467 struct vmbus_channel *channel;
469 rescind = (struct vmbus_channel_rescind_offer *)hdr;
470 channel = relid2channel(rescind->child_relid);
472 if (channel == NULL)
473 /* Just return here, no channel found */
474 return;
476 /* work is initialized for vmbus_process_rescind_offer() from
477 * vmbus_process_offer() where the channel got created */
478 queue_work(channel->controlwq, &channel->work);
482 * vmbus_onoffers_delivered -
483 * This is invoked when all offers have been delivered.
485 * Nothing to do here.
487 static void vmbus_onoffers_delivered(
488 struct vmbus_channel_message_header *hdr)
493 * vmbus_onopen_result - Open result handler.
495 * This is invoked when we received a response to our channel open request.
496 * Find the matching request, copy the response and signal the requesting
497 * thread.
499 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
501 struct vmbus_channel_open_result *result;
502 struct vmbus_channel_msginfo *msginfo;
503 struct vmbus_channel_message_header *requestheader;
504 struct vmbus_channel_open_channel *openmsg;
505 unsigned long flags;
507 result = (struct vmbus_channel_open_result *)hdr;
510 * Find the open msg, copy the result and signal/unblock the wait event
512 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
514 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
515 msglistentry) {
516 requestheader =
517 (struct vmbus_channel_message_header *)msginfo->msg;
519 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
520 openmsg =
521 (struct vmbus_channel_open_channel *)msginfo->msg;
522 if (openmsg->child_relid == result->child_relid &&
523 openmsg->openid == result->openid) {
524 memcpy(&msginfo->response.open_result,
525 result,
526 sizeof(
527 struct vmbus_channel_open_result));
528 complete(&msginfo->waitevent);
529 break;
533 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
537 * vmbus_ongpadl_created - GPADL created handler.
539 * This is invoked when we received a response to our gpadl create request.
540 * Find the matching request, copy the response and signal the requesting
541 * thread.
543 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
545 struct vmbus_channel_gpadl_created *gpadlcreated;
546 struct vmbus_channel_msginfo *msginfo;
547 struct vmbus_channel_message_header *requestheader;
548 struct vmbus_channel_gpadl_header *gpadlheader;
549 unsigned long flags;
551 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
554 * Find the establish msg, copy the result and signal/unblock the wait
555 * event
557 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
559 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
560 msglistentry) {
561 requestheader =
562 (struct vmbus_channel_message_header *)msginfo->msg;
564 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
565 gpadlheader =
566 (struct vmbus_channel_gpadl_header *)requestheader;
568 if ((gpadlcreated->child_relid ==
569 gpadlheader->child_relid) &&
570 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
571 memcpy(&msginfo->response.gpadl_created,
572 gpadlcreated,
573 sizeof(
574 struct vmbus_channel_gpadl_created));
575 complete(&msginfo->waitevent);
576 break;
580 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
584 * vmbus_ongpadl_torndown - GPADL torndown handler.
586 * This is invoked when we received a response to our gpadl teardown request.
587 * Find the matching request, copy the response and signal the requesting
588 * thread.
590 static void vmbus_ongpadl_torndown(
591 struct vmbus_channel_message_header *hdr)
593 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
594 struct vmbus_channel_msginfo *msginfo;
595 struct vmbus_channel_message_header *requestheader;
596 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
597 unsigned long flags;
599 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
602 * Find the open msg, copy the result and signal/unblock the wait event
604 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
606 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
607 msglistentry) {
608 requestheader =
609 (struct vmbus_channel_message_header *)msginfo->msg;
611 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
612 gpadl_teardown =
613 (struct vmbus_channel_gpadl_teardown *)requestheader;
615 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
616 memcpy(&msginfo->response.gpadl_torndown,
617 gpadl_torndown,
618 sizeof(
619 struct vmbus_channel_gpadl_torndown));
620 complete(&msginfo->waitevent);
621 break;
625 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
629 * vmbus_onversion_response - Version response handler
631 * This is invoked when we received a response to our initiate contact request.
632 * Find the matching request, copy the response and signal the requesting
633 * thread.
635 static void vmbus_onversion_response(
636 struct vmbus_channel_message_header *hdr)
638 struct vmbus_channel_msginfo *msginfo;
639 struct vmbus_channel_message_header *requestheader;
640 struct vmbus_channel_version_response *version_response;
641 unsigned long flags;
643 version_response = (struct vmbus_channel_version_response *)hdr;
644 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
646 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
647 msglistentry) {
648 requestheader =
649 (struct vmbus_channel_message_header *)msginfo->msg;
651 if (requestheader->msgtype ==
652 CHANNELMSG_INITIATE_CONTACT) {
653 memcpy(&msginfo->response.version_response,
654 version_response,
655 sizeof(struct vmbus_channel_version_response));
656 complete(&msginfo->waitevent);
659 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
662 /* Channel message dispatch table */
663 static struct vmbus_channel_message_table_entry
664 channel_message_table[CHANNELMSG_COUNT] = {
665 {CHANNELMSG_INVALID, NULL},
666 {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
667 {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
668 {CHANNELMSG_REQUESTOFFERS, NULL},
669 {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
670 {CHANNELMSG_OPENCHANNEL, NULL},
671 {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
672 {CHANNELMSG_CLOSECHANNEL, NULL},
673 {CHANNELMSG_GPADL_HEADER, NULL},
674 {CHANNELMSG_GPADL_BODY, NULL},
675 {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
676 {CHANNELMSG_GPADL_TEARDOWN, NULL},
677 {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
678 {CHANNELMSG_RELID_RELEASED, NULL},
679 {CHANNELMSG_INITIATE_CONTACT, NULL},
680 {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
681 {CHANNELMSG_UNLOAD, NULL},
685 * vmbus_onmessage - Handler for channel protocol messages.
687 * This is invoked in the vmbus worker thread context.
689 void vmbus_onmessage(void *context)
691 struct hv_message *msg = context;
692 struct vmbus_channel_message_header *hdr;
693 int size;
695 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
696 size = msg->header.payload_size;
698 if (hdr->msgtype >= CHANNELMSG_COUNT) {
699 pr_err("Received invalid channel message type %d size %d\n",
700 hdr->msgtype, size);
701 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
702 (unsigned char *)msg->u.payload, size);
703 return;
706 if (channel_message_table[hdr->msgtype].message_handler)
707 channel_message_table[hdr->msgtype].message_handler(hdr);
708 else
709 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
713 * vmbus_request_offers - Send a request to get all our pending offers.
715 int vmbus_request_offers(void)
717 struct vmbus_channel_message_header *msg;
718 struct vmbus_channel_msginfo *msginfo;
719 int ret, t;
721 msginfo = kmalloc(sizeof(*msginfo) +
722 sizeof(struct vmbus_channel_message_header),
723 GFP_KERNEL);
724 if (!msginfo)
725 return -ENOMEM;
727 init_completion(&msginfo->waitevent);
729 msg = (struct vmbus_channel_message_header *)msginfo->msg;
731 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
734 ret = vmbus_post_msg(msg,
735 sizeof(struct vmbus_channel_message_header));
736 if (ret != 0) {
737 pr_err("Unable to request offers - %d\n", ret);
739 goto cleanup;
742 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
743 if (t == 0) {
744 ret = -ETIMEDOUT;
745 goto cleanup;
750 cleanup:
751 kfree(msginfo);
753 return ret;
757 * Retrieve the (sub) channel on which to send an outgoing request.
758 * When a primary channel has multiple sub-channels, we choose a
759 * channel whose VCPU binding is closest to the VCPU on which
760 * this call is being made.
762 struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
764 struct list_head *cur, *tmp;
765 int cur_cpu = hv_context.vp_index[smp_processor_id()];
766 struct vmbus_channel *cur_channel;
767 struct vmbus_channel *outgoing_channel = primary;
768 int cpu_distance, new_cpu_distance;
770 if (list_empty(&primary->sc_list))
771 return outgoing_channel;
773 list_for_each_safe(cur, tmp, &primary->sc_list) {
774 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
775 if (cur_channel->state != CHANNEL_OPENED_STATE)
776 continue;
778 if (cur_channel->target_vp == cur_cpu)
779 return cur_channel;
781 cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ?
782 (outgoing_channel->target_vp - cur_cpu) :
783 (cur_cpu - outgoing_channel->target_vp));
785 new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ?
786 (cur_channel->target_vp - cur_cpu) :
787 (cur_cpu - cur_channel->target_vp));
789 if (cpu_distance < new_cpu_distance)
790 continue;
792 outgoing_channel = cur_channel;
795 return outgoing_channel;
797 EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
799 static void invoke_sc_cb(struct vmbus_channel *primary_channel)
801 struct list_head *cur, *tmp;
802 struct vmbus_channel *cur_channel;
804 if (primary_channel->sc_creation_callback == NULL)
805 return;
807 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
808 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
810 primary_channel->sc_creation_callback(cur_channel);
814 void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
815 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
817 primary_channel->sc_creation_callback = sc_cr_cb;
819 EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
821 bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
823 bool ret;
825 ret = !list_empty(&primary->sc_list);
827 if (ret) {
829 * Invoke the callback on sub-channel creation.
830 * This will present a uniform interface to the
831 * clients.
833 invoke_sc_cb(primary);
836 return ret;
838 EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);