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