]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/staging/hv/channel.c
de5b1f2b3a87fc868483e447e3a46d5c0ac86d91
[lisovros/linux_canprio.git] / drivers / staging / hv / channel.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/module.h>
25 #include "osd.h"
26 #include "logging.h"
27 #include "vmbus_private.h"
28
29 /* Internal routines */
30 static int VmbusChannelCreateGpadlHeader(
31         void *Kbuffer,  /* must be phys and virt contiguous */
32         u32 Size,       /* page-size multiple */
33         struct vmbus_channel_msginfo **msgInfo,
34         u32 *MessageCount);
35 static void DumpVmbusChannel(struct vmbus_channel *channel);
36 static void VmbusChannelSetEvent(struct vmbus_channel *channel);
37
38
39 #if 0
40 static void DumpMonitorPage(struct hv_monitor_page *MonitorPage)
41 {
42         int i = 0;
43         int j = 0;
44
45         DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d",
46                    MonitorPage, MonitorPage->TriggerState);
47
48         for (i = 0; i < 4; i++)
49                 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i,
50                            MonitorPage->TriggerGroup[i].AsUINT64);
51
52         for (i = 0; i < 4; i++) {
53                 for (j = 0; j < 32; j++) {
54                         DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j,
55                                    MonitorPage->Latency[i][j]);
56                 }
57         }
58         for (i = 0; i < 4; i++) {
59                 for (j = 0; j < 32; j++) {
60                         DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j,
61                                MonitorPage->Parameter[i][j].ConnectionId.Asu32);
62                         DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j,
63                                 MonitorPage->Parameter[i][j].FlagNumber);
64                 }
65         }
66 }
67 #endif
68
69 /*
70  * VmbusChannelSetEvent - Trigger an event notification on the specified
71  * channel.
72  */
73 static void VmbusChannelSetEvent(struct vmbus_channel *Channel)
74 {
75         struct hv_monitor_page *monitorPage;
76
77         if (Channel->OfferMsg.MonitorAllocated) {
78                 /* Each u32 represents 32 channels */
79                 set_bit(Channel->OfferMsg.ChildRelId & 31,
80                         (unsigned long *) gVmbusConnection.SendInterruptPage +
81                         (Channel->OfferMsg.ChildRelId >> 5));
82
83                 monitorPage = gVmbusConnection.MonitorPages;
84                 monitorPage++; /* Get the child to parent monitor page */
85
86                 set_bit(Channel->MonitorBit,
87                         (unsigned long *)&monitorPage->TriggerGroup
88                                         [Channel->MonitorGroup].Pending);
89
90         } else {
91                 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
92         }
93
94         DPRINT_EXIT(VMBUS);
95 }
96
97 #if 0
98 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
99 {
100         struct hv_monitor_page *monitorPage;
101
102         if (Channel->OfferMsg.MonitorAllocated) {
103                 /* Each u32 represents 32 channels */
104                 clear_bit(Channel->OfferMsg.ChildRelId & 31,
105                           (unsigned long *)gVmbusConnection.SendInterruptPage +
106                           (Channel->OfferMsg.ChildRelId >> 5));
107
108                 monitorPage =
109                         (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
110                 monitorPage++; /* Get the child to parent monitor page */
111
112                 clear_bit(Channel->MonitorBit,
113                           (unsigned long *)&monitorPage->TriggerGroup
114                                         [Channel->MonitorGroup].Pending);
115         }
116
117         DPRINT_EXIT(VMBUS);
118 }
119
120 #endif
121 /*
122  * VmbusChannelGetDebugInfo -Retrieve various channel debug info
123  */
124 void VmbusChannelGetDebugInfo(struct vmbus_channel *Channel,
125                               struct vmbus_channel_debug_info *DebugInfo)
126 {
127         struct hv_monitor_page *monitorPage;
128         u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
129         u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
130         /* u32 monitorBit       = 1 << monitorOffset; */
131
132         DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
133         DebugInfo->State = Channel->State;
134         memcpy(&DebugInfo->InterfaceType,
135                &Channel->OfferMsg.Offer.InterfaceType, sizeof(struct hv_guid));
136         memcpy(&DebugInfo->InterfaceInstance,
137                &Channel->OfferMsg.Offer.InterfaceInstance,
138                sizeof(struct hv_guid));
139
140         monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
141
142         DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
143
144         DebugInfo->ServerMonitorPending =
145                         monitorPage->TriggerGroup[monitorGroup].Pending;
146         DebugInfo->ServerMonitorLatency =
147                         monitorPage->Latency[monitorGroup][monitorOffset];
148         DebugInfo->ServerMonitorConnectionId =
149                         monitorPage->Parameter[monitorGroup]
150                                               [monitorOffset].ConnectionId.u.Id;
151
152         monitorPage++;
153
154         DebugInfo->ClientMonitorPending =
155                         monitorPage->TriggerGroup[monitorGroup].Pending;
156         DebugInfo->ClientMonitorLatency =
157                         monitorPage->Latency[monitorGroup][monitorOffset];
158         DebugInfo->ClientMonitorConnectionId =
159                         monitorPage->Parameter[monitorGroup]
160                                               [monitorOffset].ConnectionId.u.Id;
161
162         RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
163         RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
164 }
165
166 /*
167  * VmbusChannelOpen - Open the specified channel.
168  */
169 int VmbusChannelOpen(struct vmbus_channel *NewChannel, u32 SendRingBufferSize,
170                      u32 RecvRingBufferSize, void *UserData, u32 UserDataLen,
171                      void (*OnChannelCallback)(void *context), void *Context)
172 {
173         struct vmbus_channel_open_channel *openMsg;
174         struct vmbus_channel_msginfo *openInfo = NULL;
175         void *in, *out;
176         unsigned long flags;
177         int ret, err = 0;
178
179         /* Aligned to page size */
180         /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
181         /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
182
183         NewChannel->OnChannelCallback = OnChannelCallback;
184         NewChannel->ChannelCallbackContext = Context;
185
186         /* Allocate the ring buffer */
187         out = osd_PageAlloc((SendRingBufferSize + RecvRingBufferSize)
188                              >> PAGE_SHIFT);
189         if (!out)
190                 return -ENOMEM;
191
192         /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
193
194         in = (void *)((unsigned long)out + SendRingBufferSize);
195
196         NewChannel->RingBufferPages = out;
197         NewChannel->RingBufferPageCount = (SendRingBufferSize +
198                                            RecvRingBufferSize) >> PAGE_SHIFT;
199
200         ret = RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
201         if (ret != 0) {
202                 err = ret;
203                 goto errorout;
204         }
205
206         ret = RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
207         if (ret != 0) {
208                 err = ret;
209                 goto errorout;
210         }
211
212
213         /* Establish the gpadl for the ring buffer */
214         DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...",
215                    NewChannel);
216
217         NewChannel->RingBufferGpadlHandle = 0;
218
219         ret = VmbusChannelEstablishGpadl(NewChannel,
220                                          NewChannel->Outbound.RingBuffer,
221                                          SendRingBufferSize +
222                                          RecvRingBufferSize,
223                                          &NewChannel->RingBufferGpadlHandle);
224
225         if (ret != 0) {
226                 err = ret;
227                 goto errorout;
228         }
229
230         DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p "
231                    "size %d recv ring %p size %d, downstreamoffset %d>",
232                    NewChannel, NewChannel->OfferMsg.ChildRelId,
233                    NewChannel->RingBufferGpadlHandle,
234                    NewChannel->Outbound.RingBuffer,
235                    NewChannel->Outbound.RingSize,
236                    NewChannel->Inbound.RingBuffer,
237                    NewChannel->Inbound.RingSize,
238                    SendRingBufferSize);
239
240         /* Create and init the channel open message */
241         openInfo = kmalloc(sizeof(*openInfo) +
242                            sizeof(struct vmbus_channel_open_channel),
243                            GFP_KERNEL);
244         if (!openInfo) {
245                 err = -ENOMEM;
246                 goto errorout;
247         }
248
249         openInfo->WaitEvent = osd_WaitEventCreate();
250         if (!openInfo->WaitEvent) {
251                 err = -ENOMEM;
252                 goto errorout;
253         }
254
255         openMsg = (struct vmbus_channel_open_channel *)openInfo->Msg;
256         openMsg->Header.MessageType = ChannelMessageOpenChannel;
257         openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; /* FIXME */
258         openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
259         openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
260         openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >>
261                                                   PAGE_SHIFT;
262         openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
263
264         if (UserDataLen > MAX_USER_DEFINED_BYTES) {
265                 err = -EINVAL;
266                 goto errorout;
267         }
268
269         if (UserDataLen)
270                 memcpy(openMsg->UserData, UserData, UserDataLen);
271
272         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
273         list_add_tail(&openInfo->MsgListEntry,
274                       &gVmbusConnection.ChannelMsgList);
275         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
276
277         DPRINT_DBG(VMBUS, "Sending channel open msg...");
278
279         ret = VmbusPostMessage(openMsg,
280                                sizeof(struct vmbus_channel_open_channel));
281         if (ret != 0) {
282                 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
283                 goto Cleanup;
284         }
285
286         /* FIXME: Need to time-out here */
287         osd_WaitEventWait(openInfo->WaitEvent);
288
289         if (openInfo->Response.OpenResult.Status == 0)
290                 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
291         else
292                 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!",
293                             NewChannel, openInfo->Response.OpenResult.Status);
294
295 Cleanup:
296         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
297         list_del(&openInfo->MsgListEntry);
298         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
299
300         kfree(openInfo->WaitEvent);
301         kfree(openInfo);
302
303         DPRINT_EXIT(VMBUS);
304
305         return 0;
306
307 errorout:
308         RingBufferCleanup(&NewChannel->Outbound);
309         RingBufferCleanup(&NewChannel->Inbound);
310         osd_PageFree(out, (SendRingBufferSize + RecvRingBufferSize)
311                      >> PAGE_SHIFT);
312         kfree(openInfo);
313         return err;
314 }
315
316 /*
317  * DumpGpadlBody - Dump the gpadl body message to the console for
318  * debugging purposes.
319  */
320 static void DumpGpadlBody(struct vmbus_channel_gpadl_body *Gpadl, u32 Len)
321 {
322         int i;
323         int pfnCount;
324
325         pfnCount = (Len - sizeof(struct vmbus_channel_gpadl_body)) /
326                    sizeof(u64);
327         DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
328
329         for (i = 0; i < pfnCount; i++)
330                 DPRINT_DBG(VMBUS, "gpadl body  - %d) pfn %llu",
331                            i, Gpadl->Pfn[i]);
332 }
333
334 /*
335  * DumpGpadlHeader - Dump the gpadl header message to the console for
336  * debugging purposes.
337  */
338 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header *Gpadl)
339 {
340         int i, j;
341         int pageCount;
342
343         DPRINT_DBG(VMBUS,
344                    "gpadl header - relid %d, range count %d, range buflen %d",
345                    Gpadl->ChildRelId, Gpadl->RangeCount, Gpadl->RangeBufLen);
346         for (i = 0; i < Gpadl->RangeCount; i++) {
347                 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
348                 pageCount = (pageCount > 26) ? 26 : pageCount;
349
350                 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
351                            "page count %d", i, Gpadl->Range[i].ByteCount,
352                            Gpadl->Range[i].ByteOffset, pageCount);
353
354                 for (j = 0; j < pageCount; j++)
355                         DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
356                                    Gpadl->Range[i].PfnArray[j]);
357         }
358 }
359
360 /*
361  * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
362  */
363 static int VmbusChannelCreateGpadlHeader(void *Kbuffer, u32 Size,
364                                          struct vmbus_channel_msginfo **MsgInfo,
365                                          u32 *MessageCount)
366 {
367         int i;
368         int pageCount;
369         unsigned long long pfn;
370         struct vmbus_channel_gpadl_header *gpaHeader;
371         struct vmbus_channel_gpadl_body *gpadlBody;
372         struct vmbus_channel_msginfo *msgHeader;
373         struct vmbus_channel_msginfo *msgBody = NULL;
374         u32 msgSize;
375
376         int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
377
378         /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
379         /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
380
381         pageCount = Size >> PAGE_SHIFT;
382         pfn = virt_to_phys(Kbuffer) >> PAGE_SHIFT;
383
384         /* do we need a gpadl body msg */
385         pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
386                   sizeof(struct vmbus_channel_gpadl_header) -
387                   sizeof(struct gpa_range);
388         pfnCount = pfnSize / sizeof(u64);
389
390         if (pageCount > pfnCount) {
391                 /* we need a gpadl body */
392                 /* fill in the header */
393                 msgSize = sizeof(struct vmbus_channel_msginfo) +
394                           sizeof(struct vmbus_channel_gpadl_header) +
395                           sizeof(struct gpa_range) + pfnCount * sizeof(u64);
396                 msgHeader =  kzalloc(msgSize, GFP_KERNEL);
397                 if (!msgHeader)
398                         goto nomem;
399
400                 INIT_LIST_HEAD(&msgHeader->SubMsgList);
401                 msgHeader->MessageSize = msgSize;
402
403                 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
404                 gpaHeader->RangeCount = 1;
405                 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
406                                          pageCount * sizeof(u64);
407                 gpaHeader->Range[0].ByteOffset = 0;
408                 gpaHeader->Range[0].ByteCount = Size;
409                 for (i = 0; i < pfnCount; i++)
410                         gpaHeader->Range[0].PfnArray[i] = pfn+i;
411                 *MsgInfo = msgHeader;
412                 *MessageCount = 1;
413
414                 pfnSum = pfnCount;
415                 pfnLeft = pageCount - pfnCount;
416
417                 /* how many pfns can we fit */
418                 pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
419                           sizeof(struct vmbus_channel_gpadl_body);
420                 pfnCount = pfnSize / sizeof(u64);
421
422                 /* fill in the body */
423                 while (pfnLeft) {
424                         if (pfnLeft > pfnCount)
425                                 pfnCurr = pfnCount;
426                         else
427                                 pfnCurr = pfnLeft;
428
429                         msgSize = sizeof(struct vmbus_channel_msginfo) +
430                                   sizeof(struct vmbus_channel_gpadl_body) +
431                                   pfnCurr * sizeof(u64);
432                         msgBody = kzalloc(msgSize, GFP_KERNEL);
433                         /* FIXME: we probably need to more if this fails */
434                         if (!msgBody)
435                                 goto nomem;
436                         msgBody->MessageSize = msgSize;
437                         (*MessageCount)++;
438                         gpadlBody =
439                                 (struct vmbus_channel_gpadl_body *)msgBody->Msg;
440
441                         /*
442                          * FIXME:
443                          * Gpadl is u32 and we are using a pointer which could
444                          * be 64-bit
445                          */
446                         /* gpadlBody->Gpadl = kbuffer; */
447                         for (i = 0; i < pfnCurr; i++)
448                                 gpadlBody->Pfn[i] = pfn + pfnSum + i;
449
450                         /* add to msg header */
451                         list_add_tail(&msgBody->MsgListEntry,
452                                       &msgHeader->SubMsgList);
453                         pfnSum += pfnCurr;
454                         pfnLeft -= pfnCurr;
455                 }
456         } else {
457                 /* everything fits in a header */
458                 msgSize = sizeof(struct vmbus_channel_msginfo) +
459                           sizeof(struct vmbus_channel_gpadl_header) +
460                           sizeof(struct gpa_range) + pageCount * sizeof(u64);
461                 msgHeader = kzalloc(msgSize, GFP_KERNEL);
462                 if (msgHeader == NULL)
463                         goto nomem;
464                 msgHeader->MessageSize = msgSize;
465
466                 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
467                 gpaHeader->RangeCount = 1;
468                 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
469                                          pageCount * sizeof(u64);
470                 gpaHeader->Range[0].ByteOffset = 0;
471                 gpaHeader->Range[0].ByteCount = Size;
472                 for (i = 0; i < pageCount; i++)
473                         gpaHeader->Range[0].PfnArray[i] = pfn+i;
474
475                 *MsgInfo = msgHeader;
476                 *MessageCount = 1;
477         }
478
479         return 0;
480 nomem:
481         kfree(msgHeader);
482         kfree(msgBody);
483         return -ENOMEM;
484 }
485
486 /*
487  * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
488  *
489  * @Channel: a channel
490  * @Kbuffer: from kmalloc
491  * @Size: page-size multiple
492  * @GpadlHandle: some funky thing
493  */
494 int VmbusChannelEstablishGpadl(struct vmbus_channel *Channel, void *Kbuffer,
495                                u32 Size, u32 *GpadlHandle)
496 {
497         struct vmbus_channel_gpadl_header *gpadlMsg;
498         struct vmbus_channel_gpadl_body *gpadlBody;
499         /* struct vmbus_channel_gpadl_created *gpadlCreated; */
500         struct vmbus_channel_msginfo *msgInfo = NULL;
501         struct vmbus_channel_msginfo *subMsgInfo;
502         u32 msgCount;
503         struct list_head *curr;
504         u32 nextGpadlHandle;
505         unsigned long flags;
506         int ret = 0;
507
508         nextGpadlHandle = atomic_read(&gVmbusConnection.NextGpadlHandle);
509         atomic_inc(&gVmbusConnection.NextGpadlHandle);
510
511         ret = VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
512         if (ret)
513                 return ret;
514
515         msgInfo->WaitEvent = osd_WaitEventCreate();
516         if (!msgInfo->WaitEvent) {
517                 ret = -ENOMEM;
518                 goto Cleanup;
519         }
520
521         gpadlMsg = (struct vmbus_channel_gpadl_header *)msgInfo->Msg;
522         gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
523         gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
524         gpadlMsg->Gpadl = nextGpadlHandle;
525
526         DumpGpadlHeader(gpadlMsg);
527
528         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
529         list_add_tail(&msgInfo->MsgListEntry,
530                       &gVmbusConnection.ChannelMsgList);
531
532         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
533         DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d",
534                    Kbuffer, Size, msgCount);
535
536         DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd",
537                    msgInfo->MessageSize - sizeof(*msgInfo));
538
539         ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize -
540                                sizeof(*msgInfo));
541         if (ret != 0) {
542                 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
543                 goto Cleanup;
544         }
545
546         if (msgCount > 1) {
547                 list_for_each(curr, &msgInfo->SubMsgList) {
548
549                         /* FIXME: should this use list_entry() instead ? */
550                         subMsgInfo = (struct vmbus_channel_msginfo *)curr;
551                         gpadlBody =
552                              (struct vmbus_channel_gpadl_body *)subMsgInfo->Msg;
553
554                         gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
555                         gpadlBody->Gpadl = nextGpadlHandle;
556
557                         DPRINT_DBG(VMBUS, "Sending GPADL Body - len %zd",
558                                    subMsgInfo->MessageSize -
559                                    sizeof(*subMsgInfo));
560
561                         DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize -
562                                       sizeof(*subMsgInfo));
563                         ret = VmbusPostMessage(gpadlBody,
564                                                subMsgInfo->MessageSize -
565                                                sizeof(*subMsgInfo));
566                         if (ret != 0)
567                                 goto Cleanup;
568
569                 }
570         }
571         osd_WaitEventWait(msgInfo->WaitEvent);
572
573         /* At this point, we received the gpadl created msg */
574         DPRINT_DBG(VMBUS, "Received GPADL created "
575                    "(relid %d, status %d handle %x)",
576                    Channel->OfferMsg.ChildRelId,
577                    msgInfo->Response.GpadlCreated.CreationStatus,
578                    gpadlMsg->Gpadl);
579
580         *GpadlHandle = gpadlMsg->Gpadl;
581
582 Cleanup:
583         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
584         list_del(&msgInfo->MsgListEntry);
585         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
586
587         kfree(msgInfo->WaitEvent);
588         kfree(msgInfo);
589
590         DPRINT_EXIT(VMBUS);
591
592         return ret;
593 }
594
595 /*
596  * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
597  */
598 int VmbusChannelTeardownGpadl(struct vmbus_channel *Channel, u32 GpadlHandle)
599 {
600         struct vmbus_channel_gpadl_teardown *msg;
601         struct vmbus_channel_msginfo *info;
602         unsigned long flags;
603         int ret;
604
605         /* ASSERT(GpadlHandle != 0); */
606
607         info = kmalloc(sizeof(*info) +
608                        sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
609         if (!info)
610                 return -ENOMEM;
611
612         info->WaitEvent = osd_WaitEventCreate();
613         if (!info->WaitEvent) {
614                 kfree(info);
615                 return -ENOMEM;
616         }
617
618         msg = (struct vmbus_channel_gpadl_teardown *)info->Msg;
619
620         msg->Header.MessageType = ChannelMessageGpadlTeardown;
621         msg->ChildRelId = Channel->OfferMsg.ChildRelId;
622         msg->Gpadl = GpadlHandle;
623
624         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
625         list_add_tail(&info->MsgListEntry,
626                       &gVmbusConnection.ChannelMsgList);
627         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
628
629         ret = VmbusPostMessage(msg,
630                                sizeof(struct vmbus_channel_gpadl_teardown));
631         if (ret != 0) {
632                 /* TODO: */
633                 /* something... */
634         }
635
636         osd_WaitEventWait(info->WaitEvent);
637
638         /* Received a torndown response */
639         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
640         list_del(&info->MsgListEntry);
641         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
642
643         kfree(info->WaitEvent);
644         kfree(info);
645
646         DPRINT_EXIT(VMBUS);
647
648         return ret;
649 }
650
651 /*
652  * VmbusChannelClose - Close the specified channel
653  */
654 void VmbusChannelClose(struct vmbus_channel *Channel)
655 {
656         struct vmbus_channel_close_channel *msg;
657         struct vmbus_channel_msginfo *info;
658         unsigned long flags;
659         int ret;
660
661         /* Stop callback and cancel the timer asap */
662         Channel->OnChannelCallback = NULL;
663         del_timer_sync(&Channel->poll_timer);
664
665         /* Send a closing message */
666         info = kmalloc(sizeof(*info) +
667                        sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
668         /* FIXME: can't do anything other than return here because the
669          *        function is void */
670         if (!info)
671                 return;
672
673         /* info->waitEvent = osd_WaitEventCreate(); */
674
675         msg = (struct vmbus_channel_close_channel *)info->Msg;
676         msg->Header.MessageType = ChannelMessageCloseChannel;
677         msg->ChildRelId = Channel->OfferMsg.ChildRelId;
678
679         ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_close_channel));
680         if (ret != 0) {
681                 /* TODO: */
682                 /* something... */
683         }
684
685         /* Tear down the gpadl for the channel's ring buffer */
686         if (Channel->RingBufferGpadlHandle)
687                 VmbusChannelTeardownGpadl(Channel,
688                                           Channel->RingBufferGpadlHandle);
689
690         /* TODO: Send a msg to release the childRelId */
691
692         /* Cleanup the ring buffers for this channel */
693         RingBufferCleanup(&Channel->Outbound);
694         RingBufferCleanup(&Channel->Inbound);
695
696         osd_PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
697
698         kfree(info);
699
700         /*
701          * If we are closing the channel during an error path in
702          * opening the channel, don't free the channel since the
703          * caller will free the channel
704          */
705
706         if (Channel->State == CHANNEL_OPEN_STATE) {
707                 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
708                 list_del(&Channel->ListEntry);
709                 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
710
711                 FreeVmbusChannel(Channel);
712         }
713
714         DPRINT_EXIT(VMBUS);
715 }
716
717 /**
718  * VmbusChannelSendPacket() - Send the specified buffer on the given channel
719  * @Channel: Pointer to vmbus_channel structure.
720  * @Buffer: Pointer to the buffer you want to receive the data into.
721  * @BufferLen: Maximum size of what the the buffer will hold
722  * @RequestId: Identifier of the request
723  * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
724  * packet etc.
725  *
726  * Sends data in @Buffer directly to hyper-v via the vmbus
727  * This will send the data unparsed to hyper-v.
728  *
729  * Mainly used by Hyper-V drivers.
730  */
731 int VmbusChannelSendPacket(struct vmbus_channel *Channel, const void *Buffer,
732                            u32 BufferLen, u64 RequestId,
733                            enum vmbus_packet_type Type, u32 Flags)
734 {
735         struct vmpacket_descriptor desc;
736         u32 packetLen = sizeof(struct vmpacket_descriptor) + BufferLen;
737         u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
738         struct scatterlist bufferList[3];
739         u64 alignedData = 0;
740         int ret;
741
742         DPRINT_DBG(VMBUS, "channel %p buffer %p len %d",
743                    Channel, Buffer, BufferLen);
744
745         DumpVmbusChannel(Channel);
746
747         /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
748
749         /* Setup the descriptor */
750         desc.Type = Type; /* VmbusPacketTypeDataInBand; */
751         desc.Flags = Flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
752         /* in 8-bytes granularity */
753         desc.DataOffset8 = sizeof(struct vmpacket_descriptor) >> 3;
754         desc.Length8 = (u16)(packetLenAligned >> 3);
755         desc.TransactionId = RequestId;
756
757         sg_init_table(bufferList, 3);
758         sg_set_buf(&bufferList[0], &desc, sizeof(struct vmpacket_descriptor));
759         sg_set_buf(&bufferList[1], Buffer, BufferLen);
760         sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
761
762         ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
763
764         /* TODO: We should determine if this is optional */
765         if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
766                 VmbusChannelSetEvent(Channel);
767
768         DPRINT_EXIT(VMBUS);
769
770         return ret;
771 }
772 EXPORT_SYMBOL(VmbusChannelSendPacket);
773
774 /*
775  * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
776  * packets using a GPADL Direct packet type.
777  */
778 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel *Channel,
779                                      struct hv_page_buffer PageBuffers[],
780                                      u32 PageCount, void *Buffer, u32 BufferLen,
781                                      u64 RequestId)
782 {
783         int ret;
784         int i;
785         struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
786         u32 descSize;
787         u32 packetLen;
788         u32 packetLenAligned;
789         struct scatterlist bufferList[3];
790         u64 alignedData = 0;
791
792         if (PageCount > MAX_PAGE_BUFFER_COUNT)
793                 return -EINVAL;
794
795         DumpVmbusChannel(Channel);
796
797         /*
798          * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
799          * largest size we support
800          */
801         descSize = sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER) -
802                           ((MAX_PAGE_BUFFER_COUNT - PageCount) *
803                           sizeof(struct hv_page_buffer));
804         packetLen = descSize + BufferLen;
805         packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
806
807         /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
808
809         /* Setup the descriptor */
810         desc.Type = VmbusPacketTypeDataUsingGpaDirect;
811         desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
812         desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
813         desc.Length8 = (u16)(packetLenAligned >> 3);
814         desc.TransactionId = RequestId;
815         desc.RangeCount = PageCount;
816
817         for (i = 0; i < PageCount; i++) {
818                 desc.Range[i].Length = PageBuffers[i].Length;
819                 desc.Range[i].Offset = PageBuffers[i].Offset;
820                 desc.Range[i].Pfn        = PageBuffers[i].Pfn;
821         }
822
823         sg_init_table(bufferList, 3);
824         sg_set_buf(&bufferList[0], &desc, descSize);
825         sg_set_buf(&bufferList[1], Buffer, BufferLen);
826         sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
827
828         ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
829
830         /* TODO: We should determine if this is optional */
831         if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
832                 VmbusChannelSetEvent(Channel);
833
834         DPRINT_EXIT(VMBUS);
835
836         return ret;
837 }
838
839 /*
840  * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
841  * using a GPADL Direct packet type.
842  */
843 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel *Channel,
844                                 struct hv_multipage_buffer *MultiPageBuffer,
845                                 void *Buffer, u32 BufferLen, u64 RequestId)
846 {
847         int ret;
848         struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
849         u32 descSize;
850         u32 packetLen;
851         u32 packetLenAligned;
852         struct scatterlist bufferList[3];
853         u64 alignedData = 0;
854         u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset,
855                                          MultiPageBuffer->Length);
856
857         DumpVmbusChannel(Channel);
858
859         DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u",
860                    MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
861
862         if ((PfnCount < 0) || (PfnCount > MAX_MULTIPAGE_BUFFER_COUNT))
863                 return -EINVAL;
864
865         /*
866          * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
867          * the largest size we support
868          */
869         descSize = sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) -
870                           ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount) *
871                           sizeof(u64));
872         packetLen = descSize + BufferLen;
873         packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
874
875         /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
876
877         /* Setup the descriptor */
878         desc.Type = VmbusPacketTypeDataUsingGpaDirect;
879         desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
880         desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
881         desc.Length8 = (u16)(packetLenAligned >> 3);
882         desc.TransactionId = RequestId;
883         desc.RangeCount = 1;
884
885         desc.Range.Length = MultiPageBuffer->Length;
886         desc.Range.Offset = MultiPageBuffer->Offset;
887
888         memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray,
889                PfnCount * sizeof(u64));
890
891         sg_init_table(bufferList, 3);
892         sg_set_buf(&bufferList[0], &desc, descSize);
893         sg_set_buf(&bufferList[1], Buffer, BufferLen);
894         sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
895
896         ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
897
898         /* TODO: We should determine if this is optional */
899         if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
900                 VmbusChannelSetEvent(Channel);
901
902         DPRINT_EXIT(VMBUS);
903
904         return ret;
905 }
906
907
908 /**
909  * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
910  * @Channel: Pointer to vmbus_channel structure.
911  * @Buffer: Pointer to the buffer you want to receive the data into.
912  * @BufferLen: Maximum size of what the the buffer will hold
913  * @BufferActualLen: The actual size of the data after it was received
914  * @RequestId: Identifier of the request
915  *
916  * Receives directly from the hyper-v vmbus and puts the data it received
917  * into Buffer. This will receive the data unparsed from hyper-v.
918  *
919  * Mainly used by Hyper-V drivers.
920  */
921 int VmbusChannelRecvPacket(struct vmbus_channel *Channel, void *Buffer,
922                            u32 BufferLen, u32 *BufferActualLen, u64 *RequestId)
923 {
924         struct vmpacket_descriptor desc;
925         u32 packetLen;
926         u32 userLen;
927         int ret;
928         unsigned long flags;
929
930         *BufferActualLen = 0;
931         *RequestId = 0;
932
933         spin_lock_irqsave(&Channel->inbound_lock, flags);
934
935         ret = RingBufferPeek(&Channel->Inbound, &desc,
936                              sizeof(struct vmpacket_descriptor));
937         if (ret != 0) {
938                 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
939
940                 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
941                 DPRINT_EXIT(VMBUS);
942                 return 0;
943         }
944
945         /* VmbusChannelClearEvent(Channel); */
946
947         packetLen = desc.Length8 << 3;
948         userLen = packetLen - (desc.DataOffset8 << 3);
949         /* ASSERT(userLen > 0); */
950
951         DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
952                    "flag %d tid %llx pktlen %d datalen %d> ",
953                    Channel, Channel->OfferMsg.ChildRelId, desc.Type,
954                    desc.Flags, desc.TransactionId, packetLen, userLen);
955
956         *BufferActualLen = userLen;
957
958         if (userLen > BufferLen) {
959                 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
960
961                 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d",
962                            BufferLen, userLen);
963                 DPRINT_EXIT(VMBUS);
964
965                 return -1;
966         }
967
968         *RequestId = desc.TransactionId;
969
970         /* Copy over the packet to the user buffer */
971         ret = RingBufferRead(&Channel->Inbound, Buffer, userLen,
972                              (desc.DataOffset8 << 3));
973
974         spin_unlock_irqrestore(&Channel->inbound_lock, flags);
975
976         DPRINT_EXIT(VMBUS);
977
978         return 0;
979 }
980 EXPORT_SYMBOL(VmbusChannelRecvPacket);
981
982 /*
983  * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
984  */
985 int VmbusChannelRecvPacketRaw(struct vmbus_channel *Channel, void *Buffer,
986                               u32 BufferLen, u32 *BufferActualLen,
987                               u64 *RequestId)
988 {
989         struct vmpacket_descriptor desc;
990         u32 packetLen;
991         u32 userLen;
992         int ret;
993         unsigned long flags;
994
995         *BufferActualLen = 0;
996         *RequestId = 0;
997
998         spin_lock_irqsave(&Channel->inbound_lock, flags);
999
1000         ret = RingBufferPeek(&Channel->Inbound, &desc,
1001                              sizeof(struct vmpacket_descriptor));
1002         if (ret != 0) {
1003                 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1004
1005                 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1006                 DPRINT_EXIT(VMBUS);
1007                 return 0;
1008         }
1009
1010         /* VmbusChannelClearEvent(Channel); */
1011
1012         packetLen = desc.Length8 << 3;
1013         userLen = packetLen - (desc.DataOffset8 << 3);
1014
1015         DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
1016                    "flag %d tid %llx pktlen %d datalen %d> ",
1017                    Channel, Channel->OfferMsg.ChildRelId, desc.Type,
1018                    desc.Flags, desc.TransactionId, packetLen, userLen);
1019
1020         *BufferActualLen = packetLen;
1021
1022         if (packetLen > BufferLen) {
1023                 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1024
1025                 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but "
1026                            "got space for only %d bytes", packetLen, BufferLen);
1027                 DPRINT_EXIT(VMBUS);
1028                 return -2;
1029         }
1030
1031         *RequestId = desc.TransactionId;
1032
1033         /* Copy over the entire packet to the user buffer */
1034         ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1035
1036         spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1037
1038         DPRINT_EXIT(VMBUS);
1039
1040         return 0;
1041 }
1042
1043 /*
1044  * VmbusChannelOnChannelEvent - Channel event callback
1045  */
1046 void VmbusChannelOnChannelEvent(struct vmbus_channel *Channel)
1047 {
1048         DumpVmbusChannel(Channel);
1049         /* ASSERT(Channel->OnChannelCallback); */
1050
1051         Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1052
1053         mod_timer(&Channel->poll_timer, jiffies + usecs_to_jiffies(100));
1054 }
1055
1056 /*
1057  * VmbusChannelOnTimer - Timer event callback
1058  */
1059 void VmbusChannelOnTimer(unsigned long data)
1060 {
1061         struct vmbus_channel *channel = (struct vmbus_channel *)data;
1062
1063         if (channel->OnChannelCallback)
1064                 channel->OnChannelCallback(channel->ChannelCallbackContext);
1065 }
1066
1067 /*
1068  * DumpVmbusChannel - Dump vmbus channel info to the console
1069  */
1070 static void DumpVmbusChannel(struct vmbus_channel *Channel)
1071 {
1072         DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1073         DumpRingInfo(&Channel->Outbound, "Outbound ");
1074         DumpRingInfo(&Channel->Inbound, "Inbound ");
1075 }