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