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>
23 #include <linux/kernel.h>
25 #include <linux/delay.h>
26 #include "include/logging.h"
28 #include "RndisFilter.h"
32 static const char* gDriverName="netvsc";
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}
40 /* Internal routines */
43 struct hv_device *Device,
49 struct hv_device *Device
54 struct hv_driver *Driver
58 NetVscOnChannelCallback(
63 NetVscInitializeSendBufferWithNetVsp(
64 struct hv_device *Device
68 NetVscInitializeReceiveBufferWithNetVsp(
69 struct hv_device *Device
73 NetVscDestroySendBuffer(
74 struct NETVSC_DEVICE *NetDevice
78 NetVscDestroyReceiveBuffer(
79 struct NETVSC_DEVICE *NetDevice
84 struct hv_device *Device
88 NetVscOnSendCompletion(
89 struct hv_device *Device,
90 VMPACKET_DESCRIPTOR *Packet
95 struct hv_device *Device,
96 struct hv_netvsc_packet *Packet
101 struct hv_device *Device,
102 VMPACKET_DESCRIPTOR *Packet
106 NetVscOnReceiveCompletion(
111 NetVscSendReceiveCompletion(
112 struct hv_device *Device,
116 static inline struct NETVSC_DEVICE *AllocNetDevice(struct hv_device *Device)
118 struct NETVSC_DEVICE *netDevice;
120 netDevice = kzalloc(sizeof(struct NETVSC_DEVICE), GFP_KERNEL);
124 /* Set to 2 to allow both inbound and outbound traffic */
125 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
127 netDevice->Device = Device;
128 Device->Extension = netDevice;
133 static inline void FreeNetDevice(struct NETVSC_DEVICE *Device)
135 ASSERT(atomic_read(&Device->RefCount) == 0);
136 Device->Device->Extension = NULL;
141 /* Get the net device object iff exists and its refcount > 1 */
142 static inline struct NETVSC_DEVICE *GetOutboundNetDevice(struct hv_device *Device)
144 struct NETVSC_DEVICE *netDevice;
146 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
147 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
148 atomic_inc(&netDevice->RefCount);
155 /* Get the net device object iff exists and its refcount > 0 */
156 static inline struct NETVSC_DEVICE *GetInboundNetDevice(struct hv_device *Device)
158 struct NETVSC_DEVICE *netDevice;
160 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
161 if (netDevice && atomic_read(&netDevice->RefCount))
162 atomic_inc(&netDevice->RefCount);
169 static inline void PutNetDevice(struct hv_device *Device)
171 struct NETVSC_DEVICE *netDevice;
173 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
176 atomic_dec(&netDevice->RefCount);
179 static inline struct NETVSC_DEVICE *ReleaseOutboundNetDevice(struct hv_device *Device)
181 struct NETVSC_DEVICE *netDevice;
183 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
184 if (netDevice == NULL)
187 /* Busy wait until the ref drop to 2, then set it to 1 */
188 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
196 static inline struct NETVSC_DEVICE *ReleaseInboundNetDevice(struct hv_device *Device)
198 struct NETVSC_DEVICE *netDevice;
200 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
201 if (netDevice == NULL)
204 /* Busy wait until the ref drop to 1, then set it to 0 */
205 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
210 Device->Extension = NULL;
226 struct hv_driver *drv
229 NETVSC_DRIVER_OBJECT* driver = (NETVSC_DRIVER_OBJECT*)drv;
232 DPRINT_ENTER(NETVSC);
234 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, sizeof(NVSP_MESSAGE)=%zd, sizeof(VMTRANSFER_PAGE_PACKET_HEADER)=%zd",
235 sizeof(struct hv_netvsc_packet), sizeof(NVSP_MESSAGE), sizeof(VMTRANSFER_PAGE_PACKET_HEADER));
237 /* Make sure we are at least 2 pages since 1 page is used for control */
238 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
240 drv->name = gDriverName;
241 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(GUID));
243 /* Make sure it is set by the caller */
244 ASSERT(driver->OnReceiveCallback);
245 ASSERT(driver->OnLinkStatusChanged);
247 /* Setup the dispatch table */
248 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
249 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
250 driver->Base.OnCleanup = NetVscOnCleanup;
252 driver->OnSend = NetVscOnSend;
254 RndisFilterInit(driver);
262 NetVscInitializeReceiveBufferWithNetVsp(
263 struct hv_device *Device
267 struct NETVSC_DEVICE *netDevice;
268 NVSP_MESSAGE *initPacket;
270 DPRINT_ENTER(NETVSC);
272 netDevice = GetOutboundNetDevice(Device);
275 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
279 ASSERT(netDevice->ReceiveBufferSize > 0);
280 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE-1)) == 0); /* page-size grandularity */
282 netDevice->ReceiveBuffer = PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
283 if (!netDevice->ReceiveBuffer)
285 DPRINT_ERR(NETVSC, "unable to allocate receive buffer of size %d", netDevice->ReceiveBufferSize);
289 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE-1)) == 0); /* page-aligned buffer */
291 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
294 * Establish the gpadl handle for this buffer on this
295 * channel. Note: This call uses the vmbus connection rather
296 * than the channel to establish the gpadl handle.
298 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
299 netDevice->ReceiveBuffer,
300 netDevice->ReceiveBufferSize,
301 &netDevice->ReceiveBufferGpadlHandle);
305 DPRINT_ERR(NETVSC, "unable to establish receive buffer's gpadl");
309 /* WaitEventWait(ext->ChannelInitEvent); */
311 /* Notify the NetVsp of the gpadl handle */
312 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
314 initPacket = &netDevice->ChannelInitPacket;
316 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
318 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
319 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
320 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
322 /* Send the gpadl notification request */
323 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
325 sizeof(NVSP_MESSAGE),
326 (unsigned long)initPacket,
327 VmbusPacketTypeDataInBand,
328 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
331 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
335 WaitEventWait(netDevice->ChannelInitEvent);
337 /* Check the response */
338 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess)
341 "Unable to complete receive buffer initialzation with NetVsp - status %d",
342 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
347 /* Parse the response */
348 ASSERT(netDevice->ReceiveSectionCount == 0);
349 ASSERT(netDevice->ReceiveSections == NULL);
351 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
353 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION), GFP_KERNEL);
354 if (netDevice->ReceiveSections == NULL)
360 memcpy(netDevice->ReceiveSections,
361 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
362 netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION));
365 "Receive sections info (count %d, offset %d, endoffset %d, suballoc size %d, num suballocs %d)",
366 netDevice->ReceiveSectionCount, netDevice->ReceiveSections[0].Offset, netDevice->ReceiveSections[0].EndOffset,
367 netDevice->ReceiveSections[0].SubAllocationSize, netDevice->ReceiveSections[0].NumSubAllocations);
370 /* For 1st release, there should only be 1 section that represents the entire receive buffer */
371 if (netDevice->ReceiveSectionCount != 1 ||
372 netDevice->ReceiveSections->Offset != 0 )
381 NetVscDestroyReceiveBuffer(netDevice);
384 PutNetDevice(Device);
391 NetVscInitializeSendBufferWithNetVsp(
392 struct hv_device *Device
396 struct NETVSC_DEVICE *netDevice;
397 NVSP_MESSAGE *initPacket;
399 DPRINT_ENTER(NETVSC);
401 netDevice = GetOutboundNetDevice(Device);
404 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
408 ASSERT(netDevice->SendBufferSize > 0);
409 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE-1)) == 0); /* page-size grandularity */
411 netDevice->SendBuffer = PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
412 if (!netDevice->SendBuffer)
414 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d", netDevice->SendBufferSize);
418 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE-1)) == 0); /* page-aligned buffer */
420 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
423 * Establish the gpadl handle for this buffer on this
424 * channel. Note: This call uses the vmbus connection rather
425 * than the channel to establish the gpadl handle.
427 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
428 netDevice->SendBuffer,
429 netDevice->SendBufferSize,
430 &netDevice->SendBufferGpadlHandle);
434 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
438 /* WaitEventWait(ext->ChannelInitEvent); */
440 /* Notify the NetVsp of the gpadl handle */
441 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
443 initPacket = &netDevice->ChannelInitPacket;
445 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
447 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
448 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
449 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
451 /* Send the gpadl notification request */
452 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
454 sizeof(NVSP_MESSAGE),
455 (unsigned long)initPacket,
456 VmbusPacketTypeDataInBand,
457 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
460 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
464 WaitEventWait(netDevice->ChannelInitEvent);
466 /* Check the response */
467 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess)
470 "Unable to complete send buffer initialzation with NetVsp - status %d",
471 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
476 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
481 NetVscDestroySendBuffer(netDevice);
484 PutNetDevice(Device);
490 NetVscDestroyReceiveBuffer(
491 struct NETVSC_DEVICE *NetDevice
494 NVSP_MESSAGE *revokePacket;
498 DPRINT_ENTER(NETVSC);
501 * If we got a section count, it means we received a
502 * SendReceiveBufferComplete msg (ie sent
503 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
504 * to send a revoke msg here
506 if (NetDevice->ReceiveSectionCount)
508 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeReceiveBuffer...");
510 /* Send the revoke receive buffer */
511 revokePacket = &NetDevice->RevokePacket;
512 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
514 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
515 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
517 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
519 sizeof(NVSP_MESSAGE),
520 (unsigned long)revokePacket,
521 VmbusPacketTypeDataInBand,
524 * If we failed here, we might as well return and
525 * have a leak rather than continue and a bugchk
529 DPRINT_ERR(NETVSC, "unable to send revoke receive buffer to netvsp");
535 /* Teardown the gpadl on the vsp end */
536 if (NetDevice->ReceiveBufferGpadlHandle)
538 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
540 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
541 NetDevice->ReceiveBufferGpadlHandle);
543 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
546 DPRINT_ERR(NETVSC, "unable to teardown receive buffer's gpadl");
550 NetDevice->ReceiveBufferGpadlHandle = 0;
553 if (NetDevice->ReceiveBuffer)
555 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
557 /* Free up the receive buffer */
558 PageFree(NetDevice->ReceiveBuffer, NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
559 NetDevice->ReceiveBuffer = NULL;
562 if (NetDevice->ReceiveSections)
564 kfree(NetDevice->ReceiveSections);
565 NetDevice->ReceiveSections = NULL;
566 NetDevice->ReceiveSectionCount = 0;
578 NetVscDestroySendBuffer(
579 struct NETVSC_DEVICE *NetDevice
582 NVSP_MESSAGE *revokePacket;
586 DPRINT_ENTER(NETVSC);
589 * If we got a section count, it means we received a
590 * SendReceiveBufferComplete msg (ie sent
591 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
592 * to send a revoke msg here
594 if (NetDevice->SendSectionSize)
596 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeSendBuffer...");
598 /* Send the revoke send buffer */
599 revokePacket = &NetDevice->RevokePacket;
600 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
602 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
603 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
605 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
607 sizeof(NVSP_MESSAGE),
608 (unsigned long)revokePacket,
609 VmbusPacketTypeDataInBand,
611 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
614 DPRINT_ERR(NETVSC, "unable to send revoke send buffer to netvsp");
620 /* Teardown the gpadl on the vsp end */
621 if (NetDevice->SendBufferGpadlHandle)
623 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
625 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
626 NetDevice->SendBufferGpadlHandle);
628 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
631 DPRINT_ERR(NETVSC, "unable to teardown send buffer's gpadl");
635 NetDevice->SendBufferGpadlHandle = 0;
638 if (NetDevice->SendBuffer)
640 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
642 /* Free up the receive buffer */
643 PageFree(NetDevice->SendBuffer, NetDevice->SendBufferSize >> PAGE_SHIFT);
644 NetDevice->SendBuffer = NULL;
656 struct hv_device *Device
660 struct NETVSC_DEVICE *netDevice;
661 NVSP_MESSAGE *initPacket;
664 DPRINT_ENTER(NETVSC);
666 netDevice = GetOutboundNetDevice(Device);
669 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
674 initPacket = &netDevice->ChannelInitPacket;
676 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
677 initPacket->Header.MessageType = NvspMessageTypeInit;
678 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
679 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
681 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
683 /* Send the init request */
684 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
686 sizeof(NVSP_MESSAGE),
687 (unsigned long)initPacket,
688 VmbusPacketTypeDataInBand,
689 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
693 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
697 WaitEventWait(netDevice->ChannelInitEvent);
699 /* Now, check the response */
700 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
701 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
702 initPacket->Messages.InitMessages.InitComplete.Status,
703 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
705 if (initPacket->Messages.InitMessages.InitComplete.Status != NvspStatusSuccess)
707 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (status 0x%x)", initPacket->Messages.InitMessages.InitComplete.Status);
712 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1)
714 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (version expected 1 got %d)",
715 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
719 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
721 /* Send the ndis version */
722 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
724 ndisVersion = 0x00050000;
726 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
727 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion = (ndisVersion & 0xFFFF0000) >> 16;
728 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion = ndisVersion & 0xFFFF;
730 /* Send the init request */
731 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
733 sizeof(NVSP_MESSAGE),
734 (unsigned long)initPacket,
735 VmbusPacketTypeDataInBand,
739 DPRINT_ERR(NETVSC, "unable to send NvspMessage1TypeSendNdisVersion");
744 * BUGBUG - We have to wait for the above msg since the
745 * netvsp uses KMCL which acknowledges packet (completion
746 * packet) since our Vmbus always set the
747 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
749 /* WaitEventWait(NetVscChannel->ChannelInitEvent); */
751 /* Post the big receive buffer to NetVSP */
752 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
755 ret = NetVscInitializeSendBufferWithNetVsp(Device);
759 PutNetDevice(Device);
765 NetVscDisconnectFromVsp(
766 struct NETVSC_DEVICE *NetDevice
769 DPRINT_ENTER(NETVSC);
771 NetVscDestroyReceiveBuffer(NetDevice);
772 NetVscDestroySendBuffer(NetDevice);
784 Callback when the device belonging to this driver is added
789 struct hv_device *Device,
796 struct NETVSC_DEVICE *netDevice;
797 struct hv_netvsc_packet *packet;
800 NETVSC_DRIVER_OBJECT *netDriver = (NETVSC_DRIVER_OBJECT*) Device->Driver;;
802 DPRINT_ENTER(NETVSC);
804 netDevice = AllocNetDevice(Device);
811 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
813 /* Initialize the NetVSC channel extension */
814 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
815 spin_lock_init(&netDevice->receive_packet_list_lock);
817 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
819 INITIALIZE_LIST_HEAD(&netDevice->ReceivePacketList);
821 for (i=0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++)
823 packet = kzalloc(sizeof(struct hv_netvsc_packet) + (NETVSC_RECEIVE_SG_COUNT* sizeof(PAGE_BUFFER)), GFP_KERNEL);
826 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts for receive pool (wanted %d got %d)", NETVSC_RECEIVE_PACKETLIST_COUNT, i);
830 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
832 netDevice->ChannelInitEvent = WaitEventCreate();
834 /* Open the channel */
835 ret = Device->Driver->VmbusChannelInterface.Open(Device,
836 netDriver->RingBufferSize,
837 netDriver->RingBufferSize,
839 NetVscOnChannelCallback,
845 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
850 /* Channel is opened */
851 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
853 /* Connect with the NetVsp */
854 ret = NetVscConnectToVsp(Device);
857 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
862 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***", ret);
868 /* Now, we can close the channel safely */
869 Device->Driver->VmbusChannelInterface.Close(Device);
875 kfree(netDevice->ChannelInitEvent);
877 while (!IsListEmpty(&netDevice->ReceivePacketList))
879 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
880 packet = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
884 ReleaseOutboundNetDevice(Device);
885 ReleaseInboundNetDevice(Device);
887 FreeNetDevice(netDevice);
898 NetVscOnDeviceRemove()
901 Callback when the root bus device is removed
905 NetVscOnDeviceRemove(
906 struct hv_device *Device
909 struct NETVSC_DEVICE *netDevice;
910 struct hv_netvsc_packet *netvscPacket;
914 DPRINT_ENTER(NETVSC);
916 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...", Device->Extension);
918 /* Stop outbound traffic ie sends and receives completions */
919 netDevice = ReleaseOutboundNetDevice(Device);
922 DPRINT_ERR(NETVSC, "No net device present!!");
926 /* Wait for all send completions */
927 while (atomic_read(&netDevice->NumOutstandingSends))
929 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...", atomic_read(&netDevice->NumOutstandingSends));
934 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
936 NetVscDisconnectFromVsp(netDevice);
938 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...", Device->Extension);
940 /* Stop inbound traffic ie receives and sends completions */
941 netDevice = ReleaseInboundNetDevice(Device);
943 /* At this point, no one should be accessing netDevice except in here */
944 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
946 /* Now, we can close the channel safely */
947 Device->Driver->VmbusChannelInterface.Close(Device);
949 /* Release all resources */
950 while (!IsListEmpty(&netDevice->ReceivePacketList))
952 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
953 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
958 kfree(netDevice->ChannelInitEvent);
959 FreeNetDevice(netDevice);
973 Perform any cleanup when the driver is removed
978 struct hv_driver *drv
981 DPRINT_ENTER(NETVSC);
987 NetVscOnSendCompletion(
988 struct hv_device *Device,
989 VMPACKET_DESCRIPTOR *Packet
992 struct NETVSC_DEVICE *netDevice;
993 NVSP_MESSAGE *nvspPacket;
994 struct hv_netvsc_packet *nvscPacket;
996 DPRINT_ENTER(NETVSC);
998 netDevice = GetInboundNetDevice(Device);
1001 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1002 DPRINT_EXIT(NETVSC);
1006 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1008 DPRINT_DBG(NETVSC, "send completion packet - type %d", nvspPacket->Header.MessageType);
1010 if (nvspPacket->Header.MessageType == NvspMessageTypeInitComplete ||
1011 nvspPacket->Header.MessageType == NvspMessage1TypeSendReceiveBufferComplete ||
1012 nvspPacket->Header.MessageType == NvspMessage1TypeSendSendBufferComplete)
1014 /* Copy the response back */
1015 memcpy(&netDevice->ChannelInitPacket, nvspPacket, sizeof(NVSP_MESSAGE));
1016 WaitEventSet(netDevice->ChannelInitEvent);
1018 else if (nvspPacket->Header.MessageType == NvspMessage1TypeSendRNDISPacketComplete)
1020 /* Get the send context */
1021 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
1024 /* Notify the layer above us */
1025 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
1027 atomic_dec(&netDevice->NumOutstandingSends);
1031 DPRINT_ERR(NETVSC, "Unknown send completion packet type - %d received!!", nvspPacket->Header.MessageType);
1034 PutNetDevice(Device);
1035 DPRINT_EXIT(NETVSC);
1042 struct hv_device *Device,
1043 struct hv_netvsc_packet *Packet
1046 struct NETVSC_DEVICE *netDevice;
1049 NVSP_MESSAGE sendMessage;
1051 DPRINT_ENTER(NETVSC);
1053 netDevice = GetOutboundNetDevice(Device);
1056 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring outbound packets", netDevice);
1057 DPRINT_EXIT(NETVSC);
1061 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
1062 if (Packet->IsDataPacket)
1063 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;/* 0 is RMC_DATA; */
1065 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;/* 1 is RMC_CONTROL; */
1067 /* Not using send buffer section */
1068 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
1069 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
1071 if (Packet->PageBufferCount)
1073 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(Device,
1074 Packet->PageBuffers,
1075 Packet->PageBufferCount,
1077 sizeof(NVSP_MESSAGE),
1078 (unsigned long)Packet);
1082 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1084 sizeof(NVSP_MESSAGE),
1085 (unsigned long)Packet,
1086 VmbusPacketTypeDataInBand,
1087 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1093 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d", Packet, ret);
1096 atomic_inc(&netDevice->NumOutstandingSends);
1097 PutNetDevice(Device);
1099 DPRINT_EXIT(NETVSC);
1106 struct hv_device *Device,
1107 VMPACKET_DESCRIPTOR *Packet
1110 struct NETVSC_DEVICE *netDevice;
1111 VMTRANSFER_PAGE_PACKET_HEADER *vmxferpagePacket;
1112 NVSP_MESSAGE *nvspPacket;
1113 struct hv_netvsc_packet *netvscPacket=NULL;
1115 unsigned long start;
1116 unsigned long end, endVirtual;
1117 /* NETVSC_DRIVER_OBJECT *netvscDriver; */
1118 XFERPAGE_PACKET *xferpagePacket=NULL;
1119 LIST_ENTRY listHead;
1122 int count=0, bytesRemain=0;
1123 unsigned long flags;
1125 DPRINT_ENTER(NETVSC);
1127 netDevice = GetInboundNetDevice(Device);
1130 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1131 DPRINT_EXIT(NETVSC);
1135 /* All inbound packets other than send completion should be xfer page packet */
1136 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages)
1138 DPRINT_ERR(NETVSC, "Unknown packet type received - %d", Packet->Type);
1139 PutNetDevice(Device);
1143 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1145 /* Make sure this is a valid nvsp packet */
1146 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket )
1148 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d", nvspPacket->Header.MessageType);
1149 PutNetDevice(Device);
1153 DPRINT_DBG(NETVSC, "NVSP packet received - type %d", nvspPacket->Header.MessageType);
1155 vmxferpagePacket = (VMTRANSFER_PAGE_PACKET_HEADER*)Packet;
1157 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID)
1159 DPRINT_ERR(NETVSC, "Invalid xfer page set id - expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, vmxferpagePacket->TransferPageSetId);
1160 PutNetDevice(Device);
1164 DPRINT_DBG(NETVSC, "xfer page - range count %d", vmxferpagePacket->RangeCount);
1166 INITIALIZE_LIST_HEAD(&listHead);
1169 * Grab free packets (range count + 1) to represent this xfer
1170 * page packet. +1 to represent the xfer page packet itself.
1171 * We grab it here so that we know exactly how many we can
1174 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1175 while (!IsListEmpty(&netDevice->ReceivePacketList))
1177 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
1178 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
1180 INSERT_TAIL_LIST(&listHead, &netvscPacket->ListEntry);
1182 if (++count == vmxferpagePacket->RangeCount + 1)
1185 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1188 * We need at least 2 netvsc pkts (1 to represent the xfer
1189 * page and at least 1 for the range) i.e. we can handled
1190 * some of the xfer page packet ranges...
1194 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. Dropping this xfer page packet completely!", count, vmxferpagePacket->RangeCount+1);
1196 /* Return it to the freelist */
1197 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1198 for (i=count; i != 0; i--)
1200 entry = REMOVE_HEAD_LIST(&listHead);
1201 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
1203 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &netvscPacket->ListEntry);
1205 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1207 NetVscSendReceiveCompletion(Device, vmxferpagePacket->d.TransactionId);
1209 PutNetDevice(Device);
1213 /* Remove the 1st packet to represent the xfer page packet itself */
1214 entry = REMOVE_HEAD_LIST(&listHead);
1215 xferpagePacket = CONTAINING_RECORD(entry, XFERPAGE_PACKET, ListEntry);
1216 xferpagePacket->Count = count - 1; /* This is how much we can satisfy */
1217 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= vmxferpagePacket->RangeCount);
1219 if (xferpagePacket->Count != vmxferpagePacket->RangeCount)
1221 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer page...got %d", vmxferpagePacket->RangeCount, xferpagePacket->Count);
1224 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1225 for (i=0; i < (count - 1); i++)
1227 entry = REMOVE_HEAD_LIST(&listHead);
1228 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
1230 /* Initialize the netvsc packet */
1231 netvscPacket->XferPagePacket = xferpagePacket;
1232 netvscPacket->Completion.Recv.OnReceiveCompletion = NetVscOnReceiveCompletion;
1233 netvscPacket->Completion.Recv.ReceiveCompletionContext = netvscPacket;
1234 netvscPacket->Device = Device;
1235 netvscPacket->Completion.Recv.ReceiveCompletionTid = vmxferpagePacket->d.TransactionId; /* Save this so that we can send it back */
1237 netvscPacket->TotalDataBufferLength = vmxferpagePacket->Ranges[i].ByteCount;
1238 netvscPacket->PageBufferCount = 1;
1240 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + vmxferpagePacket->Ranges[i].ByteCount < netDevice->ReceiveBufferSize);
1242 netvscPacket->PageBuffers[0].Length = vmxferpagePacket->Ranges[i].ByteCount;
1244 start = GetPhysicalAddress((void*)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1246 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1247 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1248 + vmxferpagePacket->Ranges[i].ByteOffset
1249 + vmxferpagePacket->Ranges[i].ByteCount -1;
1250 end = GetPhysicalAddress((void*)endVirtual);
1252 /* Calculate the page relative offset */
1253 netvscPacket->PageBuffers[0].Offset = vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE -1);
1254 if ((end >> PAGE_SHIFT) != (start>>PAGE_SHIFT)) {
1255 /* Handle frame across multiple pages: */
1256 netvscPacket->PageBuffers[0].Length =
1257 (netvscPacket->PageBuffers[0].Pfn <<PAGE_SHIFT) + PAGE_SIZE - start;
1258 bytesRemain = netvscPacket->TotalDataBufferLength - netvscPacket->PageBuffers[0].Length;
1259 for (j=1; j<NETVSC_PACKET_MAXPAGE; j++) {
1260 netvscPacket->PageBuffers[j].Offset = 0;
1261 if (bytesRemain <= PAGE_SIZE) {
1262 netvscPacket->PageBuffers[j].Length = bytesRemain;
1265 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1266 bytesRemain -= PAGE_SIZE;
1268 netvscPacket->PageBuffers[j].Pfn =
1269 GetPhysicalAddress((void*)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1270 netvscPacket->PageBufferCount++;
1271 if (bytesRemain == 0)
1274 ASSERT(bytesRemain == 0);
1276 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => (pfn %llx, offset %u, len %u)",
1278 vmxferpagePacket->Ranges[i].ByteOffset,
1279 vmxferpagePacket->Ranges[i].ByteCount,
1280 netvscPacket->PageBuffers[0].Pfn,
1281 netvscPacket->PageBuffers[0].Offset,
1282 netvscPacket->PageBuffers[0].Length);
1284 /* Pass it to the upper layer */
1285 ((NETVSC_DRIVER_OBJECT*)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1287 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1290 ASSERT(IsListEmpty(&listHead));
1292 PutNetDevice(Device);
1293 DPRINT_EXIT(NETVSC);
1298 NetVscSendReceiveCompletion(
1299 struct hv_device *Device,
1303 NVSP_MESSAGE recvcompMessage;
1307 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx", TransactionId);
1309 recvcompMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacketComplete;
1311 /* FIXME: Pass in the status */
1312 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1315 /* Send the completion */
1316 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1318 sizeof(NVSP_MESSAGE),
1320 VmbusPacketTypeCompletion,
1322 if (ret == 0) /* success */
1326 else if (ret == -1) /* no more room...wait a bit and attempt to retry 3 times */
1329 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...retrying %d", TransactionId, retries);
1334 goto retry_send_cmplt;
1338 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...give up retrying", TransactionId);
1343 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - %llx", TransactionId);
1347 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1349 NetVscOnReceiveCompletion(
1352 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet*)Context;
1353 struct hv_device *device = (struct hv_device*)packet->Device;
1354 struct NETVSC_DEVICE *netDevice;
1355 u64 transactionId=0;
1356 bool fSendReceiveComp = false;
1357 unsigned long flags;
1359 DPRINT_ENTER(NETVSC);
1361 ASSERT(packet->XferPagePacket);
1363 /* Even though it seems logical to do a GetOutboundNetDevice() here to send out receive completion, */
1364 /* we are using GetInboundNetDevice() since we may have disable outbound traffic already. */
1365 netDevice = GetInboundNetDevice(device);
1368 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1369 DPRINT_EXIT(NETVSC);
1373 /* Overloading use of the lock. */
1374 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1376 ASSERT(packet->XferPagePacket->Count > 0);
1377 packet->XferPagePacket->Count--;
1379 /* Last one in the line that represent 1 xfer page packet. */
1380 /* Return the xfer page packet itself to the freelist */
1381 if (packet->XferPagePacket->Count == 0)
1383 fSendReceiveComp = true;
1384 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1386 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->XferPagePacket->ListEntry);
1389 /* Put the packet back */
1390 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
1391 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1393 /* Send a receive completion for the xfer page packet */
1394 if (fSendReceiveComp)
1396 NetVscSendReceiveCompletion(device, transactionId);
1399 PutNetDevice(device);
1400 DPRINT_EXIT(NETVSC);
1406 NetVscOnChannelCallback(
1410 const int netPacketSize=2048;
1412 struct hv_device *device=(struct hv_device*)Context;
1413 struct NETVSC_DEVICE *netDevice;
1417 unsigned char packet[netPacketSize];
1418 VMPACKET_DESCRIPTOR *desc;
1419 unsigned char *buffer=packet;
1420 int bufferlen=netPacketSize;
1423 DPRINT_ENTER(NETVSC);
1427 netDevice = GetInboundNetDevice(device);
1430 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring inbound packets", netDevice);
1431 DPRINT_EXIT(NETVSC);
1437 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(device,
1447 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx", bytesRecvd, requestId);
1449 desc = (VMPACKET_DESCRIPTOR*)buffer;
1452 case VmbusPacketTypeCompletion:
1453 NetVscOnSendCompletion(device, desc);
1456 case VmbusPacketTypeDataUsingTransferPages:
1457 NetVscOnReceive(device, desc);
1461 DPRINT_ERR(NETVSC, "unhandled packet type %d, tid %llx len %d\n", desc->Type, requestId, bytesRecvd);
1466 if (bufferlen > netPacketSize)
1471 bufferlen = netPacketSize;
1476 /* DPRINT_DBG(NETVSC, "nothing else to read..."); */
1479 if (bufferlen > netPacketSize)
1484 bufferlen = netPacketSize;
1490 else if (ret == -2) /* Handle large packet */
1492 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
1495 /* Try again next time around */
1496 DPRINT_ERR(NETVSC, "unable to allocate buffer of size (%d)!!", bytesRecvd);
1500 bufferlen = bytesRecvd;
1508 PutNetDevice(device);
1509 DPRINT_EXIT(NETVSC);