2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/completion.h>
31 #include <linux/hyperv.h>
33 #include "hyperv_vmbus.h"
35 struct vmbus_channel_message_table_entry {
36 enum vmbus_channel_message_type message_type;
37 void (*message_handler)(struct vmbus_channel_message_header *msg);
42 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
43 * @icmsghdrp: Pointer to msg header structure
44 * @icmsg_negotiate: Pointer to negotiate message structure
45 * @buf: Raw buffer channel data
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
54 * Mainly used by Hyper-V drivers.
56 void vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
57 struct icmsg_negotiate *negop, u8 *buf)
59 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
60 icmsghdrp->icmsgsize = 0x10;
62 negop = (struct icmsg_negotiate *)&buf[
63 sizeof(struct vmbuspipe_hdr) +
64 sizeof(struct icmsg_hdr)];
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;
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;
79 negop->icframe_vercnt = 1;
80 negop->icmsg_vercnt = 1;
83 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
86 * alloc_channel - Allocate and initialize a vmbus channel object
88 static struct vmbus_channel *alloc_channel(void)
90 struct vmbus_channel *channel;
92 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
96 spin_lock_init(&channel->inbound_lock);
98 channel->controlwq = create_workqueue("hv_vmbus_ctl");
99 if (!channel->controlwq) {
108 * release_hannel - Release the vmbus channel object itself
110 static void release_channel(struct work_struct *work)
112 struct vmbus_channel *channel = container_of(work,
113 struct vmbus_channel,
116 destroy_workqueue(channel->controlwq);
122 * free_channel - Release the resources used by the vmbus channel object
124 static void free_channel(struct vmbus_channel *channel)
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.
132 INIT_WORK(&channel->work, release_channel);
133 queue_work(vmbus_connection.work_queue, &channel->work);
139 * vmbus_process_rescind_offer -
140 * Rescind the offer by initiating a device removal
142 static void vmbus_process_rescind_offer(struct work_struct *work)
144 struct vmbus_channel *channel = container_of(work,
145 struct vmbus_channel,
148 vmbus_device_unregister(channel->device_obj);
151 void vmbus_free_channels(void)
153 struct vmbus_channel *channel;
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);
163 * vmbus_process_offer - Process the offer by creating a channel/device
164 * associated with this offer
166 static void vmbus_process_offer(struct work_struct *work)
168 struct vmbus_channel *newchannel = container_of(work,
169 struct vmbus_channel,
171 struct vmbus_channel *channel;
176 /* The next possible work is rescind handling */
177 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
179 /* Make sure this is a new offer */
180 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
182 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
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)) {
193 list_add_tail(&newchannel->listentry,
194 &vmbus_connection.chn_list);
196 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
199 free_channel(newchannel);
204 * Start the process of binding this offer to the driver
205 * We need to set the DeviceObject field before calling
206 * vmbus_child_dev_add()
208 newchannel->device_obj = vmbus_device_create(
209 &newchannel->offermsg.offer.if_type,
210 &newchannel->offermsg.offer.if_instance,
214 * Add the new device to the bus. This will kick off device-driver
215 * binding which eventually invokes the device driver's AddDevice()
218 ret = vmbus_device_register(newchannel->device_obj);
220 pr_err("unable to add child device object (relid %d)\n",
221 newchannel->offermsg.child_relid);
223 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
224 list_del(&newchannel->listentry);
225 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
226 kfree(newchannel->device_obj);
228 free_channel(newchannel);
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
235 newchannel->state = CHANNEL_OPEN_STATE;
240 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
243 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
245 struct vmbus_channel_offer_channel *offer;
246 struct vmbus_channel *newchannel;
248 uuid_le *guidinstance;
250 offer = (struct vmbus_channel_offer_channel *)hdr;
252 guidtype = &offer->offer.if_type;
253 guidinstance = &offer->offer.if_instance;
255 /* Allocate the channel object and save this offer. */
256 newchannel = alloc_channel();
258 pr_err("Unable to allocate channel object\n");
262 memcpy(&newchannel->offermsg, offer,
263 sizeof(struct vmbus_channel_offer_channel));
264 newchannel->monitor_grp = (u8)offer->monitorid / 32;
265 newchannel->monitor_bit = (u8)offer->monitorid % 32;
267 INIT_WORK(&newchannel->work, vmbus_process_offer);
268 queue_work(newchannel->controlwq, &newchannel->work);
272 * vmbus_onoffer_rescind - Rescind offer handler.
274 * We queue a work item to process this offer synchronously
276 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
278 struct vmbus_channel_rescind_offer *rescind;
279 struct vmbus_channel *channel;
281 rescind = (struct vmbus_channel_rescind_offer *)hdr;
282 channel = relid2channel(rescind->child_relid);
285 /* Just return here, no channel found */
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);
294 * vmbus_onoffers_delivered -
295 * This is invoked when all offers have been delivered.
297 * Nothing to do here.
299 static void vmbus_onoffers_delivered(
300 struct vmbus_channel_message_header *hdr)
305 * vmbus_onopen_result - Open result handler.
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
311 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
313 struct vmbus_channel_open_result *result;
314 struct vmbus_channel_msginfo *msginfo;
315 struct vmbus_channel_message_header *requestheader;
316 struct vmbus_channel_open_channel *openmsg;
319 result = (struct vmbus_channel_open_result *)hdr;
322 * Find the open msg, copy the result and signal/unblock the wait event
324 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
326 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
329 (struct vmbus_channel_message_header *)msginfo->msg;
331 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
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,
339 struct vmbus_channel_open_result));
340 complete(&msginfo->waitevent);
345 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
349 * vmbus_ongpadl_created - GPADL created handler.
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
355 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
357 struct vmbus_channel_gpadl_created *gpadlcreated;
358 struct vmbus_channel_msginfo *msginfo;
359 struct vmbus_channel_message_header *requestheader;
360 struct vmbus_channel_gpadl_header *gpadlheader;
363 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
366 * Find the establish msg, copy the result and signal/unblock the wait
369 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
371 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
374 (struct vmbus_channel_message_header *)msginfo->msg;
376 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
378 (struct vmbus_channel_gpadl_header *)requestheader;
380 if ((gpadlcreated->child_relid ==
381 gpadlheader->child_relid) &&
382 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
383 memcpy(&msginfo->response.gpadl_created,
386 struct vmbus_channel_gpadl_created));
387 complete(&msginfo->waitevent);
392 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
396 * vmbus_ongpadl_torndown - GPADL torndown handler.
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
402 static void vmbus_ongpadl_torndown(
403 struct vmbus_channel_message_header *hdr)
405 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
406 struct vmbus_channel_msginfo *msginfo;
407 struct vmbus_channel_message_header *requestheader;
408 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
411 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
414 * Find the open msg, copy the result and signal/unblock the wait event
416 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
418 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
421 (struct vmbus_channel_message_header *)msginfo->msg;
423 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
425 (struct vmbus_channel_gpadl_teardown *)requestheader;
427 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
428 memcpy(&msginfo->response.gpadl_torndown,
431 struct vmbus_channel_gpadl_torndown));
432 complete(&msginfo->waitevent);
437 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
441 * vmbus_onversion_response - Version response handler
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
447 static void vmbus_onversion_response(
448 struct vmbus_channel_message_header *hdr)
450 struct vmbus_channel_msginfo *msginfo;
451 struct vmbus_channel_message_header *requestheader;
452 struct vmbus_channel_initiate_contact *initiate;
453 struct vmbus_channel_version_response *version_response;
456 version_response = (struct vmbus_channel_version_response *)hdr;
457 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
459 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
462 (struct vmbus_channel_message_header *)msginfo->msg;
464 if (requestheader->msgtype ==
465 CHANNELMSG_INITIATE_CONTACT) {
467 (struct vmbus_channel_initiate_contact *)requestheader;
468 memcpy(&msginfo->response.version_response,
470 sizeof(struct vmbus_channel_version_response));
471 complete(&msginfo->waitevent);
474 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
477 /* Channel message dispatch table */
478 static struct vmbus_channel_message_table_entry
479 channel_message_table[CHANNELMSG_COUNT] = {
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},
500 * vmbus_onmessage - Handler for channel protocol messages.
502 * This is invoked in the vmbus worker thread context.
504 void vmbus_onmessage(void *context)
506 struct hv_message *msg = context;
507 struct vmbus_channel_message_header *hdr;
510 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
511 size = msg->header.payload_size;
513 if (hdr->msgtype >= CHANNELMSG_COUNT) {
514 pr_err("Received invalid channel message type %d size %d\n",
516 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
517 (unsigned char *)msg->u.payload, size);
521 if (channel_message_table[hdr->msgtype].message_handler)
522 channel_message_table[hdr->msgtype].message_handler(hdr);
524 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
528 * vmbus_request_offers - Send a request to get all our pending offers.
530 int vmbus_request_offers(void)
532 struct vmbus_channel_message_header *msg;
533 struct vmbus_channel_msginfo *msginfo;
536 msginfo = kmalloc(sizeof(*msginfo) +
537 sizeof(struct vmbus_channel_message_header),
542 init_completion(&msginfo->waitevent);
544 msg = (struct vmbus_channel_message_header *)msginfo->msg;
546 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
549 ret = vmbus_post_msg(msg,
550 sizeof(struct vmbus_channel_message_header));
552 pr_err("Unable to request offers - %d\n", ret);
557 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);