2 * Copyright (c) 2009-2012 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/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.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 *);
99 * Implementation of the work abstraction.
102 work_item_callback(void *work, int pending)
104 struct hv_work_item *w = (struct hv_work_item *)work;
106 w->callback(w->context);
112 * @brief Create work item
116 void (*callback)(void *), void *context)
118 struct hv_work_item *w = malloc(sizeof(struct hv_work_item),
120 KASSERT(w != NULL, ("Error VMBUS: Failed to allocate WorkItem\n"));
124 w->callback = callback;
125 w->context = context;
127 TASK_INIT(&w->work, 0, work_item_callback, w);
129 return (taskqueue_enqueue(taskqueue_thread, &w->work));
134 * @brief Allocate and initialize a vmbus channel object
137 hv_vmbus_allocate_channel(void)
139 hv_vmbus_channel* channel;
141 channel = (hv_vmbus_channel*) malloc(
142 sizeof(hv_vmbus_channel),
146 mtx_init(&channel->sc_lock, "vmbus multi channel", NULL, MTX_DEF);
147 TAILQ_INIT(&channel->sc_list_anchor);
153 * @brief Release the resources used by the vmbus channel object
156 hv_vmbus_free_vmbus_channel(hv_vmbus_channel* channel)
158 mtx_destroy(&channel->sc_lock);
159 free(channel, M_DEVBUF);
163 * @brief Process the offer by creating a channel/device
164 * associated with this offer
167 vmbus_channel_process_offer(hv_vmbus_channel *new_channel)
170 hv_vmbus_channel* channel;
176 relid = new_channel->offer_msg.child_rel_id;
178 * Make sure this is a new offer
180 mtx_lock(&hv_vmbus_g_connection.channel_lock);
181 hv_vmbus_g_connection.channels[relid] = new_channel;
183 TAILQ_FOREACH(channel, &hv_vmbus_g_connection.channel_anchor,
186 if (memcmp(&channel->offer_msg.offer.interface_type,
187 &new_channel->offer_msg.offer.interface_type,
188 sizeof(hv_guid)) == 0 &&
189 memcmp(&channel->offer_msg.offer.interface_instance,
190 &new_channel->offer_msg.offer.interface_instance,
191 sizeof(hv_guid)) == 0) {
200 &hv_vmbus_g_connection.channel_anchor,
204 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
206 /*XXX add new channel to percpu_list */
210 * Check if this is a sub channel.
212 if (new_channel->offer_msg.offer.sub_channel_index != 0) {
214 * It is a sub channel offer, process it.
216 new_channel->primary_channel = channel;
217 new_channel->device = channel->device;
218 mtx_lock(&channel->sc_lock);
220 &channel->sc_list_anchor,
223 mtx_unlock(&channel->sc_lock);
226 printf("VMBUS get multi-channel offer, "
228 new_channel->offer_msg.child_rel_id,
229 new_channel->offer_msg.offer.sub_channel_index);
232 /* Insert new channel into channel_anchor. */
233 mtx_lock(&hv_vmbus_g_connection.channel_lock);
234 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_anchor,
235 new_channel, list_entry);
236 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
239 printf("VMBUS: new multi-channel offer <%p>, "
240 "its primary channel is <%p>.\n",
241 new_channel, new_channel->primary_channel);
243 /*XXX add it to percpu_list */
245 new_channel->state = HV_CHANNEL_OPEN_STATE;
246 if (channel->sc_creation_callback != NULL) {
247 channel->sc_creation_callback(new_channel);
252 hv_vmbus_free_vmbus_channel(new_channel);
256 new_channel->state = HV_CHANNEL_OPEN_STATE;
259 * Start the process of binding this offer to the driver
260 * (We need to set the device field before calling
261 * hv_vmbus_child_device_add())
263 new_channel->device = hv_vmbus_child_device_create(
264 new_channel->offer_msg.offer.interface_type,
265 new_channel->offer_msg.offer.interface_instance, new_channel);
268 * Add the new device to the bus. This will kick off device-driver
269 * binding which eventually invokes the device driver's AddDevice()
272 ret = hv_vmbus_child_device_register(new_channel->device);
274 mtx_lock(&hv_vmbus_g_connection.channel_lock);
276 &hv_vmbus_g_connection.channel_anchor,
279 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
280 hv_vmbus_free_vmbus_channel(new_channel);
285 vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu)
287 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
289 if (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008 ||
290 hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7) {
291 /* Only cpu0 is supported */
295 chan->target_cpu = cpu;
296 chan->target_vcpu = hv_vmbus_g_context.hv_vcpu_index[cpu];
299 printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n",
300 chan->offer_msg.child_rel_id,
301 chan->target_cpu, chan->target_vcpu);
306 * Array of device guids that are performance critical. We try to distribute
307 * the interrupt load for these devices across all online cpus.
309 static const hv_guid high_perf_devices[] = {
323 * We use this static number to distribute the channel interrupt load.
325 static uint32_t next_vcpu;
328 * Starting with Win8, we can statically distribute the incoming
329 * channel interrupt load by binding a channel to VCPU. We
330 * implement here a simple round robin scheme for distributing
331 * the interrupt load.
332 * We will bind channels that are not performance critical to cpu 0 and
333 * performance critical channels (IDE, SCSI and Network) will be uniformly
334 * distributed across all available CPUs.
337 vmbus_channel_select_defcpu(struct hv_vmbus_channel *channel)
339 uint32_t current_cpu;
341 boolean_t is_perf_channel = FALSE;
342 const hv_guid *guid = &channel->offer_msg.offer.interface_type;
344 for (i = PERF_CHN_NIC; i < MAX_PERF_CHN; i++) {
345 if (memcmp(guid->data, high_perf_devices[i].data,
346 sizeof(hv_guid)) == 0) {
347 is_perf_channel = TRUE;
352 if (!is_perf_channel) {
354 vmbus_channel_cpu_set(channel, 0);
357 /* mp_ncpus should have the number cpus currently online */
358 current_cpu = (++next_vcpu % mp_ncpus);
359 vmbus_channel_cpu_set(channel, current_cpu);
363 * @brief Handler for channel offers from Hyper-V/Azure
365 * Handler for channel offers from vmbus in parent partition. We ignore
366 * all offers except network and storage offers. For each network and storage
367 * offers, we create a channel object and queue a work item to the channel
368 * object to process the offer synchronously
371 vmbus_channel_on_offer(hv_vmbus_channel_msg_header* hdr)
373 hv_vmbus_channel_offer_channel* offer;
374 hv_vmbus_channel_offer_channel* copied;
376 offer = (hv_vmbus_channel_offer_channel*) hdr;
379 hv_guid *guidInstance;
381 guidType = &offer->offer.interface_type;
382 guidInstance = &offer->offer.interface_instance;
385 copied = malloc(sizeof(*copied), M_DEVBUF, M_NOWAIT);
386 if (copied == NULL) {
387 printf("fail to allocate memory\n");
391 memcpy(copied, hdr, sizeof(*copied));
392 hv_queue_work_item(vmbus_channel_on_offer_internal, copied);
396 vmbus_channel_on_offer_internal(void* context)
398 hv_vmbus_channel* new_channel;
400 hv_vmbus_channel_offer_channel* offer = (hv_vmbus_channel_offer_channel*)context;
401 /* Allocate the channel object and save this offer */
402 new_channel = hv_vmbus_allocate_channel();
405 * By default we setup state to enable batched
406 * reading. A specific service can choose to
407 * disable this prior to opening the channel.
409 new_channel->batched_reading = TRUE;
411 new_channel->signal_event_param =
412 (hv_vmbus_input_signal_event *)
413 (HV_ALIGN_UP((unsigned long)
414 &new_channel->signal_event_buffer,
415 HV_HYPERCALL_PARAM_ALIGN));
417 new_channel->signal_event_param->connection_id.as_uint32_t = 0;
418 new_channel->signal_event_param->connection_id.u.id =
419 HV_VMBUS_EVENT_CONNECTION_ID;
420 new_channel->signal_event_param->flag_number = 0;
421 new_channel->signal_event_param->rsvd_z = 0;
423 if (hv_vmbus_protocal_version != HV_VMBUS_VERSION_WS2008) {
424 new_channel->is_dedicated_interrupt =
425 (offer->is_dedicated_interrupt != 0);
426 new_channel->signal_event_param->connection_id.u.id =
427 offer->connection_id;
430 memcpy(&new_channel->offer_msg, offer,
431 sizeof(hv_vmbus_channel_offer_channel));
432 new_channel->monitor_group = (uint8_t) offer->monitor_id / 32;
433 new_channel->monitor_bit = (uint8_t) offer->monitor_id % 32;
435 /* Select default cpu for this channel. */
436 vmbus_channel_select_defcpu(new_channel);
438 vmbus_channel_process_offer(new_channel);
440 free(offer, M_DEVBUF);
444 * @brief Rescind offer handler.
446 * We queue a work item to process this offer
450 vmbus_channel_on_offer_rescind(hv_vmbus_channel_msg_header* hdr)
452 hv_vmbus_channel_rescind_offer* rescind;
453 hv_vmbus_channel* channel;
455 rescind = (hv_vmbus_channel_rescind_offer*) hdr;
457 channel = hv_vmbus_g_connection.channels[rescind->child_rel_id];
461 hv_queue_work_item(vmbus_channel_on_offer_rescind_internal, channel);
462 hv_vmbus_g_connection.channels[rescind->child_rel_id] = NULL;
466 vmbus_channel_on_offer_rescind_internal(void *context)
468 hv_vmbus_channel* channel;
470 channel = (hv_vmbus_channel*)context;
471 if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
472 /* Only primary channel owns the hv_device */
473 hv_vmbus_child_device_unregister(channel->device);
479 * @brief Invoked when all offers have been delivered.
482 vmbus_channel_on_offers_delivered(hv_vmbus_channel_msg_header* hdr)
487 * @brief Open result handler.
489 * This is invoked when we received a response
490 * to our channel open request. Find the matching request, copy the
491 * response and signal the requesting thread.
494 vmbus_channel_on_open_result(hv_vmbus_channel_msg_header* hdr)
496 hv_vmbus_channel_open_result* result;
497 hv_vmbus_channel_msg_info* msg_info;
498 hv_vmbus_channel_msg_header* requestHeader;
499 hv_vmbus_channel_open_channel* openMsg;
501 result = (hv_vmbus_channel_open_result*) hdr;
504 * Find the open msg, copy the result and signal/unblock the wait event
506 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
508 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
510 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
512 if (requestHeader->message_type ==
513 HV_CHANNEL_MESSAGE_OPEN_CHANNEL) {
514 openMsg = (hv_vmbus_channel_open_channel*) msg_info->msg;
515 if (openMsg->child_rel_id == result->child_rel_id
516 && openMsg->open_id == result->open_id) {
517 memcpy(&msg_info->response.open_result, result,
518 sizeof(hv_vmbus_channel_open_result));
519 sema_post(&msg_info->wait_sema);
524 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
529 * @brief GPADL created handler.
531 * This is invoked when we received a response
532 * to our gpadl create request. Find the matching request, copy the
533 * response and signal the requesting thread.
536 vmbus_channel_on_gpadl_created(hv_vmbus_channel_msg_header* hdr)
538 hv_vmbus_channel_gpadl_created* gpadl_created;
539 hv_vmbus_channel_msg_info* msg_info;
540 hv_vmbus_channel_msg_header* request_header;
541 hv_vmbus_channel_gpadl_header* gpadl_header;
543 gpadl_created = (hv_vmbus_channel_gpadl_created*) hdr;
545 /* Find the establish msg, copy the result and signal/unblock
548 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
549 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
551 request_header = (hv_vmbus_channel_msg_header*) msg_info->msg;
552 if (request_header->message_type ==
553 HV_CHANNEL_MESSAGEL_GPADL_HEADER) {
555 (hv_vmbus_channel_gpadl_header*) request_header;
557 if ((gpadl_created->child_rel_id == gpadl_header->child_rel_id)
558 && (gpadl_created->gpadl == gpadl_header->gpadl)) {
559 memcpy(&msg_info->response.gpadl_created,
561 sizeof(hv_vmbus_channel_gpadl_created));
562 sema_post(&msg_info->wait_sema);
567 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
571 * @brief GPADL torndown handler.
573 * This is invoked when we received a respons
574 * to our gpadl teardown request. Find the matching request, copy the
575 * response and signal the requesting thread
578 vmbus_channel_on_gpadl_torndown(hv_vmbus_channel_msg_header* hdr)
580 hv_vmbus_channel_gpadl_torndown* gpadl_torndown;
581 hv_vmbus_channel_msg_info* msg_info;
582 hv_vmbus_channel_msg_header* requestHeader;
583 hv_vmbus_channel_gpadl_teardown* gpadlTeardown;
585 gpadl_torndown = (hv_vmbus_channel_gpadl_torndown*)hdr;
588 * Find the open msg, copy the result and signal/unblock the
592 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
594 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
596 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
598 if (requestHeader->message_type
599 == HV_CHANNEL_MESSAGE_GPADL_TEARDOWN) {
601 (hv_vmbus_channel_gpadl_teardown*) requestHeader;
603 if (gpadl_torndown->gpadl == gpadlTeardown->gpadl) {
604 memcpy(&msg_info->response.gpadl_torndown,
606 sizeof(hv_vmbus_channel_gpadl_torndown));
607 sema_post(&msg_info->wait_sema);
612 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
616 * @brief Version response handler.
618 * This is invoked when we received a response
619 * to our initiate contact request. Find the matching request, copy th
620 * response and signal the requesting thread.
623 vmbus_channel_on_version_response(hv_vmbus_channel_msg_header* hdr)
625 hv_vmbus_channel_msg_info* msg_info;
626 hv_vmbus_channel_msg_header* requestHeader;
627 hv_vmbus_channel_initiate_contact* initiate;
628 hv_vmbus_channel_version_response* versionResponse;
630 versionResponse = (hv_vmbus_channel_version_response*)hdr;
632 mtx_lock(&hv_vmbus_g_connection.channel_msg_lock);
633 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
635 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
636 if (requestHeader->message_type
637 == HV_CHANNEL_MESSAGE_INITIATED_CONTACT) {
639 (hv_vmbus_channel_initiate_contact*) requestHeader;
640 memcpy(&msg_info->response.version_response,
642 sizeof(hv_vmbus_channel_version_response));
643 sema_post(&msg_info->wait_sema);
646 mtx_unlock(&hv_vmbus_g_connection.channel_msg_lock);
651 * @brief Send a request to get all our pending offers.
654 hv_vmbus_request_channel_offers(void)
657 hv_vmbus_channel_msg_header* msg;
658 hv_vmbus_channel_msg_info* msg_info;
660 msg_info = (hv_vmbus_channel_msg_info *)
661 malloc(sizeof(hv_vmbus_channel_msg_info)
662 + sizeof(hv_vmbus_channel_msg_header), M_DEVBUF, M_NOWAIT);
664 if (msg_info == NULL) {
666 printf("Error VMBUS: malloc failed for Request Offers\n");
670 msg = (hv_vmbus_channel_msg_header*) msg_info->msg;
671 msg->message_type = HV_CHANNEL_MESSAGE_REQUEST_OFFERS;
673 ret = hv_vmbus_post_message(msg, sizeof(hv_vmbus_channel_msg_header));
675 free(msg_info, M_DEVBUF);
681 * @brief Release channels that are unattached/unconnected (i.e., no drivers associated)
684 hv_vmbus_release_unattached_channels(void)
686 hv_vmbus_channel *channel;
688 mtx_lock(&hv_vmbus_g_connection.channel_lock);
690 while (!TAILQ_EMPTY(&hv_vmbus_g_connection.channel_anchor)) {
691 channel = TAILQ_FIRST(&hv_vmbus_g_connection.channel_anchor);
692 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_anchor,
693 channel, list_entry);
695 if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
696 /* Only primary channel owns the hv_device */
697 hv_vmbus_child_device_unregister(channel->device);
699 hv_vmbus_free_vmbus_channel(channel);
701 bzero(hv_vmbus_g_connection.channels,
702 sizeof(hv_vmbus_channel*) * HV_CHANNEL_MAX_COUNT);
703 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
707 * @brief Select the best outgoing channel
709 * The channel whose vcpu binding is closest to the currect vcpu will
711 * If no multi-channel, always select primary channel
713 * @param primary - primary channel
715 struct hv_vmbus_channel *
716 vmbus_select_outgoing_channel(struct hv_vmbus_channel *primary)
718 hv_vmbus_channel *new_channel = NULL;
719 hv_vmbus_channel *outgoing_channel = primary;
720 int old_cpu_distance = 0;
721 int new_cpu_distance = 0;
723 int smp_pro_id = PCPU_GET(cpuid);
725 if (TAILQ_EMPTY(&primary->sc_list_anchor)) {
726 return outgoing_channel;
729 if (smp_pro_id >= MAXCPU) {
730 return outgoing_channel;
733 cur_vcpu = hv_vmbus_g_context.hv_vcpu_index[smp_pro_id];
735 TAILQ_FOREACH(new_channel, &primary->sc_list_anchor, sc_list_entry) {
736 if (new_channel->state != HV_CHANNEL_OPENED_STATE){
740 if (new_channel->target_vcpu == cur_vcpu){
744 old_cpu_distance = ((outgoing_channel->target_vcpu > cur_vcpu) ?
745 (outgoing_channel->target_vcpu - cur_vcpu) :
746 (cur_vcpu - outgoing_channel->target_vcpu));
748 new_cpu_distance = ((new_channel->target_vcpu > cur_vcpu) ?
749 (new_channel->target_vcpu - cur_vcpu) :
750 (cur_vcpu - new_channel->target_vcpu));
752 if (old_cpu_distance < new_cpu_distance) {
756 outgoing_channel = new_channel;
759 return(outgoing_channel);