Commit | Line | Data |
---|---|---|
fceaf24a 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 | * Hank Janssen <hjanssen@microsoft.com> | |
20 | * | |
21 | */ | |
22 | ||
23 | ||
24 | #include "logging.h" | |
25 | #include "NetVsc.h" | |
26 | #include "RndisFilter.h" | |
27 | ||
28 | ||
29 | // | |
30 | // Globals | |
31 | // | |
32 | static const char* gDriverName="netvsc"; | |
33 | ||
34 | // {F8615163-DF3E-46c5-913F-F2D2F965ED0E} | |
35 | static const GUID gNetVscDeviceType={ | |
36 | .Data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E} | |
37 | }; | |
38 | ||
39 | ||
40 | // | |
41 | // Internal routines | |
42 | // | |
43 | static int | |
44 | NetVscOnDeviceAdd( | |
45 | DEVICE_OBJECT *Device, | |
46 | void *AdditionalInfo | |
47 | ); | |
48 | ||
49 | static int | |
50 | NetVscOnDeviceRemove( | |
51 | DEVICE_OBJECT *Device | |
52 | ); | |
53 | ||
54 | static void | |
55 | NetVscOnCleanup( | |
56 | DRIVER_OBJECT *Driver | |
57 | ); | |
58 | ||
59 | static void | |
60 | NetVscOnChannelCallback( | |
61 | PVOID context | |
62 | ); | |
63 | ||
64 | static int | |
65 | NetVscInitializeSendBufferWithNetVsp( | |
66 | DEVICE_OBJECT *Device | |
67 | ); | |
68 | ||
69 | static int | |
70 | NetVscInitializeReceiveBufferWithNetVsp( | |
71 | DEVICE_OBJECT *Device | |
72 | ); | |
73 | ||
74 | static int | |
75 | NetVscDestroySendBuffer( | |
76 | NETVSC_DEVICE *NetDevice | |
77 | ); | |
78 | ||
79 | static int | |
80 | NetVscDestroyReceiveBuffer( | |
81 | NETVSC_DEVICE *NetDevice | |
82 | ); | |
83 | ||
84 | static int | |
85 | NetVscConnectToVsp( | |
86 | DEVICE_OBJECT *Device | |
87 | ); | |
88 | ||
89 | static void | |
90 | NetVscOnSendCompletion( | |
91 | DEVICE_OBJECT *Device, | |
92 | VMPACKET_DESCRIPTOR *Packet | |
93 | ); | |
94 | ||
95 | static int | |
96 | NetVscOnSend( | |
97 | DEVICE_OBJECT *Device, | |
98 | NETVSC_PACKET *Packet | |
99 | ); | |
100 | ||
101 | static void | |
102 | NetVscOnReceive( | |
103 | DEVICE_OBJECT *Device, | |
104 | VMPACKET_DESCRIPTOR *Packet | |
105 | ); | |
106 | ||
107 | static void | |
108 | NetVscOnReceiveCompletion( | |
109 | PVOID Context | |
110 | ); | |
111 | ||
112 | static void | |
113 | NetVscSendReceiveCompletion( | |
114 | DEVICE_OBJECT *Device, | |
115 | UINT64 TransactionId | |
116 | ); | |
117 | ||
118 | static inline NETVSC_DEVICE* AllocNetDevice(DEVICE_OBJECT *Device) | |
119 | { | |
120 | NETVSC_DEVICE *netDevice; | |
121 | ||
122 | netDevice = MemAllocZeroed(sizeof(NETVSC_DEVICE)); | |
123 | if (!netDevice) | |
124 | return NULL; | |
125 | ||
126 | // Set to 2 to allow both inbound and outbound traffic | |
127 | InterlockedCompareExchange(&netDevice->RefCount, 2, 0); | |
128 | ||
129 | netDevice->Device = Device; | |
130 | Device->Extension = netDevice; | |
131 | ||
132 | return netDevice; | |
133 | } | |
134 | ||
135 | static inline void FreeNetDevice(NETVSC_DEVICE *Device) | |
136 | { | |
137 | ASSERT(Device->RefCount == 0); | |
138 | Device->Device->Extension = NULL; | |
139 | MemFree(Device); | |
140 | } | |
141 | ||
142 | ||
143 | // Get the net device object iff exists and its refcount > 1 | |
144 | static inline NETVSC_DEVICE* GetOutboundNetDevice(DEVICE_OBJECT *Device) | |
145 | { | |
146 | NETVSC_DEVICE *netDevice; | |
147 | ||
148 | netDevice = (NETVSC_DEVICE*)Device->Extension; | |
149 | if (netDevice && netDevice->RefCount > 1) | |
150 | { | |
151 | InterlockedIncrement(&netDevice->RefCount); | |
152 | } | |
153 | else | |
154 | { | |
155 | netDevice = NULL; | |
156 | } | |
157 | ||
158 | return netDevice; | |
159 | } | |
160 | ||
161 | // Get the net device object iff exists and its refcount > 0 | |
162 | static inline NETVSC_DEVICE* GetInboundNetDevice(DEVICE_OBJECT *Device) | |
163 | { | |
164 | NETVSC_DEVICE *netDevice; | |
165 | ||
166 | netDevice = (NETVSC_DEVICE*)Device->Extension; | |
167 | if (netDevice && netDevice->RefCount) | |
168 | { | |
169 | InterlockedIncrement(&netDevice->RefCount); | |
170 | } | |
171 | else | |
172 | { | |
173 | netDevice = NULL; | |
174 | } | |
175 | ||
176 | return netDevice; | |
177 | } | |
178 | ||
179 | static inline void PutNetDevice(DEVICE_OBJECT *Device) | |
180 | { | |
181 | NETVSC_DEVICE *netDevice; | |
182 | ||
183 | netDevice = (NETVSC_DEVICE*)Device->Extension; | |
184 | ASSERT(netDevice); | |
185 | ||
186 | InterlockedDecrement(&netDevice->RefCount); | |
187 | } | |
188 | ||
189 | static inline NETVSC_DEVICE* ReleaseOutboundNetDevice(DEVICE_OBJECT *Device) | |
190 | { | |
191 | NETVSC_DEVICE *netDevice; | |
192 | ||
193 | netDevice = (NETVSC_DEVICE*)Device->Extension; | |
194 | if (netDevice == NULL) | |
195 | return NULL; | |
196 | ||
197 | // Busy wait until the ref drop to 2, then set it to 1 | |
198 | while (InterlockedCompareExchange(&netDevice->RefCount, 1, 2) != 2) | |
199 | { | |
200 | Sleep(100); | |
201 | } | |
202 | ||
203 | return netDevice; | |
204 | } | |
205 | ||
206 | static inline NETVSC_DEVICE* ReleaseInboundNetDevice(DEVICE_OBJECT *Device) | |
207 | { | |
208 | NETVSC_DEVICE *netDevice; | |
209 | ||
210 | netDevice = (NETVSC_DEVICE*)Device->Extension; | |
211 | if (netDevice == NULL) | |
212 | return NULL; | |
213 | ||
214 | // Busy wait until the ref drop to 1, then set it to 0 | |
215 | while (InterlockedCompareExchange(&netDevice->RefCount, 0, 1) != 1) | |
216 | { | |
217 | Sleep(100); | |
218 | } | |
219 | ||
220 | Device->Extension = NULL; | |
221 | return netDevice; | |
222 | } | |
223 | ||
224 | /*++; | |
225 | ||
226 | ||
227 | Name: | |
228 | NetVscInitialize() | |
229 | ||
230 | Description: | |
231 | Main entry point | |
232 | ||
233 | --*/ | |
234 | int | |
235 | NetVscInitialize( | |
236 | DRIVER_OBJECT *drv | |
237 | ) | |
238 | { | |
239 | NETVSC_DRIVER_OBJECT* driver = (NETVSC_DRIVER_OBJECT*)drv; | |
240 | int ret=0; | |
241 | ||
242 | DPRINT_ENTER(NETVSC); | |
243 | ||
244 | DPRINT_DBG(NETVSC, "sizeof(NETVSC_PACKET)=%d, sizeof(NVSP_MESSAGE)=%d, sizeof(VMTRANSFER_PAGE_PACKET_HEADER)=%d", | |
245 | sizeof(NETVSC_PACKET), sizeof(NVSP_MESSAGE), sizeof(VMTRANSFER_PAGE_PACKET_HEADER)); | |
246 | ||
247 | // Make sure we are at least 2 pages since 1 page is used for control | |
248 | ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); | |
249 | ||
250 | drv->name = gDriverName; | |
251 | memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(GUID)); | |
252 | ||
253 | // Make sure it is set by the caller | |
254 | ASSERT(driver->OnReceiveCallback); | |
255 | ASSERT(driver->OnLinkStatusChanged); | |
256 | ||
257 | // Setup the dispatch table | |
258 | driver->Base.OnDeviceAdd = NetVscOnDeviceAdd; | |
259 | driver->Base.OnDeviceRemove = NetVscOnDeviceRemove; | |
260 | driver->Base.OnCleanup = NetVscOnCleanup; | |
261 | ||
262 | driver->OnSend = NetVscOnSend; | |
263 | ||
264 | RndisFilterInit(driver); | |
265 | ||
266 | DPRINT_EXIT(NETVSC); | |
267 | ||
268 | return ret; | |
269 | } | |
270 | ||
271 | static int | |
272 | NetVscInitializeReceiveBufferWithNetVsp( | |
273 | DEVICE_OBJECT *Device | |
274 | ) | |
275 | { | |
276 | int ret=0; | |
277 | NETVSC_DEVICE *netDevice; | |
278 | NVSP_MESSAGE *initPacket; | |
279 | ||
280 | DPRINT_ENTER(NETVSC); | |
281 | ||
282 | netDevice = GetOutboundNetDevice(Device); | |
283 | if (!netDevice) | |
284 | { | |
285 | DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?"); | |
286 | DPRINT_EXIT(NETVSC); | |
287 | return -1; | |
288 | } | |
289 | ASSERT(netDevice->ReceiveBufferSize > 0); | |
290 | ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity | |
291 | ||
292 | netDevice->ReceiveBuffer = PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT); | |
293 | if (!netDevice->ReceiveBuffer) | |
294 | { | |
295 | DPRINT_ERR(NETVSC, "unable to allocate receive buffer of size %d", netDevice->ReceiveBufferSize); | |
296 | ret = -1; | |
297 | goto Cleanup; | |
298 | } | |
299 | ASSERT(((ULONG_PTR)netDevice->ReceiveBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer | |
300 | ||
301 | DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL..."); | |
302 | ||
303 | // Establish the gpadl handle for this buffer on this channel. | |
304 | // Note: This call uses the vmbus connection rather than the channel to establish | |
305 | // the gpadl handle. | |
306 | ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device, | |
307 | netDevice->ReceiveBuffer, | |
308 | netDevice->ReceiveBufferSize, | |
309 | &netDevice->ReceiveBufferGpadlHandle); | |
310 | ||
311 | if (ret != 0) | |
312 | { | |
313 | DPRINT_ERR(NETVSC, "unable to establish receive buffer's gpadl"); | |
314 | goto Cleanup; | |
315 | } | |
316 | ||
317 | //WaitEventWait(ext->ChannelInitEvent); | |
318 | ||
319 | // Notify the NetVsp of the gpadl handle | |
320 | DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer..."); | |
321 | ||
322 | initPacket = &netDevice->ChannelInitPacket; | |
323 | ||
324 | memset(initPacket, 0, sizeof(NVSP_MESSAGE)); | |
325 | ||
326 | initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer; | |
327 | initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle; | |
328 | initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID; | |
329 | ||
330 | // Send the gpadl notification request | |
331 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
332 | initPacket, | |
333 | sizeof(NVSP_MESSAGE), | |
334 | (ULONG_PTR)initPacket, | |
335 | VmbusPacketTypeDataInBand, | |
336 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
337 | if (ret != 0) | |
338 | { | |
339 | DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp"); | |
340 | goto Cleanup; | |
341 | } | |
342 | ||
343 | WaitEventWait(netDevice->ChannelInitEvent); | |
344 | ||
345 | // Check the response | |
346 | if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess) | |
347 | { | |
348 | DPRINT_ERR(NETVSC, | |
349 | "Unable to complete receive buffer initialzation with NetVsp - status %d", | |
350 | initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status); | |
351 | ret = -1; | |
352 | goto Cleanup; | |
353 | } | |
354 | ||
355 | // Parse the response | |
356 | ASSERT(netDevice->ReceiveSectionCount == 0); | |
357 | ASSERT(netDevice->ReceiveSections == NULL); | |
358 | ||
359 | netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections; | |
360 | ||
361 | netDevice->ReceiveSections = MemAlloc(netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION)); | |
362 | if (netDevice->ReceiveSections == NULL) | |
363 | { | |
364 | ret = -1; | |
365 | goto Cleanup; | |
366 | } | |
367 | ||
368 | memcpy(netDevice->ReceiveSections, | |
369 | initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections, | |
370 | netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION)); | |
371 | ||
372 | DPRINT_INFO(NETVSC, | |
373 | "Receive sections info (count %d, offset %d, endoffset %d, suballoc size %d, num suballocs %d)", | |
374 | netDevice->ReceiveSectionCount, netDevice->ReceiveSections[0].Offset, netDevice->ReceiveSections[0].EndOffset, | |
375 | netDevice->ReceiveSections[0].SubAllocationSize, netDevice->ReceiveSections[0].NumSubAllocations); | |
376 | ||
377 | ||
378 | //For 1st release, there should only be 1 section that represents the entire receive buffer | |
379 | if (netDevice->ReceiveSectionCount != 1 || | |
380 | netDevice->ReceiveSections->Offset != 0 ) | |
381 | { | |
382 | ret = -1; | |
383 | goto Cleanup; | |
384 | } | |
385 | ||
386 | goto Exit; | |
387 | ||
388 | Cleanup: | |
389 | NetVscDestroyReceiveBuffer(netDevice); | |
390 | ||
391 | Exit: | |
392 | PutNetDevice(Device); | |
393 | DPRINT_EXIT(NETVSC); | |
394 | return ret; | |
395 | } | |
396 | ||
397 | ||
398 | static int | |
399 | NetVscInitializeSendBufferWithNetVsp( | |
400 | DEVICE_OBJECT *Device | |
401 | ) | |
402 | { | |
403 | int ret=0; | |
404 | NETVSC_DEVICE *netDevice; | |
405 | NVSP_MESSAGE *initPacket; | |
406 | ||
407 | DPRINT_ENTER(NETVSC); | |
408 | ||
409 | netDevice = GetOutboundNetDevice(Device); | |
410 | if (!netDevice) | |
411 | { | |
412 | DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?"); | |
413 | DPRINT_EXIT(NETVSC); | |
414 | return -1; | |
415 | } | |
416 | ASSERT(netDevice->SendBufferSize > 0); | |
417 | ASSERT((netDevice->SendBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity | |
418 | ||
419 | netDevice->SendBuffer = PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT); | |
420 | if (!netDevice->SendBuffer) | |
421 | { | |
422 | DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d", netDevice->SendBufferSize); | |
423 | ret = -1; | |
424 | goto Cleanup; | |
425 | } | |
426 | ASSERT(((ULONG_PTR)netDevice->SendBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer | |
427 | ||
428 | DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL..."); | |
429 | ||
430 | // Establish the gpadl handle for this buffer on this channel. | |
431 | // Note: This call uses the vmbus connection rather than the channel to establish | |
432 | // the gpadl handle. | |
433 | ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device, | |
434 | netDevice->SendBuffer, | |
435 | netDevice->SendBufferSize, | |
436 | &netDevice->SendBufferGpadlHandle); | |
437 | ||
438 | if (ret != 0) | |
439 | { | |
440 | DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl"); | |
441 | goto Cleanup; | |
442 | } | |
443 | ||
444 | //WaitEventWait(ext->ChannelInitEvent); | |
445 | ||
446 | // Notify the NetVsp of the gpadl handle | |
447 | DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer..."); | |
448 | ||
449 | initPacket = &netDevice->ChannelInitPacket; | |
450 | ||
451 | memset(initPacket, 0, sizeof(NVSP_MESSAGE)); | |
452 | ||
453 | initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer; | |
454 | initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle; | |
455 | initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID; | |
456 | ||
457 | // Send the gpadl notification request | |
458 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
459 | initPacket, | |
460 | sizeof(NVSP_MESSAGE), | |
461 | (ULONG_PTR)initPacket, | |
462 | VmbusPacketTypeDataInBand, | |
463 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
464 | if (ret != 0) | |
465 | { | |
466 | DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp"); | |
467 | goto Cleanup; | |
468 | } | |
469 | ||
470 | WaitEventWait(netDevice->ChannelInitEvent); | |
471 | ||
472 | // Check the response | |
473 | if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess) | |
474 | { | |
475 | DPRINT_ERR(NETVSC, | |
476 | "Unable to complete send buffer initialzation with NetVsp - status %d", | |
477 | initPacket->Messages.Version1Messages.SendSendBufferComplete.Status); | |
478 | ret = -1; | |
479 | goto Cleanup; | |
480 | } | |
481 | ||
482 | netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize; | |
483 | ||
484 | goto Exit; | |
485 | ||
486 | Cleanup: | |
487 | NetVscDestroySendBuffer(netDevice); | |
488 | ||
489 | Exit: | |
490 | PutNetDevice(Device); | |
491 | DPRINT_EXIT(NETVSC); | |
492 | return ret; | |
493 | } | |
494 | ||
495 | static int | |
496 | NetVscDestroyReceiveBuffer( | |
497 | NETVSC_DEVICE *NetDevice | |
498 | ) | |
499 | { | |
500 | NVSP_MESSAGE *revokePacket; | |
501 | int ret=0; | |
502 | ||
503 | ||
504 | DPRINT_ENTER(NETVSC); | |
505 | ||
506 | // If we got a section count, it means we received a SendReceiveBufferComplete msg | |
507 | // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here | |
508 | if (NetDevice->ReceiveSectionCount) | |
509 | { | |
510 | DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeReceiveBuffer..."); | |
511 | ||
512 | // Send the revoke receive buffer | |
513 | revokePacket = &NetDevice->RevokePacket; | |
514 | memset(revokePacket, 0, sizeof(NVSP_MESSAGE)); | |
515 | ||
516 | revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer; | |
517 | revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID; | |
518 | ||
519 | ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device, | |
520 | revokePacket, | |
521 | sizeof(NVSP_MESSAGE), | |
522 | (ULONG_PTR)revokePacket, | |
523 | VmbusPacketTypeDataInBand, | |
524 | 0); | |
525 | // If we failed here, we might as well return and have a leak rather than continue and a bugchk | |
526 | if (ret != 0) | |
527 | { | |
528 | DPRINT_ERR(NETVSC, "unable to send revoke receive buffer to netvsp"); | |
529 | DPRINT_EXIT(NETVSC); | |
530 | return -1; | |
531 | } | |
532 | } | |
533 | ||
534 | // Teardown the gpadl on the vsp end | |
535 | if (NetDevice->ReceiveBufferGpadlHandle) | |
536 | { | |
537 | DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL..."); | |
538 | ||
539 | ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, | |
540 | NetDevice->ReceiveBufferGpadlHandle); | |
541 | ||
542 | // If we failed here, we might as well return and have a leak rather than continue and a bugchk | |
543 | if (ret != 0) | |
544 | { | |
545 | DPRINT_ERR(NETVSC, "unable to teardown receive buffer's gpadl"); | |
546 | DPRINT_EXIT(NETVSC); | |
547 | return -1; | |
548 | } | |
549 | NetDevice->ReceiveBufferGpadlHandle = 0; | |
550 | } | |
551 | ||
552 | if (NetDevice->ReceiveBuffer) | |
553 | { | |
554 | DPRINT_INFO(NETVSC, "Freeing up receive buffer..."); | |
555 | ||
556 | // Free up the receive buffer | |
557 | PageFree(NetDevice->ReceiveBuffer, NetDevice->ReceiveBufferSize >> PAGE_SHIFT); | |
558 | NetDevice->ReceiveBuffer = NULL; | |
559 | } | |
560 | ||
561 | if (NetDevice->ReceiveSections) | |
562 | { | |
563 | MemFree(NetDevice->ReceiveSections); | |
564 | NetDevice->ReceiveSections = NULL; | |
565 | NetDevice->ReceiveSectionCount = 0; | |
566 | } | |
567 | ||
568 | DPRINT_EXIT(NETVSC); | |
569 | ||
570 | return ret; | |
571 | } | |
572 | ||
573 | ||
574 | ||
575 | ||
576 | static int | |
577 | NetVscDestroySendBuffer( | |
578 | NETVSC_DEVICE *NetDevice | |
579 | ) | |
580 | { | |
581 | NVSP_MESSAGE *revokePacket; | |
582 | int ret=0; | |
583 | ||
584 | ||
585 | DPRINT_ENTER(NETVSC); | |
586 | ||
587 | // If we got a section count, it means we received a SendReceiveBufferComplete msg | |
588 | // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here | |
589 | if (NetDevice->SendSectionSize) | |
590 | { | |
591 | DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeSendBuffer..."); | |
592 | ||
593 | // Send the revoke send buffer | |
594 | revokePacket = &NetDevice->RevokePacket; | |
595 | memset(revokePacket, 0, sizeof(NVSP_MESSAGE)); | |
596 | ||
597 | revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer; | |
598 | revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID; | |
599 | ||
600 | ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device, | |
601 | revokePacket, | |
602 | sizeof(NVSP_MESSAGE), | |
603 | (ULONG_PTR)revokePacket, | |
604 | VmbusPacketTypeDataInBand, | |
605 | 0); | |
606 | // If we failed here, we might as well return and have a leak rather than continue and a bugchk | |
607 | if (ret != 0) | |
608 | { | |
609 | DPRINT_ERR(NETVSC, "unable to send revoke send buffer to netvsp"); | |
610 | DPRINT_EXIT(NETVSC); | |
611 | return -1; | |
612 | } | |
613 | } | |
614 | ||
615 | // Teardown the gpadl on the vsp end | |
616 | if (NetDevice->SendBufferGpadlHandle) | |
617 | { | |
618 | DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL..."); | |
619 | ||
620 | ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, | |
621 | NetDevice->SendBufferGpadlHandle); | |
622 | ||
623 | // If we failed here, we might as well return and have a leak rather than continue and a bugchk | |
624 | if (ret != 0) | |
625 | { | |
626 | DPRINT_ERR(NETVSC, "unable to teardown send buffer's gpadl"); | |
627 | DPRINT_EXIT(NETVSC); | |
628 | return -1; | |
629 | } | |
630 | NetDevice->SendBufferGpadlHandle = 0; | |
631 | } | |
632 | ||
633 | if (NetDevice->SendBuffer) | |
634 | { | |
635 | DPRINT_INFO(NETVSC, "Freeing up send buffer..."); | |
636 | ||
637 | // Free up the receive buffer | |
638 | PageFree(NetDevice->SendBuffer, NetDevice->SendBufferSize >> PAGE_SHIFT); | |
639 | NetDevice->SendBuffer = NULL; | |
640 | } | |
641 | ||
642 | DPRINT_EXIT(NETVSC); | |
643 | ||
644 | return ret; | |
645 | } | |
646 | ||
647 | ||
648 | ||
649 | static int | |
650 | NetVscConnectToVsp( | |
651 | DEVICE_OBJECT *Device | |
652 | ) | |
653 | { | |
654 | int ret=0; | |
655 | NETVSC_DEVICE *netDevice; | |
656 | NVSP_MESSAGE *initPacket; | |
657 | int ndisVersion; | |
658 | ||
659 | DPRINT_ENTER(NETVSC); | |
660 | ||
661 | netDevice = GetOutboundNetDevice(Device); | |
662 | if (!netDevice) | |
663 | { | |
664 | DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?"); | |
665 | DPRINT_EXIT(NETVSC); | |
666 | return -1; | |
667 | } | |
668 | ||
669 | initPacket = &netDevice->ChannelInitPacket; | |
670 | ||
671 | memset(initPacket, 0, sizeof(NVSP_MESSAGE)); | |
672 | initPacket->Header.MessageType = NvspMessageTypeInit; | |
673 | initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION; | |
674 | initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION; | |
675 | ||
676 | DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit..."); | |
677 | ||
678 | // Send the init request | |
679 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
680 | initPacket, | |
681 | sizeof(NVSP_MESSAGE), | |
682 | (ULONG_PTR)initPacket, | |
683 | VmbusPacketTypeDataInBand, | |
684 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
685 | ||
686 | if( ret != 0) | |
687 | { | |
688 | DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit"); | |
689 | goto Cleanup; | |
690 | } | |
691 | ||
692 | WaitEventWait(netDevice->ChannelInitEvent); | |
693 | ||
694 | // Now, check the response | |
695 | //ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); | |
696 | DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)", | |
697 | initPacket->Messages.InitMessages.InitComplete.Status, | |
698 | initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength); | |
699 | ||
700 | if (initPacket->Messages.InitMessages.InitComplete.Status != NvspStatusSuccess) | |
701 | { | |
702 | DPRINT_ERR(NETVSC, "unable to initialize with netvsp (status 0x%x)", initPacket->Messages.InitMessages.InitComplete.Status); | |
703 | ret = -1; | |
704 | goto Cleanup; | |
705 | } | |
706 | ||
707 | if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) | |
708 | { | |
709 | DPRINT_ERR(NETVSC, "unable to initialize with netvsp (version expected 1 got %d)", | |
710 | initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion); | |
711 | ret = -1; | |
712 | goto Cleanup; | |
713 | } | |
714 | DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion..."); | |
715 | ||
716 | // Send the ndis version | |
717 | memset(initPacket, 0, sizeof(NVSP_MESSAGE)); | |
718 | ||
719 | ndisVersion = 0x00050000; | |
720 | ||
721 | initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion; | |
722 | initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion = (ndisVersion & 0xFFFF0000) >> 16; | |
723 | initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion = ndisVersion & 0xFFFF; | |
724 | ||
725 | // Send the init request | |
726 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
727 | initPacket, | |
728 | sizeof(NVSP_MESSAGE), | |
729 | (ULONG_PTR)initPacket, | |
730 | VmbusPacketTypeDataInBand, | |
731 | 0); | |
732 | if (ret != 0) | |
733 | { | |
734 | DPRINT_ERR(NETVSC, "unable to send NvspMessage1TypeSendNdisVersion"); | |
735 | ret = -1; | |
736 | goto Cleanup; | |
737 | } | |
738 | // | |
739 | // BUGBUG - We have to wait for the above msg since the netvsp uses KMCL which acknowledges packet (completion packet) | |
740 | // since our Vmbus always set the VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag | |
741 | //WaitEventWait(NetVscChannel->ChannelInitEvent); | |
742 | ||
743 | // Post the big receive buffer to NetVSP | |
744 | ret = NetVscInitializeReceiveBufferWithNetVsp(Device); | |
745 | if (ret == 0) | |
746 | { | |
747 | ret = NetVscInitializeSendBufferWithNetVsp(Device); | |
748 | } | |
749 | ||
750 | Cleanup: | |
751 | PutNetDevice(Device); | |
752 | DPRINT_EXIT(NETVSC); | |
753 | return ret; | |
754 | } | |
755 | ||
756 | static void | |
757 | NetVscDisconnectFromVsp( | |
758 | NETVSC_DEVICE *NetDevice | |
759 | ) | |
760 | { | |
761 | DPRINT_ENTER(NETVSC); | |
762 | ||
763 | NetVscDestroyReceiveBuffer(NetDevice); | |
764 | NetVscDestroySendBuffer(NetDevice); | |
765 | ||
766 | DPRINT_EXIT(NETVSC); | |
767 | } | |
768 | ||
769 | ||
770 | /*++ | |
771 | ||
772 | Name: | |
773 | NetVscOnDeviceAdd() | |
774 | ||
775 | Description: | |
776 | Callback when the device belonging to this driver is added | |
777 | ||
778 | --*/ | |
779 | int | |
780 | NetVscOnDeviceAdd( | |
781 | DEVICE_OBJECT *Device, | |
782 | void *AdditionalInfo | |
783 | ) | |
784 | { | |
785 | int ret=0; | |
786 | int i; | |
787 | ||
788 | NETVSC_DEVICE* netDevice; | |
789 | NETVSC_PACKET* packet; | |
790 | LIST_ENTRY *entry; | |
791 | ||
792 | NETVSC_DRIVER_OBJECT *netDriver = (NETVSC_DRIVER_OBJECT*) Device->Driver;; | |
793 | ||
794 | DPRINT_ENTER(NETVSC); | |
795 | ||
796 | netDevice = AllocNetDevice(Device); | |
797 | if (!netDevice) | |
798 | { | |
799 | ret = -1; | |
800 | goto Cleanup; | |
801 | } | |
802 | ||
803 | DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice); | |
804 | ||
805 | // Initialize the NetVSC channel extension | |
806 | netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE; | |
807 | netDevice->ReceivePacketListLock = SpinlockCreate(); | |
808 | ||
809 | netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE; | |
810 | ||
811 | INITIALIZE_LIST_HEAD(&netDevice->ReceivePacketList); | |
812 | ||
813 | for (i=0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) | |
814 | { | |
815 | packet = MemAllocZeroed(sizeof(NETVSC_PACKET) + (NETVSC_RECEIVE_SG_COUNT* sizeof(PAGE_BUFFER))); | |
816 | if (!packet) | |
817 | { | |
818 | DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts for receive pool (wanted %d got %d)", NETVSC_RECEIVE_PACKETLIST_COUNT, i); | |
819 | break; | |
820 | } | |
821 | ||
822 | INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry); | |
823 | } | |
824 | netDevice->ChannelInitEvent = WaitEventCreate(); | |
825 | ||
826 | // Open the channel | |
827 | ret = Device->Driver->VmbusChannelInterface.Open(Device, | |
828 | netDriver->RingBufferSize, | |
829 | netDriver->RingBufferSize, | |
830 | NULL, 0, | |
831 | NetVscOnChannelCallback, | |
832 | Device | |
833 | ); | |
834 | ||
835 | if (ret != 0) | |
836 | { | |
837 | DPRINT_ERR(NETVSC, "unable to open channel: %d", ret); | |
838 | ret = -1; | |
839 | goto Cleanup; | |
840 | } | |
841 | ||
842 | // Channel is opened | |
843 | DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***"); | |
844 | ||
845 | // Connect with the NetVsp | |
846 | ret = NetVscConnectToVsp(Device); | |
847 | if (ret != 0) | |
848 | { | |
849 | DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret); | |
850 | ret = -1; | |
851 | goto Close; | |
852 | } | |
853 | ||
854 | DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***", ret); | |
855 | ||
856 | DPRINT_EXIT(NETVSC); | |
857 | return ret; | |
858 | ||
859 | Close: | |
860 | // Now, we can close the channel safely | |
861 | Device->Driver->VmbusChannelInterface.Close(Device); | |
862 | ||
863 | Cleanup: | |
864 | ||
865 | if (netDevice) | |
866 | { | |
867 | WaitEventClose(netDevice->ChannelInitEvent); | |
868 | ||
869 | while (!IsListEmpty(&netDevice->ReceivePacketList)) | |
870 | { | |
871 | entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList); | |
872 | packet = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry); | |
873 | MemFree(packet); | |
874 | } | |
875 | ||
876 | SpinlockClose(netDevice->ReceivePacketListLock); | |
877 | ||
878 | ReleaseOutboundNetDevice(Device); | |
879 | ReleaseInboundNetDevice(Device); | |
880 | ||
881 | FreeNetDevice(netDevice); | |
882 | } | |
883 | ||
884 | DPRINT_EXIT(NETVSC); | |
885 | return ret; | |
886 | } | |
887 | ||
888 | ||
889 | /*++ | |
890 | ||
891 | Name: | |
892 | NetVscOnDeviceRemove() | |
893 | ||
894 | Description: | |
895 | Callback when the root bus device is removed | |
896 | ||
897 | --*/ | |
898 | int | |
899 | NetVscOnDeviceRemove( | |
900 | DEVICE_OBJECT *Device | |
901 | ) | |
902 | { | |
903 | NETVSC_DEVICE *netDevice; | |
904 | NETVSC_PACKET *netvscPacket; | |
905 | int ret=0; | |
906 | LIST_ENTRY *entry; | |
907 | ||
908 | DPRINT_ENTER(NETVSC); | |
909 | ||
910 | DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...", Device->Extension); | |
911 | ||
912 | // Stop outbound traffic ie sends and receives completions | |
913 | netDevice = ReleaseOutboundNetDevice(Device); | |
914 | if (!netDevice) | |
915 | { | |
916 | DPRINT_ERR(NETVSC, "No net device present!!"); | |
917 | return -1; | |
918 | } | |
919 | ||
920 | // Wait for all send completions | |
921 | while (netDevice->NumOutstandingSends) | |
922 | { | |
923 | DPRINT_INFO(NETVSC, "waiting for %d requests to complete...", netDevice->NumOutstandingSends); | |
924 | ||
925 | Sleep(100); | |
926 | } | |
927 | ||
928 | DPRINT_INFO(NETVSC, "Disconnecting from netvsp..."); | |
929 | ||
930 | NetVscDisconnectFromVsp(netDevice); | |
931 | ||
932 | DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...", Device->Extension); | |
933 | ||
934 | // Stop inbound traffic ie receives and sends completions | |
935 | netDevice = ReleaseInboundNetDevice(Device); | |
936 | ||
937 | // At this point, no one should be accessing netDevice except in here | |
938 | DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice); | |
939 | ||
940 | // Now, we can close the channel safely | |
941 | Device->Driver->VmbusChannelInterface.Close(Device); | |
942 | ||
943 | // Release all resources | |
944 | while (!IsListEmpty(&netDevice->ReceivePacketList)) | |
945 | { | |
946 | entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList); | |
947 | netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry); | |
948 | ||
949 | MemFree(netvscPacket); | |
950 | } | |
951 | ||
952 | SpinlockClose(netDevice->ReceivePacketListLock); | |
953 | WaitEventClose(netDevice->ChannelInitEvent); | |
954 | FreeNetDevice(netDevice); | |
955 | ||
956 | DPRINT_EXIT(NETVSC); | |
957 | return ret; | |
958 | } | |
959 | ||
960 | ||
961 | ||
962 | /*++ | |
963 | ||
964 | Name: | |
965 | NetVscOnCleanup() | |
966 | ||
967 | Description: | |
968 | Perform any cleanup when the driver is removed | |
969 | ||
970 | --*/ | |
971 | void | |
972 | NetVscOnCleanup( | |
973 | DRIVER_OBJECT *drv | |
974 | ) | |
975 | { | |
976 | DPRINT_ENTER(NETVSC); | |
977 | ||
978 | DPRINT_EXIT(NETVSC); | |
979 | } | |
980 | ||
981 | static void | |
982 | NetVscOnSendCompletion( | |
983 | DEVICE_OBJECT *Device, | |
984 | VMPACKET_DESCRIPTOR *Packet | |
985 | ) | |
986 | { | |
987 | NETVSC_DEVICE* netDevice; | |
988 | NVSP_MESSAGE *nvspPacket; | |
989 | NETVSC_PACKET *nvscPacket; | |
990 | ||
991 | DPRINT_ENTER(NETVSC); | |
992 | ||
993 | netDevice = GetInboundNetDevice(Device); | |
994 | if (!netDevice) | |
995 | { | |
996 | DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?"); | |
997 | DPRINT_EXIT(NETVSC); | |
998 | return; | |
999 | } | |
1000 | ||
1001 | nvspPacket = (NVSP_MESSAGE*)((ULONG_PTR)Packet + (Packet->DataOffset8 << 3)); | |
1002 | ||
1003 | DPRINT_DBG(NETVSC, "send completion packet - type %d", nvspPacket->Header.MessageType); | |
1004 | ||
1005 | if (nvspPacket->Header.MessageType == NvspMessageTypeInitComplete || | |
1006 | nvspPacket->Header.MessageType == NvspMessage1TypeSendReceiveBufferComplete || | |
1007 | nvspPacket->Header.MessageType == NvspMessage1TypeSendSendBufferComplete) | |
1008 | { | |
1009 | // Copy the response back | |
1010 | memcpy(&netDevice->ChannelInitPacket, nvspPacket, sizeof(NVSP_MESSAGE)); | |
1011 | WaitEventSet(netDevice->ChannelInitEvent); | |
1012 | } | |
1013 | else if (nvspPacket->Header.MessageType == NvspMessage1TypeSendRNDISPacketComplete) | |
1014 | { | |
1015 | // Get the send context | |
1016 | nvscPacket = (NETVSC_PACKET *)(ULONG_PTR)Packet->TransactionId; | |
1017 | ASSERT(nvscPacket); | |
1018 | ||
1019 | // Notify the layer above us | |
1020 | nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext); | |
1021 | ||
1022 | InterlockedDecrement(&netDevice->NumOutstandingSends); | |
1023 | } | |
1024 | else | |
1025 | { | |
1026 | DPRINT_ERR(NETVSC, "Unknown send completion packet type - %d received!!", nvspPacket->Header.MessageType); | |
1027 | } | |
1028 | ||
1029 | PutNetDevice(Device); | |
1030 | DPRINT_EXIT(NETVSC); | |
1031 | } | |
1032 | ||
1033 | ||
1034 | ||
1035 | static int | |
1036 | NetVscOnSend( | |
1037 | DEVICE_OBJECT *Device, | |
1038 | NETVSC_PACKET *Packet | |
1039 | ) | |
1040 | { | |
1041 | NETVSC_DEVICE* netDevice; | |
1042 | int ret=0; | |
1043 | ||
1044 | NVSP_MESSAGE sendMessage; | |
1045 | ||
1046 | DPRINT_ENTER(NETVSC); | |
1047 | ||
1048 | netDevice = GetOutboundNetDevice(Device); | |
1049 | if (!netDevice) | |
1050 | { | |
1051 | DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring outbound packets", netDevice); | |
1052 | DPRINT_EXIT(NETVSC); | |
1053 | return -2; | |
1054 | } | |
1055 | ||
1056 | sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket; | |
1057 | if (Packet->IsDataPacket) | |
1058 | sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;// 0 is RMC_DATA; | |
1059 | else | |
1060 | sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;// 1 is RMC_CONTROL; | |
1061 | ||
1062 | // Not using send buffer section | |
1063 | sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF; | |
1064 | sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0; | |
1065 | ||
1066 | if (Packet->PageBufferCount) | |
1067 | { | |
1068 | ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(Device, | |
1069 | Packet->PageBuffers, | |
1070 | Packet->PageBufferCount, | |
1071 | &sendMessage, | |
1072 | sizeof(NVSP_MESSAGE), | |
1073 | (ULONG_PTR)Packet); | |
1074 | } | |
1075 | else | |
1076 | { | |
1077 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
1078 | &sendMessage, | |
1079 | sizeof(NVSP_MESSAGE), | |
1080 | (ULONG_PTR)Packet, | |
1081 | VmbusPacketTypeDataInBand, | |
1082 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1083 | ||
1084 | } | |
1085 | ||
1086 | if (ret != 0) | |
1087 | { | |
1088 | DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d", Packet, ret); | |
1089 | } | |
1090 | ||
1091 | InterlockedIncrement(&netDevice->NumOutstandingSends); | |
1092 | PutNetDevice(Device); | |
1093 | ||
1094 | DPRINT_EXIT(NETVSC); | |
1095 | return ret; | |
1096 | } | |
1097 | ||
1098 | ||
1099 | static void | |
1100 | NetVscOnReceive( | |
1101 | DEVICE_OBJECT *Device, | |
1102 | VMPACKET_DESCRIPTOR *Packet | |
1103 | ) | |
1104 | { | |
1105 | NETVSC_DEVICE* netDevice; | |
1106 | VMTRANSFER_PAGE_PACKET_HEADER *vmxferpagePacket; | |
1107 | NVSP_MESSAGE *nvspPacket; | |
1108 | NETVSC_PACKET *netvscPacket=NULL; | |
1109 | LIST_ENTRY* entry; | |
1110 | ULONG_PTR start; | |
1111 | ULONG_PTR end, endVirtual; | |
1112 | //NETVSC_DRIVER_OBJECT *netvscDriver; | |
1113 | XFERPAGE_PACKET *xferpagePacket=NULL; | |
1114 | LIST_ENTRY listHead; | |
1115 | ||
1116 | int i=0, j=0; | |
1117 | int count=0, bytesRemain=0; | |
1118 | ||
1119 | DPRINT_ENTER(NETVSC); | |
1120 | ||
1121 | netDevice = GetInboundNetDevice(Device); | |
1122 | if (!netDevice) | |
1123 | { | |
1124 | DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?"); | |
1125 | DPRINT_EXIT(NETVSC); | |
1126 | return; | |
1127 | } | |
1128 | ||
1129 | // All inbound packets other than send completion should be xfer page packet | |
1130 | if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) | |
1131 | { | |
1132 | DPRINT_ERR(NETVSC, "Unknown packet type received - %d", Packet->Type); | |
1133 | PutNetDevice(Device); | |
1134 | return; | |
1135 | } | |
1136 | ||
1137 | nvspPacket = (NVSP_MESSAGE*)((ULONG_PTR)Packet + (Packet->DataOffset8 << 3)); | |
1138 | ||
1139 | // Make sure this is a valid nvsp packet | |
1140 | if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket ) | |
1141 | { | |
1142 | DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d", nvspPacket->Header.MessageType); | |
1143 | PutNetDevice(Device); | |
1144 | return; | |
1145 | } | |
1146 | ||
1147 | DPRINT_DBG(NETVSC, "NVSP packet received - type %d", nvspPacket->Header.MessageType); | |
1148 | ||
1149 | vmxferpagePacket = (VMTRANSFER_PAGE_PACKET_HEADER*)Packet; | |
1150 | ||
1151 | if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) | |
1152 | { | |
1153 | DPRINT_ERR(NETVSC, "Invalid xfer page set id - expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, vmxferpagePacket->TransferPageSetId); | |
1154 | PutNetDevice(Device); | |
1155 | return; | |
1156 | } | |
1157 | ||
1158 | DPRINT_DBG(NETVSC, "xfer page - range count %d", vmxferpagePacket->RangeCount); | |
1159 | ||
1160 | INITIALIZE_LIST_HEAD(&listHead); | |
1161 | ||
1162 | // Grab free packets (range count + 1) to represent this xfer page packet. +1 to represent | |
1163 | // the xfer page packet itself. We grab it here so that we know exactly how many we can fulfil | |
1164 | SpinlockAcquire(netDevice->ReceivePacketListLock); | |
1165 | while (!IsListEmpty(&netDevice->ReceivePacketList)) | |
1166 | { | |
1167 | entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList); | |
1168 | netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry); | |
1169 | ||
1170 | INSERT_TAIL_LIST(&listHead, &netvscPacket->ListEntry); | |
1171 | ||
1172 | if (++count == vmxferpagePacket->RangeCount + 1) | |
1173 | break; | |
1174 | } | |
1175 | SpinlockRelease(netDevice->ReceivePacketListLock); | |
1176 | ||
1177 | // We need at least 2 netvsc pkts (1 to represent the xfer page and at least 1 for the range) | |
1178 | // i.e. we can handled some of the xfer page packet ranges... | |
1179 | if (count < 2) | |
1180 | { | |
1181 | DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. Dropping this xfer page packet completely!", count, vmxferpagePacket->RangeCount+1); | |
1182 | ||
1183 | // Return it to the freelist | |
1184 | SpinlockAcquire(netDevice->ReceivePacketListLock); | |
1185 | for (i=count; i != 0; i--) | |
1186 | { | |
1187 | entry = REMOVE_HEAD_LIST(&listHead); | |
1188 | netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry); | |
1189 | ||
1190 | INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &netvscPacket->ListEntry); | |
1191 | } | |
1192 | SpinlockRelease(netDevice->ReceivePacketListLock); | |
1193 | ||
1194 | NetVscSendReceiveCompletion(Device, vmxferpagePacket->d.TransactionId); | |
1195 | ||
1196 | PutNetDevice(Device); | |
1197 | return; | |
1198 | } | |
1199 | ||
1200 | // Remove the 1st packet to represent the xfer page packet itself | |
1201 | entry = REMOVE_HEAD_LIST(&listHead); | |
1202 | xferpagePacket = CONTAINING_RECORD(entry, XFERPAGE_PACKET, ListEntry); | |
1203 | xferpagePacket->Count = count - 1; // This is how much we can satisfy | |
1204 | ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= vmxferpagePacket->RangeCount); | |
1205 | ||
1206 | if (xferpagePacket->Count != vmxferpagePacket->RangeCount) | |
1207 | { | |
1208 | DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer page...got %d", vmxferpagePacket->RangeCount, xferpagePacket->Count); | |
1209 | } | |
1210 | ||
1211 | // Each range represents 1 RNDIS pkt that contains 1 ethernet frame | |
1212 | for (i=0; i < (count - 1); i++) | |
1213 | { | |
1214 | entry = REMOVE_HEAD_LIST(&listHead); | |
1215 | netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry); | |
1216 | ||
1217 | // Initialize the netvsc packet | |
1218 | netvscPacket->XferPagePacket = xferpagePacket; | |
1219 | netvscPacket->Completion.Recv.OnReceiveCompletion = NetVscOnReceiveCompletion; | |
1220 | netvscPacket->Completion.Recv.ReceiveCompletionContext = netvscPacket; | |
1221 | netvscPacket->Device = Device; | |
1222 | netvscPacket->Completion.Recv.ReceiveCompletionTid = vmxferpagePacket->d.TransactionId; // Save this so that we can send it back | |
1223 | ||
1224 | netvscPacket->TotalDataBufferLength = vmxferpagePacket->Ranges[i].ByteCount; | |
1225 | netvscPacket->PageBufferCount = 1; | |
1226 | ||
1227 | ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + vmxferpagePacket->Ranges[i].ByteCount < netDevice->ReceiveBufferSize); | |
1228 | ||
1229 | netvscPacket->PageBuffers[0].Length = vmxferpagePacket->Ranges[i].ByteCount; | |
1230 | ||
1231 | start = GetPhysicalAddress((void*)((ULONG_PTR)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset)); | |
1232 | ||
1233 | netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT; | |
1234 | endVirtual = (ULONG_PTR)netDevice->ReceiveBuffer | |
1235 | + vmxferpagePacket->Ranges[i].ByteOffset | |
1236 | + vmxferpagePacket->Ranges[i].ByteCount -1; | |
1237 | end = GetPhysicalAddress((void*)endVirtual); | |
1238 | ||
1239 | // Calculate the page relative offset | |
1240 | netvscPacket->PageBuffers[0].Offset = vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE -1); | |
1241 | if ((end >> PAGE_SHIFT) != (start>>PAGE_SHIFT)) { | |
1242 | //Handle frame across multiple pages: | |
1243 | netvscPacket->PageBuffers[0].Length = | |
1244 | (netvscPacket->PageBuffers[0].Pfn <<PAGE_SHIFT) + PAGE_SIZE - start; | |
1245 | bytesRemain = netvscPacket->TotalDataBufferLength - netvscPacket->PageBuffers[0].Length; | |
1246 | for (j=1; j<NETVSC_PACKET_MAXPAGE; j++) { | |
1247 | netvscPacket->PageBuffers[j].Offset = 0; | |
1248 | if (bytesRemain <= PAGE_SIZE) { | |
1249 | netvscPacket->PageBuffers[j].Length = bytesRemain; | |
1250 | bytesRemain = 0; | |
1251 | } else { | |
1252 | netvscPacket->PageBuffers[j].Length = PAGE_SIZE; | |
1253 | bytesRemain -= PAGE_SIZE; | |
1254 | } | |
1255 | netvscPacket->PageBuffers[j].Pfn = | |
1256 | GetPhysicalAddress((void*)(endVirtual - bytesRemain)) >> PAGE_SHIFT; | |
1257 | netvscPacket->PageBufferCount++; | |
1258 | if (bytesRemain == 0) | |
1259 | break; | |
1260 | } | |
1261 | ASSERT(bytesRemain == 0); | |
1262 | } | |
1263 | DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => (pfn %llx, offset %u, len %u)", | |
1264 | i, | |
1265 | vmxferpagePacket->Ranges[i].ByteOffset, | |
1266 | vmxferpagePacket->Ranges[i].ByteCount, | |
1267 | netvscPacket->PageBuffers[0].Pfn, | |
1268 | netvscPacket->PageBuffers[0].Offset, | |
1269 | netvscPacket->PageBuffers[0].Length); | |
1270 | ||
1271 | // Pass it to the upper layer | |
1272 | ((NETVSC_DRIVER_OBJECT*)Device->Driver)->OnReceiveCallback(Device, netvscPacket); | |
1273 | ||
1274 | NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext); | |
1275 | } | |
1276 | ||
1277 | ASSERT(IsListEmpty(&listHead)); | |
1278 | ||
1279 | PutNetDevice(Device); | |
1280 | DPRINT_EXIT(NETVSC); | |
1281 | } | |
1282 | ||
1283 | ||
1284 | static void | |
1285 | NetVscSendReceiveCompletion( | |
1286 | DEVICE_OBJECT *Device, | |
1287 | UINT64 TransactionId | |
1288 | ) | |
1289 | { | |
1290 | NVSP_MESSAGE recvcompMessage; | |
1291 | int retries=0; | |
1292 | int ret=0; | |
1293 | ||
1294 | DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx", TransactionId); | |
1295 | ||
1296 | recvcompMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacketComplete; | |
1297 | ||
1298 | // FIXME: Pass in the status | |
1299 | recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess; | |
1300 | ||
1301 | retry_send_cmplt: | |
1302 | // Send the completion | |
1303 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
1304 | &recvcompMessage, | |
1305 | sizeof(NVSP_MESSAGE), | |
1306 | TransactionId, | |
1307 | VmbusPacketTypeCompletion, | |
1308 | 0); | |
1309 | if (ret == 0) // success | |
1310 | { | |
1311 | // no-op | |
1312 | } | |
1313 | else if (ret == -1) // no more room...wait a bit and attempt to retry 3 times | |
1314 | { | |
1315 | retries++; | |
1316 | DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...retrying %d", TransactionId, retries); | |
1317 | ||
1318 | if (retries < 4) | |
1319 | { | |
1320 | Sleep(100); | |
1321 | goto retry_send_cmplt; | |
1322 | } | |
1323 | else | |
1324 | { | |
1325 | DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...give up retrying", TransactionId); | |
1326 | } | |
1327 | } | |
1328 | else | |
1329 | { | |
1330 | DPRINT_ERR(NETVSC, "unable to send receive completion pkt - %llx", TransactionId); | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | // | |
1335 | // Send a receive completion packet to RNDIS device (ie NetVsp) | |
1336 | // | |
1337 | static void | |
1338 | NetVscOnReceiveCompletion( | |
1339 | PVOID Context) | |
1340 | { | |
1341 | NETVSC_PACKET *packet = (NETVSC_PACKET*)Context; | |
1342 | DEVICE_OBJECT *device = (DEVICE_OBJECT*)packet->Device; | |
1343 | NETVSC_DEVICE* netDevice; | |
1344 | UINT64 transactionId=0; | |
1345 | BOOL fSendReceiveComp = FALSE; | |
1346 | ||
1347 | DPRINT_ENTER(NETVSC); | |
1348 | ||
1349 | ASSERT(packet->XferPagePacket); | |
1350 | ||
1351 | // Even though it seems logical to do a GetOutboundNetDevice() here to send out receive completion, | |
1352 | // we are using GetInboundNetDevice() since we may have disable outbound traffic already. | |
1353 | netDevice = GetInboundNetDevice(device); | |
1354 | if (!netDevice) | |
1355 | { | |
1356 | DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?"); | |
1357 | DPRINT_EXIT(NETVSC); | |
1358 | return; | |
1359 | } | |
1360 | ||
1361 | // Overloading use of the lock. | |
1362 | SpinlockAcquire(netDevice->ReceivePacketListLock); | |
1363 | ||
1364 | ASSERT(packet->XferPagePacket->Count > 0); | |
1365 | packet->XferPagePacket->Count--; | |
1366 | ||
1367 | // Last one in the line that represent 1 xfer page packet. | |
1368 | // Return the xfer page packet itself to the freelist | |
1369 | if (packet->XferPagePacket->Count == 0) | |
1370 | { | |
1371 | fSendReceiveComp = TRUE; | |
1372 | transactionId = packet->Completion.Recv.ReceiveCompletionTid; | |
1373 | ||
1374 | INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->XferPagePacket->ListEntry); | |
1375 | } | |
1376 | ||
1377 | // Put the packet back | |
1378 | INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry); | |
1379 | SpinlockRelease(netDevice->ReceivePacketListLock); | |
1380 | ||
1381 | // Send a receive completion for the xfer page packet | |
1382 | if (fSendReceiveComp) | |
1383 | { | |
1384 | NetVscSendReceiveCompletion(device, transactionId); | |
1385 | } | |
1386 | ||
1387 | PutNetDevice(device); | |
1388 | DPRINT_EXIT(NETVSC); | |
1389 | } | |
1390 | ||
1391 | ||
1392 | ||
1393 | void | |
1394 | NetVscOnChannelCallback( | |
1395 | PVOID Context | |
1396 | ) | |
1397 | { | |
1398 | const int netPacketSize=2048; | |
1399 | int ret=0; | |
1400 | DEVICE_OBJECT *device=(DEVICE_OBJECT*)Context; | |
1401 | NETVSC_DEVICE *netDevice; | |
1402 | ||
1403 | UINT32 bytesRecvd; | |
1404 | UINT64 requestId; | |
1405 | UCHAR packet[netPacketSize]; | |
1406 | VMPACKET_DESCRIPTOR *desc; | |
1407 | UCHAR *buffer=packet; | |
1408 | int bufferlen=netPacketSize; | |
1409 | ||
1410 | ||
1411 | DPRINT_ENTER(NETVSC); | |
1412 | ||
1413 | ASSERT(device); | |
1414 | ||
1415 | netDevice = GetInboundNetDevice(device); | |
1416 | if (!netDevice) | |
1417 | { | |
1418 | DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring inbound packets", netDevice); | |
1419 | DPRINT_EXIT(NETVSC); | |
1420 | return; | |
1421 | } | |
1422 | ||
1423 | do | |
1424 | { | |
1425 | ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(device, | |
1426 | buffer, | |
1427 | bufferlen, | |
1428 | &bytesRecvd, | |
1429 | &requestId); | |
1430 | ||
1431 | if (ret == 0) | |
1432 | { | |
1433 | if (bytesRecvd > 0) | |
1434 | { | |
1435 | DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx", bytesRecvd, requestId); | |
1436 | ||
1437 | desc = (VMPACKET_DESCRIPTOR*)buffer; | |
1438 | switch (desc->Type) | |
1439 | { | |
1440 | case VmbusPacketTypeCompletion: | |
1441 | NetVscOnSendCompletion(device, desc); | |
1442 | break; | |
1443 | ||
1444 | case VmbusPacketTypeDataUsingTransferPages: | |
1445 | NetVscOnReceive(device, desc); | |
1446 | break; | |
1447 | ||
1448 | default: | |
1449 | DPRINT_ERR(NETVSC, "unhandled packet type %d, tid %llx len %d\n", desc->Type, requestId, bytesRecvd); | |
1450 | break; | |
1451 | } | |
1452 | ||
1453 | // reset | |
1454 | if (bufferlen > netPacketSize) | |
1455 | { | |
1456 | MemFree(buffer); | |
1457 | ||
1458 | buffer = packet; | |
1459 | bufferlen = netPacketSize; | |
1460 | } | |
1461 | } | |
1462 | else | |
1463 | { | |
1464 | //DPRINT_DBG(NETVSC, "nothing else to read..."); | |
1465 | ||
1466 | // reset | |
1467 | if (bufferlen > netPacketSize) | |
1468 | { | |
1469 | MemFree(buffer); | |
1470 | ||
1471 | buffer = packet; | |
1472 | bufferlen = netPacketSize; | |
1473 | } | |
1474 | ||
1475 | break; | |
1476 | } | |
1477 | } | |
1478 | else if (ret == -2) // Handle large packet | |
1479 | { | |
1480 | buffer = MemAllocAtomic(bytesRecvd); | |
1481 | if (buffer == NULL) | |
1482 | { | |
1483 | // Try again next time around | |
1484 | DPRINT_ERR(NETVSC, "unable to allocate buffer of size (%d)!!", bytesRecvd); | |
1485 | break; | |
1486 | } | |
1487 | ||
1488 | bufferlen = bytesRecvd; | |
1489 | } | |
1490 | else | |
1491 | { | |
1492 | ASSERT(0); | |
1493 | } | |
1494 | } while (1); | |
1495 | ||
1496 | PutNetDevice(device); | |
1497 | DPRINT_EXIT(NETVSC); | |
1498 | return; | |
1499 | } |