Staging: hv: clean up typedefs in ChannelMgmt.h
[deliverable/linux.git] / drivers / staging / hv / Channel.c
1 /*
2 *
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 *
22 */
23
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include "include/osd.h"
27 #include "include/logging.h"
28
29 #include "VmbusPrivate.h"
30
31 /* Internal routines */
32 static int VmbusChannelCreateGpadlHeader(
33 void * Kbuffer, /* must be phys and virt contiguous */
34 u32 Size, /* page-size multiple */
35 struct vmbus_channel_msginfo **msgInfo,
36 u32 *MessageCount
37 );
38 static void DumpVmbusChannel(struct vmbus_channel *channel);
39 static void VmbusChannelSetEvent(struct vmbus_channel *channel);
40
41
42 #if 0
43 static void
44 DumpMonitorPage(
45 HV_MONITOR_PAGE *MonitorPage
46 )
47 {
48 int i=0;
49 int j=0;
50
51 DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d", MonitorPage, MonitorPage->TriggerState);
52
53 for (i=0; i<4; i++)
54 {
55 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i, MonitorPage->TriggerGroup[i].AsUINT64);
56 }
57
58 for (i=0; i<4; i++)
59 {
60 for (j=0; j<32; j++)
61 {
62 DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j, MonitorPage->Latency[i][j]);
63 }
64 }
65 for (i=0; i<4; i++)
66 {
67 for (j=0; j<32; j++)
68 {
69 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].ConnectionId.Asu32);
70 DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].FlagNumber);
71
72 }
73 }
74 }
75 #endif
76
77 /*++
78
79 Name:
80 VmbusChannelSetEvent()
81
82 Description:
83 Trigger an event notification on the specified channel.
84
85 --*/
86 static void VmbusChannelSetEvent(struct vmbus_channel *Channel)
87 {
88 HV_MONITOR_PAGE *monitorPage;
89
90 DPRINT_ENTER(VMBUS);
91
92 if (Channel->OfferMsg.MonitorAllocated)
93 {
94 /* Each u32 represents 32 channels */
95 set_bit(Channel->OfferMsg.ChildRelId & 31,
96 (unsigned long *) gVmbusConnection.SendInterruptPage +
97 (Channel->OfferMsg.ChildRelId >> 5) );
98
99 monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
100 monitorPage++; /* Get the child to parent monitor page */
101
102 set_bit(Channel->MonitorBit,
103 (unsigned long *) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending);
104
105 }
106 else
107 {
108 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
109 }
110
111 DPRINT_EXIT(VMBUS);
112 }
113
114 #if 0
115 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
116 {
117 HV_MONITOR_PAGE *monitorPage;
118
119 DPRINT_ENTER(VMBUS);
120
121 if (Channel->OfferMsg.MonitorAllocated)
122 {
123 /* Each u32 represents 32 channels */
124 clear_bit(Channel->OfferMsg.ChildRelId & 31,
125 (unsigned long *) gVmbusConnection.SendInterruptPage + (Channel->OfferMsg.ChildRelId >> 5));
126
127 monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
128 monitorPage++; /* Get the child to parent monitor page */
129
130 clear_bit(Channel->MonitorBit,
131 (unsigned long *) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending);
132 }
133
134 DPRINT_EXIT(VMBUS);
135 }
136
137 #endif
138 /*++;
139
140 Name:
141 VmbusChannelGetDebugInfo()
142
143 Description:
144 Retrieve various channel debug info
145
146 --*/
147 void VmbusChannelGetDebugInfo(struct vmbus_channel *Channel,
148 struct vmbus_channel_debug_info *DebugInfo)
149 {
150 HV_MONITOR_PAGE *monitorPage;
151 u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
152 u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
153 /* u32 monitorBit = 1 << monitorOffset; */
154
155 DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
156 DebugInfo->State = Channel->State;
157 memcpy(&DebugInfo->InterfaceType, &Channel->OfferMsg.Offer.InterfaceType, sizeof(GUID));
158 memcpy(&DebugInfo->InterfaceInstance, &Channel->OfferMsg.Offer.InterfaceInstance, sizeof(GUID));
159
160 monitorPage = (HV_MONITOR_PAGE*)gVmbusConnection.MonitorPages;
161
162 DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
163
164 DebugInfo->ServerMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
165 DebugInfo->ServerMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
166 DebugInfo->ServerMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
167
168 monitorPage++;
169
170 DebugInfo->ClientMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
171 DebugInfo->ClientMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
172 DebugInfo->ClientMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
173
174 RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
175 RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
176 }
177
178
179 /*++;
180
181 Name:
182 VmbusChannelOpen()
183
184 Description:
185 Open the specified channel.
186
187 --*/
188 int VmbusChannelOpen(struct vmbus_channel *NewChannel,
189 u32 SendRingBufferSize,
190 u32 RecvRingBufferSize,
191 void * UserData,
192 u32 UserDataLen,
193 PFN_CHANNEL_CALLBACK pfnOnChannelCallback,
194 void * Context
195 )
196 {
197 int ret=0;
198 VMBUS_CHANNEL_OPEN_CHANNEL* openMsg;
199 struct vmbus_channel_msginfo *openInfo;
200 void *in, *out;
201 unsigned long flags;
202
203 DPRINT_ENTER(VMBUS);
204
205 /* Aligned to page size */
206 ASSERT(!(SendRingBufferSize & (PAGE_SIZE -1)));
207 ASSERT(!(RecvRingBufferSize & (PAGE_SIZE -1)));
208
209 NewChannel->OnChannelCallback = pfnOnChannelCallback;
210 NewChannel->ChannelCallbackContext = Context;
211
212 /* Allocate the ring buffer */
213 out = osd_PageAlloc((SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT);
214 /* out = kzalloc(sendRingBufferSize + recvRingBufferSize, GFP_KERNEL); */
215 ASSERT(out);
216 ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0);
217
218 in = (void*)((unsigned long)out + SendRingBufferSize);
219
220 NewChannel->RingBufferPages = out;
221 NewChannel->RingBufferPageCount = (SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT;
222
223 RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
224
225 RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
226
227 /* Establish the gpadl for the ring buffer */
228 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...", NewChannel);
229
230 NewChannel->RingBufferGpadlHandle = 0;
231
232 ret = VmbusChannelEstablishGpadl(NewChannel,
233 NewChannel->Outbound.RingBuffer,
234 SendRingBufferSize + RecvRingBufferSize,
235 &NewChannel->RingBufferGpadlHandle);
236
237 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p size %d recv ring %p size %d, downstreamoffset %d>",
238 NewChannel,
239 NewChannel->OfferMsg.ChildRelId,
240 NewChannel->RingBufferGpadlHandle,
241 NewChannel->Outbound.RingBuffer,
242 NewChannel->Outbound.RingSize,
243 NewChannel->Inbound.RingBuffer,
244 NewChannel->Inbound.RingSize,
245 SendRingBufferSize);
246
247 /* Create and init the channel open message */
248 openInfo = kmalloc(sizeof(*openInfo) + sizeof(VMBUS_CHANNEL_OPEN_CHANNEL), GFP_KERNEL);
249 ASSERT(openInfo != NULL);
250
251 openInfo->WaitEvent = osd_WaitEventCreate();
252
253 openMsg = (VMBUS_CHANNEL_OPEN_CHANNEL*)openInfo->Msg;
254 openMsg->Header.MessageType = ChannelMessageOpenChannel;
255 openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; /* FIXME */
256 openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
257 openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
258 ASSERT(openMsg->RingBufferGpadlHandle);
259 openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >> PAGE_SHIFT;
260 openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
261
262 ASSERT(UserDataLen <= MAX_USER_DEFINED_BYTES);
263 if (UserDataLen)
264 {
265 memcpy(openMsg->UserData, UserData, UserDataLen);
266 }
267
268 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
269 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &openInfo->MsgListEntry);
270 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
271
272 DPRINT_DBG(VMBUS, "Sending channel open msg...");
273
274 ret = VmbusPostMessage(openMsg, sizeof(VMBUS_CHANNEL_OPEN_CHANNEL));
275 if (ret != 0)
276 {
277 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
278 goto Cleanup;
279 }
280
281 /* FIXME: Need to time-out here */
282 osd_WaitEventWait(openInfo->WaitEvent);
283
284 if (openInfo->Response.OpenResult.Status == 0)
285 {
286 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
287 }
288 else
289 {
290 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!", NewChannel, openInfo->Response.OpenResult.Status);
291 }
292
293 Cleanup:
294 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
295 REMOVE_ENTRY_LIST(&openInfo->MsgListEntry);
296 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
297
298 kfree(openInfo->WaitEvent);
299 kfree(openInfo);
300
301 DPRINT_EXIT(VMBUS);
302
303 return 0;
304 }
305
306 /*++;
307
308 Name:
309 DumpGpadlBody()
310
311 Description:
312 Dump the gpadl body message to the console for debugging purposes.
313
314 --*/
315 static void DumpGpadlBody(
316 VMBUS_CHANNEL_GPADL_BODY *Gpadl,
317 u32 Len)
318 {
319 int i=0;
320 int pfnCount=0;
321
322 pfnCount = (Len - sizeof(VMBUS_CHANNEL_GPADL_BODY))/ sizeof(u64);
323 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
324
325 for (i=0; i< pfnCount; i++)
326 {
327 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu", i, Gpadl->Pfn[i]);
328 }
329 }
330
331
332 /*++;
333
334 Name:
335 DumpGpadlHeader()
336
337 Description:
338 Dump the gpadl header message to the console for debugging purposes.
339
340 --*/
341 static void DumpGpadlHeader(
342 VMBUS_CHANNEL_GPADL_HEADER *Gpadl
343 )
344 {
345 int i=0,j=0;
346 int pageCount=0;
347
348
349 DPRINT_DBG(VMBUS, "gpadl header - relid %d, range count %d, range buflen %d",
350 Gpadl->ChildRelId,
351 Gpadl->RangeCount,
352 Gpadl->RangeBufLen);
353 for (i=0; i< Gpadl->RangeCount; i++)
354 {
355 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
356 pageCount = (pageCount > 26)? 26 : pageCount;
357
358 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d page count %d",
359 i, Gpadl->Range[i].ByteCount, Gpadl->Range[i].ByteOffset, pageCount);
360
361 for (j=0; j< pageCount; j++)
362 {
363 DPRINT_DBG(VMBUS, "%d) pfn %llu", j, Gpadl->Range[i].PfnArray[j]);
364 }
365 }
366 }
367
368 /*++;
369
370 Name:
371 VmbusChannelCreateGpadlHeader()
372
373 Description:
374 Creates a gpadl for the specified buffer
375
376 --*/
377 static int
378 VmbusChannelCreateGpadlHeader(
379 void * Kbuffer, /* from kmalloc() */
380 u32 Size, /* page-size multiple */
381 struct vmbus_channel_msginfo **MsgInfo,
382 u32 *MessageCount)
383 {
384 int i;
385 int pageCount;
386 unsigned long long pfn;
387 VMBUS_CHANNEL_GPADL_HEADER* gpaHeader;
388 VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
389 struct vmbus_channel_msginfo *msgHeader;
390 struct vmbus_channel_msginfo *msgBody;
391 u32 msgSize;
392
393 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
394
395 /* ASSERT( (kbuffer & (PAGE_SIZE-1)) == 0); */
396 ASSERT( (Size & (PAGE_SIZE-1)) == 0);
397
398 pageCount = Size >> PAGE_SHIFT;
399 pfn = virt_to_phys(Kbuffer) >> PAGE_SHIFT;
400
401 /* do we need a gpadl body msg */
402 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_HEADER) - sizeof(GPA_RANGE);
403 pfnCount = pfnSize / sizeof(u64);
404
405 if (pageCount > pfnCount) /* we need a gpadl body */
406 {
407 /* fill in the header */
408 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pfnCount*sizeof(u64);
409 msgHeader = kzalloc(msgSize, GFP_KERNEL);
410
411 INITIALIZE_LIST_HEAD(&msgHeader->SubMsgList);
412 msgHeader->MessageSize=msgSize;
413
414 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
415 gpaHeader->RangeCount = 1;
416 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
417 gpaHeader->Range[0].ByteOffset = 0;
418 gpaHeader->Range[0].ByteCount = Size;
419 for (i=0; i<pfnCount; i++)
420 {
421 gpaHeader->Range[0].PfnArray[i] = pfn+i;
422 }
423 *MsgInfo = msgHeader;
424 *MessageCount = 1;
425
426 pfnSum = pfnCount;
427 pfnLeft = pageCount - pfnCount;
428
429 /* how many pfns can we fit */
430 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(VMBUS_CHANNEL_GPADL_BODY);
431 pfnCount = pfnSize / sizeof(u64);
432
433 /* fill in the body */
434 while (pfnLeft)
435 {
436 if (pfnLeft > pfnCount)
437 {
438 pfnCurr = pfnCount;
439 }
440 else
441 {
442 pfnCurr = pfnLeft;
443 }
444
445 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(VMBUS_CHANNEL_GPADL_BODY) + pfnCurr*sizeof(u64);
446 msgBody = kzalloc(msgSize, GFP_KERNEL);
447 ASSERT(msgBody);
448 msgBody->MessageSize = msgSize;
449 (*MessageCount)++;
450 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)msgBody->Msg;
451
452 /* FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit */
453 /* gpadlBody->Gpadl = kbuffer; */
454 for (i=0; i<pfnCurr; i++)
455 {
456 gpadlBody->Pfn[i] = pfn + pfnSum + i;
457 }
458
459 /* add to msg header */
460 INSERT_TAIL_LIST(&msgHeader->SubMsgList, &msgBody->MsgListEntry);
461 pfnSum += pfnCurr;
462 pfnLeft -= pfnCurr;
463 }
464 }
465 else
466 {
467 /* everything fits in a header */
468 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(VMBUS_CHANNEL_GPADL_HEADER) + sizeof(GPA_RANGE) + pageCount*sizeof(u64);
469 msgHeader = kzalloc(msgSize, GFP_KERNEL);
470 msgHeader->MessageSize=msgSize;
471
472 gpaHeader = (VMBUS_CHANNEL_GPADL_HEADER*)msgHeader->Msg;
473 gpaHeader->RangeCount = 1;
474 gpaHeader->RangeBufLen = sizeof(GPA_RANGE) + pageCount*sizeof(u64);
475 gpaHeader->Range[0].ByteOffset = 0;
476 gpaHeader->Range[0].ByteCount = Size;
477 for (i=0; i<pageCount; i++)
478 {
479 gpaHeader->Range[0].PfnArray[i] = pfn+i;
480 }
481
482 *MsgInfo = msgHeader;
483 *MessageCount = 1;
484 }
485
486 return 0;
487 }
488
489
490 /*++;
491
492 Name:
493 VmbusChannelEstablishGpadl()
494
495 Description:
496 Estabish a GPADL for the specified buffer
497
498 --*/
499 int VmbusChannelEstablishGpadl(struct vmbus_channel *Channel,
500 void * Kbuffer, /* from kmalloc() */
501 u32 Size, /* page-size multiple */
502 u32 *GpadlHandle
503 )
504 {
505 int ret=0;
506 VMBUS_CHANNEL_GPADL_HEADER* gpadlMsg;
507 VMBUS_CHANNEL_GPADL_BODY* gpadlBody;
508 /* VMBUS_CHANNEL_GPADL_CREATED* gpadlCreated; */
509
510 struct vmbus_channel_msginfo *msgInfo;
511 struct vmbus_channel_msginfo *subMsgInfo;
512
513 u32 msgCount;
514 LIST_ENTRY* anchor;
515 LIST_ENTRY* curr;
516 u32 nextGpadlHandle;
517 unsigned long flags;
518
519 DPRINT_ENTER(VMBUS);
520
521 nextGpadlHandle = atomic_read(&gVmbusConnection.NextGpadlHandle);
522 atomic_inc(&gVmbusConnection.NextGpadlHandle);
523
524 VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
525 ASSERT(msgInfo != NULL);
526 ASSERT(msgCount >0);
527
528 msgInfo->WaitEvent = osd_WaitEventCreate();
529 gpadlMsg = (VMBUS_CHANNEL_GPADL_HEADER*)msgInfo->Msg;
530 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
531 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
532 gpadlMsg->Gpadl = nextGpadlHandle;
533
534 DumpGpadlHeader(gpadlMsg);
535
536 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
537 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
538 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
539
540 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d", Kbuffer, Size, msgCount);
541
542 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd", msgInfo->MessageSize - sizeof(*msgInfo));
543
544 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize - sizeof(*msgInfo));
545 if (ret != 0)
546 {
547 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
548 goto Cleanup;
549 }
550
551 if (msgCount>1)
552 {
553 ITERATE_LIST_ENTRIES(anchor, curr, &msgInfo->SubMsgList)
554 {
555 subMsgInfo = (struct vmbus_channel_msginfo*)curr;
556 gpadlBody = (VMBUS_CHANNEL_GPADL_BODY*)subMsgInfo->Msg;
557
558 gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
559 gpadlBody->Gpadl = nextGpadlHandle;
560
561 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %zd", subMsgInfo->MessageSize - sizeof(*subMsgInfo));
562
563 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize - sizeof(*subMsgInfo));
564 ret = VmbusPostMessage(gpadlBody, subMsgInfo->MessageSize - sizeof(*subMsgInfo));
565 ASSERT(ret == 0);
566 }
567 }
568 osd_WaitEventWait(msgInfo->WaitEvent);
569
570 /* At this point, we received the gpadl created msg */
571 DPRINT_DBG(VMBUS, "Received GPADL created (relid %d, status %d handle %x)",
572 Channel->OfferMsg.ChildRelId,
573 msgInfo->Response.GpadlCreated.CreationStatus,
574 gpadlMsg->Gpadl);
575
576 *GpadlHandle = gpadlMsg->Gpadl;
577
578 Cleanup:
579 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
580 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
581 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
582
583 kfree(msgInfo->WaitEvent);
584 kfree(msgInfo);
585
586 DPRINT_EXIT(VMBUS);
587
588 return ret;
589 }
590
591
592
593 /*++;
594
595 Name:
596 VmbusChannelTeardownGpadl()
597
598 Description:
599 Teardown the specified GPADL handle
600
601 --*/
602 int VmbusChannelTeardownGpadl(struct vmbus_channel *Channel, u32 GpadlHandle)
603 {
604 int ret=0;
605 VMBUS_CHANNEL_GPADL_TEARDOWN *msg;
606 struct vmbus_channel_msginfo *info;
607 unsigned long flags;
608
609 DPRINT_ENTER(VMBUS);
610
611 ASSERT(GpadlHandle != 0);
612
613 info = kmalloc(sizeof(*info) + sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN), GFP_KERNEL);
614 ASSERT(info != NULL);
615
616 info->WaitEvent = osd_WaitEventCreate();
617
618 msg = (VMBUS_CHANNEL_GPADL_TEARDOWN*)info->Msg;
619
620 msg->Header.MessageType = ChannelMessageGpadlTeardown;
621 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
622 msg->Gpadl = GpadlHandle;
623
624 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
625 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &info->MsgListEntry);
626 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
627
628 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN));
629 if (ret != 0)
630 {
631 /* TODO: */
632 }
633
634 osd_WaitEventWait(info->WaitEvent);
635
636 /* Received a torndown response */
637 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
638 REMOVE_ENTRY_LIST(&info->MsgListEntry);
639 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
640
641 kfree(info->WaitEvent);
642 kfree(info);
643
644 DPRINT_EXIT(VMBUS);
645
646 return ret;
647 }
648
649
650 /*++
651
652 Name:
653 VmbusChannelClose()
654
655 Description:
656 Close the specified channel
657
658 --*/
659 void VmbusChannelClose(struct vmbus_channel *Channel)
660 {
661 int ret=0;
662 VMBUS_CHANNEL_CLOSE_CHANNEL* msg;
663 struct vmbus_channel_msginfo *info;
664 unsigned long flags;
665
666 DPRINT_ENTER(VMBUS);
667
668 /* Stop callback and cancel the timer asap */
669 Channel->OnChannelCallback = NULL;
670 del_timer(&Channel->poll_timer);
671
672 /* Send a closing message */
673 info = kmalloc(sizeof(*info) + sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL), GFP_KERNEL);
674 ASSERT(info != NULL);
675
676 /* info->waitEvent = osd_WaitEventCreate(); */
677
678 msg = (VMBUS_CHANNEL_CLOSE_CHANNEL*)info->Msg;
679 msg->Header.MessageType = ChannelMessageCloseChannel;
680 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
681
682 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL));
683 if (ret != 0)
684 {
685 /* TODO: */
686 }
687
688 /* Tear down the gpadl for the channel's ring buffer */
689 if (Channel->RingBufferGpadlHandle)
690 {
691 VmbusChannelTeardownGpadl(Channel, Channel->RingBufferGpadlHandle);
692 }
693
694 /* TODO: Send a msg to release the childRelId */
695
696 /* Cleanup the ring buffers for this channel */
697 RingBufferCleanup(&Channel->Outbound);
698 RingBufferCleanup(&Channel->Inbound);
699
700 osd_PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
701
702 kfree(info);
703
704
705 /*
706 * If we are closing the channel during an error path in
707 * opening the channel, don't free the channel since the
708 * caller will free the channel
709 */
710
711 if (Channel->State == CHANNEL_OPEN_STATE)
712 {
713 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
714 REMOVE_ENTRY_LIST(&Channel->ListEntry);
715 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
716
717 FreeVmbusChannel(Channel);
718 }
719
720 DPRINT_EXIT(VMBUS);
721 }
722
723
724 /*++
725
726 Name:
727 VmbusChannelSendPacket()
728
729 Description:
730 Send the specified buffer on the given channel
731
732 --*/
733 int VmbusChannelSendPacket(struct vmbus_channel *Channel,
734 const void * Buffer,
735 u32 BufferLen,
736 u64 RequestId,
737 VMBUS_PACKET_TYPE Type,
738 u32 Flags
739 )
740 {
741 int ret=0;
742 VMPACKET_DESCRIPTOR desc;
743 u32 packetLen = sizeof(VMPACKET_DESCRIPTOR) + BufferLen;
744 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
745 struct scatterlist bufferList[3];
746 u64 alignedData=0;
747
748 DPRINT_ENTER(VMBUS);
749 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d", Channel, Buffer, BufferLen);
750
751 DumpVmbusChannel(Channel);
752
753 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
754
755 /* Setup the descriptor */
756 desc.Type = Type; /* VmbusPacketTypeDataInBand; */
757 desc.Flags = Flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
758 desc.DataOffset8 = sizeof(VMPACKET_DESCRIPTOR) >> 3; /* in 8-bytes granularity */
759 desc.Length8 = (u16)(packetLenAligned >> 3);
760 desc.TransactionId = RequestId;
761
762 sg_init_table(bufferList,3);
763 sg_set_buf(&bufferList[0], &desc, sizeof(VMPACKET_DESCRIPTOR));
764 sg_set_buf(&bufferList[1], Buffer, BufferLen);
765 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
766
767 ret = RingBufferWrite(
768 &Channel->Outbound,
769 bufferList,
770 3);
771
772 /* TODO: We should determine if this is optional */
773 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
774 {
775 VmbusChannelSetEvent(Channel);
776 }
777
778 DPRINT_EXIT(VMBUS);
779
780 return ret;
781 }
782
783
784 /*++
785
786 Name:
787 VmbusChannelSendPacketPageBuffer()
788
789 Description:
790 Send a range of single-page buffer packets using a GPADL Direct packet type.
791
792 --*/
793 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel *Channel,
794 PAGE_BUFFER PageBuffers[],
795 u32 PageCount,
796 void * Buffer,
797 u32 BufferLen,
798 u64 RequestId
799 )
800 {
801 int ret=0;
802 int i=0;
803 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
804 u32 descSize;
805 u32 packetLen;
806 u32 packetLenAligned;
807 struct scatterlist bufferList[3];
808 u64 alignedData=0;
809
810 DPRINT_ENTER(VMBUS);
811
812 ASSERT(PageCount <= MAX_PAGE_BUFFER_COUNT);
813
814 DumpVmbusChannel(Channel);
815
816 /* Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support */
817 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER) - ((MAX_PAGE_BUFFER_COUNT - PageCount)*sizeof(PAGE_BUFFER));
818 packetLen = descSize + BufferLen;
819 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
820
821 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
822
823 /* Setup the descriptor */
824 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
825 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
826 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
827 desc.Length8 = (u16)(packetLenAligned >> 3);
828 desc.TransactionId = RequestId;
829 desc.RangeCount = PageCount;
830
831 for (i=0; i<PageCount; i++)
832 {
833 desc.Range[i].Length = PageBuffers[i].Length;
834 desc.Range[i].Offset = PageBuffers[i].Offset;
835 desc.Range[i].Pfn = PageBuffers[i].Pfn;
836 }
837
838 sg_init_table(bufferList,3);
839 sg_set_buf(&bufferList[0], &desc, descSize);
840 sg_set_buf(&bufferList[1], Buffer, BufferLen);
841 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
842
843 ret = RingBufferWrite(
844 &Channel->Outbound,
845 bufferList,
846 3);
847
848 /* TODO: We should determine if this is optional */
849 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
850 {
851 VmbusChannelSetEvent(Channel);
852 }
853
854 DPRINT_EXIT(VMBUS);
855
856 return ret;
857 }
858
859
860
861 /*++
862
863 Name:
864 VmbusChannelSendPacketMultiPageBuffer()
865
866 Description:
867 Send a multi-page buffer packet using a GPADL Direct packet type.
868
869 --*/
870 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel *Channel,
871 MULTIPAGE_BUFFER *MultiPageBuffer,
872 void * Buffer,
873 u32 BufferLen,
874 u64 RequestId
875 )
876 {
877 int ret=0;
878 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
879 u32 descSize;
880 u32 packetLen;
881 u32 packetLenAligned;
882 struct scatterlist bufferList[3];
883 u64 alignedData=0;
884 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset, MultiPageBuffer->Length);
885
886 DPRINT_ENTER(VMBUS);
887
888 DumpVmbusChannel(Channel);
889
890 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
891
892 ASSERT(PfnCount > 0);
893 ASSERT(PfnCount <= MAX_MULTIPAGE_BUFFER_COUNT);
894
895 /* Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support */
896 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) - ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount)*sizeof(u64));
897 packetLen = descSize + BufferLen;
898 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
899
900 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
901
902 /* Setup the descriptor */
903 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
904 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
905 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
906 desc.Length8 = (u16)(packetLenAligned >> 3);
907 desc.TransactionId = RequestId;
908 desc.RangeCount = 1;
909
910 desc.Range.Length = MultiPageBuffer->Length;
911 desc.Range.Offset = MultiPageBuffer->Offset;
912
913 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray, PfnCount*sizeof(u64));
914
915 sg_init_table(bufferList,3);
916 sg_set_buf(&bufferList[0], &desc, descSize);
917 sg_set_buf(&bufferList[1], Buffer, BufferLen);
918 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
919
920 ret = RingBufferWrite(
921 &Channel->Outbound,
922 bufferList,
923 3);
924
925 /* TODO: We should determine if this is optional */
926 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
927 {
928 VmbusChannelSetEvent(Channel);
929 }
930
931 DPRINT_EXIT(VMBUS);
932
933 return ret;
934 }
935
936
937 /*++
938
939 Name:
940 VmbusChannelRecvPacket()
941
942 Description:
943 Retrieve the user packet on the specified channel
944
945 --*/
946 /* TODO: Do we ever receive a gpa direct packet other than the ones we send ? */
947 int VmbusChannelRecvPacket(struct vmbus_channel *Channel,
948 void *Buffer,
949 u32 BufferLen,
950 u32 *BufferActualLen,
951 u64 *RequestId)
952 {
953 VMPACKET_DESCRIPTOR desc;
954 u32 packetLen;
955 u32 userLen;
956 int ret;
957 unsigned long flags;
958
959 DPRINT_ENTER(VMBUS);
960
961 *BufferActualLen = 0;
962 *RequestId = 0;
963
964 spin_lock_irqsave(&Channel->inbound_lock, flags);
965
966 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
967 if (ret != 0)
968 {
969 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
970
971 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
972 DPRINT_EXIT(VMBUS);
973 return 0;
974 }
975
976 /* VmbusChannelClearEvent(Channel); */
977
978 packetLen = desc.Length8 << 3;
979 userLen = packetLen - (desc.DataOffset8 << 3);
980 /* ASSERT(userLen > 0); */
981
982 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
983 Channel,
984 Channel->OfferMsg.ChildRelId,
985 desc.Type,
986 desc.Flags,
987 desc.TransactionId, packetLen, userLen);
988
989 *BufferActualLen = userLen;
990
991 if (userLen > BufferLen)
992 {
993 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
994
995 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d", BufferLen, userLen);
996 DPRINT_EXIT(VMBUS);
997
998 return -1;
999 }
1000
1001 *RequestId = desc.TransactionId;
1002
1003 /* Copy over the packet to the user buffer */
1004 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen, (desc.DataOffset8 << 3));
1005
1006 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1007
1008 DPRINT_EXIT(VMBUS);
1009
1010 return 0;
1011 }
1012
1013 /*++
1014
1015 Name:
1016 VmbusChannelRecvPacketRaw()
1017
1018 Description:
1019 Retrieve the raw packet on the specified channel
1020
1021 --*/
1022 int VmbusChannelRecvPacketRaw(struct vmbus_channel *Channel,
1023 void * Buffer,
1024 u32 BufferLen,
1025 u32* BufferActualLen,
1026 u64* RequestId
1027 )
1028 {
1029 VMPACKET_DESCRIPTOR desc;
1030 u32 packetLen;
1031 u32 userLen;
1032 int ret;
1033 unsigned long flags;
1034
1035 DPRINT_ENTER(VMBUS);
1036
1037 *BufferActualLen = 0;
1038 *RequestId = 0;
1039
1040 spin_lock_irqsave(&Channel->inbound_lock, flags);
1041
1042 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(VMPACKET_DESCRIPTOR));
1043 if (ret != 0)
1044 {
1045 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1046
1047 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1048 DPRINT_EXIT(VMBUS);
1049 return 0;
1050 }
1051
1052 /* VmbusChannelClearEvent(Channel); */
1053
1054 packetLen = desc.Length8 << 3;
1055 userLen = packetLen - (desc.DataOffset8 << 3);
1056
1057 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1058 Channel,
1059 Channel->OfferMsg.ChildRelId,
1060 desc.Type,
1061 desc.Flags,
1062 desc.TransactionId, packetLen, userLen);
1063
1064 *BufferActualLen = packetLen;
1065
1066 if (packetLen > BufferLen)
1067 {
1068 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1069
1070 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen, BufferLen);
1071 DPRINT_EXIT(VMBUS);
1072 return -2;
1073 }
1074
1075 *RequestId = desc.TransactionId;
1076
1077 /* Copy over the entire packet to the user buffer */
1078 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1079
1080 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1081
1082 DPRINT_EXIT(VMBUS);
1083
1084 return 0;
1085 }
1086
1087
1088 /*++
1089
1090 Name:
1091 VmbusChannelOnChannelEvent()
1092
1093 Description:
1094 Channel event callback
1095
1096 --*/
1097 void VmbusChannelOnChannelEvent(struct vmbus_channel *Channel)
1098 {
1099 DumpVmbusChannel(Channel);
1100 ASSERT(Channel->OnChannelCallback);
1101 #ifdef ENABLE_POLLING
1102 del_timer(&Channel->poll_timer);
1103 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1104 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
1105 add_timer(&channel->poll_timer);
1106 #else
1107 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1108 #endif
1109 }
1110
1111 /*++
1112
1113 Name:
1114 VmbusChannelOnTimer()
1115
1116 Description:
1117 Timer event callback
1118
1119 --*/
1120 void VmbusChannelOnTimer(unsigned long data)
1121 {
1122 struct vmbus_channel *channel = (struct vmbus_channel *)data;
1123
1124 if (channel->OnChannelCallback)
1125 {
1126 channel->OnChannelCallback(channel->ChannelCallbackContext);
1127 #ifdef ENABLE_POLLING
1128 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
1129 add_timer(&channel->poll_timer);
1130 #endif
1131 }
1132 }
1133
1134
1135 /*++
1136
1137 Name:
1138 DumpVmbusChannel()
1139
1140 Description:
1141 Dump vmbus channel info to the console
1142
1143 --*/
1144 static void DumpVmbusChannel(struct vmbus_channel *Channel)
1145 {
1146 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1147 DumpRingInfo(&Channel->Outbound, "Outbound ");
1148 DumpRingInfo(&Channel->Inbound, "Inbound ");
1149 }
1150
1151
1152 /* eof */
This page took 0.054706 seconds and 5 git commands to generate.