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 "include/osd.h"
26 #include "include/logging.h"
28 #include "VmbusPrivate.h"
32 typedef void (*PFN_CHANNEL_MESSAGE_HANDLER)(VMBUS_CHANNEL_MESSAGE_HEADER* msg);
34 typedef struct _VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY {
35 VMBUS_CHANNEL_MESSAGE_TYPE messageType;
36 PFN_CHANNEL_MESSAGE_HANDLER messageHandler;
37 } VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY;
39 /* Internal routines */
43 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
46 VmbusChannelOnOpenResult(
47 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
51 VmbusChannelOnOfferRescind(
52 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
56 VmbusChannelOnGpadlCreated(
57 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
61 VmbusChannelOnGpadlTorndown(
62 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
66 VmbusChannelOnOffersDelivered(
67 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
71 VmbusChannelOnVersionResponse(
72 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
76 VmbusChannelProcessOffer(
81 VmbusChannelProcessRescindOffer(
88 #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 4
90 static const GUID gSupportedDeviceClasses[MAX_NUM_DEVICE_CLASSES_SUPPORTED]= {
91 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
92 {.Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}},/* Storage - SCSI */
93 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
94 {.Data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}}, /* Network */
95 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
96 {.Data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c, 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}}, /* Input */
97 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
98 {.Data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}}, /* IDE */
102 /* Channel message dispatch table */
103 static VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY gChannelMessageTable[ChannelMessageCount]= {
104 {ChannelMessageInvalid, NULL},
105 {ChannelMessageOfferChannel, VmbusChannelOnOffer},
106 {ChannelMessageRescindChannelOffer, VmbusChannelOnOfferRescind},
107 {ChannelMessageRequestOffers, NULL},
108 {ChannelMessageAllOffersDelivered, VmbusChannelOnOffersDelivered},
109 {ChannelMessageOpenChannel, NULL},
110 {ChannelMessageOpenChannelResult, VmbusChannelOnOpenResult},
111 {ChannelMessageCloseChannel, NULL},
112 {ChannelMessageGpadlHeader, NULL},
113 {ChannelMessageGpadlBody, NULL},
114 {ChannelMessageGpadlCreated, VmbusChannelOnGpadlCreated},
115 {ChannelMessageGpadlTeardown, NULL},
116 {ChannelMessageGpadlTorndown, VmbusChannelOnGpadlTorndown},
117 {ChannelMessageRelIdReleased, NULL},
118 {ChannelMessageInitiateContact, NULL},
119 {ChannelMessageVersionResponse, VmbusChannelOnVersionResponse},
120 {ChannelMessageUnload, NULL},
129 Allocate and initialize a vmbus channel object
132 static VMBUS_CHANNEL* AllocVmbusChannel(void)
134 VMBUS_CHANNEL* channel;
136 channel = kzalloc(sizeof(VMBUS_CHANNEL), GFP_ATOMIC);
142 spin_lock_init(&channel->inbound_lock);
144 channel->PollTimer = osd_TimerCreate(VmbusChannelOnTimer, channel);
145 if (!channel->PollTimer)
151 /* channel->dataWorkQueue = WorkQueueCreate("data"); */
152 channel->ControlWQ = create_workqueue("hv_vmbus_ctl");
153 if (!channel->ControlWQ)
155 osd_TimerClose(channel->PollTimer);
166 ReleaseVmbusChannel()
169 Release the vmbus channel object itself
172 static inline void ReleaseVmbusChannel(void* Context)
174 VMBUS_CHANNEL* channel = (VMBUS_CHANNEL*)Context;
178 DPRINT_DBG(VMBUS, "releasing channel (%p)", channel);
179 destroy_workqueue(channel->ControlWQ);
180 DPRINT_DBG(VMBUS, "channel released (%p)", channel);
193 Release the resources used by the vmbus channel object
196 static void FreeVmbusChannel(VMBUS_CHANNEL* Channel)
198 osd_TimerClose(Channel->PollTimer);
200 /* We have to release the channel's workqueue/thread in the vmbus's workqueue/thread context */
201 /* ie we can't destroy ourselves. */
202 osd_schedule_callback(gVmbusConnection.WorkQueue, ReleaseVmbusChannel,
210 VmbusChannelProcessOffer()
213 Process the offer by creating a channel/device associated with this offer
217 VmbusChannelProcessOffer(
222 VMBUS_CHANNEL* newChannel=(VMBUS_CHANNEL*)context;
226 VMBUS_CHANNEL* channel;
231 /* Make sure this is a new offer */
232 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
234 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
236 channel = CONTAINING_RECORD(curr, VMBUS_CHANNEL, ListEntry);
238 if (!memcmp(&channel->OfferMsg.Offer.InterfaceType, &newChannel->OfferMsg.Offer.InterfaceType,sizeof(GUID)) &&
239 !memcmp(&channel->OfferMsg.Offer.InterfaceInstance, &newChannel->OfferMsg.Offer.InterfaceInstance, sizeof(GUID)))
248 INSERT_TAIL_LIST(&gVmbusConnection.ChannelList, &newChannel->ListEntry);
250 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
254 DPRINT_DBG(VMBUS, "Ignoring duplicate offer for relid (%d)", newChannel->OfferMsg.ChildRelId);
255 FreeVmbusChannel(newChannel);
260 /* Start the process of binding this offer to the driver */
261 /* We need to set the DeviceObject field before calling VmbusChildDeviceAdd() */
262 newChannel->DeviceObject = VmbusChildDeviceCreate(
263 newChannel->OfferMsg.Offer.InterfaceType,
264 newChannel->OfferMsg.Offer.InterfaceInstance,
267 DPRINT_DBG(VMBUS, "child device object allocated - %p", newChannel->DeviceObject);
270 * Add the new device to the bus. This will kick off device-driver
271 * binding which eventually invokes the device driver's AddDevice()
275 ret = VmbusChildDeviceAdd(newChannel->DeviceObject);
278 DPRINT_ERR(VMBUS, "unable to add child device object (relid %d)",
279 newChannel->OfferMsg.ChildRelId);
281 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
282 REMOVE_ENTRY_LIST(&newChannel->ListEntry);
283 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
285 FreeVmbusChannel(newChannel);
290 * This state is used to indicate a successful open
291 * so that when we do close the channel normally, we
292 * can cleanup properly
294 newChannel->State = CHANNEL_OPEN_STATE;
302 VmbusChannelProcessRescindOffer()
305 Rescind the offer by initiating a device removal
309 VmbusChannelProcessRescindOffer(
313 VMBUS_CHANNEL* channel=(VMBUS_CHANNEL*)context;
317 VmbusChildDeviceRemove(channel->DeviceObject);
326 VmbusChannelOnOffer()
329 Handler for channel offers from vmbus in parent partition. We ignore all offers except
330 network and storage offers. For each network and storage offers, we create a channel object
331 and queue a work item to the channel object to process the offer synchronously
336 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
339 VMBUS_CHANNEL_OFFER_CHANNEL* offer = (VMBUS_CHANNEL_OFFER_CHANNEL*)hdr;
340 VMBUS_CHANNEL* newChannel;
349 for (i=0; i<MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++)
351 if (memcmp(&offer->Offer.InterfaceType, &gSupportedDeviceClasses[i], sizeof(GUID)) == 0)
360 DPRINT_DBG(VMBUS, "Ignoring channel offer notification for child relid %d", offer->ChildRelId);
366 guidType = &offer->Offer.InterfaceType;
367 guidInstance = &offer->Offer.InterfaceInstance;
369 DPRINT_INFO(VMBUS, "Channel offer notification - child relid %d monitor id %d allocated %d, "
370 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x} "
371 "instance {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x}",
374 offer->MonitorAllocated,
375 guidType->Data[3], guidType->Data[2], guidType->Data[1], guidType->Data[0], guidType->Data[5], guidType->Data[4], guidType->Data[7], guidType->Data[6], guidType->Data[8], guidType->Data[9], guidType->Data[10], guidType->Data[11], guidType->Data[12], guidType->Data[13], guidType->Data[14], guidType->Data[15],
376 guidInstance->Data[3], guidInstance->Data[2], guidInstance->Data[1], guidInstance->Data[0], guidInstance->Data[5], guidInstance->Data[4], guidInstance->Data[7], guidInstance->Data[6], guidInstance->Data[8], guidInstance->Data[9], guidInstance->Data[10], guidInstance->Data[11], guidInstance->Data[12], guidInstance->Data[13], guidInstance->Data[14], guidInstance->Data[15]);
378 /* Allocate the channel object and save this offer. */
379 newChannel = AllocVmbusChannel();
382 DPRINT_ERR(VMBUS, "unable to allocate channel object");
386 DPRINT_DBG(VMBUS, "channel object allocated - %p", newChannel);
388 memcpy(&newChannel->OfferMsg, offer, sizeof(VMBUS_CHANNEL_OFFER_CHANNEL));
389 newChannel->MonitorGroup = (u8)offer->MonitorId / 32;
390 newChannel->MonitorBit = (u8)offer->MonitorId % 32;
392 /* TODO: Make sure the offer comes from our parent partition */
393 osd_schedule_callback(newChannel->ControlWQ, VmbusChannelProcessOffer,
403 VmbusChannelOnOfferRescind()
406 Rescind offer handler. We queue a work item to process this offer
411 VmbusChannelOnOfferRescind(
412 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
415 VMBUS_CHANNEL_RESCIND_OFFER* rescind = (VMBUS_CHANNEL_RESCIND_OFFER*)hdr;
416 VMBUS_CHANNEL* channel;
420 channel = GetChannelFromRelId(rescind->ChildRelId);
423 DPRINT_DBG(VMBUS, "channel not found for relId %d", rescind->ChildRelId);
427 osd_schedule_callback(channel->ControlWQ,
428 VmbusChannelProcessRescindOffer,
438 VmbusChannelOnOffersDelivered()
441 This is invoked when all offers have been delivered.
446 VmbusChannelOnOffersDelivered(
447 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
458 VmbusChannelOnOpenResult()
461 Open result handler. This is invoked when we received a response
462 to our channel open request. Find the matching request, copy the
463 response and signal the requesting thread.
467 VmbusChannelOnOpenResult(
468 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
471 VMBUS_CHANNEL_OPEN_RESULT* result = (VMBUS_CHANNEL_OPEN_RESULT*)hdr;
474 VMBUS_CHANNEL_MSGINFO* msgInfo;
475 VMBUS_CHANNEL_MESSAGE_HEADER* requestHeader;
476 VMBUS_CHANNEL_OPEN_CHANNEL* openMsg;
481 DPRINT_DBG(VMBUS, "vmbus open result - %d", result->Status);
483 /* Find the open msg, copy the result and signal/unblock the wait event */
484 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
486 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
488 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
489 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
491 if (requestHeader->MessageType == ChannelMessageOpenChannel)
493 openMsg = (VMBUS_CHANNEL_OPEN_CHANNEL*)msgInfo->Msg;
494 if (openMsg->ChildRelId == result->ChildRelId &&
495 openMsg->OpenId == result->OpenId)
497 memcpy(&msgInfo->Response.OpenResult, result, sizeof(VMBUS_CHANNEL_OPEN_RESULT));
498 osd_WaitEventSet(msgInfo->WaitEvent);
503 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
512 VmbusChannelOnGpadlCreated()
515 GPADL created handler. This is invoked when we received a response
516 to our gpadl create request. Find the matching request, copy the
517 response and signal the requesting thread.
521 VmbusChannelOnGpadlCreated(
522 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
525 VMBUS_CHANNEL_GPADL_CREATED *gpadlCreated = (VMBUS_CHANNEL_GPADL_CREATED*)hdr;
528 VMBUS_CHANNEL_MSGINFO *msgInfo;
529 VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
530 VMBUS_CHANNEL_GPADL_HEADER *gpadlHeader;
535 DPRINT_DBG(VMBUS, "vmbus gpadl created result - %d", gpadlCreated->CreationStatus);
537 /* Find the establish msg, copy the result and signal/unblock the wait event */
538 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
540 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
542 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
543 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
545 if (requestHeader->MessageType == ChannelMessageGpadlHeader)
547 gpadlHeader = (VMBUS_CHANNEL_GPADL_HEADER*)requestHeader;
549 if ((gpadlCreated->ChildRelId == gpadlHeader->ChildRelId) &&
550 (gpadlCreated->Gpadl == gpadlHeader->Gpadl))
552 memcpy(&msgInfo->Response.GpadlCreated, gpadlCreated, sizeof(VMBUS_CHANNEL_GPADL_CREATED));
553 osd_WaitEventSet(msgInfo->WaitEvent);
558 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
567 VmbusChannelOnGpadlTorndown()
570 GPADL torndown handler. This is invoked when we received a response
571 to our gpadl teardown request. Find the matching request, copy the
572 response and signal the requesting thread.
576 VmbusChannelOnGpadlTorndown(
577 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
580 VMBUS_CHANNEL_GPADL_TORNDOWN* gpadlTorndown = (VMBUS_CHANNEL_GPADL_TORNDOWN*)hdr;
583 VMBUS_CHANNEL_MSGINFO* msgInfo;
584 VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
585 VMBUS_CHANNEL_GPADL_TEARDOWN *gpadlTeardown;
590 /* Find the open msg, copy the result and signal/unblock the wait event */
591 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
593 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
595 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
596 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
598 if (requestHeader->MessageType == ChannelMessageGpadlTeardown)
600 gpadlTeardown = (VMBUS_CHANNEL_GPADL_TEARDOWN*)requestHeader;
602 if (gpadlTorndown->Gpadl == gpadlTeardown->Gpadl)
604 memcpy(&msgInfo->Response.GpadlTorndown, gpadlTorndown, sizeof(VMBUS_CHANNEL_GPADL_TORNDOWN));
605 osd_WaitEventSet(msgInfo->WaitEvent);
610 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
619 VmbusChannelOnVersionResponse()
622 Version response handler. This is invoked when we received a response
623 to our initiate contact request. Find the matching request, copy the
624 response and signal the requesting thread.
628 VmbusChannelOnVersionResponse(
629 PVMBUS_CHANNEL_MESSAGE_HEADER hdr
634 VMBUS_CHANNEL_MSGINFO *msgInfo;
635 VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
636 VMBUS_CHANNEL_INITIATE_CONTACT *initiate;
637 VMBUS_CHANNEL_VERSION_RESPONSE *versionResponse = (VMBUS_CHANNEL_VERSION_RESPONSE*)hdr;
642 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
644 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
646 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
647 requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
649 if (requestHeader->MessageType == ChannelMessageInitiateContact)
651 initiate = (VMBUS_CHANNEL_INITIATE_CONTACT*)requestHeader;
652 memcpy(&msgInfo->Response.VersionResponse, versionResponse, sizeof(VMBUS_CHANNEL_VERSION_RESPONSE));
653 osd_WaitEventSet(msgInfo->WaitEvent);
656 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
665 VmbusOnChannelMessage()
668 Handler for channel protocol messages.
669 This is invoked in the vmbus worker thread context.
673 VmbusOnChannelMessage(
677 HV_MESSAGE *msg=(HV_MESSAGE*)Context;
678 VMBUS_CHANNEL_MESSAGE_HEADER* hdr;
683 hdr = (VMBUS_CHANNEL_MESSAGE_HEADER*)msg->u.Payload;
684 size=msg->Header.PayloadSize;
686 DPRINT_DBG(VMBUS, "message type %d size %d", hdr->MessageType, size);
688 if (hdr->MessageType >= ChannelMessageCount)
690 DPRINT_ERR(VMBUS, "Received invalid channel message type %d size %d", hdr->MessageType, size);
691 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
692 (unsigned char *)msg->u.Payload, size);
697 if (gChannelMessageTable[hdr->MessageType].messageHandler)
699 gChannelMessageTable[hdr->MessageType].messageHandler(hdr);
703 DPRINT_ERR(VMBUS, "Unhandled channel message type %d", hdr->MessageType);
706 /* Free the msg that was allocated in VmbusOnMsgDPC() */
715 VmbusChannelRequestOffers()
718 Send a request to get all our pending offers.
722 VmbusChannelRequestOffers(
727 VMBUS_CHANNEL_MESSAGE_HEADER* msg;
728 VMBUS_CHANNEL_MSGINFO* msgInfo;
732 msgInfo = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_MESSAGE_HEADER), GFP_KERNEL);
733 ASSERT(msgInfo != NULL);
735 msgInfo->WaitEvent = osd_WaitEventCreate();
736 msg = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
738 msg->MessageType = ChannelMessageRequestOffers;
740 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
741 INSERT_TAIL_LIST(&gVmbusConnection.channelMsgList, &msgInfo->msgListEntry);
742 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
744 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_MESSAGE_HEADER));
747 DPRINT_ERR(VMBUS, "Unable to request offers - %d", ret);
749 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
750 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
751 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
755 /* osd_WaitEventWait(msgInfo->waitEvent); */
757 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
758 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
759 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
765 kfree(msgInfo->WaitEvent);
777 VmbusChannelReleaseUnattachedChannels()
780 Release channels that are unattached/unconnected ie (no drivers associated)
784 VmbusChannelReleaseUnattachedChannels(
789 VMBUS_CHANNEL *channel;
790 VMBUS_CHANNEL *start=NULL;
793 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
795 while (!IsListEmpty(&gVmbusConnection.ChannelList))
797 entry = TOP_LIST_ENTRY(&gVmbusConnection.ChannelList);
798 channel = CONTAINING_RECORD(entry, VMBUS_CHANNEL, ListEntry);
800 if (channel == start)
803 if (!channel->DeviceObject->Driver)
805 REMOVE_ENTRY_LIST(&channel->ListEntry);
806 DPRINT_INFO(VMBUS, "Releasing unattached device object %p", channel->DeviceObject);
808 VmbusChildDeviceRemove(channel->DeviceObject);
809 FreeVmbusChannel(channel);
820 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);