Staging: hv: remove UINT32 and INT32 typedefs
[safe/jmp/linux-2.6] / drivers / staging / hv / Channel.c
1 /*
2  *
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include "include/osd.h"
26 #include "include/logging.h"
27
28 #include "VmbusPrivate.h"
29
30 //
31 // Internal routines
32 //
33 static int
34 VmbusChannelCreateGpadlHeader(
35         void *                                  Kbuffer,        // must be phys and virt contiguous
36         u32                                     Size,           // page-size multiple
37         VMBUS_CHANNEL_MSGINFO   **msgInfo,
38         u32                                     *MessageCount
39         );
40
41 static void
42 DumpVmbusChannel(
43         VMBUS_CHANNEL                   *Channel
44         );
45
46
47 static void
48 VmbusChannelSetEvent(
49         VMBUS_CHANNEL                   *Channel
50         );
51
52
53 #if 0
54 static void
55 DumpMonitorPage(
56         HV_MONITOR_PAGE *MonitorPage
57         )
58 {
59         int i=0;
60         int j=0;
61
62         DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d", MonitorPage, MonitorPage->TriggerState);
63
64         for (i=0; i<4; i++)
65         {
66                 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i, MonitorPage->TriggerGroup[i].AsUINT64);
67         }
68
69         for (i=0; i<4; i++)
70         {
71                 for (j=0; j<32; j++)
72                 {
73                         DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j, MonitorPage->Latency[i][j]);
74                 }
75         }
76         for (i=0; i<4; i++)
77         {
78                 for (j=0; j<32; j++)
79                 {
80                         DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].ConnectionId.Asu32);
81                         DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].FlagNumber);
82
83                 }
84         }
85 }
86 #endif
87
88 /*++
89
90 Name:
91         VmbusChannelSetEvent()
92
93 Description:
94         Trigger an event notification on the specified channel.
95
96 --*/
97 static void
98 VmbusChannelSetEvent(
99         VMBUS_CHANNEL                   *Channel
100         )
101 {
102         HV_MONITOR_PAGE *monitorPage;
103
104         DPRINT_ENTER(VMBUS);
105
106         if (Channel->OfferMsg.MonitorAllocated)
107         {
108                 // Each u32 represents 32 channels
109                 BitSet((u32*)gVmbusConnection.SendInterruptPage + (Channel->OfferMsg.ChildRelId >> 5), Channel->OfferMsg.ChildRelId & 31);
110
111                 monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
112                 monitorPage++; // Get the child to parent monitor page
113
114                 BitSet((u32*) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending, Channel->MonitorBit);
115         }
116         else
117         {
118                 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
119         }
120
121         DPRINT_EXIT(VMBUS);
122 }
123
124 #if 0
125 static void
126 VmbusChannelClearEvent(
127         VMBUS_CHANNEL                   *Channel
128         )
129 {
130         HV_MONITOR_PAGE *monitorPage;
131
132         DPRINT_ENTER(VMBUS);
133
134         if (Channel->OfferMsg.MonitorAllocated)
135         {
136                 // Each u32 represents 32 channels
137                 BitClear((u32*)gVmbusConnection.SendInterruptPage + (Channel->OfferMsg.ChildRelId >> 5), Channel->OfferMsg.ChildRelId & 31);
138
139                 monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
140                 monitorPage++; // Get the child to parent monitor page
141
142                 BitClear((u32*) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending, Channel->MonitorBit);
143         }
144
145         DPRINT_EXIT(VMBUS);
146 }
147
148 #endif
149 /*++;
150
151 Name:
152         VmbusChannelGetDebugInfo()
153
154 Description:
155         Retrieve various channel debug info
156
157 --*/
158 void
159 VmbusChannelGetDebugInfo(
160         VMBUS_CHANNEL                           *Channel,
161         VMBUS_CHANNEL_DEBUG_INFO        *DebugInfo
162         )
163 {
164         HV_MONITOR_PAGE *monitorPage;
165     u8 monitorGroup    = (u8)Channel->OfferMsg.MonitorId / 32;
166     u8 monitorOffset   = (u8)Channel->OfferMsg.MonitorId % 32;
167         //u32 monitorBit        = 1 << monitorOffset;
168
169         DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
170         DebugInfo->State = Channel->State;
171         memcpy(&DebugInfo->InterfaceType, &Channel->OfferMsg.Offer.InterfaceType, sizeof(GUID));
172         memcpy(&DebugInfo->InterfaceInstance, &Channel->OfferMsg.Offer.InterfaceInstance, sizeof(GUID));
173
174         monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
175
176         DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
177
178         DebugInfo->ServerMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
179         DebugInfo->ServerMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
180         DebugInfo->ServerMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
181
182         monitorPage++;
183
184         DebugInfo->ClientMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
185         DebugInfo->ClientMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
186         DebugInfo->ClientMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
187
188         RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
189         RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
190 }
191
192
193 /*++;
194
195 Name:
196         VmbusChannelOpen()
197
198 Description:
199         Open the specified channel.
200
201 --*/
202 int
203 VmbusChannelOpen(
204         VMBUS_CHANNEL                   *NewChannel,
205         u32                                     SendRingBufferSize,
206         u32                                     RecvRingBufferSize,
207         void *                                  UserData,
208         u32                                     UserDataLen,
209         PFN_CHANNEL_CALLBACK    pfnOnChannelCallback,
210         void *                                  Context
211         )
212 {
213         int ret=0;
214         VMBUS_CHANNEL_OPEN_CHANNEL* openMsg;
215         VMBUS_CHANNEL_MSGINFO* openInfo;
216         void *in, *out;
217
218         DPRINT_ENTER(VMBUS);
219
220         // Aligned to page size
221         ASSERT(!(SendRingBufferSize & (PAGE_SIZE -1)));
222         ASSERT(!(RecvRingBufferSize & (PAGE_SIZE -1)));
223
224         NewChannel->OnChannelCallback = pfnOnChannelCallback;
225         NewChannel->ChannelCallbackContext = Context;
226
227         // Allocate the ring buffer
228         out = PageAlloc((SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT);
229         //out = MemAllocZeroed(sendRingBufferSize + recvRingBufferSize);
230         ASSERT(out);
231         ASSERT(((ULONG_PTR)out & (PAGE_SIZE-1)) == 0);
232
233         in = (void*)((ULONG_PTR)out + SendRingBufferSize);
234
235         NewChannel->RingBufferPages = out;
236         NewChannel->RingBufferPageCount = (SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT;
237
238         RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
239
240         RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
241
242         // Establish the gpadl for the ring buffer
243         DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...", NewChannel);
244
245         NewChannel->RingBufferGpadlHandle = 0;
246
247         ret = VmbusChannelEstablishGpadl(NewChannel,
248                 NewChannel->Outbound.RingBuffer,
249                 SendRingBufferSize + RecvRingBufferSize,
250                 &NewChannel->RingBufferGpadlHandle);
251
252         DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p size %d recv ring %p size %d, downstreamoffset %d>",
253                 NewChannel,
254                 NewChannel->OfferMsg.ChildRelId,
255                 NewChannel->RingBufferGpadlHandle,
256                 NewChannel->Outbound.RingBuffer,
257                 NewChannel->Outbound.RingSize,
258                 NewChannel->Inbound.RingBuffer,
259                 NewChannel->Inbound.RingSize,
260                 SendRingBufferSize);
261
262         // Create and init the channel open message
263         openInfo =
264                 (VMBUS_CHANNEL_MSGINFO*)MemAlloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_OPEN_CHANNEL));
265         ASSERT(openInfo != NULL);
266
267         openInfo->WaitEvent = WaitEventCreate();
268
269         openMsg = (VMBUS_CHANNEL_OPEN_CHANNEL*)openInfo->Msg;
270         openMsg->Header.MessageType                             = ChannelMessageOpenChannel;
271         openMsg->OpenId                                                 = NewChannel->OfferMsg.ChildRelId; // FIXME
272     openMsg->ChildRelId                                         = NewChannel->OfferMsg.ChildRelId;
273     openMsg->RingBufferGpadlHandle                      = NewChannel->RingBufferGpadlHandle;
274     ASSERT(openMsg->RingBufferGpadlHandle);
275     openMsg->DownstreamRingBufferPageOffset             = SendRingBufferSize >> PAGE_SHIFT;
276     openMsg->ServerContextAreaGpadlHandle       = 0; // TODO
277
278         ASSERT(UserDataLen <= MAX_USER_DEFINED_BYTES);
279         if (UserDataLen)
280         {
281                 memcpy(openMsg->UserData, UserData, UserDataLen);
282         }
283
284         SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
285         INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &openInfo->MsgListEntry);
286         SpinlockRelease(gVmbusConnection.ChannelMsgLock);
287
288         DPRINT_DBG(VMBUS, "Sending channel open msg...");
289
290         ret = VmbusPostMessage(openMsg, sizeof(VMBUS_CHANNEL_OPEN_CHANNEL));
291         if (ret != 0)
292         {
293                 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
294                 goto Cleanup;
295         }
296
297         // FIXME: Need to time-out here
298         WaitEventWait(openInfo->WaitEvent);
299
300         if (openInfo->Response.OpenResult.Status == 0)
301         {
302                 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
303         }
304         else
305         {
306                 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!", NewChannel, openInfo->Response.OpenResult.Status);
307         }
308
309 Cleanup:
310         SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
311         REMOVE_ENTRY_LIST(&openInfo->MsgListEntry);
312         SpinlockRelease(gVmbusConnection.ChannelMsgLock);
313
314         WaitEventClose(openInfo->WaitEvent);
315         MemFree(openInfo);
316
317         DPRINT_EXIT(VMBUS);
318
319         return 0;
320 }
321
322 /*++;
323
324 Name:
325         DumpGpadlBody()
326
327 Description:
328         Dump the gpadl body message to the console for debugging purposes.
329
330 --*/
331 static void DumpGpadlBody(
332         VMBUS_CHANNEL_GPADL_BODY        *Gpadl,
333         u32                                             Len)
334 {
335         int i=0;
336         int pfnCount=0;
337
338         pfnCount = (Len - sizeof(VMBUS_CHANNEL_GPADL_BODY))/ sizeof(UINT64);
339         DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
340
341         for (i=0; i< pfnCount; i++)
342         {
343                 DPRINT_DBG(VMBUS, "gpadl body  - %d) pfn %llu", i, Gpadl->Pfn[i]);
344         }
345 }
346
347
348 /*++;
349
350 Name:
351         DumpGpadlHeader()
352
353 Description:
354         Dump the gpadl header message to the console for debugging purposes.
355
356 --*/
357 static void DumpGpadlHeader(
358         VMBUS_CHANNEL_GPADL_HEADER      *Gpadl
359         )
360 {
361         int i=0,j=0;
362         int pageCount=0;
363
364
365         DPRINT_DBG(VMBUS, "gpadl header - relid %d, range count %d, range buflen %d",
366                                 Gpadl->ChildRelId,
367                                 Gpadl->RangeCount,
368                                 Gpadl->RangeBufLen);
369         for (i=0; i< Gpadl->RangeCount; i++)
370         {
371                 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
372                 pageCount = (pageCount > 26)? 26 : pageCount;
373
374                 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d page count %d",
375                         i, Gpadl->Range[i].ByteCount, Gpadl->Range[i].ByteOffset, pageCount);
376
377                 for (j=0; j< pageCount; j++)
378                 {
379                         DPRINT_DBG(VMBUS, "%d) pfn %llu", j, Gpadl->Range[i].PfnArray[j]);
380                 }
381         }
382 }
383
384 /*++;
385
386 Name:
387         VmbusChannelCreateGpadlHeader()
388
389 Description:
390         Creates a gpadl for the specified buffer
391
392 --*/
393 static int
394 VmbusChannelCreateGpadlHeader(
395         void *                                  Kbuffer,        // from kmalloc()
396         u32                                     Size,           // page-size multiple
397         VMBUS_CHANNEL_MSGINFO   **MsgInfo,
398         u32                                     *MessageCount)
399 {
400         int i;
401         int pageCount;
402     unsigned long long pfn;
403         VMBUS_CHANNEL_GPADL_HEADER* gpaHeader;
404         VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
405         VMBUS_CHANNEL_MSGINFO* msgHeader;
406         VMBUS_CHANNEL_MSGINFO* msgBody;
407         u32                             msgSize;
408
409         int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
410
411         //ASSERT( (kbuffer & (PAGE_SIZE-1)) == 0);
412         ASSERT( (Size & (PAGE_SIZE-1)) == 0);
413
414         pageCount = Size >> PAGE_SHIFT;
415         pfn = GetPhysicalAddress(Kbuffer) >> PAGE_SHIFT;
416
417         // do we need a gpadl body msg
418         pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_HEADER) - sizeof(GPA_RANGE);
419         pfnCount = pfnSize / sizeof(UINT64);
420
421         if (pageCount > pfnCount) // we need a gpadl body
422         {
423                 // fill in the header
424                 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pfnCount*sizeof(UINT64);
425                 msgHeader =  MemAllocZeroed(msgSize);
426
427                 INITIALIZE_LIST_HEAD(&msgHeader->SubMsgList);
428                 msgHeader->MessageSize=msgSize;
429
430                 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
431                 gpaHeader->RangeCount = 1;
432                 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(UINT64);
433                 gpaHeader->Range[0].ByteOffset = 0;
434                 gpaHeader->Range[0].ByteCount = Size;
435                 for (i=0; i<pfnCount; i++)
436                 {
437                         gpaHeader->Range[0].PfnArray[i] = pfn+i;
438                 }
439                 *MsgInfo = msgHeader;
440                 *MessageCount = 1;
441
442                 pfnSum = pfnCount;
443                 pfnLeft = pageCount - pfnCount;
444
445                 // how many pfns can we fit
446                 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_BODY);
447                 pfnCount = pfnSize / sizeof(UINT64);
448
449                 // fill in the body
450                 while (pfnLeft)
451                 {
452                         if (pfnLeft > pfnCount)
453                         {
454                                 pfnCurr = pfnCount;
455                         }
456                         else
457                         {
458                                 pfnCurr = pfnLeft;
459                         }
460
461                         msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_BODY) + pfnCurr*sizeof(UINT64);
462                         msgBody =  MemAllocZeroed(msgSize);
463                         ASSERT(msgBody);
464                         msgBody->MessageSize = msgSize;
465                         (*MessageCount)++;
466                         gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)msgBody->Msg;
467
468                         // FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit
469                         //gpadlBody->Gpadl = kbuffer;
470                         for (i=0; i<pfnCurr; i++)
471                         {
472                                 gpadlBody->Pfn[i] = pfn + pfnSum + i;
473                         }
474
475                         // add to msg header
476                         INSERT_TAIL_LIST(&msgHeader->SubMsgList, &msgBody->MsgListEntry);
477                         pfnSum += pfnCurr;
478                         pfnLeft -= pfnCurr;
479                 }
480         }
481         else
482         {
483                 // everything fits in a header
484                 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pageCount*sizeof(UINT64);
485                 msgHeader =  MemAllocZeroed(msgSize);
486                 msgHeader->MessageSize=msgSize;
487
488                 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
489                 gpaHeader->RangeCount = 1;
490                 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(UINT64);
491                 gpaHeader->Range[0].ByteOffset = 0;
492                 gpaHeader->Range[0].ByteCount = Size;
493                 for (i=0; i<pageCount; i++)
494                 {
495                         gpaHeader->Range[0].PfnArray[i] = pfn+i;
496                 }
497
498                 *MsgInfo = msgHeader;
499                 *MessageCount = 1;
500         }
501
502         return 0;
503 }
504
505
506 /*++;
507
508 Name:
509         VmbusChannelEstablishGpadl()
510
511 Description:
512         Estabish a GPADL for the specified buffer
513
514 --*/
515 int
516 VmbusChannelEstablishGpadl(
517         VMBUS_CHANNEL   *Channel,
518         void *                  Kbuffer,        // from kmalloc()
519         u32                     Size,           // page-size multiple
520         u32                     *GpadlHandle
521         )
522 {
523         int ret=0;
524         VMBUS_CHANNEL_GPADL_HEADER* gpadlMsg;
525         VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
526         //VMBUS_CHANNEL_GPADL_CREATED* gpadlCreated;
527
528         VMBUS_CHANNEL_MSGINFO *msgInfo;
529         VMBUS_CHANNEL_MSGINFO *subMsgInfo;
530
531         u32 msgCount;
532         LIST_ENTRY* anchor;
533         LIST_ENTRY* curr;
534         u32 nextGpadlHandle;
535
536         DPRINT_ENTER(VMBUS);
537
538         nextGpadlHandle = gVmbusConnection.NextGpadlHandle;
539         InterlockedIncrement((int*)&gVmbusConnection.NextGpadlHandle);
540
541         VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
542         ASSERT(msgInfo != NULL);
543         ASSERT(msgCount >0);
544
545         msgInfo->WaitEvent = WaitEventCreate();
546         gpadlMsg = (VMBUS_CHANNEL_GPADL_HEADER*)msgInfo->Msg;
547         gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
548         gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
549         gpadlMsg->Gpadl = nextGpadlHandle;
550
551         DumpGpadlHeader(gpadlMsg);
552
553         SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
554         INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
555         SpinlockRelease(gVmbusConnection.ChannelMsgLock);
556
557         DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d", Kbuffer, Size, msgCount);
558
559         DPRINT_DBG(VMBUS, "Sending GPADL Header - len %d", msgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
560
561         ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
562         if (ret != 0)
563         {
564                 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
565                 goto Cleanup;
566         }
567
568         if (msgCount>1)
569         {
570                 ITERATE_LIST_ENTRIES(anchor, curr, &msgInfo->SubMsgList)
571                 {
572                         subMsgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
573                         gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)subMsgInfo->Msg;
574
575                         gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
576                         gpadlBody->Gpadl = nextGpadlHandle;
577
578                         DPRINT_DBG(VMBUS, "Sending GPADL Body - len %d", subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
579
580                         DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
581                         ret = VmbusPostMessage(gpadlBody, subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
582                         ASSERT(ret == 0);
583                 }
584         }
585         WaitEventWait(msgInfo->WaitEvent);
586
587         // At this point, we received the gpadl created msg
588         DPRINT_DBG(VMBUS, "Received GPADL created (relid %d, status %d handle %x)",
589                 Channel->OfferMsg.ChildRelId,
590                 msgInfo->Response.GpadlCreated.CreationStatus,
591                 gpadlMsg->Gpadl);
592
593         *GpadlHandle = gpadlMsg->Gpadl;
594
595 Cleanup:
596         SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
597         REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
598         SpinlockRelease(gVmbusConnection.ChannelMsgLock);
599
600         WaitEventClose(msgInfo->WaitEvent);
601         MemFree(msgInfo);
602
603         DPRINT_EXIT(VMBUS);
604
605         return ret;
606 }
607
608
609
610 /*++;
611
612 Name:
613         VmbusChannelTeardownGpadl()
614
615 Description:
616         Teardown the specified GPADL handle
617
618 --*/
619 int
620 VmbusChannelTeardownGpadl(
621         VMBUS_CHANNEL   *Channel,
622         u32                     GpadlHandle
623         )
624 {
625         int ret=0;
626         VMBUS_CHANNEL_GPADL_TEARDOWN *msg;
627         VMBUS_CHANNEL_MSGINFO* info;
628
629         DPRINT_ENTER(VMBUS);
630
631         ASSERT(GpadlHandle != 0);
632
633         info =
634                 (VMBUS_CHANNEL_MSGINFO*)MemAlloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN));
635         ASSERT(info != NULL);
636
637         info->WaitEvent = WaitEventCreate();
638
639         msg = (VMBUS_CHANNEL_GPADL_TEARDOWN*)info->Msg;
640
641         msg->Header.MessageType = ChannelMessageGpadlTeardown;
642     msg->ChildRelId  = Channel->OfferMsg.ChildRelId;
643     msg->Gpadl       = GpadlHandle;
644
645         SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
646         INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &info->MsgListEntry);
647         SpinlockRelease(gVmbusConnection.ChannelMsgLock);
648
649         ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN));
650         if (ret != 0)
651         {
652                 // TODO:
653         }
654
655         WaitEventWait(info->WaitEvent);
656
657         // Received a torndown response
658         SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
659         REMOVE_ENTRY_LIST(&info->MsgListEntry);
660         SpinlockRelease(gVmbusConnection.ChannelMsgLock);
661
662         WaitEventClose(info->WaitEvent);
663         MemFree(info);
664
665         DPRINT_EXIT(VMBUS);
666
667         return ret;
668 }
669
670
671 /*++
672
673 Name:
674         VmbusChannelClose()
675
676 Description:
677         Close the specified channel
678
679 --*/
680 void
681 VmbusChannelClose(
682         VMBUS_CHANNEL   *Channel
683         )
684 {
685         int ret=0;
686         VMBUS_CHANNEL_CLOSE_CHANNEL* msg;
687         VMBUS_CHANNEL_MSGINFO* info;
688
689         DPRINT_ENTER(VMBUS);
690
691         // Stop callback and cancel the timer asap
692         Channel->OnChannelCallback = NULL;
693         TimerStop(Channel->PollTimer);
694
695         // Send a closing message
696         info =
697                 (VMBUS_CHANNEL_MSGINFO*)MemAlloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL));
698         ASSERT(info != NULL);
699
700         //info->waitEvent = WaitEventCreate();
701
702         msg = (VMBUS_CHANNEL_CLOSE_CHANNEL*)info->Msg;
703         msg->Header.MessageType                         = ChannelMessageCloseChannel;
704     msg->ChildRelId                                             = Channel->OfferMsg.ChildRelId;
705
706         ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL));
707         if (ret != 0)
708         {
709                 // TODO:
710         }
711
712         // Tear down the gpadl for the channel's ring buffer
713         if (Channel->RingBufferGpadlHandle)
714         {
715                 VmbusChannelTeardownGpadl(Channel, Channel->RingBufferGpadlHandle);
716         }
717
718         // TODO: Send a msg to release the childRelId
719
720         // Cleanup the ring buffers for this channel
721         RingBufferCleanup(&Channel->Outbound);
722         RingBufferCleanup(&Channel->Inbound);
723
724         PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
725
726         MemFree(info);
727
728         // If we are closing the channel during an error path in opening the channel, don't free the channel
729         // since the caller will free the channel
730         if (Channel->State == CHANNEL_OPEN_STATE)
731         {
732                 SpinlockAcquire(gVmbusConnection.ChannelLock);
733                 REMOVE_ENTRY_LIST(&Channel->ListEntry);
734                 SpinlockRelease(gVmbusConnection.ChannelLock);
735
736                 FreeVmbusChannel(Channel);
737         }
738
739         DPRINT_EXIT(VMBUS);
740 }
741
742
743 /*++
744
745 Name:
746         VmbusChannelSendPacket()
747
748 Description:
749         Send the specified buffer on the given channel
750
751 --*/
752 int
753 VmbusChannelSendPacket(
754         VMBUS_CHANNEL           *Channel,
755         const void *                    Buffer,
756         u32                             BufferLen,
757         UINT64                          RequestId,
758         VMBUS_PACKET_TYPE       Type,
759         u32                             Flags
760 )
761 {
762         int ret=0;
763         VMPACKET_DESCRIPTOR desc;
764         u32 packetLen = sizeof(VMPACKET_DESCRIPTOR) + BufferLen;
765         u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(UINT64));
766         SG_BUFFER_LIST bufferList[3];
767         UINT64 alignedData=0;
768
769         DPRINT_ENTER(VMBUS);
770         DPRINT_DBG(VMBUS, "channel %p buffer %p len %d", Channel, Buffer, BufferLen);
771
772         DumpVmbusChannel(Channel);
773
774         ASSERT((packetLenAligned - packetLen) < sizeof(UINT64));
775
776         // Setup the descriptor
777         desc.Type = Type;//VmbusPacketTypeDataInBand;
778         desc.Flags = Flags;//VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
779     desc.DataOffset8 = sizeof(VMPACKET_DESCRIPTOR) >> 3; // in 8-bytes granularity
780     desc.Length8 = (u16)(packetLenAligned >> 3);
781     desc.TransactionId = RequestId;
782
783         bufferList[0].Data = &desc;
784         bufferList[0].Length = sizeof(VMPACKET_DESCRIPTOR);
785
786         bufferList[1].Data = Buffer;
787         bufferList[1].Length = BufferLen;
788
789         bufferList[2].Data = &alignedData;
790         bufferList[2].Length = packetLenAligned - packetLen;
791
792         ret = RingBufferWrite(
793                 &Channel->Outbound,
794                 bufferList,
795                 3);
796
797         // TODO: We should determine if this is optional
798         if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
799         {
800                 VmbusChannelSetEvent(Channel);
801         }
802
803         DPRINT_EXIT(VMBUS);
804
805         return ret;
806 }
807
808
809 /*++
810
811 Name:
812         VmbusChannelSendPacketPageBuffer()
813
814 Description:
815         Send a range of single-page buffer packets using a GPADL Direct packet type.
816
817 --*/
818 int
819 VmbusChannelSendPacketPageBuffer(
820         VMBUS_CHANNEL           *Channel,
821         PAGE_BUFFER                     PageBuffers[],
822         u32                             PageCount,
823         void *                          Buffer,
824         u32                             BufferLen,
825         UINT64                          RequestId
826 )
827 {
828         int ret=0;
829         int i=0;
830         VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
831         u32 descSize;
832         u32 packetLen;
833         u32 packetLenAligned;
834         SG_BUFFER_LIST bufferList[3];
835         UINT64 alignedData=0;
836
837         DPRINT_ENTER(VMBUS);
838
839         ASSERT(PageCount <= MAX_PAGE_BUFFER_COUNT);
840
841         DumpVmbusChannel(Channel);
842
843         // Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support
844         descSize = sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER) - ((MAX_PAGE_BUFFER_COUNT - PageCount)*sizeof(PAGE_BUFFER));
845         packetLen = descSize + BufferLen;
846         packetLenAligned = ALIGN_UP(packetLen, sizeof(UINT64));
847
848         ASSERT((packetLenAligned - packetLen) < sizeof(UINT64));
849
850         // Setup the descriptor
851         desc.Type = VmbusPacketTypeDataUsingGpaDirect;
852         desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
853     desc.DataOffset8 = descSize >> 3; // in 8-bytes grandularity
854     desc.Length8 = (u16)(packetLenAligned >> 3);
855     desc.TransactionId = RequestId;
856         desc.RangeCount = PageCount;
857
858         for (i=0; i<PageCount; i++)
859         {
860                 desc.Range[i].Length = PageBuffers[i].Length;
861                 desc.Range[i].Offset = PageBuffers[i].Offset;
862                 desc.Range[i].Pfn        = PageBuffers[i].Pfn;
863         }
864
865         bufferList[0].Data = &desc;
866         bufferList[0].Length = descSize;
867
868         bufferList[1].Data = Buffer;
869         bufferList[1].Length = BufferLen;
870
871         bufferList[2].Data = &alignedData;
872         bufferList[2].Length = packetLenAligned - packetLen;
873
874         ret = RingBufferWrite(
875                 &Channel->Outbound,
876                 bufferList,
877                 3);
878
879         // TODO: We should determine if this is optional
880         if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
881         {
882                 VmbusChannelSetEvent(Channel);
883         }
884
885         DPRINT_EXIT(VMBUS);
886
887         return ret;
888 }
889
890
891
892 /*++
893
894 Name:
895         VmbusChannelSendPacketMultiPageBuffer()
896
897 Description:
898         Send a multi-page buffer packet using a GPADL Direct packet type.
899
900 --*/
901 int
902 VmbusChannelSendPacketMultiPageBuffer(
903         VMBUS_CHANNEL           *Channel,
904         MULTIPAGE_BUFFER        *MultiPageBuffer,
905         void *                          Buffer,
906         u32                             BufferLen,
907         UINT64                          RequestId
908 )
909 {
910         int ret=0;
911         VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
912         u32 descSize;
913         u32 packetLen;
914         u32 packetLenAligned;
915         SG_BUFFER_LIST bufferList[3];
916         UINT64 alignedData=0;
917         u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset, MultiPageBuffer->Length);
918
919         DPRINT_ENTER(VMBUS);
920
921         DumpVmbusChannel(Channel);
922
923         DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
924
925         ASSERT(PfnCount > 0);
926         ASSERT(PfnCount <= MAX_MULTIPAGE_BUFFER_COUNT);
927
928         // Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support
929         descSize = sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) - ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount)*sizeof(UINT64));
930         packetLen = descSize + BufferLen;
931         packetLenAligned = ALIGN_UP(packetLen, sizeof(UINT64));
932
933         ASSERT((packetLenAligned - packetLen) < sizeof(UINT64));
934
935         // Setup the descriptor
936         desc.Type = VmbusPacketTypeDataUsingGpaDirect;
937         desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
938     desc.DataOffset8 = descSize >> 3; // in 8-bytes grandularity
939     desc.Length8 = (u16)(packetLenAligned >> 3);
940     desc.TransactionId = RequestId;
941         desc.RangeCount = 1;
942
943         desc.Range.Length = MultiPageBuffer->Length;
944         desc.Range.Offset = MultiPageBuffer->Offset;
945
946         memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray, PfnCount*sizeof(UINT64));
947
948         bufferList[0].Data = &desc;
949         bufferList[0].Length = descSize;
950
951         bufferList[1].Data = Buffer;
952         bufferList[1].Length = BufferLen;
953
954         bufferList[2].Data = &alignedData;
955         bufferList[2].Length = packetLenAligned - packetLen;
956
957         ret = RingBufferWrite(
958                 &Channel->Outbound,
959                 bufferList,
960                 3);
961
962         // TODO: We should determine if this is optional
963         if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
964         {
965                 VmbusChannelSetEvent(Channel);
966         }
967
968         DPRINT_EXIT(VMBUS);
969
970         return ret;
971 }
972
973
974 /*++
975
976 Name:
977         VmbusChannelRecvPacket()
978
979 Description:
980         Retrieve the user packet on the specified channel
981
982 --*/
983 // TODO: Do we ever receive a gpa direct packet other than the ones we send ?
984 int
985 VmbusChannelRecvPacket(
986         VMBUS_CHANNEL           *Channel,
987         void *                          Buffer,
988         u32                             BufferLen,
989         u32*                            BufferActualLen,
990         UINT64*                         RequestId
991         )
992 {
993         VMPACKET_DESCRIPTOR desc;
994         u32 packetLen;
995         u32 userLen;
996         int ret;
997
998         DPRINT_ENTER(VMBUS);
999
1000         *BufferActualLen = 0;
1001         *RequestId = 0;
1002
1003         SpinlockAcquire(Channel->InboundLock);
1004
1005         ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1006         if (ret != 0)
1007         {
1008                 SpinlockRelease(Channel->InboundLock);
1009
1010                 //DPRINT_DBG(VMBUS, "nothing to read!!");
1011                 DPRINT_EXIT(VMBUS);
1012                 return 0;
1013         }
1014
1015         //VmbusChannelClearEvent(Channel);
1016
1017         packetLen = desc.Length8 << 3;
1018         userLen = packetLen - (desc.DataOffset8 << 3);
1019         //ASSERT(userLen > 0);
1020
1021         DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1022                 Channel,
1023                 Channel->OfferMsg.ChildRelId,
1024                 desc.Type,
1025                 desc.Flags,
1026                 desc.TransactionId, packetLen, userLen);
1027
1028         *BufferActualLen = userLen;
1029
1030         if (userLen > BufferLen)
1031         {
1032                 SpinlockRelease(Channel->InboundLock);
1033
1034                 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d", BufferLen, userLen);
1035                 DPRINT_EXIT(VMBUS);
1036
1037                 return -1;
1038         }
1039
1040         *RequestId = desc.TransactionId;
1041
1042         // Copy over the packet to the user buffer
1043         ret = RingBufferRead(&Channel->Inbound, Buffer, userLen, (desc.DataOffset8 << 3));
1044
1045         SpinlockRelease(Channel->InboundLock);
1046
1047         DPRINT_EXIT(VMBUS);
1048
1049         return 0;
1050 }
1051
1052 /*++
1053
1054 Name:
1055         VmbusChannelRecvPacketRaw()
1056
1057 Description:
1058         Retrieve the raw packet on the specified channel
1059
1060 --*/
1061 int
1062 VmbusChannelRecvPacketRaw(
1063         VMBUS_CHANNEL           *Channel,
1064         void *                          Buffer,
1065         u32                             BufferLen,
1066         u32*                            BufferActualLen,
1067         UINT64*                         RequestId
1068         )
1069 {
1070         VMPACKET_DESCRIPTOR desc;
1071         u32 packetLen;
1072         u32 userLen;
1073         int ret;
1074
1075         DPRINT_ENTER(VMBUS);
1076
1077         *BufferActualLen = 0;
1078         *RequestId = 0;
1079
1080         SpinlockAcquire(Channel->InboundLock);
1081
1082         ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1083         if (ret != 0)
1084         {
1085                 SpinlockRelease(Channel->InboundLock);
1086
1087                 //DPRINT_DBG(VMBUS, "nothing to read!!");
1088                 DPRINT_EXIT(VMBUS);
1089                 return 0;
1090         }
1091
1092         //VmbusChannelClearEvent(Channel);
1093
1094         packetLen = desc.Length8 << 3;
1095         userLen = packetLen - (desc.DataOffset8 << 3);
1096
1097         DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1098                 Channel,
1099                 Channel->OfferMsg.ChildRelId,
1100                 desc.Type,
1101                 desc.Flags,
1102                 desc.TransactionId, packetLen, userLen);
1103
1104         *BufferActualLen = packetLen;
1105
1106         if (packetLen > BufferLen)
1107         {
1108                 SpinlockRelease(Channel->InboundLock);
1109
1110                 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen, BufferLen);
1111                 DPRINT_EXIT(VMBUS);
1112                 return -2;
1113         }
1114
1115         *RequestId = desc.TransactionId;
1116
1117         // Copy over the entire packet to the user buffer
1118         ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1119
1120         SpinlockRelease(Channel->InboundLock);
1121
1122         DPRINT_EXIT(VMBUS);
1123
1124         return 0;
1125 }
1126
1127
1128 /*++
1129
1130 Name:
1131         VmbusChannelOnChannelEvent()
1132
1133 Description:
1134         Channel event callback
1135
1136 --*/
1137 void
1138 VmbusChannelOnChannelEvent(
1139         VMBUS_CHANNEL           *Channel
1140         )
1141 {
1142         DumpVmbusChannel(Channel);
1143         ASSERT(Channel->OnChannelCallback);
1144 #ifdef ENABLE_POLLING
1145         TimerStop(Channel->PollTimer);
1146         Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1147         TimerStart(Channel->PollTimer, 100 /* 100us */);
1148 #else
1149         Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1150 #endif
1151 }
1152
1153 /*++
1154
1155 Name:
1156         VmbusChannelOnTimer()
1157
1158 Description:
1159         Timer event callback
1160
1161 --*/
1162 void
1163 VmbusChannelOnTimer(
1164         void            *Context
1165         )
1166 {
1167         VMBUS_CHANNEL *channel = (VMBUS_CHANNEL*)Context;
1168
1169         if (channel->OnChannelCallback)
1170         {
1171                 channel->OnChannelCallback(channel->ChannelCallbackContext);
1172 #ifdef ENABLE_POLLING
1173                 TimerStart(channel->PollTimer, 100 /* 100us */);
1174 #endif
1175         }
1176 }
1177
1178
1179 /*++
1180
1181 Name:
1182         DumpVmbusChannel()
1183
1184 Description:
1185         Dump vmbus channel info to the console
1186
1187 --*/
1188 static void
1189 DumpVmbusChannel(
1190         VMBUS_CHANNEL           *Channel
1191         )
1192 {
1193         DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1194         DumpRingInfo(&Channel->Outbound, "Outbound ");
1195         DumpRingInfo(&Channel->Inbound, "Inbound ");
1196 }
1197
1198
1199 // eof