3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
25 #include <linux/kernel.h>
28 #include "include/logging.h"
30 #include "VmbusPrivate.h"
34 typedef void (*PFN_CHANNEL_MESSAGE_HANDLER)(VMBUS_CHANNEL_MESSAGE_HEADER* msg);
36 typedef struct _VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY {
37 VMBUS_CHANNEL_MESSAGE_TYPE messageType;
38 PFN_CHANNEL_MESSAGE_HANDLER messageHandler;
39 } VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY;
41 /* Internal routines */
45 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
48 VmbusChannelOnOpenResult(
49 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
53 VmbusChannelOnOfferRescind(
54 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
58 VmbusChannelOnGpadlCreated(
59 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
63 VmbusChannelOnGpadlTorndown(
64 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
68 VmbusChannelOnOffersDelivered(
69 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
73 VmbusChannelOnVersionResponse(
74 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
78 VmbusChannelProcessOffer(
83 VmbusChannelProcessRescindOffer(
90 #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 4
92 static const struct hv_guid gSupportedDeviceClasses[MAX_NUM_DEVICE_CLASSES_SUPPORTED] = {
93 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
97 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
98 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
102 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
106 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
107 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
111 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
115 0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
116 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A
120 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
124 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
125 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5
130 /* Channel message dispatch table */
131 static VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY gChannelMessageTable[ChannelMessageCount]= {
132 {ChannelMessageInvalid, NULL},
133 {ChannelMessageOfferChannel, VmbusChannelOnOffer},
134 {ChannelMessageRescindChannelOffer, VmbusChannelOnOfferRescind},
135 {ChannelMessageRequestOffers, NULL},
136 {ChannelMessageAllOffersDelivered, VmbusChannelOnOffersDelivered},
137 {ChannelMessageOpenChannel, NULL},
138 {ChannelMessageOpenChannelResult, VmbusChannelOnOpenResult},
139 {ChannelMessageCloseChannel, NULL},
140 {ChannelMessageGpadlHeader, NULL},
141 {ChannelMessageGpadlBody, NULL},
142 {ChannelMessageGpadlCreated, VmbusChannelOnGpadlCreated},
143 {ChannelMessageGpadlTeardown, NULL},
144 {ChannelMessageGpadlTorndown, VmbusChannelOnGpadlTorndown},
145 {ChannelMessageRelIdReleased, NULL},
146 {ChannelMessageInitiateContact, NULL},
147 {ChannelMessageVersionResponse, VmbusChannelOnVersionResponse},
148 {ChannelMessageUnload, NULL},
157 Allocate and initialize a vmbus channel object
160 struct vmbus_channel *AllocVmbusChannel(void)
162 struct vmbus_channel *channel;
164 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
170 spin_lock_init(&channel->inbound_lock);
172 init_timer(&channel->poll_timer);
173 channel->poll_timer.data = (unsigned long)channel;
174 channel->poll_timer.function = VmbusChannelOnTimer;
176 /* channel->dataWorkQueue = WorkQueueCreate("data"); */
177 channel->ControlWQ = create_workqueue("hv_vmbus_ctl");
178 if (!channel->ControlWQ)
190 ReleaseVmbusChannel()
193 Release the vmbus channel object itself
196 static inline void ReleaseVmbusChannel(void* Context)
198 struct vmbus_channel *channel = Context;
202 DPRINT_DBG(VMBUS, "releasing channel (%p)", channel);
203 destroy_workqueue(channel->ControlWQ);
204 DPRINT_DBG(VMBUS, "channel released (%p)", channel);
217 Release the resources used by the vmbus channel object
220 void FreeVmbusChannel(struct vmbus_channel *Channel)
222 del_timer(&Channel->poll_timer);
224 /* We have to release the channel's workqueue/thread in the vmbus's workqueue/thread context */
225 /* ie we can't destroy ourselves. */
226 osd_schedule_callback(gVmbusConnection.WorkQueue, ReleaseVmbusChannel,
234 VmbusChannelProcessOffer()
237 Process the offer by creating a channel/device associated with this offer
241 VmbusChannelProcessOffer(
246 struct vmbus_channel *newChannel = context;
247 struct vmbus_channel *channel;
255 /* Make sure this is a new offer */
256 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
258 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
260 channel = CONTAINING_RECORD(curr, struct vmbus_channel, ListEntry);
262 if (!memcmp(&channel->OfferMsg.Offer.InterfaceType, &newChannel->OfferMsg.Offer.InterfaceType,sizeof(struct hv_guid)) &&
263 !memcmp(&channel->OfferMsg.Offer.InterfaceInstance, &newChannel->OfferMsg.Offer.InterfaceInstance, sizeof(struct hv_guid)))
272 INSERT_TAIL_LIST(&gVmbusConnection.ChannelList, &newChannel->ListEntry);
274 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
278 DPRINT_DBG(VMBUS, "Ignoring duplicate offer for relid (%d)", newChannel->OfferMsg.ChildRelId);
279 FreeVmbusChannel(newChannel);
284 /* Start the process of binding this offer to the driver */
285 /* We need to set the DeviceObject field before calling VmbusChildDeviceAdd() */
286 newChannel->DeviceObject = VmbusChildDeviceCreate(
287 &newChannel->OfferMsg.Offer.InterfaceType,
288 &newChannel->OfferMsg.Offer.InterfaceInstance,
291 DPRINT_DBG(VMBUS, "child device object allocated - %p", newChannel->DeviceObject);
294 * Add the new device to the bus. This will kick off device-driver
295 * binding which eventually invokes the device driver's AddDevice()
299 ret = VmbusChildDeviceAdd(newChannel->DeviceObject);
302 DPRINT_ERR(VMBUS, "unable to add child device object (relid %d)",
303 newChannel->OfferMsg.ChildRelId);
305 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
306 REMOVE_ENTRY_LIST(&newChannel->ListEntry);
307 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
309 FreeVmbusChannel(newChannel);
314 * This state is used to indicate a successful open
315 * so that when we do close the channel normally, we
316 * can cleanup properly
318 newChannel->State = CHANNEL_OPEN_STATE;
326 VmbusChannelProcessRescindOffer()
329 Rescind the offer by initiating a device removal
333 VmbusChannelProcessRescindOffer(
337 struct vmbus_channel *channel = context;
341 VmbusChildDeviceRemove(channel->DeviceObject);
350 VmbusChannelOnOffer()
353 Handler for channel offers from vmbus in parent partition. We ignore all offers except
354 network and storage offers. For each network and storage offers, we create a channel object
355 and queue a work item to the channel object to process the offer synchronously
360 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
363 VMBUS_CHANNEL_OFFER_CHANNEL* offer = (VMBUS_CHANNEL_OFFER_CHANNEL*)hdr;
364 struct vmbus_channel *newChannel;
366 struct hv_guid *guidType;
367 struct hv_guid *guidInstance;
373 for (i=0; i<MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++)
375 if (memcmp(&offer->Offer.InterfaceType, &gSupportedDeviceClasses[i], sizeof(struct hv_guid)) == 0)
384 DPRINT_DBG(VMBUS, "Ignoring channel offer notification for child relid %d", offer->ChildRelId);
390 guidType = &offer->Offer.InterfaceType;
391 guidInstance = &offer->Offer.InterfaceInstance;
393 DPRINT_INFO(VMBUS, "Channel offer notification - child relid %d monitor id %d allocated %d, "
394 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x} "
395 "instance {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x}",
398 offer->MonitorAllocated,
399 guidType->data[3], guidType->data[2], guidType->data[1], guidType->data[0],
400 guidType->data[5], guidType->data[4], guidType->data[7], guidType->data[6],
401 guidType->data[8], guidType->data[9], guidType->data[10], guidType->data[11],
402 guidType->data[12], guidType->data[13], guidType->data[14], guidType->data[15],
403 guidInstance->data[3], guidInstance->data[2], guidInstance->data[1], guidInstance->data[0],
404 guidInstance->data[5], guidInstance->data[4], guidInstance->data[7], guidInstance->data[6],
405 guidInstance->data[8], guidInstance->data[9], guidInstance->data[10], guidInstance->data[11],
406 guidInstance->data[12], guidInstance->data[13], guidInstance->data[14], guidInstance->data[15]);
408 /* Allocate the channel object and save this offer. */
409 newChannel = AllocVmbusChannel();
412 DPRINT_ERR(VMBUS, "unable to allocate channel object");
416 DPRINT_DBG(VMBUS, "channel object allocated - %p", newChannel);
418 memcpy(&newChannel->OfferMsg, offer, sizeof(VMBUS_CHANNEL_OFFER_CHANNEL));
419 newChannel->MonitorGroup = (u8)offer->MonitorId / 32;
420 newChannel->MonitorBit = (u8)offer->MonitorId % 32;
422 /* TODO: Make sure the offer comes from our parent partition */
423 osd_schedule_callback(newChannel->ControlWQ, VmbusChannelProcessOffer,
433 VmbusChannelOnOfferRescind()
436 Rescind offer handler. We queue a work item to process this offer
441 VmbusChannelOnOfferRescind(
442 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
445 VMBUS_CHANNEL_RESCIND_OFFER* rescind = (VMBUS_CHANNEL_RESCIND_OFFER*)hdr;
446 struct vmbus_channel *channel;
450 channel = GetChannelFromRelId(rescind->ChildRelId);
453 DPRINT_DBG(VMBUS, "channel not found for relId %d", rescind->ChildRelId);
457 osd_schedule_callback(channel->ControlWQ,
458 VmbusChannelProcessRescindOffer,
468 VmbusChannelOnOffersDelivered()
471 This is invoked when all offers have been delivered.
476 VmbusChannelOnOffersDelivered(
477 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
488 VmbusChannelOnOpenResult()
491 Open result handler. This is invoked when we received a response
492 to our channel open request. Find the matching request, copy the
493 response and signal the requesting thread.
497 VmbusChannelOnOpenResult(
498 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
501 VMBUS_CHANNEL_OPEN_RESULT* result = (VMBUS_CHANNEL_OPEN_RESULT*)hdr;
504 struct vmbus_channel_msginfo *msgInfo;
505 VMBUS_CHANNEL_MESSAGE_HEADER* requestHeader;
506 VMBUS_CHANNEL_OPEN_CHANNEL* openMsg;
511 DPRINT_DBG(VMBUS, "vmbus open result - %d", result->Status);
513 /* Find the open msg, copy the result and signal/unblock the wait event */
514 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
516 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
518 msgInfo = (struct vmbus_channel_msginfo *)curr;
519 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
521 if (requestHeader->MessageType == ChannelMessageOpenChannel)
523 openMsg = (VMBUS_CHANNEL_OPEN_CHANNEL*)msgInfo->Msg;
524 if (openMsg->ChildRelId == result->ChildRelId &&
525 openMsg->OpenId == result->OpenId)
527 memcpy(&msgInfo->Response.OpenResult, result, sizeof(VMBUS_CHANNEL_OPEN_RESULT));
528 osd_WaitEventSet(msgInfo->WaitEvent);
533 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
542 VmbusChannelOnGpadlCreated()
545 GPADL created handler. This is invoked when we received a response
546 to our gpadl create request. Find the matching request, copy the
547 response and signal the requesting thread.
551 VmbusChannelOnGpadlCreated(
552 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
555 VMBUS_CHANNEL_GPADL_CREATED *gpadlCreated = (VMBUS_CHANNEL_GPADL_CREATED*)hdr;
558 struct vmbus_channel_msginfo *msgInfo;
559 VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
560 VMBUS_CHANNEL_GPADL_HEADER *gpadlHeader;
565 DPRINT_DBG(VMBUS, "vmbus gpadl created result - %d", gpadlCreated->CreationStatus);
567 /* Find the establish msg, copy the result and signal/unblock the wait event */
568 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
570 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
572 msgInfo = (struct vmbus_channel_msginfo *)curr;
573 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
575 if (requestHeader->MessageType == ChannelMessageGpadlHeader)
577 gpadlHeader = (VMBUS_CHANNEL_GPADL_HEADER*)requestHeader;
579 if ((gpadlCreated->ChildRelId == gpadlHeader->ChildRelId) &&
580 (gpadlCreated->Gpadl == gpadlHeader->Gpadl))
582 memcpy(&msgInfo->Response.GpadlCreated, gpadlCreated, sizeof(VMBUS_CHANNEL_GPADL_CREATED));
583 osd_WaitEventSet(msgInfo->WaitEvent);
588 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
597 VmbusChannelOnGpadlTorndown()
600 GPADL torndown handler. This is invoked when we received a response
601 to our gpadl teardown request. Find the matching request, copy the
602 response and signal the requesting thread.
606 VmbusChannelOnGpadlTorndown(
607 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
610 VMBUS_CHANNEL_GPADL_TORNDOWN* gpadlTorndown = (VMBUS_CHANNEL_GPADL_TORNDOWN*)hdr;
613 struct vmbus_channel_msginfo *msgInfo;
614 VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
615 VMBUS_CHANNEL_GPADL_TEARDOWN *gpadlTeardown;
620 /* Find the open msg, copy the result and signal/unblock the wait event */
621 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
623 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
625 msgInfo = (struct vmbus_channel_msginfo *)curr;
626 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
628 if (requestHeader->MessageType == ChannelMessageGpadlTeardown)
630 gpadlTeardown = (VMBUS_CHANNEL_GPADL_TEARDOWN*)requestHeader;
632 if (gpadlTorndown->Gpadl == gpadlTeardown->Gpadl)
634 memcpy(&msgInfo->Response.GpadlTorndown, gpadlTorndown, sizeof(VMBUS_CHANNEL_GPADL_TORNDOWN));
635 osd_WaitEventSet(msgInfo->WaitEvent);
640 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
649 VmbusChannelOnVersionResponse()
652 Version response handler. This is invoked when we received a response
653 to our initiate contact request. Find the matching request, copy the
654 response and signal the requesting thread.
658 VmbusChannelOnVersionResponse(
659 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
664 struct vmbus_channel_msginfo *msgInfo;
665 VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
666 VMBUS_CHANNEL_INITIATE_CONTACT *initiate;
667 VMBUS_CHANNEL_VERSION_RESPONSE *versionResponse = (VMBUS_CHANNEL_VERSION_RESPONSE*)hdr;
672 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
674 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
676 msgInfo = (struct vmbus_channel_msginfo *)curr;
677 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
679 if (requestHeader->MessageType == ChannelMessageInitiateContact)
681 initiate = (VMBUS_CHANNEL_INITIATE_CONTACT*)requestHeader;
682 memcpy(&msgInfo->Response.VersionResponse, versionResponse, sizeof(VMBUS_CHANNEL_VERSION_RESPONSE));
683 osd_WaitEventSet(msgInfo->WaitEvent);
686 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
695 VmbusOnChannelMessage()
698 Handler for channel protocol messages.
699 This is invoked in the vmbus worker thread context.
702 void VmbusOnChannelMessage(void *Context)
704 HV_MESSAGE *msg=(HV_MESSAGE*)Context;
705 VMBUS_CHANNEL_MESSAGE_HEADER* hdr;
710 hdr = (VMBUS_CHANNEL_MESSAGE_HEADER*)msg->u.Payload;
711 size=msg->Header.PayloadSize;
713 DPRINT_DBG(VMBUS, "message type %d size %d", hdr->MessageType, size);
715 if (hdr->MessageType >= ChannelMessageCount)
717 DPRINT_ERR(VMBUS, "Received invalid channel message type %d size %d", hdr->MessageType, size);
718 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
719 (unsigned char *)msg->u.Payload, size);
724 if (gChannelMessageTable[hdr->MessageType].messageHandler)
726 gChannelMessageTable[hdr->MessageType].messageHandler(hdr);
730 DPRINT_ERR(VMBUS, "Unhandled channel message type %d", hdr->MessageType);
733 /* Free the msg that was allocated in VmbusOnMsgDPC() */
742 VmbusChannelRequestOffers()
745 Send a request to get all our pending offers.
748 int VmbusChannelRequestOffers(void)
751 VMBUS_CHANNEL_MESSAGE_HEADER* msg;
752 struct vmbus_channel_msginfo *msgInfo;
756 msgInfo = kmalloc(sizeof(*msgInfo) + sizeof(VMBUS_CHANNEL_MESSAGE_HEADER), GFP_KERNEL);
757 ASSERT(msgInfo != NULL);
759 msgInfo->WaitEvent = osd_WaitEventCreate();
760 msg = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
762 msg->MessageType = ChannelMessageRequestOffers;
764 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
765 INSERT_TAIL_LIST(&gVmbusConnection.channelMsgList, &msgInfo->msgListEntry);
766 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
768 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_MESSAGE_HEADER));
771 DPRINT_ERR(VMBUS, "Unable to request offers - %d", ret);
773 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
774 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
775 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
779 /* osd_WaitEventWait(msgInfo->waitEvent); */
781 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
782 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
783 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
789 kfree(msgInfo->WaitEvent);
801 VmbusChannelReleaseUnattachedChannels()
804 Release channels that are unattached/unconnected ie (no drivers associated)
807 void VmbusChannelReleaseUnattachedChannels(void)
810 struct vmbus_channel *channel;
811 struct vmbus_channel *start = NULL;
814 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
816 while (!IsListEmpty(&gVmbusConnection.ChannelList))
818 entry = TOP_LIST_ENTRY(&gVmbusConnection.ChannelList);
819 channel = CONTAINING_RECORD(entry, struct vmbus_channel, ListEntry);
821 if (channel == start)
824 if (!channel->DeviceObject->Driver)
826 REMOVE_ENTRY_LIST(&channel->ListEntry);
827 DPRINT_INFO(VMBUS, "Releasing unattached device object %p", channel->DeviceObject);
829 VmbusChildDeviceRemove(channel->DeviceObject);
830 FreeVmbusChannel(channel);
841 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);