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>
28 #include "RndisFilter.h"
32 static const char *gDriverName
= "netvsc";
34 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
35 static const struct hv_guid gNetVscDeviceType
= {
37 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
38 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
42 static int NetVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
);
44 static int NetVscOnDeviceRemove(struct hv_device
*Device
);
46 static void NetVscOnCleanup(struct hv_driver
*Driver
);
48 static void NetVscOnChannelCallback(void *context
);
50 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device
*Device
);
52 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device
*Device
);
54 static int NetVscDestroySendBuffer(struct netvsc_device
*NetDevice
);
56 static int NetVscDestroyReceiveBuffer(struct netvsc_device
*NetDevice
);
58 static int NetVscConnectToVsp(struct hv_device
*Device
);
60 static void NetVscOnSendCompletion(struct hv_device
*Device
,
61 struct vmpacket_descriptor
*Packet
);
63 static int NetVscOnSend(struct hv_device
*Device
,
64 struct hv_netvsc_packet
*Packet
);
66 static void NetVscOnReceive(struct hv_device
*Device
,
67 struct vmpacket_descriptor
*Packet
);
69 static void NetVscOnReceiveCompletion(void *Context
);
71 static void NetVscSendReceiveCompletion(struct hv_device
*Device
,
75 static struct netvsc_device
*AllocNetDevice(struct hv_device
*Device
)
77 struct netvsc_device
*netDevice
;
79 netDevice
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
83 /* Set to 2 to allow both inbound and outbound traffic */
84 atomic_cmpxchg(&netDevice
->RefCount
, 0, 2);
86 netDevice
->Device
= Device
;
87 Device
->Extension
= netDevice
;
92 static void FreeNetDevice(struct netvsc_device
*Device
)
94 ASSERT(atomic_read(&Device
->RefCount
) == 0);
95 Device
->Device
->Extension
= NULL
;
100 /* Get the net device object iff exists and its refcount > 1 */
101 static struct netvsc_device
*GetOutboundNetDevice(struct hv_device
*Device
)
103 struct netvsc_device
*netDevice
;
105 netDevice
= Device
->Extension
;
106 if (netDevice
&& atomic_read(&netDevice
->RefCount
) > 1)
107 atomic_inc(&netDevice
->RefCount
);
114 /* Get the net device object iff exists and its refcount > 0 */
115 static struct netvsc_device
*GetInboundNetDevice(struct hv_device
*Device
)
117 struct netvsc_device
*netDevice
;
119 netDevice
= Device
->Extension
;
120 if (netDevice
&& atomic_read(&netDevice
->RefCount
))
121 atomic_inc(&netDevice
->RefCount
);
128 static void PutNetDevice(struct hv_device
*Device
)
130 struct netvsc_device
*netDevice
;
132 netDevice
= Device
->Extension
;
135 atomic_dec(&netDevice
->RefCount
);
138 static struct netvsc_device
*ReleaseOutboundNetDevice(struct hv_device
*Device
)
140 struct netvsc_device
*netDevice
;
142 netDevice
= Device
->Extension
;
143 if (netDevice
== NULL
)
146 /* Busy wait until the ref drop to 2, then set it to 1 */
147 while (atomic_cmpxchg(&netDevice
->RefCount
, 2, 1) != 2)
153 static struct netvsc_device
*ReleaseInboundNetDevice(struct hv_device
*Device
)
155 struct netvsc_device
*netDevice
;
157 netDevice
= Device
->Extension
;
158 if (netDevice
== NULL
)
161 /* Busy wait until the ref drop to 1, then set it to 0 */
162 while (atomic_cmpxchg(&netDevice
->RefCount
, 1, 0) != 1)
165 Device
->Extension
= NULL
;
170 * NetVscInitialize - Main entry point
172 int NetVscInitialize(struct hv_driver
*drv
)
174 struct netvsc_driver
*driver
= (struct netvsc_driver
*)drv
;
176 DPRINT_ENTER(NETVSC
);
178 DPRINT_DBG(NETVSC
, "sizeof(struct hv_netvsc_packet)=%zd, "
179 "sizeof(struct nvsp_message)=%zd, "
180 "sizeof(struct vmtransfer_page_packet_header)=%zd",
181 sizeof(struct hv_netvsc_packet
),
182 sizeof(struct nvsp_message
),
183 sizeof(struct vmtransfer_page_packet_header
));
185 /* Make sure we are at least 2 pages since 1 page is used for control */
186 ASSERT(driver
->RingBufferSize
>= (PAGE_SIZE
<< 1));
188 drv
->name
= gDriverName
;
189 memcpy(&drv
->deviceType
, &gNetVscDeviceType
, sizeof(struct hv_guid
));
191 /* Make sure it is set by the caller */
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 DPRINT_ENTER(NETVSC
);
217 netDevice
= GetOutboundNetDevice(Device
);
219 DPRINT_ERR(NETVSC
, "unable to get net device..."
220 "device being destroyed?");
224 ASSERT(netDevice
->ReceiveBufferSize
> 0);
225 /* page-size grandularity */
226 ASSERT((netDevice
->ReceiveBufferSize
& (PAGE_SIZE
- 1)) == 0);
228 netDevice
->ReceiveBuffer
=
229 osd_PageAlloc(netDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
230 if (!netDevice
->ReceiveBuffer
) {
232 "unable to allocate receive buffer of size %d",
233 netDevice
->ReceiveBufferSize
);
237 /* page-aligned buffer */
238 ASSERT(((unsigned long)netDevice
->ReceiveBuffer
& (PAGE_SIZE
- 1)) ==
241 DPRINT_INFO(NETVSC
, "Establishing receive buffer's GPADL...");
244 * Establish the gpadl handle for this buffer on this
245 * channel. Note: This call uses the vmbus connection rather
246 * than the channel to establish the gpadl handle.
248 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
249 netDevice
->ReceiveBuffer
,
250 netDevice
->ReceiveBufferSize
,
251 &netDevice
->ReceiveBufferGpadlHandle
);
254 "unable to establish receive buffer's gpadl");
258 /* osd_WaitEventWait(ext->ChannelInitEvent); */
260 /* Notify the NetVsp of the gpadl handle */
261 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendReceiveBuffer...");
263 initPacket
= &netDevice
->ChannelInitPacket
;
265 memset(initPacket
, 0, sizeof(struct nvsp_message
));
267 initPacket
->Header
.MessageType
= NvspMessage1TypeSendReceiveBuffer
;
268 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->ReceiveBufferGpadlHandle
;
269 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
271 /* Send the gpadl notification request */
272 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
274 sizeof(struct nvsp_message
),
275 (unsigned long)initPacket
,
276 VmbusPacketTypeDataInBand
,
277 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
280 "unable to send receive buffer's gpadl to netvsp");
284 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
286 /* Check the response */
287 if (initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
!= NvspStatusSuccess
) {
288 DPRINT_ERR(NETVSC
, "Unable to complete receive buffer "
289 "initialzation with NetVsp - status %d",
290 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
);
295 /* Parse the response */
296 ASSERT(netDevice
->ReceiveSectionCount
== 0);
297 ASSERT(netDevice
->ReceiveSections
== NULL
);
299 netDevice
->ReceiveSectionCount
= initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.NumSections
;
301 netDevice
->ReceiveSections
= kmalloc(netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
302 if (netDevice
->ReceiveSections
== NULL
) {
307 memcpy(netDevice
->ReceiveSections
,
308 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Sections
,
309 netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
));
311 DPRINT_INFO(NETVSC
, "Receive sections info (count %d, offset %d, "
312 "endoffset %d, suballoc size %d, num suballocs %d)",
313 netDevice
->ReceiveSectionCount
,
314 netDevice
->ReceiveSections
[0].Offset
,
315 netDevice
->ReceiveSections
[0].EndOffset
,
316 netDevice
->ReceiveSections
[0].SubAllocationSize
,
317 netDevice
->ReceiveSections
[0].NumSubAllocations
);
320 * For 1st release, there should only be 1 section that represents the
321 * entire receive buffer
323 if (netDevice
->ReceiveSectionCount
!= 1 ||
324 netDevice
->ReceiveSections
->Offset
!= 0) {
332 NetVscDestroyReceiveBuffer(netDevice
);
335 PutNetDevice(Device
);
340 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device
*Device
)
343 struct netvsc_device
*netDevice
;
344 struct nvsp_message
*initPacket
;
346 DPRINT_ENTER(NETVSC
);
348 netDevice
= GetOutboundNetDevice(Device
);
350 DPRINT_ERR(NETVSC
, "unable to get net device..."
351 "device being destroyed?");
355 ASSERT(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
;
440 DPRINT_ENTER(NETVSC
);
443 * If we got a section count, it means we received a
444 * SendReceiveBufferComplete msg (ie sent
445 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
446 * to send a revoke msg here
448 if (NetDevice
->ReceiveSectionCount
) {
450 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
452 /* Send the revoke receive buffer */
453 revokePacket
= &NetDevice
->RevokePacket
;
454 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
456 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeReceiveBuffer
;
457 revokePacket
->Messages
.Version1Messages
.RevokeReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
459 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(
462 sizeof(struct nvsp_message
),
463 (unsigned long)revokePacket
,
464 VmbusPacketTypeDataInBand
, 0);
466 * If we failed here, we might as well return and
467 * have a leak rather than continue and a bugchk
470 DPRINT_ERR(NETVSC
, "unable to send revoke receive "
477 /* Teardown the gpadl on the vsp end */
478 if (NetDevice
->ReceiveBufferGpadlHandle
) {
479 DPRINT_INFO(NETVSC
, "Tearing down receive buffer's GPADL...");
481 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(
483 NetDevice
->ReceiveBufferGpadlHandle
);
485 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
488 "unable to teardown receive buffer's gpadl");
492 NetDevice
->ReceiveBufferGpadlHandle
= 0;
495 if (NetDevice
->ReceiveBuffer
) {
496 DPRINT_INFO(NETVSC
, "Freeing up receive buffer...");
498 /* Free up the receive buffer */
499 osd_PageFree(NetDevice
->ReceiveBuffer
,
500 NetDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
501 NetDevice
->ReceiveBuffer
= NULL
;
504 if (NetDevice
->ReceiveSections
) {
505 NetDevice
->ReceiveSectionCount
= 0;
506 kfree(NetDevice
->ReceiveSections
);
507 NetDevice
->ReceiveSections
= NULL
;
515 static int NetVscDestroySendBuffer(struct netvsc_device
*NetDevice
)
517 struct nvsp_message
*revokePacket
;
520 DPRINT_ENTER(NETVSC
);
523 * If we got a section count, it means we received a
524 * SendReceiveBufferComplete msg (ie sent
525 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
526 * to send a revoke msg here
528 if (NetDevice
->SendSectionSize
) {
530 "Sending NvspMessage1TypeRevokeSendBuffer...");
532 /* Send the revoke send buffer */
533 revokePacket
= &NetDevice
->RevokePacket
;
534 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
536 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeSendBuffer
;
537 revokePacket
->Messages
.Version1Messages
.RevokeSendBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
539 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(NetDevice
->Device
,
541 sizeof(struct nvsp_message
),
542 (unsigned long)revokePacket
,
543 VmbusPacketTypeDataInBand
, 0);
545 * If we failed here, we might as well return and have a leak
546 * rather than continue and a bugchk
549 DPRINT_ERR(NETVSC
, "unable to send revoke send buffer "
556 /* Teardown the gpadl on the vsp end */
557 if (NetDevice
->SendBufferGpadlHandle
) {
558 DPRINT_INFO(NETVSC
, "Tearing down send buffer's GPADL...");
560 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(NetDevice
->Device
, NetDevice
->SendBufferGpadlHandle
);
563 * If we failed here, we might as well return and have a leak
564 * rather than continue and a bugchk
567 DPRINT_ERR(NETVSC
, "unable to teardown send buffer's "
572 NetDevice
->SendBufferGpadlHandle
= 0;
575 if (NetDevice
->SendBuffer
) {
576 DPRINT_INFO(NETVSC
, "Freeing up send buffer...");
578 /* Free up the receive buffer */
579 osd_PageFree(NetDevice
->SendBuffer
,
580 NetDevice
->SendBufferSize
>> PAGE_SHIFT
);
581 NetDevice
->SendBuffer
= NULL
;
590 static int NetVscConnectToVsp(struct hv_device
*Device
)
593 struct netvsc_device
*netDevice
;
594 struct nvsp_message
*initPacket
;
597 DPRINT_ENTER(NETVSC
);
599 netDevice
= GetOutboundNetDevice(Device
);
601 DPRINT_ERR(NETVSC
, "unable to get net device..."
602 "device being destroyed?");
607 initPacket
= &netDevice
->ChannelInitPacket
;
609 memset(initPacket
, 0, sizeof(struct nvsp_message
));
610 initPacket
->Header
.MessageType
= NvspMessageTypeInit
;
611 initPacket
->Messages
.InitMessages
.Init
.MinProtocolVersion
= NVSP_MIN_PROTOCOL_VERSION
;
612 initPacket
->Messages
.InitMessages
.Init
.MaxProtocolVersion
= NVSP_MAX_PROTOCOL_VERSION
;
614 DPRINT_INFO(NETVSC
, "Sending NvspMessageTypeInit...");
616 /* Send the init request */
617 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
619 sizeof(struct nvsp_message
),
620 (unsigned long)initPacket
,
621 VmbusPacketTypeDataInBand
,
622 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
625 DPRINT_ERR(NETVSC
, "unable to send NvspMessageTypeInit");
629 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
631 /* Now, check the response */
632 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
633 DPRINT_INFO(NETVSC
, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
634 initPacket
->Messages
.InitMessages
.InitComplete
.Status
,
635 initPacket
->Messages
.InitMessages
.InitComplete
.MaximumMdlChainLength
);
637 if (initPacket
->Messages
.InitMessages
.InitComplete
.Status
!=
640 "unable to initialize with netvsp (status 0x%x)",
641 initPacket
->Messages
.InitMessages
.InitComplete
.Status
);
646 if (initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
!= NVSP_PROTOCOL_VERSION_1
) {
647 DPRINT_ERR(NETVSC
, "unable to initialize with netvsp "
648 "(version expected 1 got %d)",
649 initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
);
653 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendNdisVersion...");
655 /* Send the ndis version */
656 memset(initPacket
, 0, sizeof(struct nvsp_message
));
658 ndisVersion
= 0x00050000;
660 initPacket
->Header
.MessageType
= NvspMessage1TypeSendNdisVersion
;
661 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMajorVersion
=
662 (ndisVersion
& 0xFFFF0000) >> 16;
663 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMinorVersion
=
664 ndisVersion
& 0xFFFF;
666 /* Send the init request */
667 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
669 sizeof(struct nvsp_message
),
670 (unsigned long)initPacket
,
671 VmbusPacketTypeDataInBand
, 0);
674 "unable to send NvspMessage1TypeSendNdisVersion");
679 * BUGBUG - We have to wait for the above msg since the
680 * netvsp uses KMCL which acknowledges packet (completion
681 * packet) since our Vmbus always set the
682 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
684 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
686 /* Post the big receive buffer to NetVSP */
687 ret
= NetVscInitializeReceiveBufferWithNetVsp(Device
);
689 ret
= NetVscInitializeSendBufferWithNetVsp(Device
);
692 PutNetDevice(Device
);
697 static void NetVscDisconnectFromVsp(struct netvsc_device
*NetDevice
)
699 DPRINT_ENTER(NETVSC
);
701 NetVscDestroyReceiveBuffer(NetDevice
);
702 NetVscDestroySendBuffer(NetDevice
);
708 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
710 static int NetVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
714 struct netvsc_device
*netDevice
;
715 struct hv_netvsc_packet
*packet
, *pos
;
716 struct netvsc_driver
*netDriver
=
717 (struct netvsc_driver
*)Device
->Driver
;
719 DPRINT_ENTER(NETVSC
);
721 netDevice
= AllocNetDevice(Device
);
727 DPRINT_DBG(NETVSC
, "netvsc channel object allocated - %p", netDevice
);
729 /* Initialize the NetVSC channel extension */
730 netDevice
->ReceiveBufferSize
= NETVSC_RECEIVE_BUFFER_SIZE
;
731 spin_lock_init(&netDevice
->receive_packet_list_lock
);
733 netDevice
->SendBufferSize
= NETVSC_SEND_BUFFER_SIZE
;
735 INIT_LIST_HEAD(&netDevice
->ReceivePacketList
);
737 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
738 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
739 (NETVSC_RECEIVE_SG_COUNT
*
740 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
742 DPRINT_DBG(NETVSC
, "unable to allocate netvsc pkts "
743 "for receive pool (wanted %d got %d)",
744 NETVSC_RECEIVE_PACKETLIST_COUNT
, i
);
747 list_add_tail(&packet
->ListEntry
,
748 &netDevice
->ReceivePacketList
);
750 netDevice
->ChannelInitEvent
= osd_WaitEventCreate();
752 /* Open the channel */
753 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
754 netDriver
->RingBufferSize
,
755 netDriver
->RingBufferSize
,
757 NetVscOnChannelCallback
,
761 DPRINT_ERR(NETVSC
, "unable to open channel: %d", ret
);
766 /* Channel is opened */
767 DPRINT_INFO(NETVSC
, "*** NetVSC channel opened successfully! ***");
769 /* Connect with the NetVsp */
770 ret
= NetVscConnectToVsp(Device
);
772 DPRINT_ERR(NETVSC
, "unable to connect to NetVSP - %d", ret
);
777 DPRINT_INFO(NETVSC
, "*** NetVSC channel handshake result - %d ***",
784 /* Now, we can close the channel safely */
785 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
790 kfree(netDevice
->ChannelInitEvent
);
792 list_for_each_entry_safe(packet
, pos
,
793 &netDevice
->ReceivePacketList
,
795 list_del(&packet
->ListEntry
);
799 ReleaseOutboundNetDevice(Device
);
800 ReleaseInboundNetDevice(Device
);
802 FreeNetDevice(netDevice
);
810 * NetVscOnDeviceRemove - Callback when the root bus device is removed
812 static int NetVscOnDeviceRemove(struct hv_device
*Device
)
814 struct netvsc_device
*netDevice
;
815 struct hv_netvsc_packet
*netvscPacket
, *pos
;
817 DPRINT_ENTER(NETVSC
);
819 DPRINT_INFO(NETVSC
, "Disabling outbound traffic on net device (%p)...",
822 /* Stop outbound traffic ie sends and receives completions */
823 netDevice
= ReleaseOutboundNetDevice(Device
);
825 DPRINT_ERR(NETVSC
, "No net device present!!");
829 /* Wait for all send completions */
830 while (atomic_read(&netDevice
->NumOutstandingSends
)) {
831 DPRINT_INFO(NETVSC
, "waiting for %d requests to complete...",
832 atomic_read(&netDevice
->NumOutstandingSends
));
836 DPRINT_INFO(NETVSC
, "Disconnecting from netvsp...");
838 NetVscDisconnectFromVsp(netDevice
);
840 DPRINT_INFO(NETVSC
, "Disabling inbound traffic on net device (%p)...",
843 /* Stop inbound traffic ie receives and sends completions */
844 netDevice
= ReleaseInboundNetDevice(Device
);
846 /* At this point, no one should be accessing netDevice except in here */
847 DPRINT_INFO(NETVSC
, "net device (%p) safe to remove", netDevice
);
849 /* Now, we can close the channel safely */
850 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
852 /* Release all resources */
853 list_for_each_entry_safe(netvscPacket
, pos
,
854 &netDevice
->ReceivePacketList
, ListEntry
) {
855 list_del(&netvscPacket
->ListEntry
);
859 kfree(netDevice
->ChannelInitEvent
);
860 FreeNetDevice(netDevice
);
867 * NetVscOnCleanup - Perform any cleanup when the driver is removed
869 static void NetVscOnCleanup(struct hv_driver
*drv
)
871 DPRINT_ENTER(NETVSC
);
875 static void NetVscOnSendCompletion(struct hv_device
*Device
,
876 struct vmpacket_descriptor
*Packet
)
878 struct netvsc_device
*netDevice
;
879 struct nvsp_message
*nvspPacket
;
880 struct hv_netvsc_packet
*nvscPacket
;
882 DPRINT_ENTER(NETVSC
);
884 netDevice
= GetInboundNetDevice(Device
);
886 DPRINT_ERR(NETVSC
, "unable to get net device..."
887 "device being destroyed?");
892 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+ (Packet
->DataOffset8
<< 3));
894 DPRINT_DBG(NETVSC
, "send completion packet - type %d",
895 nvspPacket
->Header
.MessageType
);
897 if ((nvspPacket
->Header
.MessageType
== NvspMessageTypeInitComplete
) ||
898 (nvspPacket
->Header
.MessageType
==
899 NvspMessage1TypeSendReceiveBufferComplete
) ||
900 (nvspPacket
->Header
.MessageType
==
901 NvspMessage1TypeSendSendBufferComplete
)) {
902 /* Copy the response back */
903 memcpy(&netDevice
->ChannelInitPacket
, nvspPacket
,
904 sizeof(struct nvsp_message
));
905 osd_WaitEventSet(netDevice
->ChannelInitEvent
);
906 } else if (nvspPacket
->Header
.MessageType
==
907 NvspMessage1TypeSendRNDISPacketComplete
) {
908 /* Get the send context */
909 nvscPacket
= (struct hv_netvsc_packet
*)(unsigned long)Packet
->TransactionId
;
912 /* Notify the layer above us */
913 nvscPacket
->Completion
.Send
.OnSendCompletion(nvscPacket
->Completion
.Send
.SendCompletionContext
);
915 atomic_dec(&netDevice
->NumOutstandingSends
);
917 DPRINT_ERR(NETVSC
, "Unknown send completion packet type - "
918 "%d received!!", nvspPacket
->Header
.MessageType
);
921 PutNetDevice(Device
);
925 static int NetVscOnSend(struct hv_device
*Device
,
926 struct hv_netvsc_packet
*Packet
)
928 struct netvsc_device
*netDevice
;
931 struct nvsp_message sendMessage
;
933 DPRINT_ENTER(NETVSC
);
935 netDevice
= GetOutboundNetDevice(Device
);
937 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
938 "ignoring outbound packets", netDevice
);
943 sendMessage
.Header
.MessageType
= NvspMessage1TypeSendRNDISPacket
;
944 if (Packet
->IsDataPacket
) {
946 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 0;
948 /* 1 is RMC_CONTROL; */
949 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 1;
952 /* Not using send buffer section */
953 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionIndex
= 0xFFFFFFFF;
954 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionSize
= 0;
956 if (Packet
->PageBufferCount
) {
957 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacketPageBuffer(
958 Device
, Packet
->PageBuffers
,
959 Packet
->PageBufferCount
,
961 sizeof(struct nvsp_message
),
962 (unsigned long)Packet
);
964 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
966 sizeof(struct nvsp_message
),
967 (unsigned long)Packet
,
968 VmbusPacketTypeDataInBand
,
969 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
974 DPRINT_ERR(NETVSC
, "Unable to send packet %p ret %d",
977 atomic_inc(&netDevice
->NumOutstandingSends
);
978 PutNetDevice(Device
);
984 static void NetVscOnReceive(struct hv_device
*Device
,
985 struct vmpacket_descriptor
*Packet
)
987 struct netvsc_device
*netDevice
;
988 struct vmtransfer_page_packet_header
*vmxferpagePacket
;
989 struct nvsp_message
*nvspPacket
;
990 struct hv_netvsc_packet
*netvscPacket
= NULL
;
992 unsigned long end
, endVirtual
;
993 /* struct netvsc_driver *netvscDriver; */
994 struct xferpage_packet
*xferpagePacket
= NULL
;
996 int count
= 0, bytesRemain
= 0;
1000 DPRINT_ENTER(NETVSC
);
1002 netDevice
= GetInboundNetDevice(Device
);
1004 DPRINT_ERR(NETVSC
, "unable to get net device..."
1005 "device being destroyed?");
1006 DPRINT_EXIT(NETVSC
);
1011 * All inbound packets other than send completion should be xfer page
1014 if (Packet
->Type
!= VmbusPacketTypeDataUsingTransferPages
) {
1015 DPRINT_ERR(NETVSC
, "Unknown packet type received - %d",
1017 PutNetDevice(Device
);
1021 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+
1022 (Packet
->DataOffset8
<< 3));
1024 /* Make sure this is a valid nvsp packet */
1025 if (nvspPacket
->Header
.MessageType
!= NvspMessage1TypeSendRNDISPacket
) {
1026 DPRINT_ERR(NETVSC
, "Unknown nvsp packet type received - %d",
1027 nvspPacket
->Header
.MessageType
);
1028 PutNetDevice(Device
);
1032 DPRINT_DBG(NETVSC
, "NVSP packet received - type %d",
1033 nvspPacket
->Header
.MessageType
);
1035 vmxferpagePacket
= (struct vmtransfer_page_packet_header
*)Packet
;
1037 if (vmxferpagePacket
->TransferPageSetId
!= NETVSC_RECEIVE_BUFFER_ID
) {
1038 DPRINT_ERR(NETVSC
, "Invalid xfer page set id - "
1039 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
1040 vmxferpagePacket
->TransferPageSetId
);
1041 PutNetDevice(Device
);
1045 DPRINT_DBG(NETVSC
, "xfer page - range count %d",
1046 vmxferpagePacket
->RangeCount
);
1049 * Grab free packets (range count + 1) to represent this xfer
1050 * page packet. +1 to represent the xfer page packet itself.
1051 * We grab it here so that we know exactly how many we can
1054 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1055 while (!list_empty(&netDevice
->ReceivePacketList
)) {
1056 list_move_tail(netDevice
->ReceivePacketList
.next
, &listHead
);
1057 if (++count
== vmxferpagePacket
->RangeCount
+ 1)
1060 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1063 * We need at least 2 netvsc pkts (1 to represent the xfer
1064 * page and at least 1 for the range) i.e. we can handled
1065 * some of the xfer page packet ranges...
1068 DPRINT_ERR(NETVSC
, "Got only %d netvsc pkt...needed %d pkts. "
1069 "Dropping this xfer page packet completely!",
1070 count
, vmxferpagePacket
->RangeCount
+ 1);
1072 /* Return it to the freelist */
1073 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1074 for (i
= count
; i
!= 0; i
--) {
1075 list_move_tail(listHead
.next
,
1076 &netDevice
->ReceivePacketList
);
1078 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
,
1081 NetVscSendReceiveCompletion(Device
,
1082 vmxferpagePacket
->d
.TransactionId
);
1084 PutNetDevice(Device
);
1088 /* Remove the 1st packet to represent the xfer page packet itself */
1089 xferpagePacket
= (struct xferpage_packet
*)listHead
.next
;
1090 list_del(&xferpagePacket
->ListEntry
);
1092 /* This is how much we can satisfy */
1093 xferpagePacket
->Count
= count
- 1;
1094 ASSERT(xferpagePacket
->Count
> 0 && xferpagePacket
->Count
<=
1095 vmxferpagePacket
->RangeCount
);
1097 if (xferpagePacket
->Count
!= vmxferpagePacket
->RangeCount
) {
1098 DPRINT_INFO(NETVSC
, "Needed %d netvsc pkts to satisy this xfer "
1099 "page...got %d", vmxferpagePacket
->RangeCount
,
1100 xferpagePacket
->Count
);
1103 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1104 for (i
= 0; i
< (count
- 1); i
++) {
1105 netvscPacket
= (struct hv_netvsc_packet
*)listHead
.next
;
1106 list_del(&netvscPacket
->ListEntry
);
1108 /* Initialize the netvsc packet */
1109 netvscPacket
->XferPagePacket
= xferpagePacket
;
1110 netvscPacket
->Completion
.Recv
.OnReceiveCompletion
=
1111 NetVscOnReceiveCompletion
;
1112 netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
=
1114 netvscPacket
->Device
= Device
;
1115 /* Save this so that we can send it back */
1116 netvscPacket
->Completion
.Recv
.ReceiveCompletionTid
=
1117 vmxferpagePacket
->d
.TransactionId
;
1119 netvscPacket
->TotalDataBufferLength
=
1120 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1121 netvscPacket
->PageBufferCount
= 1;
1123 ASSERT(vmxferpagePacket
->Ranges
[i
].ByteOffset
+
1124 vmxferpagePacket
->Ranges
[i
].ByteCount
<
1125 netDevice
->ReceiveBufferSize
);
1127 netvscPacket
->PageBuffers
[0].Length
=
1128 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1130 start
= virt_to_phys((void *)((unsigned long)netDevice
->ReceiveBuffer
+ vmxferpagePacket
->Ranges
[i
].ByteOffset
));
1132 netvscPacket
->PageBuffers
[0].Pfn
= start
>> PAGE_SHIFT
;
1133 endVirtual
= (unsigned long)netDevice
->ReceiveBuffer
1134 + vmxferpagePacket
->Ranges
[i
].ByteOffset
1135 + vmxferpagePacket
->Ranges
[i
].ByteCount
- 1;
1136 end
= virt_to_phys((void *)endVirtual
);
1138 /* Calculate the page relative offset */
1139 netvscPacket
->PageBuffers
[0].Offset
=
1140 vmxferpagePacket
->Ranges
[i
].ByteOffset
& (PAGE_SIZE
- 1);
1141 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1142 /* Handle frame across multiple pages: */
1143 netvscPacket
->PageBuffers
[0].Length
=
1144 (netvscPacket
->PageBuffers
[0].Pfn
<< PAGE_SHIFT
)
1145 + PAGE_SIZE
- start
;
1146 bytesRemain
= netvscPacket
->TotalDataBufferLength
-
1147 netvscPacket
->PageBuffers
[0].Length
;
1148 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1149 netvscPacket
->PageBuffers
[j
].Offset
= 0;
1150 if (bytesRemain
<= PAGE_SIZE
) {
1151 netvscPacket
->PageBuffers
[j
].Length
= bytesRemain
;
1154 netvscPacket
->PageBuffers
[j
].Length
= PAGE_SIZE
;
1155 bytesRemain
-= PAGE_SIZE
;
1157 netvscPacket
->PageBuffers
[j
].Pfn
=
1158 virt_to_phys((void *)(endVirtual
- bytesRemain
)) >> PAGE_SHIFT
;
1159 netvscPacket
->PageBufferCount
++;
1160 if (bytesRemain
== 0)
1163 ASSERT(bytesRemain
== 0);
1165 DPRINT_DBG(NETVSC
, "[%d] - (abs offset %u len %u) => "
1166 "(pfn %llx, offset %u, len %u)", i
,
1167 vmxferpagePacket
->Ranges
[i
].ByteOffset
,
1168 vmxferpagePacket
->Ranges
[i
].ByteCount
,
1169 netvscPacket
->PageBuffers
[0].Pfn
,
1170 netvscPacket
->PageBuffers
[0].Offset
,
1171 netvscPacket
->PageBuffers
[0].Length
);
1173 /* Pass it to the upper layer */
1174 ((struct netvsc_driver
*)Device
->Driver
)->OnReceiveCallback(Device
, netvscPacket
);
1176 NetVscOnReceiveCompletion(netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
);
1179 ASSERT(list_empty(&listHead
));
1181 PutNetDevice(Device
);
1182 DPRINT_EXIT(NETVSC
);
1185 static void NetVscSendReceiveCompletion(struct hv_device
*Device
,
1188 struct nvsp_message recvcompMessage
;
1192 DPRINT_DBG(NETVSC
, "Sending receive completion pkt - %llx",
1195 recvcompMessage
.Header
.MessageType
=
1196 NvspMessage1TypeSendRNDISPacketComplete
;
1198 /* FIXME: Pass in the status */
1199 recvcompMessage
.Messages
.Version1Messages
.SendRNDISPacketComplete
.Status
= NvspStatusSuccess
;
1202 /* Send the completion */
1203 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
1205 sizeof(struct nvsp_message
),
1207 VmbusPacketTypeCompletion
, 0);
1211 } else if (ret
== -1) {
1212 /* no more room...wait a bit and attempt to retry 3 times */
1214 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt "
1215 "(tid %llx)...retrying %d", TransactionId
, retries
);
1219 goto retry_send_cmplt
;
1221 DPRINT_ERR(NETVSC
, "unable to send receive completion "
1222 "pkt (tid %llx)...give up retrying",
1226 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt - "
1227 "%llx", TransactionId
);
1231 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1232 static void NetVscOnReceiveCompletion(void *Context
)
1234 struct hv_netvsc_packet
*packet
= Context
;
1235 struct hv_device
*device
= (struct hv_device
*)packet
->Device
;
1236 struct netvsc_device
*netDevice
;
1237 u64 transactionId
= 0;
1238 bool fSendReceiveComp
= false;
1239 unsigned long flags
;
1241 DPRINT_ENTER(NETVSC
);
1243 ASSERT(packet
->XferPagePacket
);
1246 * Even though it seems logical to do a GetOutboundNetDevice() here to
1247 * send out receive completion, we are using GetInboundNetDevice()
1248 * since we may have disable outbound traffic already.
1250 netDevice
= GetInboundNetDevice(device
);
1252 DPRINT_ERR(NETVSC
, "unable to get net device..."
1253 "device being destroyed?");
1254 DPRINT_EXIT(NETVSC
);
1258 /* Overloading use of the lock. */
1259 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1261 ASSERT(packet
->XferPagePacket
->Count
> 0);
1262 packet
->XferPagePacket
->Count
--;
1265 * Last one in the line that represent 1 xfer page packet.
1266 * Return the xfer page packet itself to the freelist
1268 if (packet
->XferPagePacket
->Count
== 0) {
1269 fSendReceiveComp
= true;
1270 transactionId
= packet
->Completion
.Recv
.ReceiveCompletionTid
;
1271 list_add_tail(&packet
->XferPagePacket
->ListEntry
,
1272 &netDevice
->ReceivePacketList
);
1276 /* Put the packet back */
1277 list_add_tail(&packet
->ListEntry
, &netDevice
->ReceivePacketList
);
1278 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1280 /* Send a receive completion for the xfer page packet */
1281 if (fSendReceiveComp
)
1282 NetVscSendReceiveCompletion(device
, transactionId
);
1284 PutNetDevice(device
);
1285 DPRINT_EXIT(NETVSC
);
1288 void NetVscOnChannelCallback(void *Context
)
1290 const int netPacketSize
= 2048;
1292 struct hv_device
*device
= Context
;
1293 struct netvsc_device
*netDevice
;
1296 unsigned char packet
[netPacketSize
];
1297 struct vmpacket_descriptor
*desc
;
1298 unsigned char *buffer
= packet
;
1299 int bufferlen
= netPacketSize
;
1302 DPRINT_ENTER(NETVSC
);
1306 netDevice
= GetInboundNetDevice(device
);
1308 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
1309 "ignoring inbound packets", netDevice
);
1310 DPRINT_EXIT(NETVSC
);
1315 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacketRaw(
1316 device
, buffer
, bufferlen
,
1317 &bytesRecvd
, &requestId
);
1319 if (bytesRecvd
> 0) {
1320 DPRINT_DBG(NETVSC
, "receive %d bytes, tid %llx",
1321 bytesRecvd
, requestId
);
1323 desc
= (struct vmpacket_descriptor
*)buffer
;
1324 switch (desc
->Type
) {
1325 case VmbusPacketTypeCompletion
:
1326 NetVscOnSendCompletion(device
, desc
);
1329 case VmbusPacketTypeDataUsingTransferPages
:
1330 NetVscOnReceive(device
, desc
);
1335 "unhandled packet type %d, "
1336 "tid %llx len %d\n",
1337 desc
->Type
, requestId
,
1343 if (bufferlen
> netPacketSize
) {
1346 bufferlen
= netPacketSize
;
1350 if (bufferlen
> netPacketSize
) {
1353 bufferlen
= netPacketSize
;
1358 } else if (ret
== -2) {
1359 /* Handle large packet */
1360 buffer
= kmalloc(bytesRecvd
, GFP_ATOMIC
);
1361 if (buffer
== NULL
) {
1362 /* Try again next time around */
1364 "unable to allocate buffer of size "
1365 "(%d)!!", bytesRecvd
);
1369 bufferlen
= bytesRecvd
;
1375 PutNetDevice(device
);
1376 DPRINT_EXIT(NETVSC
);