blob: d4d355e6b7c221d1b01e737028959ec7b213afe5 [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-Hartmana0086dc2009-08-17 17:22:08 -070025#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/vmalloc.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070028#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070029#include "logging.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070030#include "VmbusPrivate.h"
31
Bill Pemberton454f18a2009-07-27 16:47:24 -040032/* Globals */
Hank Janssen3e7ee492009-07-13 16:02:34 -070033
34
Bill Pemberton662e66b2009-07-27 16:47:42 -040035struct VMBUS_CONNECTION gVmbusConnection = {
Hank Janssen3e7ee492009-07-13 16:02:34 -070036 .ConnectState = Disconnected,
Bill Pembertonf4888412009-07-29 17:00:12 -040037 .NextGpadlHandle = ATOMIC_INIT(0xE1E10),
Hank Janssen3e7ee492009-07-13 16:02:34 -070038};
39
40
41/*++
42
43Name:
44 VmbusConnect()
45
46Description:
47 Sends a connect request on the partition service connection
48
49--*/
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -070050int VmbusConnect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070051{
52 int ret=0;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -070053 struct vmbus_channel_msginfo *msgInfo = NULL;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -070054 struct vmbus_channel_initiate_contact *msg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070055 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -070056
57 DPRINT_ENTER(VMBUS);
58
Bill Pemberton454f18a2009-07-27 16:47:24 -040059 /* Make sure we are not connecting or connected */
Hank Janssen3e7ee492009-07-13 16:02:34 -070060 if (gVmbusConnection.ConnectState != Disconnected)
61 return -1;
62
Bill Pemberton454f18a2009-07-27 16:47:24 -040063 /* Initialize the vmbus connection */
Hank Janssen3e7ee492009-07-13 16:02:34 -070064 gVmbusConnection.ConnectState = Connecting;
Bill Pembertonde65a382009-07-29 17:00:09 -040065 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
66 if (!gVmbusConnection.WorkQueue)
67 {
68 ret = -1;
69 goto Cleanup;
70 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070071
72 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070073 spin_lock_init(&gVmbusConnection.channelmsg_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070074
75 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -070076 spin_lock_init(&gVmbusConnection.channel_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070077
Bill Pemberton454f18a2009-07-27 16:47:24 -040078 /*
79 * Setup the vmbus event connection for channel interrupt
80 * abstraction stuff
81 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -070082 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
Hank Janssen3e7ee492009-07-13 16:02:34 -070083 if (gVmbusConnection.InterruptPage == NULL)
84 {
85 ret = -1;
86 goto Cleanup;
87 }
88
89 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -070090 gVmbusConnection.SendInterruptPage = (void*)((unsigned long)gVmbusConnection.InterruptPage + (PAGE_SIZE >> 1));
Hank Janssen3e7ee492009-07-13 16:02:34 -070091
Bill Pemberton454f18a2009-07-27 16:47:24 -040092 /* Setup the monitor
93 * notification facility. The 1st page for parent->child and
94 * the 2nd page for child->parent
95 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -070096 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
Hank Janssen3e7ee492009-07-13 16:02:34 -070097 if (gVmbusConnection.MonitorPages == NULL)
98 {
99 ret = -1;
100 goto Cleanup;
101 }
102
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700103 msgInfo = kzalloc(sizeof(*msgInfo) + sizeof(struct vmbus_channel_initiate_contact), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700104 if (msgInfo == NULL)
105 {
106 ret = -1;
107 goto Cleanup;
108 }
109
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700110 msgInfo->WaitEvent = osd_WaitEventCreate();
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700111 msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700112
113 msg->Header.MessageType = ChannelMessageInitiateContact;
114 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
Greg Kroah-Hartmanfa56d362009-07-29 15:39:27 -0700115 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));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700118
Bill Pemberton454f18a2009-07-27 16:47:24 -0400119 /*
120 * Add to list before we send the request since we may
121 * receive the response before returning from this routine
122 */
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700123 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700124 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700125 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126
127 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, monitor1 pfn %llx,, monitor2 pfn %llx",
128 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
129
130 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
131
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700132 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_initiate_contact));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700133 if (ret != 0)
134 {
135 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
136 goto Cleanup;
137 }
138
Bill Pemberton454f18a2009-07-27 16:47:24 -0400139 /* Wait for the connection response */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700140 osd_WaitEventWait(msgInfo->WaitEvent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700141
142 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
143
Bill Pemberton454f18a2009-07-27 16:47:24 -0400144 /* Check if successful */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700145 if (msgInfo->Response.VersionResponse.VersionSupported)
146 {
147 DPRINT_INFO(VMBUS, "Vmbus connected!!");
148 gVmbusConnection.ConnectState = Connected;
149
150 }
151 else
152 {
153 DPRINT_ERR(VMBUS, "Vmbus connection failed!!...current version (%d) not supported", VMBUS_REVISION_NUMBER);
154 ret = -1;
155
156 goto Cleanup;
157 }
158
159
Bill Pemberton420beac2009-07-29 17:00:10 -0400160 kfree(msgInfo->WaitEvent);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700161 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700162 DPRINT_EXIT(VMBUS);
163
164 return 0;
165
166Cleanup:
167
168 gVmbusConnection.ConnectState = Disconnected;
169
Bill Pembertonde65a382009-07-29 17:00:09 -0400170 if (gVmbusConnection.WorkQueue)
171 destroy_workqueue(gVmbusConnection.WorkQueue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700172
173 if (gVmbusConnection.InterruptPage)
174 {
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700175 osd_PageFree(gVmbusConnection.InterruptPage, 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700176 gVmbusConnection.InterruptPage = NULL;
177 }
178
179 if (gVmbusConnection.MonitorPages)
180 {
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700181 osd_PageFree(gVmbusConnection.MonitorPages, 2);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700182 gVmbusConnection.MonitorPages = NULL;
183 }
184
185 if (msgInfo)
186 {
187 if (msgInfo->WaitEvent)
Bill Pemberton420beac2009-07-29 17:00:10 -0400188 kfree(msgInfo->WaitEvent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700189
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700190 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700191 }
192
193 DPRINT_EXIT(VMBUS);
194
195 return ret;
196}
197
198
199/*++
200
201Name:
202 VmbusDisconnect()
203
204Description:
205 Sends a disconnect request on the partition service connection
206
207--*/
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700208int VmbusDisconnect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209{
210 int ret=0;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700211 struct vmbus_channel_message_header *msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212
213 DPRINT_ENTER(VMBUS);
214
Bill Pemberton454f18a2009-07-27 16:47:24 -0400215 /* Make sure we are connected */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700216 if (gVmbusConnection.ConnectState != Connected)
217 return -1;
218
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700219 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700220
221 msg->MessageType = ChannelMessageUnload;
222
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700223 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_message_header));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224
225 if (ret != 0)
226 {
227 goto Cleanup;
228 }
229
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700230 osd_PageFree(gVmbusConnection.InterruptPage, 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700231
Bill Pemberton454f18a2009-07-27 16:47:24 -0400232 /* TODO: iterate thru the msg list and free up */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700233
Bill Pembertonde65a382009-07-29 17:00:09 -0400234 destroy_workqueue(gVmbusConnection.WorkQueue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700235
236 gVmbusConnection.ConnectState = Disconnected;
237
238 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
239
240Cleanup:
241 if (msg)
242 {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700243 kfree(msg);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700244 }
245
246 DPRINT_EXIT(VMBUS);
247
248 return ret;
249}
250
251
252/*++
253
254Name:
255 GetChannelFromRelId()
256
257Description:
258 Get the channel object given its child relative id (ie channel id)
259
260--*/
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700261struct vmbus_channel *GetChannelFromRelId(u32 relId)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700263 struct vmbus_channel *channel;
264 struct vmbus_channel *foundChannel = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265 LIST_ENTRY* anchor;
266 LIST_ENTRY* curr;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700267 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700268
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700269 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700270 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
271 {
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700272 channel = CONTAINING_RECORD(curr, struct vmbus_channel, ListEntry);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273
274 if (channel->OfferMsg.ChildRelId == relId)
275 {
276 foundChannel = channel;
277 break;
278 }
279 }
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700280 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700281
282 return foundChannel;
283}
284
285
286
287/*++
288
289Name:
290 VmbusProcessChannelEvent()
291
292Description:
293 Process a channel event notification
294
295--*/
296static void
297VmbusProcessChannelEvent(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700298 void * context
Hank Janssen3e7ee492009-07-13 16:02:34 -0700299 )
300{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700301 struct vmbus_channel *channel;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700302 u32 relId = (u32)(unsigned long)context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303
304 ASSERT(relId > 0);
305
Bill Pemberton454f18a2009-07-27 16:47:24 -0400306 /*
307 * Find the channel based on this relid and invokes the
308 * channel callback to process the event
309 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700310 channel = GetChannelFromRelId(relId);
311
312 if (channel)
313 {
314 VmbusChannelOnChannelEvent(channel);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400315 /* WorkQueueQueueWorkItem(channel->dataWorkQueue, VmbusChannelOnChannelEvent, (void*)channel); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700316 }
317 else
318 {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400319 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700320 }
321}
322
323
324/*++
325
326Name:
327 VmbusOnEvents()
328
329Description:
330 Handler for events
331
332--*/
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700333void VmbusOnEvents(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700334{
335 int dword;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400336 /* int maxdword = PAGE_SIZE >> 3; // receive size is 1/2 page and divide that by 4 bytes */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700337 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
338 int bit;
339 int relid;
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700340 u32* recvInterruptPage = gVmbusConnection.RecvInterruptPage;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400341 /* VMBUS_CHANNEL_MESSAGE* receiveMsg; */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700342
343 DPRINT_ENTER(VMBUS);
344
Bill Pemberton454f18a2009-07-27 16:47:24 -0400345 /* Check events */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700346 if (recvInterruptPage)
347 {
348 for (dword = 0; dword < maxdword; dword++)
349 {
350 if (recvInterruptPage[dword])
351 {
352 for (bit = 0; bit < 32; bit++)
353 {
Bill Pemberton7c369f42009-07-29 17:00:11 -0400354 if (test_and_clear_bit(bit, (unsigned long *) &recvInterruptPage[dword]))
Hank Janssen3e7ee492009-07-13 16:02:34 -0700355 {
356 relid = (dword << 5) + bit;
357
358 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
359
Bill Pemberton454f18a2009-07-27 16:47:24 -0400360 if (relid == 0) /* special case - vmbus channel protocol msg */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700361 {
362 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
363
364 continue; }
365 else
366 {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400367 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
368 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700369 VmbusProcessChannelEvent((void*)(unsigned long)relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370 }
371 }
372 }
373 }
374 }
375 }
376 DPRINT_EXIT(VMBUS);
377
378 return;
379}
380
381/*++
382
383Name:
384 VmbusPostMessage()
385
386Description:
387 Send a msg on the vmbus's message connection
388
389--*/
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700390int VmbusPostMessage(void *buffer, size_t bufferLen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391{
392 int ret=0;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700393 union hv_connection_id connId;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700394
395
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700396 connId.Asu32 =0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700397 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
398 ret = HvPostMessage(
399 connId,
400 1,
401 buffer,
402 bufferLen);
403
404 return ret;
405}
406
407/*++
408
409Name:
410 VmbusSetEvent()
411
412Description:
413 Send an event notification to the parent
414
415--*/
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700416int VmbusSetEvent(u32 childRelId)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417{
418 int ret=0;
419
420 DPRINT_ENTER(VMBUS);
421
Bill Pemberton454f18a2009-07-27 16:47:24 -0400422 /* Each u32 represents 32 channels */
Bill Pemberton7c369f42009-07-29 17:00:11 -0400423 set_bit(childRelId & 31,
424 (unsigned long *) gVmbusConnection.SendInterruptPage + (childRelId >> 5));
425
Hank Janssen3e7ee492009-07-13 16:02:34 -0700426 ret = HvSignalEvent();
427
428 DPRINT_EXIT(VMBUS);
429
430 return ret;
431}
432
Bill Pemberton454f18a2009-07-27 16:47:24 -0400433/* EOF */