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