blob: e6b40392e08d511b560f53080160b4d06ad20db2 [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 */
Hank Janssen0a466182011-03-29 13:58:47 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070025#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080026#include <linux/sched.h>
27#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070028#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070030#include <linux/vmalloc.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070031
32#include "hyperv.h"
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070033#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070034
Hank Janssen3e7ee492009-07-13 16:02:34 -070035
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080036struct vmbus_connection vmbus_connection = {
37 .conn_state = DISCONNECTED,
38 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
Hank Janssen3e7ee492009-07-13 16:02:34 -070039};
40
Hank Janssen3e189512010-03-04 22:11:00 +000041/*
Haiyang Zhangc6977672011-01-26 12:12:08 -080042 * vmbus_connect - Sends a connect request on the partition service connection
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070043 */
Haiyang Zhangc6977672011-01-26 12:12:08 -080044int vmbus_connect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070045{
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070046 int ret = 0;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -070047 int t;
Haiyang Zhang15b2f642011-01-26 12:12:07 -080048 struct vmbus_channel_msginfo *msginfo = NULL;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -070049 struct vmbus_channel_initiate_contact *msg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070050 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -070051
Bill Pemberton454f18a2009-07-27 16:47:24 -040052 /* Make sure we are not connecting or connected */
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080053 if (vmbus_connection.conn_state != DISCONNECTED)
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -070054 return -EISCONN;
Hank Janssen3e7ee492009-07-13 16:02:34 -070055
Bill Pemberton454f18a2009-07-27 16:47:24 -040056 /* Initialize the vmbus connection */
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080057 vmbus_connection.conn_state = CONNECTING;
58 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
59 if (!vmbus_connection.work_queue) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -070060 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -070061 goto cleanup;
Bill Pembertonde65a382009-07-29 17:00:09 -040062 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070063
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080064 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -080065 spin_lock_init(&vmbus_connection.channelmsg_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070066
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080067 INIT_LIST_HEAD(&vmbus_connection.chn_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -080068 spin_lock_init(&vmbus_connection.channel_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070069
Bill Pemberton454f18a2009-07-27 16:47:24 -040070 /*
71 * Setup the vmbus event connection for channel interrupt
72 * abstraction stuff
73 */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -080074 vmbus_connection.int_page =
75 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080076 if (vmbus_connection.int_page == NULL) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -070077 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -070078 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -070079 }
80
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080081 vmbus_connection.recv_int_page = vmbus_connection.int_page;
82 vmbus_connection.send_int_page =
83 (void *)((unsigned long)vmbus_connection.int_page +
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070084 (PAGE_SIZE >> 1));
Hank Janssen3e7ee492009-07-13 16:02:34 -070085
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070086 /*
87 * Setup the monitor notification facility. The 1st page for
88 * parent->child and the 2nd page for child->parent
Bill Pemberton454f18a2009-07-27 16:47:24 -040089 */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -080090 vmbus_connection.monitor_pages =
91 (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080092 if (vmbus_connection.monitor_pages == NULL) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -070093 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -070094 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -070095 }
96
Haiyang Zhang15b2f642011-01-26 12:12:07 -080097 msginfo = kzalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -070098 sizeof(struct vmbus_channel_initiate_contact),
99 GFP_KERNEL);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800100 if (msginfo == NULL) {
Bill Pemberton8cad0af2010-05-05 15:27:32 -0400101 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700102 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700103 }
104
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700105 init_completion(&msginfo->waitevent);
Bill Pemberton80d11b22010-05-05 15:27:33 -0400106
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800107 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700108
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800109 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
110 msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800111 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
112 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800113 msg->monitor_page2 = virt_to_phys(
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800114 (void *)((unsigned long)vmbus_connection.monitor_pages +
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700115 PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700116
Bill Pemberton454f18a2009-07-27 16:47:24 -0400117 /*
118 * Add to list before we send the request since we may
119 * receive the response before returning from this routine
120 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800121 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
122 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800123 &vmbus_connection.chn_msg_list);
Bill Pemberton53af5452009-09-11 21:46:44 -0400124
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800125 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126
Haiyang Zhangc6977672011-01-26 12:12:08 -0800127 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700128 sizeof(struct vmbus_channel_initiate_contact));
129 if (ret != 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800130 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800131 list_del(&msginfo->msglistentry);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800132 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
133 flags);
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700134 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700135 }
136
Bill Pemberton454f18a2009-07-27 16:47:24 -0400137 /* Wait for the connection response */
K. Y. Srinivasan2dfde962011-06-16 13:16:34 -0700138 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700139 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800140 spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
141 flags);
142 list_del(&msginfo->msglistentry);
143 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
144 flags);
145 ret = -ETIMEDOUT;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700146 goto cleanup;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800147 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700148
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800149 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800150 list_del(&msginfo->msglistentry);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800151 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700152
Bill Pemberton454f18a2009-07-27 16:47:24 -0400153 /* Check if successful */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800154 if (msginfo->response.version_response.version_supported) {
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800155 vmbus_connection.conn_state = CONNECTED;
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700156 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700157 pr_err("Unable to connect, "
158 "Version %d not supported by Hyper-V\n",
159 VMBUS_REVISION_NUMBER);
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -0700160 ret = -ECONNREFUSED;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700161 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700162 }
163
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800164 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700165 return 0;
166
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700167cleanup:
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800168 vmbus_connection.conn_state = DISCONNECTED;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700169
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800170 if (vmbus_connection.work_queue)
171 destroy_workqueue(vmbus_connection.work_queue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700172
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800173 if (vmbus_connection.int_page) {
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800174 free_pages((unsigned long)vmbus_connection.int_page, 0);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800175 vmbus_connection.int_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700176 }
177
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800178 if (vmbus_connection.monitor_pages) {
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800179 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800180 vmbus_connection.monitor_pages = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700181 }
182
Ilia Mirkindd9b15d2011-03-13 00:29:00 -0500183 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
Hank Janssen3e7ee492009-07-13 16:02:34 -0700185 return ret;
186}
187
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188
Hank Janssen3e189512010-03-04 22:11:00 +0000189/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800190 * relid2channel - Get the channel object given its
191 * child relative id (ie channel id)
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700192 */
Haiyang Zhangc6977672011-01-26 12:12:08 -0800193struct vmbus_channel *relid2channel(u32 relid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700194{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700195 struct vmbus_channel *channel;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800196 struct vmbus_channel *found_channel = NULL;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700197 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700198
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800199 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800200 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800201 if (channel->offermsg.child_relid == relid) {
202 found_channel = channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700203 break;
204 }
205 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800206 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700207
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800208 return found_channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209}
210
Hank Janssen3e189512010-03-04 22:11:00 +0000211/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800212 * process_chn_event - Process a channel event notification
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700213 */
Olaf Hering35436482011-04-16 18:50:40 +0200214static void process_chn_event(u32 relid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700215{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700216 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700217
Bill Pemberton972b9522010-05-05 15:27:54 -0400218 /* ASSERT(relId > 0); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700219
Bill Pemberton454f18a2009-07-27 16:47:24 -0400220 /*
221 * Find the channel based on this relid and invokes the
222 * channel callback to process the event
223 */
Haiyang Zhangc6977672011-01-26 12:12:08 -0800224 channel = relid2channel(relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700225
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700226 if (channel) {
K. Y. Srinivasandf452fa2011-06-06 15:49:47 -0700227 channel->onchannel_callback(channel->channel_callback_context);
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700228 } else {
Olaf Hering35436482011-04-16 18:50:40 +0200229 pr_err("channel not found for relid - %u\n", relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700230 }
231}
232
Hank Janssen3e189512010-03-04 22:11:00 +0000233/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800234 * vmbus_on_event - Handler for events
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700235 */
K. Y. Srinivasan6de3d6a2011-03-10 14:05:16 -0800236void vmbus_on_event(unsigned long data)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700237{
Olaf Hering35436482011-04-16 18:50:40 +0200238 u32 dword;
239 u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700240 int bit;
Olaf Hering35436482011-04-16 18:50:40 +0200241 u32 relid;
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800242 u32 *recv_int_page = vmbus_connection.recv_int_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700243
Bill Pemberton454f18a2009-07-27 16:47:24 -0400244 /* Check events */
Olaf Hering242b45a2011-04-16 18:50:39 +0200245 if (!recv_int_page)
246 return;
247 for (dword = 0; dword < maxdword; dword++) {
248 if (!recv_int_page[dword])
249 continue;
250 for (bit = 0; bit < 32; bit++) {
251 if (sync_test_and_clear_bit(bit, (unsigned long *)&recv_int_page[dword])) {
252 relid = (dword << 5) + bit;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253
Olaf Hering242b45a2011-04-16 18:50:39 +0200254 if (relid == 0) {
K. Y. Srinivasan6d81d332011-05-10 07:55:45 -0700255 /*
256 * Special case - vmbus
257 * channel protocol msg
258 */
Olaf Hering242b45a2011-04-16 18:50:39 +0200259 continue;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700260 }
Olaf Hering35436482011-04-16 18:50:40 +0200261 process_chn_event(relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262 }
Olaf Hering242b45a2011-04-16 18:50:39 +0200263 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700264 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265}
266
Hank Janssen3e189512010-03-04 22:11:00 +0000267/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800268 * vmbus_post_msg - Send a msg on the vmbus's message connection
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700269 */
Haiyang Zhangc6977672011-01-26 12:12:08 -0800270int vmbus_post_msg(void *buffer, size_t buflen)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700271{
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800272 union hv_connection_id conn_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800274 conn_id.asu32 = 0;
275 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
276 return hv_post_message(conn_id, 1, buffer, buflen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277}
278
Hank Janssen3e189512010-03-04 22:11:00 +0000279/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800280 * vmbus_set_event - Send an event notification to the parent
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700281 */
Haiyang Zhangc6977672011-01-26 12:12:08 -0800282int vmbus_set_event(u32 child_relid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700283{
Bill Pemberton454f18a2009-07-27 16:47:24 -0400284 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +0100285 sync_set_bit(child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800286 (unsigned long *)vmbus_connection.send_int_page +
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800287 (child_relid >> 5));
Bill Pemberton7c369f42009-07-29 17:00:11 -0400288
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800289 return hv_signal_event();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700290}