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
;
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
);
221 /* FIXME: the value of ret is not checked */
223 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p "
224 "size %d recv ring %p size %d, downstreamoffset %d>",
225 NewChannel
, NewChannel
->OfferMsg
.ChildRelId
,
226 NewChannel
->RingBufferGpadlHandle
,
227 NewChannel
->Outbound
.RingBuffer
,
228 NewChannel
->Outbound
.RingSize
,
229 NewChannel
->Inbound
.RingBuffer
,
230 NewChannel
->Inbound
.RingSize
,
233 /* Create and init the channel open message */
234 openInfo
= kmalloc(sizeof(*openInfo
) +
235 sizeof(struct vmbus_channel_open_channel
),
242 openInfo
->WaitEvent
= osd_WaitEventCreate();
243 if (!openInfo
->WaitEvent
) {
248 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->Msg
;
249 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
250 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
251 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
252 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
253 ASSERT(openMsg
->RingBufferGpadlHandle
);
254 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>>
256 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
258 ASSERT(UserDataLen
<= MAX_USER_DEFINED_BYTES
);
260 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
262 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
263 list_add_tail(&openInfo
->MsgListEntry
,
264 &gVmbusConnection
.ChannelMsgList
);
265 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
267 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
269 ret
= VmbusPostMessage(openMsg
,
270 sizeof(struct vmbus_channel_open_channel
));
272 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
276 /* FIXME: Need to time-out here */
277 osd_WaitEventWait(openInfo
->WaitEvent
);
279 if (openInfo
->Response
.OpenResult
.Status
== 0)
280 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
282 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!",
283 NewChannel
, openInfo
->Response
.OpenResult
.Status
);
286 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
287 list_del(&openInfo
->MsgListEntry
);
288 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
290 kfree(openInfo
->WaitEvent
);
298 osd_PageFree(out
, (SendRingBufferSize
+ RecvRingBufferSize
)
305 * DumpGpadlBody - Dump the gpadl body message to the console for
306 * debugging purposes.
308 static void DumpGpadlBody(struct vmbus_channel_gpadl_body
*Gpadl
, u32 Len
)
313 pfnCount
= (Len
- sizeof(struct vmbus_channel_gpadl_body
)) /
315 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
317 for (i
= 0; i
< pfnCount
; i
++)
318 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
323 * DumpGpadlHeader - Dump the gpadl header message to the console for
324 * debugging purposes.
326 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header
*Gpadl
)
332 "gpadl header - relid %d, range count %d, range buflen %d",
333 Gpadl
->ChildRelId
, Gpadl
->RangeCount
, Gpadl
->RangeBufLen
);
334 for (i
= 0; i
< Gpadl
->RangeCount
; i
++) {
335 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
336 pageCount
= (pageCount
> 26) ? 26 : pageCount
;
338 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
339 "page count %d", i
, Gpadl
->Range
[i
].ByteCount
,
340 Gpadl
->Range
[i
].ByteOffset
, pageCount
);
342 for (j
= 0; j
< pageCount
; j
++)
343 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
344 Gpadl
->Range
[i
].PfnArray
[j
]);
349 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
351 static int VmbusChannelCreateGpadlHeader(void *Kbuffer
, u32 Size
,
352 struct vmbus_channel_msginfo
**MsgInfo
,
357 unsigned long long pfn
;
358 struct vmbus_channel_gpadl_header
*gpaHeader
;
359 struct vmbus_channel_gpadl_body
*gpadlBody
;
360 struct vmbus_channel_msginfo
*msgHeader
;
361 struct vmbus_channel_msginfo
*msgBody
;
364 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
366 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
367 ASSERT((Size
& (PAGE_SIZE
-1)) == 0);
369 pageCount
= Size
>> PAGE_SHIFT
;
370 pfn
= virt_to_phys(Kbuffer
) >> PAGE_SHIFT
;
372 /* do we need a gpadl body msg */
373 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
374 sizeof(struct vmbus_channel_gpadl_header
) -
375 sizeof(struct gpa_range
);
376 pfnCount
= pfnSize
/ sizeof(u64
);
378 if (pageCount
> pfnCount
) {
379 /* we need a gpadl body */
380 /* fill in the header */
381 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
382 sizeof(struct vmbus_channel_gpadl_header
) +
383 sizeof(struct gpa_range
) + pfnCount
* sizeof(u64
);
384 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
388 INIT_LIST_HEAD(&msgHeader
->SubMsgList
);
389 msgHeader
->MessageSize
= msgSize
;
391 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
392 gpaHeader
->RangeCount
= 1;
393 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
394 pageCount
* sizeof(u64
);
395 gpaHeader
->Range
[0].ByteOffset
= 0;
396 gpaHeader
->Range
[0].ByteCount
= Size
;
397 for (i
= 0; i
< pfnCount
; i
++)
398 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
399 *MsgInfo
= msgHeader
;
403 pfnLeft
= pageCount
- pfnCount
;
405 /* how many pfns can we fit */
406 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
407 sizeof(struct vmbus_channel_gpadl_body
);
408 pfnCount
= pfnSize
/ sizeof(u64
);
410 /* fill in the body */
412 if (pfnLeft
> pfnCount
)
417 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
418 sizeof(struct vmbus_channel_gpadl_body
) +
419 pfnCurr
* sizeof(u64
);
420 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
421 /* FIXME: we probably need to more if this fails */
424 msgBody
->MessageSize
= msgSize
;
427 (struct vmbus_channel_gpadl_body
*)msgBody
->Msg
;
431 * Gpadl is u32 and we are using a pointer which could
434 /* gpadlBody->Gpadl = kbuffer; */
435 for (i
= 0; i
< pfnCurr
; i
++)
436 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
438 /* add to msg header */
439 list_add_tail(&msgBody
->MsgListEntry
,
440 &msgHeader
->SubMsgList
);
445 /* everything fits in a header */
446 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
447 sizeof(struct vmbus_channel_gpadl_header
) +
448 sizeof(struct gpa_range
) + pageCount
* sizeof(u64
);
449 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
450 msgHeader
->MessageSize
= msgSize
;
452 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
453 gpaHeader
->RangeCount
= 1;
454 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
455 pageCount
* sizeof(u64
);
456 gpaHeader
->Range
[0].ByteOffset
= 0;
457 gpaHeader
->Range
[0].ByteCount
= Size
;
458 for (i
= 0; i
< pageCount
; i
++)
459 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
461 *MsgInfo
= msgHeader
;
473 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
475 * @Channel: a channel
476 * @Kbuffer: from kmalloc
477 * @Size: page-size multiple
478 * @GpadlHandle: some funky thing
480 int VmbusChannelEstablishGpadl(struct vmbus_channel
*Channel
, void *Kbuffer
,
481 u32 Size
, u32
*GpadlHandle
)
483 struct vmbus_channel_gpadl_header
*gpadlMsg
;
484 struct vmbus_channel_gpadl_body
*gpadlBody
;
485 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
486 struct vmbus_channel_msginfo
*msgInfo
= NULL
;
487 struct vmbus_channel_msginfo
*subMsgInfo
;
489 struct list_head
*curr
;
496 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
497 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
499 ret
= VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
503 msgInfo
->WaitEvent
= osd_WaitEventCreate();
504 if (!msgInfo
->WaitEvent
) {
509 gpadlMsg
= (struct vmbus_channel_gpadl_header
*)msgInfo
->Msg
;
510 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
511 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
512 gpadlMsg
->Gpadl
= nextGpadlHandle
;
514 DumpGpadlHeader(gpadlMsg
);
516 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
517 list_add_tail(&msgInfo
->MsgListEntry
,
518 &gVmbusConnection
.ChannelMsgList
);
520 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
521 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d",
522 Kbuffer
, Size
, msgCount
);
524 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd",
525 msgInfo
->MessageSize
- sizeof(*msgInfo
));
527 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
-
530 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
535 list_for_each(curr
, &msgInfo
->SubMsgList
) {
537 /* FIXME: should this use list_entry() instead ? */
538 subMsgInfo
= (struct vmbus_channel_msginfo
*)curr
;
540 (struct vmbus_channel_gpadl_body
*)subMsgInfo
->Msg
;
542 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
543 gpadlBody
->Gpadl
= nextGpadlHandle
;
545 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd",
546 subMsgInfo
->MessageSize
-
547 sizeof(*subMsgInfo
));
549 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
-
550 sizeof(*subMsgInfo
));
551 ret
= VmbusPostMessage(gpadlBody
,
552 subMsgInfo
->MessageSize
-
553 sizeof(*subMsgInfo
));
557 osd_WaitEventWait(msgInfo
->WaitEvent
);
559 /* At this point, we received the gpadl created msg */
560 DPRINT_DBG(VMBUS
, "Received GPADL created "
561 "(relid %d, status %d handle %x)",
562 Channel
->OfferMsg
.ChildRelId
,
563 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
566 *GpadlHandle
= gpadlMsg
->Gpadl
;
569 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
570 list_del(&msgInfo
->MsgListEntry
);
571 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
573 kfree(msgInfo
->WaitEvent
);
582 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
584 int VmbusChannelTeardownGpadl(struct vmbus_channel
*Channel
, u32 GpadlHandle
)
586 struct vmbus_channel_gpadl_teardown
*msg
;
587 struct vmbus_channel_msginfo
*info
;
593 ASSERT(GpadlHandle
!= 0);
595 info
= kmalloc(sizeof(*info
) +
596 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
600 info
->WaitEvent
= osd_WaitEventCreate();
601 if (!info
->WaitEvent
) {
606 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->Msg
;
608 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
609 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
610 msg
->Gpadl
= GpadlHandle
;
612 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
613 list_add_tail(&info
->MsgListEntry
,
614 &gVmbusConnection
.ChannelMsgList
);
615 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
617 ret
= VmbusPostMessage(msg
,
618 sizeof(struct vmbus_channel_gpadl_teardown
));
624 osd_WaitEventWait(info
->WaitEvent
);
626 /* Received a torndown response */
627 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
628 list_del(&info
->MsgListEntry
);
629 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
631 kfree(info
->WaitEvent
);
640 * VmbusChannelClose - Close the specified channel
642 void VmbusChannelClose(struct vmbus_channel
*Channel
)
644 struct vmbus_channel_close_channel
*msg
;
645 struct vmbus_channel_msginfo
*info
;
651 /* Stop callback and cancel the timer asap */
652 Channel
->OnChannelCallback
= NULL
;
653 del_timer_sync(&Channel
->poll_timer
);
655 /* Send a closing message */
656 info
= kmalloc(sizeof(*info
) +
657 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
658 /* FIXME: can't do anything other than return here because the
659 * function is void */
663 /* info->waitEvent = osd_WaitEventCreate(); */
665 msg
= (struct vmbus_channel_close_channel
*)info
->Msg
;
666 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
667 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
669 ret
= VmbusPostMessage(msg
, sizeof(struct vmbus_channel_close_channel
));
675 /* Tear down the gpadl for the channel's ring buffer */
676 if (Channel
->RingBufferGpadlHandle
)
677 VmbusChannelTeardownGpadl(Channel
,
678 Channel
->RingBufferGpadlHandle
);
680 /* TODO: Send a msg to release the childRelId */
682 /* Cleanup the ring buffers for this channel */
683 RingBufferCleanup(&Channel
->Outbound
);
684 RingBufferCleanup(&Channel
->Inbound
);
686 osd_PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
691 * If we are closing the channel during an error path in
692 * opening the channel, don't free the channel since the
693 * caller will free the channel
696 if (Channel
->State
== CHANNEL_OPEN_STATE
) {
697 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
698 list_del(&Channel
->ListEntry
);
699 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
701 FreeVmbusChannel(Channel
);
708 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
709 * @Channel: Pointer to vmbus_channel structure.
710 * @Buffer: Pointer to the buffer you want to receive the data into.
711 * @BufferLen: Maximum size of what the the buffer will hold
712 * @RequestId: Identifier of the request
713 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
716 * Sends data in @Buffer directly to hyper-v via the vmbus
717 * This will send the data unparsed to hyper-v.
719 * Mainly used by Hyper-V drivers.
721 int VmbusChannelSendPacket(struct vmbus_channel
*Channel
, const void *Buffer
,
722 u32 BufferLen
, u64 RequestId
,
723 enum vmbus_packet_type Type
, u32 Flags
)
725 struct vmpacket_descriptor desc
;
726 u32 packetLen
= sizeof(struct vmpacket_descriptor
) + BufferLen
;
727 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
728 struct scatterlist bufferList
[3];
733 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d",
734 Channel
, Buffer
, BufferLen
);
736 DumpVmbusChannel(Channel
);
738 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
740 /* Setup the descriptor */
741 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
742 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
743 /* in 8-bytes granularity */
744 desc
.DataOffset8
= sizeof(struct vmpacket_descriptor
) >> 3;
745 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
746 desc
.TransactionId
= RequestId
;
748 sg_init_table(bufferList
, 3);
749 sg_set_buf(&bufferList
[0], &desc
, sizeof(struct vmpacket_descriptor
));
750 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
751 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
753 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
755 /* TODO: We should determine if this is optional */
756 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
757 VmbusChannelSetEvent(Channel
);
763 EXPORT_SYMBOL(VmbusChannelSendPacket
);
766 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
767 * packets using a GPADL Direct packet type.
769 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel
*Channel
,
770 struct hv_page_buffer PageBuffers
[],
771 u32 PageCount
, void *Buffer
, u32 BufferLen
,
776 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
779 u32 packetLenAligned
;
780 struct scatterlist bufferList
[3];
785 ASSERT(PageCount
<= MAX_PAGE_BUFFER_COUNT
);
787 DumpVmbusChannel(Channel
);
790 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
791 * largest size we support
793 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) -
794 ((MAX_PAGE_BUFFER_COUNT
- PageCount
) *
795 sizeof(struct hv_page_buffer
));
796 packetLen
= descSize
+ BufferLen
;
797 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
799 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
801 /* Setup the descriptor */
802 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
803 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
804 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
805 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
806 desc
.TransactionId
= RequestId
;
807 desc
.RangeCount
= PageCount
;
809 for (i
= 0; i
< PageCount
; i
++) {
810 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
811 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
812 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
815 sg_init_table(bufferList
, 3);
816 sg_set_buf(&bufferList
[0], &desc
, descSize
);
817 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
818 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
820 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
822 /* TODO: We should determine if this is optional */
823 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
824 VmbusChannelSetEvent(Channel
);
832 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
833 * using a GPADL Direct packet type.
835 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel
*Channel
,
836 struct hv_multipage_buffer
*MultiPageBuffer
,
837 void *Buffer
, u32 BufferLen
, u64 RequestId
)
840 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
843 u32 packetLenAligned
;
844 struct scatterlist bufferList
[3];
846 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
,
847 MultiPageBuffer
->Length
);
851 DumpVmbusChannel(Channel
);
853 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u",
854 MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
856 ASSERT(PfnCount
> 0);
857 ASSERT(PfnCount
<= MAX_MULTIPAGE_BUFFER_COUNT
);
860 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
861 * the largest size we support
863 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) -
864 ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
) *
866 packetLen
= descSize
+ BufferLen
;
867 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
869 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
871 /* Setup the descriptor */
872 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
873 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
874 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
875 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
876 desc
.TransactionId
= RequestId
;
879 desc
.Range
.Length
= MultiPageBuffer
->Length
;
880 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
882 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
,
883 PfnCount
* sizeof(u64
));
885 sg_init_table(bufferList
, 3);
886 sg_set_buf(&bufferList
[0], &desc
, descSize
);
887 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
888 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
890 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
892 /* TODO: We should determine if this is optional */
893 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
894 VmbusChannelSetEvent(Channel
);
903 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
904 * @Channel: Pointer to vmbus_channel structure.
905 * @Buffer: Pointer to the buffer you want to receive the data into.
906 * @BufferLen: Maximum size of what the the buffer will hold
907 * @BufferActualLen: The actual size of the data after it was received
908 * @RequestId: Identifier of the request
910 * Receives directly from the hyper-v vmbus and puts the data it received
911 * into Buffer. This will receive the data unparsed from hyper-v.
913 * Mainly used by Hyper-V drivers.
915 int VmbusChannelRecvPacket(struct vmbus_channel
*Channel
, void *Buffer
,
916 u32 BufferLen
, u32
*BufferActualLen
, u64
*RequestId
)
918 struct vmpacket_descriptor desc
;
926 *BufferActualLen
= 0;
929 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
931 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
932 sizeof(struct vmpacket_descriptor
));
934 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
936 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
941 /* VmbusChannelClearEvent(Channel); */
943 packetLen
= desc
.Length8
<< 3;
944 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
945 /* ASSERT(userLen > 0); */
947 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
948 "flag %d tid %llx pktlen %d datalen %d> ",
949 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
950 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
952 *BufferActualLen
= userLen
;
954 if (userLen
> BufferLen
) {
955 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
957 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d",
964 *RequestId
= desc
.TransactionId
;
966 /* Copy over the packet to the user buffer */
967 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
,
968 (desc
.DataOffset8
<< 3));
970 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
976 EXPORT_SYMBOL(VmbusChannelRecvPacket
);
979 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
981 int VmbusChannelRecvPacketRaw(struct vmbus_channel
*Channel
, void *Buffer
,
982 u32 BufferLen
, u32
*BufferActualLen
,
985 struct vmpacket_descriptor desc
;
993 *BufferActualLen
= 0;
996 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
998 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
999 sizeof(struct vmpacket_descriptor
));
1001 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1003 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1008 /* VmbusChannelClearEvent(Channel); */
1010 packetLen
= desc
.Length8
<< 3;
1011 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
1013 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
1014 "flag %d tid %llx pktlen %d datalen %d> ",
1015 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
1016 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
1018 *BufferActualLen
= packetLen
;
1020 if (packetLen
> BufferLen
) {
1021 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1023 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but "
1024 "got space for only %d bytes", packetLen
, BufferLen
);
1029 *RequestId
= desc
.TransactionId
;
1031 /* Copy over the entire packet to the user buffer */
1032 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
1034 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1042 * VmbusChannelOnChannelEvent - Channel event callback
1044 void VmbusChannelOnChannelEvent(struct vmbus_channel
*Channel
)
1046 DumpVmbusChannel(Channel
);
1047 ASSERT(Channel
->OnChannelCallback
);
1049 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1051 mod_timer(&Channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
1055 * VmbusChannelOnTimer - Timer event callback
1057 void VmbusChannelOnTimer(unsigned long data
)
1059 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
1061 if (channel
->OnChannelCallback
)
1062 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1066 * DumpVmbusChannel - Dump vmbus channel info to the console
1068 static void DumpVmbusChannel(struct vmbus_channel
*Channel
)
1070 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1071 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1072 DumpRingInfo(&Channel
->Inbound
, "Inbound ");