staging: hv: Convert camel cased struct fields in vmbus_private.h to lower cases
[deliverable/linux.git] / drivers / staging / hv / connection.c
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 */
23 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include "osd.h"
28 #include "logging.h"
29 #include "vmbus_private.h"
30
31
32 struct vmbus_connection vmbus_connection = {
33 .conn_state = DISCONNECTED,
34 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
35 };
36
37 /*
38 * vmbus_connect - Sends a connect request on the partition service connection
39 */
40 int vmbus_connect(void)
41 {
42 int ret = 0;
43 struct vmbus_channel_msginfo *msginfo = NULL;
44 struct vmbus_channel_initiate_contact *msg;
45 unsigned long flags;
46
47 /* Make sure we are not connecting or connected */
48 if (vmbus_connection.conn_state != DISCONNECTED)
49 return -1;
50
51 /* Initialize the vmbus connection */
52 vmbus_connection.conn_state = CONNECTING;
53 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
54 if (!vmbus_connection.work_queue) {
55 ret = -1;
56 goto Cleanup;
57 }
58
59 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
60 spin_lock_init(&vmbus_connection.channelmsg_lock);
61
62 INIT_LIST_HEAD(&vmbus_connection.chn_list);
63 spin_lock_init(&vmbus_connection.channel_lock);
64
65 /*
66 * Setup the vmbus event connection for channel interrupt
67 * abstraction stuff
68 */
69 vmbus_connection.int_page = osd_page_alloc(1);
70 if (vmbus_connection.int_page == NULL) {
71 ret = -1;
72 goto Cleanup;
73 }
74
75 vmbus_connection.recv_int_page = vmbus_connection.int_page;
76 vmbus_connection.send_int_page =
77 (void *)((unsigned long)vmbus_connection.int_page +
78 (PAGE_SIZE >> 1));
79
80 /*
81 * Setup the monitor notification facility. The 1st page for
82 * parent->child and the 2nd page for child->parent
83 */
84 vmbus_connection.monitor_pages = osd_page_alloc(2);
85 if (vmbus_connection.monitor_pages == NULL) {
86 ret = -1;
87 goto Cleanup;
88 }
89
90 msginfo = kzalloc(sizeof(*msginfo) +
91 sizeof(struct vmbus_channel_initiate_contact),
92 GFP_KERNEL);
93 if (msginfo == NULL) {
94 ret = -ENOMEM;
95 goto Cleanup;
96 }
97
98 msginfo->waitevent = osd_waitevent_create();
99 if (!msginfo->waitevent) {
100 ret = -ENOMEM;
101 goto Cleanup;
102 }
103
104 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
105
106 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
107 msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
108 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
109 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 * Add to list before we send the request since we may
116 * receive the response before returning from this routine
117 */
118 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
119 list_add_tail(&msginfo->msglistentry,
120 &vmbus_connection.chn_msg_list);
121
122 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
123
124 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
125 "monitor1 pfn %llx,, monitor2 pfn %llx",
126 msg->interrupt_page, msg->monitor_page1, msg->monitor_page2);
127
128 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
129 ret = vmbus_post_msg(msg,
130 sizeof(struct vmbus_channel_initiate_contact));
131 if (ret != 0) {
132 list_del(&msginfo->msglistentry);
133 goto Cleanup;
134 }
135
136 /* Wait for the connection response */
137 osd_waitevent_wait(msginfo->waitevent);
138
139 list_del(&msginfo->msglistentry);
140
141 /* Check if successful */
142 if (msginfo->response.version_response.version_supported) {
143 DPRINT_INFO(VMBUS, "Vmbus connected!!");
144 vmbus_connection.conn_state = CONNECTED;
145
146 } else {
147 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
148 "current version (%d) not supported",
149 VMBUS_REVISION_NUMBER);
150 ret = -1;
151 goto Cleanup;
152 }
153
154 kfree(msginfo->waitevent);
155 kfree(msginfo);
156 return 0;
157
158 Cleanup:
159 vmbus_connection.conn_state = DISCONNECTED;
160
161 if (vmbus_connection.work_queue)
162 destroy_workqueue(vmbus_connection.work_queue);
163
164 if (vmbus_connection.int_page) {
165 osd_page_free(vmbus_connection.int_page, 1);
166 vmbus_connection.int_page = NULL;
167 }
168
169 if (vmbus_connection.monitor_pages) {
170 osd_page_free(vmbus_connection.monitor_pages, 2);
171 vmbus_connection.monitor_pages = NULL;
172 }
173
174 if (msginfo) {
175 kfree(msginfo->waitevent);
176 kfree(msginfo);
177 }
178
179 return ret;
180 }
181
182 /*
183 * vmbus_disconnect -
184 * Sends a disconnect request on the partition service connection
185 */
186 int vmbus_disconnect(void)
187 {
188 int ret = 0;
189 struct vmbus_channel_message_header *msg;
190
191 /* Make sure we are connected */
192 if (vmbus_connection.conn_state != CONNECTED)
193 return -1;
194
195 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
196 if (!msg)
197 return -ENOMEM;
198
199 msg->msgtype = CHANNELMSG_UNLOAD;
200
201 ret = vmbus_post_msg(msg,
202 sizeof(struct vmbus_channel_message_header));
203 if (ret != 0)
204 goto Cleanup;
205
206 osd_page_free(vmbus_connection.int_page, 1);
207
208 /* TODO: iterate thru the msg list and free up */
209 destroy_workqueue(vmbus_connection.work_queue);
210
211 vmbus_connection.conn_state = DISCONNECTED;
212
213 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
214
215 Cleanup:
216 kfree(msg);
217 return ret;
218 }
219
220 /*
221 * relid2channel - Get the channel object given its
222 * child relative id (ie channel id)
223 */
224 struct vmbus_channel *relid2channel(u32 relid)
225 {
226 struct vmbus_channel *channel;
227 struct vmbus_channel *found_channel = NULL;
228 unsigned long flags;
229
230 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
231 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
232 if (channel->offermsg.child_relid == relid) {
233 found_channel = channel;
234 break;
235 }
236 }
237 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
238
239 return found_channel;
240 }
241
242 /*
243 * process_chn_event - Process a channel event notification
244 */
245 static void process_chn_event(void *context)
246 {
247 struct vmbus_channel *channel;
248 u32 relid = (u32)(unsigned long)context;
249
250 /* ASSERT(relId > 0); */
251
252 /*
253 * Find the channel based on this relid and invokes the
254 * channel callback to process the event
255 */
256 channel = relid2channel(relid);
257
258 if (channel) {
259 vmbus_onchannel_event(channel);
260 /*
261 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
262 * vmbus_onchannel_event,
263 * (void*)channel);
264 */
265 } else {
266 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relid);
267 }
268 }
269
270 /*
271 * vmbus_on_event - Handler for events
272 */
273 void vmbus_on_event(void)
274 {
275 int dword;
276 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
277 int bit;
278 int relid;
279 u32 *recv_int_page = vmbus_connection.recv_int_page;
280
281 /* Check events */
282 if (recv_int_page) {
283 for (dword = 0; dword < maxdword; dword++) {
284 if (recv_int_page[dword]) {
285 for (bit = 0; bit < 32; bit++) {
286 if (test_and_clear_bit(bit,
287 (unsigned long *)
288 &recv_int_page[dword])) {
289 relid = (dword << 5) + bit;
290 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
291
292 if (relid == 0) {
293 /* special case - vmbus channel protocol msg */
294 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
295 continue;
296 } else {
297 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
298 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
299 process_chn_event((void *)
300 (unsigned long)relid);
301 }
302 }
303 }
304 }
305 }
306 }
307 return;
308 }
309
310 /*
311 * vmbus_post_msg - Send a msg on the vmbus's message connection
312 */
313 int vmbus_post_msg(void *buffer, size_t buflen)
314 {
315 union hv_connection_id conn_id;
316
317 conn_id.asu32 = 0;
318 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
319 return hv_post_message(conn_id, 1, buffer, buflen);
320 }
321
322 /*
323 * vmbus_set_event - Send an event notification to the parent
324 */
325 int vmbus_set_event(u32 child_relid)
326 {
327 /* Each u32 represents 32 channels */
328 set_bit(child_relid & 31,
329 (unsigned long *)vmbus_connection.send_int_page +
330 (child_relid >> 5));
331
332 return hv_signal_event();
333 }
This page took 0.063286 seconds and 5 git commands to generate.