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>
27 #include "include/logging.h"
29 #include "RndisFilter.h"
35 static const char* gDriverName="netvsc";
37 // {F8615163-DF3E-46c5-913F-F2D2F965ED0E}
38 static const GUID gNetVscDeviceType={
39 .Data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
48 DEVICE_OBJECT *Device,
63 NetVscOnChannelCallback(
68 NetVscInitializeSendBufferWithNetVsp(
73 NetVscInitializeReceiveBufferWithNetVsp(
78 NetVscDestroySendBuffer(
79 NETVSC_DEVICE *NetDevice
83 NetVscDestroyReceiveBuffer(
84 NETVSC_DEVICE *NetDevice
93 NetVscOnSendCompletion(
94 DEVICE_OBJECT *Device,
95 VMPACKET_DESCRIPTOR *Packet
100 DEVICE_OBJECT *Device,
101 NETVSC_PACKET *Packet
106 DEVICE_OBJECT *Device,
107 VMPACKET_DESCRIPTOR *Packet
111 NetVscOnReceiveCompletion(
116 NetVscSendReceiveCompletion(
117 DEVICE_OBJECT *Device,
121 static inline NETVSC_DEVICE* AllocNetDevice(DEVICE_OBJECT *Device)
123 NETVSC_DEVICE *netDevice;
125 netDevice = kzalloc(sizeof(NETVSC_DEVICE), GFP_KERNEL);
129 // Set to 2 to allow both inbound and outbound traffic
130 InterlockedCompareExchange(&netDevice->RefCount, 2, 0);
132 netDevice->Device = Device;
133 Device->Extension = netDevice;
138 static inline void FreeNetDevice(NETVSC_DEVICE *Device)
140 ASSERT(Device->RefCount == 0);
141 Device->Device->Extension = NULL;
146 // Get the net device object iff exists and its refcount > 1
147 static inline NETVSC_DEVICE* GetOutboundNetDevice(DEVICE_OBJECT *Device)
149 NETVSC_DEVICE *netDevice;
151 netDevice = (NETVSC_DEVICE*)Device->Extension;
152 if (netDevice && netDevice->RefCount > 1)
154 InterlockedIncrement(&netDevice->RefCount);
164 // Get the net device object iff exists and its refcount > 0
165 static inline NETVSC_DEVICE* GetInboundNetDevice(DEVICE_OBJECT *Device)
167 NETVSC_DEVICE *netDevice;
169 netDevice = (NETVSC_DEVICE*)Device->Extension;
170 if (netDevice && netDevice->RefCount)
172 InterlockedIncrement(&netDevice->RefCount);
182 static inline void PutNetDevice(DEVICE_OBJECT *Device)
184 NETVSC_DEVICE *netDevice;
186 netDevice = (NETVSC_DEVICE*)Device->Extension;
189 InterlockedDecrement(&netDevice->RefCount);
192 static inline NETVSC_DEVICE* ReleaseOutboundNetDevice(DEVICE_OBJECT *Device)
194 NETVSC_DEVICE *netDevice;
196 netDevice = (NETVSC_DEVICE*)Device->Extension;
197 if (netDevice == NULL)
200 // Busy wait until the ref drop to 2, then set it to 1
201 while (InterlockedCompareExchange(&netDevice->RefCount, 1, 2) != 2)
209 static inline NETVSC_DEVICE* ReleaseInboundNetDevice(DEVICE_OBJECT *Device)
211 NETVSC_DEVICE *netDevice;
213 netDevice = (NETVSC_DEVICE*)Device->Extension;
214 if (netDevice == NULL)
217 // Busy wait until the ref drop to 1, then set it to 0
218 while (InterlockedCompareExchange(&netDevice->RefCount, 0, 1) != 1)
223 Device->Extension = NULL;
242 NETVSC_DRIVER_OBJECT* driver = (NETVSC_DRIVER_OBJECT*)drv;
245 DPRINT_ENTER(NETVSC);
247 DPRINT_DBG(NETVSC, "sizeof(NETVSC_PACKET)=%d, sizeof(NVSP_MESSAGE)=%d, sizeof(VMTRANSFER_PAGE_PACKET_HEADER)=%d",
248 sizeof(NETVSC_PACKET), sizeof(NVSP_MESSAGE), sizeof(VMTRANSFER_PAGE_PACKET_HEADER));
250 // Make sure we are at least 2 pages since 1 page is used for control
251 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
253 drv->name = gDriverName;
254 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(GUID));
256 // Make sure it is set by the caller
257 ASSERT(driver->OnReceiveCallback);
258 ASSERT(driver->OnLinkStatusChanged);
260 // Setup the dispatch table
261 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
262 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
263 driver->Base.OnCleanup = NetVscOnCleanup;
265 driver->OnSend = NetVscOnSend;
267 RndisFilterInit(driver);
275 NetVscInitializeReceiveBufferWithNetVsp(
276 DEVICE_OBJECT *Device
280 NETVSC_DEVICE *netDevice;
281 NVSP_MESSAGE *initPacket;
283 DPRINT_ENTER(NETVSC);
285 netDevice = GetOutboundNetDevice(Device);
288 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
292 ASSERT(netDevice->ReceiveBufferSize > 0);
293 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity
295 netDevice->ReceiveBuffer = PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
296 if (!netDevice->ReceiveBuffer)
298 DPRINT_ERR(NETVSC, "unable to allocate receive buffer of size %d", netDevice->ReceiveBufferSize);
302 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer
304 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
306 // Establish the gpadl handle for this buffer on this channel.
307 // Note: This call uses the vmbus connection rather than the channel to establish
309 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
310 netDevice->ReceiveBuffer,
311 netDevice->ReceiveBufferSize,
312 &netDevice->ReceiveBufferGpadlHandle);
316 DPRINT_ERR(NETVSC, "unable to establish receive buffer's gpadl");
320 //WaitEventWait(ext->ChannelInitEvent);
322 // Notify the NetVsp of the gpadl handle
323 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
325 initPacket = &netDevice->ChannelInitPacket;
327 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
329 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
330 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
331 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
333 // Send the gpadl notification request
334 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
336 sizeof(NVSP_MESSAGE),
337 (unsigned long)initPacket,
338 VmbusPacketTypeDataInBand,
339 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
342 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
346 WaitEventWait(netDevice->ChannelInitEvent);
348 // Check the response
349 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess)
352 "Unable to complete receive buffer initialzation with NetVsp - status %d",
353 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
358 // Parse the response
359 ASSERT(netDevice->ReceiveSectionCount == 0);
360 ASSERT(netDevice->ReceiveSections == NULL);
362 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
364 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION), GFP_KERNEL);
365 if (netDevice->ReceiveSections == NULL)
371 memcpy(netDevice->ReceiveSections,
372 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
373 netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION));
376 "Receive sections info (count %d, offset %d, endoffset %d, suballoc size %d, num suballocs %d)",
377 netDevice->ReceiveSectionCount, netDevice->ReceiveSections[0].Offset, netDevice->ReceiveSections[0].EndOffset,
378 netDevice->ReceiveSections[0].SubAllocationSize, netDevice->ReceiveSections[0].NumSubAllocations);
381 //For 1st release, there should only be 1 section that represents the entire receive buffer
382 if (netDevice->ReceiveSectionCount != 1 ||
383 netDevice->ReceiveSections->Offset != 0 )
392 NetVscDestroyReceiveBuffer(netDevice);
395 PutNetDevice(Device);
402 NetVscInitializeSendBufferWithNetVsp(
403 DEVICE_OBJECT *Device
407 NETVSC_DEVICE *netDevice;
408 NVSP_MESSAGE *initPacket;
410 DPRINT_ENTER(NETVSC);
412 netDevice = GetOutboundNetDevice(Device);
415 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
419 ASSERT(netDevice->SendBufferSize > 0);
420 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity
422 netDevice->SendBuffer = PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
423 if (!netDevice->SendBuffer)
425 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d", netDevice->SendBufferSize);
429 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer
431 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
433 // Establish the gpadl handle for this buffer on this channel.
434 // Note: This call uses the vmbus connection rather than the channel to establish
436 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
437 netDevice->SendBuffer,
438 netDevice->SendBufferSize,
439 &netDevice->SendBufferGpadlHandle);
443 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
447 //WaitEventWait(ext->ChannelInitEvent);
449 // Notify the NetVsp of the gpadl handle
450 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
452 initPacket = &netDevice->ChannelInitPacket;
454 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
456 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
457 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
458 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
460 // Send the gpadl notification request
461 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
463 sizeof(NVSP_MESSAGE),
464 (unsigned long)initPacket,
465 VmbusPacketTypeDataInBand,
466 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
469 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
473 WaitEventWait(netDevice->ChannelInitEvent);
475 // Check the response
476 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess)
479 "Unable to complete send buffer initialzation with NetVsp - status %d",
480 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
485 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
490 NetVscDestroySendBuffer(netDevice);
493 PutNetDevice(Device);
499 NetVscDestroyReceiveBuffer(
500 NETVSC_DEVICE *NetDevice
503 NVSP_MESSAGE *revokePacket;
507 DPRINT_ENTER(NETVSC);
509 // If we got a section count, it means we received a SendReceiveBufferComplete msg
510 // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here
511 if (NetDevice->ReceiveSectionCount)
513 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeReceiveBuffer...");
515 // Send the revoke receive buffer
516 revokePacket = &NetDevice->RevokePacket;
517 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
519 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
520 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
522 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
524 sizeof(NVSP_MESSAGE),
525 (unsigned long)revokePacket,
526 VmbusPacketTypeDataInBand,
528 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
531 DPRINT_ERR(NETVSC, "unable to send revoke receive buffer to netvsp");
537 // Teardown the gpadl on the vsp end
538 if (NetDevice->ReceiveBufferGpadlHandle)
540 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
542 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
543 NetDevice->ReceiveBufferGpadlHandle);
545 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
548 DPRINT_ERR(NETVSC, "unable to teardown receive buffer's gpadl");
552 NetDevice->ReceiveBufferGpadlHandle = 0;
555 if (NetDevice->ReceiveBuffer)
557 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
559 // Free up the receive buffer
560 PageFree(NetDevice->ReceiveBuffer, NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
561 NetDevice->ReceiveBuffer = NULL;
564 if (NetDevice->ReceiveSections)
566 kfree(NetDevice->ReceiveSections);
567 NetDevice->ReceiveSections = NULL;
568 NetDevice->ReceiveSectionCount = 0;
580 NetVscDestroySendBuffer(
581 NETVSC_DEVICE *NetDevice
584 NVSP_MESSAGE *revokePacket;
588 DPRINT_ENTER(NETVSC);
590 // If we got a section count, it means we received a SendReceiveBufferComplete msg
591 // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here
592 if (NetDevice->SendSectionSize)
594 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeSendBuffer...");
596 // Send the revoke send buffer
597 revokePacket = &NetDevice->RevokePacket;
598 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
600 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
601 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
603 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
605 sizeof(NVSP_MESSAGE),
606 (unsigned long)revokePacket,
607 VmbusPacketTypeDataInBand,
609 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
612 DPRINT_ERR(NETVSC, "unable to send revoke send buffer to netvsp");
618 // Teardown the gpadl on the vsp end
619 if (NetDevice->SendBufferGpadlHandle)
621 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
623 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
624 NetDevice->SendBufferGpadlHandle);
626 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
629 DPRINT_ERR(NETVSC, "unable to teardown send buffer's gpadl");
633 NetDevice->SendBufferGpadlHandle = 0;
636 if (NetDevice->SendBuffer)
638 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
640 // Free up the receive buffer
641 PageFree(NetDevice->SendBuffer, NetDevice->SendBufferSize >> PAGE_SHIFT);
642 NetDevice->SendBuffer = NULL;
654 DEVICE_OBJECT *Device
658 NETVSC_DEVICE *netDevice;
659 NVSP_MESSAGE *initPacket;
662 DPRINT_ENTER(NETVSC);
664 netDevice = GetOutboundNetDevice(Device);
667 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
672 initPacket = &netDevice->ChannelInitPacket;
674 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
675 initPacket->Header.MessageType = NvspMessageTypeInit;
676 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
677 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
679 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
681 // Send the init request
682 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
684 sizeof(NVSP_MESSAGE),
685 (unsigned long)initPacket,
686 VmbusPacketTypeDataInBand,
687 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
691 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
695 WaitEventWait(netDevice->ChannelInitEvent);
697 // Now, check the response
698 //ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT);
699 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
700 initPacket->Messages.InitMessages.InitComplete.Status,
701 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
703 if (initPacket->Messages.InitMessages.InitComplete.Status != NvspStatusSuccess)
705 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (status 0x%x)", initPacket->Messages.InitMessages.InitComplete.Status);
710 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1)
712 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (version expected 1 got %d)",
713 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
717 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
719 // Send the ndis version
720 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
722 ndisVersion = 0x00050000;
724 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
725 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion = (ndisVersion & 0xFFFF0000) >> 16;
726 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion = ndisVersion & 0xFFFF;
728 // Send the init request
729 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
731 sizeof(NVSP_MESSAGE),
732 (unsigned long)initPacket,
733 VmbusPacketTypeDataInBand,
737 DPRINT_ERR(NETVSC, "unable to send NvspMessage1TypeSendNdisVersion");
742 // BUGBUG - We have to wait for the above msg since the netvsp uses KMCL which acknowledges packet (completion packet)
743 // since our Vmbus always set the VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
744 //WaitEventWait(NetVscChannel->ChannelInitEvent);
746 // Post the big receive buffer to NetVSP
747 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
750 ret = NetVscInitializeSendBufferWithNetVsp(Device);
754 PutNetDevice(Device);
760 NetVscDisconnectFromVsp(
761 NETVSC_DEVICE *NetDevice
764 DPRINT_ENTER(NETVSC);
766 NetVscDestroyReceiveBuffer(NetDevice);
767 NetVscDestroySendBuffer(NetDevice);
779 Callback when the device belonging to this driver is added
784 DEVICE_OBJECT *Device,
791 NETVSC_DEVICE* netDevice;
792 NETVSC_PACKET* packet;
795 NETVSC_DRIVER_OBJECT *netDriver = (NETVSC_DRIVER_OBJECT*) Device->Driver;;
797 DPRINT_ENTER(NETVSC);
799 netDevice = AllocNetDevice(Device);
806 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
808 // Initialize the NetVSC channel extension
809 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
810 spin_lock_init(&netDevice->receive_packet_list_lock);
812 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
814 INITIALIZE_LIST_HEAD(&netDevice->ReceivePacketList);
816 for (i=0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++)
818 packet = kzalloc(sizeof(NETVSC_PACKET) + (NETVSC_RECEIVE_SG_COUNT* sizeof(PAGE_BUFFER)), GFP_KERNEL);
821 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts for receive pool (wanted %d got %d)", NETVSC_RECEIVE_PACKETLIST_COUNT, i);
825 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
827 netDevice->ChannelInitEvent = WaitEventCreate();
830 ret = Device->Driver->VmbusChannelInterface.Open(Device,
831 netDriver->RingBufferSize,
832 netDriver->RingBufferSize,
834 NetVscOnChannelCallback,
840 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
846 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
848 // Connect with the NetVsp
849 ret = NetVscConnectToVsp(Device);
852 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
857 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***", ret);
863 // Now, we can close the channel safely
864 Device->Driver->VmbusChannelInterface.Close(Device);
870 WaitEventClose(netDevice->ChannelInitEvent);
872 while (!IsListEmpty(&netDevice->ReceivePacketList))
874 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
875 packet = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
879 ReleaseOutboundNetDevice(Device);
880 ReleaseInboundNetDevice(Device);
882 FreeNetDevice(netDevice);
893 NetVscOnDeviceRemove()
896 Callback when the root bus device is removed
900 NetVscOnDeviceRemove(
901 DEVICE_OBJECT *Device
904 NETVSC_DEVICE *netDevice;
905 NETVSC_PACKET *netvscPacket;
909 DPRINT_ENTER(NETVSC);
911 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...", Device->Extension);
913 // Stop outbound traffic ie sends and receives completions
914 netDevice = ReleaseOutboundNetDevice(Device);
917 DPRINT_ERR(NETVSC, "No net device present!!");
921 // Wait for all send completions
922 while (netDevice->NumOutstandingSends)
924 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...", netDevice->NumOutstandingSends);
929 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
931 NetVscDisconnectFromVsp(netDevice);
933 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...", Device->Extension);
935 // Stop inbound traffic ie receives and sends completions
936 netDevice = ReleaseInboundNetDevice(Device);
938 // At this point, no one should be accessing netDevice except in here
939 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
941 // Now, we can close the channel safely
942 Device->Driver->VmbusChannelInterface.Close(Device);
944 // Release all resources
945 while (!IsListEmpty(&netDevice->ReceivePacketList))
947 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
948 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
953 WaitEventClose(netDevice->ChannelInitEvent);
954 FreeNetDevice(netDevice);
968 Perform any cleanup when the driver is removed
976 DPRINT_ENTER(NETVSC);
982 NetVscOnSendCompletion(
983 DEVICE_OBJECT *Device,
984 VMPACKET_DESCRIPTOR *Packet
987 NETVSC_DEVICE* netDevice;
988 NVSP_MESSAGE *nvspPacket;
989 NETVSC_PACKET *nvscPacket;
991 DPRINT_ENTER(NETVSC);
993 netDevice = GetInboundNetDevice(Device);
996 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1001 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1003 DPRINT_DBG(NETVSC, "send completion packet - type %d", nvspPacket->Header.MessageType);
1005 if (nvspPacket->Header.MessageType == NvspMessageTypeInitComplete ||
1006 nvspPacket->Header.MessageType == NvspMessage1TypeSendReceiveBufferComplete ||
1007 nvspPacket->Header.MessageType == NvspMessage1TypeSendSendBufferComplete)
1009 // Copy the response back
1010 memcpy(&netDevice->ChannelInitPacket, nvspPacket, sizeof(NVSP_MESSAGE));
1011 WaitEventSet(netDevice->ChannelInitEvent);
1013 else if (nvspPacket->Header.MessageType == NvspMessage1TypeSendRNDISPacketComplete)
1015 // Get the send context
1016 nvscPacket = (NETVSC_PACKET *)(unsigned long)Packet->TransactionId;
1019 // Notify the layer above us
1020 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
1022 InterlockedDecrement(&netDevice->NumOutstandingSends);
1026 DPRINT_ERR(NETVSC, "Unknown send completion packet type - %d received!!", nvspPacket->Header.MessageType);
1029 PutNetDevice(Device);
1030 DPRINT_EXIT(NETVSC);
1037 DEVICE_OBJECT *Device,
1038 NETVSC_PACKET *Packet
1041 NETVSC_DEVICE* netDevice;
1044 NVSP_MESSAGE sendMessage;
1046 DPRINT_ENTER(NETVSC);
1048 netDevice = GetOutboundNetDevice(Device);
1051 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring outbound packets", netDevice);
1052 DPRINT_EXIT(NETVSC);
1056 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
1057 if (Packet->IsDataPacket)
1058 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;// 0 is RMC_DATA;
1060 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;// 1 is RMC_CONTROL;
1062 // Not using send buffer section
1063 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
1064 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
1066 if (Packet->PageBufferCount)
1068 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(Device,
1069 Packet->PageBuffers,
1070 Packet->PageBufferCount,
1072 sizeof(NVSP_MESSAGE),
1073 (unsigned long)Packet);
1077 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1079 sizeof(NVSP_MESSAGE),
1080 (unsigned long)Packet,
1081 VmbusPacketTypeDataInBand,
1082 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1088 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d", Packet, ret);
1091 InterlockedIncrement(&netDevice->NumOutstandingSends);
1092 PutNetDevice(Device);
1094 DPRINT_EXIT(NETVSC);
1101 DEVICE_OBJECT *Device,
1102 VMPACKET_DESCRIPTOR *Packet
1105 NETVSC_DEVICE* netDevice;
1106 VMTRANSFER_PAGE_PACKET_HEADER *vmxferpagePacket;
1107 NVSP_MESSAGE *nvspPacket;
1108 NETVSC_PACKET *netvscPacket=NULL;
1110 unsigned long start;
1111 unsigned long end, endVirtual;
1112 //NETVSC_DRIVER_OBJECT *netvscDriver;
1113 XFERPAGE_PACKET *xferpagePacket=NULL;
1114 LIST_ENTRY listHead;
1117 int count=0, bytesRemain=0;
1118 unsigned long flags;
1120 DPRINT_ENTER(NETVSC);
1122 netDevice = GetInboundNetDevice(Device);
1125 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1126 DPRINT_EXIT(NETVSC);
1130 // All inbound packets other than send completion should be xfer page packet
1131 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages)
1133 DPRINT_ERR(NETVSC, "Unknown packet type received - %d", Packet->Type);
1134 PutNetDevice(Device);
1138 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1140 // Make sure this is a valid nvsp packet
1141 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket )
1143 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d", nvspPacket->Header.MessageType);
1144 PutNetDevice(Device);
1148 DPRINT_DBG(NETVSC, "NVSP packet received - type %d", nvspPacket->Header.MessageType);
1150 vmxferpagePacket = (VMTRANSFER_PAGE_PACKET_HEADER*)Packet;
1152 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID)
1154 DPRINT_ERR(NETVSC, "Invalid xfer page set id - expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, vmxferpagePacket->TransferPageSetId);
1155 PutNetDevice(Device);
1159 DPRINT_DBG(NETVSC, "xfer page - range count %d", vmxferpagePacket->RangeCount);
1161 INITIALIZE_LIST_HEAD(&listHead);
1163 // Grab free packets (range count + 1) to represent this xfer page packet. +1 to represent
1164 // the xfer page packet itself. We grab it here so that we know exactly how many we can fulfil
1165 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1166 while (!IsListEmpty(&netDevice->ReceivePacketList))
1168 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
1169 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1171 INSERT_TAIL_LIST(&listHead, &netvscPacket->ListEntry);
1173 if (++count == vmxferpagePacket->RangeCount + 1)
1176 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1178 // We need at least 2 netvsc pkts (1 to represent the xfer page and at least 1 for the range)
1179 // i.e. we can handled some of the xfer page packet ranges...
1182 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. Dropping this xfer page packet completely!", count, vmxferpagePacket->RangeCount+1);
1184 // Return it to the freelist
1185 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1186 for (i=count; i != 0; i--)
1188 entry = REMOVE_HEAD_LIST(&listHead);
1189 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1191 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &netvscPacket->ListEntry);
1193 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1195 NetVscSendReceiveCompletion(Device, vmxferpagePacket->d.TransactionId);
1197 PutNetDevice(Device);
1201 // Remove the 1st packet to represent the xfer page packet itself
1202 entry = REMOVE_HEAD_LIST(&listHead);
1203 xferpagePacket = CONTAINING_RECORD(entry, XFERPAGE_PACKET, ListEntry);
1204 xferpagePacket->Count = count - 1; // This is how much we can satisfy
1205 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= vmxferpagePacket->RangeCount);
1207 if (xferpagePacket->Count != vmxferpagePacket->RangeCount)
1209 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer page...got %d", vmxferpagePacket->RangeCount, xferpagePacket->Count);
1212 // Each range represents 1 RNDIS pkt that contains 1 ethernet frame
1213 for (i=0; i < (count - 1); i++)
1215 entry = REMOVE_HEAD_LIST(&listHead);
1216 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1218 // Initialize the netvsc packet
1219 netvscPacket->XferPagePacket = xferpagePacket;
1220 netvscPacket->Completion.Recv.OnReceiveCompletion = NetVscOnReceiveCompletion;
1221 netvscPacket->Completion.Recv.ReceiveCompletionContext = netvscPacket;
1222 netvscPacket->Device = Device;
1223 netvscPacket->Completion.Recv.ReceiveCompletionTid = vmxferpagePacket->d.TransactionId; // Save this so that we can send it back
1225 netvscPacket->TotalDataBufferLength = vmxferpagePacket->Ranges[i].ByteCount;
1226 netvscPacket->PageBufferCount = 1;
1228 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + vmxferpagePacket->Ranges[i].ByteCount < netDevice->ReceiveBufferSize);
1230 netvscPacket->PageBuffers[0].Length = vmxferpagePacket->Ranges[i].ByteCount;
1232 start = GetPhysicalAddress((void*)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1234 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1235 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1236 + vmxferpagePacket->Ranges[i].ByteOffset
1237 + vmxferpagePacket->Ranges[i].ByteCount -1;
1238 end = GetPhysicalAddress((void*)endVirtual);
1240 // Calculate the page relative offset
1241 netvscPacket->PageBuffers[0].Offset = vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE -1);
1242 if ((end >> PAGE_SHIFT) != (start>>PAGE_SHIFT)) {
1243 //Handle frame across multiple pages:
1244 netvscPacket->PageBuffers[0].Length =
1245 (netvscPacket->PageBuffers[0].Pfn <<PAGE_SHIFT) + PAGE_SIZE - start;
1246 bytesRemain = netvscPacket->TotalDataBufferLength - netvscPacket->PageBuffers[0].Length;
1247 for (j=1; j<NETVSC_PACKET_MAXPAGE; j++) {
1248 netvscPacket->PageBuffers[j].Offset = 0;
1249 if (bytesRemain <= PAGE_SIZE) {
1250 netvscPacket->PageBuffers[j].Length = bytesRemain;
1253 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1254 bytesRemain -= PAGE_SIZE;
1256 netvscPacket->PageBuffers[j].Pfn =
1257 GetPhysicalAddress((void*)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1258 netvscPacket->PageBufferCount++;
1259 if (bytesRemain == 0)
1262 ASSERT(bytesRemain == 0);
1264 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => (pfn %llx, offset %u, len %u)",
1266 vmxferpagePacket->Ranges[i].ByteOffset,
1267 vmxferpagePacket->Ranges[i].ByteCount,
1268 netvscPacket->PageBuffers[0].Pfn,
1269 netvscPacket->PageBuffers[0].Offset,
1270 netvscPacket->PageBuffers[0].Length);
1272 // Pass it to the upper layer
1273 ((NETVSC_DRIVER_OBJECT*)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1275 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1278 ASSERT(IsListEmpty(&listHead));
1280 PutNetDevice(Device);
1281 DPRINT_EXIT(NETVSC);
1286 NetVscSendReceiveCompletion(
1287 DEVICE_OBJECT *Device,
1291 NVSP_MESSAGE recvcompMessage;
1295 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx", TransactionId);
1297 recvcompMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacketComplete;
1299 // FIXME: Pass in the status
1300 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1303 // Send the completion
1304 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1306 sizeof(NVSP_MESSAGE),
1308 VmbusPacketTypeCompletion,
1310 if (ret == 0) // success
1314 else if (ret == -1) // no more room...wait a bit and attempt to retry 3 times
1317 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...retrying %d", TransactionId, retries);
1322 goto retry_send_cmplt;
1326 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...give up retrying", TransactionId);
1331 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - %llx", TransactionId);
1336 // Send a receive completion packet to RNDIS device (ie NetVsp)
1339 NetVscOnReceiveCompletion(
1342 NETVSC_PACKET *packet = (NETVSC_PACKET*)Context;
1343 DEVICE_OBJECT *device = (DEVICE_OBJECT*)packet->Device;
1344 NETVSC_DEVICE* netDevice;
1345 u64 transactionId=0;
1346 bool fSendReceiveComp = false;
1347 unsigned long flags;
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 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
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 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
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 = kmalloc(bytesRecvd, GFP_ATOMIC);
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);