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 "vmbus_private.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 ret
= RingBufferInit(&NewChannel
->Outbound
, out
, SendRingBufferSize
);
212 ret
= RingBufferInit(&NewChannel
->Inbound
, in
, RecvRingBufferSize
);
219 /* Establish the gpadl for the ring buffer */
220 DPRINT_DBG(VMBUS
, "Establishing ring buffer's gpadl for channel %p...",
223 NewChannel
->RingBufferGpadlHandle
= 0;
225 ret
= VmbusChannelEstablishGpadl(NewChannel
,
226 NewChannel
->Outbound
.RingBuffer
,
229 &NewChannel
->RingBufferGpadlHandle
);
236 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p "
237 "size %d recv ring %p size %d, downstreamoffset %d>",
238 NewChannel
, NewChannel
->OfferMsg
.ChildRelId
,
239 NewChannel
->RingBufferGpadlHandle
,
240 NewChannel
->Outbound
.RingBuffer
,
241 NewChannel
->Outbound
.RingSize
,
242 NewChannel
->Inbound
.RingBuffer
,
243 NewChannel
->Inbound
.RingSize
,
246 /* Create and init the channel open message */
247 openInfo
= kmalloc(sizeof(*openInfo
) +
248 sizeof(struct vmbus_channel_open_channel
),
255 openInfo
->WaitEvent
= osd_WaitEventCreate();
256 if (!openInfo
->WaitEvent
) {
261 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->Msg
;
262 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
263 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
264 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
265 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
266 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>>
268 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
270 if (UserDataLen
> MAX_USER_DEFINED_BYTES
) {
276 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
278 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
279 list_add_tail(&openInfo
->MsgListEntry
,
280 &gVmbusConnection
.ChannelMsgList
);
281 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
283 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
285 ret
= VmbusPostMessage(openMsg
,
286 sizeof(struct vmbus_channel_open_channel
));
288 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
292 /* FIXME: Need to time-out here */
293 osd_WaitEventWait(openInfo
->WaitEvent
);
295 if (openInfo
->Response
.OpenResult
.Status
== 0)
296 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
298 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!",
299 NewChannel
, openInfo
->Response
.OpenResult
.Status
);
302 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
303 list_del(&openInfo
->MsgListEntry
);
304 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
306 kfree(openInfo
->WaitEvent
);
314 RingBufferCleanup(&NewChannel
->Outbound
);
315 RingBufferCleanup(&NewChannel
->Inbound
);
316 osd_PageFree(out
, (SendRingBufferSize
+ RecvRingBufferSize
)
323 * DumpGpadlBody - Dump the gpadl body message to the console for
324 * debugging purposes.
326 static void DumpGpadlBody(struct vmbus_channel_gpadl_body
*Gpadl
, u32 Len
)
331 pfnCount
= (Len
- sizeof(struct vmbus_channel_gpadl_body
)) /
333 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
335 for (i
= 0; i
< pfnCount
; i
++)
336 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
341 * DumpGpadlHeader - Dump the gpadl header message to the console for
342 * debugging purposes.
344 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header
*Gpadl
)
350 "gpadl header - relid %d, range count %d, range buflen %d",
351 Gpadl
->ChildRelId
, Gpadl
->RangeCount
, Gpadl
->RangeBufLen
);
352 for (i
= 0; i
< Gpadl
->RangeCount
; i
++) {
353 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
354 pageCount
= (pageCount
> 26) ? 26 : pageCount
;
356 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
357 "page count %d", i
, Gpadl
->Range
[i
].ByteCount
,
358 Gpadl
->Range
[i
].ByteOffset
, pageCount
);
360 for (j
= 0; j
< pageCount
; j
++)
361 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
362 Gpadl
->Range
[i
].PfnArray
[j
]);
367 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
369 static int VmbusChannelCreateGpadlHeader(void *Kbuffer
, u32 Size
,
370 struct vmbus_channel_msginfo
**MsgInfo
,
375 unsigned long long pfn
;
376 struct vmbus_channel_gpadl_header
*gpaHeader
;
377 struct vmbus_channel_gpadl_body
*gpadlBody
;
378 struct vmbus_channel_msginfo
*msgHeader
;
379 struct vmbus_channel_msginfo
*msgBody
= NULL
;
382 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
384 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
385 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
387 pageCount
= Size
>> PAGE_SHIFT
;
388 pfn
= virt_to_phys(Kbuffer
) >> PAGE_SHIFT
;
390 /* do we need a gpadl body msg */
391 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
392 sizeof(struct vmbus_channel_gpadl_header
) -
393 sizeof(struct gpa_range
);
394 pfnCount
= pfnSize
/ sizeof(u64
);
396 if (pageCount
> pfnCount
) {
397 /* we need a gpadl body */
398 /* fill in the header */
399 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
400 sizeof(struct vmbus_channel_gpadl_header
) +
401 sizeof(struct gpa_range
) + pfnCount
* sizeof(u64
);
402 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
406 INIT_LIST_HEAD(&msgHeader
->SubMsgList
);
407 msgHeader
->MessageSize
= msgSize
;
409 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
410 gpaHeader
->RangeCount
= 1;
411 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
412 pageCount
* sizeof(u64
);
413 gpaHeader
->Range
[0].ByteOffset
= 0;
414 gpaHeader
->Range
[0].ByteCount
= Size
;
415 for (i
= 0; i
< pfnCount
; i
++)
416 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
417 *MsgInfo
= msgHeader
;
421 pfnLeft
= pageCount
- pfnCount
;
423 /* how many pfns can we fit */
424 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
425 sizeof(struct vmbus_channel_gpadl_body
);
426 pfnCount
= pfnSize
/ sizeof(u64
);
428 /* fill in the body */
430 if (pfnLeft
> pfnCount
)
435 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
436 sizeof(struct vmbus_channel_gpadl_body
) +
437 pfnCurr
* sizeof(u64
);
438 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
439 /* FIXME: we probably need to more if this fails */
442 msgBody
->MessageSize
= msgSize
;
445 (struct vmbus_channel_gpadl_body
*)msgBody
->Msg
;
449 * Gpadl is u32 and we are using a pointer which could
452 /* gpadlBody->Gpadl = kbuffer; */
453 for (i
= 0; i
< pfnCurr
; i
++)
454 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
456 /* add to msg header */
457 list_add_tail(&msgBody
->MsgListEntry
,
458 &msgHeader
->SubMsgList
);
463 /* everything fits in a header */
464 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
465 sizeof(struct vmbus_channel_gpadl_header
) +
466 sizeof(struct gpa_range
) + pageCount
* sizeof(u64
);
467 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
468 msgHeader
->MessageSize
= msgSize
;
470 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
471 gpaHeader
->RangeCount
= 1;
472 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
473 pageCount
* sizeof(u64
);
474 gpaHeader
->Range
[0].ByteOffset
= 0;
475 gpaHeader
->Range
[0].ByteCount
= Size
;
476 for (i
= 0; i
< pageCount
; i
++)
477 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
479 *MsgInfo
= msgHeader
;
491 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
493 * @Channel: a channel
494 * @Kbuffer: from kmalloc
495 * @Size: page-size multiple
496 * @GpadlHandle: some funky thing
498 int VmbusChannelEstablishGpadl(struct vmbus_channel
*Channel
, void *Kbuffer
,
499 u32 Size
, u32
*GpadlHandle
)
501 struct vmbus_channel_gpadl_header
*gpadlMsg
;
502 struct vmbus_channel_gpadl_body
*gpadlBody
;
503 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
504 struct vmbus_channel_msginfo
*msgInfo
= NULL
;
505 struct vmbus_channel_msginfo
*subMsgInfo
;
507 struct list_head
*curr
;
514 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
515 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
517 ret
= VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
521 msgInfo
->WaitEvent
= osd_WaitEventCreate();
522 if (!msgInfo
->WaitEvent
) {
527 gpadlMsg
= (struct vmbus_channel_gpadl_header
*)msgInfo
->Msg
;
528 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
529 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
530 gpadlMsg
->Gpadl
= nextGpadlHandle
;
532 DumpGpadlHeader(gpadlMsg
);
534 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
535 list_add_tail(&msgInfo
->MsgListEntry
,
536 &gVmbusConnection
.ChannelMsgList
);
538 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
539 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d",
540 Kbuffer
, Size
, msgCount
);
542 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd",
543 msgInfo
->MessageSize
- sizeof(*msgInfo
));
545 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
-
548 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
553 list_for_each(curr
, &msgInfo
->SubMsgList
) {
555 /* FIXME: should this use list_entry() instead ? */
556 subMsgInfo
= (struct vmbus_channel_msginfo
*)curr
;
558 (struct vmbus_channel_gpadl_body
*)subMsgInfo
->Msg
;
560 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
561 gpadlBody
->Gpadl
= nextGpadlHandle
;
563 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd",
564 subMsgInfo
->MessageSize
-
565 sizeof(*subMsgInfo
));
567 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
-
568 sizeof(*subMsgInfo
));
569 ret
= VmbusPostMessage(gpadlBody
,
570 subMsgInfo
->MessageSize
-
571 sizeof(*subMsgInfo
));
577 osd_WaitEventWait(msgInfo
->WaitEvent
);
579 /* At this point, we received the gpadl created msg */
580 DPRINT_DBG(VMBUS
, "Received GPADL created "
581 "(relid %d, status %d handle %x)",
582 Channel
->OfferMsg
.ChildRelId
,
583 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
586 *GpadlHandle
= gpadlMsg
->Gpadl
;
589 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
590 list_del(&msgInfo
->MsgListEntry
);
591 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
593 kfree(msgInfo
->WaitEvent
);
602 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
604 int VmbusChannelTeardownGpadl(struct vmbus_channel
*Channel
, u32 GpadlHandle
)
606 struct vmbus_channel_gpadl_teardown
*msg
;
607 struct vmbus_channel_msginfo
*info
;
613 /* ASSERT(GpadlHandle != 0); */
615 info
= kmalloc(sizeof(*info
) +
616 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
620 info
->WaitEvent
= osd_WaitEventCreate();
621 if (!info
->WaitEvent
) {
626 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->Msg
;
628 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
629 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
630 msg
->Gpadl
= GpadlHandle
;
632 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
633 list_add_tail(&info
->MsgListEntry
,
634 &gVmbusConnection
.ChannelMsgList
);
635 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
637 ret
= VmbusPostMessage(msg
,
638 sizeof(struct vmbus_channel_gpadl_teardown
));
644 osd_WaitEventWait(info
->WaitEvent
);
646 /* Received a torndown response */
647 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
648 list_del(&info
->MsgListEntry
);
649 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
651 kfree(info
->WaitEvent
);
660 * VmbusChannelClose - Close the specified channel
662 void VmbusChannelClose(struct vmbus_channel
*Channel
)
664 struct vmbus_channel_close_channel
*msg
;
665 struct vmbus_channel_msginfo
*info
;
671 /* Stop callback and cancel the timer asap */
672 Channel
->OnChannelCallback
= NULL
;
673 del_timer_sync(&Channel
->poll_timer
);
675 /* Send a closing message */
676 info
= kmalloc(sizeof(*info
) +
677 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
678 /* FIXME: can't do anything other than return here because the
679 * function is void */
683 /* info->waitEvent = osd_WaitEventCreate(); */
685 msg
= (struct vmbus_channel_close_channel
*)info
->Msg
;
686 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
687 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
689 ret
= VmbusPostMessage(msg
, sizeof(struct vmbus_channel_close_channel
));
695 /* Tear down the gpadl for the channel's ring buffer */
696 if (Channel
->RingBufferGpadlHandle
)
697 VmbusChannelTeardownGpadl(Channel
,
698 Channel
->RingBufferGpadlHandle
);
700 /* TODO: Send a msg to release the childRelId */
702 /* Cleanup the ring buffers for this channel */
703 RingBufferCleanup(&Channel
->Outbound
);
704 RingBufferCleanup(&Channel
->Inbound
);
706 osd_PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
711 * If we are closing the channel during an error path in
712 * opening the channel, don't free the channel since the
713 * caller will free the channel
716 if (Channel
->State
== CHANNEL_OPEN_STATE
) {
717 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
718 list_del(&Channel
->ListEntry
);
719 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
721 FreeVmbusChannel(Channel
);
728 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
729 * @Channel: Pointer to vmbus_channel structure.
730 * @Buffer: Pointer to the buffer you want to receive the data into.
731 * @BufferLen: Maximum size of what the the buffer will hold
732 * @RequestId: Identifier of the request
733 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
736 * Sends data in @Buffer directly to hyper-v via the vmbus
737 * This will send the data unparsed to hyper-v.
739 * Mainly used by Hyper-V drivers.
741 int VmbusChannelSendPacket(struct vmbus_channel
*Channel
, const void *Buffer
,
742 u32 BufferLen
, u64 RequestId
,
743 enum vmbus_packet_type Type
, u32 Flags
)
745 struct vmpacket_descriptor desc
;
746 u32 packetLen
= sizeof(struct vmpacket_descriptor
) + BufferLen
;
747 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
748 struct scatterlist bufferList
[3];
753 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d",
754 Channel
, Buffer
, BufferLen
);
756 DumpVmbusChannel(Channel
);
758 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
760 /* Setup the descriptor */
761 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
762 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
763 /* in 8-bytes granularity */
764 desc
.DataOffset8
= sizeof(struct vmpacket_descriptor
) >> 3;
765 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
766 desc
.TransactionId
= RequestId
;
768 sg_init_table(bufferList
, 3);
769 sg_set_buf(&bufferList
[0], &desc
, sizeof(struct vmpacket_descriptor
));
770 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
771 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
773 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
775 /* TODO: We should determine if this is optional */
776 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
777 VmbusChannelSetEvent(Channel
);
783 EXPORT_SYMBOL(VmbusChannelSendPacket
);
786 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
787 * packets using a GPADL Direct packet type.
789 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel
*Channel
,
790 struct hv_page_buffer PageBuffers
[],
791 u32 PageCount
, void *Buffer
, u32 BufferLen
,
796 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
799 u32 packetLenAligned
;
800 struct scatterlist bufferList
[3];
805 if (PageCount
> MAX_PAGE_BUFFER_COUNT
)
808 DumpVmbusChannel(Channel
);
811 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
812 * largest size we support
814 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) -
815 ((MAX_PAGE_BUFFER_COUNT
- PageCount
) *
816 sizeof(struct hv_page_buffer
));
817 packetLen
= descSize
+ BufferLen
;
818 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
820 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
822 /* Setup the descriptor */
823 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
824 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
825 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
826 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
827 desc
.TransactionId
= RequestId
;
828 desc
.RangeCount
= PageCount
;
830 for (i
= 0; i
< PageCount
; i
++) {
831 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
832 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
833 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
836 sg_init_table(bufferList
, 3);
837 sg_set_buf(&bufferList
[0], &desc
, descSize
);
838 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
839 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
841 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
843 /* TODO: We should determine if this is optional */
844 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
845 VmbusChannelSetEvent(Channel
);
853 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
854 * using a GPADL Direct packet type.
856 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel
*Channel
,
857 struct hv_multipage_buffer
*MultiPageBuffer
,
858 void *Buffer
, u32 BufferLen
, u64 RequestId
)
861 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
864 u32 packetLenAligned
;
865 struct scatterlist bufferList
[3];
867 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
,
868 MultiPageBuffer
->Length
);
872 DumpVmbusChannel(Channel
);
874 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u",
875 MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
877 if ((PfnCount
< 0) || (PfnCount
> MAX_MULTIPAGE_BUFFER_COUNT
))
881 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
882 * the largest size we support
884 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) -
885 ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
) *
887 packetLen
= descSize
+ BufferLen
;
888 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
890 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
892 /* Setup the descriptor */
893 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
894 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
895 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
896 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
897 desc
.TransactionId
= RequestId
;
900 desc
.Range
.Length
= MultiPageBuffer
->Length
;
901 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
903 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
,
904 PfnCount
* sizeof(u64
));
906 sg_init_table(bufferList
, 3);
907 sg_set_buf(&bufferList
[0], &desc
, descSize
);
908 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
909 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
911 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
913 /* TODO: We should determine if this is optional */
914 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
915 VmbusChannelSetEvent(Channel
);
924 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
925 * @Channel: Pointer to vmbus_channel structure.
926 * @Buffer: Pointer to the buffer you want to receive the data into.
927 * @BufferLen: Maximum size of what the the buffer will hold
928 * @BufferActualLen: The actual size of the data after it was received
929 * @RequestId: Identifier of the request
931 * Receives directly from the hyper-v vmbus and puts the data it received
932 * into Buffer. This will receive the data unparsed from hyper-v.
934 * Mainly used by Hyper-V drivers.
936 int VmbusChannelRecvPacket(struct vmbus_channel
*Channel
, void *Buffer
,
937 u32 BufferLen
, u32
*BufferActualLen
, u64
*RequestId
)
939 struct vmpacket_descriptor desc
;
947 *BufferActualLen
= 0;
950 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
952 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
953 sizeof(struct vmpacket_descriptor
));
955 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
957 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
962 /* VmbusChannelClearEvent(Channel); */
964 packetLen
= desc
.Length8
<< 3;
965 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
966 /* ASSERT(userLen > 0); */
968 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
969 "flag %d tid %llx pktlen %d datalen %d> ",
970 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
971 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
973 *BufferActualLen
= userLen
;
975 if (userLen
> BufferLen
) {
976 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
978 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d",
985 *RequestId
= desc
.TransactionId
;
987 /* Copy over the packet to the user buffer */
988 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
,
989 (desc
.DataOffset8
<< 3));
991 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
997 EXPORT_SYMBOL(VmbusChannelRecvPacket
);
1000 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
1002 int VmbusChannelRecvPacketRaw(struct vmbus_channel
*Channel
, void *Buffer
,
1003 u32 BufferLen
, u32
*BufferActualLen
,
1006 struct vmpacket_descriptor desc
;
1010 unsigned long flags
;
1012 DPRINT_ENTER(VMBUS
);
1014 *BufferActualLen
= 0;
1017 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
1019 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
1020 sizeof(struct vmpacket_descriptor
));
1022 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1024 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1029 /* VmbusChannelClearEvent(Channel); */
1031 packetLen
= desc
.Length8
<< 3;
1032 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
1034 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
1035 "flag %d tid %llx pktlen %d datalen %d> ",
1036 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
1037 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
1039 *BufferActualLen
= packetLen
;
1041 if (packetLen
> BufferLen
) {
1042 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1044 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but "
1045 "got space for only %d bytes", packetLen
, BufferLen
);
1050 *RequestId
= desc
.TransactionId
;
1052 /* Copy over the entire packet to the user buffer */
1053 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
1055 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1063 * VmbusChannelOnChannelEvent - Channel event callback
1065 void VmbusChannelOnChannelEvent(struct vmbus_channel
*Channel
)
1067 DumpVmbusChannel(Channel
);
1068 /* ASSERT(Channel->OnChannelCallback); */
1070 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1072 mod_timer(&Channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
1076 * VmbusChannelOnTimer - Timer event callback
1078 void VmbusChannelOnTimer(unsigned long data
)
1080 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
1082 if (channel
->OnChannelCallback
)
1083 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1087 * DumpVmbusChannel - Dump vmbus channel info to the console
1089 static void DumpVmbusChannel(struct vmbus_channel
*Channel
)
1091 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1092 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1093 DumpRingInfo(&Channel
->Inbound
, "Inbound ");