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
;
77 if (Channel
->OfferMsg
.MonitorAllocated
) {
78 /* Each u32 represents 32 channels */
79 set_bit(Channel
->OfferMsg
.ChildRelId
& 31,
80 (unsigned long *) gVmbusConnection
.SendInterruptPage
+
81 (Channel
->OfferMsg
.ChildRelId
>> 5));
83 monitorPage
= gVmbusConnection
.MonitorPages
;
84 monitorPage
++; /* Get the child to parent monitor page */
86 set_bit(Channel
->MonitorBit
,
87 (unsigned long *)&monitorPage
->TriggerGroup
88 [Channel
->MonitorGroup
].Pending
);
91 VmbusSetEvent(Channel
->OfferMsg
.ChildRelId
);
96 static void VmbusChannelClearEvent(struct vmbus_channel
*channel
)
98 struct hv_monitor_page
*monitorPage
;
100 if (Channel
->OfferMsg
.MonitorAllocated
) {
101 /* Each u32 represents 32 channels */
102 clear_bit(Channel
->OfferMsg
.ChildRelId
& 31,
103 (unsigned long *)gVmbusConnection
.SendInterruptPage
+
104 (Channel
->OfferMsg
.ChildRelId
>> 5));
107 (struct hv_monitor_page
*)gVmbusConnection
.MonitorPages
;
108 monitorPage
++; /* Get the child to parent monitor page */
110 clear_bit(Channel
->MonitorBit
,
111 (unsigned long *)&monitorPage
->TriggerGroup
112 [Channel
->MonitorGroup
].Pending
);
118 * VmbusChannelGetDebugInfo -Retrieve various channel debug info
120 void VmbusChannelGetDebugInfo(struct vmbus_channel
*Channel
,
121 struct vmbus_channel_debug_info
*DebugInfo
)
123 struct hv_monitor_page
*monitorPage
;
124 u8 monitorGroup
= (u8
)Channel
->OfferMsg
.MonitorId
/ 32;
125 u8 monitorOffset
= (u8
)Channel
->OfferMsg
.MonitorId
% 32;
126 /* u32 monitorBit = 1 << monitorOffset; */
128 DebugInfo
->RelId
= Channel
->OfferMsg
.ChildRelId
;
129 DebugInfo
->State
= Channel
->State
;
130 memcpy(&DebugInfo
->InterfaceType
,
131 &Channel
->OfferMsg
.Offer
.InterfaceType
, sizeof(struct hv_guid
));
132 memcpy(&DebugInfo
->InterfaceInstance
,
133 &Channel
->OfferMsg
.Offer
.InterfaceInstance
,
134 sizeof(struct hv_guid
));
136 monitorPage
= (struct hv_monitor_page
*)gVmbusConnection
.MonitorPages
;
138 DebugInfo
->MonitorId
= Channel
->OfferMsg
.MonitorId
;
140 DebugInfo
->ServerMonitorPending
=
141 monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
142 DebugInfo
->ServerMonitorLatency
=
143 monitorPage
->Latency
[monitorGroup
][monitorOffset
];
144 DebugInfo
->ServerMonitorConnectionId
=
145 monitorPage
->Parameter
[monitorGroup
]
146 [monitorOffset
].ConnectionId
.u
.Id
;
150 DebugInfo
->ClientMonitorPending
=
151 monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
152 DebugInfo
->ClientMonitorLatency
=
153 monitorPage
->Latency
[monitorGroup
][monitorOffset
];
154 DebugInfo
->ClientMonitorConnectionId
=
155 monitorPage
->Parameter
[monitorGroup
]
156 [monitorOffset
].ConnectionId
.u
.Id
;
158 RingBufferGetDebugInfo(&Channel
->Inbound
, &DebugInfo
->Inbound
);
159 RingBufferGetDebugInfo(&Channel
->Outbound
, &DebugInfo
->Outbound
);
163 * VmbusChannelOpen - Open the specified channel.
165 int VmbusChannelOpen(struct vmbus_channel
*NewChannel
, u32 SendRingBufferSize
,
166 u32 RecvRingBufferSize
, void *UserData
, u32 UserDataLen
,
167 void (*OnChannelCallback
)(void *context
), void *Context
)
169 struct vmbus_channel_open_channel
*openMsg
;
170 struct vmbus_channel_msginfo
*openInfo
= NULL
;
175 /* Aligned to page size */
176 /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
177 /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
179 NewChannel
->OnChannelCallback
= OnChannelCallback
;
180 NewChannel
->ChannelCallbackContext
= Context
;
182 /* Allocate the ring buffer */
183 out
= osd_PageAlloc((SendRingBufferSize
+ RecvRingBufferSize
)
188 /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
190 in
= (void *)((unsigned long)out
+ SendRingBufferSize
);
192 NewChannel
->RingBufferPages
= out
;
193 NewChannel
->RingBufferPageCount
= (SendRingBufferSize
+
194 RecvRingBufferSize
) >> PAGE_SHIFT
;
196 ret
= RingBufferInit(&NewChannel
->Outbound
, out
, SendRingBufferSize
);
202 ret
= RingBufferInit(&NewChannel
->Inbound
, in
, RecvRingBufferSize
);
209 /* Establish the gpadl for the ring buffer */
210 DPRINT_DBG(VMBUS
, "Establishing ring buffer's gpadl for channel %p...",
213 NewChannel
->RingBufferGpadlHandle
= 0;
215 ret
= VmbusChannelEstablishGpadl(NewChannel
,
216 NewChannel
->Outbound
.RingBuffer
,
219 &NewChannel
->RingBufferGpadlHandle
);
226 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p "
227 "size %d recv ring %p size %d, downstreamoffset %d>",
228 NewChannel
, NewChannel
->OfferMsg
.ChildRelId
,
229 NewChannel
->RingBufferGpadlHandle
,
230 NewChannel
->Outbound
.RingBuffer
,
231 NewChannel
->Outbound
.RingSize
,
232 NewChannel
->Inbound
.RingBuffer
,
233 NewChannel
->Inbound
.RingSize
,
236 /* Create and init the channel open message */
237 openInfo
= kmalloc(sizeof(*openInfo
) +
238 sizeof(struct vmbus_channel_open_channel
),
245 openInfo
->WaitEvent
= osd_WaitEventCreate();
246 if (!openInfo
->WaitEvent
) {
251 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->Msg
;
252 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
253 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
254 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
255 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
256 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>>
258 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
260 if (UserDataLen
> MAX_USER_DEFINED_BYTES
) {
266 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
268 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
269 list_add_tail(&openInfo
->MsgListEntry
,
270 &gVmbusConnection
.ChannelMsgList
);
271 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
273 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
275 ret
= VmbusPostMessage(openMsg
,
276 sizeof(struct vmbus_channel_open_channel
));
278 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
282 /* FIXME: Need to time-out here */
283 osd_WaitEventWait(openInfo
->WaitEvent
);
285 if (openInfo
->Response
.OpenResult
.Status
== 0)
286 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
288 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!",
289 NewChannel
, openInfo
->Response
.OpenResult
.Status
);
292 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
293 list_del(&openInfo
->MsgListEntry
);
294 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
296 kfree(openInfo
->WaitEvent
);
301 RingBufferCleanup(&NewChannel
->Outbound
);
302 RingBufferCleanup(&NewChannel
->Inbound
);
303 osd_PageFree(out
, (SendRingBufferSize
+ RecvRingBufferSize
)
310 * DumpGpadlBody - Dump the gpadl body message to the console for
311 * debugging purposes.
313 static void DumpGpadlBody(struct vmbus_channel_gpadl_body
*Gpadl
, u32 Len
)
318 pfnCount
= (Len
- sizeof(struct vmbus_channel_gpadl_body
)) /
320 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
322 for (i
= 0; i
< pfnCount
; i
++)
323 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
328 * DumpGpadlHeader - Dump the gpadl header message to the console for
329 * debugging purposes.
331 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header
*Gpadl
)
337 "gpadl header - relid %d, range count %d, range buflen %d",
338 Gpadl
->ChildRelId
, Gpadl
->RangeCount
, Gpadl
->RangeBufLen
);
339 for (i
= 0; i
< Gpadl
->RangeCount
; i
++) {
340 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
341 pageCount
= (pageCount
> 26) ? 26 : pageCount
;
343 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
344 "page count %d", i
, Gpadl
->Range
[i
].ByteCount
,
345 Gpadl
->Range
[i
].ByteOffset
, pageCount
);
347 for (j
= 0; j
< pageCount
; j
++)
348 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
349 Gpadl
->Range
[i
].PfnArray
[j
]);
354 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
356 static int VmbusChannelCreateGpadlHeader(void *Kbuffer
, u32 Size
,
357 struct vmbus_channel_msginfo
**MsgInfo
,
362 unsigned long long pfn
;
363 struct vmbus_channel_gpadl_header
*gpaHeader
;
364 struct vmbus_channel_gpadl_body
*gpadlBody
;
365 struct vmbus_channel_msginfo
*msgHeader
;
366 struct vmbus_channel_msginfo
*msgBody
= NULL
;
369 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
371 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
372 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
374 pageCount
= Size
>> PAGE_SHIFT
;
375 pfn
= virt_to_phys(Kbuffer
) >> PAGE_SHIFT
;
377 /* do we need a gpadl body msg */
378 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
379 sizeof(struct vmbus_channel_gpadl_header
) -
380 sizeof(struct gpa_range
);
381 pfnCount
= pfnSize
/ sizeof(u64
);
383 if (pageCount
> pfnCount
) {
384 /* we need a gpadl body */
385 /* fill in the header */
386 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
387 sizeof(struct vmbus_channel_gpadl_header
) +
388 sizeof(struct gpa_range
) + pfnCount
* sizeof(u64
);
389 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
393 INIT_LIST_HEAD(&msgHeader
->SubMsgList
);
394 msgHeader
->MessageSize
= msgSize
;
396 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
397 gpaHeader
->RangeCount
= 1;
398 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
399 pageCount
* sizeof(u64
);
400 gpaHeader
->Range
[0].ByteOffset
= 0;
401 gpaHeader
->Range
[0].ByteCount
= Size
;
402 for (i
= 0; i
< pfnCount
; i
++)
403 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
404 *MsgInfo
= msgHeader
;
408 pfnLeft
= pageCount
- pfnCount
;
410 /* how many pfns can we fit */
411 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
-
412 sizeof(struct vmbus_channel_gpadl_body
);
413 pfnCount
= pfnSize
/ sizeof(u64
);
415 /* fill in the body */
417 if (pfnLeft
> pfnCount
)
422 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
423 sizeof(struct vmbus_channel_gpadl_body
) +
424 pfnCurr
* sizeof(u64
);
425 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
426 /* FIXME: we probably need to more if this fails */
429 msgBody
->MessageSize
= msgSize
;
432 (struct vmbus_channel_gpadl_body
*)msgBody
->Msg
;
436 * Gpadl is u32 and we are using a pointer which could
439 /* gpadlBody->Gpadl = kbuffer; */
440 for (i
= 0; i
< pfnCurr
; i
++)
441 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
443 /* add to msg header */
444 list_add_tail(&msgBody
->MsgListEntry
,
445 &msgHeader
->SubMsgList
);
450 /* everything fits in a header */
451 msgSize
= sizeof(struct vmbus_channel_msginfo
) +
452 sizeof(struct vmbus_channel_gpadl_header
) +
453 sizeof(struct gpa_range
) + pageCount
* sizeof(u64
);
454 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
455 if (msgHeader
== NULL
)
457 msgHeader
->MessageSize
= msgSize
;
459 gpaHeader
= (struct vmbus_channel_gpadl_header
*)msgHeader
->Msg
;
460 gpaHeader
->RangeCount
= 1;
461 gpaHeader
->RangeBufLen
= sizeof(struct gpa_range
) +
462 pageCount
* sizeof(u64
);
463 gpaHeader
->Range
[0].ByteOffset
= 0;
464 gpaHeader
->Range
[0].ByteCount
= Size
;
465 for (i
= 0; i
< pageCount
; i
++)
466 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
468 *MsgInfo
= msgHeader
;
480 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
482 * @Channel: a channel
483 * @Kbuffer: from kmalloc
484 * @Size: page-size multiple
485 * @GpadlHandle: some funky thing
487 int VmbusChannelEstablishGpadl(struct vmbus_channel
*Channel
, void *Kbuffer
,
488 u32 Size
, u32
*GpadlHandle
)
490 struct vmbus_channel_gpadl_header
*gpadlMsg
;
491 struct vmbus_channel_gpadl_body
*gpadlBody
;
492 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
493 struct vmbus_channel_msginfo
*msgInfo
= NULL
;
494 struct vmbus_channel_msginfo
*subMsgInfo
;
496 struct list_head
*curr
;
501 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
502 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
504 ret
= VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
508 msgInfo
->WaitEvent
= osd_WaitEventCreate();
509 if (!msgInfo
->WaitEvent
) {
514 gpadlMsg
= (struct vmbus_channel_gpadl_header
*)msgInfo
->Msg
;
515 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
516 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
517 gpadlMsg
->Gpadl
= nextGpadlHandle
;
519 DumpGpadlHeader(gpadlMsg
);
521 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
522 list_add_tail(&msgInfo
->MsgListEntry
,
523 &gVmbusConnection
.ChannelMsgList
);
525 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
526 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d",
527 Kbuffer
, Size
, msgCount
);
529 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd",
530 msgInfo
->MessageSize
- sizeof(*msgInfo
));
532 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
-
535 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
540 list_for_each(curr
, &msgInfo
->SubMsgList
) {
542 /* FIXME: should this use list_entry() instead ? */
543 subMsgInfo
= (struct vmbus_channel_msginfo
*)curr
;
545 (struct vmbus_channel_gpadl_body
*)subMsgInfo
->Msg
;
547 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
548 gpadlBody
->Gpadl
= nextGpadlHandle
;
550 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd",
551 subMsgInfo
->MessageSize
-
552 sizeof(*subMsgInfo
));
554 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
-
555 sizeof(*subMsgInfo
));
556 ret
= VmbusPostMessage(gpadlBody
,
557 subMsgInfo
->MessageSize
-
558 sizeof(*subMsgInfo
));
564 osd_WaitEventWait(msgInfo
->WaitEvent
);
566 /* At this point, we received the gpadl created msg */
567 DPRINT_DBG(VMBUS
, "Received GPADL created "
568 "(relid %d, status %d handle %x)",
569 Channel
->OfferMsg
.ChildRelId
,
570 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
573 *GpadlHandle
= gpadlMsg
->Gpadl
;
576 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
577 list_del(&msgInfo
->MsgListEntry
);
578 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
580 kfree(msgInfo
->WaitEvent
);
586 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
588 int VmbusChannelTeardownGpadl(struct vmbus_channel
*Channel
, u32 GpadlHandle
)
590 struct vmbus_channel_gpadl_teardown
*msg
;
591 struct vmbus_channel_msginfo
*info
;
595 /* ASSERT(GpadlHandle != 0); */
597 info
= kmalloc(sizeof(*info
) +
598 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
602 info
->WaitEvent
= osd_WaitEventCreate();
603 if (!info
->WaitEvent
) {
608 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->Msg
;
610 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
611 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
612 msg
->Gpadl
= GpadlHandle
;
614 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
615 list_add_tail(&info
->MsgListEntry
,
616 &gVmbusConnection
.ChannelMsgList
);
617 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
619 ret
= VmbusPostMessage(msg
,
620 sizeof(struct vmbus_channel_gpadl_teardown
));
626 osd_WaitEventWait(info
->WaitEvent
);
628 /* Received a torndown response */
629 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
630 list_del(&info
->MsgListEntry
);
631 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
633 kfree(info
->WaitEvent
);
639 * VmbusChannelClose - Close the specified channel
641 void VmbusChannelClose(struct vmbus_channel
*Channel
)
643 struct vmbus_channel_close_channel
*msg
;
644 struct vmbus_channel_msginfo
*info
;
648 /* Stop callback and cancel the timer asap */
649 Channel
->OnChannelCallback
= NULL
;
650 del_timer_sync(&Channel
->poll_timer
);
652 /* Send a closing message */
653 info
= kmalloc(sizeof(*info
) +
654 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
655 /* FIXME: can't do anything other than return here because the
656 * function is void */
660 /* info->waitEvent = osd_WaitEventCreate(); */
662 msg
= (struct vmbus_channel_close_channel
*)info
->Msg
;
663 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
664 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
666 ret
= VmbusPostMessage(msg
, sizeof(struct vmbus_channel_close_channel
));
672 /* Tear down the gpadl for the channel's ring buffer */
673 if (Channel
->RingBufferGpadlHandle
)
674 VmbusChannelTeardownGpadl(Channel
,
675 Channel
->RingBufferGpadlHandle
);
677 /* TODO: Send a msg to release the childRelId */
679 /* Cleanup the ring buffers for this channel */
680 RingBufferCleanup(&Channel
->Outbound
);
681 RingBufferCleanup(&Channel
->Inbound
);
683 osd_PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
688 * If we are closing the channel during an error path in
689 * opening the channel, don't free the channel since the
690 * caller will free the channel
693 if (Channel
->State
== CHANNEL_OPEN_STATE
) {
694 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
695 list_del(&Channel
->ListEntry
);
696 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
698 FreeVmbusChannel(Channel
);
703 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
704 * @Channel: Pointer to vmbus_channel structure.
705 * @Buffer: Pointer to the buffer you want to receive the data into.
706 * @BufferLen: Maximum size of what the the buffer will hold
707 * @RequestId: Identifier of the request
708 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
711 * Sends data in @Buffer directly to hyper-v via the vmbus
712 * This will send the data unparsed to hyper-v.
714 * Mainly used by Hyper-V drivers.
716 int VmbusChannelSendPacket(struct vmbus_channel
*Channel
, const void *Buffer
,
717 u32 BufferLen
, u64 RequestId
,
718 enum vmbus_packet_type Type
, u32 Flags
)
720 struct vmpacket_descriptor desc
;
721 u32 packetLen
= sizeof(struct vmpacket_descriptor
) + BufferLen
;
722 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
723 struct scatterlist bufferList
[3];
727 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d",
728 Channel
, Buffer
, BufferLen
);
730 DumpVmbusChannel(Channel
);
732 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
734 /* Setup the descriptor */
735 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
736 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
737 /* in 8-bytes granularity */
738 desc
.DataOffset8
= sizeof(struct vmpacket_descriptor
) >> 3;
739 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
740 desc
.TransactionId
= RequestId
;
742 sg_init_table(bufferList
, 3);
743 sg_set_buf(&bufferList
[0], &desc
, sizeof(struct vmpacket_descriptor
));
744 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
745 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
747 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
749 /* TODO: We should determine if this is optional */
750 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
751 VmbusChannelSetEvent(Channel
);
755 EXPORT_SYMBOL(VmbusChannelSendPacket
);
758 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
759 * packets using a GPADL Direct packet type.
761 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel
*Channel
,
762 struct hv_page_buffer PageBuffers
[],
763 u32 PageCount
, void *Buffer
, u32 BufferLen
,
768 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
771 u32 packetLenAligned
;
772 struct scatterlist bufferList
[3];
775 if (PageCount
> MAX_PAGE_BUFFER_COUNT
)
778 DumpVmbusChannel(Channel
);
781 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
782 * largest size we support
784 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) -
785 ((MAX_PAGE_BUFFER_COUNT
- PageCount
) *
786 sizeof(struct hv_page_buffer
));
787 packetLen
= descSize
+ BufferLen
;
788 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
790 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
792 /* Setup the descriptor */
793 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
794 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
795 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
796 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
797 desc
.TransactionId
= RequestId
;
798 desc
.RangeCount
= PageCount
;
800 for (i
= 0; i
< PageCount
; i
++) {
801 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
802 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
803 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
806 sg_init_table(bufferList
, 3);
807 sg_set_buf(&bufferList
[0], &desc
, descSize
);
808 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
809 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
811 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
813 /* TODO: We should determine if this is optional */
814 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
815 VmbusChannelSetEvent(Channel
);
821 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
822 * using a GPADL Direct packet type.
824 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel
*Channel
,
825 struct hv_multipage_buffer
*MultiPageBuffer
,
826 void *Buffer
, u32 BufferLen
, u64 RequestId
)
829 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
832 u32 packetLenAligned
;
833 struct scatterlist bufferList
[3];
835 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
,
836 MultiPageBuffer
->Length
);
838 DumpVmbusChannel(Channel
);
840 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u",
841 MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
843 if ((PfnCount
< 0) || (PfnCount
> MAX_MULTIPAGE_BUFFER_COUNT
))
847 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
848 * the largest size we support
850 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) -
851 ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
) *
853 packetLen
= descSize
+ BufferLen
;
854 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
856 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
858 /* Setup the descriptor */
859 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
860 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
861 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
862 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
863 desc
.TransactionId
= RequestId
;
866 desc
.Range
.Length
= MultiPageBuffer
->Length
;
867 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
869 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
,
870 PfnCount
* sizeof(u64
));
872 sg_init_table(bufferList
, 3);
873 sg_set_buf(&bufferList
[0], &desc
, descSize
);
874 sg_set_buf(&bufferList
[1], Buffer
, BufferLen
);
875 sg_set_buf(&bufferList
[2], &alignedData
, packetLenAligned
- packetLen
);
877 ret
= RingBufferWrite(&Channel
->Outbound
, bufferList
, 3);
879 /* TODO: We should determine if this is optional */
880 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
881 VmbusChannelSetEvent(Channel
);
888 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
889 * @Channel: Pointer to vmbus_channel structure.
890 * @Buffer: Pointer to the buffer you want to receive the data into.
891 * @BufferLen: Maximum size of what the the buffer will hold
892 * @BufferActualLen: The actual size of the data after it was received
893 * @RequestId: Identifier of the request
895 * Receives directly from the hyper-v vmbus and puts the data it received
896 * into Buffer. This will receive the data unparsed from hyper-v.
898 * Mainly used by Hyper-V drivers.
900 int VmbusChannelRecvPacket(struct vmbus_channel
*Channel
, void *Buffer
,
901 u32 BufferLen
, u32
*BufferActualLen
, u64
*RequestId
)
903 struct vmpacket_descriptor desc
;
909 *BufferActualLen
= 0;
912 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
914 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
915 sizeof(struct vmpacket_descriptor
));
917 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
919 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
923 /* VmbusChannelClearEvent(Channel); */
925 packetLen
= desc
.Length8
<< 3;
926 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
927 /* ASSERT(userLen > 0); */
929 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
930 "flag %d tid %llx pktlen %d datalen %d> ",
931 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
932 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
934 *BufferActualLen
= userLen
;
936 if (userLen
> BufferLen
) {
937 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
939 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d",
944 *RequestId
= desc
.TransactionId
;
946 /* Copy over the packet to the user buffer */
947 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
,
948 (desc
.DataOffset8
<< 3));
950 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
954 EXPORT_SYMBOL(VmbusChannelRecvPacket
);
957 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
959 int VmbusChannelRecvPacketRaw(struct vmbus_channel
*Channel
, void *Buffer
,
960 u32 BufferLen
, u32
*BufferActualLen
,
963 struct vmpacket_descriptor desc
;
969 *BufferActualLen
= 0;
972 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
974 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
,
975 sizeof(struct vmpacket_descriptor
));
977 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
979 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
983 /* VmbusChannelClearEvent(Channel); */
985 packetLen
= desc
.Length8
<< 3;
986 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
988 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
989 "flag %d tid %llx pktlen %d datalen %d> ",
990 Channel
, Channel
->OfferMsg
.ChildRelId
, desc
.Type
,
991 desc
.Flags
, desc
.TransactionId
, packetLen
, userLen
);
993 *BufferActualLen
= packetLen
;
995 if (packetLen
> BufferLen
) {
996 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
998 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but "
999 "got space for only %d bytes", packetLen
, BufferLen
);
1003 *RequestId
= desc
.TransactionId
;
1005 /* Copy over the entire packet to the user buffer */
1006 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
1008 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1013 * VmbusChannelOnChannelEvent - Channel event callback
1015 void VmbusChannelOnChannelEvent(struct vmbus_channel
*Channel
)
1017 DumpVmbusChannel(Channel
);
1018 /* ASSERT(Channel->OnChannelCallback); */
1020 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1022 mod_timer(&Channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
1026 * VmbusChannelOnTimer - Timer event callback
1028 void VmbusChannelOnTimer(unsigned long data
)
1030 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
1032 if (channel
->OnChannelCallback
)
1033 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1037 * DumpVmbusChannel - Dump vmbus channel info to the console
1039 static void DumpVmbusChannel(struct vmbus_channel
*Channel
)
1041 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1042 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1043 DumpRingInfo(&Channel
->Inbound
, "Inbound ");