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