]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/hv/ChannelMgmt.c
Staging: hv: Add Time Sync feature to hv_utils module.
[net-next-2.6.git] / drivers / staging / hv / ChannelMgmt.c
CommitLineData
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 */
a0086dc5
GKH
21#include <linux/kernel.h>
22#include <linux/mm.h>
5a0e3ad6 23#include <linux/slab.h>
53af545b 24#include <linux/list.h>
c88c4e4c 25#include <linux/module.h>
4983b39a 26#include "osd.h"
645954c5 27#include "logging.h"
3e7ee490 28#include "VmbusPrivate.h"
c88c4e4c 29#include "utils.h"
3e7ee490 30
1d7e907f 31struct vmbus_channel_message_table_entry {
c8212f04
GKH
32 enum vmbus_channel_message_type messageType;
33 void (*messageHandler)(struct vmbus_channel_message_header *msg);
1d7e907f 34};
3e7ee490 35
39c4e9c3
HZ
36#define MAX_MSG_TYPES 2
37#define MAX_NUM_DEVICE_CLASSES_SUPPORTED 6
c88c4e4c 38
bd60c33e 39static const struct hv_guid
c88c4e4c 40 gSupportedDeviceClasses[MAX_NUM_DEVICE_CLASSES_SUPPORTED] = {
454f18a9 41 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
caf26a31
GKH
42 /* Storage - SCSI */
43 {
44 .data = {
45 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
46 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
47 }
48 },
49
454f18a9 50 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
caf26a31
GKH
51 /* Network */
52 {
53 .data = {
54 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
55 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
56 }
57 },
58
454f18a9 59 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
caf26a31
GKH
60 /* Input */
61 {
62 .data = {
63 0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
64 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A
65 }
66 },
3e7ee490 67
caf26a31
GKH
68 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
69 /* IDE */
70 {
71 .data = {
72 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
73 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5
74 }
75 },
c88c4e4c
HJ
76 /* 0E0B6031-5213-4934-818B-38D90CED39DB */
77 /* Shutdown */
78 {
79 .data = {
80 0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
81 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB
82 }
83 },
39c4e9c3
HZ
84 /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */
85 /* TimeSync */
86 {
87 .data = {
88 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
89 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf
90 }
91 },
3e7ee490
HJ
92};
93
c88c4e4c
HJ
94
95/**
96 * prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
97 * @icmsghdrp: Pointer to msg header structure
98 * @icmsg_negotiate: Pointer to negotiate message structure
99 * @buf: Raw buffer channel data
100 *
101 * @icmsghdrp is of type &struct icmsg_hdr.
102 * @negop is of type &struct icmsg_negotiate.
103 * Set up and fill in default negotiate response message. This response can
104 * come from both the vmbus driver and the hv_utils driver. The current api
105 * will respond properly to both Windows 2008 and Windows 2008-R2 operating
106 * systems.
107 *
108 * Mainly used by Hyper-V drivers.
109 */
110void prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
111 struct icmsg_negotiate *negop,
112 u8 *buf)
113{
114 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
115 icmsghdrp->icmsgsize = 0x10;
116
117 negop = (struct icmsg_negotiate *)&buf[
118 sizeof(struct vmbuspipe_hdr) +
119 sizeof(struct icmsg_hdr)];
120
121 if (negop->icframe_vercnt == 2 &&
122 negop->icversion_data[1].major == 3) {
123 negop->icversion_data[0].major = 3;
124 negop->icversion_data[0].minor = 0;
125 negop->icversion_data[1].major = 3;
126 negop->icversion_data[1].minor = 0;
127 } else {
128 negop->icversion_data[0].major = 1;
129 negop->icversion_data[0].minor = 0;
130 negop->icversion_data[1].major = 1;
131 negop->icversion_data[1].minor = 0;
132 }
133
134 negop->icframe_vercnt = 1;
135 negop->icmsg_vercnt = 1;
136 }
137}
138EXPORT_SYMBOL(prep_negotiate_resp);
139
140/**
141 * chn_cb_negotiate() - Default handler for non IDE/SCSI/NETWORK
142 * Hyper-V requests
143 * @context: Pointer to argument structure.
144 *
145 * Set up the default handler for non device driver specific requests
146 * from Hyper-V. This stub responds to the default negotiate messages
147 * that come in for every non IDE/SCSI/Network request.
148 * This behavior is normally overwritten in the hv_utils driver. That
149 * driver handles requests like gracefull shutdown, heartbeats etc.
150 *
151 * Mainly used by Hyper-V drivers.
152 */
153void chn_cb_negotiate(void *context)
154{
155 struct vmbus_channel *channel = context;
156 u8 *buf;
157 u32 buflen, recvlen;
158 u64 requestid;
159
160 struct icmsg_hdr *icmsghdrp;
161 struct icmsg_negotiate *negop = NULL;
162
163 buflen = PAGE_SIZE;
164 buf = kmalloc(buflen, GFP_ATOMIC);
165
166 VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid);
167
168 if (recvlen > 0) {
169 icmsghdrp = (struct icmsg_hdr *)&buf[
170 sizeof(struct vmbuspipe_hdr)];
171
172 prep_negotiate_resp(icmsghdrp, negop, buf);
173
174 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
175 | ICMSGHDRFLAG_RESPONSE;
176
177 VmbusChannelSendPacket(channel, buf,
178 recvlen, requestid,
179 VmbusPacketTypeDataInBand, 0);
180 }
181
182 kfree(buf);
183}
184EXPORT_SYMBOL(chn_cb_negotiate);
185
186/*
187 * Function table used for message responses for non IDE/SCSI/Network type
188 * messages. (Such as KVP/Shutdown etc)
189 */
190struct hyperv_service_callback hv_cb_utils[MAX_MSG_TYPES] = {
191 /* 0E0B6031-5213-4934-818B-38D90CED39DB */
192 /* Shutdown */
193 {
194 .msg_type = HV_SHUTDOWN_MSG,
195 .data = {
196 0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
197 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB
198 },
199 .callback = chn_cb_negotiate,
200 .log_msg = "Shutdown channel functionality initialized"
201 },
39c4e9c3
HZ
202
203 /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */
204 /* TimeSync */
205 {
206 .msg_type = HV_TIMESYNC_MSG,
207 .data = {
208 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
209 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf
210 },
211 .callback = chn_cb_negotiate,
212 .log_msg = "Timesync channel functionality initialized"
213 },
c88c4e4c
HJ
214};
215EXPORT_SYMBOL(hv_cb_utils);
216
3e189519 217/*
bd60c33e
GKH
218 * AllocVmbusChannel - Allocate and initialize a vmbus channel object
219 */
aded7165 220struct vmbus_channel *AllocVmbusChannel(void)
3e7ee490 221{
aded7165 222 struct vmbus_channel *channel;
3e7ee490 223
aded7165 224 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
3e7ee490 225 if (!channel)
3e7ee490 226 return NULL;
3e7ee490 227
54411c42 228 spin_lock_init(&channel->inbound_lock);
3e7ee490 229
c8a429a4
GKH
230 init_timer(&channel->poll_timer);
231 channel->poll_timer.data = (unsigned long)channel;
232 channel->poll_timer.function = VmbusChannelOnTimer;
3e7ee490 233
de65a384 234 channel->ControlWQ = create_workqueue("hv_vmbus_ctl");
bd60c33e 235 if (!channel->ControlWQ) {
8c69f52a 236 kfree(channel);
3e7ee490
HJ
237 return NULL;
238 }
239
240 return channel;
241}
242
3e189519 243/*
bd60c33e
GKH
244 * ReleaseVmbusChannel - Release the vmbus channel object itself
245 */
246static inline void ReleaseVmbusChannel(void *context)
3e7ee490 247{
bd60c33e 248 struct vmbus_channel *channel = context;
3e7ee490
HJ
249
250 DPRINT_ENTER(VMBUS);
251
252 DPRINT_DBG(VMBUS, "releasing channel (%p)", channel);
de65a384 253 destroy_workqueue(channel->ControlWQ);
3e7ee490
HJ
254 DPRINT_DBG(VMBUS, "channel released (%p)", channel);
255
8c69f52a 256 kfree(channel);
3e7ee490
HJ
257
258 DPRINT_EXIT(VMBUS);
259}
260
3e189519 261/*
bd60c33e
GKH
262 * FreeVmbusChannel - Release the resources used by the vmbus channel object
263 */
aded7165 264void FreeVmbusChannel(struct vmbus_channel *Channel)
3e7ee490 265{
5996b3dd 266 del_timer_sync(&Channel->poll_timer);
3e7ee490 267
bd60c33e
GKH
268 /*
269 * We have to release the channel's workqueue/thread in the vmbus's
270 * workqueue/thread context
271 * ie we can't destroy ourselves.
272 */
de65a384 273 osd_schedule_callback(gVmbusConnection.WorkQueue, ReleaseVmbusChannel,
bd60c33e 274 Channel);
3e7ee490
HJ
275}
276
3e189519 277/*
c88c4e4c
HJ
278 * VmbusChannelProcessOffer - Process the offer by creating a channel/device
279 * associated with this offer
bd60c33e
GKH
280 */
281static void VmbusChannelProcessOffer(void *context)
3e7ee490 282{
aded7165
GKH
283 struct vmbus_channel *newChannel = context;
284 struct vmbus_channel *channel;
0e727613 285 bool fNew = true;
bd60c33e 286 int ret;
c88c4e4c 287 int cnt;
0f5e44ca 288 unsigned long flags;
3e7ee490
HJ
289
290 DPRINT_ENTER(VMBUS);
291
454f18a9 292 /* Make sure this is a new offer */
0f5e44ca 293 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
3e7ee490 294
53af545b 295 list_for_each_entry(channel, &gVmbusConnection.ChannelList, ListEntry) {
bd60c33e
GKH
296 if (!memcmp(&channel->OfferMsg.Offer.InterfaceType,
297 &newChannel->OfferMsg.Offer.InterfaceType,
298 sizeof(struct hv_guid)) &&
299 !memcmp(&channel->OfferMsg.Offer.InterfaceInstance,
300 &newChannel->OfferMsg.Offer.InterfaceInstance,
301 sizeof(struct hv_guid))) {
0e727613 302 fNew = false;
3e7ee490
HJ
303 break;
304 }
305 }
306
307 if (fNew)
53af545b
BP
308 list_add_tail(&newChannel->ListEntry,
309 &gVmbusConnection.ChannelList);
bd60c33e 310
0f5e44ca 311 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
3e7ee490 312
bd60c33e
GKH
313 if (!fNew) {
314 DPRINT_DBG(VMBUS, "Ignoring duplicate offer for relid (%d)",
315 newChannel->OfferMsg.ChildRelId);
3e7ee490
HJ
316 FreeVmbusChannel(newChannel);
317 DPRINT_EXIT(VMBUS);
318 return;
319 }
320
bd60c33e
GKH
321 /*
322 * Start the process of binding this offer to the driver
323 * We need to set the DeviceObject field before calling
324 * VmbusChildDeviceAdd()
325 */
3e7ee490 326 newChannel->DeviceObject = VmbusChildDeviceCreate(
daaa8cc3
GKH
327 &newChannel->OfferMsg.Offer.InterfaceType,
328 &newChannel->OfferMsg.Offer.InterfaceInstance,
3e7ee490
HJ
329 newChannel);
330
bd60c33e
GKH
331 DPRINT_DBG(VMBUS, "child device object allocated - %p",
332 newChannel->DeviceObject);
3e7ee490 333
454f18a9
BP
334 /*
335 * Add the new device to the bus. This will kick off device-driver
336 * binding which eventually invokes the device driver's AddDevice()
337 * method.
338 */
3e7ee490 339 ret = VmbusChildDeviceAdd(newChannel->DeviceObject);
bd60c33e
GKH
340 if (ret != 0) {
341 DPRINT_ERR(VMBUS,
342 "unable to add child device object (relid %d)",
343 newChannel->OfferMsg.ChildRelId);
3e7ee490 344
0f5e44ca 345 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
53af545b 346 list_del(&newChannel->ListEntry);
0f5e44ca 347 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
3e7ee490
HJ
348
349 FreeVmbusChannel(newChannel);
bd60c33e 350 } else {
454f18a9
BP
351 /*
352 * This state is used to indicate a successful open
353 * so that when we do close the channel normally, we
354 * can cleanup properly
355 */
3e7ee490 356 newChannel->State = CHANNEL_OPEN_STATE;
c88c4e4c
HJ
357 cnt = 0;
358
359 while (cnt != MAX_MSG_TYPES) {
360 if (memcmp(&newChannel->OfferMsg.Offer.InterfaceType,
361 &hv_cb_utils[cnt].data,
362 sizeof(struct hv_guid)) == 0) {
363 DPRINT_INFO(VMBUS, "%s",
364 hv_cb_utils[cnt].log_msg);
365
366 if (VmbusChannelOpen(newChannel, 2 * PAGE_SIZE,
367 2 * PAGE_SIZE, NULL, 0,
368 hv_cb_utils[cnt].callback,
369 newChannel) == 0)
370 hv_cb_utils[cnt].channel = newChannel;
371 }
372 cnt++;
373 }
3e7ee490
HJ
374 }
375 DPRINT_EXIT(VMBUS);
376}
377
3e189519 378/*
bd60c33e
GKH
379 * VmbusChannelProcessRescindOffer - Rescind the offer by initiating a device removal
380 */
381static void VmbusChannelProcessRescindOffer(void *context)
3e7ee490 382{
aded7165 383 struct vmbus_channel *channel = context;
3e7ee490
HJ
384
385 DPRINT_ENTER(VMBUS);
3e7ee490 386 VmbusChildDeviceRemove(channel->DeviceObject);
3e7ee490
HJ
387 DPRINT_EXIT(VMBUS);
388}
389
3e189519 390/*
bd60c33e
GKH
391 * VmbusChannelOnOffer - Handler for channel offers from vmbus in parent partition.
392 *
393 * We ignore all offers except network and storage offers. For each network and
394 * storage offers, we create a channel object and queue a work item to the
395 * channel object to process the offer synchronously
396 */
82250213 397static void VmbusChannelOnOffer(struct vmbus_channel_message_header *hdr)
3e7ee490 398{
bd60c33e 399 struct vmbus_channel_offer_channel *offer;
aded7165 400 struct vmbus_channel *newChannel;
caf26a31
GKH
401 struct hv_guid *guidType;
402 struct hv_guid *guidInstance;
3e7ee490 403 int i;
bd60c33e 404 int fSupported = 0;
3e7ee490
HJ
405
406 DPRINT_ENTER(VMBUS);
407
bd60c33e
GKH
408 offer = (struct vmbus_channel_offer_channel *)hdr;
409 for (i = 0; i < MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++) {
410 if (memcmp(&offer->Offer.InterfaceType,
411 &gSupportedDeviceClasses[i], sizeof(struct hv_guid)) == 0) {
3e7ee490
HJ
412 fSupported = 1;
413 break;
414 }
415 }
416
bd60c33e
GKH
417 if (!fSupported) {
418 DPRINT_DBG(VMBUS, "Ignoring channel offer notification for "
419 "child relid %d", offer->ChildRelId);
3e7ee490 420 DPRINT_EXIT(VMBUS);
3e7ee490
HJ
421 return;
422 }
423
424 guidType = &offer->Offer.InterfaceType;
425 guidInstance = &offer->Offer.InterfaceInstance;
426
bd60c33e
GKH
427 DPRINT_INFO(VMBUS, "Channel offer notification - "
428 "child relid %d monitor id %d allocated %d, "
429 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
430 "%02x%02x%02x%02x%02x%02x%02x%02x} "
431 "instance {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
432 "%02x%02x%02x%02x%02x%02x%02x%02x}",
433 offer->ChildRelId, offer->MonitorId,
434 offer->MonitorAllocated,
435 guidType->data[3], guidType->data[2],
436 guidType->data[1], guidType->data[0],
437 guidType->data[5], guidType->data[4],
438 guidType->data[7], guidType->data[6],
439 guidType->data[8], guidType->data[9],
440 guidType->data[10], guidType->data[11],
441 guidType->data[12], guidType->data[13],
442 guidType->data[14], guidType->data[15],
443 guidInstance->data[3], guidInstance->data[2],
444 guidInstance->data[1], guidInstance->data[0],
445 guidInstance->data[5], guidInstance->data[4],
446 guidInstance->data[7], guidInstance->data[6],
447 guidInstance->data[8], guidInstance->data[9],
448 guidInstance->data[10], guidInstance->data[11],
449 guidInstance->data[12], guidInstance->data[13],
450 guidInstance->data[14], guidInstance->data[15]);
3e7ee490 451
454f18a9 452 /* Allocate the channel object and save this offer. */
3e7ee490 453 newChannel = AllocVmbusChannel();
bd60c33e 454 if (!newChannel) {
3e7ee490
HJ
455 DPRINT_ERR(VMBUS, "unable to allocate channel object");
456 return;
457 }
458
459 DPRINT_DBG(VMBUS, "channel object allocated - %p", newChannel);
460
bd60c33e
GKH
461 memcpy(&newChannel->OfferMsg, offer,
462 sizeof(struct vmbus_channel_offer_channel));
5654e932
GKH
463 newChannel->MonitorGroup = (u8)offer->MonitorId / 32;
464 newChannel->MonitorBit = (u8)offer->MonitorId % 32;
3e7ee490 465
454f18a9 466 /* TODO: Make sure the offer comes from our parent partition */
de65a384
BP
467 osd_schedule_callback(newChannel->ControlWQ, VmbusChannelProcessOffer,
468 newChannel);
3e7ee490
HJ
469
470 DPRINT_EXIT(VMBUS);
471}
472
3e189519 473/*
bd60c33e
GKH
474 * VmbusChannelOnOfferRescind - Rescind offer handler.
475 *
476 * We queue a work item to process this offer synchronously
477 */
82250213 478static void VmbusChannelOnOfferRescind(struct vmbus_channel_message_header *hdr)
3e7ee490 479{
bd60c33e 480 struct vmbus_channel_rescind_offer *rescind;
aded7165 481 struct vmbus_channel *channel;
3e7ee490
HJ
482
483 DPRINT_ENTER(VMBUS);
484
bd60c33e 485 rescind = (struct vmbus_channel_rescind_offer *)hdr;
3e7ee490 486 channel = GetChannelFromRelId(rescind->ChildRelId);
bd60c33e
GKH
487 if (channel == NULL) {
488 DPRINT_DBG(VMBUS, "channel not found for relId %d",
489 rescind->ChildRelId);
3e7ee490
HJ
490 return;
491 }
492
de65a384
BP
493 osd_schedule_callback(channel->ControlWQ,
494 VmbusChannelProcessRescindOffer,
495 channel);
3e7ee490
HJ
496
497 DPRINT_EXIT(VMBUS);
498}
499
3e189519 500/*
bd60c33e
GKH
501 * VmbusChannelOnOffersDelivered - This is invoked when all offers have been delivered.
502 *
503 * Nothing to do here.
504 */
505static void VmbusChannelOnOffersDelivered(
506 struct vmbus_channel_message_header *hdr)
3e7ee490
HJ
507{
508 DPRINT_ENTER(VMBUS);
509 DPRINT_EXIT(VMBUS);
510}
511
3e189519 512/*
bd60c33e
GKH
513 * VmbusChannelOnOpenResult - Open result handler.
514 *
515 * This is invoked when we received a response to our channel open request.
516 * Find the matching request, copy the response and signal the requesting
517 * thread.
518 */
82250213 519static void VmbusChannelOnOpenResult(struct vmbus_channel_message_header *hdr)
3e7ee490 520{
bd60c33e 521 struct vmbus_channel_open_result *result;
53af545b 522 struct list_head *curr;
aded7165 523 struct vmbus_channel_msginfo *msgInfo;
82250213
GKH
524 struct vmbus_channel_message_header *requestHeader;
525 struct vmbus_channel_open_channel *openMsg;
dd0813b6 526 unsigned long flags;
3e7ee490
HJ
527
528 DPRINT_ENTER(VMBUS);
529
bd60c33e 530 result = (struct vmbus_channel_open_result *)hdr;
3e7ee490
HJ
531 DPRINT_DBG(VMBUS, "vmbus open result - %d", result->Status);
532
bd60c33e
GKH
533 /*
534 * Find the open msg, copy the result and signal/unblock the wait event
535 */
dd0813b6 536 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 537
53af545b
BP
538 list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
539/* FIXME: this should probably use list_entry() instead */
aded7165 540 msgInfo = (struct vmbus_channel_msginfo *)curr;
82250213 541 requestHeader = (struct vmbus_channel_message_header *)msgInfo->Msg;
3e7ee490 542
bd60c33e 543 if (requestHeader->MessageType == ChannelMessageOpenChannel) {
82250213 544 openMsg = (struct vmbus_channel_open_channel *)msgInfo->Msg;
3e7ee490 545 if (openMsg->ChildRelId == result->ChildRelId &&
bd60c33e
GKH
546 openMsg->OpenId == result->OpenId) {
547 memcpy(&msgInfo->Response.OpenResult,
548 result,
549 sizeof(struct vmbus_channel_open_result));
bfc30aae 550 osd_WaitEventSet(msgInfo->WaitEvent);
3e7ee490
HJ
551 break;
552 }
553 }
554 }
dd0813b6 555 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
556
557 DPRINT_EXIT(VMBUS);
558}
559
3e189519 560/*
bd60c33e
GKH
561 * VmbusChannelOnGpadlCreated - GPADL created handler.
562 *
563 * This is invoked when we received a response to our gpadl create request.
564 * Find the matching request, copy the response and signal the requesting
565 * thread.
566 */
82250213 567static void VmbusChannelOnGpadlCreated(struct vmbus_channel_message_header *hdr)
3e7ee490 568{
bd60c33e 569 struct vmbus_channel_gpadl_created *gpadlCreated;
53af545b 570 struct list_head *curr;
aded7165 571 struct vmbus_channel_msginfo *msgInfo;
82250213
GKH
572 struct vmbus_channel_message_header *requestHeader;
573 struct vmbus_channel_gpadl_header *gpadlHeader;
dd0813b6 574 unsigned long flags;
3e7ee490
HJ
575
576 DPRINT_ENTER(VMBUS);
577
bd60c33e
GKH
578 gpadlCreated = (struct vmbus_channel_gpadl_created *)hdr;
579 DPRINT_DBG(VMBUS, "vmbus gpadl created result - %d",
580 gpadlCreated->CreationStatus);
3e7ee490 581
bd60c33e
GKH
582 /*
583 * Find the establish msg, copy the result and signal/unblock the wait
584 * event
585 */
dd0813b6 586 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 587
53af545b
BP
588 list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
589/* FIXME: this should probably use list_entry() instead */
aded7165 590 msgInfo = (struct vmbus_channel_msginfo *)curr;
82250213 591 requestHeader = (struct vmbus_channel_message_header *)msgInfo->Msg;
3e7ee490 592
bd60c33e 593 if (requestHeader->MessageType == ChannelMessageGpadlHeader) {
82250213 594 gpadlHeader = (struct vmbus_channel_gpadl_header *)requestHeader;
3e7ee490 595
bd60c33e
GKH
596 if ((gpadlCreated->ChildRelId ==
597 gpadlHeader->ChildRelId) &&
598 (gpadlCreated->Gpadl == gpadlHeader->Gpadl)) {
599 memcpy(&msgInfo->Response.GpadlCreated,
600 gpadlCreated,
601 sizeof(struct vmbus_channel_gpadl_created));
bfc30aae 602 osd_WaitEventSet(msgInfo->WaitEvent);
3e7ee490
HJ
603 break;
604 }
605 }
606 }
dd0813b6 607 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
608
609 DPRINT_EXIT(VMBUS);
610}
611
3e189519 612/*
bd60c33e
GKH
613 * VmbusChannelOnGpadlTorndown - GPADL torndown handler.
614 *
615 * This is invoked when we received a response to our gpadl teardown request.
616 * Find the matching request, copy the response and signal the requesting
617 * thread.
618 */
619static void VmbusChannelOnGpadlTorndown(
620 struct vmbus_channel_message_header *hdr)
3e7ee490 621{
bd60c33e 622 struct vmbus_channel_gpadl_torndown *gpadlTorndown;
53af545b 623 struct list_head *curr;
aded7165 624 struct vmbus_channel_msginfo *msgInfo;
82250213
GKH
625 struct vmbus_channel_message_header *requestHeader;
626 struct vmbus_channel_gpadl_teardown *gpadlTeardown;
dd0813b6 627 unsigned long flags;
3e7ee490
HJ
628
629 DPRINT_ENTER(VMBUS);
630
bd60c33e
GKH
631 gpadlTorndown = (struct vmbus_channel_gpadl_torndown *)hdr;
632
633 /*
634 * Find the open msg, copy the result and signal/unblock the wait event
635 */
dd0813b6 636 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 637
53af545b
BP
638 list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
639/* FIXME: this should probably use list_entry() instead */
aded7165 640 msgInfo = (struct vmbus_channel_msginfo *)curr;
82250213 641 requestHeader = (struct vmbus_channel_message_header *)msgInfo->Msg;
3e7ee490 642
bd60c33e 643 if (requestHeader->MessageType == ChannelMessageGpadlTeardown) {
82250213 644 gpadlTeardown = (struct vmbus_channel_gpadl_teardown *)requestHeader;
3e7ee490 645
bd60c33e
GKH
646 if (gpadlTorndown->Gpadl == gpadlTeardown->Gpadl) {
647 memcpy(&msgInfo->Response.GpadlTorndown,
648 gpadlTorndown,
649 sizeof(struct vmbus_channel_gpadl_torndown));
bfc30aae 650 osd_WaitEventSet(msgInfo->WaitEvent);
3e7ee490
HJ
651 break;
652 }
653 }
654 }
dd0813b6 655 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
656
657 DPRINT_EXIT(VMBUS);
658}
659
3e189519 660/*
bd60c33e
GKH
661 * VmbusChannelOnVersionResponse - Version response handler
662 *
663 * This is invoked when we received a response to our initiate contact request.
664 * Find the matching request, copy the response and signal the requesting
665 * thread.
666 */
667static void VmbusChannelOnVersionResponse(
668 struct vmbus_channel_message_header *hdr)
3e7ee490 669{
53af545b 670 struct list_head *curr;
aded7165 671 struct vmbus_channel_msginfo *msgInfo;
82250213
GKH
672 struct vmbus_channel_message_header *requestHeader;
673 struct vmbus_channel_initiate_contact *initiate;
bd60c33e 674 struct vmbus_channel_version_response *versionResponse;
dd0813b6 675 unsigned long flags;
3e7ee490
HJ
676
677 DPRINT_ENTER(VMBUS);
678
bd60c33e 679 versionResponse = (struct vmbus_channel_version_response *)hdr;
dd0813b6 680 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 681
53af545b
BP
682 list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
683/* FIXME: this should probably use list_entry() instead */
aded7165 684 msgInfo = (struct vmbus_channel_msginfo *)curr;
82250213 685 requestHeader = (struct vmbus_channel_message_header *)msgInfo->Msg;
3e7ee490 686
bd60c33e
GKH
687 if (requestHeader->MessageType ==
688 ChannelMessageInitiateContact) {
82250213 689 initiate = (struct vmbus_channel_initiate_contact *)requestHeader;
bd60c33e
GKH
690 memcpy(&msgInfo->Response.VersionResponse,
691 versionResponse,
692 sizeof(struct vmbus_channel_version_response));
bfc30aae 693 osd_WaitEventSet(msgInfo->WaitEvent);
3e7ee490
HJ
694 }
695 }
dd0813b6 696 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
697
698 DPRINT_EXIT(VMBUS);
699}
700
c8212f04
GKH
701/* Channel message dispatch table */
702static struct vmbus_channel_message_table_entry
703 gChannelMessageTable[ChannelMessageCount] = {
704 {ChannelMessageInvalid, NULL},
705 {ChannelMessageOfferChannel, VmbusChannelOnOffer},
706 {ChannelMessageRescindChannelOffer, VmbusChannelOnOfferRescind},
707 {ChannelMessageRequestOffers, NULL},
708 {ChannelMessageAllOffersDelivered, VmbusChannelOnOffersDelivered},
709 {ChannelMessageOpenChannel, NULL},
710 {ChannelMessageOpenChannelResult, VmbusChannelOnOpenResult},
711 {ChannelMessageCloseChannel, NULL},
712 {ChannelMessageGpadlHeader, NULL},
713 {ChannelMessageGpadlBody, NULL},
714 {ChannelMessageGpadlCreated, VmbusChannelOnGpadlCreated},
715 {ChannelMessageGpadlTeardown, NULL},
716 {ChannelMessageGpadlTorndown, VmbusChannelOnGpadlTorndown},
717 {ChannelMessageRelIdReleased, NULL},
718 {ChannelMessageInitiateContact, NULL},
719 {ChannelMessageVersionResponse, VmbusChannelOnVersionResponse},
720 {ChannelMessageUnload, NULL},
721};
722
3e189519 723/*
bd60c33e
GKH
724 * VmbusOnChannelMessage - Handler for channel protocol messages.
725 *
726 * This is invoked in the vmbus worker thread context.
727 */
b239549c 728void VmbusOnChannelMessage(void *Context)
3e7ee490 729{
eacb1b4d 730 struct hv_message *msg = Context;
82250213 731 struct vmbus_channel_message_header *hdr;
3e7ee490
HJ
732 int size;
733
734 DPRINT_ENTER(VMBUS);
735
82250213 736 hdr = (struct vmbus_channel_message_header *)msg->u.Payload;
bd60c33e 737 size = msg->Header.PayloadSize;
3e7ee490
HJ
738
739 DPRINT_DBG(VMBUS, "message type %d size %d", hdr->MessageType, size);
740
bd60c33e
GKH
741 if (hdr->MessageType >= ChannelMessageCount) {
742 DPRINT_ERR(VMBUS,
743 "Received invalid channel message type %d size %d",
744 hdr->MessageType, size);
04f50c4d
GKH
745 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
746 (unsigned char *)msg->u.Payload, size);
8c69f52a 747 kfree(msg);
3e7ee490
HJ
748 return;
749 }
750
751 if (gChannelMessageTable[hdr->MessageType].messageHandler)
3e7ee490 752 gChannelMessageTable[hdr->MessageType].messageHandler(hdr);
3e7ee490 753 else
bd60c33e
GKH
754 DPRINT_ERR(VMBUS, "Unhandled channel message type %d",
755 hdr->MessageType);
3e7ee490 756
454f18a9 757 /* Free the msg that was allocated in VmbusOnMsgDPC() */
8c69f52a 758 kfree(msg);
3e7ee490
HJ
759 DPRINT_EXIT(VMBUS);
760}
761
3e189519 762/*
bd60c33e
GKH
763 * VmbusChannelRequestOffers - Send a request to get all our pending offers.
764 */
b239549c 765int VmbusChannelRequestOffers(void)
3e7ee490 766{
82250213 767 struct vmbus_channel_message_header *msg;
aded7165 768 struct vmbus_channel_msginfo *msgInfo;
bd60c33e 769 int ret;
3e7ee490
HJ
770
771 DPRINT_ENTER(VMBUS);
772
bd60c33e
GKH
773 msgInfo = kmalloc(sizeof(*msgInfo) +
774 sizeof(struct vmbus_channel_message_header),
775 GFP_KERNEL);
75910f23
BP
776 if (!msgInfo)
777 return -ENOMEM;
3e7ee490 778
bfc30aae 779 msgInfo->WaitEvent = osd_WaitEventCreate();
75910f23
BP
780 if (!msgInfo->WaitEvent) {
781 kfree(msgInfo);
782 return -ENOMEM;
783 }
784
82250213 785 msg = (struct vmbus_channel_message_header *)msgInfo->Msg;
3e7ee490
HJ
786
787 msg->MessageType = ChannelMessageRequestOffers;
788
789 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
bd60c33e
GKH
790 INSERT_TAIL_LIST(&gVmbusConnection.channelMsgList,
791 &msgInfo->msgListEntry);
3e7ee490
HJ
792 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
793
bd60c33e
GKH
794 ret = VmbusPostMessage(msg,
795 sizeof(struct vmbus_channel_message_header));
796 if (ret != 0) {
3e7ee490
HJ
797 DPRINT_ERR(VMBUS, "Unable to request offers - %d", ret);
798
799 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
800 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
801 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
802
803 goto Cleanup;
804 }
bfc30aae 805 /* osd_WaitEventWait(msgInfo->waitEvent); */
3e7ee490
HJ
806
807 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
808 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
809 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
810
811
812Cleanup:
bd60c33e 813 if (msgInfo) {
420beac4 814 kfree(msgInfo->WaitEvent);
8c69f52a 815 kfree(msgInfo);
3e7ee490
HJ
816 }
817
818 DPRINT_EXIT(VMBUS);
3e7ee490
HJ
819 return ret;
820}
821
3e189519
HJ
822/*
823 * VmbusChannelReleaseUnattachedChannels - Release channels that are
824 * unattached/unconnected ie (no drivers associated)
bd60c33e 825 */
b239549c 826void VmbusChannelReleaseUnattachedChannels(void)
3e7ee490 827{
53af545b 828 struct vmbus_channel *channel, *pos;
aded7165 829 struct vmbus_channel *start = NULL;
0f5e44ca 830 unsigned long flags;
3e7ee490 831
0f5e44ca 832 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
3e7ee490 833
53af545b
BP
834 list_for_each_entry_safe(channel, pos, &gVmbusConnection.ChannelList,
835 ListEntry) {
3e7ee490
HJ
836 if (channel == start)
837 break;
838
bd60c33e 839 if (!channel->DeviceObject->Driver) {
53af545b 840 list_del(&channel->ListEntry);
bd60c33e
GKH
841 DPRINT_INFO(VMBUS,
842 "Releasing unattached device object %p",
843 channel->DeviceObject);
3e7ee490
HJ
844
845 VmbusChildDeviceRemove(channel->DeviceObject);
846 FreeVmbusChannel(channel);
bd60c33e 847 } else {
3e7ee490 848 if (!start)
3e7ee490 849 start = channel;
3e7ee490
HJ
850 }
851 }
852
0f5e44ca 853 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
3e7ee490
HJ
854}
855
454f18a9 856/* eof */