connection.c 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *
 * Copyright (c) 2009, Microsoft Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 * Authors:
 *   Haiyang Zhang <haiyangz@microsoft.com>
 *   Hank Janssen  <hjanssen@microsoft.com>
 *
 */
23 24
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

25
#include <linux/kernel.h>
26 27
#include <linux/sched.h>
#include <linux/wait.h>
28
#include <linux/delay.h>
29
#include <linux/mm.h>
30
#include <linux/slab.h>
31
#include <linux/vmalloc.h>
32
#include <linux/hyperv.h>
33
#include <asm/hyperv.h>
34
#include "hyperv_vmbus.h"
35 36


37 38 39
struct vmbus_connection vmbus_connection = {
	.conn_state		= DISCONNECTED,
	.next_gpadl_handle	= ATOMIC_INIT(0xE1E10),
40 41
};

42
/*
43
 * vmbus_connect - Sends a connect request on the partition service connection
44
 */
45
int vmbus_connect(void)
46
{
47
	int ret = 0;
48
	int t;
49
	struct vmbus_channel_msginfo *msginfo = NULL;
50
	struct vmbus_channel_initiate_contact *msg;
51
	unsigned long flags;
52

53
	/* Initialize the vmbus connection */
54 55 56
	vmbus_connection.conn_state = CONNECTING;
	vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
	if (!vmbus_connection.work_queue) {
57
		ret = -ENOMEM;
58
		goto cleanup;
59
	}
60

61
	INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
62
	spin_lock_init(&vmbus_connection.channelmsg_lock);
63

64
	INIT_LIST_HEAD(&vmbus_connection.chn_list);
65
	spin_lock_init(&vmbus_connection.channel_lock);
66

67 68 69 70
	/*
	 * Setup the vmbus event connection for channel interrupt
	 * abstraction stuff
	 */
71 72
	vmbus_connection.int_page =
	(void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
73
	if (vmbus_connection.int_page == NULL) {
74
		ret = -ENOMEM;
75
		goto cleanup;
76 77
	}

78 79 80
	vmbus_connection.recv_int_page = vmbus_connection.int_page;
	vmbus_connection.send_int_page =
		(void *)((unsigned long)vmbus_connection.int_page +
81
			(PAGE_SIZE >> 1));
82

83 84 85
	/*
	 * Setup the monitor notification facility. The 1st page for
	 * parent->child and the 2nd page for child->parent
86
	 */
87 88
	vmbus_connection.monitor_pages =
	(void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
89
	if (vmbus_connection.monitor_pages == NULL) {
90
		ret = -ENOMEM;
91
		goto cleanup;
92 93
	}

94
	msginfo = kzalloc(sizeof(*msginfo) +
95 96
			  sizeof(struct vmbus_channel_initiate_contact),
			  GFP_KERNEL);
97
	if (msginfo == NULL) {
98
		ret = -ENOMEM;
99
		goto cleanup;
100 101
	}

102
	init_completion(&msginfo->waitevent);
103

104
	msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
105

106 107
	msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
	msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
108 109
	msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
	msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
110
	msg->monitor_page2 = virt_to_phys(
111
			(void *)((unsigned long)vmbus_connection.monitor_pages +
112
				 PAGE_SIZE));
113

114 115 116 117
	/*
	 * Add to list before we send the request since we may
	 * receive the response before returning from this routine
	 */
118 119
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
	list_add_tail(&msginfo->msglistentry,
120
		      &vmbus_connection.chn_msg_list);
121

122
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
123

124
	ret = vmbus_post_msg(msg,
125 126
			       sizeof(struct vmbus_channel_initiate_contact));
	if (ret != 0) {
127
		spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
128
		list_del(&msginfo->msglistentry);
129 130
		spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
					flags);
131
		goto cleanup;
132 133
	}

134
	/* Wait for the connection response */
135
	t =  wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
136
	if (t == 0) {
137 138 139 140 141 142
		spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
				flags);
		list_del(&msginfo->msglistentry);
		spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
					flags);
		ret = -ETIMEDOUT;
143
		goto cleanup;
144
	}
145

146
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
147
	list_del(&msginfo->msglistentry);
148
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
149

150
	/* Check if successful */
151
	if (msginfo->response.version_response.version_supported) {
152
		vmbus_connection.conn_state = CONNECTED;
153
	} else {
154 155 156
		pr_err("Unable to connect, "
			"Version %d not supported by Hyper-V\n",
			VMBUS_REVISION_NUMBER);
157
		ret = -ECONNREFUSED;
158
		goto cleanup;
159 160
	}

161
	kfree(msginfo);
162 163
	return 0;

164
cleanup:
165
	vmbus_connection.conn_state = DISCONNECTED;
166

167 168
	if (vmbus_connection.work_queue)
		destroy_workqueue(vmbus_connection.work_queue);
169

170
	if (vmbus_connection.int_page) {
171
		free_pages((unsigned long)vmbus_connection.int_page, 0);
172
		vmbus_connection.int_page = NULL;
173 174
	}

175
	if (vmbus_connection.monitor_pages) {
176
		free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
177
		vmbus_connection.monitor_pages = NULL;
178 179
	}

180
	kfree(msginfo);
181 182 183 184 185

	return ret;
}


186
/*
187 188
 * relid2channel - Get the channel object given its
 * child relative id (ie channel id)
189
 */
190
struct vmbus_channel *relid2channel(u32 relid)
191
{
192
	struct vmbus_channel *channel;
193
	struct vmbus_channel *found_channel  = NULL;
194
	unsigned long flags;
195

196
	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
197
	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
198 199
		if (channel->offermsg.child_relid == relid) {
			found_channel = channel;
200 201 202
			break;
		}
	}
203
	spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
204

205
	return found_channel;
206 207
}

208
/*
209
 * process_chn_event - Process a channel event notification
210
 */
211
static void process_chn_event(u32 relid)
212
{
213
	struct vmbus_channel *channel;
214
	unsigned long flags;
215

216 217 218 219
	/*
	 * Find the channel based on this relid and invokes the
	 * channel callback to process the event
	 */
220
	channel = relid2channel(relid);
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235
	if (!channel) {
		pr_err("channel not found for relid - %u\n", relid);
		return;
	}

	/*
	 * A channel once created is persistent even when there
	 * is no driver handling the device. An unloading driver
	 * sets the onchannel_callback to NULL under the
	 * protection of the channel inbound_lock. Thus, checking
	 * and invoking the driver specific callback takes care of
	 * orderly unloading of the driver.
	 */

236
	spin_lock_irqsave(&channel->inbound_lock, flags);
237
	if (channel->onchannel_callback != NULL)
238
		channel->onchannel_callback(channel->channel_callback_context);
239
	else
240
		pr_err("no channel callback for relid - %u\n", relid);
241

242
	spin_unlock_irqrestore(&channel->inbound_lock, flags);
243 244
}

245
/*
246
 * vmbus_on_event - Handler for events
247
 */
248
void vmbus_on_event(unsigned long data)
249
{
250 251
	u32 dword;
	u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
252
	int bit;
253
	u32 relid;
254
	u32 *recv_int_page = vmbus_connection.recv_int_page;
255

256
	/* Check events */
257 258 259 260 261 262
	if (!recv_int_page)
		return;
	for (dword = 0; dword < maxdword; dword++) {
		if (!recv_int_page[dword])
			continue;
		for (bit = 0; bit < 32; bit++) {
263 264
			if (sync_test_and_clear_bit(bit,
				(unsigned long *)&recv_int_page[dword])) {
265 266
				relid = (dword << 5) + bit;

267
				if (relid == 0)
268 269 270 271
					/*
					 * Special case - vmbus
					 * channel protocol msg
					 */
272
					continue;
273

274
				process_chn_event(relid);
275
			}
276
		}
277 278 279
	}
}

280
/*
281
 * vmbus_post_msg - Send a msg on the vmbus's message connection
282
 */
283
int vmbus_post_msg(void *buffer, size_t buflen)
284
{
285
	union hv_connection_id conn_id;
286 287
	int ret = 0;
	int retries = 0;
288

289 290
	conn_id.asu32 = 0;
	conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
291 292 293 294 295 296 297 298 299 300 301 302 303 304

	/*
	 * hv_post_message() can have transient failures because of
	 * insufficient resources. Retry the operation a couple of
	 * times before giving up.
	 */
	while (retries < 3) {
		ret =  hv_post_message(conn_id, 1, buffer, buflen);
		if (ret != HV_STATUS_INSUFFICIENT_BUFFERS)
			return ret;
		retries++;
		msleep(100);
	}
	return ret;
305 306
}

307
/*
308
 * vmbus_set_event - Send an event notification to the parent
309
 */
310
int vmbus_set_event(u32 child_relid)
311
{
312
	/* Each u32 represents 32 channels */
313
	sync_set_bit(child_relid & 31,
314
		(unsigned long *)vmbus_connection.send_int_page +
315
		(child_relid >> 5));
316

317
	return hv_signal_event();
318
}