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>
28 #include "include/logging.h"
30 #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}
42 /* Internal routines */
45 struct hv_device *Device,
51 struct hv_device *Device
56 struct hv_driver *Driver
60 NetVscOnChannelCallback(
65 NetVscInitializeSendBufferWithNetVsp(
66 struct hv_device *Device
70 NetVscInitializeReceiveBufferWithNetVsp(
71 struct hv_device *Device
75 NetVscDestroySendBuffer(
76 struct NETVSC_DEVICE *NetDevice
80 NetVscDestroyReceiveBuffer(
81 struct NETVSC_DEVICE *NetDevice
86 struct hv_device *Device
90 NetVscOnSendCompletion(
91 struct hv_device *Device,
92 VMPACKET_DESCRIPTOR *Packet
97 struct hv_device *Device,
98 struct hv_netvsc_packet *Packet
103 struct hv_device *Device,
104 VMPACKET_DESCRIPTOR *Packet
108 NetVscOnReceiveCompletion(
113 NetVscSendReceiveCompletion(
114 struct hv_device *Device,
118 static inline struct NETVSC_DEVICE *AllocNetDevice(struct hv_device *Device)
120 struct NETVSC_DEVICE *netDevice;
122 netDevice = kzalloc(sizeof(struct NETVSC_DEVICE), GFP_KERNEL);
126 /* Set to 2 to allow both inbound and outbound traffic */
127 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
129 netDevice->Device = Device;
130 Device->Extension = netDevice;
135 static inline void FreeNetDevice(struct NETVSC_DEVICE *Device)
137 ASSERT(atomic_read(&Device->RefCount) == 0);
138 Device->Device->Extension = NULL;
143 /* Get the net device object iff exists and its refcount > 1 */
144 static inline struct NETVSC_DEVICE *GetOutboundNetDevice(struct hv_device *Device)
146 struct NETVSC_DEVICE *netDevice;
148 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
149 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
150 atomic_inc(&netDevice->RefCount);
157 /* Get the net device object iff exists and its refcount > 0 */
158 static inline struct NETVSC_DEVICE *GetInboundNetDevice(struct hv_device *Device)
160 struct NETVSC_DEVICE *netDevice;
162 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
163 if (netDevice && atomic_read(&netDevice->RefCount))
164 atomic_inc(&netDevice->RefCount);
171 static inline void PutNetDevice(struct hv_device *Device)
173 struct NETVSC_DEVICE *netDevice;
175 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
178 atomic_dec(&netDevice->RefCount);
181 static inline struct NETVSC_DEVICE *ReleaseOutboundNetDevice(struct hv_device *Device)
183 struct NETVSC_DEVICE *netDevice;
185 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
186 if (netDevice == NULL)
189 /* Busy wait until the ref drop to 2, then set it to 1 */
190 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
198 static inline struct NETVSC_DEVICE *ReleaseInboundNetDevice(struct hv_device *Device)
200 struct NETVSC_DEVICE *netDevice;
202 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
203 if (netDevice == NULL)
206 /* Busy wait until the ref drop to 1, then set it to 0 */
207 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
212 Device->Extension = NULL;
228 struct hv_driver *drv
231 NETVSC_DRIVER_OBJECT* driver = (NETVSC_DRIVER_OBJECT*)drv;
234 DPRINT_ENTER(NETVSC);
236 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, sizeof(NVSP_MESSAGE)=%zd, sizeof(VMTRANSFER_PAGE_PACKET_HEADER)=%zd",
237 sizeof(struct hv_netvsc_packet), sizeof(NVSP_MESSAGE), sizeof(VMTRANSFER_PAGE_PACKET_HEADER));
239 /* Make sure we are at least 2 pages since 1 page is used for control */
240 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
242 drv->name = gDriverName;
243 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(GUID));
245 /* Make sure it is set by the caller */
246 ASSERT(driver->OnReceiveCallback);
247 ASSERT(driver->OnLinkStatusChanged);
249 /* Setup the dispatch table */
250 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
251 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
252 driver->Base.OnCleanup = NetVscOnCleanup;
254 driver->OnSend = NetVscOnSend;
256 RndisFilterInit(driver);
264 NetVscInitializeReceiveBufferWithNetVsp(
265 struct hv_device *Device
269 struct NETVSC_DEVICE *netDevice;
270 NVSP_MESSAGE *initPacket;
272 DPRINT_ENTER(NETVSC);
274 netDevice = GetOutboundNetDevice(Device);
277 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
281 ASSERT(netDevice->ReceiveBufferSize > 0);
282 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE-1)) == 0); /* page-size grandularity */
284 netDevice->ReceiveBuffer = osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
285 if (!netDevice->ReceiveBuffer)
287 DPRINT_ERR(NETVSC, "unable to allocate receive buffer of size %d", netDevice->ReceiveBufferSize);
291 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE-1)) == 0); /* page-aligned buffer */
293 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
296 * Establish the gpadl handle for this buffer on this
297 * channel. Note: This call uses the vmbus connection rather
298 * than the channel to establish the gpadl handle.
300 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
301 netDevice->ReceiveBuffer,
302 netDevice->ReceiveBufferSize,
303 &netDevice->ReceiveBufferGpadlHandle);
307 DPRINT_ERR(NETVSC, "unable to establish receive buffer's gpadl");
311 /* osd_WaitEventWait(ext->ChannelInitEvent); */
313 /* Notify the NetVsp of the gpadl handle */
314 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
316 initPacket = &netDevice->ChannelInitPacket;
318 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
320 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
321 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
322 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
324 /* Send the gpadl notification request */
325 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
327 sizeof(NVSP_MESSAGE),
328 (unsigned long)initPacket,
329 VmbusPacketTypeDataInBand,
330 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
333 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
337 osd_WaitEventWait(netDevice->ChannelInitEvent);
339 /* Check the response */
340 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess)
343 "Unable to complete receive buffer initialzation with NetVsp - status %d",
344 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
349 /* Parse the response */
350 ASSERT(netDevice->ReceiveSectionCount == 0);
351 ASSERT(netDevice->ReceiveSections == NULL);
353 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
355 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION), GFP_KERNEL);
356 if (netDevice->ReceiveSections == NULL)
362 memcpy(netDevice->ReceiveSections,
363 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
364 netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION));
367 "Receive sections info (count %d, offset %d, endoffset %d, suballoc size %d, num suballocs %d)",
368 netDevice->ReceiveSectionCount, netDevice->ReceiveSections[0].Offset, netDevice->ReceiveSections[0].EndOffset,
369 netDevice->ReceiveSections[0].SubAllocationSize, netDevice->ReceiveSections[0].NumSubAllocations);
372 /* For 1st release, there should only be 1 section that represents the entire receive buffer */
373 if (netDevice->ReceiveSectionCount != 1 ||
374 netDevice->ReceiveSections->Offset != 0 )
383 NetVscDestroyReceiveBuffer(netDevice);
386 PutNetDevice(Device);
393 NetVscInitializeSendBufferWithNetVsp(
394 struct hv_device *Device
398 struct NETVSC_DEVICE *netDevice;
399 NVSP_MESSAGE *initPacket;
401 DPRINT_ENTER(NETVSC);
403 netDevice = GetOutboundNetDevice(Device);
406 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
410 ASSERT(netDevice->SendBufferSize > 0);
411 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE-1)) == 0); /* page-size grandularity */
413 netDevice->SendBuffer = osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
414 if (!netDevice->SendBuffer)
416 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d", netDevice->SendBufferSize);
420 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE-1)) == 0); /* page-aligned buffer */
422 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
425 * Establish the gpadl handle for this buffer on this
426 * channel. Note: This call uses the vmbus connection rather
427 * than the channel to establish the gpadl handle.
429 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
430 netDevice->SendBuffer,
431 netDevice->SendBufferSize,
432 &netDevice->SendBufferGpadlHandle);
436 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
440 /* osd_WaitEventWait(ext->ChannelInitEvent); */
442 /* Notify the NetVsp of the gpadl handle */
443 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
445 initPacket = &netDevice->ChannelInitPacket;
447 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
449 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
450 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
451 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
453 /* Send the gpadl notification request */
454 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
456 sizeof(NVSP_MESSAGE),
457 (unsigned long)initPacket,
458 VmbusPacketTypeDataInBand,
459 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
462 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
466 osd_WaitEventWait(netDevice->ChannelInitEvent);
468 /* Check the response */
469 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess)
472 "Unable to complete send buffer initialzation with NetVsp - status %d",
473 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
478 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
483 NetVscDestroySendBuffer(netDevice);
486 PutNetDevice(Device);
492 NetVscDestroyReceiveBuffer(
493 struct NETVSC_DEVICE *NetDevice
496 NVSP_MESSAGE *revokePacket;
500 DPRINT_ENTER(NETVSC);
503 * If we got a section count, it means we received a
504 * SendReceiveBufferComplete msg (ie sent
505 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
506 * to send a revoke msg here
508 if (NetDevice->ReceiveSectionCount)
510 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeReceiveBuffer...");
512 /* Send the revoke receive buffer */
513 revokePacket = &NetDevice->RevokePacket;
514 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
516 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
517 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
519 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
521 sizeof(NVSP_MESSAGE),
522 (unsigned long)revokePacket,
523 VmbusPacketTypeDataInBand,
526 * If we failed here, we might as well return and
527 * 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 osd_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 struct NETVSC_DEVICE *NetDevice
584 NVSP_MESSAGE *revokePacket;
588 DPRINT_ENTER(NETVSC);
591 * If we got a section count, it means we received a
592 * SendReceiveBufferComplete msg (ie sent
593 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
594 * to send a revoke msg here
596 if (NetDevice->SendSectionSize)
598 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeSendBuffer...");
600 /* Send the revoke send buffer */
601 revokePacket = &NetDevice->RevokePacket;
602 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
604 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
605 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
607 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
609 sizeof(NVSP_MESSAGE),
610 (unsigned long)revokePacket,
611 VmbusPacketTypeDataInBand,
613 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
616 DPRINT_ERR(NETVSC, "unable to send revoke send buffer to netvsp");
622 /* Teardown the gpadl on the vsp end */
623 if (NetDevice->SendBufferGpadlHandle)
625 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
627 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
628 NetDevice->SendBufferGpadlHandle);
630 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
633 DPRINT_ERR(NETVSC, "unable to teardown send buffer's gpadl");
637 NetDevice->SendBufferGpadlHandle = 0;
640 if (NetDevice->SendBuffer)
642 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
644 /* Free up the receive buffer */
645 osd_PageFree(NetDevice->SendBuffer, NetDevice->SendBufferSize >> PAGE_SHIFT);
646 NetDevice->SendBuffer = NULL;
658 struct hv_device *Device
662 struct NETVSC_DEVICE *netDevice;
663 NVSP_MESSAGE *initPacket;
666 DPRINT_ENTER(NETVSC);
668 netDevice = GetOutboundNetDevice(Device);
671 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
676 initPacket = &netDevice->ChannelInitPacket;
678 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
679 initPacket->Header.MessageType = NvspMessageTypeInit;
680 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
681 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
683 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
685 /* Send the init request */
686 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
688 sizeof(NVSP_MESSAGE),
689 (unsigned long)initPacket,
690 VmbusPacketTypeDataInBand,
691 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
695 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
699 osd_WaitEventWait(netDevice->ChannelInitEvent);
701 /* Now, check the response */
702 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
703 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
704 initPacket->Messages.InitMessages.InitComplete.Status,
705 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
707 if (initPacket->Messages.InitMessages.InitComplete.Status != NvspStatusSuccess)
709 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (status 0x%x)", initPacket->Messages.InitMessages.InitComplete.Status);
714 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1)
716 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (version expected 1 got %d)",
717 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
721 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
723 /* Send the ndis version */
724 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
726 ndisVersion = 0x00050000;
728 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
729 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion = (ndisVersion & 0xFFFF0000) >> 16;
730 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion = ndisVersion & 0xFFFF;
732 /* Send the init request */
733 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
735 sizeof(NVSP_MESSAGE),
736 (unsigned long)initPacket,
737 VmbusPacketTypeDataInBand,
741 DPRINT_ERR(NETVSC, "unable to send NvspMessage1TypeSendNdisVersion");
746 * BUGBUG - We have to wait for the above msg since the
747 * netvsp uses KMCL which acknowledges packet (completion
748 * packet) since our Vmbus always set the
749 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
751 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
753 /* Post the big receive buffer to NetVSP */
754 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
757 ret = NetVscInitializeSendBufferWithNetVsp(Device);
761 PutNetDevice(Device);
767 NetVscDisconnectFromVsp(
768 struct NETVSC_DEVICE *NetDevice
771 DPRINT_ENTER(NETVSC);
773 NetVscDestroyReceiveBuffer(NetDevice);
774 NetVscDestroySendBuffer(NetDevice);
786 Callback when the device belonging to this driver is added
791 struct hv_device *Device,
798 struct NETVSC_DEVICE *netDevice;
799 struct hv_netvsc_packet *packet;
802 NETVSC_DRIVER_OBJECT *netDriver = (NETVSC_DRIVER_OBJECT*) Device->Driver;;
804 DPRINT_ENTER(NETVSC);
806 netDevice = AllocNetDevice(Device);
813 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
815 /* Initialize the NetVSC channel extension */
816 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
817 spin_lock_init(&netDevice->receive_packet_list_lock);
819 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
821 INITIALIZE_LIST_HEAD(&netDevice->ReceivePacketList);
823 for (i=0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++)
825 packet = kzalloc(sizeof(struct hv_netvsc_packet) + (NETVSC_RECEIVE_SG_COUNT* sizeof(PAGE_BUFFER)), GFP_KERNEL);
828 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts for receive pool (wanted %d got %d)", NETVSC_RECEIVE_PACKETLIST_COUNT, i);
832 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
834 netDevice->ChannelInitEvent = osd_WaitEventCreate();
836 /* Open the channel */
837 ret = Device->Driver->VmbusChannelInterface.Open(Device,
838 netDriver->RingBufferSize,
839 netDriver->RingBufferSize,
841 NetVscOnChannelCallback,
847 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
852 /* Channel is opened */
853 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
855 /* Connect with the NetVsp */
856 ret = NetVscConnectToVsp(Device);
859 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
864 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***", ret);
870 /* Now, we can close the channel safely */
871 Device->Driver->VmbusChannelInterface.Close(Device);
877 kfree(netDevice->ChannelInitEvent);
879 while (!IsListEmpty(&netDevice->ReceivePacketList))
881 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
882 packet = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
886 ReleaseOutboundNetDevice(Device);
887 ReleaseInboundNetDevice(Device);
889 FreeNetDevice(netDevice);
900 NetVscOnDeviceRemove()
903 Callback when the root bus device is removed
907 NetVscOnDeviceRemove(
908 struct hv_device *Device
911 struct NETVSC_DEVICE *netDevice;
912 struct hv_netvsc_packet *netvscPacket;
916 DPRINT_ENTER(NETVSC);
918 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...", Device->Extension);
920 /* Stop outbound traffic ie sends and receives completions */
921 netDevice = ReleaseOutboundNetDevice(Device);
924 DPRINT_ERR(NETVSC, "No net device present!!");
928 /* Wait for all send completions */
929 while (atomic_read(&netDevice->NumOutstandingSends))
931 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...", atomic_read(&netDevice->NumOutstandingSends));
936 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
938 NetVscDisconnectFromVsp(netDevice);
940 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...", Device->Extension);
942 /* Stop inbound traffic ie receives and sends completions */
943 netDevice = ReleaseInboundNetDevice(Device);
945 /* At this point, no one should be accessing netDevice except in here */
946 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
948 /* Now, we can close the channel safely */
949 Device->Driver->VmbusChannelInterface.Close(Device);
951 /* Release all resources */
952 while (!IsListEmpty(&netDevice->ReceivePacketList))
954 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
955 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
960 kfree(netDevice->ChannelInitEvent);
961 FreeNetDevice(netDevice);
975 Perform any cleanup when the driver is removed
980 struct hv_driver *drv
983 DPRINT_ENTER(NETVSC);
989 NetVscOnSendCompletion(
990 struct hv_device *Device,
991 VMPACKET_DESCRIPTOR *Packet
994 struct NETVSC_DEVICE *netDevice;
995 NVSP_MESSAGE *nvspPacket;
996 struct hv_netvsc_packet *nvscPacket;
998 DPRINT_ENTER(NETVSC);
1000 netDevice = GetInboundNetDevice(Device);
1003 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1004 DPRINT_EXIT(NETVSC);
1008 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1010 DPRINT_DBG(NETVSC, "send completion packet - type %d", nvspPacket->Header.MessageType);
1012 if (nvspPacket->Header.MessageType == NvspMessageTypeInitComplete ||
1013 nvspPacket->Header.MessageType == NvspMessage1TypeSendReceiveBufferComplete ||
1014 nvspPacket->Header.MessageType == NvspMessage1TypeSendSendBufferComplete)
1016 /* Copy the response back */
1017 memcpy(&netDevice->ChannelInitPacket, nvspPacket, sizeof(NVSP_MESSAGE));
1018 osd_WaitEventSet(netDevice->ChannelInitEvent);
1020 else if (nvspPacket->Header.MessageType == NvspMessage1TypeSendRNDISPacketComplete)
1022 /* Get the send context */
1023 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
1026 /* Notify the layer above us */
1027 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
1029 atomic_dec(&netDevice->NumOutstandingSends);
1033 DPRINT_ERR(NETVSC, "Unknown send completion packet type - %d received!!", nvspPacket->Header.MessageType);
1036 PutNetDevice(Device);
1037 DPRINT_EXIT(NETVSC);
1044 struct hv_device *Device,
1045 struct hv_netvsc_packet *Packet
1048 struct NETVSC_DEVICE *netDevice;
1051 NVSP_MESSAGE sendMessage;
1053 DPRINT_ENTER(NETVSC);
1055 netDevice = GetOutboundNetDevice(Device);
1058 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring outbound packets", netDevice);
1059 DPRINT_EXIT(NETVSC);
1063 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
1064 if (Packet->IsDataPacket)
1065 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;/* 0 is RMC_DATA; */
1067 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;/* 1 is RMC_CONTROL; */
1069 /* Not using send buffer section */
1070 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
1071 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
1073 if (Packet->PageBufferCount)
1075 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(Device,
1076 Packet->PageBuffers,
1077 Packet->PageBufferCount,
1079 sizeof(NVSP_MESSAGE),
1080 (unsigned long)Packet);
1084 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1086 sizeof(NVSP_MESSAGE),
1087 (unsigned long)Packet,
1088 VmbusPacketTypeDataInBand,
1089 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1095 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d", Packet, ret);
1098 atomic_inc(&netDevice->NumOutstandingSends);
1099 PutNetDevice(Device);
1101 DPRINT_EXIT(NETVSC);
1108 struct hv_device *Device,
1109 VMPACKET_DESCRIPTOR *Packet
1112 struct NETVSC_DEVICE *netDevice;
1113 VMTRANSFER_PAGE_PACKET_HEADER *vmxferpagePacket;
1114 NVSP_MESSAGE *nvspPacket;
1115 struct hv_netvsc_packet *netvscPacket=NULL;
1117 unsigned long start;
1118 unsigned long end, endVirtual;
1119 /* NETVSC_DRIVER_OBJECT *netvscDriver; */
1120 XFERPAGE_PACKET *xferpagePacket=NULL;
1121 LIST_ENTRY listHead;
1124 int count=0, bytesRemain=0;
1125 unsigned long flags;
1127 DPRINT_ENTER(NETVSC);
1129 netDevice = GetInboundNetDevice(Device);
1132 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1133 DPRINT_EXIT(NETVSC);
1137 /* All inbound packets other than send completion should be xfer page packet */
1138 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages)
1140 DPRINT_ERR(NETVSC, "Unknown packet type received - %d", Packet->Type);
1141 PutNetDevice(Device);
1145 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
1147 /* Make sure this is a valid nvsp packet */
1148 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket )
1150 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d", nvspPacket->Header.MessageType);
1151 PutNetDevice(Device);
1155 DPRINT_DBG(NETVSC, "NVSP packet received - type %d", nvspPacket->Header.MessageType);
1157 vmxferpagePacket = (VMTRANSFER_PAGE_PACKET_HEADER*)Packet;
1159 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID)
1161 DPRINT_ERR(NETVSC, "Invalid xfer page set id - expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, vmxferpagePacket->TransferPageSetId);
1162 PutNetDevice(Device);
1166 DPRINT_DBG(NETVSC, "xfer page - range count %d", vmxferpagePacket->RangeCount);
1168 INITIALIZE_LIST_HEAD(&listHead);
1171 * Grab free packets (range count + 1) to represent this xfer
1172 * page packet. +1 to represent the xfer page packet itself.
1173 * We grab it here so that we know exactly how many we can
1176 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1177 while (!IsListEmpty(&netDevice->ReceivePacketList))
1179 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
1180 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
1182 INSERT_TAIL_LIST(&listHead, &netvscPacket->ListEntry);
1184 if (++count == vmxferpagePacket->RangeCount + 1)
1187 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1190 * We need at least 2 netvsc pkts (1 to represent the xfer
1191 * page and at least 1 for the range) i.e. we can handled
1192 * some of the xfer page packet ranges...
1196 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. Dropping this xfer page packet completely!", count, vmxferpagePacket->RangeCount+1);
1198 /* Return it to the freelist */
1199 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1200 for (i=count; i != 0; i--)
1202 entry = REMOVE_HEAD_LIST(&listHead);
1203 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
1205 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &netvscPacket->ListEntry);
1207 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1209 NetVscSendReceiveCompletion(Device, vmxferpagePacket->d.TransactionId);
1211 PutNetDevice(Device);
1215 /* Remove the 1st packet to represent the xfer page packet itself */
1216 entry = REMOVE_HEAD_LIST(&listHead);
1217 xferpagePacket = CONTAINING_RECORD(entry, XFERPAGE_PACKET, ListEntry);
1218 xferpagePacket->Count = count - 1; /* This is how much we can satisfy */
1219 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= vmxferpagePacket->RangeCount);
1221 if (xferpagePacket->Count != vmxferpagePacket->RangeCount)
1223 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer page...got %d", vmxferpagePacket->RangeCount, xferpagePacket->Count);
1226 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1227 for (i=0; i < (count - 1); i++)
1229 entry = REMOVE_HEAD_LIST(&listHead);
1230 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
1232 /* Initialize the netvsc packet */
1233 netvscPacket->XferPagePacket = xferpagePacket;
1234 netvscPacket->Completion.Recv.OnReceiveCompletion = NetVscOnReceiveCompletion;
1235 netvscPacket->Completion.Recv.ReceiveCompletionContext = netvscPacket;
1236 netvscPacket->Device = Device;
1237 netvscPacket->Completion.Recv.ReceiveCompletionTid = vmxferpagePacket->d.TransactionId; /* Save this so that we can send it back */
1239 netvscPacket->TotalDataBufferLength = vmxferpagePacket->Ranges[i].ByteCount;
1240 netvscPacket->PageBufferCount = 1;
1242 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + vmxferpagePacket->Ranges[i].ByteCount < netDevice->ReceiveBufferSize);
1244 netvscPacket->PageBuffers[0].Length = vmxferpagePacket->Ranges[i].ByteCount;
1246 start = virt_to_phys((void*)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1248 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1249 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1250 + vmxferpagePacket->Ranges[i].ByteOffset
1251 + vmxferpagePacket->Ranges[i].ByteCount -1;
1252 end = virt_to_phys((void*)endVirtual);
1254 /* Calculate the page relative offset */
1255 netvscPacket->PageBuffers[0].Offset = vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE -1);
1256 if ((end >> PAGE_SHIFT) != (start>>PAGE_SHIFT)) {
1257 /* Handle frame across multiple pages: */
1258 netvscPacket->PageBuffers[0].Length =
1259 (netvscPacket->PageBuffers[0].Pfn <<PAGE_SHIFT) + PAGE_SIZE - start;
1260 bytesRemain = netvscPacket->TotalDataBufferLength - netvscPacket->PageBuffers[0].Length;
1261 for (j=1; j<NETVSC_PACKET_MAXPAGE; j++) {
1262 netvscPacket->PageBuffers[j].Offset = 0;
1263 if (bytesRemain <= PAGE_SIZE) {
1264 netvscPacket->PageBuffers[j].Length = bytesRemain;
1267 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1268 bytesRemain -= PAGE_SIZE;
1270 netvscPacket->PageBuffers[j].Pfn =
1271 virt_to_phys((void*)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1272 netvscPacket->PageBufferCount++;
1273 if (bytesRemain == 0)
1276 ASSERT(bytesRemain == 0);
1278 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => (pfn %llx, offset %u, len %u)",
1280 vmxferpagePacket->Ranges[i].ByteOffset,
1281 vmxferpagePacket->Ranges[i].ByteCount,
1282 netvscPacket->PageBuffers[0].Pfn,
1283 netvscPacket->PageBuffers[0].Offset,
1284 netvscPacket->PageBuffers[0].Length);
1286 /* Pass it to the upper layer */
1287 ((NETVSC_DRIVER_OBJECT*)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1289 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1292 ASSERT(IsListEmpty(&listHead));
1294 PutNetDevice(Device);
1295 DPRINT_EXIT(NETVSC);
1300 NetVscSendReceiveCompletion(
1301 struct hv_device *Device,
1305 NVSP_MESSAGE recvcompMessage;
1309 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx", TransactionId);
1311 recvcompMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacketComplete;
1313 /* FIXME: Pass in the status */
1314 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1317 /* Send the completion */
1318 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1320 sizeof(NVSP_MESSAGE),
1322 VmbusPacketTypeCompletion,
1324 if (ret == 0) /* success */
1328 else if (ret == -1) /* no more room...wait a bit and attempt to retry 3 times */
1331 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...retrying %d", TransactionId, retries);
1336 goto retry_send_cmplt;
1340 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...give up retrying", TransactionId);
1345 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - %llx", TransactionId);
1349 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1351 NetVscOnReceiveCompletion(
1354 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet*)Context;
1355 struct hv_device *device = (struct hv_device*)packet->Device;
1356 struct NETVSC_DEVICE *netDevice;
1357 u64 transactionId=0;
1358 bool fSendReceiveComp = false;
1359 unsigned long flags;
1361 DPRINT_ENTER(NETVSC);
1363 ASSERT(packet->XferPagePacket);
1365 /* Even though it seems logical to do a GetOutboundNetDevice() here to send out receive completion, */
1366 /* we are using GetInboundNetDevice() since we may have disable outbound traffic already. */
1367 netDevice = GetInboundNetDevice(device);
1370 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1371 DPRINT_EXIT(NETVSC);
1375 /* Overloading use of the lock. */
1376 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1378 ASSERT(packet->XferPagePacket->Count > 0);
1379 packet->XferPagePacket->Count--;
1381 /* Last one in the line that represent 1 xfer page packet. */
1382 /* Return the xfer page packet itself to the freelist */
1383 if (packet->XferPagePacket->Count == 0)
1385 fSendReceiveComp = true;
1386 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1388 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->XferPagePacket->ListEntry);
1391 /* Put the packet back */
1392 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
1393 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1395 /* Send a receive completion for the xfer page packet */
1396 if (fSendReceiveComp)
1398 NetVscSendReceiveCompletion(device, transactionId);
1401 PutNetDevice(device);
1402 DPRINT_EXIT(NETVSC);
1408 NetVscOnChannelCallback(
1412 const int netPacketSize=2048;
1414 struct hv_device *device=(struct hv_device*)Context;
1415 struct NETVSC_DEVICE *netDevice;
1419 unsigned char packet[netPacketSize];
1420 VMPACKET_DESCRIPTOR *desc;
1421 unsigned char *buffer=packet;
1422 int bufferlen=netPacketSize;
1425 DPRINT_ENTER(NETVSC);
1429 netDevice = GetInboundNetDevice(device);
1432 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring inbound packets", netDevice);
1433 DPRINT_EXIT(NETVSC);
1439 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(device,
1449 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx", bytesRecvd, requestId);
1451 desc = (VMPACKET_DESCRIPTOR*)buffer;
1454 case VmbusPacketTypeCompletion:
1455 NetVscOnSendCompletion(device, desc);
1458 case VmbusPacketTypeDataUsingTransferPages:
1459 NetVscOnReceive(device, desc);
1463 DPRINT_ERR(NETVSC, "unhandled packet type %d, tid %llx len %d\n", desc->Type, requestId, bytesRecvd);
1468 if (bufferlen > netPacketSize)
1473 bufferlen = netPacketSize;
1478 /* DPRINT_DBG(NETVSC, "nothing else to read..."); */
1481 if (bufferlen > netPacketSize)
1486 bufferlen = netPacketSize;
1492 else if (ret == -2) /* Handle large packet */
1494 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
1497 /* Try again next time around */
1498 DPRINT_ERR(NETVSC, "unable to allocate buffer of size (%d)!!", bytesRecvd);
1502 bufferlen = bytesRecvd;
1510 PutNetDevice(device);
1511 DPRINT_EXIT(NETVSC);