]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/hv/Channel.c
Staging: hv: remove ULONGLONG and LONGLONG typedefs
[net-next-2.6.git] / drivers / staging / hv / Channel.c
CommitLineData
3e7ee490
HJ
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
5654e932 24#include <linux/kernel.h>
09d50ff8
GKH
25#include "include/osd.h"
26#include "include/logging.h"
3e7ee490
HJ
27
28#include "VmbusPrivate.h"
29
30//
31// Internal routines
32//
33static int
34VmbusChannelCreateGpadlHeader(
8282c400 35 void * Kbuffer, // must be phys and virt contiguous
4d643114 36 u32 Size, // page-size multiple
3e7ee490 37 VMBUS_CHANNEL_MSGINFO **msgInfo,
4d643114 38 u32 *MessageCount
3e7ee490
HJ
39 );
40
41static void
42DumpVmbusChannel(
43 VMBUS_CHANNEL *Channel
44 );
45
46
47static void
48VmbusChannelSetEvent(
49 VMBUS_CHANNEL *Channel
50 );
51
52
53#if 0
54static void
55DumpMonitorPage(
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 {
4d643114 80 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].ConnectionId.Asu32);
3e7ee490
HJ
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
90Name:
91 VmbusChannelSetEvent()
92
93Description:
94 Trigger an event notification on the specified channel.
95
96--*/
97static void
98VmbusChannelSetEvent(
99 VMBUS_CHANNEL *Channel
100 )
101{
102 HV_MONITOR_PAGE *monitorPage;
103
104 DPRINT_ENTER(VMBUS);
105
106 if (Channel->OfferMsg.MonitorAllocated)
107 {
4d643114
GKH
108 // Each u32 represents 32 channels
109 BitSet((u32*)gVmbusConnection.SendInterruptPage + (Channel->OfferMsg.ChildRelId >> 5), Channel->OfferMsg.ChildRelId & 31);
3e7ee490
HJ
110
111 monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
112 monitorPage++; // Get the child to parent monitor page
113
4d643114 114 BitSet((u32*) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending, Channel->MonitorBit);
3e7ee490
HJ
115 }
116 else
117 {
118 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
119 }
120
121 DPRINT_EXIT(VMBUS);
122}
123
124#if 0
125static void
126VmbusChannelClearEvent(
127 VMBUS_CHANNEL *Channel
128 )
129{
130 HV_MONITOR_PAGE *monitorPage;
131
132 DPRINT_ENTER(VMBUS);
133
134 if (Channel->OfferMsg.MonitorAllocated)
135 {
4d643114
GKH
136 // Each u32 represents 32 channels
137 BitClear((u32*)gVmbusConnection.SendInterruptPage + (Channel->OfferMsg.ChildRelId >> 5), Channel->OfferMsg.ChildRelId & 31);
3e7ee490
HJ
138
139 monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
140 monitorPage++; // Get the child to parent monitor page
141
4d643114 142 BitClear((u32*) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending, Channel->MonitorBit);
3e7ee490
HJ
143 }
144
145 DPRINT_EXIT(VMBUS);
146}
147
148#endif
149/*++;
150
151Name:
152 VmbusChannelGetDebugInfo()
153
154Description:
155 Retrieve various channel debug info
156
157--*/
158void
159VmbusChannelGetDebugInfo(
160 VMBUS_CHANNEL *Channel,
161 VMBUS_CHANNEL_DEBUG_INFO *DebugInfo
162 )
163{
164 HV_MONITOR_PAGE *monitorPage;
5654e932
GKH
165 u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
166 u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
4d643114 167 //u32 monitorBit = 1 << monitorOffset;
3e7ee490
HJ
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
195Name:
196 VmbusChannelOpen()
197
198Description:
199 Open the specified channel.
200
201--*/
202int
203VmbusChannelOpen(
204 VMBUS_CHANNEL *NewChannel,
4d643114
GKH
205 u32 SendRingBufferSize,
206 u32 RecvRingBufferSize,
8282c400 207 void * UserData,
4d643114 208 u32 UserDataLen,
3e7ee490 209 PFN_CHANNEL_CALLBACK pfnOnChannelCallback,
8282c400 210 void * Context
3e7ee490
HJ
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
309Cleanup:
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
324Name:
325 DumpGpadlBody()
326
327Description:
328 Dump the gpadl body message to the console for debugging purposes.
329
330--*/
331static void DumpGpadlBody(
332 VMBUS_CHANNEL_GPADL_BODY *Gpadl,
4d643114 333 u32 Len)
3e7ee490
HJ
334{
335 int i=0;
336 int pfnCount=0;
337
59471438 338 pfnCount = (Len - sizeof(VMBUS_CHANNEL_GPADL_BODY))/ sizeof(u64);
3e7ee490
HJ
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
350Name:
351 DumpGpadlHeader()
352
353Description:
354 Dump the gpadl header message to the console for debugging purposes.
355
356--*/
357static 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
386Name:
387 VmbusChannelCreateGpadlHeader()
388
389Description:
390 Creates a gpadl for the specified buffer
391
392--*/
393static int
394VmbusChannelCreateGpadlHeader(
8282c400 395 void * Kbuffer, // from kmalloc()
4d643114 396 u32 Size, // page-size multiple
3e7ee490 397 VMBUS_CHANNEL_MSGINFO **MsgInfo,
4d643114 398 u32 *MessageCount)
3e7ee490
HJ
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;
4d643114 407 u32 msgSize;
3e7ee490
HJ
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);
59471438 419 pfnCount = pfnSize / sizeof(u64);
3e7ee490
HJ
420
421 if (pageCount > pfnCount) // we need a gpadl body
422 {
423 // fill in the header
59471438 424 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pfnCount*sizeof(u64);
3e7ee490
HJ
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;
59471438 432 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
3e7ee490
HJ
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);
59471438 447 pfnCount = pfnSize / sizeof(u64);
3e7ee490
HJ
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
59471438 461 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_BODY) + pfnCurr*sizeof(u64);
3e7ee490
HJ
462 msgBody = MemAllocZeroed(msgSize);
463 ASSERT(msgBody);
464 msgBody->MessageSize = msgSize;
465 (*MessageCount)++;
466 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)msgBody->Msg;
467
4d643114 468 // FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit
3e7ee490
HJ
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
59471438 484 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pageCount*sizeof(u64);
3e7ee490
HJ
485 msgHeader = MemAllocZeroed(msgSize);
486 msgHeader->MessageSize=msgSize;
487
488 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
489 gpaHeader->RangeCount = 1;
59471438 490 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
3e7ee490
HJ
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
508Name:
509 VmbusChannelEstablishGpadl()
510
511Description:
512 Estabish a GPADL for the specified buffer
513
514--*/
515int
516VmbusChannelEstablishGpadl(
517 VMBUS_CHANNEL *Channel,
8282c400 518 void * Kbuffer, // from kmalloc()
4d643114
GKH
519 u32 Size, // page-size multiple
520 u32 *GpadlHandle
3e7ee490
HJ
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
4d643114 531 u32 msgCount;
3e7ee490
HJ
532 LIST_ENTRY* anchor;
533 LIST_ENTRY* curr;
4d643114 534 u32 nextGpadlHandle;
3e7ee490
HJ
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
595Cleanup:
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
612Name:
613 VmbusChannelTeardownGpadl()
614
615Description:
616 Teardown the specified GPADL handle
617
618--*/
619int
620VmbusChannelTeardownGpadl(
621 VMBUS_CHANNEL *Channel,
4d643114 622 u32 GpadlHandle
3e7ee490
HJ
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
673Name:
674 VmbusChannelClose()
675
676Description:
677 Close the specified channel
678
679--*/
e20f683b 680void
3e7ee490
HJ
681VmbusChannelClose(
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
745Name:
746 VmbusChannelSendPacket()
747
748Description:
749 Send the specified buffer on the given channel
750
751--*/
752int
753VmbusChannelSendPacket(
754 VMBUS_CHANNEL *Channel,
8282c400 755 const void * Buffer,
4d643114 756 u32 BufferLen,
59471438 757 u64 RequestId,
3e7ee490 758 VMBUS_PACKET_TYPE Type,
4d643114 759 u32 Flags
3e7ee490
HJ
760)
761{
762 int ret=0;
763 VMPACKET_DESCRIPTOR desc;
4d643114 764 u32 packetLen = sizeof(VMPACKET_DESCRIPTOR) + BufferLen;
59471438 765 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 766 SG_BUFFER_LIST bufferList[3];
59471438 767 u64 alignedData=0;
3e7ee490
HJ
768
769 DPRINT_ENTER(VMBUS);
770 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d", Channel, Buffer, BufferLen);
771
772 DumpVmbusChannel(Channel);
773
59471438 774 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
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
5cd508bc 780 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
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
811Name:
812 VmbusChannelSendPacketPageBuffer()
813
814Description:
815 Send a range of single-page buffer packets using a GPADL Direct packet type.
816
817--*/
818int
819VmbusChannelSendPacketPageBuffer(
820 VMBUS_CHANNEL *Channel,
821 PAGE_BUFFER PageBuffers[],
4d643114 822 u32 PageCount,
8282c400 823 void * Buffer,
4d643114 824 u32 BufferLen,
59471438 825 u64 RequestId
3e7ee490
HJ
826)
827{
828 int ret=0;
829 int i=0;
830 VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
4d643114
GKH
831 u32 descSize;
832 u32 packetLen;
833 u32 packetLenAligned;
3e7ee490 834 SG_BUFFER_LIST bufferList[3];
59471438 835 u64 alignedData=0;
3e7ee490
HJ
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;
59471438 846 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 847
59471438 848 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
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
5cd508bc 854 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
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
894Name:
895 VmbusChannelSendPacketMultiPageBuffer()
896
897Description:
898 Send a multi-page buffer packet using a GPADL Direct packet type.
899
900--*/
901int
902VmbusChannelSendPacketMultiPageBuffer(
903 VMBUS_CHANNEL *Channel,
904 MULTIPAGE_BUFFER *MultiPageBuffer,
8282c400 905 void * Buffer,
4d643114 906 u32 BufferLen,
59471438 907 u64 RequestId
3e7ee490
HJ
908)
909{
910 int ret=0;
911 VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
4d643114
GKH
912 u32 descSize;
913 u32 packetLen;
914 u32 packetLenAligned;
3e7ee490 915 SG_BUFFER_LIST bufferList[3];
59471438 916 u64 alignedData=0;
4d643114 917 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset, MultiPageBuffer->Length);
3e7ee490
HJ
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
59471438 929 descSize = sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) - ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount)*sizeof(u64));
3e7ee490 930 packetLen = descSize + BufferLen;
59471438 931 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 932
59471438 933 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
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
5cd508bc 939 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
940 desc.TransactionId = RequestId;
941 desc.RangeCount = 1;
942
943 desc.Range.Length = MultiPageBuffer->Length;
944 desc.Range.Offset = MultiPageBuffer->Offset;
945
59471438 946 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray, PfnCount*sizeof(u64));
3e7ee490
HJ
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
976Name:
977 VmbusChannelRecvPacket()
978
979Description:
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 ?
984int
985VmbusChannelRecvPacket(
986 VMBUS_CHANNEL *Channel,
8282c400 987 void * Buffer,
4d643114
GKH
988 u32 BufferLen,
989 u32* BufferActualLen,
59471438 990 u64* RequestId
3e7ee490
HJ
991 )
992{
993 VMPACKET_DESCRIPTOR desc;
4d643114
GKH
994 u32 packetLen;
995 u32 userLen;
3e7ee490
HJ
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
1054Name:
1055 VmbusChannelRecvPacketRaw()
1056
1057Description:
1058 Retrieve the raw packet on the specified channel
1059
1060--*/
1061int
1062VmbusChannelRecvPacketRaw(
1063 VMBUS_CHANNEL *Channel,
8282c400 1064 void * Buffer,
4d643114
GKH
1065 u32 BufferLen,
1066 u32* BufferActualLen,
59471438 1067 u64* RequestId
3e7ee490
HJ
1068 )
1069{
1070 VMPACKET_DESCRIPTOR desc;
4d643114
GKH
1071 u32 packetLen;
1072 u32 userLen;
3e7ee490
HJ
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
1130Name:
1131 VmbusChannelOnChannelEvent()
1132
1133Description:
1134 Channel event callback
1135
1136--*/
1137void
1138VmbusChannelOnChannelEvent(
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
1155Name:
1156 VmbusChannelOnTimer()
1157
1158Description:
1159 Timer event callback
1160
1161--*/
1162void
1163VmbusChannelOnTimer(
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
1181Name:
1182 DumpVmbusChannel()
1183
1184Description:
1185 Dump vmbus channel info to the console
1186
1187--*/
1188static void
1189DumpVmbusChannel(
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