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/logging.h"
27 #include "VmbusPrivate.h"
32 struct VMBUS_CONNECTION gVmbusConnection = {
33 .ConnectState = Disconnected,
34 .NextGpadlHandle = 0xE1E10,
44 Sends a connect request on the partition service connection
51 VMBUS_CHANNEL_MSGINFO *msgInfo=NULL;
52 VMBUS_CHANNEL_INITIATE_CONTACT *msg;
57 /* Make sure we are not connecting or connected */
58 if (gVmbusConnection.ConnectState != Disconnected)
61 /* Initialize the vmbus connection */
62 gVmbusConnection.ConnectState = Connecting;
63 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
64 if (!gVmbusConnection.WorkQueue)
70 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
71 spin_lock_init(&gVmbusConnection.channelmsg_lock);
73 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
74 spin_lock_init(&gVmbusConnection.channel_lock);
77 * Setup the vmbus event connection for channel interrupt
80 gVmbusConnection.InterruptPage = PageAlloc(1);
81 if (gVmbusConnection.InterruptPage == NULL)
87 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
88 gVmbusConnection.SendInterruptPage = (void*)((unsigned long)gVmbusConnection.InterruptPage + (PAGE_SIZE >> 1));
91 * notification facility. The 1st page for parent->child and
92 * the 2nd page for child->parent
94 gVmbusConnection.MonitorPages = PageAlloc(2);
95 if (gVmbusConnection.MonitorPages == NULL)
101 msgInfo = kzalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_INITIATE_CONTACT), GFP_KERNEL);
108 msgInfo->WaitEvent = WaitEventCreate();
109 msg = (VMBUS_CHANNEL_INITIATE_CONTACT*)msgInfo->Msg;
111 msg->Header.MessageType = ChannelMessageInitiateContact;
112 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
113 msg->InterruptPage = GetPhysicalAddress(gVmbusConnection.InterruptPage);
114 msg->MonitorPage1 = GetPhysicalAddress(gVmbusConnection.MonitorPages);
115 msg->MonitorPage2 = GetPhysicalAddress((void *)((unsigned long)gVmbusConnection.MonitorPages + PAGE_SIZE));
118 * Add to list before we send the request since we may
119 * receive the response before returning from this routine
121 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
122 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
123 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
125 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, monitor1 pfn %llx,, monitor2 pfn %llx",
126 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
128 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
130 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_INITIATE_CONTACT));
133 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
137 /* Wait for the connection response */
138 WaitEventWait(msgInfo->WaitEvent);
140 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
142 /* Check if successful */
143 if (msgInfo->Response.VersionResponse.VersionSupported)
145 DPRINT_INFO(VMBUS, "Vmbus connected!!");
146 gVmbusConnection.ConnectState = Connected;
151 DPRINT_ERR(VMBUS, "Vmbus connection failed!!...current version (%d) not supported", VMBUS_REVISION_NUMBER);
158 WaitEventClose(msgInfo->WaitEvent);
166 gVmbusConnection.ConnectState = Disconnected;
168 if (gVmbusConnection.WorkQueue)
169 destroy_workqueue(gVmbusConnection.WorkQueue);
171 if (gVmbusConnection.InterruptPage)
173 PageFree(gVmbusConnection.InterruptPage, 1);
174 gVmbusConnection.InterruptPage = NULL;
177 if (gVmbusConnection.MonitorPages)
179 PageFree(gVmbusConnection.MonitorPages, 2);
180 gVmbusConnection.MonitorPages = NULL;
185 if (msgInfo->WaitEvent)
186 WaitEventClose(msgInfo->WaitEvent);
203 Sends a disconnect request on the partition service connection
212 VMBUS_CHANNEL_UNLOAD *msg;
216 /* Make sure we are connected */
217 if (gVmbusConnection.ConnectState != Connected)
220 msg = kzalloc(sizeof(VMBUS_CHANNEL_UNLOAD), GFP_KERNEL);
222 msg->MessageType = ChannelMessageUnload;
224 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_UNLOAD));
231 PageFree(gVmbusConnection.InterruptPage, 1);
233 /* TODO: iterate thru the msg list and free up */
235 destroy_workqueue(gVmbusConnection.WorkQueue);
237 gVmbusConnection.ConnectState = Disconnected;
239 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
256 GetChannelFromRelId()
259 Get the channel object given its child relative id (ie channel id)
262 static VMBUS_CHANNEL*
267 VMBUS_CHANNEL* channel;
268 VMBUS_CHANNEL* foundChannel=NULL;
273 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
274 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
276 channel = CONTAINING_RECORD(curr, VMBUS_CHANNEL, ListEntry);
278 if (channel->OfferMsg.ChildRelId == relId)
280 foundChannel = channel;
284 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
294 VmbusProcessChannelEvent()
297 Process a channel event notification
301 VmbusProcessChannelEvent(
305 VMBUS_CHANNEL* channel;
306 u32 relId = (u32)(unsigned long)context;
311 * Find the channel based on this relid and invokes the
312 * channel callback to process the event
314 channel = GetChannelFromRelId(relId);
318 VmbusChannelOnChannelEvent(channel);
319 /* WorkQueueQueueWorkItem(channel->dataWorkQueue, VmbusChannelOnChannelEvent, (void*)channel); */
323 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
343 /* int maxdword = PAGE_SIZE >> 3; // receive size is 1/2 page and divide that by 4 bytes */
344 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
347 u32* recvInterruptPage = gVmbusConnection.RecvInterruptPage;
348 /* VMBUS_CHANNEL_MESSAGE* receiveMsg; */
353 if (recvInterruptPage)
355 for (dword = 0; dword < maxdword; dword++)
357 if (recvInterruptPage[dword])
359 for (bit = 0; bit < 32; bit++)
361 if (BitTestAndClear(&recvInterruptPage[dword], bit))
363 relid = (dword << 5) + bit;
365 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
367 if (relid == 0) /* special case - vmbus channel protocol msg */
369 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
374 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
375 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
376 VmbusProcessChannelEvent((void*)(unsigned long)relid);
394 Send a msg on the vmbus's message connection
404 HV_CONNECTION_ID connId;
408 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
424 Send an event notification to the parent
428 VmbusSetEvent(u32 childRelId)
434 /* Each u32 represents 32 channels */
435 BitSet((u32*)gVmbusConnection.SendInterruptPage + (childRelId >> 5), childRelId & 31);
436 ret = HvSignalEvent();