drm/i915: Turn off hsync and vsync on ADPA when disabling crt
[linux-2.6-block.git] / drivers / 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 */
0a46618d
HJ
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
a0086dc5 25#include <linux/kernel.h>
0c3b7b2f
S
26#include <linux/sched.h>
27#include <linux/wait.h>
5289d3d1 28#include <linux/delay.h>
a0086dc5 29#include <linux/mm.h>
5a0e3ad6 30#include <linux/slab.h>
a0086dc5 31#include <linux/vmalloc.h>
46a97191 32#include <linux/hyperv.h>
407dd164 33#include <asm/hyperv.h>
0f2a6619 34#include "hyperv_vmbus.h"
3e7ee490 35
3e7ee490 36
da9fcb72
HZ
37struct vmbus_connection vmbus_connection = {
38 .conn_state = DISCONNECTED,
39 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
3e7ee490
HJ
40};
41
3e189519 42/*
c6977677 43 * vmbus_connect - Sends a connect request on the partition service connection
fd8b85ea 44 */
c6977677 45int vmbus_connect(void)
3e7ee490 46{
fd8b85ea 47 int ret = 0;
9568a193 48 int t;
15b2f647 49 struct vmbus_channel_msginfo *msginfo = NULL;
82250213 50 struct vmbus_channel_initiate_contact *msg;
dd0813b6 51 unsigned long flags;
3e7ee490 52
454f18a9 53 /* Initialize the vmbus connection */
da9fcb72
HZ
54 vmbus_connection.conn_state = CONNECTING;
55 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
56 if (!vmbus_connection.work_queue) {
3a7546d9 57 ret = -ENOMEM;
b0043863 58 goto cleanup;
de65a384 59 }
3e7ee490 60
da9fcb72 61 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
15b2f647 62 spin_lock_init(&vmbus_connection.channelmsg_lock);
3e7ee490 63
da9fcb72 64 INIT_LIST_HEAD(&vmbus_connection.chn_list);
15b2f647 65 spin_lock_init(&vmbus_connection.channel_lock);
3e7ee490 66
454f18a9
BP
67 /*
68 * Setup the vmbus event connection for channel interrupt
69 * abstraction stuff
70 */
df3493e0
S
71 vmbus_connection.int_page =
72 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
da9fcb72 73 if (vmbus_connection.int_page == NULL) {
3a7546d9 74 ret = -ENOMEM;
b0043863 75 goto cleanup;
3e7ee490
HJ
76 }
77
da9fcb72
HZ
78 vmbus_connection.recv_int_page = vmbus_connection.int_page;
79 vmbus_connection.send_int_page =
80 (void *)((unsigned long)vmbus_connection.int_page +
fd8b85ea 81 (PAGE_SIZE >> 1));
3e7ee490 82
fd8b85ea
GKH
83 /*
84 * Setup the monitor notification facility. The 1st page for
85 * parent->child and the 2nd page for child->parent
454f18a9 86 */
df3493e0
S
87 vmbus_connection.monitor_pages =
88 (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
da9fcb72 89 if (vmbus_connection.monitor_pages == NULL) {
3a7546d9 90 ret = -ENOMEM;
b0043863 91 goto cleanup;
3e7ee490
HJ
92 }
93
15b2f647 94 msginfo = kzalloc(sizeof(*msginfo) +
fd8b85ea
GKH
95 sizeof(struct vmbus_channel_initiate_contact),
96 GFP_KERNEL);
15b2f647 97 if (msginfo == NULL) {
8cad0af9 98 ret = -ENOMEM;
b0043863 99 goto cleanup;
3e7ee490
HJ
100 }
101
9568a193 102 init_completion(&msginfo->waitevent);
80d11b2a 103
15b2f647 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;
da9fcb72
HZ
108 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
109 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
c50f7fb2 110 msg->monitor_page2 = virt_to_phys(
da9fcb72 111 (void *)((unsigned long)vmbus_connection.monitor_pages +
fd8b85ea 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 */
15b2f647
HZ
118 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
119 list_add_tail(&msginfo->msglistentry,
da9fcb72 120 &vmbus_connection.chn_msg_list);
53af545b 121
15b2f647 122 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 123
c6977677 124 ret = vmbus_post_msg(msg,
fd8b85ea
GKH
125 sizeof(struct vmbus_channel_initiate_contact));
126 if (ret != 0) {
0c3b7b2f 127 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
15b2f647 128 list_del(&msginfo->msglistentry);
0c3b7b2f
S
129 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
130 flags);
b0043863 131 goto cleanup;
3e7ee490
HJ
132 }
133
454f18a9 134 /* Wait for the connection response */
2dfde964 135 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
9568a193 136 if (t == 0) {
0c3b7b2f
S
137 spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
138 flags);
139 list_del(&msginfo->msglistentry);
140 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
141 flags);
142 ret = -ETIMEDOUT;
b0043863 143 goto cleanup;
0c3b7b2f 144 }
3e7ee490 145
0c3b7b2f 146 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
15b2f647 147 list_del(&msginfo->msglistentry);
0c3b7b2f 148 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 149
454f18a9 150 /* Check if successful */
15b2f647 151 if (msginfo->response.version_response.version_supported) {
da9fcb72 152 vmbus_connection.conn_state = CONNECTED;
fd8b85ea 153 } else {
0a46618d
HJ
154 pr_err("Unable to connect, "
155 "Version %d not supported by Hyper-V\n",
156 VMBUS_REVISION_NUMBER);
3a7546d9 157 ret = -ECONNREFUSED;
b0043863 158 goto cleanup;
3e7ee490
HJ
159 }
160
15b2f647 161 kfree(msginfo);
3e7ee490
HJ
162 return 0;
163
b0043863 164cleanup:
da9fcb72 165 vmbus_connection.conn_state = DISCONNECTED;
3e7ee490 166
da9fcb72
HZ
167 if (vmbus_connection.work_queue)
168 destroy_workqueue(vmbus_connection.work_queue);
3e7ee490 169
da9fcb72 170 if (vmbus_connection.int_page) {
df3493e0 171 free_pages((unsigned long)vmbus_connection.int_page, 0);
da9fcb72 172 vmbus_connection.int_page = NULL;
3e7ee490
HJ
173 }
174
da9fcb72 175 if (vmbus_connection.monitor_pages) {
df3493e0 176 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
da9fcb72 177 vmbus_connection.monitor_pages = NULL;
3e7ee490
HJ
178 }
179
dd9b15dc 180 kfree(msginfo);
3e7ee490 181
3e7ee490
HJ
182 return ret;
183}
184
3e7ee490 185
3e189519 186/*
c6977677
HZ
187 * relid2channel - Get the channel object given its
188 * child relative id (ie channel id)
fd8b85ea 189 */
c6977677 190struct vmbus_channel *relid2channel(u32 relid)
3e7ee490 191{
aded7165 192 struct vmbus_channel *channel;
15b2f647 193 struct vmbus_channel *found_channel = NULL;
0f5e44ca 194 unsigned long flags;
3e7ee490 195
15b2f647 196 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
da9fcb72 197 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
15b2f647
HZ
198 if (channel->offermsg.child_relid == relid) {
199 found_channel = channel;
3e7ee490
HJ
200 break;
201 }
202 }
15b2f647 203 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
3e7ee490 204
15b2f647 205 return found_channel;
3e7ee490
HJ
206}
207
3e189519 208/*
c6977677 209 * process_chn_event - Process a channel event notification
fd8b85ea 210 */
35436487 211static void process_chn_event(u32 relid)
3e7ee490 212{
aded7165 213 struct vmbus_channel *channel;
dad76bf7 214 unsigned long flags;
3e7ee490 215
454f18a9
BP
216 /*
217 * Find the channel based on this relid and invokes the
218 * channel callback to process the event
219 */
c6977677 220 channel = relid2channel(relid);
3e7ee490 221
24326039
S
222 if (!channel) {
223 pr_err("channel not found for relid - %u\n", relid);
224 return;
225 }
226
227 /*
228 * A channel once created is persistent even when there
229 * is no driver handling the device. An unloading driver
230 * sets the onchannel_callback to NULL under the
231 * protection of the channel inbound_lock. Thus, checking
232 * and invoking the driver specific callback takes care of
233 * orderly unloading of the driver.
234 */
235
dad76bf7 236 spin_lock_irqsave(&channel->inbound_lock, flags);
24326039 237 if (channel->onchannel_callback != NULL)
df452fa1 238 channel->onchannel_callback(channel->channel_callback_context);
d9add43b 239 else
24326039 240 pr_err("no channel callback for relid - %u\n", relid);
d9add43b 241
dad76bf7 242 spin_unlock_irqrestore(&channel->inbound_lock, flags);
3e7ee490
HJ
243}
244
3e189519 245/*
c6977677 246 * vmbus_on_event - Handler for events
fd8b85ea 247 */
6de3d6aa 248void vmbus_on_event(unsigned long data)
3e7ee490 249{
35436487
OH
250 u32 dword;
251 u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
3e7ee490 252 int bit;
35436487 253 u32 relid;
da9fcb72 254 u32 *recv_int_page = vmbus_connection.recv_int_page;
3e7ee490 255
454f18a9 256 /* Check events */
242b45aa
OH
257 if (!recv_int_page)
258 return;
259 for (dword = 0; dword < maxdword; dword++) {
260 if (!recv_int_page[dword])
261 continue;
262 for (bit = 0; bit < 32; bit++) {
d9add43b
S
263 if (sync_test_and_clear_bit(bit,
264 (unsigned long *)&recv_int_page[dword])) {
242b45aa
OH
265 relid = (dword << 5) + bit;
266
d9add43b 267 if (relid == 0)
6d81d330
S
268 /*
269 * Special case - vmbus
270 * channel protocol msg
271 */
242b45aa 272 continue;
d9add43b 273
35436487 274 process_chn_event(relid);
3e7ee490 275 }
242b45aa 276 }
3e7ee490 277 }
3e7ee490
HJ
278}
279
3e189519 280/*
c6977677 281 * vmbus_post_msg - Send a msg on the vmbus's message connection
fd8b85ea 282 */
c6977677 283int vmbus_post_msg(void *buffer, size_t buflen)
3e7ee490 284{
15b2f647 285 union hv_connection_id conn_id;
5289d3d1
S
286 int ret = 0;
287 int retries = 0;
3e7ee490 288
15b2f647
HZ
289 conn_id.asu32 = 0;
290 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
5289d3d1
S
291
292 /*
293 * hv_post_message() can have transient failures because of
294 * insufficient resources. Retry the operation a couple of
295 * times before giving up.
296 */
297 while (retries < 3) {
298 ret = hv_post_message(conn_id, 1, buffer, buflen);
299 if (ret != HV_STATUS_INSUFFICIENT_BUFFERS)
300 return ret;
301 retries++;
302 msleep(100);
303 }
304 return ret;
3e7ee490
HJ
305}
306
3e189519 307/*
c6977677 308 * vmbus_set_event - Send an event notification to the parent
fd8b85ea 309 */
c6977677 310int vmbus_set_event(u32 child_relid)
3e7ee490 311{
454f18a9 312 /* Each u32 represents 32 channels */
22356585 313 sync_set_bit(child_relid & 31,
da9fcb72 314 (unsigned long *)vmbus_connection.send_int_page +
15b2f647 315 (child_relid >> 5));
7c369f40 316
d44890c8 317 return hv_signal_event();
3e7ee490 318}