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
);
222 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p "
223 "size %d recv ring %p size %d, downstreamoffset %d>",
224 NewChannel
, NewChannel
->OfferMsg
.ChildRelId
,
225 NewChannel
->RingBufferGpadlHandle
,
226 NewChannel
->Outbound
.RingBuffer
,
227 NewChannel
->Outbound
.RingSize
,
228 NewChannel
->Inbound
.RingBuffer
,
229 NewChannel
->Inbound
.RingSize
,
232 /* Create and init the channel open message */
233 openInfo
= kmalloc(sizeof(*openInfo
) +
234 sizeof(struct vmbus_channel_open_channel
),
236 ASSERT(openInfo
!= NULL
);
238 openInfo
->WaitEvent
= osd_WaitEventCreate();
240 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->Msg
;
241 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
242 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
243 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
244 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
245 ASSERT(openMsg
->RingBufferGpadlHandle
);
246 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>>
248 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
250 ASSERT(UserDataLen
<= MAX_USER_DEFINED_BYTES
);
252 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
254 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
255 list_add_tail(&openInfo
->MsgListEntry
,
256 &gVmbusConnection
.ChannelMsgList
);
257 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
259 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
261 ret
= VmbusPostMessage(openMsg
,
262 sizeof(struct vmbus_channel_open_channel
));
264 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
268 /* FIXME: Need to time-out here */
269 osd_WaitEventWait(openInfo
->WaitEvent
);
271 if (openInfo
->Response
.OpenResult
.Status
== 0)
272 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
274 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!",
275 NewChannel
, openInfo
->Response
.OpenResult
.Status
);
278 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
279 list_del(&openInfo
->MsgListEntry
);
280 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
282 kfree(openInfo
->WaitEvent
);
291 * DumpGpadlBody - Dump the gpadl body message to the console for
292 * debugging purposes.
294 static void DumpGpadlBody(struct vmbus_channel_gpadl_body
*Gpadl
, u32 Len
)
299 pfnCount
= (Len
- sizeof(struct vmbus_channel_gpadl_body
)) /
301 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
303 for (i
= 0; i
< pfnCount
; i
++)
304 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
309 * DumpGpadlHeader - Dump the gpadl header message to the console for
310 * debugging purposes.
312 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header
*Gpadl
)
318 "gpadl header - relid %d, range count %d, range buflen %d",
319 Gpadl
->ChildRelId
, Gpadl
->RangeCount
, Gpadl
->RangeBufLen
);
320 for (i
= 0; i
< Gpadl
->RangeCount
; i
++) {
321 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
322 pageCount
= (pageCount
> 26) ? 26 : pageCount
;
324 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
325 "page count %d", i
, Gpadl
->Range
[i
].ByteCount
,
326 Gpadl
->Range
[i
].ByteOffset
, pageCount
);
328 for (j
= 0; j
< pageCount
; j
++)
329 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
330 Gpadl
->Range
[i
].PfnArray
[j
]);
335 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
337 static int VmbusChannelCreateGpadlHeader(void *Kbuffer
, u32 Size
,
338 struct vmbus_channel_msginfo
**MsgInfo
,
343 unsigned long long pfn
;
344 struct vmbus_channel_gpadl_header
*gpaHeader
;
345 struct vmbus_channel_gpadl_body
*gpadlBody
;
346 struct vmbus_channel_msginfo
*msgHeader
;
347 struct vmbus_channel_msginfo
*msgBody
;
350 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
352 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
353 ASSERT((Size
& (PAGE_SIZE
-1)) == 0);
355 pageCount
= Size
>> PAGE_SHIFT
;
356 pfn
= virt_to_phys(Kbuffer
) >> PAGE_SHIFT
;
358 /* do we need a gpadl body msg */
359 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
360 sizeof(struct vmbus_channel_gpadl_header
) -
361 sizeof(struct gpa_range
);
362 pfnCount
= pfnSize
/ sizeof(u64
);
364 if (pageCount
> pfnCount
) {
365 /* we need a gpadl body */
366 /* fill in the header */
367 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
368 sizeof(struct vmbus_channel_gpadl_header
) +
369 sizeof(struct gpa_range
) + pfnCount
* sizeof(u64
);
370 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
372 INIT_LIST_HEAD(&msgHeader
->SubMsgList
);
373 msgHeader
->MessageSize
= msgSize
;
375 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
376 gpaHeader
->RangeCount
= 1;
377 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
378 pageCount
* sizeof(u64
);
379 gpaHeader
->Range
[0].ByteOffset
= 0;
380 gpaHeader
->Range
[0].ByteCount
= Size
;
381 for (i
= 0; i
< pfnCount
; i
++)
382 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
383 *MsgInfo
= msgHeader
;
387 pfnLeft
= pageCount
- pfnCount
;
389 /* how many pfns can we fit */
390 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
391 sizeof(struct vmbus_channel_gpadl_body
);
392 pfnCount
= pfnSize
/ sizeof(u64
);
394 /* fill in the body */
396 if (pfnLeft
> pfnCount
)
401 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
402 sizeof(struct vmbus_channel_gpadl_body
) +
403 pfnCurr
* sizeof(u64
);
404 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
406 msgBody
->MessageSize
= msgSize
;
409 (struct vmbus_channel_gpadl_body
*)msgBody
->Msg
;
413 * Gpadl is u32 and we are using a pointer which could
416 /* gpadlBody->Gpadl = kbuffer; */
417 for (i
= 0; i
< pfnCurr
; i
++)
418 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
420 /* add to msg header */
421 list_add_tail(&msgBody
->MsgListEntry
,
422 &msgHeader
->SubMsgList
);
427 /* everything fits in a header */
428 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
429 sizeof(struct vmbus_channel_gpadl_header
) +
430 sizeof(struct gpa_range
) + pageCount
* sizeof(u64
);
431 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
432 msgHeader
->MessageSize
= msgSize
;
434 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
435 gpaHeader
->RangeCount
= 1;
436 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
437 pageCount
* sizeof(u64
);
438 gpaHeader
->Range
[0].ByteOffset
= 0;
439 gpaHeader
->Range
[0].ByteCount
= Size
;
440 for (i
= 0; i
< pageCount
; i
++)
441 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
443 *MsgInfo
= msgHeader
;
451 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
453 * @Channel: a channel
454 * @Kbuffer: from kmalloc
455 * @Size: page-size multiple
456 * @GpadlHandle: some funky thing
458 int VmbusChannelEstablishGpadl(struct vmbus_channel
*Channel
, void *Kbuffer
,
459 u32 Size
, u32
*GpadlHandle
)
461 struct vmbus_channel_gpadl_header
*gpadlMsg
;
462 struct vmbus_channel_gpadl_body
*gpadlBody
;
463 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
464 struct vmbus_channel_msginfo
*msgInfo
;
465 struct vmbus_channel_msginfo
*subMsgInfo
;
467 struct list_head
*curr
;
474 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
475 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
477 VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
478 ASSERT(msgInfo
!= NULL
);
479 ASSERT(msgCount
> 0);
481 msgInfo
->WaitEvent
= osd_WaitEventCreate();
482 gpadlMsg
= (struct vmbus_channel_gpadl_header
*)msgInfo
->Msg
;
483 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
484 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
485 gpadlMsg
->Gpadl
= nextGpadlHandle
;
487 DumpGpadlHeader(gpadlMsg
);
489 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
490 list_add_tail(&msgInfo
->MsgListEntry
,
491 &gVmbusConnection
.ChannelMsgList
);
493 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
494 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d",
495 Kbuffer
, Size
, msgCount
);
497 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd",
498 msgInfo
->MessageSize
- sizeof(*msgInfo
));
500 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
-
503 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
508 list_for_each(curr
, &msgInfo
->SubMsgList
) {
510 /* FIXME: should this use list_entry() instead ? */
511 subMsgInfo
= (struct vmbus_channel_msginfo
*)curr
;
513 (struct vmbus_channel_gpadl_body
*)subMsgInfo
->Msg
;
515 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
516 gpadlBody
->Gpadl
= nextGpadlHandle
;
518 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd",
519 subMsgInfo
->MessageSize
-
520 sizeof(*subMsgInfo
));
522 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
-
523 sizeof(*subMsgInfo
));
524 ret
= VmbusPostMessage(gpadlBody
,
525 subMsgInfo
->MessageSize
-
526 sizeof(*subMsgInfo
));
530 osd_WaitEventWait(msgInfo
->WaitEvent
);
532 /* At this point, we received the gpadl created msg */
533 DPRINT_DBG(VMBUS
, "Received GPADL created "
534 "(relid %d, status %d handle %x)",
535 Channel
->OfferMsg
.ChildRelId
,
536 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
539 *GpadlHandle
= gpadlMsg
->Gpadl
;
542 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
543 list_del(&msgInfo
->MsgListEntry
);
544 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
546 kfree(msgInfo
->WaitEvent
);
555 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
557 int VmbusChannelTeardownGpadl(struct vmbus_channel
*Channel
, u32 GpadlHandle
)
559 struct vmbus_channel_gpadl_teardown
*msg
;
560 struct vmbus_channel_msginfo
*info
;
566 ASSERT(GpadlHandle
!= 0);
568 info
= kmalloc(sizeof(*info
) +
569 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
570 ASSERT(info
!= NULL
);
572 info
->WaitEvent
= osd_WaitEventCreate();
574 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->Msg
;
576 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
577 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
578 msg
->Gpadl
= GpadlHandle
;
580 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
581 list_add_tail(&info
->MsgListEntry
,
582 &gVmbusConnection
.ChannelMsgList
);
583 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
585 ret
= VmbusPostMessage(msg
,
586 sizeof(struct vmbus_channel_gpadl_teardown
));
592 osd_WaitEventWait(info
->WaitEvent
);
594 /* Received a torndown response */
595 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
596 list_del(&info
->MsgListEntry
);
597 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
599 kfree(info
->WaitEvent
);
608 * VmbusChannelClose - Close the specified channel
610 void VmbusChannelClose(struct vmbus_channel
*Channel
)
612 struct vmbus_channel_close_channel
*msg
;
613 struct vmbus_channel_msginfo
*info
;
619 /* Stop callback and cancel the timer asap */
620 Channel
->OnChannelCallback
= NULL
;
621 del_timer_sync(&Channel
->poll_timer
);
623 /* Send a closing message */
624 info
= kmalloc(sizeof(*info
) +
625 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
626 ASSERT(info
!= NULL
);
628 /* info->waitEvent = osd_WaitEventCreate(); */
630 msg
= (struct vmbus_channel_close_channel
*)info
->Msg
;
631 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
632 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
634 ret
= VmbusPostMessage(msg
, sizeof(struct vmbus_channel_close_channel
));
640 /* Tear down the gpadl for the channel's ring buffer */
641 if (Channel
->RingBufferGpadlHandle
)
642 VmbusChannelTeardownGpadl(Channel
,
643 Channel
->RingBufferGpadlHandle
);
645 /* TODO: Send a msg to release the childRelId */
647 /* Cleanup the ring buffers for this channel */
648 RingBufferCleanup(&Channel
->Outbound
);
649 RingBufferCleanup(&Channel
->Inbound
);
651 osd_PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
656 * If we are closing the channel during an error path in
657 * opening the channel, don't free the channel since the
658 * caller will free the channel
661 if (Channel
->State
== CHANNEL_OPEN_STATE
) {
662 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
663 list_del(&Channel
->ListEntry
);
664 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
666 FreeVmbusChannel(Channel
);
673 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
674 * @Channel: Pointer to vmbus_channel structure.
675 * @Buffer: Pointer to the buffer you want to receive the data into.
676 * @BufferLen: Maximum size of what the the buffer will hold
677 * @RequestId: Identifier of the request
678 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
681 * Sends data in @Buffer directly to hyper-v via the vmbus
682 * This will send the data unparsed to hyper-v.
684 * Mainly used by Hyper-V drivers.
686 int VmbusChannelSendPacket(struct vmbus_channel
*Channel
, const void *Buffer
,
687 u32 BufferLen
, u64 RequestId
,
688 enum vmbus_packet_type Type
, u32 Flags
)
690 struct vmpacket_descriptor desc
;
691 u32 packetLen
= sizeof(struct vmpacket_descriptor
) + BufferLen
;
692 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
693 struct scatterlist bufferList
[3];
698 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d",
699 Channel
, Buffer
, BufferLen
);
701 DumpVmbusChannel(Channel
);
703 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
705 /* Setup the descriptor */
706 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
707 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
708 /* in 8-bytes granularity */
709 desc
.DataOffset8
= sizeof(struct vmpacket_descriptor
) >> 3;
710 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
711 desc
.TransactionId
= RequestId
;
713 sg_init_table(bufferList
, 3);
714 sg_set_buf(&bufferList
[0], &desc
, sizeof(struct vmpacket_descriptor
));
715 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
716 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
718 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
720 /* TODO: We should determine if this is optional */
721 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
722 VmbusChannelSetEvent(Channel
);
728 EXPORT_SYMBOL(VmbusChannelSendPacket
);
731 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
732 * packets using a GPADL Direct packet type.
734 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel
*Channel
,
735 struct hv_page_buffer PageBuffers
[],
736 u32 PageCount
, void *Buffer
, u32 BufferLen
,
741 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
744 u32 packetLenAligned
;
745 struct scatterlist bufferList
[3];
750 ASSERT(PageCount
<= MAX_PAGE_BUFFER_COUNT
);
752 DumpVmbusChannel(Channel
);
755 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
756 * largest size we support
758 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) -
759 ((MAX_PAGE_BUFFER_COUNT
- PageCount
) *
760 sizeof(struct hv_page_buffer
));
761 packetLen
= descSize
+ BufferLen
;
762 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
764 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
766 /* Setup the descriptor */
767 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
768 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
769 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
770 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
771 desc
.TransactionId
= RequestId
;
772 desc
.RangeCount
= PageCount
;
774 for (i
= 0; i
< PageCount
; i
++) {
775 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
776 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
777 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
780 sg_init_table(bufferList
, 3);
781 sg_set_buf(&bufferList
[0], &desc
, descSize
);
782 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
783 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
785 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
787 /* TODO: We should determine if this is optional */
788 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
789 VmbusChannelSetEvent(Channel
);
797 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
798 * using a GPADL Direct packet type.
800 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel
*Channel
,
801 struct hv_multipage_buffer
*MultiPageBuffer
,
802 void *Buffer
, u32 BufferLen
, u64 RequestId
)
805 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
808 u32 packetLenAligned
;
809 struct scatterlist bufferList
[3];
811 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
,
812 MultiPageBuffer
->Length
);
816 DumpVmbusChannel(Channel
);
818 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u",
819 MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
821 ASSERT(PfnCount
> 0);
822 ASSERT(PfnCount
<= MAX_MULTIPAGE_BUFFER_COUNT
);
825 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
826 * the largest size we support
828 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) -
829 ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
) *
831 packetLen
= descSize
+ BufferLen
;
832 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
834 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
836 /* Setup the descriptor */
837 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
838 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
839 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
840 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
841 desc
.TransactionId
= RequestId
;
844 desc
.Range
.Length
= MultiPageBuffer
->Length
;
845 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
847 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
,
848 PfnCount
* sizeof(u64
));
850 sg_init_table(bufferList
, 3);
851 sg_set_buf(&bufferList
[0], &desc
, descSize
);
852 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
853 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
855 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
857 /* TODO: We should determine if this is optional */
858 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
859 VmbusChannelSetEvent(Channel
);
868 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
869 * @Channel: Pointer to vmbus_channel structure.
870 * @Buffer: Pointer to the buffer you want to receive the data into.
871 * @BufferLen: Maximum size of what the the buffer will hold
872 * @BufferActualLen: The actual size of the data after it was received
873 * @RequestId: Identifier of the request
875 * Receives directly from the hyper-v vmbus and puts the data it received
876 * into Buffer. This will receive the data unparsed from hyper-v.
878 * Mainly used by Hyper-V drivers.
880 int VmbusChannelRecvPacket(struct vmbus_channel
*Channel
, void *Buffer
,
881 u32 BufferLen
, u32
*BufferActualLen
, u64
*RequestId
)
883 struct vmpacket_descriptor desc
;
891 *BufferActualLen
= 0;
894 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
896 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
897 sizeof(struct vmpacket_descriptor
));
899 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
901 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
906 /* VmbusChannelClearEvent(Channel); */
908 packetLen
= desc
.Length8
<< 3;
909 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
910 /* ASSERT(userLen > 0); */
912 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
913 "flag %d tid %llx pktlen %d datalen %d> ",
914 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
915 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
917 *BufferActualLen
= userLen
;
919 if (userLen
> BufferLen
) {
920 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
922 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d",
929 *RequestId
= desc
.TransactionId
;
931 /* Copy over the packet to the user buffer */
932 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
,
933 (desc
.DataOffset8
<< 3));
935 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
941 EXPORT_SYMBOL(VmbusChannelRecvPacket
);
944 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
946 int VmbusChannelRecvPacketRaw(struct vmbus_channel
*Channel
, void *Buffer
,
947 u32 BufferLen
, u32
*BufferActualLen
,
950 struct vmpacket_descriptor desc
;
958 *BufferActualLen
= 0;
961 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
963 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
964 sizeof(struct vmpacket_descriptor
));
966 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
968 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
973 /* VmbusChannelClearEvent(Channel); */
975 packetLen
= desc
.Length8
<< 3;
976 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
978 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
979 "flag %d tid %llx pktlen %d datalen %d> ",
980 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
981 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
983 *BufferActualLen
= packetLen
;
985 if (packetLen
> BufferLen
) {
986 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
988 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but "
989 "got space for only %d bytes", packetLen
, BufferLen
);
994 *RequestId
= desc
.TransactionId
;
996 /* Copy over the entire packet to the user buffer */
997 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
999 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1007 * VmbusChannelOnChannelEvent - Channel event callback
1009 void VmbusChannelOnChannelEvent(struct vmbus_channel
*Channel
)
1011 DumpVmbusChannel(Channel
);
1012 ASSERT(Channel
->OnChannelCallback
);
1014 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1016 mod_timer(&Channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
1020 * VmbusChannelOnTimer - Timer event callback
1022 void VmbusChannelOnTimer(unsigned long data
)
1024 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
1026 if (channel
->OnChannelCallback
)
1027 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1031 * DumpVmbusChannel - Dump vmbus channel info to the console
1033 static void DumpVmbusChannel(struct vmbus_channel
*Channel
)
1035 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1036 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1037 DumpRingInfo(&Channel
->Inbound
, "Inbound ");