2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
27 #include "VmbusPrivate.h"
29 /* Internal routines */
30 static int VmbusChannelCreateGpadlHeader(
31 void *Kbuffer
, /* must be phys and virt contiguous */
32 u32 Size
, /* page-size multiple */
33 struct vmbus_channel_msginfo
**msgInfo
,
35 static void DumpVmbusChannel(struct vmbus_channel
*channel
);
36 static void VmbusChannelSetEvent(struct vmbus_channel
*channel
);
40 static void DumpMonitorPage(struct hv_monitor_page
*MonitorPage
)
45 DPRINT_DBG(VMBUS
, "monitorPage - %p, trigger state - %d",
46 MonitorPage
, MonitorPage
->TriggerState
);
48 for (i
= 0; i
< 4; i
++)
49 DPRINT_DBG(VMBUS
, "trigger group (%d) - %llx", i
,
50 MonitorPage
->TriggerGroup
[i
].AsUINT64
);
52 for (i
= 0; i
< 4; i
++) {
53 for (j
= 0; j
< 32; j
++) {
54 DPRINT_DBG(VMBUS
, "latency (%d)(%d) - %llx", i
, j
,
55 MonitorPage
->Latency
[i
][j
]);
58 for (i
= 0; i
< 4; i
++) {
59 for (j
= 0; j
< 32; j
++) {
60 DPRINT_DBG(VMBUS
, "param-conn id (%d)(%d) - %d", i
, j
,
61 MonitorPage
->Parameter
[i
][j
].ConnectionId
.Asu32
);
62 DPRINT_DBG(VMBUS
, "param-flag (%d)(%d) - %d", i
, j
,
63 MonitorPage
->Parameter
[i
][j
].FlagNumber
);
70 * VmbusChannelSetEvent - Trigger an event notification on the specified
73 static void VmbusChannelSetEvent(struct vmbus_channel
*Channel
)
75 struct hv_monitor_page
*monitorPage
;
79 if (Channel
->OfferMsg
.MonitorAllocated
) {
80 /* Each u32 represents 32 channels */
81 set_bit(Channel
->OfferMsg
.ChildRelId
& 31,
82 (unsigned long *) gVmbusConnection
.SendInterruptPage
+
83 (Channel
->OfferMsg
.ChildRelId
>> 5));
85 monitorPage
= gVmbusConnection
.MonitorPages
;
86 monitorPage
++; /* Get the child to parent monitor page */
88 set_bit(Channel
->MonitorBit
,
89 (unsigned long *)&monitorPage
->TriggerGroup
90 [Channel
->MonitorGroup
].Pending
);
93 VmbusSetEvent(Channel
->OfferMsg
.ChildRelId
);
100 static void VmbusChannelClearEvent(struct vmbus_channel
*channel
)
102 struct hv_monitor_page
*monitorPage
;
106 if (Channel
->OfferMsg
.MonitorAllocated
) {
107 /* Each u32 represents 32 channels */
108 clear_bit(Channel
->OfferMsg
.ChildRelId
& 31,
109 (unsigned long *)gVmbusConnection
.SendInterruptPage
+
110 (Channel
->OfferMsg
.ChildRelId
>> 5));
113 (struct hv_monitor_page
*)gVmbusConnection
.MonitorPages
;
114 monitorPage
++; /* Get the child to parent monitor page */
116 clear_bit(Channel
->MonitorBit
,
117 (unsigned long *)&monitorPage
->TriggerGroup
118 [Channel
->MonitorGroup
].Pending
);
126 * VmbusChannelGetDebugInfo -Retrieve various channel debug info
128 void VmbusChannelGetDebugInfo(struct vmbus_channel
*Channel
,
129 struct vmbus_channel_debug_info
*DebugInfo
)
131 struct hv_monitor_page
*monitorPage
;
132 u8 monitorGroup
= (u8
)Channel
->OfferMsg
.MonitorId
/ 32;
133 u8 monitorOffset
= (u8
)Channel
->OfferMsg
.MonitorId
% 32;
134 /* u32 monitorBit = 1 << monitorOffset; */
136 DebugInfo
->RelId
= Channel
->OfferMsg
.ChildRelId
;
137 DebugInfo
->State
= Channel
->State
;
138 memcpy(&DebugInfo
->InterfaceType
,
139 &Channel
->OfferMsg
.Offer
.InterfaceType
, sizeof(struct hv_guid
));
140 memcpy(&DebugInfo
->InterfaceInstance
,
141 &Channel
->OfferMsg
.Offer
.InterfaceInstance
,
142 sizeof(struct hv_guid
));
144 monitorPage
= (struct hv_monitor_page
*)gVmbusConnection
.MonitorPages
;
146 DebugInfo
->MonitorId
= Channel
->OfferMsg
.MonitorId
;
148 DebugInfo
->ServerMonitorPending
=
149 monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
150 DebugInfo
->ServerMonitorLatency
=
151 monitorPage
->Latency
[monitorGroup
][monitorOffset
];
152 DebugInfo
->ServerMonitorConnectionId
=
153 monitorPage
->Parameter
[monitorGroup
]
154 [monitorOffset
].ConnectionId
.u
.Id
;
158 DebugInfo
->ClientMonitorPending
=
159 monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
160 DebugInfo
->ClientMonitorLatency
=
161 monitorPage
->Latency
[monitorGroup
][monitorOffset
];
162 DebugInfo
->ClientMonitorConnectionId
=
163 monitorPage
->Parameter
[monitorGroup
]
164 [monitorOffset
].ConnectionId
.u
.Id
;
166 RingBufferGetDebugInfo(&Channel
->Inbound
, &DebugInfo
->Inbound
);
167 RingBufferGetDebugInfo(&Channel
->Outbound
, &DebugInfo
->Outbound
);
171 * VmbusChannelOpen - Open the specified channel.
173 int VmbusChannelOpen(struct vmbus_channel
*NewChannel
, u32 SendRingBufferSize
,
174 u32 RecvRingBufferSize
, void *UserData
, u32 UserDataLen
,
175 void (*OnChannelCallback
)(void *context
), void *Context
)
177 struct vmbus_channel_open_channel
*openMsg
;
178 struct vmbus_channel_msginfo
*openInfo
= NULL
;
185 /* Aligned to page size */
186 ASSERT(!(SendRingBufferSize
& (PAGE_SIZE
- 1)));
187 ASSERT(!(RecvRingBufferSize
& (PAGE_SIZE
- 1)));
189 NewChannel
->OnChannelCallback
= OnChannelCallback
;
190 NewChannel
->ChannelCallbackContext
= Context
;
192 /* Allocate the ring buffer */
193 out
= osd_PageAlloc((SendRingBufferSize
+ RecvRingBufferSize
)
198 ASSERT(((unsigned long)out
& (PAGE_SIZE
-1)) == 0);
200 in
= (void *)((unsigned long)out
+ SendRingBufferSize
);
202 NewChannel
->RingBufferPages
= out
;
203 NewChannel
->RingBufferPageCount
= (SendRingBufferSize
+
204 RecvRingBufferSize
) >> PAGE_SHIFT
;
206 RingBufferInit(&NewChannel
->Outbound
, out
, SendRingBufferSize
);
208 RingBufferInit(&NewChannel
->Inbound
, in
, RecvRingBufferSize
);
210 /* Establish the gpadl for the ring buffer */
211 DPRINT_DBG(VMBUS
, "Establishing ring buffer's gpadl for channel %p...",
214 NewChannel
->RingBufferGpadlHandle
= 0;
216 ret
= VmbusChannelEstablishGpadl(NewChannel
,
217 NewChannel
->Outbound
.RingBuffer
,
220 &NewChannel
->RingBufferGpadlHandle
);
227 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p "
228 "size %d recv ring %p size %d, downstreamoffset %d>",
229 NewChannel
, NewChannel
->OfferMsg
.ChildRelId
,
230 NewChannel
->RingBufferGpadlHandle
,
231 NewChannel
->Outbound
.RingBuffer
,
232 NewChannel
->Outbound
.RingSize
,
233 NewChannel
->Inbound
.RingBuffer
,
234 NewChannel
->Inbound
.RingSize
,
237 /* Create and init the channel open message */
238 openInfo
= kmalloc(sizeof(*openInfo
) +
239 sizeof(struct vmbus_channel_open_channel
),
246 openInfo
->WaitEvent
= osd_WaitEventCreate();
247 if (!openInfo
->WaitEvent
) {
252 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->Msg
;
253 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
254 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
255 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
256 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
257 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>>
259 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
261 if (UserDataLen
> MAX_USER_DEFINED_BYTES
) {
267 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
269 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
270 list_add_tail(&openInfo
->MsgListEntry
,
271 &gVmbusConnection
.ChannelMsgList
);
272 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
274 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
276 ret
= VmbusPostMessage(openMsg
,
277 sizeof(struct vmbus_channel_open_channel
));
279 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
283 /* FIXME: Need to time-out here */
284 osd_WaitEventWait(openInfo
->WaitEvent
);
286 if (openInfo
->Response
.OpenResult
.Status
== 0)
287 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
289 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!",
290 NewChannel
, openInfo
->Response
.OpenResult
.Status
);
293 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
294 list_del(&openInfo
->MsgListEntry
);
295 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
297 kfree(openInfo
->WaitEvent
);
305 RingBufferCleanup(&NewChannel
->Outbound
);
306 RingBufferCleanup(&NewChannel
->Inbound
);
307 osd_PageFree(out
, (SendRingBufferSize
+ RecvRingBufferSize
)
314 * DumpGpadlBody - Dump the gpadl body message to the console for
315 * debugging purposes.
317 static void DumpGpadlBody(struct vmbus_channel_gpadl_body
*Gpadl
, u32 Len
)
322 pfnCount
= (Len
- sizeof(struct vmbus_channel_gpadl_body
)) /
324 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
326 for (i
= 0; i
< pfnCount
; i
++)
327 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
332 * DumpGpadlHeader - Dump the gpadl header message to the console for
333 * debugging purposes.
335 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header
*Gpadl
)
341 "gpadl header - relid %d, range count %d, range buflen %d",
342 Gpadl
->ChildRelId
, Gpadl
->RangeCount
, Gpadl
->RangeBufLen
);
343 for (i
= 0; i
< Gpadl
->RangeCount
; i
++) {
344 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
345 pageCount
= (pageCount
> 26) ? 26 : pageCount
;
347 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
348 "page count %d", i
, Gpadl
->Range
[i
].ByteCount
,
349 Gpadl
->Range
[i
].ByteOffset
, pageCount
);
351 for (j
= 0; j
< pageCount
; j
++)
352 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
353 Gpadl
->Range
[i
].PfnArray
[j
]);
358 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
360 static int VmbusChannelCreateGpadlHeader(void *Kbuffer
, u32 Size
,
361 struct vmbus_channel_msginfo
**MsgInfo
,
366 unsigned long long pfn
;
367 struct vmbus_channel_gpadl_header
*gpaHeader
;
368 struct vmbus_channel_gpadl_body
*gpadlBody
;
369 struct vmbus_channel_msginfo
*msgHeader
;
370 struct vmbus_channel_msginfo
*msgBody
;
373 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
375 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
376 ASSERT((Size
& (PAGE_SIZE
-1)) == 0);
378 pageCount
= Size
>> PAGE_SHIFT
;
379 pfn
= virt_to_phys(Kbuffer
) >> PAGE_SHIFT
;
381 /* do we need a gpadl body msg */
382 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
383 sizeof(struct vmbus_channel_gpadl_header
) -
384 sizeof(struct gpa_range
);
385 pfnCount
= pfnSize
/ sizeof(u64
);
387 if (pageCount
> pfnCount
) {
388 /* we need a gpadl body */
389 /* fill in the header */
390 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
391 sizeof(struct vmbus_channel_gpadl_header
) +
392 sizeof(struct gpa_range
) + pfnCount
* sizeof(u64
);
393 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
397 INIT_LIST_HEAD(&msgHeader
->SubMsgList
);
398 msgHeader
->MessageSize
= msgSize
;
400 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
401 gpaHeader
->RangeCount
= 1;
402 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
403 pageCount
* sizeof(u64
);
404 gpaHeader
->Range
[0].ByteOffset
= 0;
405 gpaHeader
->Range
[0].ByteCount
= Size
;
406 for (i
= 0; i
< pfnCount
; i
++)
407 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
408 *MsgInfo
= msgHeader
;
412 pfnLeft
= pageCount
- pfnCount
;
414 /* how many pfns can we fit */
415 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
416 sizeof(struct vmbus_channel_gpadl_body
);
417 pfnCount
= pfnSize
/ sizeof(u64
);
419 /* fill in the body */
421 if (pfnLeft
> pfnCount
)
426 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
427 sizeof(struct vmbus_channel_gpadl_body
) +
428 pfnCurr
* sizeof(u64
);
429 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
430 /* FIXME: we probably need to more if this fails */
433 msgBody
->MessageSize
= msgSize
;
436 (struct vmbus_channel_gpadl_body
*)msgBody
->Msg
;
440 * Gpadl is u32 and we are using a pointer which could
443 /* gpadlBody->Gpadl = kbuffer; */
444 for (i
= 0; i
< pfnCurr
; i
++)
445 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
447 /* add to msg header */
448 list_add_tail(&msgBody
->MsgListEntry
,
449 &msgHeader
->SubMsgList
);
454 /* everything fits in a header */
455 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
456 sizeof(struct vmbus_channel_gpadl_header
) +
457 sizeof(struct gpa_range
) + pageCount
* sizeof(u64
);
458 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
459 msgHeader
->MessageSize
= msgSize
;
461 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
462 gpaHeader
->RangeCount
= 1;
463 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
464 pageCount
* sizeof(u64
);
465 gpaHeader
->Range
[0].ByteOffset
= 0;
466 gpaHeader
->Range
[0].ByteCount
= Size
;
467 for (i
= 0; i
< pageCount
; i
++)
468 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
470 *MsgInfo
= msgHeader
;
482 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
484 * @Channel: a channel
485 * @Kbuffer: from kmalloc
486 * @Size: page-size multiple
487 * @GpadlHandle: some funky thing
489 int VmbusChannelEstablishGpadl(struct vmbus_channel
*Channel
, void *Kbuffer
,
490 u32 Size
, u32
*GpadlHandle
)
492 struct vmbus_channel_gpadl_header
*gpadlMsg
;
493 struct vmbus_channel_gpadl_body
*gpadlBody
;
494 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
495 struct vmbus_channel_msginfo
*msgInfo
= NULL
;
496 struct vmbus_channel_msginfo
*subMsgInfo
;
498 struct list_head
*curr
;
505 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
506 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
508 ret
= VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
512 msgInfo
->WaitEvent
= osd_WaitEventCreate();
513 if (!msgInfo
->WaitEvent
) {
518 gpadlMsg
= (struct vmbus_channel_gpadl_header
*)msgInfo
->Msg
;
519 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
520 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
521 gpadlMsg
->Gpadl
= nextGpadlHandle
;
523 DumpGpadlHeader(gpadlMsg
);
525 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
526 list_add_tail(&msgInfo
->MsgListEntry
,
527 &gVmbusConnection
.ChannelMsgList
);
529 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
530 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d",
531 Kbuffer
, Size
, msgCount
);
533 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd",
534 msgInfo
->MessageSize
- sizeof(*msgInfo
));
536 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
-
539 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
544 list_for_each(curr
, &msgInfo
->SubMsgList
) {
546 /* FIXME: should this use list_entry() instead ? */
547 subMsgInfo
= (struct vmbus_channel_msginfo
*)curr
;
549 (struct vmbus_channel_gpadl_body
*)subMsgInfo
->Msg
;
551 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
552 gpadlBody
->Gpadl
= nextGpadlHandle
;
554 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd",
555 subMsgInfo
->MessageSize
-
556 sizeof(*subMsgInfo
));
558 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
-
559 sizeof(*subMsgInfo
));
560 ret
= VmbusPostMessage(gpadlBody
,
561 subMsgInfo
->MessageSize
-
562 sizeof(*subMsgInfo
));
568 osd_WaitEventWait(msgInfo
->WaitEvent
);
570 /* At this point, we received the gpadl created msg */
571 DPRINT_DBG(VMBUS
, "Received GPADL created "
572 "(relid %d, status %d handle %x)",
573 Channel
->OfferMsg
.ChildRelId
,
574 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
577 *GpadlHandle
= gpadlMsg
->Gpadl
;
580 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
581 list_del(&msgInfo
->MsgListEntry
);
582 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
584 kfree(msgInfo
->WaitEvent
);
593 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
595 int VmbusChannelTeardownGpadl(struct vmbus_channel
*Channel
, u32 GpadlHandle
)
597 struct vmbus_channel_gpadl_teardown
*msg
;
598 struct vmbus_channel_msginfo
*info
;
604 ASSERT(GpadlHandle
!= 0);
606 info
= kmalloc(sizeof(*info
) +
607 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
611 info
->WaitEvent
= osd_WaitEventCreate();
612 if (!info
->WaitEvent
) {
617 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->Msg
;
619 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
620 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
621 msg
->Gpadl
= GpadlHandle
;
623 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
624 list_add_tail(&info
->MsgListEntry
,
625 &gVmbusConnection
.ChannelMsgList
);
626 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
628 ret
= VmbusPostMessage(msg
,
629 sizeof(struct vmbus_channel_gpadl_teardown
));
635 osd_WaitEventWait(info
->WaitEvent
);
637 /* Received a torndown response */
638 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
639 list_del(&info
->MsgListEntry
);
640 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
642 kfree(info
->WaitEvent
);
651 * VmbusChannelClose - Close the specified channel
653 void VmbusChannelClose(struct vmbus_channel
*Channel
)
655 struct vmbus_channel_close_channel
*msg
;
656 struct vmbus_channel_msginfo
*info
;
662 /* Stop callback and cancel the timer asap */
663 Channel
->OnChannelCallback
= NULL
;
664 del_timer_sync(&Channel
->poll_timer
);
666 /* Send a closing message */
667 info
= kmalloc(sizeof(*info
) +
668 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
669 /* FIXME: can't do anything other than return here because the
670 * function is void */
674 /* info->waitEvent = osd_WaitEventCreate(); */
676 msg
= (struct vmbus_channel_close_channel
*)info
->Msg
;
677 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
678 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
680 ret
= VmbusPostMessage(msg
, sizeof(struct vmbus_channel_close_channel
));
686 /* Tear down the gpadl for the channel's ring buffer */
687 if (Channel
->RingBufferGpadlHandle
)
688 VmbusChannelTeardownGpadl(Channel
,
689 Channel
->RingBufferGpadlHandle
);
691 /* TODO: Send a msg to release the childRelId */
693 /* Cleanup the ring buffers for this channel */
694 RingBufferCleanup(&Channel
->Outbound
);
695 RingBufferCleanup(&Channel
->Inbound
);
697 osd_PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
702 * If we are closing the channel during an error path in
703 * opening the channel, don't free the channel since the
704 * caller will free the channel
707 if (Channel
->State
== CHANNEL_OPEN_STATE
) {
708 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
709 list_del(&Channel
->ListEntry
);
710 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
712 FreeVmbusChannel(Channel
);
719 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
720 * @Channel: Pointer to vmbus_channel structure.
721 * @Buffer: Pointer to the buffer you want to receive the data into.
722 * @BufferLen: Maximum size of what the the buffer will hold
723 * @RequestId: Identifier of the request
724 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
727 * Sends data in @Buffer directly to hyper-v via the vmbus
728 * This will send the data unparsed to hyper-v.
730 * Mainly used by Hyper-V drivers.
732 int VmbusChannelSendPacket(struct vmbus_channel
*Channel
, const void *Buffer
,
733 u32 BufferLen
, u64 RequestId
,
734 enum vmbus_packet_type Type
, u32 Flags
)
736 struct vmpacket_descriptor desc
;
737 u32 packetLen
= sizeof(struct vmpacket_descriptor
) + BufferLen
;
738 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
739 struct scatterlist bufferList
[3];
744 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d",
745 Channel
, Buffer
, BufferLen
);
747 DumpVmbusChannel(Channel
);
749 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
751 /* Setup the descriptor */
752 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
753 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
754 /* in 8-bytes granularity */
755 desc
.DataOffset8
= sizeof(struct vmpacket_descriptor
) >> 3;
756 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
757 desc
.TransactionId
= RequestId
;
759 sg_init_table(bufferList
, 3);
760 sg_set_buf(&bufferList
[0], &desc
, sizeof(struct vmpacket_descriptor
));
761 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
762 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
764 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
766 /* TODO: We should determine if this is optional */
767 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
768 VmbusChannelSetEvent(Channel
);
774 EXPORT_SYMBOL(VmbusChannelSendPacket
);
777 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
778 * packets using a GPADL Direct packet type.
780 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel
*Channel
,
781 struct hv_page_buffer PageBuffers
[],
782 u32 PageCount
, void *Buffer
, u32 BufferLen
,
787 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
790 u32 packetLenAligned
;
791 struct scatterlist bufferList
[3];
796 ASSERT(PageCount
<= MAX_PAGE_BUFFER_COUNT
);
798 DumpVmbusChannel(Channel
);
801 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
802 * largest size we support
804 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) -
805 ((MAX_PAGE_BUFFER_COUNT
- PageCount
) *
806 sizeof(struct hv_page_buffer
));
807 packetLen
= descSize
+ BufferLen
;
808 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
810 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
812 /* Setup the descriptor */
813 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
814 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
815 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
816 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
817 desc
.TransactionId
= RequestId
;
818 desc
.RangeCount
= PageCount
;
820 for (i
= 0; i
< PageCount
; i
++) {
821 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
822 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
823 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
826 sg_init_table(bufferList
, 3);
827 sg_set_buf(&bufferList
[0], &desc
, descSize
);
828 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
829 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
831 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
833 /* TODO: We should determine if this is optional */
834 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
835 VmbusChannelSetEvent(Channel
);
843 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
844 * using a GPADL Direct packet type.
846 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel
*Channel
,
847 struct hv_multipage_buffer
*MultiPageBuffer
,
848 void *Buffer
, u32 BufferLen
, u64 RequestId
)
851 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
854 u32 packetLenAligned
;
855 struct scatterlist bufferList
[3];
857 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
,
858 MultiPageBuffer
->Length
);
862 DumpVmbusChannel(Channel
);
864 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u",
865 MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
867 ASSERT(PfnCount
> 0);
868 ASSERT(PfnCount
<= MAX_MULTIPAGE_BUFFER_COUNT
);
871 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
872 * the largest size we support
874 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) -
875 ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
) *
877 packetLen
= descSize
+ BufferLen
;
878 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
880 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
882 /* Setup the descriptor */
883 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
884 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
885 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
886 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
887 desc
.TransactionId
= RequestId
;
890 desc
.Range
.Length
= MultiPageBuffer
->Length
;
891 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
893 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
,
894 PfnCount
* sizeof(u64
));
896 sg_init_table(bufferList
, 3);
897 sg_set_buf(&bufferList
[0], &desc
, descSize
);
898 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
899 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
901 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
903 /* TODO: We should determine if this is optional */
904 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
905 VmbusChannelSetEvent(Channel
);
914 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
915 * @Channel: Pointer to vmbus_channel structure.
916 * @Buffer: Pointer to the buffer you want to receive the data into.
917 * @BufferLen: Maximum size of what the the buffer will hold
918 * @BufferActualLen: The actual size of the data after it was received
919 * @RequestId: Identifier of the request
921 * Receives directly from the hyper-v vmbus and puts the data it received
922 * into Buffer. This will receive the data unparsed from hyper-v.
924 * Mainly used by Hyper-V drivers.
926 int VmbusChannelRecvPacket(struct vmbus_channel
*Channel
, void *Buffer
,
927 u32 BufferLen
, u32
*BufferActualLen
, u64
*RequestId
)
929 struct vmpacket_descriptor desc
;
937 *BufferActualLen
= 0;
940 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
942 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
943 sizeof(struct vmpacket_descriptor
));
945 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
947 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
952 /* VmbusChannelClearEvent(Channel); */
954 packetLen
= desc
.Length8
<< 3;
955 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
956 /* ASSERT(userLen > 0); */
958 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
959 "flag %d tid %llx pktlen %d datalen %d> ",
960 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
961 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
963 *BufferActualLen
= userLen
;
965 if (userLen
> BufferLen
) {
966 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
968 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d",
975 *RequestId
= desc
.TransactionId
;
977 /* Copy over the packet to the user buffer */
978 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
,
979 (desc
.DataOffset8
<< 3));
981 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
987 EXPORT_SYMBOL(VmbusChannelRecvPacket
);
990 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
992 int VmbusChannelRecvPacketRaw(struct vmbus_channel
*Channel
, void *Buffer
,
993 u32 BufferLen
, u32
*BufferActualLen
,
996 struct vmpacket_descriptor desc
;
1000 unsigned long flags
;
1002 DPRINT_ENTER(VMBUS
);
1004 *BufferActualLen
= 0;
1007 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
1009 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
1010 sizeof(struct vmpacket_descriptor
));
1012 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1014 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1019 /* VmbusChannelClearEvent(Channel); */
1021 packetLen
= desc
.Length8
<< 3;
1022 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
1024 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
1025 "flag %d tid %llx pktlen %d datalen %d> ",
1026 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
1027 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
1029 *BufferActualLen
= packetLen
;
1031 if (packetLen
> BufferLen
) {
1032 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1034 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but "
1035 "got space for only %d bytes", packetLen
, BufferLen
);
1040 *RequestId
= desc
.TransactionId
;
1042 /* Copy over the entire packet to the user buffer */
1043 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
1045 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1053 * VmbusChannelOnChannelEvent - Channel event callback
1055 void VmbusChannelOnChannelEvent(struct vmbus_channel
*Channel
)
1057 DumpVmbusChannel(Channel
);
1058 ASSERT(Channel
->OnChannelCallback
);
1060 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1062 mod_timer(&Channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
1066 * VmbusChannelOnTimer - Timer event callback
1068 void VmbusChannelOnTimer(unsigned long data
)
1070 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
1072 if (channel
->OnChannelCallback
)
1073 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1077 * DumpVmbusChannel - Dump vmbus channel info to the console
1079 static void DumpVmbusChannel(struct vmbus_channel
*Channel
)
1081 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1082 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1083 DumpRingInfo(&Channel
->Inbound
, "Inbound ");