staging: hv: Convert camel cased struct fields in hv_api.h to lower cases
[deliverable/linux.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 46
454f18a9 47 /* Make sure we are not connecting or connected */
3e7ee490
HJ
48 if (gVmbusConnection.ConnectState != Disconnected)
49 return -1;
50
454f18a9 51 /* Initialize the vmbus connection */
3e7ee490 52 gVmbusConnection.ConnectState = Connecting;
de65a384 53 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
fd8b85ea 54 if (!gVmbusConnection.WorkQueue) {
de65a384
BP
55 ret = -1;
56 goto Cleanup;
57 }
3e7ee490 58
53af545b 59 INIT_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
dd0813b6 60 spin_lock_init(&gVmbusConnection.channelmsg_lock);
3e7ee490 61
53af545b 62 INIT_LIST_HEAD(&gVmbusConnection.ChannelList);
0f5e44ca 63 spin_lock_init(&gVmbusConnection.channel_lock);
3e7ee490 64
454f18a9
BP
65 /*
66 * Setup the vmbus event connection for channel interrupt
67 * abstraction stuff
68 */
bfc30aae 69 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
fd8b85ea 70 if (gVmbusConnection.InterruptPage == NULL) {
3e7ee490
HJ
71 ret = -1;
72 goto Cleanup;
73 }
74
75 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
fd8b85ea
GKH
76 gVmbusConnection.SendInterruptPage =
77 (void *)((unsigned long)gVmbusConnection.InterruptPage +
78 (PAGE_SIZE >> 1));
3e7ee490 79
fd8b85ea
GKH
80 /*
81 * Setup the monitor notification facility. The 1st page for
82 * parent->child and the 2nd page for child->parent
454f18a9 83 */
bfc30aae 84 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
fd8b85ea 85 if (gVmbusConnection.MonitorPages == NULL) {
3e7ee490
HJ
86 ret = -1;
87 goto Cleanup;
88 }
89
fd8b85ea
GKH
90 msgInfo = kzalloc(sizeof(*msgInfo) +
91 sizeof(struct vmbus_channel_initiate_contact),
92 GFP_KERNEL);
93 if (msgInfo == NULL) {
8cad0af9 94 ret = -ENOMEM;
3e7ee490
HJ
95 goto Cleanup;
96 }
97
c50f7fb2
HZ
98 msgInfo->waitevent = osd_WaitEventCreate();
99 if (!msgInfo->waitevent) {
80d11b2a
BP
100 ret = -ENOMEM;
101 goto Cleanup;
102 }
103
c50f7fb2 104 msg = (struct vmbus_channel_initiate_contact *)msgInfo->msg;
3e7ee490 105
c50f7fb2
HZ
106 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
107 msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
108 msg->interrupt_page = virt_to_phys(gVmbusConnection.InterruptPage);
109 msg->monitor_page1 = virt_to_phys(gVmbusConnection.MonitorPages);
110 msg->monitor_page2 = virt_to_phys(
fd8b85ea
GKH
111 (void *)((unsigned long)gVmbusConnection.MonitorPages +
112 PAGE_SIZE));
3e7ee490 113
454f18a9
BP
114 /*
115 * Add to list before we send the request since we may
116 * receive the response before returning from this routine
117 */
dd0813b6 118 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
c50f7fb2 119 list_add_tail(&msgInfo->msglistentry,
53af545b
BP
120 &gVmbusConnection.ChannelMsgList);
121
dd0813b6 122 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 123
fd8b85ea
GKH
124 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
125 "monitor1 pfn %llx,, monitor2 pfn %llx",
c50f7fb2 126 msg->interrupt_page, msg->monitor_page1, msg->monitor_page2);
3e7ee490
HJ
127
128 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
fd8b85ea
GKH
129 ret = VmbusPostMessage(msg,
130 sizeof(struct vmbus_channel_initiate_contact));
131 if (ret != 0) {
c50f7fb2 132 list_del(&msgInfo->msglistentry);
3e7ee490
HJ
133 goto Cleanup;
134 }
135
454f18a9 136 /* Wait for the connection response */
c50f7fb2 137 osd_WaitEventWait(msgInfo->waitevent);
3e7ee490 138
c50f7fb2 139 list_del(&msgInfo->msglistentry);
3e7ee490 140
454f18a9 141 /* Check if successful */
c50f7fb2 142 if (msgInfo->response.version_response.version_supported) {
3e7ee490
HJ
143 DPRINT_INFO(VMBUS, "Vmbus connected!!");
144 gVmbusConnection.ConnectState = Connected;
145
fd8b85ea
GKH
146 } else {
147 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
148 "current version (%d) not supported",
149 VMBUS_REVISION_NUMBER);
3e7ee490 150 ret = -1;
3e7ee490
HJ
151 goto Cleanup;
152 }
153
c50f7fb2 154 kfree(msgInfo->waitevent);
8c69f52a 155 kfree(msgInfo);
3e7ee490
HJ
156 return 0;
157
158Cleanup:
3e7ee490
HJ
159 gVmbusConnection.ConnectState = Disconnected;
160
de65a384
BP
161 if (gVmbusConnection.WorkQueue)
162 destroy_workqueue(gVmbusConnection.WorkQueue);
3e7ee490 163
fd8b85ea 164 if (gVmbusConnection.InterruptPage) {
bfc30aae 165 osd_PageFree(gVmbusConnection.InterruptPage, 1);
3e7ee490
HJ
166 gVmbusConnection.InterruptPage = NULL;
167 }
168
fd8b85ea 169 if (gVmbusConnection.MonitorPages) {
bfc30aae 170 osd_PageFree(gVmbusConnection.MonitorPages, 2);
3e7ee490
HJ
171 gVmbusConnection.MonitorPages = NULL;
172 }
173
fd8b85ea 174 if (msgInfo) {
c50f7fb2 175 kfree(msgInfo->waitevent);
8c69f52a 176 kfree(msgInfo);
3e7ee490
HJ
177 }
178
3e7ee490
HJ
179 return ret;
180}
181
3e189519 182/*
fd8b85ea
GKH
183 * VmbusDisconnect - Sends a disconnect request on the partition service connection
184 */
f346fdc2 185int VmbusDisconnect(void)
3e7ee490 186{
fd8b85ea 187 int ret = 0;
82250213 188 struct vmbus_channel_message_header *msg;
3e7ee490 189
454f18a9 190 /* Make sure we are connected */
3e7ee490
HJ
191 if (gVmbusConnection.ConnectState != Connected)
192 return -1;
193
82250213 194 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
8cad0af9
BP
195 if (!msg)
196 return -ENOMEM;
3e7ee490 197
c50f7fb2 198 msg->msgtype = CHANNELMSG_UNLOAD;
3e7ee490 199
fd8b85ea
GKH
200 ret = VmbusPostMessage(msg,
201 sizeof(struct vmbus_channel_message_header));
3e7ee490 202 if (ret != 0)
3e7ee490 203 goto Cleanup;
3e7ee490 204
bfc30aae 205 osd_PageFree(gVmbusConnection.InterruptPage, 1);
3e7ee490 206
454f18a9 207 /* TODO: iterate thru the msg list and free up */
de65a384 208 destroy_workqueue(gVmbusConnection.WorkQueue);
3e7ee490
HJ
209
210 gVmbusConnection.ConnectState = Disconnected;
211
212 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
213
214Cleanup:
fd8b85ea 215 kfree(msg);
3e7ee490
HJ
216 return ret;
217}
218
3e189519 219/*
fd8b85ea
GKH
220 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
221 */
aded7165 222struct vmbus_channel *GetChannelFromRelId(u32 relId)
3e7ee490 223{
aded7165
GKH
224 struct vmbus_channel *channel;
225 struct vmbus_channel *foundChannel = NULL;
0f5e44ca 226 unsigned long flags;
3e7ee490 227
0f5e44ca 228 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
c50f7fb2
HZ
229 list_for_each_entry(channel, &gVmbusConnection.ChannelList, listentry) {
230 if (channel->offermsg.child_relid == relId) {
3e7ee490
HJ
231 foundChannel = channel;
232 break;
233 }
234 }
0f5e44ca 235 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
3e7ee490
HJ
236
237 return foundChannel;
238}
239
3e189519 240/*
fd8b85ea
GKH
241 * VmbusProcessChannelEvent - Process a channel event notification
242 */
243static void VmbusProcessChannelEvent(void *context)
3e7ee490 244{
aded7165 245 struct vmbus_channel *channel;
c4b0bc94 246 u32 relId = (u32)(unsigned long)context;
3e7ee490 247
972b9529 248 /* ASSERT(relId > 0); */
3e7ee490 249
454f18a9
BP
250 /*
251 * Find the channel based on this relid and invokes the
252 * channel callback to process the event
253 */
3e7ee490
HJ
254 channel = GetChannelFromRelId(relId);
255
fd8b85ea 256 if (channel) {
fff41b2e 257 vmbus_onchannel_event(channel);
fd8b85ea
GKH
258 /*
259 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
fff41b2e 260 * vmbus_onchannel_event,
0686e4f4 261 * (void*)channel);
fd8b85ea
GKH
262 */
263 } else {
264 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
3e7ee490
HJ
265 }
266}
267
3e189519 268/*
fd8b85ea
GKH
269 * VmbusOnEvents - Handler for events
270 */
f346fdc2 271void VmbusOnEvents(void)
3e7ee490
HJ
272{
273 int dword;
3e7ee490
HJ
274 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
275 int bit;
276 int relid;
fd8b85ea 277 u32 *recvInterruptPage = gVmbusConnection.RecvInterruptPage;
3e7ee490 278
454f18a9 279 /* Check events */
fd8b85ea
GKH
280 if (recvInterruptPage) {
281 for (dword = 0; dword < maxdword; dword++) {
282 if (recvInterruptPage[dword]) {
283 for (bit = 0; bit < 32; bit++) {
284 if (test_and_clear_bit(bit, (unsigned long *)&recvInterruptPage[dword])) {
3e7ee490 285 relid = (dword << 5) + bit;
3e7ee490
HJ
286 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
287
fd8b85ea
GKH
288 if (relid == 0) {
289 /* special case - vmbus channel protocol msg */
3e7ee490 290 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
fd8b85ea
GKH
291 continue;
292 } else {
454f18a9
BP
293 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
294 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
fd8b85ea 295 VmbusProcessChannelEvent((void *)(unsigned long)relid);
3e7ee490
HJ
296 }
297 }
298 }
299 }
300 }
301 }
3e7ee490
HJ
302 return;
303}
304
3e189519 305/*
fd8b85ea
GKH
306 * VmbusPostMessage - Send a msg on the vmbus's message connection
307 */
f346fdc2 308int VmbusPostMessage(void *buffer, size_t bufferLen)
3e7ee490 309{
eacb1b4d 310 union hv_connection_id connId;
3e7ee490 311
f6feebe0
HZ
312 connId.asu32 = 0;
313 connId.u.id = VMBUS_MESSAGE_CONNECTION_ID;
fd8b85ea 314 return HvPostMessage(connId, 1, buffer, bufferLen);
3e7ee490
HJ
315}
316
3e189519 317/*
fd8b85ea
GKH
318 * VmbusSetEvent - Send an event notification to the parent
319 */
f346fdc2 320int VmbusSetEvent(u32 childRelId)
3e7ee490 321{
454f18a9 322 /* Each u32 represents 32 channels */
7c369f40 323 set_bit(childRelId & 31,
fd8b85ea
GKH
324 (unsigned long *)gVmbusConnection.SendInterruptPage +
325 (childRelId >> 5));
7c369f40 326
83c720ea 327 return HvSignalEvent();
3e7ee490 328}
This page took 0.161298 seconds and 5 git commands to generate.