2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
30 #include <sys/kernel.h>
33 #include <sys/mutex.h>
35 #include "hv_vmbus_priv.h"
41 static void vmbus_channel_on_offer(hv_vmbus_channel_msg_header* hdr);
42 static void vmbus_channel_on_offer_internal(void* context);
43 static void vmbus_channel_on_open_result(hv_vmbus_channel_msg_header* hdr);
44 static void vmbus_channel_on_offer_rescind(hv_vmbus_channel_msg_header* hdr);
45 static void vmbus_channel_on_offer_rescind_internal(void* context);
46 static void vmbus_channel_on_gpadl_created(hv_vmbus_channel_msg_header* hdr);
47 static void vmbus_channel_on_gpadl_torndown(hv_vmbus_channel_msg_header* hdr);
48 static void vmbus_channel_on_offers_delivered(hv_vmbus_channel_msg_header* hdr);
49 static void vmbus_channel_on_version_response(hv_vmbus_channel_msg_header* hdr);
52 * Channel message dispatch table
54 hv_vmbus_channel_msg_table_entry
55 g_channel_message_table[HV_CHANNEL_MESSAGE_COUNT] = {
56 { HV_CHANNEL_MESSAGE_INVALID,
58 { HV_CHANNEL_MESSAGE_OFFER_CHANNEL,
59 vmbus_channel_on_offer },
60 { HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER,
61 vmbus_channel_on_offer_rescind },
62 { HV_CHANNEL_MESSAGE_REQUEST_OFFERS,
64 { HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED,
65 vmbus_channel_on_offers_delivered },
66 { HV_CHANNEL_MESSAGE_OPEN_CHANNEL,
68 { HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT,
69 vmbus_channel_on_open_result },
70 { HV_CHANNEL_MESSAGE_CLOSE_CHANNEL,
72 { HV_CHANNEL_MESSAGEL_GPADL_HEADER,
74 { HV_CHANNEL_MESSAGE_GPADL_BODY,
76 { HV_CHANNEL_MESSAGE_GPADL_CREATED,
77 vmbus_channel_on_gpadl_created },
78 { HV_CHANNEL_MESSAGE_GPADL_TEARDOWN,
80 { HV_CHANNEL_MESSAGE_GPADL_TORNDOWN,
81 vmbus_channel_on_gpadl_torndown },
82 { HV_CHANNEL_MESSAGE_REL_ID_RELEASED,
84 { HV_CHANNEL_MESSAGE_INITIATED_CONTACT,
86 { HV_CHANNEL_MESSAGE_VERSION_RESPONSE,
87 vmbus_channel_on_version_response },
88 { HV_CHANNEL_MESSAGE_UNLOAD,
92 typedef struct hv_work_item {
94 void (*callback)(void *);
98 static struct mtx vmbus_chwait_lock;
99 MTX_SYSINIT(vmbus_chwait_lk, &vmbus_chwait_lock, "vmbus primarych wait lock",
101 static uint32_t vmbus_chancnt;
102 static uint32_t vmbus_devcnt;
104 #define VMBUS_CHANCNT_DONE 0x80000000
107 * Implementation of the work abstraction.
110 work_item_callback(void *work, int pending)
112 struct hv_work_item *w = (struct hv_work_item *)work;
114 w->callback(w->context);
120 * @brief Create work item
124 void (*callback)(void *), void *context)
126 struct hv_work_item *w = malloc(sizeof(struct hv_work_item),
128 KASSERT(w != NULL, ("Error VMBUS: Failed to allocate WorkItem\n"));
132 w->callback = callback;
133 w->context = context;
135 TASK_INIT(&w->work, 0, work_item_callback, w);
137 return (taskqueue_enqueue(taskqueue_thread, &w->work));
142 * @brief Allocate and initialize a vmbus channel object
145 hv_vmbus_allocate_channel(void)
147 hv_vmbus_channel* channel;
149 channel = (hv_vmbus_channel*) malloc(
150 sizeof(hv_vmbus_channel),
154 mtx_init(&channel->sc_lock, "vmbus multi channel", NULL, MTX_DEF);
155 TAILQ_INIT(&channel->sc_list_anchor);
161 * @brief Release the resources used by the vmbus channel object
164 hv_vmbus_free_vmbus_channel(hv_vmbus_channel* channel)
166 mtx_destroy(&channel->sc_lock);
167 free(channel, M_DEVBUF);
171 * @brief Process the offer by creating a channel/device
172 * associated with this offer
175 vmbus_channel_process_offer(hv_vmbus_channel *new_channel)
177 hv_vmbus_channel* channel;
181 relid = new_channel->offer_msg.child_rel_id;
183 * Make sure this is a new offer
185 mtx_lock(&hv_vmbus_g_connection.channel_lock);
186 hv_vmbus_g_connection.channels[relid] = new_channel;
188 TAILQ_FOREACH(channel, &hv_vmbus_g_connection.channel_anchor,
190 if (memcmp(&channel->offer_msg.offer.interface_type,
191 &new_channel->offer_msg.offer.interface_type,
192 sizeof(hv_guid)) == 0 &&
193 memcmp(&channel->offer_msg.offer.interface_instance,
194 &new_channel->offer_msg.offer.interface_instance,
195 sizeof(hv_guid)) == 0)
199 if (channel == NULL) {
200 /* Install the new primary channel */
201 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_anchor,
202 new_channel, list_entry);
204 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
206 if (channel != NULL) {
208 * Check if this is a sub channel.
210 if (new_channel->offer_msg.offer.sub_channel_index != 0) {
212 * It is a sub channel offer, process it.
214 new_channel->primary_channel = channel;
215 new_channel->device = channel->device;
216 mtx_lock(&channel->sc_lock);
217 TAILQ_INSERT_TAIL(&channel->sc_list_anchor,
218 new_channel, sc_list_entry);
219 mtx_unlock(&channel->sc_lock);
222 printf("VMBUS get multi-channel offer, "
224 new_channel->offer_msg.child_rel_id,
225 new_channel->offer_msg.offer.sub_channel_index);
228 /* Insert new channel into channel_anchor. */
229 mtx_lock(&hv_vmbus_g_connection.channel_lock);
230 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_anchor,
231 new_channel, list_entry);
232 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
235 printf("VMBUS: new multi-channel offer <%p>, "
236 "its primary channel is <%p>.\n",
237 new_channel, new_channel->primary_channel);
239 new_channel->state = HV_CHANNEL_OPEN_STATE;
242 * Bump up sub-channel count and notify anyone that is
243 * interested in this sub-channel, after this sub-channel
246 mtx_lock(&channel->sc_lock);
247 channel->subchan_cnt++;
248 mtx_unlock(&channel->sc_lock);
254 printf("VMBUS: duplicated primary channel%u\n",
255 new_channel->offer_msg.child_rel_id);
256 hv_vmbus_free_vmbus_channel(new_channel);
260 new_channel->state = HV_CHANNEL_OPEN_STATE;
263 * Start the process of binding this offer to the driver
264 * (We need to set the device field before calling
265 * hv_vmbus_child_device_add())
267 new_channel->device = hv_vmbus_child_device_create(
268 new_channel->offer_msg.offer.interface_type,
269 new_channel->offer_msg.offer.interface_instance, new_channel);
272 * Add the new device to the bus. This will kick off device-driver
273 * binding which eventually invokes the device driver's AddDevice()
276 ret = hv_vmbus_child_device_register(new_channel->device);
278 mtx_lock(&hv_vmbus_g_connection.channel_lock);
279 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_anchor,
280 new_channel, list_entry);
281 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
282 hv_vmbus_free_vmbus_channel(new_channel);
285 mtx_lock(&vmbus_chwait_lock);
287 mtx_unlock(&vmbus_chwait_lock);
288 wakeup(&vmbus_devcnt);
292 vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu)
294 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
296 chan->target_cpu = cpu;
297 chan->target_vcpu = hv_vmbus_g_context.hv_vcpu_index[cpu];
300 printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n",
301 chan->offer_msg.child_rel_id,
302 chan->target_cpu, chan->target_vcpu);
307 * Array of device guids that are performance critical. We try to distribute
308 * the interrupt load for these devices across all online cpus.
310 static const hv_guid high_perf_devices[] = {
324 * We use this static number to distribute the channel interrupt load.
326 static uint32_t next_vcpu;
329 * Starting with Win8, we can statically distribute the incoming
330 * channel interrupt load by binding a channel to VCPU. We
331 * implement here a simple round robin scheme for distributing
332 * the interrupt load.
333 * We will bind channels that are not performance critical to cpu 0 and
334 * performance critical channels (IDE, SCSI and Network) will be uniformly
335 * distributed across all available CPUs.
338 vmbus_channel_select_defcpu(struct hv_vmbus_channel *channel)
340 uint32_t current_cpu;
342 boolean_t is_perf_channel = FALSE;
343 const hv_guid *guid = &channel->offer_msg.offer.interface_type;
345 for (i = PERF_CHN_NIC; i < MAX_PERF_CHN; i++) {
346 if (memcmp(guid->data, high_perf_devices[i].data,
347 sizeof(hv_guid)) == 0) {
348 is_perf_channel = TRUE;
353 if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
354 (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7) ||
355 (!is_perf_channel)) {
357 vmbus_channel_cpu_set(channel, 0);
360 /* mp_ncpus should have the number cpus currently online */
361 current_cpu = (++next_vcpu % mp_ncpus);
362 vmbus_channel_cpu_set(channel, current_cpu);
366 * @brief Handler for channel offers from Hyper-V/Azure
368 * Handler for channel offers from vmbus in parent partition. We ignore
369 * all offers except network and storage offers. For each network and storage
370 * offers, we create a channel object and queue a work item to the channel
371 * object to process the offer synchronously
374 vmbus_channel_on_offer(hv_vmbus_channel_msg_header* hdr)
376 hv_vmbus_channel_offer_channel* offer;
377 hv_vmbus_channel_offer_channel* copied;
379 offer = (hv_vmbus_channel_offer_channel*) hdr;
382 copied = malloc(sizeof(*copied), M_DEVBUF, M_NOWAIT);
383 if (copied == NULL) {
384 printf("fail to allocate memory\n");
388 memcpy(copied, hdr, sizeof(*copied));
389 hv_queue_work_item(vmbus_channel_on_offer_internal, copied);
391 mtx_lock(&vmbus_chwait_lock);
392 if ((vmbus_chancnt & VMBUS_CHANCNT_DONE) == 0)
394 mtx_unlock(&vmbus_chwait_lock);
398 vmbus_channel_on_offer_internal(void* context)
400 hv_vmbus_channel* new_channel;
402 hv_vmbus_channel_offer_channel* offer = (hv_vmbus_channel_offer_channel*)context;
403 /* Allocate the channel object and save this offer */
404 new_channel = hv_vmbus_allocate_channel();
407 * By default we setup state to enable batched
408 * reading. A specific service can choose to
409 * disable this prior to opening the channel.
411 new_channel->batched_reading = TRUE;
413 new_channel->signal_event_param =
414 (hv_vmbus_input_signal_event *)
415 (HV_ALIGN_UP((unsigned long)
416 &new_channel->signal_event_buffer,
417 HV_HYPERCALL_PARAM_ALIGN));
419 new_channel->signal_event_param->connection_id.as_uint32_t = 0;
420 new_channel->signal_event_param->connection_id.u.id =
421 HV_VMBUS_EVENT_CONNECTION_ID;
422 new_channel->signal_event_param->flag_number = 0;
423 new_channel->signal_event_param->rsvd_z = 0;
425 if (hv_vmbus_protocal_version != HV_VMBUS_VERSION_WS2008) {
426 new_channel->is_dedicated_interrupt =
427 (offer->is_dedicated_interrupt != 0);
428 new_channel->signal_event_param->connection_id.u.id =
429 offer->connection_id;
432 memcpy(&new_channel->offer_msg, offer,
433 sizeof(hv_vmbus_channel_offer_channel));
434 new_channel->monitor_group = (uint8_t) offer->monitor_id / 32;
435 new_channel->monitor_bit = (uint8_t) offer->monitor_id % 32;
437 /* Select default cpu for this channel. */
438 vmbus_channel_select_defcpu(new_channel);
440 vmbus_channel_process_offer(new_channel);
442 free(offer, M_DEVBUF);
446 * @brief Rescind offer handler.
448 * We queue a work item to process this offer
452 vmbus_channel_on_offer_rescind(hv_vmbus_channel_msg_header* hdr)
454 hv_vmbus_channel_rescind_offer* rescind;
455 hv_vmbus_channel* channel;
457 rescind = (hv_vmbus_channel_rescind_offer*) hdr;
459 channel = hv_vmbus_g_connection.channels[rescind->child_rel_id];
463 hv_queue_work_item(vmbus_channel_on_offer_rescind_internal, channel);
464 hv_vmbus_g_connection.channels[rescind->child_rel_id] = NULL;
468 vmbus_channel_on_offer_rescind_internal(void *context)
470 hv_vmbus_channel* channel;
472 channel = (hv_vmbus_channel*)context;
473 if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
474 /* Only primary channel owns the hv_device */
475 hv_vmbus_child_device_unregister(channel->device);
481 * @brief Invoked when all offers have been delivered.
484 vmbus_channel_on_offers_delivered(hv_vmbus_channel_msg_header* hdr)
487 mtx_lock(&vmbus_chwait_lock);
488 vmbus_chancnt |= VMBUS_CHANCNT_DONE;
489 mtx_unlock(&vmbus_chwait_lock);
490 wakeup(&vmbus_chancnt);
494 * @brief Open result handler.
496 * This is invoked when we received a response
497 * to our channel open request. Find the matching request, copy the
498 * response and signal the requesting thread.
501 vmbus_channel_on_open_result(hv_vmbus_channel_msg_header* hdr)
503 hv_vmbus_channel_open_result* result;
504 hv_vmbus_channel_msg_info* msg_info;
505 hv_vmbus_channel_msg_header* requestHeader;
506 hv_vmbus_channel_open_channel* openMsg;
508 result = (hv_vmbus_channel_open_result*) hdr;
511 * Find the open msg, copy the result and signal/unblock the wait event
513 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
515 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
517 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
519 if (requestHeader->message_type ==
520 HV_CHANNEL_MESSAGE_OPEN_CHANNEL) {
521 openMsg = (hv_vmbus_channel_open_channel*) msg_info->msg;
522 if (openMsg->child_rel_id == result->child_rel_id
523 && openMsg->open_id == result->open_id) {
524 memcpy(&msg_info->response.open_result, result,
525 sizeof(hv_vmbus_channel_open_result));
526 sema_post(&msg_info->wait_sema);
531 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
536 * @brief GPADL created handler.
538 * This is invoked when we received a response
539 * to our gpadl create request. Find the matching request, copy the
540 * response and signal the requesting thread.
543 vmbus_channel_on_gpadl_created(hv_vmbus_channel_msg_header* hdr)
545 hv_vmbus_channel_gpadl_created* gpadl_created;
546 hv_vmbus_channel_msg_info* msg_info;
547 hv_vmbus_channel_msg_header* request_header;
548 hv_vmbus_channel_gpadl_header* gpadl_header;
550 gpadl_created = (hv_vmbus_channel_gpadl_created*) hdr;
552 /* Find the establish msg, copy the result and signal/unblock
555 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
556 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
558 request_header = (hv_vmbus_channel_msg_header*) msg_info->msg;
559 if (request_header->message_type ==
560 HV_CHANNEL_MESSAGEL_GPADL_HEADER) {
562 (hv_vmbus_channel_gpadl_header*) request_header;
564 if ((gpadl_created->child_rel_id == gpadl_header->child_rel_id)
565 && (gpadl_created->gpadl == gpadl_header->gpadl)) {
566 memcpy(&msg_info->response.gpadl_created,
568 sizeof(hv_vmbus_channel_gpadl_created));
569 sema_post(&msg_info->wait_sema);
574 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
578 * @brief GPADL torndown handler.
580 * This is invoked when we received a respons
581 * to our gpadl teardown request. Find the matching request, copy the
582 * response and signal the requesting thread
585 vmbus_channel_on_gpadl_torndown(hv_vmbus_channel_msg_header* hdr)
587 hv_vmbus_channel_gpadl_torndown* gpadl_torndown;
588 hv_vmbus_channel_msg_info* msg_info;
589 hv_vmbus_channel_msg_header* requestHeader;
590 hv_vmbus_channel_gpadl_teardown* gpadlTeardown;
592 gpadl_torndown = (hv_vmbus_channel_gpadl_torndown*)hdr;
595 * Find the open msg, copy the result and signal/unblock the
599 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
601 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
603 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
605 if (requestHeader->message_type
606 == HV_CHANNEL_MESSAGE_GPADL_TEARDOWN) {
608 (hv_vmbus_channel_gpadl_teardown*) requestHeader;
610 if (gpadl_torndown->gpadl == gpadlTeardown->gpadl) {
611 memcpy(&msg_info->response.gpadl_torndown,
613 sizeof(hv_vmbus_channel_gpadl_torndown));
614 sema_post(&msg_info->wait_sema);
619 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
623 * @brief Version response handler.
625 * This is invoked when we received a response
626 * to our initiate contact request. Find the matching request, copy th
627 * response and signal the requesting thread.
630 vmbus_channel_on_version_response(hv_vmbus_channel_msg_header* hdr)
632 hv_vmbus_channel_msg_info* msg_info;
633 hv_vmbus_channel_msg_header* requestHeader;
634 hv_vmbus_channel_initiate_contact* initiate;
635 hv_vmbus_channel_version_response* versionResponse;
637 versionResponse = (hv_vmbus_channel_version_response*)hdr;
639 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
640 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
642 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
643 if (requestHeader->message_type
644 == HV_CHANNEL_MESSAGE_INITIATED_CONTACT) {
646 (hv_vmbus_channel_initiate_contact*) requestHeader;
647 memcpy(&msg_info->response.version_response,
649 sizeof(hv_vmbus_channel_version_response));
650 sema_post(&msg_info->wait_sema);
653 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
658 * @brief Send a request to get all our pending offers.
661 hv_vmbus_request_channel_offers(void)
664 hv_vmbus_channel_msg_header* msg;
665 hv_vmbus_channel_msg_info* msg_info;
667 msg_info = (hv_vmbus_channel_msg_info *)
668 malloc(sizeof(hv_vmbus_channel_msg_info)
669 + sizeof(hv_vmbus_channel_msg_header), M_DEVBUF, M_NOWAIT);
671 if (msg_info == NULL) {
673 printf("Error VMBUS: malloc failed for Request Offers\n");
677 msg = (hv_vmbus_channel_msg_header*) msg_info->msg;
678 msg->message_type = HV_CHANNEL_MESSAGE_REQUEST_OFFERS;
680 ret = hv_vmbus_post_message(msg, sizeof(hv_vmbus_channel_msg_header));
682 free(msg_info, M_DEVBUF);
688 * @brief Release channels that are unattached/unconnected (i.e., no drivers associated)
691 hv_vmbus_release_unattached_channels(void)
693 hv_vmbus_channel *channel;
695 mtx_lock(&hv_vmbus_g_connection.channel_lock);
697 while (!TAILQ_EMPTY(&hv_vmbus_g_connection.channel_anchor)) {
698 channel = TAILQ_FIRST(&hv_vmbus_g_connection.channel_anchor);
699 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_anchor,
700 channel, list_entry);
702 if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
703 /* Only primary channel owns the hv_device */
704 hv_vmbus_child_device_unregister(channel->device);
706 hv_vmbus_free_vmbus_channel(channel);
708 bzero(hv_vmbus_g_connection.channels,
709 sizeof(hv_vmbus_channel*) * HV_CHANNEL_MAX_COUNT);
710 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
714 * @brief Select the best outgoing channel
716 * The channel whose vcpu binding is closest to the currect vcpu will
718 * If no multi-channel, always select primary channel
720 * @param primary - primary channel
722 struct hv_vmbus_channel *
723 vmbus_select_outgoing_channel(struct hv_vmbus_channel *primary)
725 hv_vmbus_channel *new_channel = NULL;
726 hv_vmbus_channel *outgoing_channel = primary;
727 int old_cpu_distance = 0;
728 int new_cpu_distance = 0;
730 int smp_pro_id = PCPU_GET(cpuid);
732 if (TAILQ_EMPTY(&primary->sc_list_anchor)) {
733 return outgoing_channel;
736 if (smp_pro_id >= MAXCPU) {
737 return outgoing_channel;
740 cur_vcpu = hv_vmbus_g_context.hv_vcpu_index[smp_pro_id];
742 TAILQ_FOREACH(new_channel, &primary->sc_list_anchor, sc_list_entry) {
743 if (new_channel->state != HV_CHANNEL_OPENED_STATE){
747 if (new_channel->target_vcpu == cur_vcpu){
751 old_cpu_distance = ((outgoing_channel->target_vcpu > cur_vcpu) ?
752 (outgoing_channel->target_vcpu - cur_vcpu) :
753 (cur_vcpu - outgoing_channel->target_vcpu));
755 new_cpu_distance = ((new_channel->target_vcpu > cur_vcpu) ?
756 (new_channel->target_vcpu - cur_vcpu) :
757 (cur_vcpu - new_channel->target_vcpu));
759 if (old_cpu_distance < new_cpu_distance) {
763 outgoing_channel = new_channel;
766 return(outgoing_channel);
774 mtx_lock(&vmbus_chwait_lock);
775 while ((vmbus_chancnt & VMBUS_CHANCNT_DONE) == 0)
776 mtx_sleep(&vmbus_chancnt, &vmbus_chwait_lock, 0, "waitch", 0);
777 chancnt = vmbus_chancnt & ~VMBUS_CHANCNT_DONE;
779 while (vmbus_devcnt != chancnt)
780 mtx_sleep(&vmbus_devcnt, &vmbus_chwait_lock, 0, "waitdev", 0);
781 mtx_unlock(&vmbus_chwait_lock);
784 struct hv_vmbus_channel **
785 vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt)
787 struct hv_vmbus_channel **ret, *chan;
790 ret = malloc(subchan_cnt * sizeof(struct hv_vmbus_channel *), M_TEMP,
793 mtx_lock(&pri_chan->sc_lock);
795 while (pri_chan->subchan_cnt < subchan_cnt)
796 mtx_sleep(pri_chan, &pri_chan->sc_lock, 0, "subch", 0);
799 TAILQ_FOREACH(chan, &pri_chan->sc_list_anchor, sc_list_entry) {
800 /* TODO: refcnt chan */
804 if (i == subchan_cnt)
807 KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
808 pri_chan->subchan_cnt, subchan_cnt));
810 mtx_unlock(&pri_chan->sc_lock);
816 vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt __unused)
819 free(subchan, M_TEMP);