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