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 icmsghdrp->icmsgsize = 0x10;
61 negop = (struct icmsg_negotiate *)&buf[
62 sizeof(struct vmbuspipe_hdr) +
63 sizeof(struct icmsg_hdr)];
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;
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;
78 negop->icframe_vercnt = 1;
79 negop->icmsg_vercnt = 1;
82 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
85 * alloc_channel - Allocate and initialize a vmbus channel object
87 static struct vmbus_channel *alloc_channel(void)
89 struct vmbus_channel *channel;
91 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
95 spin_lock_init(&channel->inbound_lock);
97 channel->controlwq = create_workqueue("hv_vmbus_ctl");
98 if (!channel->controlwq) {
107 * release_hannel - Release the vmbus channel object itself
109 static void release_channel(struct work_struct *work)
111 struct vmbus_channel *channel = container_of(work,
112 struct vmbus_channel,
115 destroy_workqueue(channel->controlwq);
121 * free_channel - Release the resources used by the vmbus channel object
123 static void free_channel(struct vmbus_channel *channel)
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.
131 INIT_WORK(&channel->work, release_channel);
132 queue_work(vmbus_connection.work_queue, &channel->work);
138 * vmbus_process_rescind_offer -
139 * Rescind the offer by initiating a device removal
141 static void vmbus_process_rescind_offer(struct work_struct *work)
143 struct vmbus_channel *channel = container_of(work,
144 struct vmbus_channel,
147 vmbus_device_unregister(channel->device_obj);
150 void vmbus_free_channels(void)
152 struct vmbus_channel *channel;
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);
162 * vmbus_process_offer - Process the offer by creating a channel/device
163 * associated with this offer
165 static void vmbus_process_offer(struct work_struct *work)
167 struct vmbus_channel *newchannel = container_of(work,
168 struct vmbus_channel,
170 struct vmbus_channel *channel;
175 /* The next possible work is rescind handling */
176 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
178 /* Make sure this is a new offer */
179 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
181 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
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)) {
192 list_add_tail(&newchannel->listentry,
193 &vmbus_connection.chn_list);
195 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
198 free_channel(newchannel);
203 * Start the process of binding this offer to the driver
204 * We need to set the DeviceObject field before calling
205 * vmbus_child_dev_add()
207 newchannel->device_obj = vmbus_device_create(
208 &newchannel->offermsg.offer.if_type,
209 &newchannel->offermsg.offer.if_instance,
213 * Add the new device to the bus. This will kick off device-driver
214 * binding which eventually invokes the device driver's AddDevice()
217 ret = vmbus_device_register(newchannel->device_obj);
219 pr_err("unable to add child device object (relid %d)\n",
220 newchannel->offermsg.child_relid);
222 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
223 list_del(&newchannel->listentry);
224 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
225 kfree(newchannel->device_obj);
227 free_channel(newchannel);
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
234 newchannel->state = CHANNEL_OPEN_STATE;
239 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
242 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
244 struct vmbus_channel_offer_channel *offer;
245 struct vmbus_channel *newchannel;
247 uuid_le *guidinstance;
249 offer = (struct vmbus_channel_offer_channel *)hdr;
251 guidtype = &offer->offer.if_type;
252 guidinstance = &offer->offer.if_instance;
254 /* Allocate the channel object and save this offer. */
255 newchannel = alloc_channel();
257 pr_err("Unable to allocate channel object\n");
261 memcpy(&newchannel->offermsg, offer,
262 sizeof(struct vmbus_channel_offer_channel));
263 newchannel->monitor_grp = (u8)offer->monitorid / 32;
264 newchannel->monitor_bit = (u8)offer->monitorid % 32;
266 INIT_WORK(&newchannel->work, vmbus_process_offer);
267 queue_work(newchannel->controlwq, &newchannel->work);
271 * vmbus_onoffer_rescind - Rescind offer handler.
273 * We queue a work item to process this offer synchronously
275 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
277 struct vmbus_channel_rescind_offer *rescind;
278 struct vmbus_channel *channel;
280 rescind = (struct vmbus_channel_rescind_offer *)hdr;
281 channel = relid2channel(rescind->child_relid);
284 /* Just return here, no channel found */
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);
293 * vmbus_onoffers_delivered -
294 * This is invoked when all offers have been delivered.
296 * Nothing to do here.
298 static void vmbus_onoffers_delivered(
299 struct vmbus_channel_message_header *hdr)
304 * vmbus_onopen_result - Open result handler.
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
310 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
312 struct vmbus_channel_open_result *result;
313 struct vmbus_channel_msginfo *msginfo;
314 struct vmbus_channel_message_header *requestheader;
315 struct vmbus_channel_open_channel *openmsg;
318 result = (struct vmbus_channel_open_result *)hdr;
321 * Find the open msg, copy the result and signal/unblock the wait event
323 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
325 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
328 (struct vmbus_channel_message_header *)msginfo->msg;
330 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
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,
338 struct vmbus_channel_open_result));
339 complete(&msginfo->waitevent);
344 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
348 * vmbus_ongpadl_created - GPADL created handler.
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
354 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
356 struct vmbus_channel_gpadl_created *gpadlcreated;
357 struct vmbus_channel_msginfo *msginfo;
358 struct vmbus_channel_message_header *requestheader;
359 struct vmbus_channel_gpadl_header *gpadlheader;
362 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
365 * Find the establish msg, copy the result and signal/unblock the wait
368 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
370 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
373 (struct vmbus_channel_message_header *)msginfo->msg;
375 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
377 (struct vmbus_channel_gpadl_header *)requestheader;
379 if ((gpadlcreated->child_relid ==
380 gpadlheader->child_relid) &&
381 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
382 memcpy(&msginfo->response.gpadl_created,
385 struct vmbus_channel_gpadl_created));
386 complete(&msginfo->waitevent);
391 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
395 * vmbus_ongpadl_torndown - GPADL torndown handler.
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
401 static void vmbus_ongpadl_torndown(
402 struct vmbus_channel_message_header *hdr)
404 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
405 struct vmbus_channel_msginfo *msginfo;
406 struct vmbus_channel_message_header *requestheader;
407 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
410 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
413 * Find the open msg, copy the result and signal/unblock the wait event
415 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
417 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
420 (struct vmbus_channel_message_header *)msginfo->msg;
422 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
424 (struct vmbus_channel_gpadl_teardown *)requestheader;
426 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
427 memcpy(&msginfo->response.gpadl_torndown,
430 struct vmbus_channel_gpadl_torndown));
431 complete(&msginfo->waitevent);
436 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
440 * vmbus_onversion_response - Version response handler
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
446 static void vmbus_onversion_response(
447 struct vmbus_channel_message_header *hdr)
449 struct vmbus_channel_msginfo *msginfo;
450 struct vmbus_channel_message_header *requestheader;
451 struct vmbus_channel_initiate_contact *initiate;
452 struct vmbus_channel_version_response *version_response;
455 version_response = (struct vmbus_channel_version_response *)hdr;
456 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
458 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
461 (struct vmbus_channel_message_header *)msginfo->msg;
463 if (requestheader->msgtype ==
464 CHANNELMSG_INITIATE_CONTACT) {
466 (struct vmbus_channel_initiate_contact *)requestheader;
467 memcpy(&msginfo->response.version_response,
469 sizeof(struct vmbus_channel_version_response));
470 complete(&msginfo->waitevent);
473 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
476 /* Channel message dispatch table */
477 static struct vmbus_channel_message_table_entry
478 channel_message_table[CHANNELMSG_COUNT] = {
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},
499 * vmbus_onmessage - Handler for channel protocol messages.
501 * This is invoked in the vmbus worker thread context.
503 void vmbus_onmessage(void *context)
505 struct hv_message *msg = context;
506 struct vmbus_channel_message_header *hdr;
509 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
510 size = msg->header.payload_size;
512 if (hdr->msgtype >= CHANNELMSG_COUNT) {
513 pr_err("Received invalid channel message type %d size %d\n",
515 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
516 (unsigned char *)msg->u.payload, size);
520 if (channel_message_table[hdr->msgtype].message_handler)
521 channel_message_table[hdr->msgtype].message_handler(hdr);
523 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
527 * vmbus_request_offers - Send a request to get all our pending offers.
529 int vmbus_request_offers(void)
531 struct vmbus_channel_message_header *msg;
532 struct vmbus_channel_msginfo *msginfo;
535 msginfo = kmalloc(sizeof(*msginfo) +
536 sizeof(struct vmbus_channel_message_header),
541 init_completion(&msginfo->waitevent);
543 msg = (struct vmbus_channel_message_header *)msginfo->msg;
545 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
548 ret = vmbus_post_msg(msg,
549 sizeof(struct vmbus_channel_message_header));
551 pr_err("Unable to request offers - %d\n", ret);
556 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);