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
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.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.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
,
38 static void DumpVmbusChannel(struct vmbus_channel
*channel
);
39 static void VmbusChannelSetEvent(struct vmbus_channel
*channel
);
45 struct hv_monitor_page
*MonitorPage
51 DPRINT_DBG(VMBUS
, "monitorPage - %p, trigger state - %d", MonitorPage
, MonitorPage
->TriggerState
);
55 DPRINT_DBG(VMBUS
, "trigger group (%d) - %llx", i
, MonitorPage
->TriggerGroup
[i
].AsUINT64
);
62 DPRINT_DBG(VMBUS
, "latency (%d)(%d) - %llx", i
, j
, MonitorPage
->Latency
[i
][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
);
80 VmbusChannelSetEvent()
83 Trigger an event notification on the specified channel.
86 static void VmbusChannelSetEvent(struct vmbus_channel
*Channel
)
88 struct hv_monitor_page
*monitorPage
;
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
);
108 VmbusSetEvent(Channel
->OfferMsg
.ChildRelId
);
115 static void VmbusChannelClearEvent(struct vmbus_channel
*channel
)
117 struct hv_monitor_page
*monitorPage
;
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
);
141 VmbusChannelGetDebugInfo()
144 Retrieve various channel debug info
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
;
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
);
185 Open the specified channel.
188 int VmbusChannelOpen(struct vmbus_channel
*NewChannel
,
189 u32 SendRingBufferSize
,
190 u32 RecvRingBufferSize
,
193 PFN_CHANNEL_CALLBACK pfnOnChannelCallback
,
198 VMBUS_CHANNEL_OPEN_CHANNEL
* openMsg
;
199 struct vmbus_channel_msginfo
*openInfo
;
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); */
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>",
239 NewChannel
->OfferMsg
.ChildRelId
,
240 NewChannel
->RingBufferGpadlHandle
,
241 NewChannel
->Outbound
.RingBuffer
,
242 NewChannel
->Outbound
.RingSize
,
243 NewChannel
->Inbound
.RingBuffer
,
244 NewChannel
->Inbound
.RingSize
,
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
);
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
));
277 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
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
);
290 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!", NewChannel
, openInfo
->Response
.OpenResult
.Status
);
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
);
312 Dump the gpadl body message to the console for debugging purposes.
315 static void DumpGpadlBody(
316 VMBUS_CHANNEL_GPADL_BODY
*Gpadl
,
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
]);
338 Dump the gpadl header message to the console for debugging purposes.
341 static void DumpGpadlHeader(
342 VMBUS_CHANNEL_GPADL_HEADER
*Gpadl
349 DPRINT_DBG(VMBUS
, "gpadl header - relid %d, range count %d, range buflen %d",
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
]);
371 VmbusChannelCreateGpadlHeader()
374 Creates a gpadl for the specified buffer
378 VmbusChannelCreateGpadlHeader(
379 void * Kbuffer
, /* from kmalloc() */
380 u32 Size
, /* page-size multiple */
381 struct vmbus_channel_msginfo
**MsgInfo
,
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
;
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
;
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 */
436 if (pfnLeft
> pfnCount
)
445 msgSize
= sizeof(struct vmbus_channel_msginfo
) + sizeof(VMBUS_CHANNEL_GPADL_BODY
) + pfnCurr
*sizeof(u64
);
446 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
448 msgBody
->MessageSize
= msgSize
;
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
);
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
;
493 VmbusChannelEstablishGpadl()
496 Estabish a GPADL for the specified buffer
499 int VmbusChannelEstablishGpadl(struct vmbus_channel
*Channel
,
500 void * Kbuffer
, /* from kmalloc() */
501 u32 Size
, /* page-size multiple */
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
;
521 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
522 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
524 VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
525 ASSERT(msgInfo
!= NULL
);
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
));
547 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
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
));
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
,
576 *GpadlHandle
= gpadlMsg
->Gpadl
;
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
);
596 VmbusChannelTeardownGpadl()
599 Teardown the specified GPADL handle
602 int VmbusChannelTeardownGpadl(struct vmbus_channel
*Channel
, u32 GpadlHandle
)
605 VMBUS_CHANNEL_GPADL_TEARDOWN
*msg
;
606 struct vmbus_channel_msginfo
*info
;
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
));
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
);
656 Close the specified channel
659 void VmbusChannelClose(struct vmbus_channel
*Channel
)
662 VMBUS_CHANNEL_CLOSE_CHANNEL
* msg
;
663 struct vmbus_channel_msginfo
*info
;
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
));
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
);
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
);
727 VmbusChannelSendPacket()
730 Send the specified buffer on the given channel
733 int VmbusChannelSendPacket(struct vmbus_channel
*Channel
,
737 VMBUS_PACKET_TYPE Type
,
742 VMPACKET_DESCRIPTOR desc
;
743 u32 packetLen
= sizeof(VMPACKET_DESCRIPTOR
) + BufferLen
;
744 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
745 struct scatterlist bufferList
[3];
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(
772 /* TODO: We should determine if this is optional */
773 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
775 VmbusChannelSetEvent(Channel
);
787 VmbusChannelSendPacketPageBuffer()
790 Send a range of single-page buffer packets using a GPADL Direct packet type.
793 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel
*Channel
,
794 struct hv_page_buffer PageBuffers
[],
803 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
806 u32 packetLenAligned
;
807 struct scatterlist bufferList
[3];
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(
848 /* TODO: We should determine if this is optional */
849 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
851 VmbusChannelSetEvent(Channel
);
864 VmbusChannelSendPacketMultiPageBuffer()
867 Send a multi-page buffer packet using a GPADL Direct packet type.
870 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel
*Channel
,
871 struct hv_multipage_buffer
*MultiPageBuffer
,
878 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
881 u32 packetLenAligned
;
882 struct scatterlist bufferList
[3];
884 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
);
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
;
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(
925 /* TODO: We should determine if this is optional */
926 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
928 VmbusChannelSetEvent(Channel
);
940 VmbusChannelRecvPacket()
943 Retrieve the user packet on the specified channel
946 /* TODO: Do we ever receive a gpa direct packet other than the ones we send ? */
947 int VmbusChannelRecvPacket(struct vmbus_channel
*Channel
,
950 u32
*BufferActualLen
,
953 VMPACKET_DESCRIPTOR desc
;
961 *BufferActualLen
= 0;
964 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
966 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
, sizeof(VMPACKET_DESCRIPTOR
));
969 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
971 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
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> ",
984 Channel
->OfferMsg
.ChildRelId
,
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
);
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
);
1016 VmbusChannelRecvPacketRaw()
1019 Retrieve the raw packet on the specified channel
1022 int VmbusChannelRecvPacketRaw(struct vmbus_channel
*Channel
,
1025 u32
* BufferActualLen
,
1029 VMPACKET_DESCRIPTOR desc
;
1033 unsigned long flags
;
1035 DPRINT_ENTER(VMBUS
);
1037 *BufferActualLen
= 0;
1040 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
1042 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
, sizeof(VMPACKET_DESCRIPTOR
));
1045 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1047 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
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> ",
1059 Channel
->OfferMsg
.ChildRelId
,
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
);
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
);
1091 VmbusChannelOnChannelEvent()
1094 Channel event callback
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
);
1107 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1114 VmbusChannelOnTimer()
1117 Timer event callback
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
);
1141 Dump vmbus channel info to the console
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 ");