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 "rndis_filter.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 WARN_ON(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
;
134 /* ASSERT(netDevice); */
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_DBG(NETVSC
, "sizeof(struct hv_netvsc_packet)=%zd, "
178 "sizeof(struct nvsp_message)=%zd, "
179 "sizeof(struct vmtransfer_page_packet_header)=%zd",
180 sizeof(struct hv_netvsc_packet
),
181 sizeof(struct nvsp_message
),
182 sizeof(struct vmtransfer_page_packet_header
));
184 /* Make sure we are at least 2 pages since 1 page is used for control */
185 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
187 drv
->name
= gDriverName
;
188 memcpy(&drv
->deviceType
, &gNetVscDeviceType
, sizeof(struct hv_guid
));
190 /* Make sure it is set by the caller */
191 /* FIXME: These probably should still be tested in some way */
192 /* ASSERT(driver->OnReceiveCallback); */
193 /* ASSERT(driver->OnLinkStatusChanged); */
195 /* Setup the dispatch table */
196 driver
->Base
.OnDeviceAdd
= NetVscOnDeviceAdd
;
197 driver
->Base
.OnDeviceRemove
= NetVscOnDeviceRemove
;
198 driver
->Base
.OnCleanup
= NetVscOnCleanup
;
200 driver
->OnSend
= NetVscOnSend
;
202 RndisFilterInit(driver
);
209 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device
*Device
)
212 struct netvsc_device
*netDevice
;
213 struct nvsp_message
*initPacket
;
215 netDevice
= GetOutboundNetDevice(Device
);
217 DPRINT_ERR(NETVSC
, "unable to get net device..."
218 "device being destroyed?");
222 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
223 /* page-size grandularity */
224 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
226 netDevice
->ReceiveBuffer
=
227 osd_PageAlloc(netDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
228 if (!netDevice
->ReceiveBuffer
) {
230 "unable to allocate receive buffer of size %d",
231 netDevice
->ReceiveBufferSize
);
235 /* page-aligned buffer */
236 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
239 DPRINT_INFO(NETVSC
, "Establishing receive buffer's GPADL...");
242 * Establish the gpadl handle for this buffer on this
243 * channel. Note: This call uses the vmbus connection rather
244 * than the channel to establish the gpadl handle.
246 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
247 netDevice
->ReceiveBuffer
,
248 netDevice
->ReceiveBufferSize
,
249 &netDevice
->ReceiveBufferGpadlHandle
);
252 "unable to establish receive buffer's gpadl");
256 /* osd_WaitEventWait(ext->ChannelInitEvent); */
258 /* Notify the NetVsp of the gpadl handle */
259 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendReceiveBuffer...");
261 initPacket
= &netDevice
->ChannelInitPacket
;
263 memset(initPacket
, 0, sizeof(struct nvsp_message
));
265 initPacket
->Header
.MessageType
= NvspMessage1TypeSendReceiveBuffer
;
266 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->ReceiveBufferGpadlHandle
;
267 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
269 /* Send the gpadl notification request */
270 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
272 sizeof(struct nvsp_message
),
273 (unsigned long)initPacket
,
274 VmbusPacketTypeDataInBand
,
275 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
278 "unable to send receive buffer's gpadl to netvsp");
282 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
284 /* Check the response */
285 if (initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
!= NvspStatusSuccess
) {
286 DPRINT_ERR(NETVSC
, "Unable to complete receive buffer "
287 "initialzation with NetVsp - status %d",
288 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
);
293 /* Parse the response */
294 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
295 /* ASSERT(netDevice->ReceiveSections == NULL); */
297 netDevice
->ReceiveSectionCount
= initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.NumSections
;
299 netDevice
->ReceiveSections
= kmalloc(netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
300 if (netDevice
->ReceiveSections
== NULL
) {
305 memcpy(netDevice
->ReceiveSections
,
306 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Sections
,
307 netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
));
309 DPRINT_INFO(NETVSC
, "Receive sections info (count %d, offset %d, "
310 "endoffset %d, suballoc size %d, num suballocs %d)",
311 netDevice
->ReceiveSectionCount
,
312 netDevice
->ReceiveSections
[0].Offset
,
313 netDevice
->ReceiveSections
[0].EndOffset
,
314 netDevice
->ReceiveSections
[0].SubAllocationSize
,
315 netDevice
->ReceiveSections
[0].NumSubAllocations
);
318 * For 1st release, there should only be 1 section that represents the
319 * entire receive buffer
321 if (netDevice
->ReceiveSectionCount
!= 1 ||
322 netDevice
->ReceiveSections
->Offset
!= 0) {
330 NetVscDestroyReceiveBuffer(netDevice
);
333 PutNetDevice(Device
);
338 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device
*Device
)
341 struct netvsc_device
*netDevice
;
342 struct nvsp_message
*initPacket
;
344 netDevice
= GetOutboundNetDevice(Device
);
346 DPRINT_ERR(NETVSC
, "unable to get net device..."
347 "device being destroyed?");
351 if (netDevice
->SendBufferSize
<= 0) {
356 /* page-size grandularity */
357 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
359 netDevice
->SendBuffer
=
360 osd_PageAlloc(netDevice
->SendBufferSize
>> PAGE_SHIFT
);
361 if (!netDevice
->SendBuffer
) {
362 DPRINT_ERR(NETVSC
, "unable to allocate send buffer of size %d",
363 netDevice
->SendBufferSize
);
367 /* page-aligned buffer */
368 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
370 DPRINT_INFO(NETVSC
, "Establishing send buffer's GPADL...");
373 * Establish the gpadl handle for this buffer on this
374 * channel. Note: This call uses the vmbus connection rather
375 * than the channel to establish the gpadl handle.
377 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
378 netDevice
->SendBuffer
,
379 netDevice
->SendBufferSize
,
380 &netDevice
->SendBufferGpadlHandle
);
382 DPRINT_ERR(NETVSC
, "unable to establish send buffer's gpadl");
386 /* osd_WaitEventWait(ext->ChannelInitEvent); */
388 /* Notify the NetVsp of the gpadl handle */
389 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendSendBuffer...");
391 initPacket
= &netDevice
->ChannelInitPacket
;
393 memset(initPacket
, 0, sizeof(struct nvsp_message
));
395 initPacket
->Header
.MessageType
= NvspMessage1TypeSendSendBuffer
;
396 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->SendBufferGpadlHandle
;
397 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
399 /* Send the gpadl notification request */
400 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
401 initPacket
, sizeof(struct nvsp_message
),
402 (unsigned long)initPacket
,
403 VmbusPacketTypeDataInBand
,
404 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
407 "unable to send receive buffer's gpadl to netvsp");
411 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
413 /* Check the response */
414 if (initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
!= NvspStatusSuccess
) {
415 DPRINT_ERR(NETVSC
, "Unable to complete send buffer "
416 "initialzation with NetVsp - status %d",
417 initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
);
422 netDevice
->SendSectionSize
= initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.SectionSize
;
427 NetVscDestroySendBuffer(netDevice
);
430 PutNetDevice(Device
);
435 static int NetVscDestroyReceiveBuffer(struct netvsc_device
*NetDevice
)
437 struct nvsp_message
*revokePacket
;
441 * If we got a section count, it means we received a
442 * SendReceiveBufferComplete msg (ie sent
443 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
444 * to send a revoke msg here
446 if (NetDevice
->ReceiveSectionCount
) {
448 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
450 /* Send the revoke receive buffer */
451 revokePacket
= &NetDevice
->RevokePacket
;
452 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
454 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeReceiveBuffer
;
455 revokePacket
->Messages
.Version1Messages
.RevokeReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
457 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(
460 sizeof(struct nvsp_message
),
461 (unsigned long)revokePacket
,
462 VmbusPacketTypeDataInBand
, 0);
464 * If we failed here, we might as well return and
465 * have a leak rather than continue and a bugchk
468 DPRINT_ERR(NETVSC
, "unable to send revoke receive "
475 /* Teardown the gpadl on the vsp end */
476 if (NetDevice
->ReceiveBufferGpadlHandle
) {
477 DPRINT_INFO(NETVSC
, "Tearing down receive buffer's GPADL...");
479 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(
481 NetDevice
->ReceiveBufferGpadlHandle
);
483 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
486 "unable to teardown receive buffer's gpadl");
490 NetDevice
->ReceiveBufferGpadlHandle
= 0;
493 if (NetDevice
->ReceiveBuffer
) {
494 DPRINT_INFO(NETVSC
, "Freeing up receive buffer...");
496 /* Free up the receive buffer */
497 osd_PageFree(NetDevice
->ReceiveBuffer
,
498 NetDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
499 NetDevice
->ReceiveBuffer
= NULL
;
502 if (NetDevice
->ReceiveSections
) {
503 NetDevice
->ReceiveSectionCount
= 0;
504 kfree(NetDevice
->ReceiveSections
);
505 NetDevice
->ReceiveSections
= NULL
;
513 static int NetVscDestroySendBuffer(struct netvsc_device
*NetDevice
)
515 struct nvsp_message
*revokePacket
;
519 * If we got a section count, it means we received a
520 * SendReceiveBufferComplete msg (ie sent
521 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
522 * to send a revoke msg here
524 if (NetDevice
->SendSectionSize
) {
526 "Sending NvspMessage1TypeRevokeSendBuffer...");
528 /* Send the revoke send buffer */
529 revokePacket
= &NetDevice
->RevokePacket
;
530 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
532 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeSendBuffer
;
533 revokePacket
->Messages
.Version1Messages
.RevokeSendBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
535 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(NetDevice
->Device
,
537 sizeof(struct nvsp_message
),
538 (unsigned long)revokePacket
,
539 VmbusPacketTypeDataInBand
, 0);
541 * If we failed here, we might as well return and have a leak
542 * rather than continue and a bugchk
545 DPRINT_ERR(NETVSC
, "unable to send revoke send buffer "
552 /* Teardown the gpadl on the vsp end */
553 if (NetDevice
->SendBufferGpadlHandle
) {
554 DPRINT_INFO(NETVSC
, "Tearing down send buffer's GPADL...");
556 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(NetDevice
->Device
, NetDevice
->SendBufferGpadlHandle
);
559 * If we failed here, we might as well return and have a leak
560 * rather than continue and a bugchk
563 DPRINT_ERR(NETVSC
, "unable to teardown send buffer's "
568 NetDevice
->SendBufferGpadlHandle
= 0;
571 if (NetDevice
->SendBuffer
) {
572 DPRINT_INFO(NETVSC
, "Freeing up send buffer...");
574 /* Free up the receive buffer */
575 osd_PageFree(NetDevice
->SendBuffer
,
576 NetDevice
->SendBufferSize
>> PAGE_SHIFT
);
577 NetDevice
->SendBuffer
= NULL
;
586 static int NetVscConnectToVsp(struct hv_device
*Device
)
589 struct netvsc_device
*netDevice
;
590 struct nvsp_message
*initPacket
;
593 netDevice
= GetOutboundNetDevice(Device
);
595 DPRINT_ERR(NETVSC
, "unable to get net device..."
596 "device being destroyed?");
601 initPacket
= &netDevice
->ChannelInitPacket
;
603 memset(initPacket
, 0, sizeof(struct nvsp_message
));
604 initPacket
->Header
.MessageType
= NvspMessageTypeInit
;
605 initPacket
->Messages
.InitMessages
.Init
.MinProtocolVersion
= NVSP_MIN_PROTOCOL_VERSION
;
606 initPacket
->Messages
.InitMessages
.Init
.MaxProtocolVersion
= NVSP_MAX_PROTOCOL_VERSION
;
608 DPRINT_INFO(NETVSC
, "Sending NvspMessageTypeInit...");
610 /* Send the init request */
611 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
613 sizeof(struct nvsp_message
),
614 (unsigned long)initPacket
,
615 VmbusPacketTypeDataInBand
,
616 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
619 DPRINT_ERR(NETVSC
, "unable to send NvspMessageTypeInit");
623 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
625 /* Now, check the response */
626 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
627 DPRINT_INFO(NETVSC
, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
628 initPacket
->Messages
.InitMessages
.InitComplete
.Status
,
629 initPacket
->Messages
.InitMessages
.InitComplete
.MaximumMdlChainLength
);
631 if (initPacket
->Messages
.InitMessages
.InitComplete
.Status
!=
634 "unable to initialize with netvsp (status 0x%x)",
635 initPacket
->Messages
.InitMessages
.InitComplete
.Status
);
640 if (initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
!= NVSP_PROTOCOL_VERSION_1
) {
641 DPRINT_ERR(NETVSC
, "unable to initialize with netvsp "
642 "(version expected 1 got %d)",
643 initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
);
647 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendNdisVersion...");
649 /* Send the ndis version */
650 memset(initPacket
, 0, sizeof(struct nvsp_message
));
652 ndisVersion
= 0x00050000;
654 initPacket
->Header
.MessageType
= NvspMessage1TypeSendNdisVersion
;
655 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMajorVersion
=
656 (ndisVersion
& 0xFFFF0000) >> 16;
657 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMinorVersion
=
658 ndisVersion
& 0xFFFF;
660 /* Send the init request */
661 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
663 sizeof(struct nvsp_message
),
664 (unsigned long)initPacket
,
665 VmbusPacketTypeDataInBand
, 0);
668 "unable to send NvspMessage1TypeSendNdisVersion");
673 * BUGBUG - We have to wait for the above msg since the
674 * netvsp uses KMCL which acknowledges packet (completion
675 * packet) since our Vmbus always set the
676 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
678 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
680 /* Post the big receive buffer to NetVSP */
681 ret
= NetVscInitializeReceiveBufferWithNetVsp(Device
);
683 ret
= NetVscInitializeSendBufferWithNetVsp(Device
);
686 PutNetDevice(Device
);
691 static void NetVscDisconnectFromVsp(struct netvsc_device
*NetDevice
)
693 NetVscDestroyReceiveBuffer(NetDevice
);
694 NetVscDestroySendBuffer(NetDevice
);
700 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
702 static int NetVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
706 struct netvsc_device
*netDevice
;
707 struct hv_netvsc_packet
*packet
, *pos
;
708 struct netvsc_driver
*netDriver
=
709 (struct netvsc_driver
*)Device
->Driver
;
711 netDevice
= AllocNetDevice(Device
);
717 DPRINT_DBG(NETVSC
, "netvsc channel object allocated - %p", netDevice
);
719 /* Initialize the NetVSC channel extension */
720 netDevice
->ReceiveBufferSize
= NETVSC_RECEIVE_BUFFER_SIZE
;
721 spin_lock_init(&netDevice
->receive_packet_list_lock
);
723 netDevice
->SendBufferSize
= NETVSC_SEND_BUFFER_SIZE
;
725 INIT_LIST_HEAD(&netDevice
->ReceivePacketList
);
727 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
728 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
729 (NETVSC_RECEIVE_SG_COUNT
*
730 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
732 DPRINT_DBG(NETVSC
, "unable to allocate netvsc pkts "
733 "for receive pool (wanted %d got %d)",
734 NETVSC_RECEIVE_PACKETLIST_COUNT
, i
);
737 list_add_tail(&packet
->ListEntry
,
738 &netDevice
->ReceivePacketList
);
740 netDevice
->ChannelInitEvent
= osd_WaitEventCreate();
741 if (!netDevice
->ChannelInitEvent
) {
746 /* Open the channel */
747 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
748 netDriver
->RingBufferSize
,
749 netDriver
->RingBufferSize
,
751 NetVscOnChannelCallback
,
755 DPRINT_ERR(NETVSC
, "unable to open channel: %d", ret
);
760 /* Channel is opened */
761 DPRINT_INFO(NETVSC
, "*** NetVSC channel opened successfully! ***");
763 /* Connect with the NetVsp */
764 ret
= NetVscConnectToVsp(Device
);
766 DPRINT_ERR(NETVSC
, "unable to connect to NetVSP - %d", ret
);
771 DPRINT_INFO(NETVSC
, "*** NetVSC channel handshake result - %d ***",
778 /* Now, we can close the channel safely */
779 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
784 kfree(netDevice
->ChannelInitEvent
);
786 list_for_each_entry_safe(packet
, pos
,
787 &netDevice
->ReceivePacketList
,
789 list_del(&packet
->ListEntry
);
793 ReleaseOutboundNetDevice(Device
);
794 ReleaseInboundNetDevice(Device
);
796 FreeNetDevice(netDevice
);
804 * NetVscOnDeviceRemove - Callback when the root bus device is removed
806 static int NetVscOnDeviceRemove(struct hv_device
*Device
)
808 struct netvsc_device
*netDevice
;
809 struct hv_netvsc_packet
*netvscPacket
, *pos
;
811 DPRINT_INFO(NETVSC
, "Disabling outbound traffic on net device (%p)...",
814 /* Stop outbound traffic ie sends and receives completions */
815 netDevice
= ReleaseOutboundNetDevice(Device
);
817 DPRINT_ERR(NETVSC
, "No net device present!!");
821 /* Wait for all send completions */
822 while (atomic_read(&netDevice
->NumOutstandingSends
)) {
823 DPRINT_INFO(NETVSC
, "waiting for %d requests to complete...",
824 atomic_read(&netDevice
->NumOutstandingSends
));
828 DPRINT_INFO(NETVSC
, "Disconnecting from netvsp...");
830 NetVscDisconnectFromVsp(netDevice
);
832 DPRINT_INFO(NETVSC
, "Disabling inbound traffic on net device (%p)...",
835 /* Stop inbound traffic ie receives and sends completions */
836 netDevice
= ReleaseInboundNetDevice(Device
);
838 /* At this point, no one should be accessing netDevice except in here */
839 DPRINT_INFO(NETVSC
, "net device (%p) safe to remove", netDevice
);
841 /* Now, we can close the channel safely */
842 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
844 /* Release all resources */
845 list_for_each_entry_safe(netvscPacket
, pos
,
846 &netDevice
->ReceivePacketList
, ListEntry
) {
847 list_del(&netvscPacket
->ListEntry
);
851 kfree(netDevice
->ChannelInitEvent
);
852 FreeNetDevice(netDevice
);
859 * NetVscOnCleanup - Perform any cleanup when the driver is removed
861 static void NetVscOnCleanup(struct hv_driver
*drv
)
866 static void NetVscOnSendCompletion(struct hv_device
*Device
,
867 struct vmpacket_descriptor
*Packet
)
869 struct netvsc_device
*netDevice
;
870 struct nvsp_message
*nvspPacket
;
871 struct hv_netvsc_packet
*nvscPacket
;
873 netDevice
= GetInboundNetDevice(Device
);
875 DPRINT_ERR(NETVSC
, "unable to get net device..."
876 "device being destroyed?");
881 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+ (Packet
->DataOffset8
<< 3));
883 DPRINT_DBG(NETVSC
, "send completion packet - type %d",
884 nvspPacket
->Header
.MessageType
);
886 if ((nvspPacket
->Header
.MessageType
== NvspMessageTypeInitComplete
) ||
887 (nvspPacket
->Header
.MessageType
==
888 NvspMessage1TypeSendReceiveBufferComplete
) ||
889 (nvspPacket
->Header
.MessageType
==
890 NvspMessage1TypeSendSendBufferComplete
)) {
891 /* Copy the response back */
892 memcpy(&netDevice
->ChannelInitPacket
, nvspPacket
,
893 sizeof(struct nvsp_message
));
894 osd_WaitEventSet(netDevice
->ChannelInitEvent
);
895 } else if (nvspPacket
->Header
.MessageType
==
896 NvspMessage1TypeSendRNDISPacketComplete
) {
897 /* Get the send context */
898 nvscPacket
= (struct hv_netvsc_packet
*)(unsigned long)Packet
->TransactionId
;
899 /* ASSERT(nvscPacket); */
901 /* Notify the layer above us */
902 nvscPacket
->Completion
.Send
.OnSendCompletion(nvscPacket
->Completion
.Send
.SendCompletionContext
);
904 atomic_dec(&netDevice
->NumOutstandingSends
);
906 DPRINT_ERR(NETVSC
, "Unknown send completion packet type - "
907 "%d received!!", nvspPacket
->Header
.MessageType
);
910 PutNetDevice(Device
);
914 static int NetVscOnSend(struct hv_device
*Device
,
915 struct hv_netvsc_packet
*Packet
)
917 struct netvsc_device
*netDevice
;
920 struct nvsp_message sendMessage
;
922 netDevice
= GetOutboundNetDevice(Device
);
924 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
925 "ignoring outbound packets", netDevice
);
930 sendMessage
.Header
.MessageType
= NvspMessage1TypeSendRNDISPacket
;
931 if (Packet
->IsDataPacket
) {
933 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 0;
935 /* 1 is RMC_CONTROL; */
936 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 1;
939 /* Not using send buffer section */
940 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionIndex
= 0xFFFFFFFF;
941 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionSize
= 0;
943 if (Packet
->PageBufferCount
) {
944 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacketPageBuffer(
945 Device
, Packet
->PageBuffers
,
946 Packet
->PageBufferCount
,
948 sizeof(struct nvsp_message
),
949 (unsigned long)Packet
);
951 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
953 sizeof(struct nvsp_message
),
954 (unsigned long)Packet
,
955 VmbusPacketTypeDataInBand
,
956 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
961 DPRINT_ERR(NETVSC
, "Unable to send packet %p ret %d",
964 atomic_inc(&netDevice
->NumOutstandingSends
);
965 PutNetDevice(Device
);
971 static void NetVscOnReceive(struct hv_device
*Device
,
972 struct vmpacket_descriptor
*Packet
)
974 struct netvsc_device
*netDevice
;
975 struct vmtransfer_page_packet_header
*vmxferpagePacket
;
976 struct nvsp_message
*nvspPacket
;
977 struct hv_netvsc_packet
*netvscPacket
= NULL
;
979 unsigned long end
, endVirtual
;
980 /* struct netvsc_driver *netvscDriver; */
981 struct xferpage_packet
*xferpagePacket
= NULL
;
983 int count
= 0, bytesRemain
= 0;
987 netDevice
= GetInboundNetDevice(Device
);
989 DPRINT_ERR(NETVSC
, "unable to get net device..."
990 "device being destroyed?");
996 * All inbound packets other than send completion should be xfer page
999 if (Packet
->Type
!= VmbusPacketTypeDataUsingTransferPages
) {
1000 DPRINT_ERR(NETVSC
, "Unknown packet type received - %d",
1002 PutNetDevice(Device
);
1006 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+
1007 (Packet
->DataOffset8
<< 3));
1009 /* Make sure this is a valid nvsp packet */
1010 if (nvspPacket
->Header
.MessageType
!= NvspMessage1TypeSendRNDISPacket
) {
1011 DPRINT_ERR(NETVSC
, "Unknown nvsp packet type received - %d",
1012 nvspPacket
->Header
.MessageType
);
1013 PutNetDevice(Device
);
1017 DPRINT_DBG(NETVSC
, "NVSP packet received - type %d",
1018 nvspPacket
->Header
.MessageType
);
1020 vmxferpagePacket
= (struct vmtransfer_page_packet_header
*)Packet
;
1022 if (vmxferpagePacket
->TransferPageSetId
!= NETVSC_RECEIVE_BUFFER_ID
) {
1023 DPRINT_ERR(NETVSC
, "Invalid xfer page set id - "
1024 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
1025 vmxferpagePacket
->TransferPageSetId
);
1026 PutNetDevice(Device
);
1030 DPRINT_DBG(NETVSC
, "xfer page - range count %d",
1031 vmxferpagePacket
->RangeCount
);
1034 * Grab free packets (range count + 1) to represent this xfer
1035 * page packet. +1 to represent the xfer page packet itself.
1036 * We grab it here so that we know exactly how many we can
1039 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1040 while (!list_empty(&netDevice
->ReceivePacketList
)) {
1041 list_move_tail(netDevice
->ReceivePacketList
.next
, &listHead
);
1042 if (++count
== vmxferpagePacket
->RangeCount
+ 1)
1045 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1048 * We need at least 2 netvsc pkts (1 to represent the xfer
1049 * page and at least 1 for the range) i.e. we can handled
1050 * some of the xfer page packet ranges...
1053 DPRINT_ERR(NETVSC
, "Got only %d netvsc pkt...needed %d pkts. "
1054 "Dropping this xfer page packet completely!",
1055 count
, vmxferpagePacket
->RangeCount
+ 1);
1057 /* Return it to the freelist */
1058 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1059 for (i
= count
; i
!= 0; i
--) {
1060 list_move_tail(listHead
.next
,
1061 &netDevice
->ReceivePacketList
);
1063 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
,
1066 NetVscSendReceiveCompletion(Device
,
1067 vmxferpagePacket
->d
.TransactionId
);
1069 PutNetDevice(Device
);
1073 /* Remove the 1st packet to represent the xfer page packet itself */
1074 xferpagePacket
= (struct xferpage_packet
*)listHead
.next
;
1075 list_del(&xferpagePacket
->ListEntry
);
1077 /* This is how much we can satisfy */
1078 xferpagePacket
->Count
= count
- 1;
1079 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1080 /* vmxferpagePacket->RangeCount); */
1082 if (xferpagePacket
->Count
!= vmxferpagePacket
->RangeCount
) {
1083 DPRINT_INFO(NETVSC
, "Needed %d netvsc pkts to satisy this xfer "
1084 "page...got %d", vmxferpagePacket
->RangeCount
,
1085 xferpagePacket
->Count
);
1088 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1089 for (i
= 0; i
< (count
- 1); i
++) {
1090 netvscPacket
= (struct hv_netvsc_packet
*)listHead
.next
;
1091 list_del(&netvscPacket
->ListEntry
);
1093 /* Initialize the netvsc packet */
1094 netvscPacket
->XferPagePacket
= xferpagePacket
;
1095 netvscPacket
->Completion
.Recv
.OnReceiveCompletion
=
1096 NetVscOnReceiveCompletion
;
1097 netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
=
1099 netvscPacket
->Device
= Device
;
1100 /* Save this so that we can send it back */
1101 netvscPacket
->Completion
.Recv
.ReceiveCompletionTid
=
1102 vmxferpagePacket
->d
.TransactionId
;
1104 netvscPacket
->TotalDataBufferLength
=
1105 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1106 netvscPacket
->PageBufferCount
= 1;
1108 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1109 /* vmxferpagePacket->Ranges[i].ByteCount < */
1110 /* netDevice->ReceiveBufferSize); */
1112 netvscPacket
->PageBuffers
[0].Length
=
1113 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1115 start
= virt_to_phys((void *)((unsigned long)netDevice
->ReceiveBuffer
+ vmxferpagePacket
->Ranges
[i
].ByteOffset
));
1117 netvscPacket
->PageBuffers
[0].Pfn
= start
>> PAGE_SHIFT
;
1118 endVirtual
= (unsigned long)netDevice
->ReceiveBuffer
1119 + vmxferpagePacket
->Ranges
[i
].ByteOffset
1120 + vmxferpagePacket
->Ranges
[i
].ByteCount
- 1;
1121 end
= virt_to_phys((void *)endVirtual
);
1123 /* Calculate the page relative offset */
1124 netvscPacket
->PageBuffers
[0].Offset
=
1125 vmxferpagePacket
->Ranges
[i
].ByteOffset
& (PAGE_SIZE
- 1);
1126 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1127 /* Handle frame across multiple pages: */
1128 netvscPacket
->PageBuffers
[0].Length
=
1129 (netvscPacket
->PageBuffers
[0].Pfn
<< PAGE_SHIFT
)
1130 + PAGE_SIZE
- start
;
1131 bytesRemain
= netvscPacket
->TotalDataBufferLength
-
1132 netvscPacket
->PageBuffers
[0].Length
;
1133 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1134 netvscPacket
->PageBuffers
[j
].Offset
= 0;
1135 if (bytesRemain
<= PAGE_SIZE
) {
1136 netvscPacket
->PageBuffers
[j
].Length
= bytesRemain
;
1139 netvscPacket
->PageBuffers
[j
].Length
= PAGE_SIZE
;
1140 bytesRemain
-= PAGE_SIZE
;
1142 netvscPacket
->PageBuffers
[j
].Pfn
=
1143 virt_to_phys((void *)(endVirtual
- bytesRemain
)) >> PAGE_SHIFT
;
1144 netvscPacket
->PageBufferCount
++;
1145 if (bytesRemain
== 0)
1148 /* ASSERT(bytesRemain == 0); */
1150 DPRINT_DBG(NETVSC
, "[%d] - (abs offset %u len %u) => "
1151 "(pfn %llx, offset %u, len %u)", i
,
1152 vmxferpagePacket
->Ranges
[i
].ByteOffset
,
1153 vmxferpagePacket
->Ranges
[i
].ByteCount
,
1154 netvscPacket
->PageBuffers
[0].Pfn
,
1155 netvscPacket
->PageBuffers
[0].Offset
,
1156 netvscPacket
->PageBuffers
[0].Length
);
1158 /* Pass it to the upper layer */
1159 ((struct netvsc_driver
*)Device
->Driver
)->OnReceiveCallback(Device
, netvscPacket
);
1161 NetVscOnReceiveCompletion(netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
);
1164 /* ASSERT(list_empty(&listHead)); */
1166 PutNetDevice(Device
);
1167 DPRINT_EXIT(NETVSC
);
1170 static void NetVscSendReceiveCompletion(struct hv_device
*Device
,
1173 struct nvsp_message recvcompMessage
;
1177 DPRINT_DBG(NETVSC
, "Sending receive completion pkt - %llx",
1180 recvcompMessage
.Header
.MessageType
=
1181 NvspMessage1TypeSendRNDISPacketComplete
;
1183 /* FIXME: Pass in the status */
1184 recvcompMessage
.Messages
.Version1Messages
.SendRNDISPacketComplete
.Status
= NvspStatusSuccess
;
1187 /* Send the completion */
1188 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
1190 sizeof(struct nvsp_message
),
1192 VmbusPacketTypeCompletion
, 0);
1196 } else if (ret
== -1) {
1197 /* no more room...wait a bit and attempt to retry 3 times */
1199 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt "
1200 "(tid %llx)...retrying %d", TransactionId
, retries
);
1204 goto retry_send_cmplt
;
1206 DPRINT_ERR(NETVSC
, "unable to send receive completion "
1207 "pkt (tid %llx)...give up retrying",
1211 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt - "
1212 "%llx", TransactionId
);
1216 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1217 static void NetVscOnReceiveCompletion(void *Context
)
1219 struct hv_netvsc_packet
*packet
= Context
;
1220 struct hv_device
*device
= (struct hv_device
*)packet
->Device
;
1221 struct netvsc_device
*netDevice
;
1222 u64 transactionId
= 0;
1223 bool fSendReceiveComp
= false;
1224 unsigned long flags
;
1226 /* ASSERT(packet->XferPagePacket); */
1229 * Even though it seems logical to do a GetOutboundNetDevice() here to
1230 * send out receive completion, we are using GetInboundNetDevice()
1231 * since we may have disable outbound traffic already.
1233 netDevice
= GetInboundNetDevice(device
);
1235 DPRINT_ERR(NETVSC
, "unable to get net device..."
1236 "device being destroyed?");
1237 DPRINT_EXIT(NETVSC
);
1241 /* Overloading use of the lock. */
1242 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1244 /* ASSERT(packet->XferPagePacket->Count > 0); */
1245 packet
->XferPagePacket
->Count
--;
1248 * Last one in the line that represent 1 xfer page packet.
1249 * Return the xfer page packet itself to the freelist
1251 if (packet
->XferPagePacket
->Count
== 0) {
1252 fSendReceiveComp
= true;
1253 transactionId
= packet
->Completion
.Recv
.ReceiveCompletionTid
;
1254 list_add_tail(&packet
->XferPagePacket
->ListEntry
,
1255 &netDevice
->ReceivePacketList
);
1259 /* Put the packet back */
1260 list_add_tail(&packet
->ListEntry
, &netDevice
->ReceivePacketList
);
1261 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1263 /* Send a receive completion for the xfer page packet */
1264 if (fSendReceiveComp
)
1265 NetVscSendReceiveCompletion(device
, transactionId
);
1267 PutNetDevice(device
);
1268 DPRINT_EXIT(NETVSC
);
1271 static void NetVscOnChannelCallback(void *Context
)
1274 struct hv_device
*device
= Context
;
1275 struct netvsc_device
*netDevice
;
1278 unsigned char *packet
;
1279 struct vmpacket_descriptor
*desc
;
1280 unsigned char *buffer
;
1281 int bufferlen
= NETVSC_PACKET_SIZE
;
1283 /* ASSERT(device); */
1285 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
1291 netDevice
= GetInboundNetDevice(device
);
1293 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
1294 "ignoring inbound packets", netDevice
);
1295 DPRINT_EXIT(NETVSC
);
1300 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacketRaw(
1301 device
, buffer
, bufferlen
,
1302 &bytesRecvd
, &requestId
);
1304 if (bytesRecvd
> 0) {
1305 DPRINT_DBG(NETVSC
, "receive %d bytes, tid %llx",
1306 bytesRecvd
, requestId
);
1308 desc
= (struct vmpacket_descriptor
*)buffer
;
1309 switch (desc
->Type
) {
1310 case VmbusPacketTypeCompletion
:
1311 NetVscOnSendCompletion(device
, desc
);
1314 case VmbusPacketTypeDataUsingTransferPages
:
1315 NetVscOnReceive(device
, desc
);
1320 "unhandled packet type %d, "
1321 "tid %llx len %d\n",
1322 desc
->Type
, requestId
,
1328 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1331 bufferlen
= NETVSC_PACKET_SIZE
;
1335 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1338 bufferlen
= NETVSC_PACKET_SIZE
;
1343 } else if (ret
== -2) {
1344 /* Handle large packet */
1345 buffer
= kmalloc(bytesRecvd
, GFP_ATOMIC
);
1346 if (buffer
== NULL
) {
1347 /* Try again next time around */
1349 "unable to allocate buffer of size "
1350 "(%d)!!", bytesRecvd
);
1354 bufferlen
= bytesRecvd
;
1358 PutNetDevice(device
);
1359 DPRINT_EXIT(NETVSC
);