blob: 43c2e685501554ae6d12664ea03cf12fbba3de09 [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 */
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070023#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/vmalloc.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070026#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070027#include "logging.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070028#include "VmbusPrivate.h"
29
Hank Janssen3e7ee492009-07-13 16:02:34 -070030
Bill Pemberton662e66b2009-07-27 16:47:42 -040031struct VMBUS_CONNECTION gVmbusConnection = {
Hank Janssen3e7ee492009-07-13 16:02:34 -070032 .ConnectState = Disconnected,
Bill Pembertonf4888412009-07-29 17:00:12 -040033 .NextGpadlHandle = ATOMIC_INIT(0xE1E10),
Hank Janssen3e7ee492009-07-13 16:02:34 -070034};
35
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070036/**
37 * VmbusConnect - Sends a connect request on the partition service connection
38 */
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -070039int VmbusConnect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070040{
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070041 int ret = 0;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -070042 struct vmbus_channel_msginfo *msgInfo = NULL;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -070043 struct vmbus_channel_initiate_contact *msg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070044 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -070045
46 DPRINT_ENTER(VMBUS);
47
Bill Pemberton454f18a2009-07-27 16:47:24 -040048 /* Make sure we are not connecting or connected */
Hank Janssen3e7ee492009-07-13 16:02:34 -070049 if (gVmbusConnection.ConnectState != Disconnected)
50 return -1;
51
Bill Pemberton454f18a2009-07-27 16:47:24 -040052 /* Initialize the vmbus connection */
Hank Janssen3e7ee492009-07-13 16:02:34 -070053 gVmbusConnection.ConnectState = Connecting;
Bill Pembertonde65a382009-07-29 17:00:09 -040054 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070055 if (!gVmbusConnection.WorkQueue) {
Bill Pembertonde65a382009-07-29 17:00:09 -040056 ret = -1;
57 goto Cleanup;
58 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070059
Bill Pemberton53af5452009-09-11 21:46:44 -040060 INIT_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070061 spin_lock_init(&gVmbusConnection.channelmsg_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070062
Bill Pemberton53af5452009-09-11 21:46:44 -040063 INIT_LIST_HEAD(&gVmbusConnection.ChannelList);
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -070064 spin_lock_init(&gVmbusConnection.channel_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070065
Bill Pemberton454f18a2009-07-27 16:47:24 -040066 /*
67 * Setup the vmbus event connection for channel interrupt
68 * abstraction stuff
69 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -070070 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070071 if (gVmbusConnection.InterruptPage == NULL) {
Hank Janssen3e7ee492009-07-13 16:02:34 -070072 ret = -1;
73 goto Cleanup;
74 }
75
76 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070077 gVmbusConnection.SendInterruptPage =
78 (void *)((unsigned long)gVmbusConnection.InterruptPage +
79 (PAGE_SIZE >> 1));
Hank Janssen3e7ee492009-07-13 16:02:34 -070080
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070081 /*
82 * Setup the monitor notification facility. The 1st page for
83 * parent->child and the 2nd page for child->parent
Bill Pemberton454f18a2009-07-27 16:47:24 -040084 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -070085 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070086 if (gVmbusConnection.MonitorPages == NULL) {
Hank Janssen3e7ee492009-07-13 16:02:34 -070087 ret = -1;
88 goto Cleanup;
89 }
90
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070091 msgInfo = kzalloc(sizeof(*msgInfo) +
92 sizeof(struct vmbus_channel_initiate_contact),
93 GFP_KERNEL);
94 if (msgInfo == NULL) {
Hank Janssen3e7ee492009-07-13 16:02:34 -070095 ret = -1;
96 goto Cleanup;
97 }
98
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -070099 msgInfo->WaitEvent = osd_WaitEventCreate();
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700100 msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101
102 msg->Header.MessageType = ChannelMessageInitiateContact;
103 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
Greg Kroah-Hartmanfa56d362009-07-29 15:39:27 -0700104 msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
105 msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700106 msg->MonitorPage2 = virt_to_phys(
107 (void *)((unsigned long)gVmbusConnection.MonitorPages +
108 PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109
Bill Pemberton454f18a2009-07-27 16:47:24 -0400110 /*
111 * Add to list before we send the request since we may
112 * receive the response before returning from this routine
113 */
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700114 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
Bill Pemberton53af5452009-09-11 21:46:44 -0400115 list_add_tail(&msgInfo->MsgListEntry,
116 &gVmbusConnection.ChannelMsgList);
117
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700118 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700119
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700120 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
121 "monitor1 pfn %llx,, monitor2 pfn %llx",
122 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700123
124 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700125 ret = VmbusPostMessage(msg,
126 sizeof(struct vmbus_channel_initiate_contact));
127 if (ret != 0) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400128 list_del(&msgInfo->MsgListEntry);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700129 goto Cleanup;
130 }
131
Bill Pemberton454f18a2009-07-27 16:47:24 -0400132 /* Wait for the connection response */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700133 osd_WaitEventWait(msgInfo->WaitEvent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700134
Bill Pemberton53af5452009-09-11 21:46:44 -0400135 list_del(&msgInfo->MsgListEntry);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700136
Bill Pemberton454f18a2009-07-27 16:47:24 -0400137 /* Check if successful */
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700138 if (msgInfo->Response.VersionResponse.VersionSupported) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700139 DPRINT_INFO(VMBUS, "Vmbus connected!!");
140 gVmbusConnection.ConnectState = Connected;
141
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700142 } else {
143 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
144 "current version (%d) not supported",
145 VMBUS_REVISION_NUMBER);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700146 ret = -1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700147 goto Cleanup;
148 }
149
Bill Pemberton420beac2009-07-29 17:00:10 -0400150 kfree(msgInfo->WaitEvent);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700151 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700152 DPRINT_EXIT(VMBUS);
153
154 return 0;
155
156Cleanup:
Hank Janssen3e7ee492009-07-13 16:02:34 -0700157 gVmbusConnection.ConnectState = Disconnected;
158
Bill Pembertonde65a382009-07-29 17:00:09 -0400159 if (gVmbusConnection.WorkQueue)
160 destroy_workqueue(gVmbusConnection.WorkQueue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700161
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700162 if (gVmbusConnection.InterruptPage) {
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700163 osd_PageFree(gVmbusConnection.InterruptPage, 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700164 gVmbusConnection.InterruptPage = NULL;
165 }
166
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700167 if (gVmbusConnection.MonitorPages) {
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700168 osd_PageFree(gVmbusConnection.MonitorPages, 2);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700169 gVmbusConnection.MonitorPages = NULL;
170 }
171
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700172 if (msgInfo) {
173 kfree(msgInfo->WaitEvent);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700174 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700175 }
176
177 DPRINT_EXIT(VMBUS);
178
179 return ret;
180}
181
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700182/**
183 * VmbusDisconnect - Sends a disconnect request on the partition service connection
184 */
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700185int VmbusDisconnect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700186{
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700187 int ret = 0;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700188 struct vmbus_channel_message_header *msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700189
190 DPRINT_ENTER(VMBUS);
191
Bill Pemberton454f18a2009-07-27 16:47:24 -0400192 /* Make sure we are connected */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193 if (gVmbusConnection.ConnectState != Connected)
194 return -1;
195
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700196 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700197
198 msg->MessageType = ChannelMessageUnload;
199
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700200 ret = VmbusPostMessage(msg,
201 sizeof(struct vmbus_channel_message_header));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700203 goto Cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700204
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700205 osd_PageFree(gVmbusConnection.InterruptPage, 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700206
Bill Pemberton454f18a2009-07-27 16:47:24 -0400207 /* TODO: iterate thru the msg list and free up */
Bill Pembertonde65a382009-07-29 17:00:09 -0400208 destroy_workqueue(gVmbusConnection.WorkQueue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209
210 gVmbusConnection.ConnectState = Disconnected;
211
212 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
213
214Cleanup:
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700215 kfree(msg);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700216 DPRINT_EXIT(VMBUS);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700217 return ret;
218}
219
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700220/**
221 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
222 */
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700223struct vmbus_channel *GetChannelFromRelId(u32 relId)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700225 struct vmbus_channel *channel;
226 struct vmbus_channel *foundChannel = NULL;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700227 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700228
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700229 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
Bill Pemberton53af5452009-09-11 21:46:44 -0400230 list_for_each_entry(channel, &gVmbusConnection.ChannelList, ListEntry) {
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700231 if (channel->OfferMsg.ChildRelId == relId) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700232 foundChannel = channel;
233 break;
234 }
235 }
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700236 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700237
238 return foundChannel;
239}
240
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700241/**
242 * VmbusProcessChannelEvent - Process a channel event notification
243 */
244static void VmbusProcessChannelEvent(void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700246 struct vmbus_channel *channel;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700247 u32 relId = (u32)(unsigned long)context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700248
249 ASSERT(relId > 0);
250
Bill Pemberton454f18a2009-07-27 16:47:24 -0400251 /*
252 * Find the channel based on this relid and invokes the
253 * channel callback to process the event
254 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255 channel = GetChannelFromRelId(relId);
256
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700257 if (channel) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258 VmbusChannelOnChannelEvent(channel);
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700259 /*
260 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
261 * VmbusChannelOnChannelEvent,
262 * (void*)channel);
263 */
264 } else {
265 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266 }
267}
268
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700269/**
270 * VmbusOnEvents - Handler for events
271 */
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700272void VmbusOnEvents(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273{
274 int dword;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700275 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
276 int bit;
277 int relid;
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700278 u32 *recvInterruptPage = gVmbusConnection.RecvInterruptPage;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700279
280 DPRINT_ENTER(VMBUS);
281
Bill Pemberton454f18a2009-07-27 16:47:24 -0400282 /* Check events */
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700283 if (recvInterruptPage) {
284 for (dword = 0; dword < maxdword; dword++) {
285 if (recvInterruptPage[dword]) {
286 for (bit = 0; bit < 32; bit++) {
287 if (test_and_clear_bit(bit, (unsigned long *)&recvInterruptPage[dword])) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700288 relid = (dword << 5) + bit;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700289 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
290
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700291 if (relid == 0) {
292 /* special case - vmbus channel protocol msg */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700293 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700294 continue;
295 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400296 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
297 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700298 VmbusProcessChannelEvent((void *)(unsigned long)relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700299 }
300 }
301 }
302 }
303 }
304 }
305 DPRINT_EXIT(VMBUS);
306
307 return;
308}
309
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700310/**
311 * VmbusPostMessage - Send a msg on the vmbus's message connection
312 */
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700313int VmbusPostMessage(void *buffer, size_t bufferLen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700314{
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700315 union hv_connection_id connId;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700316
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700317 connId.Asu32 = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700318 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700319 return HvPostMessage(connId, 1, buffer, bufferLen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700320}
321
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700322/**
323 * VmbusSetEvent - Send an event notification to the parent
324 */
Greg Kroah-Hartmanf346fdc2009-08-17 17:23:00 -0700325int VmbusSetEvent(u32 childRelId)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700326{
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700327 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700328
329 DPRINT_ENTER(VMBUS);
330
Bill Pemberton454f18a2009-07-27 16:47:24 -0400331 /* Each u32 represents 32 channels */
Bill Pemberton7c369f42009-07-29 17:00:11 -0400332 set_bit(childRelId & 31,
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700333 (unsigned long *)gVmbusConnection.SendInterruptPage +
334 (childRelId >> 5));
Bill Pemberton7c369f42009-07-29 17:00:11 -0400335
Hank Janssen3e7ee492009-07-13 16:02:34 -0700336 ret = HvSignalEvent();
337
338 DPRINT_EXIT(VMBUS);
339
340 return ret;
341}