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>
27 #include <linux/vmalloc.h>
29 #include "include/logging.h"
30 #include "VmbusPrivate.h"
35 struct VMBUS_CONNECTION gVmbusConnection = {
36 .ConnectState = Disconnected,
37 .NextGpadlHandle = ATOMIC_INIT(0xE1E10),
47 Sends a connect request on the partition service connection
50 int VmbusConnect(void)
53 struct vmbus_channel_msginfo *msgInfo = NULL;
54 struct vmbus_channel_initiate_contact *msg;
59 /* Make sure we are not connecting or connected */
60 if (gVmbusConnection.ConnectState != Disconnected)
63 /* Initialize the vmbus connection */
64 gVmbusConnection.ConnectState = Connecting;
65 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
66 if (!gVmbusConnection.WorkQueue)
72 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
73 spin_lock_init(&gVmbusConnection.channelmsg_lock);
75 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
76 spin_lock_init(&gVmbusConnection.channel_lock);
79 * Setup the vmbus event connection for channel interrupt
82 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
83 if (gVmbusConnection.InterruptPage == NULL)
89 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
90 gVmbusConnection.SendInterruptPage = (void*)((unsigned long)gVmbusConnection.InterruptPage + (PAGE_SIZE >> 1));
93 * notification facility. The 1st page for parent->child and
94 * the 2nd page for child->parent
96 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
97 if (gVmbusConnection.MonitorPages == NULL)
103 msgInfo = kzalloc(sizeof(*msgInfo) + sizeof(struct vmbus_channel_initiate_contact), GFP_KERNEL);
110 msgInfo->WaitEvent = osd_WaitEventCreate();
111 msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg;
113 msg->Header.MessageType = ChannelMessageInitiateContact;
114 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
115 msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
116 msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
117 msg->MonitorPage2 = virt_to_phys((void *)((unsigned long)gVmbusConnection.MonitorPages + PAGE_SIZE));
120 * Add to list before we send the request since we may
121 * receive the response before returning from this routine
123 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
124 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
125 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
127 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, monitor1 pfn %llx,, monitor2 pfn %llx",
128 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
130 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
132 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_initiate_contact));
135 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
139 /* Wait for the connection response */
140 osd_WaitEventWait(msgInfo->WaitEvent);
142 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
144 /* Check if successful */
145 if (msgInfo->Response.VersionResponse.VersionSupported)
147 DPRINT_INFO(VMBUS, "Vmbus connected!!");
148 gVmbusConnection.ConnectState = Connected;
153 DPRINT_ERR(VMBUS, "Vmbus connection failed!!...current version (%d) not supported", VMBUS_REVISION_NUMBER);
160 kfree(msgInfo->WaitEvent);
168 gVmbusConnection.ConnectState = Disconnected;
170 if (gVmbusConnection.WorkQueue)
171 destroy_workqueue(gVmbusConnection.WorkQueue);
173 if (gVmbusConnection.InterruptPage)
175 osd_PageFree(gVmbusConnection.InterruptPage, 1);
176 gVmbusConnection.InterruptPage = NULL;
179 if (gVmbusConnection.MonitorPages)
181 osd_PageFree(gVmbusConnection.MonitorPages, 2);
182 gVmbusConnection.MonitorPages = NULL;
187 if (msgInfo->WaitEvent)
188 kfree(msgInfo->WaitEvent);
205 Sends a disconnect request on the partition service connection
208 int VmbusDisconnect(void)
211 struct vmbus_channel_message_header *msg;
215 /* Make sure we are connected */
216 if (gVmbusConnection.ConnectState != Connected)
219 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
221 msg->MessageType = ChannelMessageUnload;
223 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_message_header));
230 osd_PageFree(gVmbusConnection.InterruptPage, 1);
232 /* TODO: iterate thru the msg list and free up */
234 destroy_workqueue(gVmbusConnection.WorkQueue);
236 gVmbusConnection.ConnectState = Disconnected;
238 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
255 GetChannelFromRelId()
258 Get the channel object given its child relative id (ie channel id)
261 struct vmbus_channel *GetChannelFromRelId(u32 relId)
263 struct vmbus_channel *channel;
264 struct vmbus_channel *foundChannel = NULL;
269 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
270 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
272 channel = CONTAINING_RECORD(curr, struct vmbus_channel, ListEntry);
274 if (channel->OfferMsg.ChildRelId == relId)
276 foundChannel = channel;
280 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
290 VmbusProcessChannelEvent()
293 Process a channel event notification
297 VmbusProcessChannelEvent(
301 struct vmbus_channel *channel;
302 u32 relId = (u32)(unsigned long)context;
307 * Find the channel based on this relid and invokes the
308 * channel callback to process the event
310 channel = GetChannelFromRelId(relId);
314 VmbusChannelOnChannelEvent(channel);
315 /* WorkQueueQueueWorkItem(channel->dataWorkQueue, VmbusChannelOnChannelEvent, (void*)channel); */
319 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
333 void VmbusOnEvents(void)
336 /* int maxdword = PAGE_SIZE >> 3; // receive size is 1/2 page and divide that by 4 bytes */
337 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
340 u32* recvInterruptPage = gVmbusConnection.RecvInterruptPage;
341 /* VMBUS_CHANNEL_MESSAGE* receiveMsg; */
346 if (recvInterruptPage)
348 for (dword = 0; dword < maxdword; dword++)
350 if (recvInterruptPage[dword])
352 for (bit = 0; bit < 32; bit++)
354 if (test_and_clear_bit(bit, (unsigned long *) &recvInterruptPage[dword]))
356 relid = (dword << 5) + bit;
358 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
360 if (relid == 0) /* special case - vmbus channel protocol msg */
362 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
367 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
368 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
369 VmbusProcessChannelEvent((void*)(unsigned long)relid);
387 Send a msg on the vmbus's message connection
390 int VmbusPostMessage(void *buffer, size_t bufferLen)
393 union hv_connection_id connId;
397 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
413 Send an event notification to the parent
416 int VmbusSetEvent(u32 childRelId)
422 /* Each u32 represents 32 channels */
423 set_bit(childRelId & 31,
424 (unsigned long *) gVmbusConnection.SendInterruptPage + (childRelId >> 5));
426 ret = HvSignalEvent();