Staging: hv: Use %ld instead of %d for a long ints
[deliverable/linux.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;
dd0813b6 217 unsigned long flags;
3e7ee490
HJ
218
219 DPRINT_ENTER(VMBUS);
220
221 // Aligned to page size
222 ASSERT(!(SendRingBufferSize & (PAGE_SIZE -1)));
223 ASSERT(!(RecvRingBufferSize & (PAGE_SIZE -1)));
224
225 NewChannel->OnChannelCallback = pfnOnChannelCallback;
226 NewChannel->ChannelCallbackContext = Context;
227
228 // Allocate the ring buffer
229 out = PageAlloc((SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT);
e276a3a5 230 //out = kzalloc(sendRingBufferSize + recvRingBufferSize, GFP_KERNEL);
3e7ee490 231 ASSERT(out);
c4b0bc94 232 ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0);
3e7ee490 233
c4b0bc94 234 in = (void*)((unsigned long)out + SendRingBufferSize);
3e7ee490
HJ
235
236 NewChannel->RingBufferPages = out;
237 NewChannel->RingBufferPageCount = (SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT;
238
239 RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
240
241 RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
242
243 // Establish the gpadl for the ring buffer
244 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...", NewChannel);
245
246 NewChannel->RingBufferGpadlHandle = 0;
247
248 ret = VmbusChannelEstablishGpadl(NewChannel,
249 NewChannel->Outbound.RingBuffer,
250 SendRingBufferSize + RecvRingBufferSize,
251 &NewChannel->RingBufferGpadlHandle);
252
253 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p size %d recv ring %p size %d, downstreamoffset %d>",
254 NewChannel,
255 NewChannel->OfferMsg.ChildRelId,
256 NewChannel->RingBufferGpadlHandle,
257 NewChannel->Outbound.RingBuffer,
258 NewChannel->Outbound.RingSize,
259 NewChannel->Inbound.RingBuffer,
260 NewChannel->Inbound.RingSize,
261 SendRingBufferSize);
262
263 // Create and init the channel open message
e40d37cc 264 openInfo = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_OPEN_CHANNEL), GFP_KERNEL);
3e7ee490
HJ
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
dd0813b6 284 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 285 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &openInfo->MsgListEntry);
dd0813b6 286 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
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:
dd0813b6 310 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 311 REMOVE_ENTRY_LIST(&openInfo->MsgListEntry);
dd0813b6 312 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
313
314 WaitEventClose(openInfo->WaitEvent);
8c69f52a 315 kfree(openInfo);
3e7ee490
HJ
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);
e276a3a5 425 msgHeader = kzalloc(msgSize, GFP_KERNEL);
3e7ee490
HJ
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);
e276a3a5 462 msgBody = kzalloc(msgSize, GFP_KERNEL);
3e7ee490
HJ
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);
e276a3a5 485 msgHeader = kzalloc(msgSize, GFP_KERNEL);
3e7ee490
HJ
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;
dd0813b6 535 unsigned long flags;
3e7ee490
HJ
536
537 DPRINT_ENTER(VMBUS);
538
539 nextGpadlHandle = gVmbusConnection.NextGpadlHandle;
540 InterlockedIncrement((int*)&gVmbusConnection.NextGpadlHandle);
541
542 VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
543 ASSERT(msgInfo != NULL);
544 ASSERT(msgCount >0);
545
546 msgInfo->WaitEvent = WaitEventCreate();
547 gpadlMsg = (VMBUS_CHANNEL_GPADL_HEADER*)msgInfo->Msg;
548 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
549 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
550 gpadlMsg->Gpadl = nextGpadlHandle;
551
552 DumpGpadlHeader(gpadlMsg);
553
dd0813b6 554 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 555 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
dd0813b6 556 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
557
558 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d", Kbuffer, Size, msgCount);
559
226408a4 560 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %ld", msgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
3e7ee490
HJ
561
562 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
563 if (ret != 0)
564 {
565 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
566 goto Cleanup;
567 }
568
569 if (msgCount>1)
570 {
571 ITERATE_LIST_ENTRIES(anchor, curr, &msgInfo->SubMsgList)
572 {
573 subMsgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
574 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)subMsgInfo->Msg;
575
576 gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
577 gpadlBody->Gpadl = nextGpadlHandle;
578
226408a4 579 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %ld", subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
3e7ee490
HJ
580
581 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
582 ret = VmbusPostMessage(gpadlBody, subMsgInfo->MessageSize - sizeof(VMBUS_CHANNEL_MSGINFO));
583 ASSERT(ret == 0);
584 }
585 }
586 WaitEventWait(msgInfo->WaitEvent);
587
588 // At this point, we received the gpadl created msg
589 DPRINT_DBG(VMBUS, "Received GPADL created (relid %d, status %d handle %x)",
590 Channel->OfferMsg.ChildRelId,
591 msgInfo->Response.GpadlCreated.CreationStatus,
592 gpadlMsg->Gpadl);
593
594 *GpadlHandle = gpadlMsg->Gpadl;
595
596Cleanup:
dd0813b6 597 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 598 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
dd0813b6 599 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
600
601 WaitEventClose(msgInfo->WaitEvent);
8c69f52a 602 kfree(msgInfo);
3e7ee490
HJ
603
604 DPRINT_EXIT(VMBUS);
605
606 return ret;
607}
608
609
610
611/*++;
612
613Name:
614 VmbusChannelTeardownGpadl()
615
616Description:
617 Teardown the specified GPADL handle
618
619--*/
620int
621VmbusChannelTeardownGpadl(
622 VMBUS_CHANNEL *Channel,
4d643114 623 u32 GpadlHandle
3e7ee490
HJ
624 )
625{
626 int ret=0;
627 VMBUS_CHANNEL_GPADL_TEARDOWN *msg;
628 VMBUS_CHANNEL_MSGINFO* info;
dd0813b6 629 unsigned long flags;
3e7ee490
HJ
630
631 DPRINT_ENTER(VMBUS);
632
633 ASSERT(GpadlHandle != 0);
634
e40d37cc 635 info = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN), GFP_KERNEL);
3e7ee490
HJ
636 ASSERT(info != NULL);
637
638 info->WaitEvent = WaitEventCreate();
639
640 msg = (VMBUS_CHANNEL_GPADL_TEARDOWN*)info->Msg;
641
642 msg->Header.MessageType = ChannelMessageGpadlTeardown;
643 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
644 msg->Gpadl = GpadlHandle;
645
dd0813b6 646 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 647 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &info->MsgListEntry);
dd0813b6 648 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
649
650 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN));
651 if (ret != 0)
652 {
653 // TODO:
654 }
655
656 WaitEventWait(info->WaitEvent);
657
658 // Received a torndown response
dd0813b6 659 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490 660 REMOVE_ENTRY_LIST(&info->MsgListEntry);
dd0813b6 661 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
3e7ee490
HJ
662
663 WaitEventClose(info->WaitEvent);
8c69f52a 664 kfree(info);
3e7ee490
HJ
665
666 DPRINT_EXIT(VMBUS);
667
668 return ret;
669}
670
671
672/*++
673
674Name:
675 VmbusChannelClose()
676
677Description:
678 Close the specified channel
679
680--*/
e20f683b 681void
3e7ee490
HJ
682VmbusChannelClose(
683 VMBUS_CHANNEL *Channel
684 )
685{
686 int ret=0;
687 VMBUS_CHANNEL_CLOSE_CHANNEL* msg;
688 VMBUS_CHANNEL_MSGINFO* info;
0f5e44ca 689 unsigned long flags;
3e7ee490
HJ
690
691 DPRINT_ENTER(VMBUS);
692
693 // Stop callback and cancel the timer asap
694 Channel->OnChannelCallback = NULL;
695 TimerStop(Channel->PollTimer);
696
697 // Send a closing message
e40d37cc 698 info = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL), GFP_KERNEL);
3e7ee490
HJ
699 ASSERT(info != NULL);
700
701 //info->waitEvent = WaitEventCreate();
702
703 msg = (VMBUS_CHANNEL_CLOSE_CHANNEL*)info->Msg;
704 msg->Header.MessageType = ChannelMessageCloseChannel;
705 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
706
707 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL));
708 if (ret != 0)
709 {
710 // TODO:
711 }
712
713 // Tear down the gpadl for the channel's ring buffer
714 if (Channel->RingBufferGpadlHandle)
715 {
716 VmbusChannelTeardownGpadl(Channel, Channel->RingBufferGpadlHandle);
717 }
718
719 // TODO: Send a msg to release the childRelId
720
721 // Cleanup the ring buffers for this channel
722 RingBufferCleanup(&Channel->Outbound);
723 RingBufferCleanup(&Channel->Inbound);
724
725 PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
726
8c69f52a 727 kfree(info);
3e7ee490
HJ
728
729 // If we are closing the channel during an error path in opening the channel, don't free the channel
730 // since the caller will free the channel
731 if (Channel->State == CHANNEL_OPEN_STATE)
732 {
0f5e44ca 733 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
3e7ee490 734 REMOVE_ENTRY_LIST(&Channel->ListEntry);
0f5e44ca 735 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
3e7ee490
HJ
736
737 FreeVmbusChannel(Channel);
738 }
739
740 DPRINT_EXIT(VMBUS);
741}
742
743
744/*++
745
746Name:
747 VmbusChannelSendPacket()
748
749Description:
750 Send the specified buffer on the given channel
751
752--*/
753int
754VmbusChannelSendPacket(
755 VMBUS_CHANNEL *Channel,
8282c400 756 const void * Buffer,
4d643114 757 u32 BufferLen,
59471438 758 u64 RequestId,
3e7ee490 759 VMBUS_PACKET_TYPE Type,
4d643114 760 u32 Flags
3e7ee490
HJ
761)
762{
763 int ret=0;
764 VMPACKET_DESCRIPTOR desc;
4d643114 765 u32 packetLen = sizeof(VMPACKET_DESCRIPTOR) + BufferLen;
59471438 766 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 767 SG_BUFFER_LIST bufferList[3];
59471438 768 u64 alignedData=0;
3e7ee490
HJ
769
770 DPRINT_ENTER(VMBUS);
771 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d", Channel, Buffer, BufferLen);
772
773 DumpVmbusChannel(Channel);
774
59471438 775 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
776
777 // Setup the descriptor
778 desc.Type = Type;//VmbusPacketTypeDataInBand;
779 desc.Flags = Flags;//VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
780 desc.DataOffset8 = sizeof(VMPACKET_DESCRIPTOR) >> 3; // in 8-bytes granularity
5cd508bc 781 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
782 desc.TransactionId = RequestId;
783
784 bufferList[0].Data = &desc;
785 bufferList[0].Length = sizeof(VMPACKET_DESCRIPTOR);
786
787 bufferList[1].Data = Buffer;
788 bufferList[1].Length = BufferLen;
789
790 bufferList[2].Data = &alignedData;
791 bufferList[2].Length = packetLenAligned - packetLen;
792
793 ret = RingBufferWrite(
794 &Channel->Outbound,
795 bufferList,
796 3);
797
798 // TODO: We should determine if this is optional
799 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
800 {
801 VmbusChannelSetEvent(Channel);
802 }
803
804 DPRINT_EXIT(VMBUS);
805
806 return ret;
807}
808
809
810/*++
811
812Name:
813 VmbusChannelSendPacketPageBuffer()
814
815Description:
816 Send a range of single-page buffer packets using a GPADL Direct packet type.
817
818--*/
819int
820VmbusChannelSendPacketPageBuffer(
821 VMBUS_CHANNEL *Channel,
822 PAGE_BUFFER PageBuffers[],
4d643114 823 u32 PageCount,
8282c400 824 void * Buffer,
4d643114 825 u32 BufferLen,
59471438 826 u64 RequestId
3e7ee490
HJ
827)
828{
829 int ret=0;
830 int i=0;
831 VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
4d643114
GKH
832 u32 descSize;
833 u32 packetLen;
834 u32 packetLenAligned;
3e7ee490 835 SG_BUFFER_LIST bufferList[3];
59471438 836 u64 alignedData=0;
3e7ee490
HJ
837
838 DPRINT_ENTER(VMBUS);
839
840 ASSERT(PageCount <= MAX_PAGE_BUFFER_COUNT);
841
842 DumpVmbusChannel(Channel);
843
844 // Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support
845 descSize = sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER) - ((MAX_PAGE_BUFFER_COUNT - PageCount)*sizeof(PAGE_BUFFER));
846 packetLen = descSize + BufferLen;
59471438 847 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 848
59471438 849 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
850
851 // Setup the descriptor
852 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
853 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
854 desc.DataOffset8 = descSize >> 3; // in 8-bytes grandularity
5cd508bc 855 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
856 desc.TransactionId = RequestId;
857 desc.RangeCount = PageCount;
858
859 for (i=0; i<PageCount; i++)
860 {
861 desc.Range[i].Length = PageBuffers[i].Length;
862 desc.Range[i].Offset = PageBuffers[i].Offset;
863 desc.Range[i].Pfn = PageBuffers[i].Pfn;
864 }
865
866 bufferList[0].Data = &desc;
867 bufferList[0].Length = descSize;
868
869 bufferList[1].Data = Buffer;
870 bufferList[1].Length = BufferLen;
871
872 bufferList[2].Data = &alignedData;
873 bufferList[2].Length = packetLenAligned - packetLen;
874
875 ret = RingBufferWrite(
876 &Channel->Outbound,
877 bufferList,
878 3);
879
880 // TODO: We should determine if this is optional
881 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
882 {
883 VmbusChannelSetEvent(Channel);
884 }
885
886 DPRINT_EXIT(VMBUS);
887
888 return ret;
889}
890
891
892
893/*++
894
895Name:
896 VmbusChannelSendPacketMultiPageBuffer()
897
898Description:
899 Send a multi-page buffer packet using a GPADL Direct packet type.
900
901--*/
902int
903VmbusChannelSendPacketMultiPageBuffer(
904 VMBUS_CHANNEL *Channel,
905 MULTIPAGE_BUFFER *MultiPageBuffer,
8282c400 906 void * Buffer,
4d643114 907 u32 BufferLen,
59471438 908 u64 RequestId
3e7ee490
HJ
909)
910{
911 int ret=0;
912 VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
4d643114
GKH
913 u32 descSize;
914 u32 packetLen;
915 u32 packetLenAligned;
3e7ee490 916 SG_BUFFER_LIST bufferList[3];
59471438 917 u64 alignedData=0;
4d643114 918 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset, MultiPageBuffer->Length);
3e7ee490
HJ
919
920 DPRINT_ENTER(VMBUS);
921
922 DumpVmbusChannel(Channel);
923
924 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
925
926 ASSERT(PfnCount > 0);
927 ASSERT(PfnCount <= MAX_MULTIPAGE_BUFFER_COUNT);
928
929 // Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support
59471438 930 descSize = sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) - ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount)*sizeof(u64));
3e7ee490 931 packetLen = descSize + BufferLen;
59471438 932 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
3e7ee490 933
59471438 934 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
3e7ee490
HJ
935
936 // Setup the descriptor
937 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
938 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
939 desc.DataOffset8 = descSize >> 3; // in 8-bytes grandularity
5cd508bc 940 desc.Length8 = (u16)(packetLenAligned >> 3);
3e7ee490
HJ
941 desc.TransactionId = RequestId;
942 desc.RangeCount = 1;
943
944 desc.Range.Length = MultiPageBuffer->Length;
945 desc.Range.Offset = MultiPageBuffer->Offset;
946
59471438 947 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray, PfnCount*sizeof(u64));
3e7ee490
HJ
948
949 bufferList[0].Data = &desc;
950 bufferList[0].Length = descSize;
951
952 bufferList[1].Data = Buffer;
953 bufferList[1].Length = BufferLen;
954
955 bufferList[2].Data = &alignedData;
956 bufferList[2].Length = packetLenAligned - packetLen;
957
958 ret = RingBufferWrite(
959 &Channel->Outbound,
960 bufferList,
961 3);
962
963 // TODO: We should determine if this is optional
964 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
965 {
966 VmbusChannelSetEvent(Channel);
967 }
968
969 DPRINT_EXIT(VMBUS);
970
971 return ret;
972}
973
974
975/*++
976
977Name:
978 VmbusChannelRecvPacket()
979
980Description:
981 Retrieve the user packet on the specified channel
982
983--*/
984// TODO: Do we ever receive a gpa direct packet other than the ones we send ?
985int
986VmbusChannelRecvPacket(
987 VMBUS_CHANNEL *Channel,
8282c400 988 void * Buffer,
4d643114
GKH
989 u32 BufferLen,
990 u32* BufferActualLen,
59471438 991 u64* RequestId
3e7ee490
HJ
992 )
993{
994 VMPACKET_DESCRIPTOR desc;
4d643114
GKH
995 u32 packetLen;
996 u32 userLen;
3e7ee490 997 int ret;
54411c42 998 unsigned long flags;
3e7ee490
HJ
999
1000 DPRINT_ENTER(VMBUS);
1001
1002 *BufferActualLen = 0;
1003 *RequestId = 0;
1004
54411c42 1005 spin_lock_irqsave(&Channel->inbound_lock, flags);
3e7ee490
HJ
1006
1007 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1008 if (ret != 0)
1009 {
54411c42 1010 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
3e7ee490
HJ
1011
1012 //DPRINT_DBG(VMBUS, "nothing to read!!");
1013 DPRINT_EXIT(VMBUS);
1014 return 0;
1015 }
1016
1017 //VmbusChannelClearEvent(Channel);
1018
1019 packetLen = desc.Length8 << 3;
1020 userLen = packetLen - (desc.DataOffset8 << 3);
1021 //ASSERT(userLen > 0);
1022
1023 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1024 Channel,
1025 Channel->OfferMsg.ChildRelId,
1026 desc.Type,
1027 desc.Flags,
1028 desc.TransactionId, packetLen, userLen);
1029
1030 *BufferActualLen = userLen;
1031
1032 if (userLen > BufferLen)
1033 {
54411c42 1034 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
3e7ee490
HJ
1035
1036 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d", BufferLen, userLen);
1037 DPRINT_EXIT(VMBUS);
1038
1039 return -1;
1040 }
1041
1042 *RequestId = desc.TransactionId;
1043
1044 // Copy over the packet to the user buffer
1045 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen, (desc.DataOffset8 << 3));
1046
54411c42 1047 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
3e7ee490
HJ
1048
1049 DPRINT_EXIT(VMBUS);
1050
1051 return 0;
1052}
1053
1054/*++
1055
1056Name:
1057 VmbusChannelRecvPacketRaw()
1058
1059Description:
1060 Retrieve the raw packet on the specified channel
1061
1062--*/
1063int
1064VmbusChannelRecvPacketRaw(
1065 VMBUS_CHANNEL *Channel,
8282c400 1066 void * Buffer,
4d643114
GKH
1067 u32 BufferLen,
1068 u32* BufferActualLen,
59471438 1069 u64* RequestId
3e7ee490
HJ
1070 )
1071{
1072 VMPACKET_DESCRIPTOR desc;
4d643114
GKH
1073 u32 packetLen;
1074 u32 userLen;
3e7ee490 1075 int ret;
54411c42 1076 unsigned long flags;
3e7ee490
HJ
1077
1078 DPRINT_ENTER(VMBUS);
1079
1080 *BufferActualLen = 0;
1081 *RequestId = 0;
1082
54411c42 1083 spin_lock_irqsave(&Channel->inbound_lock, flags);
3e7ee490
HJ
1084
1085 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1086 if (ret != 0)
1087 {
54411c42 1088 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
3e7ee490
HJ
1089
1090 //DPRINT_DBG(VMBUS, "nothing to read!!");
1091 DPRINT_EXIT(VMBUS);
1092 return 0;
1093 }
1094
1095 //VmbusChannelClearEvent(Channel);
1096
1097 packetLen = desc.Length8 << 3;
1098 userLen = packetLen - (desc.DataOffset8 << 3);
1099
1100 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1101 Channel,
1102 Channel->OfferMsg.ChildRelId,
1103 desc.Type,
1104 desc.Flags,
1105 desc.TransactionId, packetLen, userLen);
1106
1107 *BufferActualLen = packetLen;
1108
1109 if (packetLen > BufferLen)
1110 {
54411c42 1111 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
3e7ee490
HJ
1112
1113 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen, BufferLen);
1114 DPRINT_EXIT(VMBUS);
1115 return -2;
1116 }
1117
1118 *RequestId = desc.TransactionId;
1119
1120 // Copy over the entire packet to the user buffer
1121 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1122
54411c42 1123 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
3e7ee490
HJ
1124
1125 DPRINT_EXIT(VMBUS);
1126
1127 return 0;
1128}
1129
1130
1131/*++
1132
1133Name:
1134 VmbusChannelOnChannelEvent()
1135
1136Description:
1137 Channel event callback
1138
1139--*/
1140void
1141VmbusChannelOnChannelEvent(
1142 VMBUS_CHANNEL *Channel
1143 )
1144{
1145 DumpVmbusChannel(Channel);
1146 ASSERT(Channel->OnChannelCallback);
1147#ifdef ENABLE_POLLING
1148 TimerStop(Channel->PollTimer);
1149 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1150 TimerStart(Channel->PollTimer, 100 /* 100us */);
1151#else
1152 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1153#endif
1154}
1155
1156/*++
1157
1158Name:
1159 VmbusChannelOnTimer()
1160
1161Description:
1162 Timer event callback
1163
1164--*/
1165void
1166VmbusChannelOnTimer(
1167 void *Context
1168 )
1169{
1170 VMBUS_CHANNEL *channel = (VMBUS_CHANNEL*)Context;
1171
1172 if (channel->OnChannelCallback)
1173 {
1174 channel->OnChannelCallback(channel->ChannelCallbackContext);
1175#ifdef ENABLE_POLLING
1176 TimerStart(channel->PollTimer, 100 /* 100us */);
1177#endif
1178 }
1179}
1180
1181
1182/*++
1183
1184Name:
1185 DumpVmbusChannel()
1186
1187Description:
1188 Dump vmbus channel info to the console
1189
1190--*/
1191static void
1192DumpVmbusChannel(
1193 VMBUS_CHANNEL *Channel
1194 )
1195{
1196 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1197 DumpRingInfo(&Channel->Outbound, "Outbound ");
1198 DumpRingInfo(&Channel->Inbound, "Inbound ");
1199}
1200
1201
1202// eof
This page took 0.079908 seconds and 5 git commands to generate.