]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/staging/hv/connection.c
b8dd8f9206b3bedd31b5ecb09b1d2d67a99c4c82
[lisovros/linux_canprio.git] / drivers / staging / hv / connection.c
1 /*
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 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include "osd.h"
28 #include "logging.h"
29 #include "vmbus_private.h"
30
31
32 struct VMBUS_CONNECTION gVmbusConnection = {
33         .ConnectState           = Disconnected,
34         .NextGpadlHandle        = ATOMIC_INIT(0xE1E10),
35 };
36
37 /*
38  * VmbusConnect - Sends a connect request on the partition service connection
39  */
40 int VmbusConnect(void)
41 {
42         int ret = 0;
43         struct vmbus_channel_msginfo *msgInfo = NULL;
44         struct vmbus_channel_initiate_contact *msg;
45         unsigned long flags;
46
47         /* Make sure we are not connecting or connected */
48         if (gVmbusConnection.ConnectState != Disconnected)
49                 return -1;
50
51         /* Initialize the vmbus connection */
52         gVmbusConnection.ConnectState = Connecting;
53         gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
54         if (!gVmbusConnection.WorkQueue) {
55                 ret = -1;
56                 goto Cleanup;
57         }
58
59         INIT_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
60         spin_lock_init(&gVmbusConnection.channelmsg_lock);
61
62         INIT_LIST_HEAD(&gVmbusConnection.ChannelList);
63         spin_lock_init(&gVmbusConnection.channel_lock);
64
65         /*
66          * Setup the vmbus event connection for channel interrupt
67          * abstraction stuff
68          */
69         gVmbusConnection.InterruptPage = osd_PageAlloc(1);
70         if (gVmbusConnection.InterruptPage == NULL) {
71                 ret = -1;
72                 goto Cleanup;
73         }
74
75         gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
76         gVmbusConnection.SendInterruptPage =
77                 (void *)((unsigned long)gVmbusConnection.InterruptPage +
78                         (PAGE_SIZE >> 1));
79
80         /*
81          * Setup the monitor notification facility. The 1st page for
82          * parent->child and the 2nd page for child->parent
83          */
84         gVmbusConnection.MonitorPages = osd_PageAlloc(2);
85         if (gVmbusConnection.MonitorPages == NULL) {
86                 ret = -1;
87                 goto Cleanup;
88         }
89
90         msgInfo = kzalloc(sizeof(*msgInfo) +
91                           sizeof(struct vmbus_channel_initiate_contact),
92                           GFP_KERNEL);
93         if (msgInfo == NULL) {
94                 ret = -ENOMEM;
95                 goto Cleanup;
96         }
97
98         msgInfo->WaitEvent = osd_WaitEventCreate();
99         if (!msgInfo->WaitEvent) {
100                 ret = -ENOMEM;
101                 goto Cleanup;
102         }
103
104         msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg;
105
106         msg->Header.MessageType = ChannelMessageInitiateContact;
107         msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
108         msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
109         msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
110         msg->MonitorPage2 = virt_to_phys(
111                         (void *)((unsigned long)gVmbusConnection.MonitorPages +
112                                  PAGE_SIZE));
113
114         /*
115          * Add to list before we send the request since we may
116          * receive the response before returning from this routine
117          */
118         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
119         list_add_tail(&msgInfo->MsgListEntry,
120                       &gVmbusConnection.ChannelMsgList);
121
122         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
123
124         DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
125                    "monitor1 pfn %llx,, monitor2 pfn %llx",
126                    msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
127
128         DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
129         ret = VmbusPostMessage(msg,
130                                sizeof(struct vmbus_channel_initiate_contact));
131         if (ret != 0) {
132                 list_del(&msgInfo->MsgListEntry);
133                 goto Cleanup;
134         }
135
136         /* Wait for the connection response */
137         osd_WaitEventWait(msgInfo->WaitEvent);
138
139         list_del(&msgInfo->MsgListEntry);
140
141         /* Check if successful */
142         if (msgInfo->Response.VersionResponse.VersionSupported) {
143                 DPRINT_INFO(VMBUS, "Vmbus connected!!");
144                 gVmbusConnection.ConnectState = Connected;
145
146         } else {
147                 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
148                            "current version (%d) not supported",
149                            VMBUS_REVISION_NUMBER);
150                 ret = -1;
151                 goto Cleanup;
152         }
153
154         kfree(msgInfo->WaitEvent);
155         kfree(msgInfo);
156         DPRINT_EXIT(VMBUS);
157
158         return 0;
159
160 Cleanup:
161         gVmbusConnection.ConnectState = Disconnected;
162
163         if (gVmbusConnection.WorkQueue)
164                 destroy_workqueue(gVmbusConnection.WorkQueue);
165
166         if (gVmbusConnection.InterruptPage) {
167                 osd_PageFree(gVmbusConnection.InterruptPage, 1);
168                 gVmbusConnection.InterruptPage = NULL;
169         }
170
171         if (gVmbusConnection.MonitorPages) {
172                 osd_PageFree(gVmbusConnection.MonitorPages, 2);
173                 gVmbusConnection.MonitorPages = NULL;
174         }
175
176         if (msgInfo) {
177                 kfree(msgInfo->WaitEvent);
178                 kfree(msgInfo);
179         }
180
181         DPRINT_EXIT(VMBUS);
182
183         return ret;
184 }
185
186 /*
187  * VmbusDisconnect - Sends a disconnect request on the partition service connection
188  */
189 int VmbusDisconnect(void)
190 {
191         int ret = 0;
192         struct vmbus_channel_message_header *msg;
193
194         /* Make sure we are connected */
195         if (gVmbusConnection.ConnectState != Connected)
196                 return -1;
197
198         msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
199         if (!msg)
200                 return -ENOMEM;
201
202         msg->MessageType = ChannelMessageUnload;
203
204         ret = VmbusPostMessage(msg,
205                                sizeof(struct vmbus_channel_message_header));
206         if (ret != 0)
207                 goto Cleanup;
208
209         osd_PageFree(gVmbusConnection.InterruptPage, 1);
210
211         /* TODO: iterate thru the msg list and free up */
212         destroy_workqueue(gVmbusConnection.WorkQueue);
213
214         gVmbusConnection.ConnectState = Disconnected;
215
216         DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
217
218 Cleanup:
219         kfree(msg);
220         DPRINT_EXIT(VMBUS);
221         return ret;
222 }
223
224 /*
225  * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
226  */
227 struct vmbus_channel *GetChannelFromRelId(u32 relId)
228 {
229         struct vmbus_channel *channel;
230         struct vmbus_channel *foundChannel  = NULL;
231         unsigned long flags;
232
233         spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
234         list_for_each_entry(channel, &gVmbusConnection.ChannelList, ListEntry) {
235                 if (channel->OfferMsg.ChildRelId == relId) {
236                         foundChannel = channel;
237                         break;
238                 }
239         }
240         spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
241
242         return foundChannel;
243 }
244
245 /*
246  * VmbusProcessChannelEvent - Process a channel event notification
247  */
248 static void VmbusProcessChannelEvent(void *context)
249 {
250         struct vmbus_channel *channel;
251         u32 relId = (u32)(unsigned long)context;
252
253         /* ASSERT(relId > 0); */
254
255         /*
256          * Find the channel based on this relid and invokes the
257          * channel callback to process the event
258          */
259         channel = GetChannelFromRelId(relId);
260
261         if (channel) {
262                 VmbusChannelOnChannelEvent(channel);
263                 /*
264                  * WorkQueueQueueWorkItem(channel->dataWorkQueue,
265                  *                        VmbusChannelOnChannelEvent,
266                  *                        (void*)channel);
267                  */
268         } else {
269                 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
270         }
271 }
272
273 /*
274  * VmbusOnEvents - Handler for events
275  */
276 void VmbusOnEvents(void)
277 {
278         int dword;
279         int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
280         int bit;
281         int relid;
282         u32 *recvInterruptPage = gVmbusConnection.RecvInterruptPage;
283
284         /* Check events */
285         if (recvInterruptPage) {
286                 for (dword = 0; dword < maxdword; dword++) {
287                         if (recvInterruptPage[dword]) {
288                                 for (bit = 0; bit < 32; bit++) {
289                                         if (test_and_clear_bit(bit, (unsigned long *)&recvInterruptPage[dword])) {
290                                                 relid = (dword << 5) + bit;
291                                                 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
292
293                                                 if (relid == 0) {
294                                                         /* special case - vmbus channel protocol msg */
295                                                         DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
296                                                         continue;
297                                                 } else {
298                                                         /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
299                                                         /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
300                                                         VmbusProcessChannelEvent((void *)(unsigned long)relid);
301                                                 }
302                                         }
303                                 }
304                         }
305                  }
306         }
307         DPRINT_EXIT(VMBUS);
308
309         return;
310 }
311
312 /*
313  * VmbusPostMessage - Send a msg on the vmbus's message connection
314  */
315 int VmbusPostMessage(void *buffer, size_t bufferLen)
316 {
317         union hv_connection_id connId;
318
319         connId.Asu32 = 0;
320         connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
321         return HvPostMessage(connId, 1, buffer, bufferLen);
322 }
323
324 /*
325  * VmbusSetEvent - Send an event notification to the parent
326  */
327 int VmbusSetEvent(u32 childRelId)
328 {
329         int ret = 0;
330
331         /* Each u32 represents 32 channels */
332         set_bit(childRelId & 31,
333                 (unsigned long *)gVmbusConnection.SendInterruptPage +
334                 (childRelId >> 5));
335
336         ret = HvSignalEvent();
337
338         DPRINT_EXIT(VMBUS);
339
340         return ret;
341 }