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