3 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
19 * Hank Janssen <hjanssen@microsoft.com>
25 #include <linux/kernel.h>
26 #include "include/logging.h"
28 #include "RndisFilter.h"
34 static const char* gDriverName="netvsc";
36 // {F8615163-DF3E-46c5-913F-F2D2F965ED0E}
37 static const GUID gNetVscDeviceType={
38 .Data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
47 DEVICE_OBJECT *Device,
62 NetVscOnChannelCallback(
67 NetVscInitializeSendBufferWithNetVsp(
72 NetVscInitializeReceiveBufferWithNetVsp(
77 NetVscDestroySendBuffer(
78 NETVSC_DEVICE *NetDevice
82 NetVscDestroyReceiveBuffer(
83 NETVSC_DEVICE *NetDevice
92 NetVscOnSendCompletion(
93 DEVICE_OBJECT *Device,
94 VMPACKET_DESCRIPTOR *Packet
99 DEVICE_OBJECT *Device,
100 NETVSC_PACKET *Packet
105 DEVICE_OBJECT *Device,
106 VMPACKET_DESCRIPTOR *Packet
110 NetVscOnReceiveCompletion(
115 NetVscSendReceiveCompletion(
116 DEVICE_OBJECT *Device,
120 static inline NETVSC_DEVICE* AllocNetDevice(DEVICE_OBJECT *Device)
122 NETVSC_DEVICE *netDevice;
124 netDevice = MemAllocZeroed(sizeof(NETVSC_DEVICE));
128 // Set to 2 to allow both inbound and outbound traffic
129 InterlockedCompareExchange(&netDevice->RefCount, 2, 0);
131 netDevice->Device = Device;
132 Device->Extension = netDevice;
137 static inline void FreeNetDevice(NETVSC_DEVICE *Device)
139 ASSERT(Device->RefCount == 0);
140 Device->Device->Extension = NULL;
145 // Get the net device object iff exists and its refcount > 1
146 static inline NETVSC_DEVICE* GetOutboundNetDevice(DEVICE_OBJECT *Device)
148 NETVSC_DEVICE *netDevice;
150 netDevice = (NETVSC_DEVICE*)Device->Extension;
151 if (netDevice && netDevice->RefCount > 1)
153 InterlockedIncrement(&netDevice->RefCount);
163 // Get the net device object iff exists and its refcount > 0
164 static inline NETVSC_DEVICE* GetInboundNetDevice(DEVICE_OBJECT *Device)
166 NETVSC_DEVICE *netDevice;
168 netDevice = (NETVSC_DEVICE*)Device->Extension;
169 if (netDevice && netDevice->RefCount)
171 InterlockedIncrement(&netDevice->RefCount);
181 static inline void PutNetDevice(DEVICE_OBJECT *Device)
183 NETVSC_DEVICE *netDevice;
185 netDevice = (NETVSC_DEVICE*)Device->Extension;
188 InterlockedDecrement(&netDevice->RefCount);
191 static inline NETVSC_DEVICE* ReleaseOutboundNetDevice(DEVICE_OBJECT *Device)
193 NETVSC_DEVICE *netDevice;
195 netDevice = (NETVSC_DEVICE*)Device->Extension;
196 if (netDevice == NULL)
199 // Busy wait until the ref drop to 2, then set it to 1
200 while (InterlockedCompareExchange(&netDevice->RefCount, 1, 2) != 2)
208 static inline NETVSC_DEVICE* ReleaseInboundNetDevice(DEVICE_OBJECT *Device)
210 NETVSC_DEVICE *netDevice;
212 netDevice = (NETVSC_DEVICE*)Device->Extension;
213 if (netDevice == NULL)
216 // Busy wait until the ref drop to 1, then set it to 0
217 while (InterlockedCompareExchange(&netDevice->RefCount, 0, 1) != 1)
222 Device->Extension = NULL;
241 NETVSC_DRIVER_OBJECT* driver = (NETVSC_DRIVER_OBJECT*)drv;
244 DPRINT_ENTER(NETVSC);
246 DPRINT_DBG(NETVSC, "sizeof(NETVSC_PACKET)=%d, sizeof(NVSP_MESSAGE)=%d, sizeof(VMTRANSFER_PAGE_PACKET_HEADER)=%d",
247 sizeof(NETVSC_PACKET), sizeof(NVSP_MESSAGE), sizeof(VMTRANSFER_PAGE_PACKET_HEADER));
249 // Make sure we are at least 2 pages since 1 page is used for control
250 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
252 drv->name = gDriverName;
253 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(GUID));
255 // Make sure it is set by the caller
256 ASSERT(driver->OnReceiveCallback);
257 ASSERT(driver->OnLinkStatusChanged);
259 // Setup the dispatch table
260 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
261 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
262 driver->Base.OnCleanup = NetVscOnCleanup;
264 driver->OnSend = NetVscOnSend;
266 RndisFilterInit(driver);
274 NetVscInitializeReceiveBufferWithNetVsp(
275 DEVICE_OBJECT *Device
279 NETVSC_DEVICE *netDevice;
280 NVSP_MESSAGE *initPacket;
282 DPRINT_ENTER(NETVSC);
284 netDevice = GetOutboundNetDevice(Device);
287 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
291 ASSERT(netDevice->ReceiveBufferSize > 0);
292 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity
294 netDevice->ReceiveBuffer = PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
295 if (!netDevice->ReceiveBuffer)
297 DPRINT_ERR(NETVSC, "unable to allocate receive buffer of size %d", netDevice->ReceiveBufferSize);
301 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer
303 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
305 // Establish the gpadl handle for this buffer on this channel.
306 // Note: This call uses the vmbus connection rather than the channel to establish
308 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
309 netDevice->ReceiveBuffer,
310 netDevice->ReceiveBufferSize,
311 &netDevice->ReceiveBufferGpadlHandle);
315 DPRINT_ERR(NETVSC, "unable to establish receive buffer's gpadl");
319 //WaitEventWait(ext->ChannelInitEvent);
321 // Notify the NetVsp of the gpadl handle
322 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
324 initPacket = &netDevice->ChannelInitPacket;
326 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
328 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
329 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
330 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
332 // Send the gpadl notification request
333 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
335 sizeof(NVSP_MESSAGE),
336 (unsigned long)initPacket,
337 VmbusPacketTypeDataInBand,
338 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
341 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
345 WaitEventWait(netDevice->ChannelInitEvent);
347 // Check the response
348 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess)
351 "Unable to complete receive buffer initialzation with NetVsp - status %d",
352 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
357 // Parse the response
358 ASSERT(netDevice->ReceiveSectionCount == 0);
359 ASSERT(netDevice->ReceiveSections == NULL);
361 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
363 netDevice->ReceiveSections = MemAlloc(netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION));
364 if (netDevice->ReceiveSections == NULL)
370 memcpy(netDevice->ReceiveSections,
371 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
372 netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION));
375 "Receive sections info (count %d, offset %d, endoffset %d, suballoc size %d, num suballocs %d)",
376 netDevice->ReceiveSectionCount, netDevice->ReceiveSections[0].Offset, netDevice->ReceiveSections[0].EndOffset,
377 netDevice->ReceiveSections[0].SubAllocationSize, netDevice->ReceiveSections[0].NumSubAllocations);
380 //For 1st release, there should only be 1 section that represents the entire receive buffer
381 if (netDevice->ReceiveSectionCount != 1 ||
382 netDevice->ReceiveSections->Offset != 0 )
391 NetVscDestroyReceiveBuffer(netDevice);
394 PutNetDevice(Device);
401 NetVscInitializeSendBufferWithNetVsp(
402 DEVICE_OBJECT *Device
406 NETVSC_DEVICE *netDevice;
407 NVSP_MESSAGE *initPacket;
409 DPRINT_ENTER(NETVSC);
411 netDevice = GetOutboundNetDevice(Device);
414 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
418 ASSERT(netDevice->SendBufferSize > 0);
419 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity
421 netDevice->SendBuffer = PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
422 if (!netDevice->SendBuffer)
424 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d", netDevice->SendBufferSize);
428 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer
430 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
432 // Establish the gpadl handle for this buffer on this channel.
433 // Note: This call uses the vmbus connection rather than the channel to establish
435 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
436 netDevice->SendBuffer,
437 netDevice->SendBufferSize,
438 &netDevice->SendBufferGpadlHandle);
442 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
446 //WaitEventWait(ext->ChannelInitEvent);
448 // Notify the NetVsp of the gpadl handle
449 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
451 initPacket = &netDevice->ChannelInitPacket;
453 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
455 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
456 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
457 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
459 // Send the gpadl notification request
460 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
462 sizeof(NVSP_MESSAGE),
463 (unsigned long)initPacket,
464 VmbusPacketTypeDataInBand,
465 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
468 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
472 WaitEventWait(netDevice->ChannelInitEvent);
474 // Check the response
475 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess)
478 "Unable to complete send buffer initialzation with NetVsp - status %d",
479 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
484 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
489 NetVscDestroySendBuffer(netDevice);
492 PutNetDevice(Device);
498 NetVscDestroyReceiveBuffer(
499 NETVSC_DEVICE *NetDevice
502 NVSP_MESSAGE *revokePacket;
506 DPRINT_ENTER(NETVSC);
508 // If we got a section count, it means we received a SendReceiveBufferComplete msg
509 // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here
510 if (NetDevice->ReceiveSectionCount)
512 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeReceiveBuffer...");
514 // Send the revoke receive buffer
515 revokePacket = &NetDevice->RevokePacket;
516 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
518 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
519 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
521 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
523 sizeof(NVSP_MESSAGE),
524 (unsigned long)revokePacket,
525 VmbusPacketTypeDataInBand,
527 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
530 DPRINT_ERR(NETVSC, "unable to send revoke receive buffer to netvsp");
536 // Teardown the gpadl on the vsp end
537 if (NetDevice->ReceiveBufferGpadlHandle)
539 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
541 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
542 NetDevice->ReceiveBufferGpadlHandle);
544 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
547 DPRINT_ERR(NETVSC, "unable to teardown receive buffer's gpadl");
551 NetDevice->ReceiveBufferGpadlHandle = 0;
554 if (NetDevice->ReceiveBuffer)
556 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
558 // Free up the receive buffer
559 PageFree(NetDevice->ReceiveBuffer, NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
560 NetDevice->ReceiveBuffer = NULL;
563 if (NetDevice->ReceiveSections)
565 MemFree(NetDevice->ReceiveSections);
566 NetDevice->ReceiveSections = NULL;
567 NetDevice->ReceiveSectionCount = 0;
579 NetVscDestroySendBuffer(
580 NETVSC_DEVICE *NetDevice
583 NVSP_MESSAGE *revokePacket;
587 DPRINT_ENTER(NETVSC);
589 // If we got a section count, it means we received a SendReceiveBufferComplete msg
590 // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here
591 if (NetDevice->SendSectionSize)
593 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeSendBuffer...");
595 // Send the revoke send buffer
596 revokePacket = &NetDevice->RevokePacket;
597 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
599 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
600 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
602 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
604 sizeof(NVSP_MESSAGE),
605 (unsigned long)revokePacket,
606 VmbusPacketTypeDataInBand,
608 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
611 DPRINT_ERR(NETVSC, "unable to send revoke send buffer to netvsp");
617 // Teardown the gpadl on the vsp end
618 if (NetDevice->SendBufferGpadlHandle)
620 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
622 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
623 NetDevice->SendBufferGpadlHandle);
625 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
628 DPRINT_ERR(NETVSC, "unable to teardown send buffer's gpadl");
632 NetDevice->SendBufferGpadlHandle = 0;
635 if (NetDevice->SendBuffer)
637 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
639 // Free up the receive buffer
640 PageFree(NetDevice->SendBuffer, NetDevice->SendBufferSize >> PAGE_SHIFT);
641 NetDevice->SendBuffer = NULL;
653 DEVICE_OBJECT *Device
657 NETVSC_DEVICE *netDevice;
658 NVSP_MESSAGE *initPacket;
661 DPRINT_ENTER(NETVSC);
663 netDevice = GetOutboundNetDevice(Device);
666 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
671 initPacket = &netDevice->ChannelInitPacket;
673 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
674 initPacket->Header.MessageType = NvspMessageTypeInit;
675 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
676 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
678 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
680 // Send the init request
681 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
683 sizeof(NVSP_MESSAGE),
684 (unsigned long)initPacket,
685 VmbusPacketTypeDataInBand,
686 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
690 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
694 WaitEventWait(netDevice->ChannelInitEvent);
696 // Now, check the response
697 //ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT);
698 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
699 initPacket->Messages.InitMessages.InitComplete.Status,
700 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
702 if (initPacket->Messages.InitMessages.InitComplete.Status != NvspStatusSuccess)
704 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (status 0x%x)", initPacket->Messages.InitMessages.InitComplete.Status);
709 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1)
711 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (version expected 1 got %d)",
712 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
716 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
718 // Send the ndis version
719 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
721 ndisVersion = 0x00050000;
723 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
724 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion = (ndisVersion & 0xFFFF0000) >> 16;
725 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion = ndisVersion & 0xFFFF;
727 // Send the init request
728 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
730 sizeof(NVSP_MESSAGE),
731 (unsigned long)initPacket,
732 VmbusPacketTypeDataInBand,
736 DPRINT_ERR(NETVSC, "unable to send NvspMessage1TypeSendNdisVersion");
741 // BUGBUG - We have to wait for the above msg since the netvsp uses KMCL which acknowledges packet (completion packet)
742 // since our Vmbus always set the VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
743 //WaitEventWait(NetVscChannel->ChannelInitEvent);
745 // Post the big receive buffer to NetVSP
746 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
749 ret = NetVscInitializeSendBufferWithNetVsp(Device);
753 PutNetDevice(Device);
759 NetVscDisconnectFromVsp(
760 NETVSC_DEVICE *NetDevice
763 DPRINT_ENTER(NETVSC);
765 NetVscDestroyReceiveBuffer(NetDevice);
766 NetVscDestroySendBuffer(NetDevice);
778 Callback when the device belonging to this driver is added
783 DEVICE_OBJECT *Device,
790 NETVSC_DEVICE* netDevice;
791 NETVSC_PACKET* packet;
794 NETVSC_DRIVER_OBJECT *netDriver = (NETVSC_DRIVER_OBJECT*) Device->Driver;;
796 DPRINT_ENTER(NETVSC);
798 netDevice = AllocNetDevice(Device);
805 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
807 // Initialize the NetVSC channel extension
808 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
809 netDevice->ReceivePacketListLock = SpinlockCreate();
811 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
813 INITIALIZE_LIST_HEAD(&netDevice->ReceivePacketList);
815 for (i=0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++)
817 packet = MemAllocZeroed(sizeof(NETVSC_PACKET) + (NETVSC_RECEIVE_SG_COUNT* sizeof(PAGE_BUFFER)));
820 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts for receive pool (wanted %d got %d)", NETVSC_RECEIVE_PACKETLIST_COUNT, i);
824 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
826 netDevice->ChannelInitEvent = WaitEventCreate();
829 ret = Device->Driver->VmbusChannelInterface.Open(Device,
830 netDriver->RingBufferSize,
831 netDriver->RingBufferSize,
833 NetVscOnChannelCallback,
839 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
845 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
847 // Connect with the NetVsp
848 ret = NetVscConnectToVsp(Device);
851 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
856 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***", ret);
862 // Now, we can close the channel safely
863 Device->Driver->VmbusChannelInterface.Close(Device);
869 WaitEventClose(netDevice->ChannelInitEvent);
871 while (!IsListEmpty(&netDevice->ReceivePacketList))
873 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
874 packet = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
878 SpinlockClose(netDevice->ReceivePacketListLock);
880 ReleaseOutboundNetDevice(Device);
881 ReleaseInboundNetDevice(Device);
883 FreeNetDevice(netDevice);
894 NetVscOnDeviceRemove()
897 Callback when the root bus device is removed
901 NetVscOnDeviceRemove(
902 DEVICE_OBJECT *Device
905 NETVSC_DEVICE *netDevice;
906 NETVSC_PACKET *netvscPacket;
910 DPRINT_ENTER(NETVSC);
912 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...", Device->Extension);
914 // Stop outbound traffic ie sends and receives completions
915 netDevice = ReleaseOutboundNetDevice(Device);
918 DPRINT_ERR(NETVSC, "No net device present!!");
922 // Wait for all send completions
923 while (netDevice->NumOutstandingSends)
925 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...", netDevice->NumOutstandingSends);
930 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
932 NetVscDisconnectFromVsp(netDevice);
934 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...", Device->Extension);
936 // Stop inbound traffic ie receives and sends completions
937 netDevice = ReleaseInboundNetDevice(Device);
939 // At this point, no one should be accessing netDevice except in here
940 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
942 // Now, we can close the channel safely
943 Device->Driver->VmbusChannelInterface.Close(Device);
945 // Release all resources
946 while (!IsListEmpty(&netDevice->ReceivePacketList))
948 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
949 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
951 MemFree(netvscPacket);
954 SpinlockClose(netDevice->ReceivePacketListLock);
955 WaitEventClose(netDevice->ChannelInitEvent);
956 FreeNetDevice(netDevice);
970 Perform any cleanup when the driver is removed
978 DPRINT_ENTER(NETVSC);
984 NetVscOnSendCompletion(
985 DEVICE_OBJECT *Device,
986 VMPACKET_DESCRIPTOR *Packet
989 NETVSC_DEVICE* netDevice;
990 NVSP_MESSAGE *nvspPacket;
991 NETVSC_PACKET *nvscPacket;
993 DPRINT_ENTER(NETVSC);
995 netDevice = GetInboundNetDevice(Device);
998 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1003 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1005 DPRINT_DBG(NETVSC, "send completion packet - type %d", nvspPacket->Header.MessageType);
1007 if (nvspPacket->Header.MessageType == NvspMessageTypeInitComplete ||
1008 nvspPacket->Header.MessageType == NvspMessage1TypeSendReceiveBufferComplete ||
1009 nvspPacket->Header.MessageType == NvspMessage1TypeSendSendBufferComplete)
1011 // Copy the response back
1012 memcpy(&netDevice->ChannelInitPacket, nvspPacket, sizeof(NVSP_MESSAGE));
1013 WaitEventSet(netDevice->ChannelInitEvent);
1015 else if (nvspPacket->Header.MessageType == NvspMessage1TypeSendRNDISPacketComplete)
1017 // Get the send context
1018 nvscPacket = (NETVSC_PACKET *)(unsigned long)Packet->TransactionId;
1021 // Notify the layer above us
1022 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
1024 InterlockedDecrement(&netDevice->NumOutstandingSends);
1028 DPRINT_ERR(NETVSC, "Unknown send completion packet type - %d received!!", nvspPacket->Header.MessageType);
1031 PutNetDevice(Device);
1032 DPRINT_EXIT(NETVSC);
1039 DEVICE_OBJECT *Device,
1040 NETVSC_PACKET *Packet
1043 NETVSC_DEVICE* netDevice;
1046 NVSP_MESSAGE sendMessage;
1048 DPRINT_ENTER(NETVSC);
1050 netDevice = GetOutboundNetDevice(Device);
1053 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring outbound packets", netDevice);
1054 DPRINT_EXIT(NETVSC);
1058 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
1059 if (Packet->IsDataPacket)
1060 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;// 0 is RMC_DATA;
1062 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;// 1 is RMC_CONTROL;
1064 // Not using send buffer section
1065 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
1066 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
1068 if (Packet->PageBufferCount)
1070 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(Device,
1071 Packet->PageBuffers,
1072 Packet->PageBufferCount,
1074 sizeof(NVSP_MESSAGE),
1075 (unsigned long)Packet);
1079 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1081 sizeof(NVSP_MESSAGE),
1082 (unsigned long)Packet,
1083 VmbusPacketTypeDataInBand,
1084 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1090 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d", Packet, ret);
1093 InterlockedIncrement(&netDevice->NumOutstandingSends);
1094 PutNetDevice(Device);
1096 DPRINT_EXIT(NETVSC);
1103 DEVICE_OBJECT *Device,
1104 VMPACKET_DESCRIPTOR *Packet
1107 NETVSC_DEVICE* netDevice;
1108 VMTRANSFER_PAGE_PACKET_HEADER *vmxferpagePacket;
1109 NVSP_MESSAGE *nvspPacket;
1110 NETVSC_PACKET *netvscPacket=NULL;
1112 unsigned long start;
1113 unsigned long end, endVirtual;
1114 //NETVSC_DRIVER_OBJECT *netvscDriver;
1115 XFERPAGE_PACKET *xferpagePacket=NULL;
1116 LIST_ENTRY listHead;
1119 int count=0, bytesRemain=0;
1121 DPRINT_ENTER(NETVSC);
1123 netDevice = GetInboundNetDevice(Device);
1126 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1127 DPRINT_EXIT(NETVSC);
1131 // All inbound packets other than send completion should be xfer page packet
1132 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages)
1134 DPRINT_ERR(NETVSC, "Unknown packet type received - %d", Packet->Type);
1135 PutNetDevice(Device);
1139 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1141 // Make sure this is a valid nvsp packet
1142 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket )
1144 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d", nvspPacket->Header.MessageType);
1145 PutNetDevice(Device);
1149 DPRINT_DBG(NETVSC, "NVSP packet received - type %d", nvspPacket->Header.MessageType);
1151 vmxferpagePacket = (VMTRANSFER_PAGE_PACKET_HEADER*)Packet;
1153 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID)
1155 DPRINT_ERR(NETVSC, "Invalid xfer page set id - expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, vmxferpagePacket->TransferPageSetId);
1156 PutNetDevice(Device);
1160 DPRINT_DBG(NETVSC, "xfer page - range count %d", vmxferpagePacket->RangeCount);
1162 INITIALIZE_LIST_HEAD(&listHead);
1164 // Grab free packets (range count + 1) to represent this xfer page packet. +1 to represent
1165 // the xfer page packet itself. We grab it here so that we know exactly how many we can fulfil
1166 SpinlockAcquire(netDevice->ReceivePacketListLock);
1167 while (!IsListEmpty(&netDevice->ReceivePacketList))
1169 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
1170 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1172 INSERT_TAIL_LIST(&listHead, &netvscPacket->ListEntry);
1174 if (++count == vmxferpagePacket->RangeCount + 1)
1177 SpinlockRelease(netDevice->ReceivePacketListLock);
1179 // We need at least 2 netvsc pkts (1 to represent the xfer page and at least 1 for the range)
1180 // i.e. we can handled some of the xfer page packet ranges...
1183 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. Dropping this xfer page packet completely!", count, vmxferpagePacket->RangeCount+1);
1185 // Return it to the freelist
1186 SpinlockAcquire(netDevice->ReceivePacketListLock);
1187 for (i=count; i != 0; i--)
1189 entry = REMOVE_HEAD_LIST(&listHead);
1190 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1192 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &netvscPacket->ListEntry);
1194 SpinlockRelease(netDevice->ReceivePacketListLock);
1196 NetVscSendReceiveCompletion(Device, vmxferpagePacket->d.TransactionId);
1198 PutNetDevice(Device);
1202 // Remove the 1st packet to represent the xfer page packet itself
1203 entry = REMOVE_HEAD_LIST(&listHead);
1204 xferpagePacket = CONTAINING_RECORD(entry, XFERPAGE_PACKET, ListEntry);
1205 xferpagePacket->Count = count - 1; // This is how much we can satisfy
1206 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= vmxferpagePacket->RangeCount);
1208 if (xferpagePacket->Count != vmxferpagePacket->RangeCount)
1210 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer page...got %d", vmxferpagePacket->RangeCount, xferpagePacket->Count);
1213 // Each range represents 1 RNDIS pkt that contains 1 ethernet frame
1214 for (i=0; i < (count - 1); i++)
1216 entry = REMOVE_HEAD_LIST(&listHead);
1217 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1219 // Initialize the netvsc packet
1220 netvscPacket->XferPagePacket = xferpagePacket;
1221 netvscPacket->Completion.Recv.OnReceiveCompletion = NetVscOnReceiveCompletion;
1222 netvscPacket->Completion.Recv.ReceiveCompletionContext = netvscPacket;
1223 netvscPacket->Device = Device;
1224 netvscPacket->Completion.Recv.ReceiveCompletionTid = vmxferpagePacket->d.TransactionId; // Save this so that we can send it back
1226 netvscPacket->TotalDataBufferLength = vmxferpagePacket->Ranges[i].ByteCount;
1227 netvscPacket->PageBufferCount = 1;
1229 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + vmxferpagePacket->Ranges[i].ByteCount < netDevice->ReceiveBufferSize);
1231 netvscPacket->PageBuffers[0].Length = vmxferpagePacket->Ranges[i].ByteCount;
1233 start = GetPhysicalAddress((void*)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1235 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1236 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1237 + vmxferpagePacket->Ranges[i].ByteOffset
1238 + vmxferpagePacket->Ranges[i].ByteCount -1;
1239 end = GetPhysicalAddress((void*)endVirtual);
1241 // Calculate the page relative offset
1242 netvscPacket->PageBuffers[0].Offset = vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE -1);
1243 if ((end >> PAGE_SHIFT) != (start>>PAGE_SHIFT)) {
1244 //Handle frame across multiple pages:
1245 netvscPacket->PageBuffers[0].Length =
1246 (netvscPacket->PageBuffers[0].Pfn <<PAGE_SHIFT) + PAGE_SIZE - start;
1247 bytesRemain = netvscPacket->TotalDataBufferLength - netvscPacket->PageBuffers[0].Length;
1248 for (j=1; j<NETVSC_PACKET_MAXPAGE; j++) {
1249 netvscPacket->PageBuffers[j].Offset = 0;
1250 if (bytesRemain <= PAGE_SIZE) {
1251 netvscPacket->PageBuffers[j].Length = bytesRemain;
1254 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1255 bytesRemain -= PAGE_SIZE;
1257 netvscPacket->PageBuffers[j].Pfn =
1258 GetPhysicalAddress((void*)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1259 netvscPacket->PageBufferCount++;
1260 if (bytesRemain == 0)
1263 ASSERT(bytesRemain == 0);
1265 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => (pfn %llx, offset %u, len %u)",
1267 vmxferpagePacket->Ranges[i].ByteOffset,
1268 vmxferpagePacket->Ranges[i].ByteCount,
1269 netvscPacket->PageBuffers[0].Pfn,
1270 netvscPacket->PageBuffers[0].Offset,
1271 netvscPacket->PageBuffers[0].Length);
1273 // Pass it to the upper layer
1274 ((NETVSC_DRIVER_OBJECT*)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1276 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1279 ASSERT(IsListEmpty(&listHead));
1281 PutNetDevice(Device);
1282 DPRINT_EXIT(NETVSC);
1287 NetVscSendReceiveCompletion(
1288 DEVICE_OBJECT *Device,
1292 NVSP_MESSAGE recvcompMessage;
1296 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx", TransactionId);
1298 recvcompMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacketComplete;
1300 // FIXME: Pass in the status
1301 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1304 // Send the completion
1305 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1307 sizeof(NVSP_MESSAGE),
1309 VmbusPacketTypeCompletion,
1311 if (ret == 0) // success
1315 else if (ret == -1) // no more room...wait a bit and attempt to retry 3 times
1318 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...retrying %d", TransactionId, retries);
1323 goto retry_send_cmplt;
1327 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...give up retrying", TransactionId);
1332 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - %llx", TransactionId);
1337 // Send a receive completion packet to RNDIS device (ie NetVsp)
1340 NetVscOnReceiveCompletion(
1343 NETVSC_PACKET *packet = (NETVSC_PACKET*)Context;
1344 DEVICE_OBJECT *device = (DEVICE_OBJECT*)packet->Device;
1345 NETVSC_DEVICE* netDevice;
1346 u64 transactionId=0;
1347 bool fSendReceiveComp = FALSE;
1349 DPRINT_ENTER(NETVSC);
1351 ASSERT(packet->XferPagePacket);
1353 // Even though it seems logical to do a GetOutboundNetDevice() here to send out receive completion,
1354 // we are using GetInboundNetDevice() since we may have disable outbound traffic already.
1355 netDevice = GetInboundNetDevice(device);
1358 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1359 DPRINT_EXIT(NETVSC);
1363 // Overloading use of the lock.
1364 SpinlockAcquire(netDevice->ReceivePacketListLock);
1366 ASSERT(packet->XferPagePacket->Count > 0);
1367 packet->XferPagePacket->Count--;
1369 // Last one in the line that represent 1 xfer page packet.
1370 // Return the xfer page packet itself to the freelist
1371 if (packet->XferPagePacket->Count == 0)
1373 fSendReceiveComp = TRUE;
1374 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1376 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->XferPagePacket->ListEntry);
1379 // Put the packet back
1380 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
1381 SpinlockRelease(netDevice->ReceivePacketListLock);
1383 // Send a receive completion for the xfer page packet
1384 if (fSendReceiveComp)
1386 NetVscSendReceiveCompletion(device, transactionId);
1389 PutNetDevice(device);
1390 DPRINT_EXIT(NETVSC);
1396 NetVscOnChannelCallback(
1400 const int netPacketSize=2048;
1402 DEVICE_OBJECT *device=(DEVICE_OBJECT*)Context;
1403 NETVSC_DEVICE *netDevice;
1407 unsigned char packet[netPacketSize];
1408 VMPACKET_DESCRIPTOR *desc;
1409 unsigned char *buffer=packet;
1410 int bufferlen=netPacketSize;
1413 DPRINT_ENTER(NETVSC);
1417 netDevice = GetInboundNetDevice(device);
1420 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring inbound packets", netDevice);
1421 DPRINT_EXIT(NETVSC);
1427 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(device,
1437 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx", bytesRecvd, requestId);
1439 desc = (VMPACKET_DESCRIPTOR*)buffer;
1442 case VmbusPacketTypeCompletion:
1443 NetVscOnSendCompletion(device, desc);
1446 case VmbusPacketTypeDataUsingTransferPages:
1447 NetVscOnReceive(device, desc);
1451 DPRINT_ERR(NETVSC, "unhandled packet type %d, tid %llx len %d\n", desc->Type, requestId, bytesRecvd);
1456 if (bufferlen > netPacketSize)
1461 bufferlen = netPacketSize;
1466 //DPRINT_DBG(NETVSC, "nothing else to read...");
1469 if (bufferlen > netPacketSize)
1474 bufferlen = netPacketSize;
1480 else if (ret == -2) // Handle large packet
1482 buffer = MemAllocAtomic(bytesRecvd);
1485 // Try again next time around
1486 DPRINT_ERR(NETVSC, "unable to allocate buffer of size (%d)!!", bytesRecvd);
1490 bufferlen = bytesRecvd;
1498 PutNetDevice(device);
1499 DPRINT_EXIT(NETVSC);