Merge branch 'mini2440-dev-unlikely' into mini2440-dev
[linux-2.6/mini2440.git] / drivers / staging / hv / Channel.c
blobd649ee169d953ae3129c0ff377dd5ee866ed968f
1 /*
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
11 * more details.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include "osd.h"
24 #include "logging.h"
25 #include "VmbusPrivate.h"
27 /* Internal routines */
28 static int VmbusChannelCreateGpadlHeader(
29 void *Kbuffer, /* must be phys and virt contiguous */
30 u32 Size, /* page-size multiple */
31 struct vmbus_channel_msginfo **msgInfo,
32 u32 *MessageCount);
33 static void DumpVmbusChannel(struct vmbus_channel *channel);
34 static void VmbusChannelSetEvent(struct vmbus_channel *channel);
37 #if 0
38 static void DumpMonitorPage(struct hv_monitor_page *MonitorPage)
40 int i = 0;
41 int j = 0;
43 DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d",
44 MonitorPage, MonitorPage->TriggerState);
46 for (i = 0; i < 4; i++)
47 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i,
48 MonitorPage->TriggerGroup[i].AsUINT64);
50 for (i = 0; i < 4; i++) {
51 for (j = 0; j < 32; j++) {
52 DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j,
53 MonitorPage->Latency[i][j]);
56 for (i = 0; i < 4; i++) {
57 for (j = 0; j < 32; j++) {
58 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j,
59 MonitorPage->Parameter[i][j].ConnectionId.Asu32);
60 DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j,
61 MonitorPage->Parameter[i][j].FlagNumber);
65 #endif
67 /**
68 * VmbusChannelSetEvent - Trigger an event notification on the specified channel.
70 static void VmbusChannelSetEvent(struct vmbus_channel *Channel)
72 struct hv_monitor_page *monitorPage;
74 DPRINT_ENTER(VMBUS);
76 if (Channel->OfferMsg.MonitorAllocated) {
77 /* Each u32 represents 32 channels */
78 set_bit(Channel->OfferMsg.ChildRelId & 31,
79 (unsigned long *) gVmbusConnection.SendInterruptPage +
80 (Channel->OfferMsg.ChildRelId >> 5));
82 monitorPage = gVmbusConnection.MonitorPages;
83 monitorPage++; /* Get the child to parent monitor page */
85 set_bit(Channel->MonitorBit,
86 (unsigned long *)&monitorPage->TriggerGroup
87 [Channel->MonitorGroup].Pending);
89 } else {
90 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
93 DPRINT_EXIT(VMBUS);
96 #if 0
97 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
99 struct hv_monitor_page *monitorPage;
101 DPRINT_ENTER(VMBUS);
103 if (Channel->OfferMsg.MonitorAllocated) {
104 /* Each u32 represents 32 channels */
105 clear_bit(Channel->OfferMsg.ChildRelId & 31,
106 (unsigned long *)gVmbusConnection.SendInterruptPage +
107 (Channel->OfferMsg.ChildRelId >> 5));
109 monitorPage =
110 (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
111 monitorPage++; /* Get the child to parent monitor page */
113 clear_bit(Channel->MonitorBit,
114 (unsigned long *)&monitorPage->TriggerGroup
115 [Channel->MonitorGroup].Pending);
118 DPRINT_EXIT(VMBUS);
121 #endif
123 * VmbusChannelGetDebugInfo -Retrieve various channel debug info
125 void VmbusChannelGetDebugInfo(struct vmbus_channel *Channel,
126 struct vmbus_channel_debug_info *DebugInfo)
128 struct hv_monitor_page *monitorPage;
129 u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
130 u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
131 /* u32 monitorBit = 1 << monitorOffset; */
133 DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
134 DebugInfo->State = Channel->State;
135 memcpy(&DebugInfo->InterfaceType,
136 &Channel->OfferMsg.Offer.InterfaceType, sizeof(struct hv_guid));
137 memcpy(&DebugInfo->InterfaceInstance,
138 &Channel->OfferMsg.Offer.InterfaceInstance,
139 sizeof(struct hv_guid));
141 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
143 DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
145 DebugInfo->ServerMonitorPending =
146 monitorPage->TriggerGroup[monitorGroup].Pending;
147 DebugInfo->ServerMonitorLatency =
148 monitorPage->Latency[monitorGroup][monitorOffset];
149 DebugInfo->ServerMonitorConnectionId =
150 monitorPage->Parameter[monitorGroup]
151 [monitorOffset].ConnectionId.u.Id;
153 monitorPage++;
155 DebugInfo->ClientMonitorPending =
156 monitorPage->TriggerGroup[monitorGroup].Pending;
157 DebugInfo->ClientMonitorLatency =
158 monitorPage->Latency[monitorGroup][monitorOffset];
159 DebugInfo->ClientMonitorConnectionId =
160 monitorPage->Parameter[monitorGroup]
161 [monitorOffset].ConnectionId.u.Id;
163 RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
164 RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
168 * VmbusChannelOpen - Open the specified channel.
170 int VmbusChannelOpen(struct vmbus_channel *NewChannel, u32 SendRingBufferSize,
171 u32 RecvRingBufferSize, void *UserData, u32 UserDataLen,
172 void (*OnChannelCallback)(void *context), void *Context)
174 struct vmbus_channel_open_channel *openMsg;
175 struct vmbus_channel_msginfo *openInfo;
176 void *in, *out;
177 unsigned long flags;
178 int ret;
180 DPRINT_ENTER(VMBUS);
182 /* Aligned to page size */
183 ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1)));
184 ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1)));
186 NewChannel->OnChannelCallback = OnChannelCallback;
187 NewChannel->ChannelCallbackContext = Context;
189 /* Allocate the ring buffer */
190 out = osd_PageAlloc((SendRingBufferSize + RecvRingBufferSize)
191 >> PAGE_SHIFT);
192 ASSERT(out);
193 ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0);
195 in = (void *)((unsigned long)out + SendRingBufferSize);
197 NewChannel->RingBufferPages = out;
198 NewChannel->RingBufferPageCount = (SendRingBufferSize +
199 RecvRingBufferSize) >> PAGE_SHIFT;
201 RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
203 RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
205 /* Establish the gpadl for the ring buffer */
206 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...",
207 NewChannel);
209 NewChannel->RingBufferGpadlHandle = 0;
211 ret = VmbusChannelEstablishGpadl(NewChannel,
212 NewChannel->Outbound.RingBuffer,
213 SendRingBufferSize +
214 RecvRingBufferSize,
215 &NewChannel->RingBufferGpadlHandle);
217 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p "
218 "size %d recv ring %p size %d, downstreamoffset %d>",
219 NewChannel, NewChannel->OfferMsg.ChildRelId,
220 NewChannel->RingBufferGpadlHandle,
221 NewChannel->Outbound.RingBuffer,
222 NewChannel->Outbound.RingSize,
223 NewChannel->Inbound.RingBuffer,
224 NewChannel->Inbound.RingSize,
225 SendRingBufferSize);
227 /* Create and init the channel open message */
228 openInfo = kmalloc(sizeof(*openInfo) +
229 sizeof(struct vmbus_channel_open_channel),
230 GFP_KERNEL);
231 ASSERT(openInfo != NULL);
233 openInfo->WaitEvent = osd_WaitEventCreate();
235 openMsg = (struct vmbus_channel_open_channel *)openInfo->Msg;
236 openMsg->Header.MessageType = ChannelMessageOpenChannel;
237 openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; /* FIXME */
238 openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
239 openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
240 ASSERT(openMsg->RingBufferGpadlHandle);
241 openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >>
242 PAGE_SHIFT;
243 openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
245 ASSERT(UserDataLen <= MAX_USER_DEFINED_BYTES);
246 if (UserDataLen)
247 memcpy(openMsg->UserData, UserData, UserDataLen);
249 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
250 list_add_tail(&openInfo->MsgListEntry,
251 &gVmbusConnection.ChannelMsgList);
252 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
254 DPRINT_DBG(VMBUS, "Sending channel open msg...");
256 ret = VmbusPostMessage(openMsg,
257 sizeof(struct vmbus_channel_open_channel));
258 if (ret != 0) {
259 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
260 goto Cleanup;
263 /* FIXME: Need to time-out here */
264 osd_WaitEventWait(openInfo->WaitEvent);
266 if (openInfo->Response.OpenResult.Status == 0)
267 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
268 else
269 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!",
270 NewChannel, openInfo->Response.OpenResult.Status);
272 Cleanup:
273 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
274 list_del(&openInfo->MsgListEntry);
275 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
277 kfree(openInfo->WaitEvent);
278 kfree(openInfo);
280 DPRINT_EXIT(VMBUS);
282 return 0;
286 * DumpGpadlBody - Dump the gpadl body message to the console for debugging purposes.
288 static void DumpGpadlBody(struct vmbus_channel_gpadl_body *Gpadl, u32 Len)
290 int i;
291 int pfnCount;
293 pfnCount = (Len - sizeof(struct vmbus_channel_gpadl_body)) /
294 sizeof(u64);
295 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
297 for (i = 0; i < pfnCount; i++)
298 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu",
299 i, Gpadl->Pfn[i]);
303 * DumpGpadlHeader - Dump the gpadl header message to the console for debugging purposes.
305 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header *Gpadl)
307 int i, j;
308 int pageCount;
310 DPRINT_DBG(VMBUS,
311 "gpadl header - relid %d, range count %d, range buflen %d",
312 Gpadl->ChildRelId, Gpadl->RangeCount, Gpadl->RangeBufLen);
313 for (i = 0; i < Gpadl->RangeCount; i++) {
314 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
315 pageCount = (pageCount > 26) ? 26 : pageCount;
317 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
318 "page count %d", i, Gpadl->Range[i].ByteCount,
319 Gpadl->Range[i].ByteOffset, pageCount);
321 for (j = 0; j < pageCount; j++)
322 DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
323 Gpadl->Range[i].PfnArray[j]);
328 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
330 static int VmbusChannelCreateGpadlHeader(void *Kbuffer, u32 Size,
331 struct vmbus_channel_msginfo **MsgInfo,
332 u32 *MessageCount)
334 int i;
335 int pageCount;
336 unsigned long long pfn;
337 struct vmbus_channel_gpadl_header *gpaHeader;
338 struct vmbus_channel_gpadl_body *gpadlBody;
339 struct vmbus_channel_msginfo *msgHeader;
340 struct vmbus_channel_msginfo *msgBody;
341 u32 msgSize;
343 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
345 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
346 ASSERT((Size & (PAGE_SIZE-1)) == 0);
348 pageCount = Size >> PAGE_SHIFT;
349 pfn = virt_to_phys(Kbuffer) >> PAGE_SHIFT;
351 /* do we need a gpadl body msg */
352 pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
353 sizeof(struct vmbus_channel_gpadl_header) -
354 sizeof(struct gpa_range);
355 pfnCount = pfnSize / sizeof(u64);
357 if (pageCount > pfnCount) {
358 /* we need a gpadl body */
359 /* fill in the header */
360 msgSize = sizeof(struct vmbus_channel_msginfo) +
361 sizeof(struct vmbus_channel_gpadl_header) +
362 sizeof(struct gpa_range) + pfnCount * sizeof(u64);
363 msgHeader = kzalloc(msgSize, GFP_KERNEL);
365 INIT_LIST_HEAD(&msgHeader->SubMsgList);
366 msgHeader->MessageSize = msgSize;
368 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
369 gpaHeader->RangeCount = 1;
370 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
371 pageCount * sizeof(u64);
372 gpaHeader->Range[0].ByteOffset = 0;
373 gpaHeader->Range[0].ByteCount = Size;
374 for (i = 0; i < pfnCount; i++)
375 gpaHeader->Range[0].PfnArray[i] = pfn+i;
376 *MsgInfo = msgHeader;
377 *MessageCount = 1;
379 pfnSum = pfnCount;
380 pfnLeft = pageCount - pfnCount;
382 /* how many pfns can we fit */
383 pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
384 sizeof(struct vmbus_channel_gpadl_body);
385 pfnCount = pfnSize / sizeof(u64);
387 /* fill in the body */
388 while (pfnLeft) {
389 if (pfnLeft > pfnCount)
390 pfnCurr = pfnCount;
391 else
392 pfnCurr = pfnLeft;
394 msgSize = sizeof(struct vmbus_channel_msginfo) +
395 sizeof(struct vmbus_channel_gpadl_body) +
396 pfnCurr * sizeof(u64);
397 msgBody = kzalloc(msgSize, GFP_KERNEL);
398 ASSERT(msgBody);
399 msgBody->MessageSize = msgSize;
400 (*MessageCount)++;
401 gpadlBody =
402 (struct vmbus_channel_gpadl_body *)msgBody->Msg;
405 * FIXME:
406 * Gpadl is u32 and we are using a pointer which could
407 * be 64-bit
409 /* gpadlBody->Gpadl = kbuffer; */
410 for (i = 0; i < pfnCurr; i++)
411 gpadlBody->Pfn[i] = pfn + pfnSum + i;
413 /* add to msg header */
414 list_add_tail(&msgBody->MsgListEntry,
415 &msgHeader->SubMsgList);
416 pfnSum += pfnCurr;
417 pfnLeft -= pfnCurr;
419 } else {
420 /* everything fits in a header */
421 msgSize = sizeof(struct vmbus_channel_msginfo) +
422 sizeof(struct vmbus_channel_gpadl_header) +
423 sizeof(struct gpa_range) + pageCount * sizeof(u64);
424 msgHeader = kzalloc(msgSize, GFP_KERNEL);
425 msgHeader->MessageSize = msgSize;
427 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
428 gpaHeader->RangeCount = 1;
429 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
430 pageCount * sizeof(u64);
431 gpaHeader->Range[0].ByteOffset = 0;
432 gpaHeader->Range[0].ByteCount = Size;
433 for (i = 0; i < pageCount; i++)
434 gpaHeader->Range[0].PfnArray[i] = pfn+i;
436 *MsgInfo = msgHeader;
437 *MessageCount = 1;
440 return 0;
444 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
446 * @Channel: a channel
447 * @Kbuffer: from kmalloc
448 * @Size: page-size multiple
449 * @GpadlHandle: some funky thing
451 int VmbusChannelEstablishGpadl(struct vmbus_channel *Channel, void *Kbuffer,
452 u32 Size, u32 *GpadlHandle)
454 struct vmbus_channel_gpadl_header *gpadlMsg;
455 struct vmbus_channel_gpadl_body *gpadlBody;
456 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
457 struct vmbus_channel_msginfo *msgInfo;
458 struct vmbus_channel_msginfo *subMsgInfo;
459 u32 msgCount;
460 struct list_head *curr;
461 u32 nextGpadlHandle;
462 unsigned long flags;
463 int ret;
465 DPRINT_ENTER(VMBUS);
467 nextGpadlHandle = atomic_read(&gVmbusConnection.NextGpadlHandle);
468 atomic_inc(&gVmbusConnection.NextGpadlHandle);
470 VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
471 ASSERT(msgInfo != NULL);
472 ASSERT(msgCount > 0);
474 msgInfo->WaitEvent = osd_WaitEventCreate();
475 gpadlMsg = (struct vmbus_channel_gpadl_header *)msgInfo->Msg;
476 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
477 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
478 gpadlMsg->Gpadl = nextGpadlHandle;
480 DumpGpadlHeader(gpadlMsg);
482 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
483 list_add_tail(&msgInfo->MsgListEntry,
484 &gVmbusConnection.ChannelMsgList);
486 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
487 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d",
488 Kbuffer, Size, msgCount);
490 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd",
491 msgInfo->MessageSize - sizeof(*msgInfo));
493 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize -
494 sizeof(*msgInfo));
495 if (ret != 0) {
496 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
497 goto Cleanup;
500 if (msgCount > 1) {
501 list_for_each(curr, &msgInfo->SubMsgList) {
503 /* FIXME: should this use list_entry() instead ? */
504 subMsgInfo = (struct vmbus_channel_msginfo *)curr;
505 gpadlBody =
506 (struct vmbus_channel_gpadl_body *)subMsgInfo->Msg;
508 gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
509 gpadlBody->Gpadl = nextGpadlHandle;
511 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %zd",
512 subMsgInfo->MessageSize -
513 sizeof(*subMsgInfo));
515 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize -
516 sizeof(*subMsgInfo));
517 ret = VmbusPostMessage(gpadlBody,
518 subMsgInfo->MessageSize -
519 sizeof(*subMsgInfo));
520 ASSERT(ret == 0);
523 osd_WaitEventWait(msgInfo->WaitEvent);
525 /* At this point, we received the gpadl created msg */
526 DPRINT_DBG(VMBUS, "Received GPADL created "
527 "(relid %d, status %d handle %x)",
528 Channel->OfferMsg.ChildRelId,
529 msgInfo->Response.GpadlCreated.CreationStatus,
530 gpadlMsg->Gpadl);
532 *GpadlHandle = gpadlMsg->Gpadl;
534 Cleanup:
535 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
536 list_del(&msgInfo->MsgListEntry);
537 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
539 kfree(msgInfo->WaitEvent);
540 kfree(msgInfo);
542 DPRINT_EXIT(VMBUS);
544 return ret;
548 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
550 int VmbusChannelTeardownGpadl(struct vmbus_channel *Channel, u32 GpadlHandle)
552 struct vmbus_channel_gpadl_teardown *msg;
553 struct vmbus_channel_msginfo *info;
554 unsigned long flags;
555 int ret;
557 DPRINT_ENTER(VMBUS);
559 ASSERT(GpadlHandle != 0);
561 info = kmalloc(sizeof(*info) +
562 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
563 ASSERT(info != NULL);
565 info->WaitEvent = osd_WaitEventCreate();
567 msg = (struct vmbus_channel_gpadl_teardown *)info->Msg;
569 msg->Header.MessageType = ChannelMessageGpadlTeardown;
570 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
571 msg->Gpadl = GpadlHandle;
573 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
574 list_add_tail(&info->MsgListEntry,
575 &gVmbusConnection.ChannelMsgList);
576 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
578 ret = VmbusPostMessage(msg,
579 sizeof(struct vmbus_channel_gpadl_teardown));
580 if (ret != 0) {
581 /* TODO: */
582 /* something... */
585 osd_WaitEventWait(info->WaitEvent);
587 /* Received a torndown response */
588 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
589 list_del(&info->MsgListEntry);
590 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
592 kfree(info->WaitEvent);
593 kfree(info);
595 DPRINT_EXIT(VMBUS);
597 return ret;
601 * VmbusChannelClose - Close the specified channel
603 void VmbusChannelClose(struct vmbus_channel *Channel)
605 struct vmbus_channel_close_channel *msg;
606 struct vmbus_channel_msginfo *info;
607 unsigned long flags;
608 int ret;
610 DPRINT_ENTER(VMBUS);
612 /* Stop callback and cancel the timer asap */
613 Channel->OnChannelCallback = NULL;
614 del_timer(&Channel->poll_timer);
616 /* Send a closing message */
617 info = kmalloc(sizeof(*info) +
618 sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
619 ASSERT(info != NULL);
621 /* info->waitEvent = osd_WaitEventCreate(); */
623 msg = (struct vmbus_channel_close_channel *)info->Msg;
624 msg->Header.MessageType = ChannelMessageCloseChannel;
625 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
627 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_close_channel));
628 if (ret != 0) {
629 /* TODO: */
630 /* something... */
633 /* Tear down the gpadl for the channel's ring buffer */
634 if (Channel->RingBufferGpadlHandle)
635 VmbusChannelTeardownGpadl(Channel,
636 Channel->RingBufferGpadlHandle);
638 /* TODO: Send a msg to release the childRelId */
640 /* Cleanup the ring buffers for this channel */
641 RingBufferCleanup(&Channel->Outbound);
642 RingBufferCleanup(&Channel->Inbound);
644 osd_PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
646 kfree(info);
649 * If we are closing the channel during an error path in
650 * opening the channel, don't free the channel since the
651 * caller will free the channel
654 if (Channel->State == CHANNEL_OPEN_STATE) {
655 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
656 list_del(&Channel->ListEntry);
657 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
659 FreeVmbusChannel(Channel);
662 DPRINT_EXIT(VMBUS);
666 * VmbusChannelSendPacket - Send the specified buffer on the given channel
668 int VmbusChannelSendPacket(struct vmbus_channel *Channel, const void *Buffer,
669 u32 BufferLen, u64 RequestId,
670 enum vmbus_packet_type Type, u32 Flags)
672 struct vmpacket_descriptor desc;
673 u32 packetLen = sizeof(struct vmpacket_descriptor) + BufferLen;
674 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
675 struct scatterlist bufferList[3];
676 u64 alignedData = 0;
677 int ret;
679 DPRINT_ENTER(VMBUS);
680 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d",
681 Channel, Buffer, BufferLen);
683 DumpVmbusChannel(Channel);
685 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
687 /* Setup the descriptor */
688 desc.Type = Type; /* VmbusPacketTypeDataInBand; */
689 desc.Flags = Flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
690 /* in 8-bytes granularity */
691 desc.DataOffset8 = sizeof(struct vmpacket_descriptor) >> 3;
692 desc.Length8 = (u16)(packetLenAligned >> 3);
693 desc.TransactionId = RequestId;
695 sg_init_table(bufferList, 3);
696 sg_set_buf(&bufferList[0], &desc, sizeof(struct vmpacket_descriptor));
697 sg_set_buf(&bufferList[1], Buffer, BufferLen);
698 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
700 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
702 /* TODO: We should determine if this is optional */
703 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
704 VmbusChannelSetEvent(Channel);
706 DPRINT_EXIT(VMBUS);
708 return ret;
712 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer packets using a GPADL Direct packet type.
714 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel *Channel,
715 struct hv_page_buffer PageBuffers[],
716 u32 PageCount, void *Buffer, u32 BufferLen,
717 u64 RequestId)
719 int ret;
720 int i;
721 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
722 u32 descSize;
723 u32 packetLen;
724 u32 packetLenAligned;
725 struct scatterlist bufferList[3];
726 u64 alignedData = 0;
728 DPRINT_ENTER(VMBUS);
730 ASSERT(PageCount <= MAX_PAGE_BUFFER_COUNT);
732 DumpVmbusChannel(Channel);
735 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
736 * largest size we support
738 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER) -
739 ((MAX_PAGE_BUFFER_COUNT - PageCount) *
740 sizeof(struct hv_page_buffer));
741 packetLen = descSize + BufferLen;
742 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
744 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
746 /* Setup the descriptor */
747 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
748 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
749 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
750 desc.Length8 = (u16)(packetLenAligned >> 3);
751 desc.TransactionId = RequestId;
752 desc.RangeCount = PageCount;
754 for (i = 0; i < PageCount; i++) {
755 desc.Range[i].Length = PageBuffers[i].Length;
756 desc.Range[i].Offset = PageBuffers[i].Offset;
757 desc.Range[i].Pfn = PageBuffers[i].Pfn;
760 sg_init_table(bufferList, 3);
761 sg_set_buf(&bufferList[0], &desc, descSize);
762 sg_set_buf(&bufferList[1], Buffer, BufferLen);
763 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
765 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
767 /* TODO: We should determine if this is optional */
768 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
769 VmbusChannelSetEvent(Channel);
771 DPRINT_EXIT(VMBUS);
773 return ret;
777 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet using a GPADL Direct packet type.
779 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel *Channel,
780 struct hv_multipage_buffer *MultiPageBuffer,
781 void *Buffer, u32 BufferLen, u64 RequestId)
783 int ret;
784 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
785 u32 descSize;
786 u32 packetLen;
787 u32 packetLenAligned;
788 struct scatterlist bufferList[3];
789 u64 alignedData = 0;
790 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset,
791 MultiPageBuffer->Length);
793 DPRINT_ENTER(VMBUS);
795 DumpVmbusChannel(Channel);
797 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u",
798 MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
800 ASSERT(PfnCount > 0);
801 ASSERT(PfnCount <= MAX_MULTIPAGE_BUFFER_COUNT);
804 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
805 * the largest size we support
807 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) -
808 ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount) *
809 sizeof(u64));
810 packetLen = descSize + BufferLen;
811 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
813 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
815 /* Setup the descriptor */
816 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
817 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
818 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
819 desc.Length8 = (u16)(packetLenAligned >> 3);
820 desc.TransactionId = RequestId;
821 desc.RangeCount = 1;
823 desc.Range.Length = MultiPageBuffer->Length;
824 desc.Range.Offset = MultiPageBuffer->Offset;
826 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray,
827 PfnCount * sizeof(u64));
829 sg_init_table(bufferList, 3);
830 sg_set_buf(&bufferList[0], &desc, descSize);
831 sg_set_buf(&bufferList[1], Buffer, BufferLen);
832 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
834 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
836 /* TODO: We should determine if this is optional */
837 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
838 VmbusChannelSetEvent(Channel);
840 DPRINT_EXIT(VMBUS);
842 return ret;
846 * VmbusChannelRecvPacket - Retrieve the user packet on the specified channel
848 /* TODO: Do we ever receive a gpa direct packet other than the ones we send ? */
849 int VmbusChannelRecvPacket(struct vmbus_channel *Channel, void *Buffer,
850 u32 BufferLen, u32 *BufferActualLen, u64 *RequestId)
852 struct vmpacket_descriptor desc;
853 u32 packetLen;
854 u32 userLen;
855 int ret;
856 unsigned long flags;
858 DPRINT_ENTER(VMBUS);
860 *BufferActualLen = 0;
861 *RequestId = 0;
863 spin_lock_irqsave(&Channel->inbound_lock, flags);
865 ret = RingBufferPeek(&Channel->Inbound, &desc,
866 sizeof(struct vmpacket_descriptor));
867 if (ret != 0) {
868 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
870 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
871 DPRINT_EXIT(VMBUS);
872 return 0;
875 /* VmbusChannelClearEvent(Channel); */
877 packetLen = desc.Length8 << 3;
878 userLen = packetLen - (desc.DataOffset8 << 3);
879 /* ASSERT(userLen > 0); */
881 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
882 "flag %d tid %llx pktlen %d datalen %d> ",
883 Channel, Channel->OfferMsg.ChildRelId, desc.Type,
884 desc.Flags, desc.TransactionId, packetLen, userLen);
886 *BufferActualLen = userLen;
888 if (userLen > BufferLen) {
889 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
891 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d",
892 BufferLen, userLen);
893 DPRINT_EXIT(VMBUS);
895 return -1;
898 *RequestId = desc.TransactionId;
900 /* Copy over the packet to the user buffer */
901 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen,
902 (desc.DataOffset8 << 3));
904 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
906 DPRINT_EXIT(VMBUS);
908 return 0;
912 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
914 int VmbusChannelRecvPacketRaw(struct vmbus_channel *Channel, void *Buffer,
915 u32 BufferLen, u32 *BufferActualLen,
916 u64 *RequestId)
918 struct vmpacket_descriptor desc;
919 u32 packetLen;
920 u32 userLen;
921 int ret;
922 unsigned long flags;
924 DPRINT_ENTER(VMBUS);
926 *BufferActualLen = 0;
927 *RequestId = 0;
929 spin_lock_irqsave(&Channel->inbound_lock, flags);
931 ret = RingBufferPeek(&Channel->Inbound, &desc,
932 sizeof(struct vmpacket_descriptor));
933 if (ret != 0) {
934 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
936 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
937 DPRINT_EXIT(VMBUS);
938 return 0;
941 /* VmbusChannelClearEvent(Channel); */
943 packetLen = desc.Length8 << 3;
944 userLen = packetLen - (desc.DataOffset8 << 3);
946 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
947 "flag %d tid %llx pktlen %d datalen %d> ",
948 Channel, Channel->OfferMsg.ChildRelId, desc.Type,
949 desc.Flags, desc.TransactionId, packetLen, userLen);
951 *BufferActualLen = packetLen;
953 if (packetLen > BufferLen) {
954 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
956 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but "
957 "got space for only %d bytes", packetLen, BufferLen);
958 DPRINT_EXIT(VMBUS);
959 return -2;
962 *RequestId = desc.TransactionId;
964 /* Copy over the entire packet to the user buffer */
965 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
967 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
969 DPRINT_EXIT(VMBUS);
971 return 0;
975 * VmbusChannelOnChannelEvent - Channel event callback
977 void VmbusChannelOnChannelEvent(struct vmbus_channel *Channel)
979 DumpVmbusChannel(Channel);
980 ASSERT(Channel->OnChannelCallback);
981 #ifdef ENABLE_POLLING
982 del_timer(&Channel->poll_timer);
983 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
984 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
985 add_timer(&channel->poll_timer);
986 #else
987 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
988 #endif
992 * VmbusChannelOnTimer - Timer event callback
994 void VmbusChannelOnTimer(unsigned long data)
996 struct vmbus_channel *channel = (struct vmbus_channel *)data;
998 if (channel->OnChannelCallback) {
999 channel->OnChannelCallback(channel->ChannelCallbackContext);
1000 #ifdef ENABLE_POLLING
1001 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
1002 add_timer(&channel->poll_timer);
1003 #endif
1008 * DumpVmbusChannel - Dump vmbus channel info to the console
1010 static void DumpVmbusChannel(struct vmbus_channel *Channel)
1012 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1013 DumpRingInfo(&Channel->Outbound, "Outbound ");
1014 DumpRingInfo(&Channel->Inbound, "Inbound ");