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>
23 #include <linux/kernel.h>
25 #include <linux/vmalloc.h>
28 #include "VmbusPrivate.h"
31 struct VMBUS_CONNECTION gVmbusConnection = {
32 .ConnectState = Disconnected,
33 .NextGpadlHandle = ATOMIC_INIT(0xE1E10),
37 * VmbusConnect - Sends a connect request on the partition service connection
39 int VmbusConnect(void)
42 struct vmbus_channel_msginfo *msgInfo = NULL;
43 struct vmbus_channel_initiate_contact *msg;
48 /* Make sure we are not connecting or connected */
49 if (gVmbusConnection.ConnectState != Disconnected)
52 /* Initialize the vmbus connection */
53 gVmbusConnection.ConnectState = Connecting;
54 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
55 if (!gVmbusConnection.WorkQueue) {
60 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
61 spin_lock_init(&gVmbusConnection.channelmsg_lock);
63 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
64 spin_lock_init(&gVmbusConnection.channel_lock);
67 * Setup the vmbus event connection for channel interrupt
70 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
71 if (gVmbusConnection.InterruptPage == NULL) {
76 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
77 gVmbusConnection.SendInterruptPage =
78 (void *)((unsigned long)gVmbusConnection.InterruptPage +
82 * Setup the monitor notification facility. The 1st page for
83 * parent->child and the 2nd page for child->parent
85 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
86 if (gVmbusConnection.MonitorPages == NULL) {
91 msgInfo = kzalloc(sizeof(*msgInfo) +
92 sizeof(struct vmbus_channel_initiate_contact),
94 if (msgInfo == NULL) {
99 msgInfo->WaitEvent = osd_WaitEventCreate();
100 msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg;
102 msg->Header.MessageType = ChannelMessageInitiateContact;
103 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
104 msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
105 msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
106 msg->MonitorPage2 = virt_to_phys(
107 (void *)((unsigned long)gVmbusConnection.MonitorPages +
111 * Add to list before we send the request since we may
112 * receive the response before returning from this routine
114 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
115 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList,
116 &msgInfo->MsgListEntry);
117 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
119 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
120 "monitor1 pfn %llx,, monitor2 pfn %llx",
121 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
123 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
124 ret = VmbusPostMessage(msg,
125 sizeof(struct vmbus_channel_initiate_contact));
127 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
131 /* Wait for the connection response */
132 osd_WaitEventWait(msgInfo->WaitEvent);
134 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
136 /* Check if successful */
137 if (msgInfo->Response.VersionResponse.VersionSupported) {
138 DPRINT_INFO(VMBUS, "Vmbus connected!!");
139 gVmbusConnection.ConnectState = Connected;
142 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
143 "current version (%d) not supported",
144 VMBUS_REVISION_NUMBER);
149 kfree(msgInfo->WaitEvent);
156 gVmbusConnection.ConnectState = Disconnected;
158 if (gVmbusConnection.WorkQueue)
159 destroy_workqueue(gVmbusConnection.WorkQueue);
161 if (gVmbusConnection.InterruptPage) {
162 osd_PageFree(gVmbusConnection.InterruptPage, 1);
163 gVmbusConnection.InterruptPage = NULL;
166 if (gVmbusConnection.MonitorPages) {
167 osd_PageFree(gVmbusConnection.MonitorPages, 2);
168 gVmbusConnection.MonitorPages = NULL;
172 kfree(msgInfo->WaitEvent);
182 * VmbusDisconnect - Sends a disconnect request on the partition service connection
184 int VmbusDisconnect(void)
187 struct vmbus_channel_message_header *msg;
191 /* Make sure we are connected */
192 if (gVmbusConnection.ConnectState != Connected)
195 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
197 msg->MessageType = ChannelMessageUnload;
199 ret = VmbusPostMessage(msg,
200 sizeof(struct vmbus_channel_message_header));
204 osd_PageFree(gVmbusConnection.InterruptPage, 1);
206 /* TODO: iterate thru the msg list and free up */
207 destroy_workqueue(gVmbusConnection.WorkQueue);
209 gVmbusConnection.ConnectState = Disconnected;
211 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
220 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
222 struct vmbus_channel *GetChannelFromRelId(u32 relId)
224 struct vmbus_channel *channel;
225 struct vmbus_channel *foundChannel = NULL;
230 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
231 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList) {
232 channel = CONTAINING_RECORD(curr, struct vmbus_channel,
235 if (channel->OfferMsg.ChildRelId == relId) {
236 foundChannel = channel;
240 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
246 * VmbusProcessChannelEvent - Process a channel event notification
248 static void VmbusProcessChannelEvent(void *context)
250 struct vmbus_channel *channel;
251 u32 relId = (u32)(unsigned long)context;
256 * Find the channel based on this relid and invokes the
257 * channel callback to process the event
259 channel = GetChannelFromRelId(relId);
262 VmbusChannelOnChannelEvent(channel);
264 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
265 * VmbusChannelOnChannelEvent,
269 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
274 * VmbusOnEvents - Handler for events
276 void VmbusOnEvents(void)
279 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
282 u32 *recvInterruptPage = gVmbusConnection.RecvInterruptPage;
287 if (recvInterruptPage) {
288 for (dword = 0; dword < maxdword; dword++) {
289 if (recvInterruptPage[dword]) {
290 for (bit = 0; bit < 32; bit++) {
291 if (test_and_clear_bit(bit, (unsigned long *)&recvInterruptPage[dword])) {
292 relid = (dword << 5) + bit;
293 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
296 /* special case - vmbus channel protocol msg */
297 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
300 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
301 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
302 VmbusProcessChannelEvent((void *)(unsigned long)relid);
315 * VmbusPostMessage - Send a msg on the vmbus's message connection
317 int VmbusPostMessage(void *buffer, size_t bufferLen)
319 union hv_connection_id connId;
322 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
323 return HvPostMessage(connId, 1, buffer, bufferLen);
327 * VmbusSetEvent - Send an event notification to the parent
329 int VmbusSetEvent(u32 childRelId)
335 /* Each u32 represents 32 channels */
336 set_bit(childRelId & 31,
337 (unsigned long *)gVmbusConnection.SendInterruptPage +
340 ret = HvSignalEvent();