Commit | Line | Data |
---|---|---|
3e7ee490 | 1 | /* |
3e7ee490 HJ |
2 | * Copyright (c) 2009, Microsoft Corporation. |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307 USA. | |
16 | * | |
17 | * Authors: | |
18 | * Haiyang Zhang <haiyangz@microsoft.com> | |
19 | * Hank Janssen <hjanssen@microsoft.com> | |
3e7ee490 | 20 | */ |
0a46618d HJ |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
a0086dc5 | 23 | #include <linux/kernel.h> |
0c3b7b2f S |
24 | #include <linux/sched.h> |
25 | #include <linux/wait.h> | |
a0086dc5 | 26 | #include <linux/mm.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
53af545b | 28 | #include <linux/list.h> |
c88c4e4c | 29 | #include <linux/module.h> |
8b5d6d3b | 30 | #include <linux/completion.h> |
46a97191 | 31 | #include <linux/hyperv.h> |
3f335ea2 | 32 | |
0f2a6619 | 33 | #include "hyperv_vmbus.h" |
3e7ee490 | 34 | |
1d7e907f | 35 | struct vmbus_channel_message_table_entry { |
cf9dcba7 | 36 | enum vmbus_channel_message_type message_type; |
fa90f1de | 37 | void (*message_handler)(struct vmbus_channel_message_header *msg); |
1d7e907f | 38 | }; |
3e7ee490 | 39 | |
c88c4e4c HJ |
40 | |
41 | /** | |
da0e9631 | 42 | * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message |
c88c4e4c HJ |
43 | * @icmsghdrp: Pointer to msg header structure |
44 | * @icmsg_negotiate: Pointer to negotiate message structure | |
45 | * @buf: Raw buffer channel data | |
46 | * | |
47 | * @icmsghdrp is of type &struct icmsg_hdr. | |
48 | * @negop is of type &struct icmsg_negotiate. | |
49 | * Set up and fill in default negotiate response message. This response can | |
50 | * come from both the vmbus driver and the hv_utils driver. The current api | |
51 | * will respond properly to both Windows 2008 and Windows 2008-R2 operating | |
52 | * systems. | |
53 | * | |
54 | * Mainly used by Hyper-V drivers. | |
55 | */ | |
da0e9631 GKH |
56 | void vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, |
57 | struct icmsg_negotiate *negop, u8 *buf) | |
c88c4e4c | 58 | { |
a3605300 | 59 | icmsghdrp->icmsgsize = 0x10; |
c88c4e4c | 60 | |
a3605300 S |
61 | negop = (struct icmsg_negotiate *)&buf[ |
62 | sizeof(struct vmbuspipe_hdr) + | |
63 | sizeof(struct icmsg_hdr)]; | |
c88c4e4c | 64 | |
a3605300 S |
65 | if (negop->icframe_vercnt == 2 && |
66 | negop->icversion_data[1].major == 3) { | |
67 | negop->icversion_data[0].major = 3; | |
68 | negop->icversion_data[0].minor = 0; | |
69 | negop->icversion_data[1].major = 3; | |
70 | negop->icversion_data[1].minor = 0; | |
71 | } else { | |
72 | negop->icversion_data[0].major = 1; | |
73 | negop->icversion_data[0].minor = 0; | |
74 | negop->icversion_data[1].major = 1; | |
75 | negop->icversion_data[1].minor = 0; | |
c88c4e4c | 76 | } |
a3605300 S |
77 | |
78 | negop->icframe_vercnt = 1; | |
79 | negop->icmsg_vercnt = 1; | |
c88c4e4c | 80 | } |
a3605300 | 81 | |
da0e9631 | 82 | EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); |
c88c4e4c | 83 | |
3e189519 | 84 | /* |
e98cb276 | 85 | * alloc_channel - Allocate and initialize a vmbus channel object |
bd60c33e | 86 | */ |
50fe56d2 | 87 | static struct vmbus_channel *alloc_channel(void) |
3e7ee490 | 88 | { |
aded7165 | 89 | struct vmbus_channel *channel; |
3e7ee490 | 90 | |
aded7165 | 91 | channel = kzalloc(sizeof(*channel), GFP_ATOMIC); |
3e7ee490 | 92 | if (!channel) |
3e7ee490 | 93 | return NULL; |
3e7ee490 | 94 | |
54411c42 | 95 | spin_lock_init(&channel->inbound_lock); |
3e7ee490 | 96 | |
c50f7fb2 HZ |
97 | channel->controlwq = create_workqueue("hv_vmbus_ctl"); |
98 | if (!channel->controlwq) { | |
8c69f52a | 99 | kfree(channel); |
3e7ee490 HJ |
100 | return NULL; |
101 | } | |
102 | ||
103 | return channel; | |
104 | } | |
105 | ||
3e189519 | 106 | /* |
e98cb276 | 107 | * release_hannel - Release the vmbus channel object itself |
bd60c33e | 108 | */ |
4b2f9abe | 109 | static void release_channel(struct work_struct *work) |
3e7ee490 | 110 | { |
4b2f9abe TT |
111 | struct vmbus_channel *channel = container_of(work, |
112 | struct vmbus_channel, | |
113 | work); | |
3e7ee490 | 114 | |
c50f7fb2 | 115 | destroy_workqueue(channel->controlwq); |
3e7ee490 | 116 | |
8c69f52a | 117 | kfree(channel); |
3e7ee490 HJ |
118 | } |
119 | ||
3e189519 | 120 | /* |
e98cb276 | 121 | * free_channel - Release the resources used by the vmbus channel object |
bd60c33e | 122 | */ |
9f3e28e3 | 123 | static void free_channel(struct vmbus_channel *channel) |
3e7ee490 | 124 | { |
3e7ee490 | 125 | |
bd60c33e GKH |
126 | /* |
127 | * We have to release the channel's workqueue/thread in the vmbus's | |
128 | * workqueue/thread context | |
129 | * ie we can't destroy ourselves. | |
130 | */ | |
4b2f9abe | 131 | INIT_WORK(&channel->work, release_channel); |
da9fcb72 | 132 | queue_work(vmbus_connection.work_queue, &channel->work); |
3e7ee490 HJ |
133 | } |
134 | ||
8b5d6d3b | 135 | |
8b5d6d3b | 136 | |
4b2f9abe TT |
137 | /* |
138 | * vmbus_process_rescind_offer - | |
139 | * Rescind the offer by initiating a device removal | |
140 | */ | |
141 | static void vmbus_process_rescind_offer(struct work_struct *work) | |
142 | { | |
143 | struct vmbus_channel *channel = container_of(work, | |
144 | struct vmbus_channel, | |
145 | work); | |
146 | ||
696453ba | 147 | vmbus_device_unregister(channel->device_obj); |
4b2f9abe | 148 | } |
8b5d6d3b | 149 | |
93e5bd06 S |
150 | void vmbus_free_channels(void) |
151 | { | |
152 | struct vmbus_channel *channel; | |
153 | ||
154 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { | |
155 | vmbus_device_unregister(channel->device_obj); | |
156 | kfree(channel->device_obj); | |
157 | free_channel(channel); | |
158 | } | |
159 | } | |
160 | ||
3e189519 | 161 | /* |
e98cb276 | 162 | * vmbus_process_offer - Process the offer by creating a channel/device |
c88c4e4c | 163 | * associated with this offer |
bd60c33e | 164 | */ |
4b2f9abe | 165 | static void vmbus_process_offer(struct work_struct *work) |
3e7ee490 | 166 | { |
4b2f9abe TT |
167 | struct vmbus_channel *newchannel = container_of(work, |
168 | struct vmbus_channel, | |
169 | work); | |
aded7165 | 170 | struct vmbus_channel *channel; |
188963ec | 171 | bool fnew = true; |
bd60c33e | 172 | int ret; |
0f5e44ca | 173 | unsigned long flags; |
3e7ee490 | 174 | |
4b2f9abe TT |
175 | /* The next possible work is rescind handling */ |
176 | INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); | |
177 | ||
454f18a9 | 178 | /* Make sure this is a new offer */ |
15b2f647 | 179 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 180 | |
da9fcb72 | 181 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
358d2ee2 S |
182 | if (!uuid_le_cmp(channel->offermsg.offer.if_type, |
183 | newchannel->offermsg.offer.if_type) && | |
184 | !uuid_le_cmp(channel->offermsg.offer.if_instance, | |
185 | newchannel->offermsg.offer.if_instance)) { | |
188963ec | 186 | fnew = false; |
3e7ee490 HJ |
187 | break; |
188 | } | |
189 | } | |
190 | ||
188963ec | 191 | if (fnew) |
c50f7fb2 | 192 | list_add_tail(&newchannel->listentry, |
da9fcb72 | 193 | &vmbus_connection.chn_list); |
bd60c33e | 194 | |
15b2f647 | 195 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 196 | |
188963ec | 197 | if (!fnew) { |
e98cb276 | 198 | free_channel(newchannel); |
3e7ee490 HJ |
199 | return; |
200 | } | |
201 | ||
bd60c33e GKH |
202 | /* |
203 | * Start the process of binding this offer to the driver | |
204 | * We need to set the DeviceObject field before calling | |
646f1ea3 | 205 | * vmbus_child_dev_add() |
bd60c33e | 206 | */ |
f2c73011 | 207 | newchannel->device_obj = vmbus_device_create( |
767dff68 HZ |
208 | &newchannel->offermsg.offer.if_type, |
209 | &newchannel->offermsg.offer.if_instance, | |
188963ec | 210 | newchannel); |
3e7ee490 | 211 | |
454f18a9 BP |
212 | /* |
213 | * Add the new device to the bus. This will kick off device-driver | |
214 | * binding which eventually invokes the device driver's AddDevice() | |
215 | * method. | |
216 | */ | |
22794281 | 217 | ret = vmbus_device_register(newchannel->device_obj); |
bd60c33e | 218 | if (ret != 0) { |
0a46618d | 219 | pr_err("unable to add child device object (relid %d)\n", |
c50f7fb2 | 220 | newchannel->offermsg.child_relid); |
3e7ee490 | 221 | |
15b2f647 | 222 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
c50f7fb2 | 223 | list_del(&newchannel->listentry); |
15b2f647 | 224 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
8b8ee675 | 225 | kfree(newchannel->device_obj); |
3e7ee490 | 226 | |
e98cb276 | 227 | free_channel(newchannel); |
bd60c33e | 228 | } else { |
454f18a9 BP |
229 | /* |
230 | * This state is used to indicate a successful open | |
231 | * so that when we do close the channel normally, we | |
232 | * can cleanup properly | |
233 | */ | |
c50f7fb2 | 234 | newchannel->state = CHANNEL_OPEN_STATE; |
3e7ee490 | 235 | } |
3e7ee490 HJ |
236 | } |
237 | ||
3e189519 | 238 | /* |
e98cb276 | 239 | * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. |
bd60c33e | 240 | * |
bd60c33e | 241 | */ |
e98cb276 | 242 | static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 243 | { |
bd60c33e | 244 | struct vmbus_channel_offer_channel *offer; |
188963ec | 245 | struct vmbus_channel *newchannel; |
358d2ee2 S |
246 | uuid_le *guidtype; |
247 | uuid_le *guidinstance; | |
3e7ee490 | 248 | |
bd60c33e | 249 | offer = (struct vmbus_channel_offer_channel *)hdr; |
3e7ee490 | 250 | |
767dff68 HZ |
251 | guidtype = &offer->offer.if_type; |
252 | guidinstance = &offer->offer.if_instance; | |
3e7ee490 | 253 | |
454f18a9 | 254 | /* Allocate the channel object and save this offer. */ |
e98cb276 | 255 | newchannel = alloc_channel(); |
188963ec | 256 | if (!newchannel) { |
0a46618d | 257 | pr_err("Unable to allocate channel object\n"); |
3e7ee490 HJ |
258 | return; |
259 | } | |
260 | ||
c50f7fb2 | 261 | memcpy(&newchannel->offermsg, offer, |
bd60c33e | 262 | sizeof(struct vmbus_channel_offer_channel)); |
c50f7fb2 HZ |
263 | newchannel->monitor_grp = (u8)offer->monitorid / 32; |
264 | newchannel->monitor_bit = (u8)offer->monitorid % 32; | |
3e7ee490 | 265 | |
4b2f9abe TT |
266 | INIT_WORK(&newchannel->work, vmbus_process_offer); |
267 | queue_work(newchannel->controlwq, &newchannel->work); | |
3e7ee490 HJ |
268 | } |
269 | ||
3e189519 | 270 | /* |
e98cb276 | 271 | * vmbus_onoffer_rescind - Rescind offer handler. |
bd60c33e GKH |
272 | * |
273 | * We queue a work item to process this offer synchronously | |
274 | */ | |
e98cb276 | 275 | static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 276 | { |
bd60c33e | 277 | struct vmbus_channel_rescind_offer *rescind; |
aded7165 | 278 | struct vmbus_channel *channel; |
3e7ee490 | 279 | |
bd60c33e | 280 | rescind = (struct vmbus_channel_rescind_offer *)hdr; |
c6977677 | 281 | channel = relid2channel(rescind->child_relid); |
98e08702 HJ |
282 | |
283 | if (channel == NULL) | |
284 | /* Just return here, no channel found */ | |
3e7ee490 | 285 | return; |
3e7ee490 | 286 | |
4b2f9abe TT |
287 | /* work is initialized for vmbus_process_rescind_offer() from |
288 | * vmbus_process_offer() where the channel got created */ | |
289 | queue_work(channel->controlwq, &channel->work); | |
3e7ee490 HJ |
290 | } |
291 | ||
3e189519 | 292 | /* |
e98cb276 HZ |
293 | * vmbus_onoffers_delivered - |
294 | * This is invoked when all offers have been delivered. | |
bd60c33e GKH |
295 | * |
296 | * Nothing to do here. | |
297 | */ | |
e98cb276 | 298 | static void vmbus_onoffers_delivered( |
bd60c33e | 299 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 300 | { |
3e7ee490 HJ |
301 | } |
302 | ||
3e189519 | 303 | /* |
e98cb276 | 304 | * vmbus_onopen_result - Open result handler. |
bd60c33e GKH |
305 | * |
306 | * This is invoked when we received a response to our channel open request. | |
307 | * Find the matching request, copy the response and signal the requesting | |
308 | * thread. | |
309 | */ | |
e98cb276 | 310 | static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 311 | { |
bd60c33e | 312 | struct vmbus_channel_open_result *result; |
188963ec HZ |
313 | struct vmbus_channel_msginfo *msginfo; |
314 | struct vmbus_channel_message_header *requestheader; | |
315 | struct vmbus_channel_open_channel *openmsg; | |
dd0813b6 | 316 | unsigned long flags; |
3e7ee490 | 317 | |
bd60c33e | 318 | result = (struct vmbus_channel_open_result *)hdr; |
3e7ee490 | 319 | |
bd60c33e GKH |
320 | /* |
321 | * Find the open msg, copy the result and signal/unblock the wait event | |
322 | */ | |
15b2f647 | 323 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 324 | |
ebb61e5f HJ |
325 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
326 | msglistentry) { | |
188963ec | 327 | requestheader = |
c50f7fb2 | 328 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 329 | |
c50f7fb2 | 330 | if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { |
188963ec | 331 | openmsg = |
c50f7fb2 HZ |
332 | (struct vmbus_channel_open_channel *)msginfo->msg; |
333 | if (openmsg->child_relid == result->child_relid && | |
334 | openmsg->openid == result->openid) { | |
335 | memcpy(&msginfo->response.open_result, | |
bd60c33e | 336 | result, |
9568a193 S |
337 | sizeof( |
338 | struct vmbus_channel_open_result)); | |
339 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
340 | break; |
341 | } | |
342 | } | |
343 | } | |
15b2f647 | 344 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
345 | } |
346 | ||
3e189519 | 347 | /* |
e98cb276 | 348 | * vmbus_ongpadl_created - GPADL created handler. |
bd60c33e GKH |
349 | * |
350 | * This is invoked when we received a response to our gpadl create request. | |
351 | * Find the matching request, copy the response and signal the requesting | |
352 | * thread. | |
353 | */ | |
e98cb276 | 354 | static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 355 | { |
188963ec | 356 | struct vmbus_channel_gpadl_created *gpadlcreated; |
188963ec HZ |
357 | struct vmbus_channel_msginfo *msginfo; |
358 | struct vmbus_channel_message_header *requestheader; | |
359 | struct vmbus_channel_gpadl_header *gpadlheader; | |
dd0813b6 | 360 | unsigned long flags; |
3e7ee490 | 361 | |
188963ec | 362 | gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; |
3e7ee490 | 363 | |
bd60c33e GKH |
364 | /* |
365 | * Find the establish msg, copy the result and signal/unblock the wait | |
366 | * event | |
367 | */ | |
15b2f647 | 368 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 369 | |
ebb61e5f HJ |
370 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
371 | msglistentry) { | |
188963ec | 372 | requestheader = |
c50f7fb2 | 373 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 374 | |
c50f7fb2 | 375 | if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { |
188963ec HZ |
376 | gpadlheader = |
377 | (struct vmbus_channel_gpadl_header *)requestheader; | |
378 | ||
c50f7fb2 HZ |
379 | if ((gpadlcreated->child_relid == |
380 | gpadlheader->child_relid) && | |
381 | (gpadlcreated->gpadl == gpadlheader->gpadl)) { | |
382 | memcpy(&msginfo->response.gpadl_created, | |
188963ec | 383 | gpadlcreated, |
9568a193 S |
384 | sizeof( |
385 | struct vmbus_channel_gpadl_created)); | |
386 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
387 | break; |
388 | } | |
389 | } | |
390 | } | |
15b2f647 | 391 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
392 | } |
393 | ||
3e189519 | 394 | /* |
e98cb276 | 395 | * vmbus_ongpadl_torndown - GPADL torndown handler. |
bd60c33e GKH |
396 | * |
397 | * This is invoked when we received a response to our gpadl teardown request. | |
398 | * Find the matching request, copy the response and signal the requesting | |
399 | * thread. | |
400 | */ | |
e98cb276 | 401 | static void vmbus_ongpadl_torndown( |
bd60c33e | 402 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 403 | { |
188963ec | 404 | struct vmbus_channel_gpadl_torndown *gpadl_torndown; |
188963ec HZ |
405 | struct vmbus_channel_msginfo *msginfo; |
406 | struct vmbus_channel_message_header *requestheader; | |
407 | struct vmbus_channel_gpadl_teardown *gpadl_teardown; | |
dd0813b6 | 408 | unsigned long flags; |
3e7ee490 | 409 | |
188963ec | 410 | gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; |
bd60c33e GKH |
411 | |
412 | /* | |
413 | * Find the open msg, copy the result and signal/unblock the wait event | |
414 | */ | |
15b2f647 | 415 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 416 | |
ebb61e5f HJ |
417 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
418 | msglistentry) { | |
188963ec | 419 | requestheader = |
c50f7fb2 | 420 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 421 | |
c50f7fb2 | 422 | if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { |
188963ec HZ |
423 | gpadl_teardown = |
424 | (struct vmbus_channel_gpadl_teardown *)requestheader; | |
3e7ee490 | 425 | |
c50f7fb2 HZ |
426 | if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { |
427 | memcpy(&msginfo->response.gpadl_torndown, | |
188963ec | 428 | gpadl_torndown, |
9568a193 S |
429 | sizeof( |
430 | struct vmbus_channel_gpadl_torndown)); | |
431 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
432 | break; |
433 | } | |
434 | } | |
435 | } | |
15b2f647 | 436 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
437 | } |
438 | ||
3e189519 | 439 | /* |
e98cb276 | 440 | * vmbus_onversion_response - Version response handler |
bd60c33e GKH |
441 | * |
442 | * This is invoked when we received a response to our initiate contact request. | |
443 | * Find the matching request, copy the response and signal the requesting | |
444 | * thread. | |
445 | */ | |
e98cb276 | 446 | static void vmbus_onversion_response( |
bd60c33e | 447 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 448 | { |
188963ec HZ |
449 | struct vmbus_channel_msginfo *msginfo; |
450 | struct vmbus_channel_message_header *requestheader; | |
82250213 | 451 | struct vmbus_channel_initiate_contact *initiate; |
188963ec | 452 | struct vmbus_channel_version_response *version_response; |
dd0813b6 | 453 | unsigned long flags; |
3e7ee490 | 454 | |
188963ec | 455 | version_response = (struct vmbus_channel_version_response *)hdr; |
15b2f647 | 456 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 457 | |
ebb61e5f HJ |
458 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
459 | msglistentry) { | |
188963ec | 460 | requestheader = |
c50f7fb2 | 461 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 462 | |
c50f7fb2 HZ |
463 | if (requestheader->msgtype == |
464 | CHANNELMSG_INITIATE_CONTACT) { | |
188963ec HZ |
465 | initiate = |
466 | (struct vmbus_channel_initiate_contact *)requestheader; | |
c50f7fb2 | 467 | memcpy(&msginfo->response.version_response, |
188963ec | 468 | version_response, |
bd60c33e | 469 | sizeof(struct vmbus_channel_version_response)); |
9568a193 | 470 | complete(&msginfo->waitevent); |
3e7ee490 HJ |
471 | } |
472 | } | |
15b2f647 | 473 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
474 | } |
475 | ||
c8212f04 GKH |
476 | /* Channel message dispatch table */ |
477 | static struct vmbus_channel_message_table_entry | |
b7c6b02f | 478 | channel_message_table[CHANNELMSG_COUNT] = { |
c50f7fb2 HZ |
479 | {CHANNELMSG_INVALID, NULL}, |
480 | {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer}, | |
481 | {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind}, | |
482 | {CHANNELMSG_REQUESTOFFERS, NULL}, | |
483 | {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered}, | |
484 | {CHANNELMSG_OPENCHANNEL, NULL}, | |
485 | {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result}, | |
486 | {CHANNELMSG_CLOSECHANNEL, NULL}, | |
487 | {CHANNELMSG_GPADL_HEADER, NULL}, | |
488 | {CHANNELMSG_GPADL_BODY, NULL}, | |
489 | {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created}, | |
490 | {CHANNELMSG_GPADL_TEARDOWN, NULL}, | |
491 | {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown}, | |
492 | {CHANNELMSG_RELID_RELEASED, NULL}, | |
493 | {CHANNELMSG_INITIATE_CONTACT, NULL}, | |
494 | {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response}, | |
495 | {CHANNELMSG_UNLOAD, NULL}, | |
c8212f04 GKH |
496 | }; |
497 | ||
3e189519 | 498 | /* |
e98cb276 | 499 | * vmbus_onmessage - Handler for channel protocol messages. |
bd60c33e GKH |
500 | * |
501 | * This is invoked in the vmbus worker thread context. | |
502 | */ | |
e98cb276 | 503 | void vmbus_onmessage(void *context) |
3e7ee490 | 504 | { |
188963ec | 505 | struct hv_message *msg = context; |
82250213 | 506 | struct vmbus_channel_message_header *hdr; |
3e7ee490 HJ |
507 | int size; |
508 | ||
f6feebe0 HZ |
509 | hdr = (struct vmbus_channel_message_header *)msg->u.payload; |
510 | size = msg->header.payload_size; | |
3e7ee490 | 511 | |
c50f7fb2 | 512 | if (hdr->msgtype >= CHANNELMSG_COUNT) { |
0a46618d | 513 | pr_err("Received invalid channel message type %d size %d\n", |
c50f7fb2 | 514 | hdr->msgtype, size); |
04f50c4d | 515 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, |
f6feebe0 | 516 | (unsigned char *)msg->u.payload, size); |
3e7ee490 HJ |
517 | return; |
518 | } | |
519 | ||
b7c6b02f S |
520 | if (channel_message_table[hdr->msgtype].message_handler) |
521 | channel_message_table[hdr->msgtype].message_handler(hdr); | |
3e7ee490 | 522 | else |
0a46618d | 523 | pr_err("Unhandled channel message type %d\n", hdr->msgtype); |
3e7ee490 HJ |
524 | } |
525 | ||
3e189519 | 526 | /* |
e98cb276 | 527 | * vmbus_request_offers - Send a request to get all our pending offers. |
bd60c33e | 528 | */ |
e98cb276 | 529 | int vmbus_request_offers(void) |
3e7ee490 | 530 | { |
82250213 | 531 | struct vmbus_channel_message_header *msg; |
188963ec | 532 | struct vmbus_channel_msginfo *msginfo; |
9568a193 | 533 | int ret, t; |
3e7ee490 | 534 | |
188963ec | 535 | msginfo = kmalloc(sizeof(*msginfo) + |
bd60c33e GKH |
536 | sizeof(struct vmbus_channel_message_header), |
537 | GFP_KERNEL); | |
188963ec | 538 | if (!msginfo) |
75910f23 | 539 | return -ENOMEM; |
3e7ee490 | 540 | |
9568a193 | 541 | init_completion(&msginfo->waitevent); |
75910f23 | 542 | |
c50f7fb2 | 543 | msg = (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 544 | |
c50f7fb2 | 545 | msg->msgtype = CHANNELMSG_REQUESTOFFERS; |
3e7ee490 | 546 | |
3e7ee490 | 547 | |
c6977677 | 548 | ret = vmbus_post_msg(msg, |
bd60c33e GKH |
549 | sizeof(struct vmbus_channel_message_header)); |
550 | if (ret != 0) { | |
0a46618d | 551 | pr_err("Unable to request offers - %d\n", ret); |
3e7ee490 | 552 | |
0c3b7b2f S |
553 | goto cleanup; |
554 | } | |
3e7ee490 | 555 | |
2dfde964 | 556 | t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ); |
9568a193 | 557 | if (t == 0) { |
0c3b7b2f S |
558 | ret = -ETIMEDOUT; |
559 | goto cleanup; | |
3e7ee490 | 560 | } |
3e7ee490 | 561 | |
3e7ee490 HJ |
562 | |
563 | ||
0c3b7b2f | 564 | cleanup: |
dd9b15dc | 565 | kfree(msginfo); |
3e7ee490 | 566 | |
3e7ee490 HJ |
567 | return ret; |
568 | } | |
569 | ||
454f18a9 | 570 | /* eof */ |