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