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
)
196 ASSERT(((unsigned long)out
& (PAGE_SIZE
-1)) == 0);
198 in
= (void *)((unsigned long)out
+ SendRingBufferSize
);
200 NewChannel
->RingBufferPages
= out
;
201 NewChannel
->RingBufferPageCount
= (SendRingBufferSize
+
202 RecvRingBufferSize
) >> PAGE_SHIFT
;
204 RingBufferInit(&NewChannel
->Outbound
, out
, SendRingBufferSize
);
206 RingBufferInit(&NewChannel
->Inbound
, in
, RecvRingBufferSize
);
208 /* Establish the gpadl for the ring buffer */
209 DPRINT_DBG(VMBUS
, "Establishing ring buffer's gpadl for channel %p...",
212 NewChannel
->RingBufferGpadlHandle
= 0;
214 ret
= VmbusChannelEstablishGpadl(NewChannel
,
215 NewChannel
->Outbound
.RingBuffer
,
218 &NewChannel
->RingBufferGpadlHandle
);
220 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p "
221 "size %d recv ring %p size %d, downstreamoffset %d>",
222 NewChannel
, NewChannel
->OfferMsg
.ChildRelId
,
223 NewChannel
->RingBufferGpadlHandle
,
224 NewChannel
->Outbound
.RingBuffer
,
225 NewChannel
->Outbound
.RingSize
,
226 NewChannel
->Inbound
.RingBuffer
,
227 NewChannel
->Inbound
.RingSize
,
230 /* Create and init the channel open message */
231 openInfo
= kmalloc(sizeof(*openInfo
) +
232 sizeof(struct vmbus_channel_open_channel
),
234 ASSERT(openInfo
!= NULL
);
236 openInfo
->WaitEvent
= osd_WaitEventCreate();
238 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->Msg
;
239 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
240 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
241 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
242 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
243 ASSERT(openMsg
->RingBufferGpadlHandle
);
244 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>>
246 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
248 ASSERT(UserDataLen
<= MAX_USER_DEFINED_BYTES
);
250 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
252 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
253 list_add_tail(&openInfo
->MsgListEntry
,
254 &gVmbusConnection
.ChannelMsgList
);
255 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
257 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
259 ret
= VmbusPostMessage(openMsg
,
260 sizeof(struct vmbus_channel_open_channel
));
262 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
266 /* FIXME: Need to time-out here */
267 osd_WaitEventWait(openInfo
->WaitEvent
);
269 if (openInfo
->Response
.OpenResult
.Status
== 0)
270 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
272 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!",
273 NewChannel
, openInfo
->Response
.OpenResult
.Status
);
276 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
277 list_del(&openInfo
->MsgListEntry
);
278 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
280 kfree(openInfo
->WaitEvent
);
289 * DumpGpadlBody - Dump the gpadl body message to the console for
290 * debugging purposes.
292 static void DumpGpadlBody(struct vmbus_channel_gpadl_body
*Gpadl
, u32 Len
)
297 pfnCount
= (Len
- sizeof(struct vmbus_channel_gpadl_body
)) /
299 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
301 for (i
= 0; i
< pfnCount
; i
++)
302 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
307 * DumpGpadlHeader - Dump the gpadl header message to the console for
308 * debugging purposes.
310 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header
*Gpadl
)
316 "gpadl header - relid %d, range count %d, range buflen %d",
317 Gpadl
->ChildRelId
, Gpadl
->RangeCount
, Gpadl
->RangeBufLen
);
318 for (i
= 0; i
< Gpadl
->RangeCount
; i
++) {
319 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
320 pageCount
= (pageCount
> 26) ? 26 : pageCount
;
322 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
323 "page count %d", i
, Gpadl
->Range
[i
].ByteCount
,
324 Gpadl
->Range
[i
].ByteOffset
, pageCount
);
326 for (j
= 0; j
< pageCount
; j
++)
327 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
328 Gpadl
->Range
[i
].PfnArray
[j
]);
333 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
335 static int VmbusChannelCreateGpadlHeader(void *Kbuffer
, u32 Size
,
336 struct vmbus_channel_msginfo
**MsgInfo
,
341 unsigned long long pfn
;
342 struct vmbus_channel_gpadl_header
*gpaHeader
;
343 struct vmbus_channel_gpadl_body
*gpadlBody
;
344 struct vmbus_channel_msginfo
*msgHeader
;
345 struct vmbus_channel_msginfo
*msgBody
;
348 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
350 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
351 ASSERT((Size
& (PAGE_SIZE
-1)) == 0);
353 pageCount
= Size
>> PAGE_SHIFT
;
354 pfn
= virt_to_phys(Kbuffer
) >> PAGE_SHIFT
;
356 /* do we need a gpadl body msg */
357 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
358 sizeof(struct vmbus_channel_gpadl_header
) -
359 sizeof(struct gpa_range
);
360 pfnCount
= pfnSize
/ sizeof(u64
);
362 if (pageCount
> pfnCount
) {
363 /* we need a gpadl body */
364 /* fill in the header */
365 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
366 sizeof(struct vmbus_channel_gpadl_header
) +
367 sizeof(struct gpa_range
) + pfnCount
* sizeof(u64
);
368 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
370 INIT_LIST_HEAD(&msgHeader
->SubMsgList
);
371 msgHeader
->MessageSize
= msgSize
;
373 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
374 gpaHeader
->RangeCount
= 1;
375 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
376 pageCount
* sizeof(u64
);
377 gpaHeader
->Range
[0].ByteOffset
= 0;
378 gpaHeader
->Range
[0].ByteCount
= Size
;
379 for (i
= 0; i
< pfnCount
; i
++)
380 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
381 *MsgInfo
= msgHeader
;
385 pfnLeft
= pageCount
- pfnCount
;
387 /* how many pfns can we fit */
388 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
389 sizeof(struct vmbus_channel_gpadl_body
);
390 pfnCount
= pfnSize
/ sizeof(u64
);
392 /* fill in the body */
394 if (pfnLeft
> pfnCount
)
399 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
400 sizeof(struct vmbus_channel_gpadl_body
) +
401 pfnCurr
* sizeof(u64
);
402 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
404 msgBody
->MessageSize
= msgSize
;
407 (struct vmbus_channel_gpadl_body
*)msgBody
->Msg
;
411 * Gpadl is u32 and we are using a pointer which could
414 /* gpadlBody->Gpadl = kbuffer; */
415 for (i
= 0; i
< pfnCurr
; i
++)
416 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
418 /* add to msg header */
419 list_add_tail(&msgBody
->MsgListEntry
,
420 &msgHeader
->SubMsgList
);
425 /* everything fits in a header */
426 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
427 sizeof(struct vmbus_channel_gpadl_header
) +
428 sizeof(struct gpa_range
) + pageCount
* sizeof(u64
);
429 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
430 msgHeader
->MessageSize
= msgSize
;
432 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
433 gpaHeader
->RangeCount
= 1;
434 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
435 pageCount
* sizeof(u64
);
436 gpaHeader
->Range
[0].ByteOffset
= 0;
437 gpaHeader
->Range
[0].ByteCount
= Size
;
438 for (i
= 0; i
< pageCount
; i
++)
439 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
441 *MsgInfo
= msgHeader
;
449 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
451 * @Channel: a channel
452 * @Kbuffer: from kmalloc
453 * @Size: page-size multiple
454 * @GpadlHandle: some funky thing
456 int VmbusChannelEstablishGpadl(struct vmbus_channel
*Channel
, void *Kbuffer
,
457 u32 Size
, u32
*GpadlHandle
)
459 struct vmbus_channel_gpadl_header
*gpadlMsg
;
460 struct vmbus_channel_gpadl_body
*gpadlBody
;
461 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
462 struct vmbus_channel_msginfo
*msgInfo
;
463 struct vmbus_channel_msginfo
*subMsgInfo
;
465 struct list_head
*curr
;
472 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
473 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
475 VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
476 ASSERT(msgInfo
!= NULL
);
477 ASSERT(msgCount
> 0);
479 msgInfo
->WaitEvent
= osd_WaitEventCreate();
480 gpadlMsg
= (struct vmbus_channel_gpadl_header
*)msgInfo
->Msg
;
481 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
482 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
483 gpadlMsg
->Gpadl
= nextGpadlHandle
;
485 DumpGpadlHeader(gpadlMsg
);
487 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
488 list_add_tail(&msgInfo
->MsgListEntry
,
489 &gVmbusConnection
.ChannelMsgList
);
491 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
492 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d",
493 Kbuffer
, Size
, msgCount
);
495 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd",
496 msgInfo
->MessageSize
- sizeof(*msgInfo
));
498 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
-
501 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
506 list_for_each(curr
, &msgInfo
->SubMsgList
) {
508 /* FIXME: should this use list_entry() instead ? */
509 subMsgInfo
= (struct vmbus_channel_msginfo
*)curr
;
511 (struct vmbus_channel_gpadl_body
*)subMsgInfo
->Msg
;
513 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
514 gpadlBody
->Gpadl
= nextGpadlHandle
;
516 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd",
517 subMsgInfo
->MessageSize
-
518 sizeof(*subMsgInfo
));
520 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
-
521 sizeof(*subMsgInfo
));
522 ret
= VmbusPostMessage(gpadlBody
,
523 subMsgInfo
->MessageSize
-
524 sizeof(*subMsgInfo
));
528 osd_WaitEventWait(msgInfo
->WaitEvent
);
530 /* At this point, we received the gpadl created msg */
531 DPRINT_DBG(VMBUS
, "Received GPADL created "
532 "(relid %d, status %d handle %x)",
533 Channel
->OfferMsg
.ChildRelId
,
534 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
537 *GpadlHandle
= gpadlMsg
->Gpadl
;
540 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
541 list_del(&msgInfo
->MsgListEntry
);
542 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
544 kfree(msgInfo
->WaitEvent
);
553 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
555 int VmbusChannelTeardownGpadl(struct vmbus_channel
*Channel
, u32 GpadlHandle
)
557 struct vmbus_channel_gpadl_teardown
*msg
;
558 struct vmbus_channel_msginfo
*info
;
564 ASSERT(GpadlHandle
!= 0);
566 info
= kmalloc(sizeof(*info
) +
567 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
568 ASSERT(info
!= NULL
);
570 info
->WaitEvent
= osd_WaitEventCreate();
572 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->Msg
;
574 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
575 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
576 msg
->Gpadl
= GpadlHandle
;
578 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
579 list_add_tail(&info
->MsgListEntry
,
580 &gVmbusConnection
.ChannelMsgList
);
581 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
583 ret
= VmbusPostMessage(msg
,
584 sizeof(struct vmbus_channel_gpadl_teardown
));
590 osd_WaitEventWait(info
->WaitEvent
);
592 /* Received a torndown response */
593 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
594 list_del(&info
->MsgListEntry
);
595 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
597 kfree(info
->WaitEvent
);
606 * VmbusChannelClose - Close the specified channel
608 void VmbusChannelClose(struct vmbus_channel
*Channel
)
610 struct vmbus_channel_close_channel
*msg
;
611 struct vmbus_channel_msginfo
*info
;
617 /* Stop callback and cancel the timer asap */
618 Channel
->OnChannelCallback
= NULL
;
619 del_timer_sync(&Channel
->poll_timer
);
621 /* Send a closing message */
622 info
= kmalloc(sizeof(*info
) +
623 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
624 ASSERT(info
!= NULL
);
626 /* info->waitEvent = osd_WaitEventCreate(); */
628 msg
= (struct vmbus_channel_close_channel
*)info
->Msg
;
629 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
630 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
632 ret
= VmbusPostMessage(msg
, sizeof(struct vmbus_channel_close_channel
));
638 /* Tear down the gpadl for the channel's ring buffer */
639 if (Channel
->RingBufferGpadlHandle
)
640 VmbusChannelTeardownGpadl(Channel
,
641 Channel
->RingBufferGpadlHandle
);
643 /* TODO: Send a msg to release the childRelId */
645 /* Cleanup the ring buffers for this channel */
646 RingBufferCleanup(&Channel
->Outbound
);
647 RingBufferCleanup(&Channel
->Inbound
);
649 osd_PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
654 * If we are closing the channel during an error path in
655 * opening the channel, don't free the channel since the
656 * caller will free the channel
659 if (Channel
->State
== CHANNEL_OPEN_STATE
) {
660 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
661 list_del(&Channel
->ListEntry
);
662 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
664 FreeVmbusChannel(Channel
);
671 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
672 * @Channel: Pointer to vmbus_channel structure.
673 * @Buffer: Pointer to the buffer you want to receive the data into.
674 * @BufferLen: Maximum size of what the the buffer will hold
675 * @RequestId: Identifier of the request
676 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
679 * Sends data in @Buffer directly to hyper-v via the vmbus
680 * This will send the data unparsed to hyper-v.
682 * Mainly used by Hyper-V drivers.
684 int VmbusChannelSendPacket(struct vmbus_channel
*Channel
, const void *Buffer
,
685 u32 BufferLen
, u64 RequestId
,
686 enum vmbus_packet_type Type
, u32 Flags
)
688 struct vmpacket_descriptor desc
;
689 u32 packetLen
= sizeof(struct vmpacket_descriptor
) + BufferLen
;
690 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
691 struct scatterlist bufferList
[3];
696 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d",
697 Channel
, Buffer
, BufferLen
);
699 DumpVmbusChannel(Channel
);
701 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
703 /* Setup the descriptor */
704 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
705 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
706 /* in 8-bytes granularity */
707 desc
.DataOffset8
= sizeof(struct vmpacket_descriptor
) >> 3;
708 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
709 desc
.TransactionId
= RequestId
;
711 sg_init_table(bufferList
, 3);
712 sg_set_buf(&bufferList
[0], &desc
, sizeof(struct vmpacket_descriptor
));
713 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
714 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
716 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
718 /* TODO: We should determine if this is optional */
719 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
720 VmbusChannelSetEvent(Channel
);
726 EXPORT_SYMBOL(VmbusChannelSendPacket
);
729 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
730 * packets using a GPADL Direct packet type.
732 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel
*Channel
,
733 struct hv_page_buffer PageBuffers
[],
734 u32 PageCount
, void *Buffer
, u32 BufferLen
,
739 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
742 u32 packetLenAligned
;
743 struct scatterlist bufferList
[3];
748 ASSERT(PageCount
<= MAX_PAGE_BUFFER_COUNT
);
750 DumpVmbusChannel(Channel
);
753 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
754 * largest size we support
756 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) -
757 ((MAX_PAGE_BUFFER_COUNT
- PageCount
) *
758 sizeof(struct hv_page_buffer
));
759 packetLen
= descSize
+ BufferLen
;
760 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
762 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
764 /* Setup the descriptor */
765 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
766 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
767 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
768 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
769 desc
.TransactionId
= RequestId
;
770 desc
.RangeCount
= PageCount
;
772 for (i
= 0; i
< PageCount
; i
++) {
773 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
774 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
775 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
778 sg_init_table(bufferList
, 3);
779 sg_set_buf(&bufferList
[0], &desc
, descSize
);
780 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
781 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
783 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
785 /* TODO: We should determine if this is optional */
786 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
787 VmbusChannelSetEvent(Channel
);
795 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
796 * using a GPADL Direct packet type.
798 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel
*Channel
,
799 struct hv_multipage_buffer
*MultiPageBuffer
,
800 void *Buffer
, u32 BufferLen
, u64 RequestId
)
803 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
806 u32 packetLenAligned
;
807 struct scatterlist bufferList
[3];
809 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
,
810 MultiPageBuffer
->Length
);
814 DumpVmbusChannel(Channel
);
816 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u",
817 MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
819 ASSERT(PfnCount
> 0);
820 ASSERT(PfnCount
<= MAX_MULTIPAGE_BUFFER_COUNT
);
823 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
824 * the largest size we support
826 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) -
827 ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
) *
829 packetLen
= descSize
+ BufferLen
;
830 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
832 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
834 /* Setup the descriptor */
835 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
836 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
837 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
838 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
839 desc
.TransactionId
= RequestId
;
842 desc
.Range
.Length
= MultiPageBuffer
->Length
;
843 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
845 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
,
846 PfnCount
* sizeof(u64
));
848 sg_init_table(bufferList
, 3);
849 sg_set_buf(&bufferList
[0], &desc
, descSize
);
850 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
851 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
853 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
855 /* TODO: We should determine if this is optional */
856 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
857 VmbusChannelSetEvent(Channel
);
866 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
867 * @Channel: Pointer to vmbus_channel structure.
868 * @Buffer: Pointer to the buffer you want to receive the data into.
869 * @BufferLen: Maximum size of what the the buffer will hold
870 * @BufferActualLen: The actual size of the data after it was received
871 * @RequestId: Identifier of the request
873 * Receives directly from the hyper-v vmbus and puts the data it received
874 * into Buffer. This will receive the data unparsed from hyper-v.
876 * Mainly used by Hyper-V drivers.
878 int VmbusChannelRecvPacket(struct vmbus_channel
*Channel
, void *Buffer
,
879 u32 BufferLen
, u32
*BufferActualLen
, u64
*RequestId
)
881 struct vmpacket_descriptor desc
;
889 *BufferActualLen
= 0;
892 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
894 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
895 sizeof(struct vmpacket_descriptor
));
897 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
899 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
904 /* VmbusChannelClearEvent(Channel); */
906 packetLen
= desc
.Length8
<< 3;
907 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
908 /* ASSERT(userLen > 0); */
910 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
911 "flag %d tid %llx pktlen %d datalen %d> ",
912 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
913 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
915 *BufferActualLen
= userLen
;
917 if (userLen
> BufferLen
) {
918 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
920 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d",
927 *RequestId
= desc
.TransactionId
;
929 /* Copy over the packet to the user buffer */
930 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
,
931 (desc
.DataOffset8
<< 3));
933 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
939 EXPORT_SYMBOL(VmbusChannelRecvPacket
);
942 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
944 int VmbusChannelRecvPacketRaw(struct vmbus_channel
*Channel
, void *Buffer
,
945 u32 BufferLen
, u32
*BufferActualLen
,
948 struct vmpacket_descriptor desc
;
956 *BufferActualLen
= 0;
959 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
961 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
962 sizeof(struct vmpacket_descriptor
));
964 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
966 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
971 /* VmbusChannelClearEvent(Channel); */
973 packetLen
= desc
.Length8
<< 3;
974 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
976 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
977 "flag %d tid %llx pktlen %d datalen %d> ",
978 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
979 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
981 *BufferActualLen
= packetLen
;
983 if (packetLen
> BufferLen
) {
984 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
986 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but "
987 "got space for only %d bytes", packetLen
, BufferLen
);
992 *RequestId
= desc
.TransactionId
;
994 /* Copy over the entire packet to the user buffer */
995 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
997 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1005 * VmbusChannelOnChannelEvent - Channel event callback
1007 void VmbusChannelOnChannelEvent(struct vmbus_channel
*Channel
)
1009 DumpVmbusChannel(Channel
);
1010 ASSERT(Channel
->OnChannelCallback
);
1012 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1014 mod_timer(&Channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
1018 * VmbusChannelOnTimer - Timer event callback
1020 void VmbusChannelOnTimer(unsigned long data
)
1022 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
1024 if (channel
->OnChannelCallback
)
1025 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1029 * DumpVmbusChannel - Dump vmbus channel info to the console
1031 static void DumpVmbusChannel(struct vmbus_channel
*Channel
)
1033 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1034 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1035 DumpRingInfo(&Channel
->Inbound
, "Inbound ");