blob: 17364343495346692dca85adef17dc2d4747bb16 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
2 *
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
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.
8 *
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
12 * more details.
13 *
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.
17 *
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 *
22 */
23
24
Greg Kroah-Hartman09d50ff2009-07-13 17:09:34 -070025#include "include/logging.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070026
27#include "VmbusPrivate.h"
28
Bill Pemberton454f18a2009-07-27 16:47:24 -040029/* Globals */
Hank Janssen3e7ee492009-07-13 16:02:34 -070030
31
Bill Pemberton662e66b2009-07-27 16:47:42 -040032struct VMBUS_CONNECTION gVmbusConnection = {
Hank Janssen3e7ee492009-07-13 16:02:34 -070033 .ConnectState = Disconnected,
Bill Pembertonf4888412009-07-29 17:00:12 -040034 .NextGpadlHandle = ATOMIC_INIT(0xE1E10),
Hank Janssen3e7ee492009-07-13 16:02:34 -070035};
36
37
38/*++
39
40Name:
41 VmbusConnect()
42
43Description:
44 Sends a connect request on the partition service connection
45
46--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -070047static int
Greg Kroah-Hartman12772902009-07-29 09:05:33 -070048VmbusConnect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070049{
50 int ret=0;
51 VMBUS_CHANNEL_MSGINFO *msgInfo=NULL;
52 VMBUS_CHANNEL_INITIATE_CONTACT *msg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070053 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -070054
55 DPRINT_ENTER(VMBUS);
56
Bill Pemberton454f18a2009-07-27 16:47:24 -040057 /* Make sure we are not connecting or connected */
Hank Janssen3e7ee492009-07-13 16:02:34 -070058 if (gVmbusConnection.ConnectState != Disconnected)
59 return -1;
60
Bill Pemberton454f18a2009-07-27 16:47:24 -040061 /* Initialize the vmbus connection */
Hank Janssen3e7ee492009-07-13 16:02:34 -070062 gVmbusConnection.ConnectState = Connecting;
Bill Pembertonde65a382009-07-29 17:00:09 -040063 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
64 if (!gVmbusConnection.WorkQueue)
65 {
66 ret = -1;
67 goto Cleanup;
68 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070069
70 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070071 spin_lock_init(&gVmbusConnection.channelmsg_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070072
73 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -070074 spin_lock_init(&gVmbusConnection.channel_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070075
Bill Pemberton454f18a2009-07-27 16:47:24 -040076 /*
77 * Setup the vmbus event connection for channel interrupt
78 * abstraction stuff
79 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -070080 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
Hank Janssen3e7ee492009-07-13 16:02:34 -070081 if (gVmbusConnection.InterruptPage == NULL)
82 {
83 ret = -1;
84 goto Cleanup;
85 }
86
87 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -070088 gVmbusConnection.SendInterruptPage = (void*)((unsigned long)gVmbusConnection.InterruptPage + (PAGE_SIZE >> 1));
Hank Janssen3e7ee492009-07-13 16:02:34 -070089
Bill Pemberton454f18a2009-07-27 16:47:24 -040090 /* Setup the monitor
91 * notification facility. The 1st page for parent->child and
92 * the 2nd page for child->parent
93 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -070094 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
Hank Janssen3e7ee492009-07-13 16:02:34 -070095 if (gVmbusConnection.MonitorPages == NULL)
96 {
97 ret = -1;
98 goto Cleanup;
99 }
100
Greg Kroah-Hartmane276a3a2009-07-15 12:47:43 -0700101 msgInfo = kzalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_INITIATE_CONTACT), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700102 if (msgInfo == NULL)
103 {
104 ret = -1;
105 goto Cleanup;
106 }
107
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700108 msgInfo->WaitEvent = osd_WaitEventCreate();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109 msg = (VMBUS_CHANNEL_INITIATE_CONTACT*)msgInfo->Msg;
110
111 msg->Header.MessageType = ChannelMessageInitiateContact;
112 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
Greg Kroah-Hartmanfa56d362009-07-29 15:39:27 -0700113 msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
114 msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
115 msg->MonitorPage2 = virt_to_phys((void *)((unsigned long)gVmbusConnection.MonitorPages + PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700116
Bill Pemberton454f18a2009-07-27 16:47:24 -0400117 /*
118 * Add to list before we send the request since we may
119 * receive the response before returning from this routine
120 */
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700121 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700123 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700124
125 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, monitor1 pfn %llx,, monitor2 pfn %llx",
126 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
127
128 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
129
130 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_INITIATE_CONTACT));
131 if (ret != 0)
132 {
133 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
134 goto Cleanup;
135 }
136
Bill Pemberton454f18a2009-07-27 16:47:24 -0400137 /* Wait for the connection response */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700138 osd_WaitEventWait(msgInfo->WaitEvent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700139
140 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
141
Bill Pemberton454f18a2009-07-27 16:47:24 -0400142 /* Check if successful */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700143 if (msgInfo->Response.VersionResponse.VersionSupported)
144 {
145 DPRINT_INFO(VMBUS, "Vmbus connected!!");
146 gVmbusConnection.ConnectState = Connected;
147
148 }
149 else
150 {
151 DPRINT_ERR(VMBUS, "Vmbus connection failed!!...current version (%d) not supported", VMBUS_REVISION_NUMBER);
152 ret = -1;
153
154 goto Cleanup;
155 }
156
157
Bill Pemberton420beac2009-07-29 17:00:10 -0400158 kfree(msgInfo->WaitEvent);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700159 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160 DPRINT_EXIT(VMBUS);
161
162 return 0;
163
164Cleanup:
165
166 gVmbusConnection.ConnectState = Disconnected;
167
Bill Pembertonde65a382009-07-29 17:00:09 -0400168 if (gVmbusConnection.WorkQueue)
169 destroy_workqueue(gVmbusConnection.WorkQueue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700170
171 if (gVmbusConnection.InterruptPage)
172 {
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700173 osd_PageFree(gVmbusConnection.InterruptPage, 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700174 gVmbusConnection.InterruptPage = NULL;
175 }
176
177 if (gVmbusConnection.MonitorPages)
178 {
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700179 osd_PageFree(gVmbusConnection.MonitorPages, 2);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700180 gVmbusConnection.MonitorPages = NULL;
181 }
182
183 if (msgInfo)
184 {
185 if (msgInfo->WaitEvent)
Bill Pemberton420beac2009-07-29 17:00:10 -0400186 kfree(msgInfo->WaitEvent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700187
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700188 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700189 }
190
191 DPRINT_EXIT(VMBUS);
192
193 return ret;
194}
195
196
197/*++
198
199Name:
200 VmbusDisconnect()
201
202Description:
203 Sends a disconnect request on the partition service connection
204
205--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700206static int
Hank Janssen3e7ee492009-07-13 16:02:34 -0700207VmbusDisconnect(
Greg Kroah-Hartmane20f6832009-07-14 15:07:21 -0700208 void
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209 )
210{
211 int ret=0;
212 VMBUS_CHANNEL_UNLOAD *msg;
213
214 DPRINT_ENTER(VMBUS);
215
Bill Pemberton454f18a2009-07-27 16:47:24 -0400216 /* Make sure we are connected */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700217 if (gVmbusConnection.ConnectState != Connected)
218 return -1;
219
Greg Kroah-Hartmane276a3a2009-07-15 12:47:43 -0700220 msg = kzalloc(sizeof(VMBUS_CHANNEL_UNLOAD), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221
222 msg->MessageType = ChannelMessageUnload;
223
224 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_UNLOAD));
225
226 if (ret != 0)
227 {
228 goto Cleanup;
229 }
230
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700231 osd_PageFree(gVmbusConnection.InterruptPage, 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700232
Bill Pemberton454f18a2009-07-27 16:47:24 -0400233 /* TODO: iterate thru the msg list and free up */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700234
Bill Pembertonde65a382009-07-29 17:00:09 -0400235 destroy_workqueue(gVmbusConnection.WorkQueue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236
237 gVmbusConnection.ConnectState = Disconnected;
238
239 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
240
241Cleanup:
242 if (msg)
243 {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700244 kfree(msg);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245 }
246
247 DPRINT_EXIT(VMBUS);
248
249 return ret;
250}
251
252
253/*++
254
255Name:
256 GetChannelFromRelId()
257
258Description:
259 Get the channel object given its child relative id (ie channel id)
260
261--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700262static VMBUS_CHANNEL*
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263GetChannelFromRelId(
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700264 u32 relId
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265 )
266{
267 VMBUS_CHANNEL* channel;
268 VMBUS_CHANNEL* foundChannel=NULL;
269 LIST_ENTRY* anchor;
270 LIST_ENTRY* curr;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700271 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700272
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700273 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700274 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
275 {
276 channel = CONTAINING_RECORD(curr, VMBUS_CHANNEL, ListEntry);
277
278 if (channel->OfferMsg.ChildRelId == relId)
279 {
280 foundChannel = channel;
281 break;
282 }
283 }
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700284 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700285
286 return foundChannel;
287}
288
289
290
291/*++
292
293Name:
294 VmbusProcessChannelEvent()
295
296Description:
297 Process a channel event notification
298
299--*/
300static void
301VmbusProcessChannelEvent(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700302 void * context
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303 )
304{
305 VMBUS_CHANNEL* channel;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700306 u32 relId = (u32)(unsigned long)context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
308 ASSERT(relId > 0);
309
Bill Pemberton454f18a2009-07-27 16:47:24 -0400310 /*
311 * Find the channel based on this relid and invokes the
312 * channel callback to process the event
313 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700314 channel = GetChannelFromRelId(relId);
315
316 if (channel)
317 {
318 VmbusChannelOnChannelEvent(channel);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400319 /* WorkQueueQueueWorkItem(channel->dataWorkQueue, VmbusChannelOnChannelEvent, (void*)channel); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700320 }
321 else
322 {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400323 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700324 }
325}
326
327
328/*++
329
330Name:
331 VmbusOnEvents()
332
333Description:
334 Handler for events
335
336--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700337static void
Hank Janssen3e7ee492009-07-13 16:02:34 -0700338VmbusOnEvents(
Greg Kroah-Hartmane20f6832009-07-14 15:07:21 -0700339 void
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340 )
341{
342 int dword;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400343 /* int maxdword = PAGE_SIZE >> 3; // receive size is 1/2 page and divide that by 4 bytes */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
345 int bit;
346 int relid;
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700347 u32* recvInterruptPage = gVmbusConnection.RecvInterruptPage;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400348 /* VMBUS_CHANNEL_MESSAGE* receiveMsg; */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700349
350 DPRINT_ENTER(VMBUS);
351
Bill Pemberton454f18a2009-07-27 16:47:24 -0400352 /* Check events */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700353 if (recvInterruptPage)
354 {
355 for (dword = 0; dword < maxdword; dword++)
356 {
357 if (recvInterruptPage[dword])
358 {
359 for (bit = 0; bit < 32; bit++)
360 {
Bill Pemberton7c369f42009-07-29 17:00:11 -0400361 if (test_and_clear_bit(bit, (unsigned long *) &recvInterruptPage[dword]))
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362 {
363 relid = (dword << 5) + bit;
364
365 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
366
Bill Pemberton454f18a2009-07-27 16:47:24 -0400367 if (relid == 0) /* special case - vmbus channel protocol msg */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700368 {
369 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
370
371 continue; }
372 else
373 {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400374 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
375 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700376 VmbusProcessChannelEvent((void*)(unsigned long)relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700377 }
378 }
379 }
380 }
381 }
382 }
383 DPRINT_EXIT(VMBUS);
384
385 return;
386}
387
388/*++
389
390Name:
391 VmbusPostMessage()
392
393Description:
394 Send a msg on the vmbus's message connection
395
396--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700397static int
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398VmbusPostMessage(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700399 void * buffer,
Greg Kroah-Hartman45635d92009-07-14 15:14:20 -0700400 size_t bufferLen
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401 )
402{
403 int ret=0;
404 HV_CONNECTION_ID connId;
405
406
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700407 connId.Asu32 =0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700408 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
409 ret = HvPostMessage(
410 connId,
411 1,
412 buffer,
413 bufferLen);
414
415 return ret;
416}
417
418/*++
419
420Name:
421 VmbusSetEvent()
422
423Description:
424 Send an event notification to the parent
425
426--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700427static int
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700428VmbusSetEvent(u32 childRelId)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700429{
430 int ret=0;
431
432 DPRINT_ENTER(VMBUS);
433
Bill Pemberton454f18a2009-07-27 16:47:24 -0400434 /* Each u32 represents 32 channels */
Bill Pemberton7c369f42009-07-29 17:00:11 -0400435 set_bit(childRelId & 31,
436 (unsigned long *) gVmbusConnection.SendInterruptPage + (childRelId >> 5));
437
Hank Janssen3e7ee492009-07-13 16:02:34 -0700438 ret = HvSignalEvent();
439
440 DPRINT_EXIT(VMBUS);
441
442 return ret;
443}
444
Bill Pemberton454f18a2009-07-27 16:47:24 -0400445/* EOF */