2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
23 #include <linux/delay.h>
25 #include <linux/slab.h>
29 #include "RndisFilter.h"
33 static const char *gDriverName = "netvsc";
35 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
36 static const struct hv_guid gNetVscDeviceType = {
38 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
39 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
43 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
45 static int NetVscOnDeviceRemove(struct hv_device *Device);
47 static void NetVscOnCleanup(struct hv_driver *Driver);
49 static void NetVscOnChannelCallback(void *context);
51 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device);
53 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device);
55 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice);
57 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice);
59 static int NetVscConnectToVsp(struct hv_device *Device);
61 static void NetVscOnSendCompletion(struct hv_device *Device,
62 struct vmpacket_descriptor *Packet);
64 static int NetVscOnSend(struct hv_device *Device,
65 struct hv_netvsc_packet *Packet);
67 static void NetVscOnReceive(struct hv_device *Device,
68 struct vmpacket_descriptor *Packet);
70 static void NetVscOnReceiveCompletion(void *Context);
72 static void NetVscSendReceiveCompletion(struct hv_device *Device,
76 static struct netvsc_device *AllocNetDevice(struct hv_device *Device)
78 struct netvsc_device *netDevice;
80 netDevice = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
84 /* Set to 2 to allow both inbound and outbound traffic */
85 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
87 netDevice->Device = Device;
88 Device->Extension = netDevice;
93 static void FreeNetDevice(struct netvsc_device *Device)
95 ASSERT(atomic_read(&Device->RefCount) == 0);
96 Device->Device->Extension = NULL;
101 /* Get the net device object iff exists and its refcount > 1 */
102 static struct netvsc_device *GetOutboundNetDevice(struct hv_device *Device)
104 struct netvsc_device *netDevice;
106 netDevice = Device->Extension;
107 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
108 atomic_inc(&netDevice->RefCount);
115 /* Get the net device object iff exists and its refcount > 0 */
116 static struct netvsc_device *GetInboundNetDevice(struct hv_device *Device)
118 struct netvsc_device *netDevice;
120 netDevice = Device->Extension;
121 if (netDevice && atomic_read(&netDevice->RefCount))
122 atomic_inc(&netDevice->RefCount);
129 static void PutNetDevice(struct hv_device *Device)
131 struct netvsc_device *netDevice;
133 netDevice = Device->Extension;
136 atomic_dec(&netDevice->RefCount);
139 static struct netvsc_device *ReleaseOutboundNetDevice(struct hv_device *Device)
141 struct netvsc_device *netDevice;
143 netDevice = Device->Extension;
144 if (netDevice == NULL)
147 /* Busy wait until the ref drop to 2, then set it to 1 */
148 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
154 static struct netvsc_device *ReleaseInboundNetDevice(struct hv_device *Device)
156 struct netvsc_device *netDevice;
158 netDevice = Device->Extension;
159 if (netDevice == NULL)
162 /* Busy wait until the ref drop to 1, then set it to 0 */
163 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
166 Device->Extension = NULL;
171 * NetVscInitialize - Main entry point
173 int NetVscInitialize(struct hv_driver *drv)
175 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
177 DPRINT_ENTER(NETVSC);
179 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
180 "sizeof(struct nvsp_message)=%zd, "
181 "sizeof(struct vmtransfer_page_packet_header)=%zd",
182 sizeof(struct hv_netvsc_packet),
183 sizeof(struct nvsp_message),
184 sizeof(struct vmtransfer_page_packet_header));
186 /* Make sure we are at least 2 pages since 1 page is used for control */
187 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
189 drv->name = gDriverName;
190 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
192 /* Make sure it is set by the caller */
193 ASSERT(driver->OnReceiveCallback);
194 ASSERT(driver->OnLinkStatusChanged);
196 /* Setup the dispatch table */
197 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
198 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
199 driver->Base.OnCleanup = NetVscOnCleanup;
201 driver->OnSend = NetVscOnSend;
203 RndisFilterInit(driver);
210 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
213 struct netvsc_device *netDevice;
214 struct nvsp_message *initPacket;
216 DPRINT_ENTER(NETVSC);
218 netDevice = GetOutboundNetDevice(Device);
220 DPRINT_ERR(NETVSC, "unable to get net device..."
221 "device being destroyed?");
225 ASSERT(netDevice->ReceiveBufferSize > 0);
226 /* page-size grandularity */
227 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0);
229 netDevice->ReceiveBuffer =
230 osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
231 if (!netDevice->ReceiveBuffer) {
233 "unable to allocate receive buffer of size %d",
234 netDevice->ReceiveBufferSize);
238 /* page-aligned buffer */
239 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) ==
242 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
245 * Establish the gpadl handle for this buffer on this
246 * channel. Note: This call uses the vmbus connection rather
247 * than the channel to establish the gpadl handle.
249 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
250 netDevice->ReceiveBuffer,
251 netDevice->ReceiveBufferSize,
252 &netDevice->ReceiveBufferGpadlHandle);
255 "unable to establish receive buffer's gpadl");
259 /* osd_WaitEventWait(ext->ChannelInitEvent); */
261 /* Notify the NetVsp of the gpadl handle */
262 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
264 initPacket = &netDevice->ChannelInitPacket;
266 memset(initPacket, 0, sizeof(struct nvsp_message));
268 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
269 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
270 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
272 /* Send the gpadl notification request */
273 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
275 sizeof(struct nvsp_message),
276 (unsigned long)initPacket,
277 VmbusPacketTypeDataInBand,
278 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
281 "unable to send receive buffer's gpadl to netvsp");
285 osd_WaitEventWait(netDevice->ChannelInitEvent);
287 /* Check the response */
288 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess) {
289 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
290 "initialzation with NetVsp - status %d",
291 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
296 /* Parse the response */
297 ASSERT(netDevice->ReceiveSectionCount == 0);
298 ASSERT(netDevice->ReceiveSections == NULL);
300 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
302 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
303 if (netDevice->ReceiveSections == NULL) {
308 memcpy(netDevice->ReceiveSections,
309 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
310 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
312 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
313 "endoffset %d, suballoc size %d, num suballocs %d)",
314 netDevice->ReceiveSectionCount,
315 netDevice->ReceiveSections[0].Offset,
316 netDevice->ReceiveSections[0].EndOffset,
317 netDevice->ReceiveSections[0].SubAllocationSize,
318 netDevice->ReceiveSections[0].NumSubAllocations);
321 * For 1st release, there should only be 1 section that represents the
322 * entire receive buffer
324 if (netDevice->ReceiveSectionCount != 1 ||
325 netDevice->ReceiveSections->Offset != 0) {
333 NetVscDestroyReceiveBuffer(netDevice);
336 PutNetDevice(Device);
341 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
344 struct netvsc_device *netDevice;
345 struct nvsp_message *initPacket;
347 DPRINT_ENTER(NETVSC);
349 netDevice = GetOutboundNetDevice(Device);
351 DPRINT_ERR(NETVSC, "unable to get net device..."
352 "device being destroyed?");
356 ASSERT(netDevice->SendBufferSize > 0);
357 /* page-size grandularity */
358 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0);
360 netDevice->SendBuffer =
361 osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
362 if (!netDevice->SendBuffer) {
363 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
364 netDevice->SendBufferSize);
368 /* page-aligned buffer */
369 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0);
371 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
374 * Establish the gpadl handle for this buffer on this
375 * channel. Note: This call uses the vmbus connection rather
376 * than the channel to establish the gpadl handle.
378 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
379 netDevice->SendBuffer,
380 netDevice->SendBufferSize,
381 &netDevice->SendBufferGpadlHandle);
383 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
387 /* osd_WaitEventWait(ext->ChannelInitEvent); */
389 /* Notify the NetVsp of the gpadl handle */
390 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
392 initPacket = &netDevice->ChannelInitPacket;
394 memset(initPacket, 0, sizeof(struct nvsp_message));
396 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
397 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
398 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
400 /* Send the gpadl notification request */
401 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
402 initPacket, sizeof(struct nvsp_message),
403 (unsigned long)initPacket,
404 VmbusPacketTypeDataInBand,
405 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
408 "unable to send receive buffer's gpadl to netvsp");
412 osd_WaitEventWait(netDevice->ChannelInitEvent);
414 /* Check the response */
415 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess) {
416 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
417 "initialzation with NetVsp - status %d",
418 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
423 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
428 NetVscDestroySendBuffer(netDevice);
431 PutNetDevice(Device);
436 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
438 struct nvsp_message *revokePacket;
441 DPRINT_ENTER(NETVSC);
444 * If we got a section count, it means we received a
445 * SendReceiveBufferComplete msg (ie sent
446 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
447 * to send a revoke msg here
449 if (NetDevice->ReceiveSectionCount) {
451 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
453 /* Send the revoke receive buffer */
454 revokePacket = &NetDevice->RevokePacket;
455 memset(revokePacket, 0, sizeof(struct nvsp_message));
457 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
458 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
460 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
463 sizeof(struct nvsp_message),
464 (unsigned long)revokePacket,
465 VmbusPacketTypeDataInBand, 0);
467 * If we failed here, we might as well return and
468 * have a leak rather than continue and a bugchk
471 DPRINT_ERR(NETVSC, "unable to send revoke receive "
478 /* Teardown the gpadl on the vsp end */
479 if (NetDevice->ReceiveBufferGpadlHandle) {
480 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
482 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
484 NetDevice->ReceiveBufferGpadlHandle);
486 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
489 "unable to teardown receive buffer's gpadl");
493 NetDevice->ReceiveBufferGpadlHandle = 0;
496 if (NetDevice->ReceiveBuffer) {
497 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
499 /* Free up the receive buffer */
500 osd_PageFree(NetDevice->ReceiveBuffer,
501 NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
502 NetDevice->ReceiveBuffer = NULL;
505 if (NetDevice->ReceiveSections) {
506 NetDevice->ReceiveSectionCount = 0;
507 kfree(NetDevice->ReceiveSections);
508 NetDevice->ReceiveSections = NULL;
516 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
518 struct nvsp_message *revokePacket;
521 DPRINT_ENTER(NETVSC);
524 * If we got a section count, it means we received a
525 * SendReceiveBufferComplete msg (ie sent
526 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
527 * to send a revoke msg here
529 if (NetDevice->SendSectionSize) {
531 "Sending NvspMessage1TypeRevokeSendBuffer...");
533 /* Send the revoke send buffer */
534 revokePacket = &NetDevice->RevokePacket;
535 memset(revokePacket, 0, sizeof(struct nvsp_message));
537 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
538 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
540 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
542 sizeof(struct nvsp_message),
543 (unsigned long)revokePacket,
544 VmbusPacketTypeDataInBand, 0);
546 * If we failed here, we might as well return and have a leak
547 * rather than continue and a bugchk
550 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
557 /* Teardown the gpadl on the vsp end */
558 if (NetDevice->SendBufferGpadlHandle) {
559 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
561 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
564 * If we failed here, we might as well return and have a leak
565 * rather than continue and a bugchk
568 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
573 NetDevice->SendBufferGpadlHandle = 0;
576 if (NetDevice->SendBuffer) {
577 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
579 /* Free up the receive buffer */
580 osd_PageFree(NetDevice->SendBuffer,
581 NetDevice->SendBufferSize >> PAGE_SHIFT);
582 NetDevice->SendBuffer = NULL;
591 static int NetVscConnectToVsp(struct hv_device *Device)
594 struct netvsc_device *netDevice;
595 struct nvsp_message *initPacket;
598 DPRINT_ENTER(NETVSC);
600 netDevice = GetOutboundNetDevice(Device);
602 DPRINT_ERR(NETVSC, "unable to get net device..."
603 "device being destroyed?");
608 initPacket = &netDevice->ChannelInitPacket;
610 memset(initPacket, 0, sizeof(struct nvsp_message));
611 initPacket->Header.MessageType = NvspMessageTypeInit;
612 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
613 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
615 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
617 /* Send the init request */
618 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
620 sizeof(struct nvsp_message),
621 (unsigned long)initPacket,
622 VmbusPacketTypeDataInBand,
623 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
626 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
630 osd_WaitEventWait(netDevice->ChannelInitEvent);
632 /* Now, check the response */
633 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
634 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
635 initPacket->Messages.InitMessages.InitComplete.Status,
636 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
638 if (initPacket->Messages.InitMessages.InitComplete.Status !=
641 "unable to initialize with netvsp (status 0x%x)",
642 initPacket->Messages.InitMessages.InitComplete.Status);
647 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) {
648 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
649 "(version expected 1 got %d)",
650 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
654 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
656 /* Send the ndis version */
657 memset(initPacket, 0, sizeof(struct nvsp_message));
659 ndisVersion = 0x00050000;
661 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
662 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
663 (ndisVersion & 0xFFFF0000) >> 16;
664 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
665 ndisVersion & 0xFFFF;
667 /* Send the init request */
668 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
670 sizeof(struct nvsp_message),
671 (unsigned long)initPacket,
672 VmbusPacketTypeDataInBand, 0);
675 "unable to send NvspMessage1TypeSendNdisVersion");
680 * BUGBUG - We have to wait for the above msg since the
681 * netvsp uses KMCL which acknowledges packet (completion
682 * packet) since our Vmbus always set the
683 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
685 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
687 /* Post the big receive buffer to NetVSP */
688 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
690 ret = NetVscInitializeSendBufferWithNetVsp(Device);
693 PutNetDevice(Device);
698 static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
700 DPRINT_ENTER(NETVSC);
702 NetVscDestroyReceiveBuffer(NetDevice);
703 NetVscDestroySendBuffer(NetDevice);
709 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
711 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
715 struct netvsc_device *netDevice;
716 struct hv_netvsc_packet *packet, *pos;
717 struct netvsc_driver *netDriver =
718 (struct netvsc_driver *)Device->Driver;
720 DPRINT_ENTER(NETVSC);
722 netDevice = AllocNetDevice(Device);
728 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
730 /* Initialize the NetVSC channel extension */
731 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
732 spin_lock_init(&netDevice->receive_packet_list_lock);
734 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
736 INIT_LIST_HEAD(&netDevice->ReceivePacketList);
738 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
739 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
740 (NETVSC_RECEIVE_SG_COUNT *
741 sizeof(struct hv_page_buffer)), GFP_KERNEL);
743 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
744 "for receive pool (wanted %d got %d)",
745 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
748 list_add_tail(&packet->ListEntry,
749 &netDevice->ReceivePacketList);
751 netDevice->ChannelInitEvent = osd_WaitEventCreate();
753 /* Open the channel */
754 ret = Device->Driver->VmbusChannelInterface.Open(Device,
755 netDriver->RingBufferSize,
756 netDriver->RingBufferSize,
758 NetVscOnChannelCallback,
762 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
767 /* Channel is opened */
768 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
770 /* Connect with the NetVsp */
771 ret = NetVscConnectToVsp(Device);
773 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
778 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
785 /* Now, we can close the channel safely */
786 Device->Driver->VmbusChannelInterface.Close(Device);
791 kfree(netDevice->ChannelInitEvent);
793 list_for_each_entry_safe(packet, pos,
794 &netDevice->ReceivePacketList,
796 list_del(&packet->ListEntry);
800 ReleaseOutboundNetDevice(Device);
801 ReleaseInboundNetDevice(Device);
803 FreeNetDevice(netDevice);
811 * NetVscOnDeviceRemove - Callback when the root bus device is removed
813 static int NetVscOnDeviceRemove(struct hv_device *Device)
815 struct netvsc_device *netDevice;
816 struct hv_netvsc_packet *netvscPacket, *pos;
818 DPRINT_ENTER(NETVSC);
820 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
823 /* Stop outbound traffic ie sends and receives completions */
824 netDevice = ReleaseOutboundNetDevice(Device);
826 DPRINT_ERR(NETVSC, "No net device present!!");
830 /* Wait for all send completions */
831 while (atomic_read(&netDevice->NumOutstandingSends)) {
832 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
833 atomic_read(&netDevice->NumOutstandingSends));
837 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
839 NetVscDisconnectFromVsp(netDevice);
841 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
844 /* Stop inbound traffic ie receives and sends completions */
845 netDevice = ReleaseInboundNetDevice(Device);
847 /* At this point, no one should be accessing netDevice except in here */
848 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
850 /* Now, we can close the channel safely */
851 Device->Driver->VmbusChannelInterface.Close(Device);
853 /* Release all resources */
854 list_for_each_entry_safe(netvscPacket, pos,
855 &netDevice->ReceivePacketList, ListEntry) {
856 list_del(&netvscPacket->ListEntry);
860 kfree(netDevice->ChannelInitEvent);
861 FreeNetDevice(netDevice);
868 * NetVscOnCleanup - Perform any cleanup when the driver is removed
870 static void NetVscOnCleanup(struct hv_driver *drv)
872 DPRINT_ENTER(NETVSC);
876 static void NetVscOnSendCompletion(struct hv_device *Device,
877 struct vmpacket_descriptor *Packet)
879 struct netvsc_device *netDevice;
880 struct nvsp_message *nvspPacket;
881 struct hv_netvsc_packet *nvscPacket;
883 DPRINT_ENTER(NETVSC);
885 netDevice = GetInboundNetDevice(Device);
887 DPRINT_ERR(NETVSC, "unable to get net device..."
888 "device being destroyed?");
893 nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
895 DPRINT_DBG(NETVSC, "send completion packet - type %d",
896 nvspPacket->Header.MessageType);
898 if ((nvspPacket->Header.MessageType == NvspMessageTypeInitComplete) ||
899 (nvspPacket->Header.MessageType ==
900 NvspMessage1TypeSendReceiveBufferComplete) ||
901 (nvspPacket->Header.MessageType ==
902 NvspMessage1TypeSendSendBufferComplete)) {
903 /* Copy the response back */
904 memcpy(&netDevice->ChannelInitPacket, nvspPacket,
905 sizeof(struct nvsp_message));
906 osd_WaitEventSet(netDevice->ChannelInitEvent);
907 } else if (nvspPacket->Header.MessageType ==
908 NvspMessage1TypeSendRNDISPacketComplete) {
909 /* Get the send context */
910 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
913 /* Notify the layer above us */
914 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
916 atomic_dec(&netDevice->NumOutstandingSends);
918 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
919 "%d received!!", nvspPacket->Header.MessageType);
922 PutNetDevice(Device);
926 static int NetVscOnSend(struct hv_device *Device,
927 struct hv_netvsc_packet *Packet)
929 struct netvsc_device *netDevice;
932 struct nvsp_message sendMessage;
934 DPRINT_ENTER(NETVSC);
936 netDevice = GetOutboundNetDevice(Device);
938 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
939 "ignoring outbound packets", netDevice);
944 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
945 if (Packet->IsDataPacket) {
947 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
949 /* 1 is RMC_CONTROL; */
950 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
953 /* Not using send buffer section */
954 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
955 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
957 if (Packet->PageBufferCount) {
958 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(
959 Device, Packet->PageBuffers,
960 Packet->PageBufferCount,
962 sizeof(struct nvsp_message),
963 (unsigned long)Packet);
965 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
967 sizeof(struct nvsp_message),
968 (unsigned long)Packet,
969 VmbusPacketTypeDataInBand,
970 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
975 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
978 atomic_inc(&netDevice->NumOutstandingSends);
979 PutNetDevice(Device);
985 static void NetVscOnReceive(struct hv_device *Device,
986 struct vmpacket_descriptor *Packet)
988 struct netvsc_device *netDevice;
989 struct vmtransfer_page_packet_header *vmxferpagePacket;
990 struct nvsp_message *nvspPacket;
991 struct hv_netvsc_packet *netvscPacket = NULL;
993 unsigned long end, endVirtual;
994 /* struct netvsc_driver *netvscDriver; */
995 struct xferpage_packet *xferpagePacket = NULL;
997 int count = 0, bytesRemain = 0;
1001 DPRINT_ENTER(NETVSC);
1003 netDevice = GetInboundNetDevice(Device);
1005 DPRINT_ERR(NETVSC, "unable to get net device..."
1006 "device being destroyed?");
1007 DPRINT_EXIT(NETVSC);
1012 * All inbound packets other than send completion should be xfer page
1015 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
1016 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
1018 PutNetDevice(Device);
1022 nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
1023 (Packet->DataOffset8 << 3));
1025 /* Make sure this is a valid nvsp packet */
1026 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket) {
1027 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1028 nvspPacket->Header.MessageType);
1029 PutNetDevice(Device);
1033 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1034 nvspPacket->Header.MessageType);
1036 vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
1038 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1039 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1040 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1041 vmxferpagePacket->TransferPageSetId);
1042 PutNetDevice(Device);
1046 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1047 vmxferpagePacket->RangeCount);
1050 * Grab free packets (range count + 1) to represent this xfer
1051 * page packet. +1 to represent the xfer page packet itself.
1052 * We grab it here so that we know exactly how many we can
1055 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1056 while (!list_empty(&netDevice->ReceivePacketList)) {
1057 list_move_tail(netDevice->ReceivePacketList.next, &listHead);
1058 if (++count == vmxferpagePacket->RangeCount + 1)
1061 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1064 * We need at least 2 netvsc pkts (1 to represent the xfer
1065 * page and at least 1 for the range) i.e. we can handled
1066 * some of the xfer page packet ranges...
1069 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1070 "Dropping this xfer page packet completely!",
1071 count, vmxferpagePacket->RangeCount + 1);
1073 /* Return it to the freelist */
1074 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1075 for (i = count; i != 0; i--) {
1076 list_move_tail(listHead.next,
1077 &netDevice->ReceivePacketList);
1079 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1082 NetVscSendReceiveCompletion(Device,
1083 vmxferpagePacket->d.TransactionId);
1085 PutNetDevice(Device);
1089 /* Remove the 1st packet to represent the xfer page packet itself */
1090 xferpagePacket = (struct xferpage_packet *)listHead.next;
1091 list_del(&xferpagePacket->ListEntry);
1093 /* This is how much we can satisfy */
1094 xferpagePacket->Count = count - 1;
1095 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <=
1096 vmxferpagePacket->RangeCount);
1098 if (xferpagePacket->Count != vmxferpagePacket->RangeCount) {
1099 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1100 "page...got %d", vmxferpagePacket->RangeCount,
1101 xferpagePacket->Count);
1104 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1105 for (i = 0; i < (count - 1); i++) {
1106 netvscPacket = (struct hv_netvsc_packet *)listHead.next;
1107 list_del(&netvscPacket->ListEntry);
1109 /* Initialize the netvsc packet */
1110 netvscPacket->XferPagePacket = xferpagePacket;
1111 netvscPacket->Completion.Recv.OnReceiveCompletion =
1112 NetVscOnReceiveCompletion;
1113 netvscPacket->Completion.Recv.ReceiveCompletionContext =
1115 netvscPacket->Device = Device;
1116 /* Save this so that we can send it back */
1117 netvscPacket->Completion.Recv.ReceiveCompletionTid =
1118 vmxferpagePacket->d.TransactionId;
1120 netvscPacket->TotalDataBufferLength =
1121 vmxferpagePacket->Ranges[i].ByteCount;
1122 netvscPacket->PageBufferCount = 1;
1124 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset +
1125 vmxferpagePacket->Ranges[i].ByteCount <
1126 netDevice->ReceiveBufferSize);
1128 netvscPacket->PageBuffers[0].Length =
1129 vmxferpagePacket->Ranges[i].ByteCount;
1131 start = virt_to_phys((void *)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1133 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1134 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1135 + vmxferpagePacket->Ranges[i].ByteOffset
1136 + vmxferpagePacket->Ranges[i].ByteCount - 1;
1137 end = virt_to_phys((void *)endVirtual);
1139 /* Calculate the page relative offset */
1140 netvscPacket->PageBuffers[0].Offset =
1141 vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE - 1);
1142 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1143 /* Handle frame across multiple pages: */
1144 netvscPacket->PageBuffers[0].Length =
1145 (netvscPacket->PageBuffers[0].Pfn << PAGE_SHIFT)
1146 + PAGE_SIZE - start;
1147 bytesRemain = netvscPacket->TotalDataBufferLength -
1148 netvscPacket->PageBuffers[0].Length;
1149 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1150 netvscPacket->PageBuffers[j].Offset = 0;
1151 if (bytesRemain <= PAGE_SIZE) {
1152 netvscPacket->PageBuffers[j].Length = bytesRemain;
1155 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1156 bytesRemain -= PAGE_SIZE;
1158 netvscPacket->PageBuffers[j].Pfn =
1159 virt_to_phys((void *)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1160 netvscPacket->PageBufferCount++;
1161 if (bytesRemain == 0)
1164 ASSERT(bytesRemain == 0);
1166 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1167 "(pfn %llx, offset %u, len %u)", i,
1168 vmxferpagePacket->Ranges[i].ByteOffset,
1169 vmxferpagePacket->Ranges[i].ByteCount,
1170 netvscPacket->PageBuffers[0].Pfn,
1171 netvscPacket->PageBuffers[0].Offset,
1172 netvscPacket->PageBuffers[0].Length);
1174 /* Pass it to the upper layer */
1175 ((struct netvsc_driver *)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1177 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1180 ASSERT(list_empty(&listHead));
1182 PutNetDevice(Device);
1183 DPRINT_EXIT(NETVSC);
1186 static void NetVscSendReceiveCompletion(struct hv_device *Device,
1189 struct nvsp_message recvcompMessage;
1193 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1196 recvcompMessage.Header.MessageType =
1197 NvspMessage1TypeSendRNDISPacketComplete;
1199 /* FIXME: Pass in the status */
1200 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1203 /* Send the completion */
1204 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1206 sizeof(struct nvsp_message),
1208 VmbusPacketTypeCompletion, 0);
1212 } else if (ret == -1) {
1213 /* no more room...wait a bit and attempt to retry 3 times */
1215 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1216 "(tid %llx)...retrying %d", TransactionId, retries);
1220 goto retry_send_cmplt;
1222 DPRINT_ERR(NETVSC, "unable to send receive completion "
1223 "pkt (tid %llx)...give up retrying",
1227 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1228 "%llx", TransactionId);
1232 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1233 static void NetVscOnReceiveCompletion(void *Context)
1235 struct hv_netvsc_packet *packet = Context;
1236 struct hv_device *device = (struct hv_device *)packet->Device;
1237 struct netvsc_device *netDevice;
1238 u64 transactionId = 0;
1239 bool fSendReceiveComp = false;
1240 unsigned long flags;
1242 DPRINT_ENTER(NETVSC);
1244 ASSERT(packet->XferPagePacket);
1247 * Even though it seems logical to do a GetOutboundNetDevice() here to
1248 * send out receive completion, we are using GetInboundNetDevice()
1249 * since we may have disable outbound traffic already.
1251 netDevice = GetInboundNetDevice(device);
1253 DPRINT_ERR(NETVSC, "unable to get net device..."
1254 "device being destroyed?");
1255 DPRINT_EXIT(NETVSC);
1259 /* Overloading use of the lock. */
1260 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1262 ASSERT(packet->XferPagePacket->Count > 0);
1263 packet->XferPagePacket->Count--;
1266 * Last one in the line that represent 1 xfer page packet.
1267 * Return the xfer page packet itself to the freelist
1269 if (packet->XferPagePacket->Count == 0) {
1270 fSendReceiveComp = true;
1271 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1272 list_add_tail(&packet->XferPagePacket->ListEntry,
1273 &netDevice->ReceivePacketList);
1277 /* Put the packet back */
1278 list_add_tail(&packet->ListEntry, &netDevice->ReceivePacketList);
1279 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1281 /* Send a receive completion for the xfer page packet */
1282 if (fSendReceiveComp)
1283 NetVscSendReceiveCompletion(device, transactionId);
1285 PutNetDevice(device);
1286 DPRINT_EXIT(NETVSC);
1289 void NetVscOnChannelCallback(void *Context)
1292 struct hv_device *device = Context;
1293 struct netvsc_device *netDevice;
1296 unsigned char *packet;
1297 struct vmpacket_descriptor *desc;
1298 unsigned char *buffer;
1299 int bufferlen = NETVSC_PACKET_SIZE;
1302 DPRINT_ENTER(NETVSC);
1306 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1312 netDevice = GetInboundNetDevice(device);
1314 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1315 "ignoring inbound packets", netDevice);
1316 DPRINT_EXIT(NETVSC);
1321 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(
1322 device, buffer, bufferlen,
1323 &bytesRecvd, &requestId);
1325 if (bytesRecvd > 0) {
1326 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1327 bytesRecvd, requestId);
1329 desc = (struct vmpacket_descriptor *)buffer;
1330 switch (desc->Type) {
1331 case VmbusPacketTypeCompletion:
1332 NetVscOnSendCompletion(device, desc);
1335 case VmbusPacketTypeDataUsingTransferPages:
1336 NetVscOnReceive(device, desc);
1341 "unhandled packet type %d, "
1342 "tid %llx len %d\n",
1343 desc->Type, requestId,
1349 if (bufferlen > NETVSC_PACKET_SIZE) {
1352 bufferlen = NETVSC_PACKET_SIZE;
1356 if (bufferlen > NETVSC_PACKET_SIZE) {
1359 bufferlen = NETVSC_PACKET_SIZE;
1364 } else if (ret == -2) {
1365 /* Handle large packet */
1366 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
1367 if (buffer == NULL) {
1368 /* Try again next time around */
1370 "unable to allocate buffer of size "
1371 "(%d)!!", bytesRecvd);
1375 bufferlen = bytesRecvd;
1381 PutNetDevice(device);
1382 DPRINT_EXIT(NETVSC);