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