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>
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
);
40 #define MAX_MSG_TYPES 4
41 #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 8
44 supported_device_classes
[MAX_NUM_DEVICE_CLASSES_SUPPORTED
] = {
45 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
49 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
50 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
54 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
58 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
59 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
63 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
67 0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
68 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A
72 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
76 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
77 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5
80 /* 0E0B6031-5213-4934-818B-38D90CED39DB */
84 0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
85 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB
88 /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */
92 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
93 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf
96 /* {57164f39-9115-4e78-ab55-382f3bd5422d} */
100 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
101 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d
104 /* {A9A0F4E7-5A45-4d96-B827-8A841E8C03E6} */
108 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
109 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6
117 * prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
118 * @icmsghdrp: Pointer to msg header structure
119 * @icmsg_negotiate: Pointer to negotiate message structure
120 * @buf: Raw buffer channel data
122 * @icmsghdrp is of type &struct icmsg_hdr.
123 * @negop is of type &struct icmsg_negotiate.
124 * Set up and fill in default negotiate response message. This response can
125 * come from both the vmbus driver and the hv_utils driver. The current api
126 * will respond properly to both Windows 2008 and Windows 2008-R2 operating
129 * Mainly used by Hyper-V drivers.
131 void prep_negotiate_resp(struct icmsg_hdr
*icmsghdrp
,
132 struct icmsg_negotiate
*negop
,
135 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
136 icmsghdrp
->icmsgsize
= 0x10;
138 negop
= (struct icmsg_negotiate
*)&buf
[
139 sizeof(struct vmbuspipe_hdr
) +
140 sizeof(struct icmsg_hdr
)];
142 if (negop
->icframe_vercnt
== 2 &&
143 negop
->icversion_data
[1].major
== 3) {
144 negop
->icversion_data
[0].major
= 3;
145 negop
->icversion_data
[0].minor
= 0;
146 negop
->icversion_data
[1].major
= 3;
147 negop
->icversion_data
[1].minor
= 0;
149 negop
->icversion_data
[0].major
= 1;
150 negop
->icversion_data
[0].minor
= 0;
151 negop
->icversion_data
[1].major
= 1;
152 negop
->icversion_data
[1].minor
= 0;
155 negop
->icframe_vercnt
= 1;
156 negop
->icmsg_vercnt
= 1;
159 EXPORT_SYMBOL(prep_negotiate_resp
);
162 * alloc_channel - Allocate and initialize a vmbus channel object
164 static struct vmbus_channel
*alloc_channel(void)
166 struct vmbus_channel
*channel
;
168 channel
= kzalloc(sizeof(*channel
), GFP_ATOMIC
);
172 spin_lock_init(&channel
->inbound_lock
);
174 channel
->controlwq
= create_workqueue("hv_vmbus_ctl");
175 if (!channel
->controlwq
) {
184 * release_hannel - Release the vmbus channel object itself
186 static void release_channel(struct work_struct
*work
)
188 struct vmbus_channel
*channel
= container_of(work
,
189 struct vmbus_channel
,
192 destroy_workqueue(channel
->controlwq
);
198 * free_channel - Release the resources used by the vmbus channel object
200 void free_channel(struct vmbus_channel
*channel
)
204 * We have to release the channel's workqueue/thread in the vmbus's
205 * workqueue/thread context
206 * ie we can't destroy ourselves.
208 INIT_WORK(&channel
->work
, release_channel
);
209 queue_work(vmbus_connection
.work_queue
, &channel
->work
);
215 * vmbus_process_rescind_offer -
216 * Rescind the offer by initiating a device removal
218 static void vmbus_process_rescind_offer(struct work_struct
*work
)
220 struct vmbus_channel
*channel
= container_of(work
,
221 struct vmbus_channel
,
224 vmbus_device_unregister(channel
->device_obj
);
228 * vmbus_process_offer - Process the offer by creating a channel/device
229 * associated with this offer
231 static void vmbus_process_offer(struct work_struct
*work
)
233 struct vmbus_channel
*newchannel
= container_of(work
,
234 struct vmbus_channel
,
236 struct vmbus_channel
*channel
;
241 /* The next possible work is rescind handling */
242 INIT_WORK(&newchannel
->work
, vmbus_process_rescind_offer
);
244 /* Make sure this is a new offer */
245 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
247 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
248 if (!uuid_le_cmp(channel
->offermsg
.offer
.if_type
,
249 newchannel
->offermsg
.offer
.if_type
) &&
250 !uuid_le_cmp(channel
->offermsg
.offer
.if_instance
,
251 newchannel
->offermsg
.offer
.if_instance
)) {
258 list_add_tail(&newchannel
->listentry
,
259 &vmbus_connection
.chn_list
);
261 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
264 free_channel(newchannel
);
269 * Start the process of binding this offer to the driver
270 * We need to set the DeviceObject field before calling
271 * vmbus_child_dev_add()
273 newchannel
->device_obj
= vmbus_device_create(
274 &newchannel
->offermsg
.offer
.if_type
,
275 &newchannel
->offermsg
.offer
.if_instance
,
279 * Add the new device to the bus. This will kick off device-driver
280 * binding which eventually invokes the device driver's AddDevice()
283 ret
= vmbus_device_register(newchannel
->device_obj
);
285 pr_err("unable to add child device object (relid %d)\n",
286 newchannel
->offermsg
.child_relid
);
288 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
289 list_del(&newchannel
->listentry
);
290 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
292 free_channel(newchannel
);
295 * This state is used to indicate a successful open
296 * so that when we do close the channel normally, we
297 * can cleanup properly
299 newchannel
->state
= CHANNEL_OPEN_STATE
;
304 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
307 static void vmbus_onoffer(struct vmbus_channel_message_header
*hdr
)
309 struct vmbus_channel_offer_channel
*offer
;
310 struct vmbus_channel
*newchannel
;
312 uuid_le
*guidinstance
;
316 offer
= (struct vmbus_channel_offer_channel
*)hdr
;
317 for (i
= 0; i
< MAX_NUM_DEVICE_CLASSES_SUPPORTED
; i
++) {
318 if (!uuid_le_cmp(offer
->offer
.if_type
,
319 supported_device_classes
[i
])) {
328 guidtype
= &offer
->offer
.if_type
;
329 guidinstance
= &offer
->offer
.if_instance
;
331 /* Allocate the channel object and save this offer. */
332 newchannel
= alloc_channel();
334 pr_err("Unable to allocate channel object\n");
338 memcpy(&newchannel
->offermsg
, offer
,
339 sizeof(struct vmbus_channel_offer_channel
));
340 newchannel
->monitor_grp
= (u8
)offer
->monitorid
/ 32;
341 newchannel
->monitor_bit
= (u8
)offer
->monitorid
% 32;
343 INIT_WORK(&newchannel
->work
, vmbus_process_offer
);
344 queue_work(newchannel
->controlwq
, &newchannel
->work
);
348 * vmbus_onoffer_rescind - Rescind offer handler.
350 * We queue a work item to process this offer synchronously
352 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header
*hdr
)
354 struct vmbus_channel_rescind_offer
*rescind
;
355 struct vmbus_channel
*channel
;
357 rescind
= (struct vmbus_channel_rescind_offer
*)hdr
;
358 channel
= relid2channel(rescind
->child_relid
);
361 /* Just return here, no channel found */
364 /* work is initialized for vmbus_process_rescind_offer() from
365 * vmbus_process_offer() where the channel got created */
366 queue_work(channel
->controlwq
, &channel
->work
);
370 * vmbus_onoffers_delivered -
371 * This is invoked when all offers have been delivered.
373 * Nothing to do here.
375 static void vmbus_onoffers_delivered(
376 struct vmbus_channel_message_header
*hdr
)
381 * vmbus_onopen_result - Open result handler.
383 * This is invoked when we received a response to our channel open request.
384 * Find the matching request, copy the response and signal the requesting
387 static void vmbus_onopen_result(struct vmbus_channel_message_header
*hdr
)
389 struct vmbus_channel_open_result
*result
;
390 struct vmbus_channel_msginfo
*msginfo
;
391 struct vmbus_channel_message_header
*requestheader
;
392 struct vmbus_channel_open_channel
*openmsg
;
395 result
= (struct vmbus_channel_open_result
*)hdr
;
398 * Find the open msg, copy the result and signal/unblock the wait event
400 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
402 list_for_each_entry(msginfo
, &vmbus_connection
.chn_msg_list
,
405 (struct vmbus_channel_message_header
*)msginfo
->msg
;
407 if (requestheader
->msgtype
== CHANNELMSG_OPENCHANNEL
) {
409 (struct vmbus_channel_open_channel
*)msginfo
->msg
;
410 if (openmsg
->child_relid
== result
->child_relid
&&
411 openmsg
->openid
== result
->openid
) {
412 memcpy(&msginfo
->response
.open_result
,
415 struct vmbus_channel_open_result
));
416 complete(&msginfo
->waitevent
);
421 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
425 * vmbus_ongpadl_created - GPADL created handler.
427 * This is invoked when we received a response to our gpadl create request.
428 * Find the matching request, copy the response and signal the requesting
431 static void vmbus_ongpadl_created(struct vmbus_channel_message_header
*hdr
)
433 struct vmbus_channel_gpadl_created
*gpadlcreated
;
434 struct vmbus_channel_msginfo
*msginfo
;
435 struct vmbus_channel_message_header
*requestheader
;
436 struct vmbus_channel_gpadl_header
*gpadlheader
;
439 gpadlcreated
= (struct vmbus_channel_gpadl_created
*)hdr
;
442 * Find the establish msg, copy the result and signal/unblock the wait
445 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
447 list_for_each_entry(msginfo
, &vmbus_connection
.chn_msg_list
,
450 (struct vmbus_channel_message_header
*)msginfo
->msg
;
452 if (requestheader
->msgtype
== CHANNELMSG_GPADL_HEADER
) {
454 (struct vmbus_channel_gpadl_header
*)requestheader
;
456 if ((gpadlcreated
->child_relid
==
457 gpadlheader
->child_relid
) &&
458 (gpadlcreated
->gpadl
== gpadlheader
->gpadl
)) {
459 memcpy(&msginfo
->response
.gpadl_created
,
462 struct vmbus_channel_gpadl_created
));
463 complete(&msginfo
->waitevent
);
468 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
472 * vmbus_ongpadl_torndown - GPADL torndown handler.
474 * This is invoked when we received a response to our gpadl teardown request.
475 * Find the matching request, copy the response and signal the requesting
478 static void vmbus_ongpadl_torndown(
479 struct vmbus_channel_message_header
*hdr
)
481 struct vmbus_channel_gpadl_torndown
*gpadl_torndown
;
482 struct vmbus_channel_msginfo
*msginfo
;
483 struct vmbus_channel_message_header
*requestheader
;
484 struct vmbus_channel_gpadl_teardown
*gpadl_teardown
;
487 gpadl_torndown
= (struct vmbus_channel_gpadl_torndown
*)hdr
;
490 * Find the open msg, copy the result and signal/unblock the wait event
492 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
494 list_for_each_entry(msginfo
, &vmbus_connection
.chn_msg_list
,
497 (struct vmbus_channel_message_header
*)msginfo
->msg
;
499 if (requestheader
->msgtype
== CHANNELMSG_GPADL_TEARDOWN
) {
501 (struct vmbus_channel_gpadl_teardown
*)requestheader
;
503 if (gpadl_torndown
->gpadl
== gpadl_teardown
->gpadl
) {
504 memcpy(&msginfo
->response
.gpadl_torndown
,
507 struct vmbus_channel_gpadl_torndown
));
508 complete(&msginfo
->waitevent
);
513 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
517 * vmbus_onversion_response - Version response handler
519 * This is invoked when we received a response to our initiate contact request.
520 * Find the matching request, copy the response and signal the requesting
523 static void vmbus_onversion_response(
524 struct vmbus_channel_message_header
*hdr
)
526 struct vmbus_channel_msginfo
*msginfo
;
527 struct vmbus_channel_message_header
*requestheader
;
528 struct vmbus_channel_initiate_contact
*initiate
;
529 struct vmbus_channel_version_response
*version_response
;
532 version_response
= (struct vmbus_channel_version_response
*)hdr
;
533 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
535 list_for_each_entry(msginfo
, &vmbus_connection
.chn_msg_list
,
538 (struct vmbus_channel_message_header
*)msginfo
->msg
;
540 if (requestheader
->msgtype
==
541 CHANNELMSG_INITIATE_CONTACT
) {
543 (struct vmbus_channel_initiate_contact
*)requestheader
;
544 memcpy(&msginfo
->response
.version_response
,
546 sizeof(struct vmbus_channel_version_response
));
547 complete(&msginfo
->waitevent
);
550 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
553 /* Channel message dispatch table */
554 static struct vmbus_channel_message_table_entry
555 channel_message_table
[CHANNELMSG_COUNT
] = {
556 {CHANNELMSG_INVALID
, NULL
},
557 {CHANNELMSG_OFFERCHANNEL
, vmbus_onoffer
},
558 {CHANNELMSG_RESCIND_CHANNELOFFER
, vmbus_onoffer_rescind
},
559 {CHANNELMSG_REQUESTOFFERS
, NULL
},
560 {CHANNELMSG_ALLOFFERS_DELIVERED
, vmbus_onoffers_delivered
},
561 {CHANNELMSG_OPENCHANNEL
, NULL
},
562 {CHANNELMSG_OPENCHANNEL_RESULT
, vmbus_onopen_result
},
563 {CHANNELMSG_CLOSECHANNEL
, NULL
},
564 {CHANNELMSG_GPADL_HEADER
, NULL
},
565 {CHANNELMSG_GPADL_BODY
, NULL
},
566 {CHANNELMSG_GPADL_CREATED
, vmbus_ongpadl_created
},
567 {CHANNELMSG_GPADL_TEARDOWN
, NULL
},
568 {CHANNELMSG_GPADL_TORNDOWN
, vmbus_ongpadl_torndown
},
569 {CHANNELMSG_RELID_RELEASED
, NULL
},
570 {CHANNELMSG_INITIATE_CONTACT
, NULL
},
571 {CHANNELMSG_VERSION_RESPONSE
, vmbus_onversion_response
},
572 {CHANNELMSG_UNLOAD
, NULL
},
576 * vmbus_onmessage - Handler for channel protocol messages.
578 * This is invoked in the vmbus worker thread context.
580 void vmbus_onmessage(void *context
)
582 struct hv_message
*msg
= context
;
583 struct vmbus_channel_message_header
*hdr
;
586 hdr
= (struct vmbus_channel_message_header
*)msg
->u
.payload
;
587 size
= msg
->header
.payload_size
;
589 if (hdr
->msgtype
>= CHANNELMSG_COUNT
) {
590 pr_err("Received invalid channel message type %d size %d\n",
592 print_hex_dump_bytes("", DUMP_PREFIX_NONE
,
593 (unsigned char *)msg
->u
.payload
, size
);
597 if (channel_message_table
[hdr
->msgtype
].message_handler
)
598 channel_message_table
[hdr
->msgtype
].message_handler(hdr
);
600 pr_err("Unhandled channel message type %d\n", hdr
->msgtype
);
604 * vmbus_request_offers - Send a request to get all our pending offers.
606 int vmbus_request_offers(void)
608 struct vmbus_channel_message_header
*msg
;
609 struct vmbus_channel_msginfo
*msginfo
;
612 msginfo
= kmalloc(sizeof(*msginfo
) +
613 sizeof(struct vmbus_channel_message_header
),
618 init_completion(&msginfo
->waitevent
);
620 msg
= (struct vmbus_channel_message_header
*)msginfo
->msg
;
622 msg
->msgtype
= CHANNELMSG_REQUESTOFFERS
;
625 ret
= vmbus_post_msg(msg
,
626 sizeof(struct vmbus_channel_message_header
));
628 pr_err("Unable to request offers - %d\n", ret
);
633 t
= wait_for_completion_timeout(&msginfo
->waitevent
, 5*HZ
);