Staging: hv: Implement key/value pair (KVP)
[firefly-linux-kernel-4.4.55.git] / drivers / staging / hv / channel_mgmt.c
1 /*
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>
20  */
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/completion.h>
27 #include "osd.h"
28 #include "logging.h"
29 #include "vmbus_private.h"
30 #include "utils.h"
31
32 struct vmbus_channel_message_table_entry {
33         enum vmbus_channel_message_type messageType;
34         void (*messageHandler)(struct vmbus_channel_message_header *msg);
35 };
36
37 #define MAX_MSG_TYPES                    4
38 #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 8
39
40 static const struct hv_guid
41         gSupportedDeviceClasses[MAX_NUM_DEVICE_CLASSES_SUPPORTED] = {
42         /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
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
51         /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
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
60         /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
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         },
68
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         },
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         },
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         },
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         },
101         /* {A9A0F4E7-5A45-4d96-B827-8A841E8C03E6} */
102         /* KVP */
103         {
104                 .data = {
105                         0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
106                         0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3,  0xe6
107         }
108         },
109
110 };
111
112
113 /**
114  * prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
115  * @icmsghdrp: Pointer to msg header structure
116  * @icmsg_negotiate: Pointer to negotiate message structure
117  * @buf: Raw buffer channel data
118  *
119  * @icmsghdrp is of type &struct icmsg_hdr.
120  * @negop is of type &struct icmsg_negotiate.
121  * Set up and fill in default negotiate response message. This response can
122  * come from both the vmbus driver and the hv_utils driver. The current api
123  * will respond properly to both Windows 2008 and Windows 2008-R2 operating
124  * systems.
125  *
126  * Mainly used by Hyper-V drivers.
127  */
128 void prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
129                              struct icmsg_negotiate *negop,
130                              u8 *buf)
131 {
132         if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
133                 icmsghdrp->icmsgsize = 0x10;
134
135                 negop = (struct icmsg_negotiate *)&buf[
136                         sizeof(struct vmbuspipe_hdr) +
137                         sizeof(struct icmsg_hdr)];
138
139                 if (negop->icframe_vercnt == 2 &&
140                    negop->icversion_data[1].major == 3) {
141                         negop->icversion_data[0].major = 3;
142                         negop->icversion_data[0].minor = 0;
143                         negop->icversion_data[1].major = 3;
144                         negop->icversion_data[1].minor = 0;
145                 } else {
146                         negop->icversion_data[0].major = 1;
147                         negop->icversion_data[0].minor = 0;
148                         negop->icversion_data[1].major = 1;
149                         negop->icversion_data[1].minor = 0;
150                 }
151
152                 negop->icframe_vercnt = 1;
153                 negop->icmsg_vercnt = 1;
154         }
155 }
156 EXPORT_SYMBOL(prep_negotiate_resp);
157
158 /**
159  * chn_cb_negotiate() - Default handler for non IDE/SCSI/NETWORK
160  * Hyper-V requests
161  * @context: Pointer to argument structure.
162  *
163  * Set up the default handler for non device driver specific requests
164  * from Hyper-V. This stub responds to the default negotiate messages
165  * that come in for every non IDE/SCSI/Network request.
166  * This behavior is normally overwritten in the hv_utils driver. That
167  * driver handles requests like gracefull shutdown, heartbeats etc.
168  *
169  * Mainly used by Hyper-V drivers.
170  */
171 void chn_cb_negotiate(void *context)
172 {
173         struct vmbus_channel *channel = context;
174         u8 *buf;
175         u32 buflen, recvlen;
176         u64 requestid;
177
178         struct icmsg_hdr *icmsghdrp;
179         struct icmsg_negotiate *negop = NULL;
180
181         buflen = PAGE_SIZE;
182         buf = kmalloc(buflen, GFP_ATOMIC);
183
184         vmbus_recvpacket(channel, buf, buflen, &recvlen, &requestid);
185
186         if (recvlen > 0) {
187                 icmsghdrp = (struct icmsg_hdr *)&buf[
188                         sizeof(struct vmbuspipe_hdr)];
189
190                 prep_negotiate_resp(icmsghdrp, negop, buf);
191
192                 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
193                         | ICMSGHDRFLAG_RESPONSE;
194
195                 vmbus_sendpacket(channel, buf,
196                                        recvlen, requestid,
197                                        VmbusPacketTypeDataInBand, 0);
198         }
199
200         kfree(buf);
201 }
202 EXPORT_SYMBOL(chn_cb_negotiate);
203
204 /*
205  * Function table used for message responses for non IDE/SCSI/Network type
206  * messages. (Such as KVP/Shutdown etc)
207  */
208 struct hyperv_service_callback hv_cb_utils[MAX_MSG_TYPES] = {
209         /* 0E0B6031-5213-4934-818B-38D90CED39DB */
210         /* Shutdown */
211         {
212                 .msg_type = HV_SHUTDOWN_MSG,
213                 .data = {
214                         0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
215                         0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB
216                 },
217                 .callback = chn_cb_negotiate,
218                 .log_msg = "Shutdown channel functionality initialized"
219         },
220
221         /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */
222         /* TimeSync */
223         {
224                 .msg_type = HV_TIMESYNC_MSG,
225                 .data = {
226                         0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
227                         0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf
228                 },
229                 .callback = chn_cb_negotiate,
230                 .log_msg = "Timesync channel functionality initialized"
231         },
232         /* {57164f39-9115-4e78-ab55-382f3bd5422d} */
233         /* Heartbeat */
234         {
235                 .msg_type = HV_HEARTBEAT_MSG,
236                 .data = {
237                         0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
238                         0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d
239                 },
240                 .callback = chn_cb_negotiate,
241                 .log_msg = "Heartbeat channel functionality initialized"
242         },
243         /* {A9A0F4E7-5A45-4d96-B827-8A841E8C03E6} */
244         /* KVP */
245         {
246                 .data = {
247                         0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
248                         0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3,  0xe6
249                 },
250                 .callback = chn_cb_negotiate,
251                 .log_msg = "KVP channel functionality initialized"
252         },
253 };
254 EXPORT_SYMBOL(hv_cb_utils);
255
256 /*
257  * alloc_channel - Allocate and initialize a vmbus channel object
258  */
259 static struct vmbus_channel *alloc_channel(void)
260 {
261         struct vmbus_channel *channel;
262
263         channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
264         if (!channel)
265                 return NULL;
266
267         spin_lock_init(&channel->inbound_lock);
268
269         init_timer(&channel->poll_timer);
270         channel->poll_timer.data = (unsigned long)channel;
271         channel->poll_timer.function = vmbus_ontimer;
272
273         channel->controlwq = create_workqueue("hv_vmbus_ctl");
274         if (!channel->controlwq) {
275                 kfree(channel);
276                 return NULL;
277         }
278
279         return channel;
280 }
281
282 /*
283  * release_hannel - Release the vmbus channel object itself
284  */
285 static void release_channel(struct work_struct *work)
286 {
287         struct vmbus_channel *channel = container_of(work,
288                                                      struct vmbus_channel,
289                                                      work);
290
291         DPRINT_DBG(VMBUS, "releasing channel (%p)", channel);
292         destroy_workqueue(channel->controlwq);
293         DPRINT_DBG(VMBUS, "channel released (%p)", channel);
294
295         kfree(channel);
296 }
297
298 /*
299  * free_channel - Release the resources used by the vmbus channel object
300  */
301 void free_channel(struct vmbus_channel *channel)
302 {
303         del_timer_sync(&channel->poll_timer);
304
305         /*
306          * We have to release the channel's workqueue/thread in the vmbus's
307          * workqueue/thread context
308          * ie we can't destroy ourselves.
309          */
310         INIT_WORK(&channel->work, release_channel);
311         queue_work(gVmbusConnection.WorkQueue, &channel->work);
312 }
313
314
315 DECLARE_COMPLETION(hv_channel_ready);
316
317 /*
318  * Count initialized channels, and ensure all channels are ready when hv_vmbus
319  * module loading completes.
320  */
321 static void count_hv_channel(void)
322 {
323         static int counter;
324         unsigned long flags;
325
326         spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
327         if (++counter == MAX_MSG_TYPES)
328                 complete(&hv_channel_ready);
329         spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
330 }
331
332 /*
333  * vmbus_process_rescind_offer -
334  * Rescind the offer by initiating a device removal
335  */
336 static void vmbus_process_rescind_offer(struct work_struct *work)
337 {
338         struct vmbus_channel *channel = container_of(work,
339                                                      struct vmbus_channel,
340                                                      work);
341
342         vmbus_child_device_unregister(channel->device_obj);
343 }
344
345 /*
346  * vmbus_process_offer - Process the offer by creating a channel/device
347  * associated with this offer
348  */
349 static void vmbus_process_offer(struct work_struct *work)
350 {
351         struct vmbus_channel *newchannel = container_of(work,
352                                                         struct vmbus_channel,
353                                                         work);
354         struct vmbus_channel *channel;
355         bool fnew = true;
356         int ret;
357         int cnt;
358         unsigned long flags;
359
360         /* The next possible work is rescind handling */
361         INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
362
363         /* Make sure this is a new offer */
364         spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
365
366         list_for_each_entry(channel, &gVmbusConnection.ChannelList, listentry) {
367                 if (!memcmp(&channel->offermsg.offer.InterfaceType,
368                             &newchannel->offermsg.offer.InterfaceType,
369                             sizeof(struct hv_guid)) &&
370                     !memcmp(&channel->offermsg.offer.InterfaceInstance,
371                             &newchannel->offermsg.offer.InterfaceInstance,
372                             sizeof(struct hv_guid))) {
373                         fnew = false;
374                         break;
375                 }
376         }
377
378         if (fnew)
379                 list_add_tail(&newchannel->listentry,
380                               &gVmbusConnection.ChannelList);
381
382         spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
383
384         if (!fnew) {
385                 DPRINT_DBG(VMBUS, "Ignoring duplicate offer for relid (%d)",
386                            newchannel->offermsg.child_relid);
387                 free_channel(newchannel);
388                 return;
389         }
390
391         /*
392          * Start the process of binding this offer to the driver
393          * We need to set the DeviceObject field before calling
394          * VmbusChildDeviceAdd()
395          */
396         newchannel->device_obj = vmbus_child_device_create(
397                 &newchannel->offermsg.offer.InterfaceType,
398                 &newchannel->offermsg.offer.InterfaceInstance,
399                 newchannel);
400
401         DPRINT_DBG(VMBUS, "child device object allocated - %p",
402                    newchannel->device_obj);
403
404         /*
405          * Add the new device to the bus. This will kick off device-driver
406          * binding which eventually invokes the device driver's AddDevice()
407          * method.
408          */
409         ret = VmbusChildDeviceAdd(newchannel->device_obj);
410         if (ret != 0) {
411                 DPRINT_ERR(VMBUS,
412                            "unable to add child device object (relid %d)",
413                            newchannel->offermsg.child_relid);
414
415                 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
416                 list_del(&newchannel->listentry);
417                 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
418
419                 free_channel(newchannel);
420         } else {
421                 /*
422                  * This state is used to indicate a successful open
423                  * so that when we do close the channel normally, we
424                  * can cleanup properly
425                  */
426                 newchannel->state = CHANNEL_OPEN_STATE;
427
428                 /* Open IC channels */
429                 for (cnt = 0; cnt < MAX_MSG_TYPES; cnt++) {
430                         if (memcmp(&newchannel->offermsg.offer.InterfaceType,
431                                    &hv_cb_utils[cnt].data,
432                                    sizeof(struct hv_guid)) == 0 &&
433                                 vmbus_open(newchannel, 2 * PAGE_SIZE,
434                                                  2 * PAGE_SIZE, NULL, 0,
435                                                  hv_cb_utils[cnt].callback,
436                                                  newchannel) == 0) {
437                                 hv_cb_utils[cnt].channel = newchannel;
438                                 DPRINT_INFO(VMBUS, "%s",
439                                                 hv_cb_utils[cnt].log_msg);
440                                 count_hv_channel();
441                         }
442                 }
443         }
444 }
445
446 /*
447  * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
448  *
449  * We ignore all offers except network and storage offers. For each network and
450  * storage offers, we create a channel object and queue a work item to the
451  * channel object to process the offer synchronously
452  */
453 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
454 {
455         struct vmbus_channel_offer_channel *offer;
456         struct vmbus_channel *newchannel;
457         struct hv_guid *guidtype;
458         struct hv_guid *guidinstance;
459         int i;
460         int fsupported = 0;
461
462         offer = (struct vmbus_channel_offer_channel *)hdr;
463         for (i = 0; i < MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++) {
464                 if (memcmp(&offer->offer.InterfaceType,
465                     &gSupportedDeviceClasses[i], sizeof(struct hv_guid)) == 0) {
466                         fsupported = 1;
467                         break;
468                 }
469         }
470
471         if (!fsupported) {
472                 DPRINT_DBG(VMBUS, "Ignoring channel offer notification for "
473                            "child relid %d", offer->child_relid);
474                 return;
475         }
476
477         guidtype = &offer->offer.InterfaceType;
478         guidinstance = &offer->offer.InterfaceInstance;
479
480         DPRINT_INFO(VMBUS, "Channel offer notification - "
481                     "child relid %d monitor id %d allocated %d, "
482                     "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
483                     "%02x%02x%02x%02x%02x%02x%02x%02x} "
484                     "instance {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
485                     "%02x%02x%02x%02x%02x%02x%02x%02x}",
486                     offer->child_relid, offer->monitorid,
487                     offer->monitor_allocated,
488                     guidtype->data[3], guidtype->data[2],
489                     guidtype->data[1], guidtype->data[0],
490                     guidtype->data[5], guidtype->data[4],
491                     guidtype->data[7], guidtype->data[6],
492                     guidtype->data[8], guidtype->data[9],
493                     guidtype->data[10], guidtype->data[11],
494                     guidtype->data[12], guidtype->data[13],
495                     guidtype->data[14], guidtype->data[15],
496                     guidinstance->data[3], guidinstance->data[2],
497                     guidinstance->data[1], guidinstance->data[0],
498                     guidinstance->data[5], guidinstance->data[4],
499                     guidinstance->data[7], guidinstance->data[6],
500                     guidinstance->data[8], guidinstance->data[9],
501                     guidinstance->data[10], guidinstance->data[11],
502                     guidinstance->data[12], guidinstance->data[13],
503                     guidinstance->data[14], guidinstance->data[15]);
504
505         /* Allocate the channel object and save this offer. */
506         newchannel = alloc_channel();
507         if (!newchannel) {
508                 DPRINT_ERR(VMBUS, "unable to allocate channel object");
509                 return;
510         }
511
512         DPRINT_DBG(VMBUS, "channel object allocated - %p", newchannel);
513
514         memcpy(&newchannel->offermsg, offer,
515                sizeof(struct vmbus_channel_offer_channel));
516         newchannel->monitor_grp = (u8)offer->monitorid / 32;
517         newchannel->monitor_bit = (u8)offer->monitorid % 32;
518
519         /* TODO: Make sure the offer comes from our parent partition */
520         INIT_WORK(&newchannel->work, vmbus_process_offer);
521         queue_work(newchannel->controlwq, &newchannel->work);
522 }
523
524 /*
525  * vmbus_onoffer_rescind - Rescind offer handler.
526  *
527  * We queue a work item to process this offer synchronously
528  */
529 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
530 {
531         struct vmbus_channel_rescind_offer *rescind;
532         struct vmbus_channel *channel;
533
534         rescind = (struct vmbus_channel_rescind_offer *)hdr;
535         channel = GetChannelFromRelId(rescind->child_relid);
536         if (channel == NULL) {
537                 DPRINT_DBG(VMBUS, "channel not found for relId %d",
538                            rescind->child_relid);
539                 return;
540         }
541
542         /* work is initialized for vmbus_process_rescind_offer() from
543          * vmbus_process_offer() where the channel got created */
544         queue_work(channel->controlwq, &channel->work);
545 }
546
547 /*
548  * vmbus_onoffers_delivered -
549  * This is invoked when all offers have been delivered.
550  *
551  * Nothing to do here.
552  */
553 static void vmbus_onoffers_delivered(
554                         struct vmbus_channel_message_header *hdr)
555 {
556 }
557
558 /*
559  * vmbus_onopen_result - Open result handler.
560  *
561  * This is invoked when we received a response to our channel open request.
562  * Find the matching request, copy the response and signal the requesting
563  * thread.
564  */
565 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
566 {
567         struct vmbus_channel_open_result *result;
568         struct list_head *curr;
569         struct vmbus_channel_msginfo *msginfo;
570         struct vmbus_channel_message_header *requestheader;
571         struct vmbus_channel_open_channel *openmsg;
572         unsigned long flags;
573
574         result = (struct vmbus_channel_open_result *)hdr;
575         DPRINT_DBG(VMBUS, "vmbus open result - %d", result->status);
576
577         /*
578          * Find the open msg, copy the result and signal/unblock the wait event
579          */
580         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
581
582         list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
583 /* FIXME: this should probably use list_entry() instead */
584                 msginfo = (struct vmbus_channel_msginfo *)curr;
585                 requestheader =
586                         (struct vmbus_channel_message_header *)msginfo->msg;
587
588                 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
589                         openmsg =
590                         (struct vmbus_channel_open_channel *)msginfo->msg;
591                         if (openmsg->child_relid == result->child_relid &&
592                             openmsg->openid == result->openid) {
593                                 memcpy(&msginfo->response.open_result,
594                                        result,
595                                        sizeof(struct vmbus_channel_open_result));
596                                 osd_waitevent_set(msginfo->waitevent);
597                                 break;
598                         }
599                 }
600         }
601         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
602 }
603
604 /*
605  * vmbus_ongpadl_created - GPADL created handler.
606  *
607  * This is invoked when we received a response to our gpadl create request.
608  * Find the matching request, copy the response and signal the requesting
609  * thread.
610  */
611 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
612 {
613         struct vmbus_channel_gpadl_created *gpadlcreated;
614         struct list_head *curr;
615         struct vmbus_channel_msginfo *msginfo;
616         struct vmbus_channel_message_header *requestheader;
617         struct vmbus_channel_gpadl_header *gpadlheader;
618         unsigned long flags;
619
620         gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
621         DPRINT_DBG(VMBUS, "vmbus gpadl created result - %d",
622                    gpadlcreated->creation_status);
623
624         /*
625          * Find the establish msg, copy the result and signal/unblock the wait
626          * event
627          */
628         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
629
630         list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
631 /* FIXME: this should probably use list_entry() instead */
632                 msginfo = (struct vmbus_channel_msginfo *)curr;
633                 requestheader =
634                         (struct vmbus_channel_message_header *)msginfo->msg;
635
636                 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
637                         gpadlheader =
638                         (struct vmbus_channel_gpadl_header *)requestheader;
639
640                         if ((gpadlcreated->child_relid ==
641                              gpadlheader->child_relid) &&
642                             (gpadlcreated->gpadl == gpadlheader->gpadl)) {
643                                 memcpy(&msginfo->response.gpadl_created,
644                                        gpadlcreated,
645                                        sizeof(struct vmbus_channel_gpadl_created));
646                                 osd_waitevent_set(msginfo->waitevent);
647                                 break;
648                         }
649                 }
650         }
651         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
652 }
653
654 /*
655  * vmbus_ongpadl_torndown - GPADL torndown handler.
656  *
657  * This is invoked when we received a response to our gpadl teardown request.
658  * Find the matching request, copy the response and signal the requesting
659  * thread.
660  */
661 static void vmbus_ongpadl_torndown(
662                         struct vmbus_channel_message_header *hdr)
663 {
664         struct vmbus_channel_gpadl_torndown *gpadl_torndown;
665         struct list_head *curr;
666         struct vmbus_channel_msginfo *msginfo;
667         struct vmbus_channel_message_header *requestheader;
668         struct vmbus_channel_gpadl_teardown *gpadl_teardown;
669         unsigned long flags;
670
671         gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
672
673         /*
674          * Find the open msg, copy the result and signal/unblock the wait event
675          */
676         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
677
678         list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
679 /* FIXME: this should probably use list_entry() instead */
680                 msginfo = (struct vmbus_channel_msginfo *)curr;
681                 requestheader =
682                         (struct vmbus_channel_message_header *)msginfo->msg;
683
684                 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
685                         gpadl_teardown =
686                         (struct vmbus_channel_gpadl_teardown *)requestheader;
687
688                         if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
689                                 memcpy(&msginfo->response.gpadl_torndown,
690                                        gpadl_torndown,
691                                        sizeof(struct vmbus_channel_gpadl_torndown));
692                                 osd_waitevent_set(msginfo->waitevent);
693                                 break;
694                         }
695                 }
696         }
697         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
698 }
699
700 /*
701  * vmbus_onversion_response - Version response handler
702  *
703  * This is invoked when we received a response to our initiate contact request.
704  * Find the matching request, copy the response and signal the requesting
705  * thread.
706  */
707 static void vmbus_onversion_response(
708                 struct vmbus_channel_message_header *hdr)
709 {
710         struct list_head *curr;
711         struct vmbus_channel_msginfo *msginfo;
712         struct vmbus_channel_message_header *requestheader;
713         struct vmbus_channel_initiate_contact *initiate;
714         struct vmbus_channel_version_response *version_response;
715         unsigned long flags;
716
717         version_response = (struct vmbus_channel_version_response *)hdr;
718         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
719
720         list_for_each(curr, &gVmbusConnection.ChannelMsgList) {
721 /* FIXME: this should probably use list_entry() instead */
722                 msginfo = (struct vmbus_channel_msginfo *)curr;
723                 requestheader =
724                         (struct vmbus_channel_message_header *)msginfo->msg;
725
726                 if (requestheader->msgtype ==
727                     CHANNELMSG_INITIATE_CONTACT) {
728                         initiate =
729                         (struct vmbus_channel_initiate_contact *)requestheader;
730                         memcpy(&msginfo->response.version_response,
731                               version_response,
732                               sizeof(struct vmbus_channel_version_response));
733                         osd_waitevent_set(msginfo->waitevent);
734                 }
735         }
736         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
737 }
738
739 /* Channel message dispatch table */
740 static struct vmbus_channel_message_table_entry
741         gChannelMessageTable[CHANNELMSG_COUNT] = {
742         {CHANNELMSG_INVALID,                    NULL},
743         {CHANNELMSG_OFFERCHANNEL,               vmbus_onoffer},
744         {CHANNELMSG_RESCIND_CHANNELOFFER,       vmbus_onoffer_rescind},
745         {CHANNELMSG_REQUESTOFFERS,              NULL},
746         {CHANNELMSG_ALLOFFERS_DELIVERED,        vmbus_onoffers_delivered},
747         {CHANNELMSG_OPENCHANNEL,                NULL},
748         {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
749         {CHANNELMSG_CLOSECHANNEL,               NULL},
750         {CHANNELMSG_GPADL_HEADER,               NULL},
751         {CHANNELMSG_GPADL_BODY,         NULL},
752         {CHANNELMSG_GPADL_CREATED,              vmbus_ongpadl_created},
753         {CHANNELMSG_GPADL_TEARDOWN,             NULL},
754         {CHANNELMSG_GPADL_TORNDOWN,             vmbus_ongpadl_torndown},
755         {CHANNELMSG_RELID_RELEASED,             NULL},
756         {CHANNELMSG_INITIATE_CONTACT,           NULL},
757         {CHANNELMSG_VERSION_RESPONSE,           vmbus_onversion_response},
758         {CHANNELMSG_UNLOAD,                     NULL},
759 };
760
761 /*
762  * vmbus_onmessage - Handler for channel protocol messages.
763  *
764  * This is invoked in the vmbus worker thread context.
765  */
766 void vmbus_onmessage(void *context)
767 {
768         struct hv_message *msg = context;
769         struct vmbus_channel_message_header *hdr;
770         int size;
771
772         hdr = (struct vmbus_channel_message_header *)msg->u.payload;
773         size = msg->header.payload_size;
774
775         DPRINT_DBG(VMBUS, "message type %d size %d", hdr->msgtype, size);
776
777         if (hdr->msgtype >= CHANNELMSG_COUNT) {
778                 DPRINT_ERR(VMBUS,
779                            "Received invalid channel message type %d size %d",
780                            hdr->msgtype, size);
781                 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
782                                      (unsigned char *)msg->u.payload, size);
783                 return;
784         }
785
786         if (gChannelMessageTable[hdr->msgtype].messageHandler)
787                 gChannelMessageTable[hdr->msgtype].messageHandler(hdr);
788         else
789                 DPRINT_ERR(VMBUS, "Unhandled channel message type %d",
790                            hdr->msgtype);
791 }
792
793 /*
794  * vmbus_request_offers - Send a request to get all our pending offers.
795  */
796 int vmbus_request_offers(void)
797 {
798         struct vmbus_channel_message_header *msg;
799         struct vmbus_channel_msginfo *msginfo;
800         int ret;
801
802         msginfo = kmalloc(sizeof(*msginfo) +
803                           sizeof(struct vmbus_channel_message_header),
804                           GFP_KERNEL);
805         if (!msginfo)
806                 return -ENOMEM;
807
808         msginfo->waitevent = osd_waitevent_create();
809         if (!msginfo->waitevent) {
810                 kfree(msginfo);
811                 return -ENOMEM;
812         }
813
814         msg = (struct vmbus_channel_message_header *)msginfo->msg;
815
816         msg->msgtype = CHANNELMSG_REQUESTOFFERS;
817
818         /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
819         INSERT_TAIL_LIST(&gVmbusConnection.channelMsgList,
820                          &msgInfo->msgListEntry);
821         SpinlockRelease(gVmbusConnection.channelMsgLock);*/
822
823         ret = VmbusPostMessage(msg,
824                                sizeof(struct vmbus_channel_message_header));
825         if (ret != 0) {
826                 DPRINT_ERR(VMBUS, "Unable to request offers - %d", ret);
827
828                 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
829                 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
830                 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
831
832                 goto Cleanup;
833         }
834         /* osd_waitevent_wait(msgInfo->waitEvent); */
835
836         /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
837         REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
838         SpinlockRelease(gVmbusConnection.channelMsgLock);*/
839
840
841 Cleanup:
842         if (msginfo) {
843                 kfree(msginfo->waitevent);
844                 kfree(msginfo);
845         }
846
847         return ret;
848 }
849
850 /*
851  * vmbus_release_unattached_channels - Release channels that are
852  * unattached/unconnected ie (no drivers associated)
853  */
854 void vmbus_release_unattached_channels(void)
855 {
856         struct vmbus_channel *channel, *pos;
857         struct vmbus_channel *start = NULL;
858         unsigned long flags;
859
860         spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
861
862         list_for_each_entry_safe(channel, pos, &gVmbusConnection.ChannelList,
863                                  listentry) {
864                 if (channel == start)
865                         break;
866
867                 if (!channel->device_obj->Driver) {
868                         list_del(&channel->listentry);
869                         DPRINT_INFO(VMBUS,
870                                     "Releasing unattached device object %p",
871                                     channel->device_obj);
872
873                         vmbus_child_device_unregister(channel->device_obj);
874                         free_channel(channel);
875                 } else {
876                         if (!start)
877                                 start = channel;
878                 }
879         }
880
881         spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
882 }
883
884 /* eof */