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