Staging: hv: typedef removal for VmbusApi.h
[wandboard.git] / drivers / staging / hv / Channel.c
blob5bea2d6bff99de4e240350fd76562f7ebd08fd79
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include "osd.h"
27 #include "include/logging.h"
29 #include "VmbusPrivate.h"
31 /* Internal routines */
32 static int VmbusChannelCreateGpadlHeader(
33 void * Kbuffer, /* must be phys and virt contiguous */
34 u32 Size, /* page-size multiple */
35 struct vmbus_channel_msginfo **msgInfo,
36 u32 *MessageCount
38 static void DumpVmbusChannel(struct vmbus_channel *channel);
39 static void VmbusChannelSetEvent(struct vmbus_channel *channel);
42 #if 0
43 static void
44 DumpMonitorPage(
45 struct hv_monitor_page *MonitorPage
48 int i=0;
49 int j=0;
51 DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d", MonitorPage, MonitorPage->TriggerState);
53 for (i=0; i<4; i++)
55 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i, MonitorPage->TriggerGroup[i].AsUINT64);
58 for (i=0; i<4; i++)
60 for (j=0; j<32; j++)
62 DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j, MonitorPage->Latency[i][j]);
65 for (i=0; i<4; i++)
67 for (j=0; j<32; j++)
69 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].ConnectionId.Asu32);
70 DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].FlagNumber);
75 #endif
77 /*++
79 Name:
80 VmbusChannelSetEvent()
82 Description:
83 Trigger an event notification on the specified channel.
85 --*/
86 static void VmbusChannelSetEvent(struct vmbus_channel *Channel)
88 struct hv_monitor_page *monitorPage;
90 DPRINT_ENTER(VMBUS);
92 if (Channel->OfferMsg.MonitorAllocated)
94 /* Each u32 represents 32 channels */
95 set_bit(Channel->OfferMsg.ChildRelId & 31,
96 (unsigned long *) gVmbusConnection.SendInterruptPage +
97 (Channel->OfferMsg.ChildRelId >> 5) );
99 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
100 monitorPage++; /* Get the child to parent monitor page */
102 set_bit(Channel->MonitorBit,
103 (unsigned long *) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending);
106 else
108 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
111 DPRINT_EXIT(VMBUS);
114 #if 0
115 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
117 struct hv_monitor_page *monitorPage;
119 DPRINT_ENTER(VMBUS);
121 if (Channel->OfferMsg.MonitorAllocated)
123 /* Each u32 represents 32 channels */
124 clear_bit(Channel->OfferMsg.ChildRelId & 31,
125 (unsigned long *) gVmbusConnection.SendInterruptPage + (Channel->OfferMsg.ChildRelId >> 5));
127 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
128 monitorPage++; /* Get the child to parent monitor page */
130 clear_bit(Channel->MonitorBit,
131 (unsigned long *) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending);
134 DPRINT_EXIT(VMBUS);
137 #endif
138 /*++;
140 Name:
141 VmbusChannelGetDebugInfo()
143 Description:
144 Retrieve various channel debug info
146 --*/
147 void VmbusChannelGetDebugInfo(struct vmbus_channel *Channel,
148 struct vmbus_channel_debug_info *DebugInfo)
150 struct hv_monitor_page *monitorPage;
151 u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
152 u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
153 /* u32 monitorBit = 1 << monitorOffset; */
155 DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
156 DebugInfo->State = Channel->State;
157 memcpy(&DebugInfo->InterfaceType, &Channel->OfferMsg.Offer.InterfaceType, sizeof(struct hv_guid));
158 memcpy(&DebugInfo->InterfaceInstance, &Channel->OfferMsg.Offer.InterfaceInstance, sizeof(struct hv_guid));
160 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
162 DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
164 DebugInfo->ServerMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
165 DebugInfo->ServerMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
166 DebugInfo->ServerMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
168 monitorPage++;
170 DebugInfo->ClientMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
171 DebugInfo->ClientMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
172 DebugInfo->ClientMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
174 RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
175 RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
179 /*++;
181 Name:
182 VmbusChannelOpen()
184 Description:
185 Open the specified channel.
187 --*/
188 int VmbusChannelOpen(struct vmbus_channel *NewChannel,
189 u32 SendRingBufferSize,
190 u32 RecvRingBufferSize,
191 void * UserData,
192 u32 UserDataLen,
193 PFN_CHANNEL_CALLBACK pfnOnChannelCallback,
194 void * Context
197 int ret=0;
198 VMBUS_CHANNEL_OPEN_CHANNEL* openMsg;
199 struct vmbus_channel_msginfo *openInfo;
200 void *in, *out;
201 unsigned long flags;
203 DPRINT_ENTER(VMBUS);
205 /* Aligned to page size */
206 ASSERT(!(SendRingBufferSize & (PAGE_SIZE -1)));
207 ASSERT(!(RecvRingBufferSize & (PAGE_SIZE -1)));
209 NewChannel->OnChannelCallback = pfnOnChannelCallback;
210 NewChannel->ChannelCallbackContext = Context;
212 /* Allocate the ring buffer */
213 out = osd_PageAlloc((SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT);
214 /* out = kzalloc(sendRingBufferSize + recvRingBufferSize, GFP_KERNEL); */
215 ASSERT(out);
216 ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0);
218 in = (void*)((unsigned long)out + SendRingBufferSize);
220 NewChannel->RingBufferPages = out;
221 NewChannel->RingBufferPageCount = (SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT;
223 RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
225 RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
227 /* Establish the gpadl for the ring buffer */
228 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...", NewChannel);
230 NewChannel->RingBufferGpadlHandle = 0;
232 ret = VmbusChannelEstablishGpadl(NewChannel,
233 NewChannel->Outbound.RingBuffer,
234 SendRingBufferSize + RecvRingBufferSize,
235 &NewChannel->RingBufferGpadlHandle);
237 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p size %d recv ring %p size %d, downstreamoffset %d>",
238 NewChannel,
239 NewChannel->OfferMsg.ChildRelId,
240 NewChannel->RingBufferGpadlHandle,
241 NewChannel->Outbound.RingBuffer,
242 NewChannel->Outbound.RingSize,
243 NewChannel->Inbound.RingBuffer,
244 NewChannel->Inbound.RingSize,
245 SendRingBufferSize);
247 /* Create and init the channel open message */
248 openInfo = kmalloc(sizeof(*openInfo) + sizeof(VMBUS_CHANNEL_OPEN_CHANNEL), GFP_KERNEL);
249 ASSERT(openInfo != NULL);
251 openInfo->WaitEvent = osd_WaitEventCreate();
253 openMsg = (VMBUS_CHANNEL_OPEN_CHANNEL*)openInfo->Msg;
254 openMsg->Header.MessageType = ChannelMessageOpenChannel;
255 openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; /* FIXME */
256 openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
257 openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
258 ASSERT(openMsg->RingBufferGpadlHandle);
259 openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >> PAGE_SHIFT;
260 openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
262 ASSERT(UserDataLen <= MAX_USER_DEFINED_BYTES);
263 if (UserDataLen)
265 memcpy(openMsg->UserData, UserData, UserDataLen);
268 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
269 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &openInfo->MsgListEntry);
270 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
272 DPRINT_DBG(VMBUS, "Sending channel open msg...");
274 ret = VmbusPostMessage(openMsg, sizeof(VMBUS_CHANNEL_OPEN_CHANNEL));
275 if (ret != 0)
277 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
278 goto Cleanup;
281 /* FIXME: Need to time-out here */
282 osd_WaitEventWait(openInfo->WaitEvent);
284 if (openInfo->Response.OpenResult.Status == 0)
286 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
288 else
290 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!", NewChannel, openInfo->Response.OpenResult.Status);
293 Cleanup:
294 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
295 REMOVE_ENTRY_LIST(&openInfo->MsgListEntry);
296 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
298 kfree(openInfo->WaitEvent);
299 kfree(openInfo);
301 DPRINT_EXIT(VMBUS);
303 return 0;
306 /*++;
308 Name:
309 DumpGpadlBody()
311 Description:
312 Dump the gpadl body message to the console for debugging purposes.
314 --*/
315 static void DumpGpadlBody(
316 VMBUS_CHANNEL_GPADL_BODY *Gpadl,
317 u32 Len)
319 int i=0;
320 int pfnCount=0;
322 pfnCount = (Len - sizeof(VMBUS_CHANNEL_GPADL_BODY))/ sizeof(u64);
323 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
325 for (i=0; i< pfnCount; i++)
327 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu", i, Gpadl->Pfn[i]);
332 /*++;
334 Name:
335 DumpGpadlHeader()
337 Description:
338 Dump the gpadl header message to the console for debugging purposes.
340 --*/
341 static void DumpGpadlHeader(
342 VMBUS_CHANNEL_GPADL_HEADER *Gpadl
345 int i=0,j=0;
346 int pageCount=0;
349 DPRINT_DBG(VMBUS, "gpadl header - relid %d, range count %d, range buflen %d",
350 Gpadl->ChildRelId,
351 Gpadl->RangeCount,
352 Gpadl->RangeBufLen);
353 for (i=0; i< Gpadl->RangeCount; i++)
355 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
356 pageCount = (pageCount > 26)? 26 : pageCount;
358 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d page count %d",
359 i, Gpadl->Range[i].ByteCount, Gpadl->Range[i].ByteOffset, pageCount);
361 for (j=0; j< pageCount; j++)
363 DPRINT_DBG(VMBUS, "%d) pfn %llu", j, Gpadl->Range[i].PfnArray[j]);
368 /*++;
370 Name:
371 VmbusChannelCreateGpadlHeader()
373 Description:
374 Creates a gpadl for the specified buffer
376 --*/
377 static int
378 VmbusChannelCreateGpadlHeader(
379 void * Kbuffer, /* from kmalloc() */
380 u32 Size, /* page-size multiple */
381 struct vmbus_channel_msginfo **MsgInfo,
382 u32 *MessageCount)
384 int i;
385 int pageCount;
386 unsigned long long pfn;
387 VMBUS_CHANNEL_GPADL_HEADER* gpaHeader;
388 VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
389 struct vmbus_channel_msginfo *msgHeader;
390 struct vmbus_channel_msginfo *msgBody;
391 u32 msgSize;
393 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
395 /* ASSERT( (kbuffer & (PAGE_SIZE-1)) == 0); */
396 ASSERT( (Size & (PAGE_SIZE-1)) == 0);
398 pageCount = Size >> PAGE_SHIFT;
399 pfn = virt_to_phys(Kbuffer) >> PAGE_SHIFT;
401 /* do we need a gpadl body msg */
402 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_HEADER) - sizeof(GPA_RANGE);
403 pfnCount = pfnSize / sizeof(u64);
405 if (pageCount > pfnCount) /* we need a gpadl body */
407 /* fill in the header */
408 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pfnCount*sizeof(u64);
409 msgHeader = kzalloc(msgSize, GFP_KERNEL);
411 INITIALIZE_LIST_HEAD(&msgHeader->SubMsgList);
412 msgHeader->MessageSize=msgSize;
414 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
415 gpaHeader->RangeCount = 1;
416 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
417 gpaHeader->Range[0].ByteOffset = 0;
418 gpaHeader->Range[0].ByteCount = Size;
419 for (i=0; i<pfnCount; i++)
421 gpaHeader->Range[0].PfnArray[i] = pfn+i;
423 *MsgInfo = msgHeader;
424 *MessageCount = 1;
426 pfnSum = pfnCount;
427 pfnLeft = pageCount - pfnCount;
429 /* how many pfns can we fit */
430 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_BODY);
431 pfnCount = pfnSize / sizeof(u64);
433 /* fill in the body */
434 while (pfnLeft)
436 if (pfnLeft > pfnCount)
438 pfnCurr = pfnCount;
440 else
442 pfnCurr = pfnLeft;
445 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(VMBUS_CHANNEL_GPADL_BODY) + pfnCurr*sizeof(u64);
446 msgBody = kzalloc(msgSize, GFP_KERNEL);
447 ASSERT(msgBody);
448 msgBody->MessageSize = msgSize;
449 (*MessageCount)++;
450 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)msgBody->Msg;
452 /* FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit */
453 /* gpadlBody->Gpadl = kbuffer; */
454 for (i=0; i<pfnCurr; i++)
456 gpadlBody->Pfn[i] = pfn + pfnSum + i;
459 /* add to msg header */
460 INSERT_TAIL_LIST(&msgHeader->SubMsgList, &msgBody->MsgListEntry);
461 pfnSum += pfnCurr;
462 pfnLeft -= pfnCurr;
465 else
467 /* everything fits in a header */
468 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pageCount*sizeof(u64);
469 msgHeader = kzalloc(msgSize, GFP_KERNEL);
470 msgHeader->MessageSize=msgSize;
472 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
473 gpaHeader->RangeCount = 1;
474 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
475 gpaHeader->Range[0].ByteOffset = 0;
476 gpaHeader->Range[0].ByteCount = Size;
477 for (i=0; i<pageCount; i++)
479 gpaHeader->Range[0].PfnArray[i] = pfn+i;
482 *MsgInfo = msgHeader;
483 *MessageCount = 1;
486 return 0;
490 /*++;
492 Name:
493 VmbusChannelEstablishGpadl()
495 Description:
496 Estabish a GPADL for the specified buffer
498 --*/
499 int VmbusChannelEstablishGpadl(struct vmbus_channel *Channel,
500 void * Kbuffer, /* from kmalloc() */
501 u32 Size, /* page-size multiple */
502 u32 *GpadlHandle
505 int ret=0;
506 VMBUS_CHANNEL_GPADL_HEADER* gpadlMsg;
507 VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
508 /* VMBUS_CHANNEL_GPADL_CREATED* gpadlCreated; */
510 struct vmbus_channel_msginfo *msgInfo;
511 struct vmbus_channel_msginfo *subMsgInfo;
513 u32 msgCount;
514 LIST_ENTRY* anchor;
515 LIST_ENTRY* curr;
516 u32 nextGpadlHandle;
517 unsigned long flags;
519 DPRINT_ENTER(VMBUS);
521 nextGpadlHandle = atomic_read(&gVmbusConnection.NextGpadlHandle);
522 atomic_inc(&gVmbusConnection.NextGpadlHandle);
524 VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
525 ASSERT(msgInfo != NULL);
526 ASSERT(msgCount >0);
528 msgInfo->WaitEvent = osd_WaitEventCreate();
529 gpadlMsg = (VMBUS_CHANNEL_GPADL_HEADER*)msgInfo->Msg;
530 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
531 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
532 gpadlMsg->Gpadl = nextGpadlHandle;
534 DumpGpadlHeader(gpadlMsg);
536 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
537 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
538 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
540 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d", Kbuffer, Size, msgCount);
542 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd", msgInfo->MessageSize - sizeof(*msgInfo));
544 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize - sizeof(*msgInfo));
545 if (ret != 0)
547 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
548 goto Cleanup;
551 if (msgCount>1)
553 ITERATE_LIST_ENTRIES(anchor, curr, &msgInfo->SubMsgList)
555 subMsgInfo = (struct vmbus_channel_msginfo*)curr;
556 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)subMsgInfo->Msg;
558 gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
559 gpadlBody->Gpadl = nextGpadlHandle;
561 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %zd", subMsgInfo->MessageSize - sizeof(*subMsgInfo));
563 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize - sizeof(*subMsgInfo));
564 ret = VmbusPostMessage(gpadlBody, subMsgInfo->MessageSize - sizeof(*subMsgInfo));
565 ASSERT(ret == 0);
568 osd_WaitEventWait(msgInfo->WaitEvent);
570 /* At this point, we received the gpadl created msg */
571 DPRINT_DBG(VMBUS, "Received GPADL created (relid %d, status %d handle %x)",
572 Channel->OfferMsg.ChildRelId,
573 msgInfo->Response.GpadlCreated.CreationStatus,
574 gpadlMsg->Gpadl);
576 *GpadlHandle = gpadlMsg->Gpadl;
578 Cleanup:
579 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
580 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
581 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
583 kfree(msgInfo->WaitEvent);
584 kfree(msgInfo);
586 DPRINT_EXIT(VMBUS);
588 return ret;
593 /*++;
595 Name:
596 VmbusChannelTeardownGpadl()
598 Description:
599 Teardown the specified GPADL handle
601 --*/
602 int VmbusChannelTeardownGpadl(struct vmbus_channel *Channel, u32 GpadlHandle)
604 int ret=0;
605 VMBUS_CHANNEL_GPADL_TEARDOWN *msg;
606 struct vmbus_channel_msginfo *info;
607 unsigned long flags;
609 DPRINT_ENTER(VMBUS);
611 ASSERT(GpadlHandle != 0);
613 info = kmalloc(sizeof(*info) + sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN), GFP_KERNEL);
614 ASSERT(info != NULL);
616 info->WaitEvent = osd_WaitEventCreate();
618 msg = (VMBUS_CHANNEL_GPADL_TEARDOWN*)info->Msg;
620 msg->Header.MessageType = ChannelMessageGpadlTeardown;
621 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
622 msg->Gpadl = GpadlHandle;
624 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
625 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &info->MsgListEntry);
626 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
628 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN));
629 if (ret != 0)
631 /* TODO: */
634 osd_WaitEventWait(info->WaitEvent);
636 /* Received a torndown response */
637 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
638 REMOVE_ENTRY_LIST(&info->MsgListEntry);
639 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
641 kfree(info->WaitEvent);
642 kfree(info);
644 DPRINT_EXIT(VMBUS);
646 return ret;
650 /*++
652 Name:
653 VmbusChannelClose()
655 Description:
656 Close the specified channel
658 --*/
659 void VmbusChannelClose(struct vmbus_channel *Channel)
661 int ret=0;
662 VMBUS_CHANNEL_CLOSE_CHANNEL* msg;
663 struct vmbus_channel_msginfo *info;
664 unsigned long flags;
666 DPRINT_ENTER(VMBUS);
668 /* Stop callback and cancel the timer asap */
669 Channel->OnChannelCallback = NULL;
670 del_timer(&Channel->poll_timer);
672 /* Send a closing message */
673 info = kmalloc(sizeof(*info) + sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL), GFP_KERNEL);
674 ASSERT(info != NULL);
676 /* info->waitEvent = osd_WaitEventCreate(); */
678 msg = (VMBUS_CHANNEL_CLOSE_CHANNEL*)info->Msg;
679 msg->Header.MessageType = ChannelMessageCloseChannel;
680 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
682 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL));
683 if (ret != 0)
685 /* TODO: */
688 /* Tear down the gpadl for the channel's ring buffer */
689 if (Channel->RingBufferGpadlHandle)
691 VmbusChannelTeardownGpadl(Channel, Channel->RingBufferGpadlHandle);
694 /* TODO: Send a msg to release the childRelId */
696 /* Cleanup the ring buffers for this channel */
697 RingBufferCleanup(&Channel->Outbound);
698 RingBufferCleanup(&Channel->Inbound);
700 osd_PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
702 kfree(info);
706 * If we are closing the channel during an error path in
707 * opening the channel, don't free the channel since the
708 * caller will free the channel
711 if (Channel->State == CHANNEL_OPEN_STATE)
713 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
714 REMOVE_ENTRY_LIST(&Channel->ListEntry);
715 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
717 FreeVmbusChannel(Channel);
720 DPRINT_EXIT(VMBUS);
724 /*++
726 Name:
727 VmbusChannelSendPacket()
729 Description:
730 Send the specified buffer on the given channel
732 --*/
733 int VmbusChannelSendPacket(struct vmbus_channel *Channel,
734 const void * Buffer,
735 u32 BufferLen,
736 u64 RequestId,
737 VMBUS_PACKET_TYPE Type,
738 u32 Flags
741 int ret=0;
742 VMPACKET_DESCRIPTOR desc;
743 u32 packetLen = sizeof(VMPACKET_DESCRIPTOR) + BufferLen;
744 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
745 struct scatterlist bufferList[3];
746 u64 alignedData=0;
748 DPRINT_ENTER(VMBUS);
749 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d", Channel, Buffer, BufferLen);
751 DumpVmbusChannel(Channel);
753 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
755 /* Setup the descriptor */
756 desc.Type = Type; /* VmbusPacketTypeDataInBand; */
757 desc.Flags = Flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
758 desc.DataOffset8 = sizeof(VMPACKET_DESCRIPTOR) >> 3; /* in 8-bytes granularity */
759 desc.Length8 = (u16)(packetLenAligned >> 3);
760 desc.TransactionId = RequestId;
762 sg_init_table(bufferList,3);
763 sg_set_buf(&bufferList[0], &desc, sizeof(VMPACKET_DESCRIPTOR));
764 sg_set_buf(&bufferList[1], Buffer, BufferLen);
765 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
767 ret = RingBufferWrite(
768 &Channel->Outbound,
769 bufferList,
772 /* TODO: We should determine if this is optional */
773 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
775 VmbusChannelSetEvent(Channel);
778 DPRINT_EXIT(VMBUS);
780 return ret;
784 /*++
786 Name:
787 VmbusChannelSendPacketPageBuffer()
789 Description:
790 Send a range of single-page buffer packets using a GPADL Direct packet type.
792 --*/
793 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel *Channel,
794 struct hv_page_buffer PageBuffers[],
795 u32 PageCount,
796 void * Buffer,
797 u32 BufferLen,
798 u64 RequestId
801 int ret=0;
802 int i=0;
803 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
804 u32 descSize;
805 u32 packetLen;
806 u32 packetLenAligned;
807 struct scatterlist bufferList[3];
808 u64 alignedData=0;
810 DPRINT_ENTER(VMBUS);
812 ASSERT(PageCount <= MAX_PAGE_BUFFER_COUNT);
814 DumpVmbusChannel(Channel);
816 /* Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support */
817 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER) - ((MAX_PAGE_BUFFER_COUNT - PageCount)*sizeof(struct hv_page_buffer));
818 packetLen = descSize + BufferLen;
819 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
821 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
823 /* Setup the descriptor */
824 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
825 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
826 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
827 desc.Length8 = (u16)(packetLenAligned >> 3);
828 desc.TransactionId = RequestId;
829 desc.RangeCount = PageCount;
831 for (i=0; i<PageCount; i++)
833 desc.Range[i].Length = PageBuffers[i].Length;
834 desc.Range[i].Offset = PageBuffers[i].Offset;
835 desc.Range[i].Pfn = PageBuffers[i].Pfn;
838 sg_init_table(bufferList,3);
839 sg_set_buf(&bufferList[0], &desc, descSize);
840 sg_set_buf(&bufferList[1], Buffer, BufferLen);
841 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
843 ret = RingBufferWrite(
844 &Channel->Outbound,
845 bufferList,
848 /* TODO: We should determine if this is optional */
849 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
851 VmbusChannelSetEvent(Channel);
854 DPRINT_EXIT(VMBUS);
856 return ret;
861 /*++
863 Name:
864 VmbusChannelSendPacketMultiPageBuffer()
866 Description:
867 Send a multi-page buffer packet using a GPADL Direct packet type.
869 --*/
870 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel *Channel,
871 struct hv_multipage_buffer *MultiPageBuffer,
872 void * Buffer,
873 u32 BufferLen,
874 u64 RequestId
877 int ret=0;
878 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
879 u32 descSize;
880 u32 packetLen;
881 u32 packetLenAligned;
882 struct scatterlist bufferList[3];
883 u64 alignedData=0;
884 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset, MultiPageBuffer->Length);
886 DPRINT_ENTER(VMBUS);
888 DumpVmbusChannel(Channel);
890 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
892 ASSERT(PfnCount > 0);
893 ASSERT(PfnCount <= MAX_MULTIPAGE_BUFFER_COUNT);
895 /* Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support */
896 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) - ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount)*sizeof(u64));
897 packetLen = descSize + BufferLen;
898 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
900 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
902 /* Setup the descriptor */
903 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
904 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
905 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
906 desc.Length8 = (u16)(packetLenAligned >> 3);
907 desc.TransactionId = RequestId;
908 desc.RangeCount = 1;
910 desc.Range.Length = MultiPageBuffer->Length;
911 desc.Range.Offset = MultiPageBuffer->Offset;
913 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray, PfnCount*sizeof(u64));
915 sg_init_table(bufferList,3);
916 sg_set_buf(&bufferList[0], &desc, descSize);
917 sg_set_buf(&bufferList[1], Buffer, BufferLen);
918 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
920 ret = RingBufferWrite(
921 &Channel->Outbound,
922 bufferList,
925 /* TODO: We should determine if this is optional */
926 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
928 VmbusChannelSetEvent(Channel);
931 DPRINT_EXIT(VMBUS);
933 return ret;
937 /*++
939 Name:
940 VmbusChannelRecvPacket()
942 Description:
943 Retrieve the user packet on the specified channel
945 --*/
946 /* TODO: Do we ever receive a gpa direct packet other than the ones we send ? */
947 int VmbusChannelRecvPacket(struct vmbus_channel *Channel,
948 void *Buffer,
949 u32 BufferLen,
950 u32 *BufferActualLen,
951 u64 *RequestId)
953 VMPACKET_DESCRIPTOR desc;
954 u32 packetLen;
955 u32 userLen;
956 int ret;
957 unsigned long flags;
959 DPRINT_ENTER(VMBUS);
961 *BufferActualLen = 0;
962 *RequestId = 0;
964 spin_lock_irqsave(&Channel->inbound_lock, flags);
966 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
967 if (ret != 0)
969 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
971 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
972 DPRINT_EXIT(VMBUS);
973 return 0;
976 /* VmbusChannelClearEvent(Channel); */
978 packetLen = desc.Length8 << 3;
979 userLen = packetLen - (desc.DataOffset8 << 3);
980 /* ASSERT(userLen > 0); */
982 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
983 Channel,
984 Channel->OfferMsg.ChildRelId,
985 desc.Type,
986 desc.Flags,
987 desc.TransactionId, packetLen, userLen);
989 *BufferActualLen = userLen;
991 if (userLen > BufferLen)
993 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
995 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d", BufferLen, userLen);
996 DPRINT_EXIT(VMBUS);
998 return -1;
1001 *RequestId = desc.TransactionId;
1003 /* Copy over the packet to the user buffer */
1004 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen, (desc.DataOffset8 << 3));
1006 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1008 DPRINT_EXIT(VMBUS);
1010 return 0;
1013 /*++
1015 Name:
1016 VmbusChannelRecvPacketRaw()
1018 Description:
1019 Retrieve the raw packet on the specified channel
1021 --*/
1022 int VmbusChannelRecvPacketRaw(struct vmbus_channel *Channel,
1023 void * Buffer,
1024 u32 BufferLen,
1025 u32* BufferActualLen,
1026 u64* RequestId
1029 VMPACKET_DESCRIPTOR desc;
1030 u32 packetLen;
1031 u32 userLen;
1032 int ret;
1033 unsigned long flags;
1035 DPRINT_ENTER(VMBUS);
1037 *BufferActualLen = 0;
1038 *RequestId = 0;
1040 spin_lock_irqsave(&Channel->inbound_lock, flags);
1042 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1043 if (ret != 0)
1045 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1047 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1048 DPRINT_EXIT(VMBUS);
1049 return 0;
1052 /* VmbusChannelClearEvent(Channel); */
1054 packetLen = desc.Length8 << 3;
1055 userLen = packetLen - (desc.DataOffset8 << 3);
1057 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1058 Channel,
1059 Channel->OfferMsg.ChildRelId,
1060 desc.Type,
1061 desc.Flags,
1062 desc.TransactionId, packetLen, userLen);
1064 *BufferActualLen = packetLen;
1066 if (packetLen > BufferLen)
1068 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1070 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen, BufferLen);
1071 DPRINT_EXIT(VMBUS);
1072 return -2;
1075 *RequestId = desc.TransactionId;
1077 /* Copy over the entire packet to the user buffer */
1078 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1080 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1082 DPRINT_EXIT(VMBUS);
1084 return 0;
1088 /*++
1090 Name:
1091 VmbusChannelOnChannelEvent()
1093 Description:
1094 Channel event callback
1096 --*/
1097 void VmbusChannelOnChannelEvent(struct vmbus_channel *Channel)
1099 DumpVmbusChannel(Channel);
1100 ASSERT(Channel->OnChannelCallback);
1101 #ifdef ENABLE_POLLING
1102 del_timer(&Channel->poll_timer);
1103 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1104 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
1105 add_timer(&channel->poll_timer);
1106 #else
1107 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1108 #endif
1111 /*++
1113 Name:
1114 VmbusChannelOnTimer()
1116 Description:
1117 Timer event callback
1119 --*/
1120 void VmbusChannelOnTimer(unsigned long data)
1122 struct vmbus_channel *channel = (struct vmbus_channel *)data;
1124 if (channel->OnChannelCallback)
1126 channel->OnChannelCallback(channel->ChannelCallbackContext);
1127 #ifdef ENABLE_POLLING
1128 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
1129 add_timer(&channel->poll_timer);
1130 #endif
1135 /*++
1137 Name:
1138 DumpVmbusChannel()
1140 Description:
1141 Dump vmbus channel info to the console
1143 --*/
1144 static void DumpVmbusChannel(struct vmbus_channel *Channel)
1146 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1147 DumpRingInfo(&Channel->Outbound, "Outbound ");
1148 DumpRingInfo(&Channel->Inbound, "Inbound ");
1152 /* eof */