Staging: hv: osd: remove MemAllocAtomic wrapper
[GitHub/mt8127/android_kernel_alcatel_ttab.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);
e276a3a5 229 //out = kzalloc(sendRingBufferSize + recvRingBufferSize, GFP_KERNEL);
3e7ee490 230 ASSERT(out);
c4b0bc94 231 ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0);
3e7ee490 232
c4b0bc94 233 in = (void*)((unsigned long)out + SendRingBufferSize);
3e7ee490
HJ
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
e40d37cc 263 openInfo = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_OPEN_CHANNEL), GFP_KERNEL);
3e7ee490
HJ
264 ASSERT(openInfo != NULL);
265
266 openInfo->WaitEvent = WaitEventCreate();
267
268 openMsg = (VMBUS_CHANNEL_OPEN_CHANNEL*)openInfo->Msg;
269 openMsg->Header.MessageType = ChannelMessageOpenChannel;
270 openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; // FIXME
271 openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
272 openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
273 ASSERT(openMsg->RingBufferGpadlHandle);
274 openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >> PAGE_SHIFT;
275 openMsg->ServerContextAreaGpadlHandle = 0; // TODO
276
277 ASSERT(UserDataLen <= MAX_USER_DEFINED_BYTES);
278 if (UserDataLen)
279 {
280 memcpy(openMsg->UserData, UserData, UserDataLen);
281 }
282
283 SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
284 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &openInfo->MsgListEntry);
285 SpinlockRelease(gVmbusConnection.ChannelMsgLock);
286
287 DPRINT_DBG(VMBUS, "Sending channel open msg...");
288
289 ret = VmbusPostMessage(openMsg, sizeof(VMBUS_CHANNEL_OPEN_CHANNEL));
290 if (ret != 0)
291 {
292 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
293 goto Cleanup;
294 }
295
296 // FIXME: Need to time-out here
297 WaitEventWait(openInfo->WaitEvent);
298
299 if (openInfo->Response.OpenResult.Status == 0)
300 {
301 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
302 }
303 else
304 {
305 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!", NewChannel, openInfo->Response.OpenResult.Status);
306 }
307
308Cleanup:
309 SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
310 REMOVE_ENTRY_LIST(&openInfo->MsgListEntry);
311 SpinlockRelease(gVmbusConnection.ChannelMsgLock);
312
313 WaitEventClose(openInfo->WaitEvent);
314 MemFree(openInfo);
315
316 DPRINT_EXIT(VMBUS);
317
318 return 0;
319}
320
321/*++;
322
323Name:
324 DumpGpadlBody()
325
326Description:
327 Dump the gpadl body message to the console for debugging purposes.
328
329--*/
330static void DumpGpadlBody(
331 VMBUS_CHANNEL_GPADL_BODY *Gpadl,
4d643114 332 u32 Len)
3e7ee490
HJ
333{
334 int i=0;
335 int pfnCount=0;
336
59471438 337 pfnCount = (Len - sizeof(VMBUS_CHANNEL_GPADL_BODY))/ sizeof(u64);
3e7ee490
HJ
338 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
339
340 for (i=0; i< pfnCount; i++)
341 {
342 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu", i, Gpadl->Pfn[i]);
343 }
344}
345
346
347/*++;
348
349Name:
350 DumpGpadlHeader()
351
352Description:
353 Dump the gpadl header message to the console for debugging purposes.
354
355--*/
356static void DumpGpadlHeader(
357 VMBUS_CHANNEL_GPADL_HEADER *Gpadl
358 )
359{
360 int i=0,j=0;
361 int pageCount=0;
362
363
364 DPRINT_DBG(VMBUS, "gpadl header - relid %d, range count %d, range buflen %d",
365 Gpadl->ChildRelId,
366 Gpadl->RangeCount,
367 Gpadl->RangeBufLen);
368 for (i=0; i< Gpadl->RangeCount; i++)
369 {
370 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
371 pageCount = (pageCount > 26)? 26 : pageCount;
372
373 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d page count %d",
374 i, Gpadl->Range[i].ByteCount, Gpadl->Range[i].ByteOffset, pageCount);
375
376 for (j=0; j< pageCount; j++)
377 {
378 DPRINT_DBG(VMBUS, "%d) pfn %llu", j, Gpadl->Range[i].PfnArray[j]);
379 }
380 }
381}
382
383/*++;
384
385Name:
386 VmbusChannelCreateGpadlHeader()
387
388Description:
389 Creates a gpadl for the specified buffer
390
391--*/
392static int
393VmbusChannelCreateGpadlHeader(
8282c400 394 void * Kbuffer, // from kmalloc()
4d643114 395 u32 Size, // page-size multiple
3e7ee490 396 VMBUS_CHANNEL_MSGINFO **MsgInfo,
4d643114 397 u32 *MessageCount)
3e7ee490
HJ
398{
399 int i;
400 int pageCount;
401 unsigned long long pfn;
402 VMBUS_CHANNEL_GPADL_HEADER* gpaHeader;
403 VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
404 VMBUS_CHANNEL_MSGINFO* msgHeader;
405 VMBUS_CHANNEL_MSGINFO* msgBody;
4d643114 406 u32 msgSize;
3e7ee490
HJ
407
408 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
409
410 //ASSERT( (kbuffer & (PAGE_SIZE-1)) == 0);
411 ASSERT( (Size & (PAGE_SIZE-1)) == 0);
412
413 pageCount = Size >> PAGE_SHIFT;
414 pfn = GetPhysicalAddress(Kbuffer) >> PAGE_SHIFT;
415
416 // do we need a gpadl body msg
417 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_HEADER) - sizeof(GPA_RANGE);
59471438 418 pfnCount = pfnSize / sizeof(u64);
3e7ee490
HJ
419
420 if (pageCount > pfnCount) // we need a gpadl body
421 {
422 // fill in the header
59471438 423 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pfnCount*sizeof(u64);
e276a3a5 424 msgHeader = kzalloc(msgSize, GFP_KERNEL);
3e7ee490
HJ
425
426 INITIALIZE_LIST_HEAD(&msgHeader->SubMsgList);
427 msgHeader->MessageSize=msgSize;
428
429 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
430 gpaHeader->RangeCount = 1;
59471438 431 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
3e7ee490
HJ
432 gpaHeader->Range[0].ByteOffset = 0;
433 gpaHeader->Range[0].ByteCount = Size;
434 for (i=0; i<pfnCount; i++)
435 {
436 gpaHeader->Range[0].PfnArray[i] = pfn+i;
437 }
438 *MsgInfo = msgHeader;
439 *MessageCount = 1;
440
441 pfnSum = pfnCount;
442 pfnLeft = pageCount - pfnCount;
443
444 // how many pfns can we fit
445 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_BODY);
59471438 446 pfnCount = pfnSize / sizeof(u64);
3e7ee490
HJ
447
448 // fill in the body
449 while (pfnLeft)
450 {
451 if (pfnLeft > pfnCount)
452 {
453 pfnCurr = pfnCount;
454 }
455 else
456 {
457 pfnCurr = pfnLeft;
458 }
459
59471438 460 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_BODY) + pfnCurr*sizeof(u64);
e276a3a5 461 msgBody = kzalloc(msgSize, GFP_KERNEL);
3e7ee490
HJ
462 ASSERT(msgBody);
463 msgBody->MessageSize = msgSize;
464 (*MessageCount)++;
465 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)msgBody->Msg;
466
4d643114 467 // FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit
3e7ee490
HJ
468 //gpadlBody->Gpadl = kbuffer;
469 for (i=0; i<pfnCurr; i++)
470 {
471 gpadlBody->Pfn[i] = pfn + pfnSum + i;
472 }
473
474 // add to msg header
475 INSERT_TAIL_LIST(&msgHeader->SubMsgList, &msgBody->MsgListEntry);
476 pfnSum += pfnCurr;
477 pfnLeft -= pfnCurr;
478 }
479 }
480 else
481 {
482 // everything fits in a header
59471438 483 msgSize = sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pageCount*sizeof(u64);
e276a3a5 484 msgHeader = kzalloc(msgSize, GFP_KERNEL);
3e7ee490
HJ
485 msgHeader->MessageSize=msgSize;
486
487 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
488 gpaHeader->RangeCount = 1;
59471438 489 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
3e7ee490
HJ
490 gpaHeader->Range[0].ByteOffset = 0;
491 gpaHeader->Range[0].ByteCount = Size;
492 for (i=0; i<pageCount; i++)
493 {
494 gpaHeader->Range[0].PfnArray[i] = pfn+i;
495 }
496
497 *MsgInfo = msgHeader;
498 *MessageCount = 1;
499 }
500
501 return 0;
502}
503
504
505/*++;
506
507Name:
508 VmbusChannelEstablishGpadl()
509
510Description:
511 Estabish a GPADL for the specified buffer
512
513--*/
514int
515VmbusChannelEstablishGpadl(
516 VMBUS_CHANNEL *Channel,
8282c400 517 void * Kbuffer, // from kmalloc()
4d643114
GKH
518 u32 Size, // page-size multiple
519 u32 *GpadlHandle
3e7ee490
HJ
520 )
521{
522 int ret=0;
523 VMBUS_CHANNEL_GPADL_HEADER* gpadlMsg;
524 VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
525 //VMBUS_CHANNEL_GPADL_CREATED* gpadlCreated;
526
527 VMBUS_CHANNEL_MSGINFO *msgInfo;
528 VMBUS_CHANNEL_MSGINFO *subMsgInfo;
529
4d643114 530 u32 msgCount;
3e7ee490
HJ
531 LIST_ENTRY* anchor;
532 LIST_ENTRY* curr;
4d643114 533 u32 nextGpadlHandle;
3e7ee490
HJ
534
535 DPRINT_ENTER(VMBUS);
536
537 nextGpadlHandle = gVmbusConnection.NextGpadlHandle;
538 InterlockedIncrement((int*)&gVmbusConnection.NextGpadlHandle);
539
540 VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
541 ASSERT(msgInfo != NULL);
542 ASSERT(msgCount >0);
543
544 msgInfo->WaitEvent = WaitEventCreate();
545 gpadlMsg = (VMBUS_CHANNEL_GPADL_HEADER*)msgInfo->Msg;
546 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
547 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
548 gpadlMsg->Gpadl = nextGpadlHandle;
549
550 DumpGpadlHeader(gpadlMsg);
551
552 SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
553 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
554 SpinlockRelease(gVmbusConnection.ChannelMsgLock);
555
556 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d", Kbuffer, Size, msgCount);
557
558 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %d", msgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
559
560 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
561 if (ret != 0)
562 {
563 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
564 goto Cleanup;
565 }
566
567 if (msgCount>1)
568 {
569 ITERATE_LIST_ENTRIES(anchor, curr, &msgInfo->SubMsgList)
570 {
571 subMsgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
572 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)subMsgInfo->Msg;
573
574 gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
575 gpadlBody->Gpadl = nextGpadlHandle;
576
577 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %d", subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
578
579 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
580 ret = VmbusPostMessage(gpadlBody, subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
581 ASSERT(ret == 0);
582 }
583 }
584 WaitEventWait(msgInfo->WaitEvent);
585
586 // At this point, we received the gpadl created msg
587 DPRINT_DBG(VMBUS, "Received GPADL created (relid %d, status %d handle %x)",
588 Channel->OfferMsg.ChildRelId,
589 msgInfo->Response.GpadlCreated.CreationStatus,
590 gpadlMsg->Gpadl);
591
592 *GpadlHandle = gpadlMsg->Gpadl;
593
594Cleanup:
595 SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
596 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
597 SpinlockRelease(gVmbusConnection.ChannelMsgLock);
598
599 WaitEventClose(msgInfo->WaitEvent);
600 MemFree(msgInfo);
601
602 DPRINT_EXIT(VMBUS);
603
604 return ret;
605}
606
607
608
609/*++;
610
611Name:
612 VmbusChannelTeardownGpadl()
613
614Description:
615 Teardown the specified GPADL handle
616
617--*/
618int
619VmbusChannelTeardownGpadl(
620 VMBUS_CHANNEL *Channel,
4d643114 621 u32 GpadlHandle
3e7ee490
HJ
622 )
623{
624 int ret=0;
625 VMBUS_CHANNEL_GPADL_TEARDOWN *msg;
626 VMBUS_CHANNEL_MSGINFO* info;
627
628 DPRINT_ENTER(VMBUS);
629
630 ASSERT(GpadlHandle != 0);
631
e40d37cc 632 info = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN), GFP_KERNEL);
3e7ee490
HJ
633 ASSERT(info != NULL);
634
635 info->WaitEvent = WaitEventCreate();
636
637 msg = (VMBUS_CHANNEL_GPADL_TEARDOWN*)info->Msg;
638
639 msg->Header.MessageType = ChannelMessageGpadlTeardown;
640 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
641 msg->Gpadl = GpadlHandle;
642
643 SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
644 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &info->MsgListEntry);
645 SpinlockRelease(gVmbusConnection.ChannelMsgLock);
646
647 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN));
648 if (ret != 0)
649 {
650 // TODO:
651 }
652
653 WaitEventWait(info->WaitEvent);
654
655 // Received a torndown response
656 SpinlockAcquire(gVmbusConnection.ChannelMsgLock);
657 REMOVE_ENTRY_LIST(&info->MsgListEntry);
658 SpinlockRelease(gVmbusConnection.ChannelMsgLock);
659
660 WaitEventClose(info->WaitEvent);
661 MemFree(info);
662
663 DPRINT_EXIT(VMBUS);
664
665 return ret;
666}
667
668
669/*++
670
671Name:
672 VmbusChannelClose()
673
674Description:
675 Close the specified channel
676
677--*/
e20f683b 678void
3e7ee490
HJ
679VmbusChannelClose(
680 VMBUS_CHANNEL *Channel
681 )
682{
683 int ret=0;
684 VMBUS_CHANNEL_CLOSE_CHANNEL* msg;
685 VMBUS_CHANNEL_MSGINFO* info;
686
687 DPRINT_ENTER(VMBUS);
688
689 // Stop callback and cancel the timer asap
690 Channel->OnChannelCallback = NULL;
691 TimerStop(Channel->PollTimer);
692
693 // Send a closing message
e40d37cc 694 info = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL), GFP_KERNEL);
3e7ee490
HJ
695 ASSERT(info != NULL);
696
697 //info->waitEvent = WaitEventCreate();
698
699 msg = (VMBUS_CHANNEL_CLOSE_CHANNEL*)info->Msg;
700 msg->Header.MessageType = ChannelMessageCloseChannel;
701 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
702
703 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL));
704 if (ret != 0)
705 {
706 // TODO:
707 }
708
709 // Tear down the gpadl for the channel's ring buffer
710 if (Channel->RingBufferGpadlHandle)
711 {
712 VmbusChannelTeardownGpadl(Channel, Channel->RingBufferGpadlHandle);
713 }
714
715 // TODO: Send a msg to release the childRelId
716
717 // Cleanup the ring buffers for this channel
718 RingBufferCleanup(&Channel->Outbound);
719 RingBufferCleanup(&Channel->Inbound);
720
721 PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
722
723 MemFree(info);
724
725 // If we are closing the channel during an error path in opening the channel, don't free the channel
726 // since the caller will free the channel
727 if (Channel->State == CHANNEL_OPEN_STATE)
728 {
729 SpinlockAcquire(gVmbusConnection.ChannelLock);
730 REMOVE_ENTRY_LIST(&Channel->ListEntry);
731 SpinlockRelease(gVmbusConnection.ChannelLock);
732
733 FreeVmbusChannel(Channel);
734 }
735
736 DPRINT_EXIT(VMBUS);
737}
738
739
740/*++
741
742Name:
743 VmbusChannelSendPacket()
744
745Description:
746 Send the specified buffer on the given channel
747
748--*/
749int
750VmbusChannelSendPacket(
751 VMBUS_CHANNEL *Channel,
8282c400 752 const void * Buffer,
4d643114 753 u32 BufferLen,
59471438 754 u64 RequestId,
3e7ee490 755 VMBUS_PACKET_TYPE Type,
4d643114 756 u32 Flags
3e7ee490
HJ
757)
758{
759 int ret=0;
760 VMPACKET_DESCRIPTOR desc;
4d643114 761 u32 packetLen = sizeof(VMPACKET_DESCRIPTOR) + BufferLen;
59471438 762 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 763 SG_BUFFER_LIST bufferList[3];
59471438 764 u64 alignedData=0;
3e7ee490
HJ
765
766 DPRINT_ENTER(VMBUS);
767 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d", Channel, Buffer, BufferLen);
768
769 DumpVmbusChannel(Channel);
770
59471438 771 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
772
773 // Setup the descriptor
774 desc.Type = Type;//VmbusPacketTypeDataInBand;
775 desc.Flags = Flags;//VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
776 desc.DataOffset8 = sizeof(VMPACKET_DESCRIPTOR) >> 3; // in 8-bytes granularity
5cd508bc 777 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
778 desc.TransactionId = RequestId;
779
780 bufferList[0].Data = &desc;
781 bufferList[0].Length = sizeof(VMPACKET_DESCRIPTOR);
782
783 bufferList[1].Data = Buffer;
784 bufferList[1].Length = BufferLen;
785
786 bufferList[2].Data = &alignedData;
787 bufferList[2].Length = packetLenAligned - packetLen;
788
789 ret = RingBufferWrite(
790 &Channel->Outbound,
791 bufferList,
792 3);
793
794 // TODO: We should determine if this is optional
795 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
796 {
797 VmbusChannelSetEvent(Channel);
798 }
799
800 DPRINT_EXIT(VMBUS);
801
802 return ret;
803}
804
805
806/*++
807
808Name:
809 VmbusChannelSendPacketPageBuffer()
810
811Description:
812 Send a range of single-page buffer packets using a GPADL Direct packet type.
813
814--*/
815int
816VmbusChannelSendPacketPageBuffer(
817 VMBUS_CHANNEL *Channel,
818 PAGE_BUFFER PageBuffers[],
4d643114 819 u32 PageCount,
8282c400 820 void * Buffer,
4d643114 821 u32 BufferLen,
59471438 822 u64 RequestId
3e7ee490
HJ
823)
824{
825 int ret=0;
826 int i=0;
827 VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
4d643114
GKH
828 u32 descSize;
829 u32 packetLen;
830 u32 packetLenAligned;
3e7ee490 831 SG_BUFFER_LIST bufferList[3];
59471438 832 u64 alignedData=0;
3e7ee490
HJ
833
834 DPRINT_ENTER(VMBUS);
835
836 ASSERT(PageCount <= MAX_PAGE_BUFFER_COUNT);
837
838 DumpVmbusChannel(Channel);
839
840 // Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support
841 descSize = sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER) - ((MAX_PAGE_BUFFER_COUNT - PageCount)*sizeof(PAGE_BUFFER));
842 packetLen = descSize + BufferLen;
59471438 843 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 844
59471438 845 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
846
847 // Setup the descriptor
848 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
849 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
850 desc.DataOffset8 = descSize >> 3; // in 8-bytes grandularity
5cd508bc 851 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
852 desc.TransactionId = RequestId;
853 desc.RangeCount = PageCount;
854
855 for (i=0; i<PageCount; i++)
856 {
857 desc.Range[i].Length = PageBuffers[i].Length;
858 desc.Range[i].Offset = PageBuffers[i].Offset;
859 desc.Range[i].Pfn = PageBuffers[i].Pfn;
860 }
861
862 bufferList[0].Data = &desc;
863 bufferList[0].Length = descSize;
864
865 bufferList[1].Data = Buffer;
866 bufferList[1].Length = BufferLen;
867
868 bufferList[2].Data = &alignedData;
869 bufferList[2].Length = packetLenAligned - packetLen;
870
871 ret = RingBufferWrite(
872 &Channel->Outbound,
873 bufferList,
874 3);
875
876 // TODO: We should determine if this is optional
877 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
878 {
879 VmbusChannelSetEvent(Channel);
880 }
881
882 DPRINT_EXIT(VMBUS);
883
884 return ret;
885}
886
887
888
889/*++
890
891Name:
892 VmbusChannelSendPacketMultiPageBuffer()
893
894Description:
895 Send a multi-page buffer packet using a GPADL Direct packet type.
896
897--*/
898int
899VmbusChannelSendPacketMultiPageBuffer(
900 VMBUS_CHANNEL *Channel,
901 MULTIPAGE_BUFFER *MultiPageBuffer,
8282c400 902 void * Buffer,
4d643114 903 u32 BufferLen,
59471438 904 u64 RequestId
3e7ee490
HJ
905)
906{
907 int ret=0;
908 VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
4d643114
GKH
909 u32 descSize;
910 u32 packetLen;
911 u32 packetLenAligned;
3e7ee490 912 SG_BUFFER_LIST bufferList[3];
59471438 913 u64 alignedData=0;
4d643114 914 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset, MultiPageBuffer->Length);
3e7ee490
HJ
915
916 DPRINT_ENTER(VMBUS);
917
918 DumpVmbusChannel(Channel);
919
920 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
921
922 ASSERT(PfnCount > 0);
923 ASSERT(PfnCount <= MAX_MULTIPAGE_BUFFER_COUNT);
924
925 // Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support
59471438 926 descSize = sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) - ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount)*sizeof(u64));
3e7ee490 927 packetLen = descSize + BufferLen;
59471438 928 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 929
59471438 930 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
931
932 // Setup the descriptor
933 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
934 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
935 desc.DataOffset8 = descSize >> 3; // in 8-bytes grandularity
5cd508bc 936 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
937 desc.TransactionId = RequestId;
938 desc.RangeCount = 1;
939
940 desc.Range.Length = MultiPageBuffer->Length;
941 desc.Range.Offset = MultiPageBuffer->Offset;
942
59471438 943 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray, PfnCount*sizeof(u64));
3e7ee490
HJ
944
945 bufferList[0].Data = &desc;
946 bufferList[0].Length = descSize;
947
948 bufferList[1].Data = Buffer;
949 bufferList[1].Length = BufferLen;
950
951 bufferList[2].Data = &alignedData;
952 bufferList[2].Length = packetLenAligned - packetLen;
953
954 ret = RingBufferWrite(
955 &Channel->Outbound,
956 bufferList,
957 3);
958
959 // TODO: We should determine if this is optional
960 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
961 {
962 VmbusChannelSetEvent(Channel);
963 }
964
965 DPRINT_EXIT(VMBUS);
966
967 return ret;
968}
969
970
971/*++
972
973Name:
974 VmbusChannelRecvPacket()
975
976Description:
977 Retrieve the user packet on the specified channel
978
979--*/
980// TODO: Do we ever receive a gpa direct packet other than the ones we send ?
981int
982VmbusChannelRecvPacket(
983 VMBUS_CHANNEL *Channel,
8282c400 984 void * Buffer,
4d643114
GKH
985 u32 BufferLen,
986 u32* BufferActualLen,
59471438 987 u64* RequestId
3e7ee490
HJ
988 )
989{
990 VMPACKET_DESCRIPTOR desc;
4d643114
GKH
991 u32 packetLen;
992 u32 userLen;
3e7ee490
HJ
993 int ret;
994
995 DPRINT_ENTER(VMBUS);
996
997 *BufferActualLen = 0;
998 *RequestId = 0;
999
1000 SpinlockAcquire(Channel->InboundLock);
1001
1002 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1003 if (ret != 0)
1004 {
1005 SpinlockRelease(Channel->InboundLock);
1006
1007 //DPRINT_DBG(VMBUS, "nothing to read!!");
1008 DPRINT_EXIT(VMBUS);
1009 return 0;
1010 }
1011
1012 //VmbusChannelClearEvent(Channel);
1013
1014 packetLen = desc.Length8 << 3;
1015 userLen = packetLen - (desc.DataOffset8 << 3);
1016 //ASSERT(userLen > 0);
1017
1018 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1019 Channel,
1020 Channel->OfferMsg.ChildRelId,
1021 desc.Type,
1022 desc.Flags,
1023 desc.TransactionId, packetLen, userLen);
1024
1025 *BufferActualLen = userLen;
1026
1027 if (userLen > BufferLen)
1028 {
1029 SpinlockRelease(Channel->InboundLock);
1030
1031 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d", BufferLen, userLen);
1032 DPRINT_EXIT(VMBUS);
1033
1034 return -1;
1035 }
1036
1037 *RequestId = desc.TransactionId;
1038
1039 // Copy over the packet to the user buffer
1040 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen, (desc.DataOffset8 << 3));
1041
1042 SpinlockRelease(Channel->InboundLock);
1043
1044 DPRINT_EXIT(VMBUS);
1045
1046 return 0;
1047}
1048
1049/*++
1050
1051Name:
1052 VmbusChannelRecvPacketRaw()
1053
1054Description:
1055 Retrieve the raw packet on the specified channel
1056
1057--*/
1058int
1059VmbusChannelRecvPacketRaw(
1060 VMBUS_CHANNEL *Channel,
8282c400 1061 void * Buffer,
4d643114
GKH
1062 u32 BufferLen,
1063 u32* BufferActualLen,
59471438 1064 u64* RequestId
3e7ee490
HJ
1065 )
1066{
1067 VMPACKET_DESCRIPTOR desc;
4d643114
GKH
1068 u32 packetLen;
1069 u32 userLen;
3e7ee490
HJ
1070 int ret;
1071
1072 DPRINT_ENTER(VMBUS);
1073
1074 *BufferActualLen = 0;
1075 *RequestId = 0;
1076
1077 SpinlockAcquire(Channel->InboundLock);
1078
1079 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1080 if (ret != 0)
1081 {
1082 SpinlockRelease(Channel->InboundLock);
1083
1084 //DPRINT_DBG(VMBUS, "nothing to read!!");
1085 DPRINT_EXIT(VMBUS);
1086 return 0;
1087 }
1088
1089 //VmbusChannelClearEvent(Channel);
1090
1091 packetLen = desc.Length8 << 3;
1092 userLen = packetLen - (desc.DataOffset8 << 3);
1093
1094 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1095 Channel,
1096 Channel->OfferMsg.ChildRelId,
1097 desc.Type,
1098 desc.Flags,
1099 desc.TransactionId, packetLen, userLen);
1100
1101 *BufferActualLen = packetLen;
1102
1103 if (packetLen > BufferLen)
1104 {
1105 SpinlockRelease(Channel->InboundLock);
1106
1107 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen, BufferLen);
1108 DPRINT_EXIT(VMBUS);
1109 return -2;
1110 }
1111
1112 *RequestId = desc.TransactionId;
1113
1114 // Copy over the entire packet to the user buffer
1115 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1116
1117 SpinlockRelease(Channel->InboundLock);
1118
1119 DPRINT_EXIT(VMBUS);
1120
1121 return 0;
1122}
1123
1124
1125/*++
1126
1127Name:
1128 VmbusChannelOnChannelEvent()
1129
1130Description:
1131 Channel event callback
1132
1133--*/
1134void
1135VmbusChannelOnChannelEvent(
1136 VMBUS_CHANNEL *Channel
1137 )
1138{
1139 DumpVmbusChannel(Channel);
1140 ASSERT(Channel->OnChannelCallback);
1141#ifdef ENABLE_POLLING
1142 TimerStop(Channel->PollTimer);
1143 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1144 TimerStart(Channel->PollTimer, 100 /* 100us */);
1145#else
1146 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1147#endif
1148}
1149
1150/*++
1151
1152Name:
1153 VmbusChannelOnTimer()
1154
1155Description:
1156 Timer event callback
1157
1158--*/
1159void
1160VmbusChannelOnTimer(
1161 void *Context
1162 )
1163{
1164 VMBUS_CHANNEL *channel = (VMBUS_CHANNEL*)Context;
1165
1166 if (channel->OnChannelCallback)
1167 {
1168 channel->OnChannelCallback(channel->ChannelCallbackContext);
1169#ifdef ENABLE_POLLING
1170 TimerStart(channel->PollTimer, 100 /* 100us */);
1171#endif
1172 }
1173}
1174
1175
1176/*++
1177
1178Name:
1179 DumpVmbusChannel()
1180
1181Description:
1182 Dump vmbus channel info to the console
1183
1184--*/
1185static void
1186DumpVmbusChannel(
1187 VMBUS_CHANNEL *Channel
1188 )
1189{
1190 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1191 DumpRingInfo(&Channel->Outbound, "Outbound ");
1192 DumpRingInfo(&Channel->Inbound, "Inbound ");
1193}
1194
1195
1196// eof