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_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 /* FIXME: These probably should still be tested in some way */
194 /* ASSERT(driver->OnReceiveCallback); */
195 /* ASSERT(driver->OnLinkStatusChanged); */
197 /* Setup the dispatch table */
198 driver
->Base
.OnDeviceAdd
= NetVscOnDeviceAdd
;
199 driver
->Base
.OnDeviceRemove
= NetVscOnDeviceRemove
;
200 driver
->Base
.OnCleanup
= NetVscOnCleanup
;
202 driver
->OnSend
= NetVscOnSend
;
204 RndisFilterInit(driver
);
211 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device
*Device
)
214 struct netvsc_device
*netDevice
;
215 struct nvsp_message
*initPacket
;
217 DPRINT_ENTER(NETVSC
);
219 netDevice
= GetOutboundNetDevice(Device
);
221 DPRINT_ERR(NETVSC
, "unable to get net device..."
222 "device being destroyed?");
226 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
227 /* page-size grandularity */
228 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
230 netDevice
->ReceiveBuffer
=
231 osd_PageAlloc(netDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
232 if (!netDevice
->ReceiveBuffer
) {
234 "unable to allocate receive buffer of size %d",
235 netDevice
->ReceiveBufferSize
);
239 /* page-aligned buffer */
240 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
243 DPRINT_INFO(NETVSC
, "Establishing receive buffer's GPADL...");
246 * Establish the gpadl handle for this buffer on this
247 * channel. Note: This call uses the vmbus connection rather
248 * than the channel to establish the gpadl handle.
250 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
251 netDevice
->ReceiveBuffer
,
252 netDevice
->ReceiveBufferSize
,
253 &netDevice
->ReceiveBufferGpadlHandle
);
256 "unable to establish receive buffer's gpadl");
260 /* osd_WaitEventWait(ext->ChannelInitEvent); */
262 /* Notify the NetVsp of the gpadl handle */
263 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendReceiveBuffer...");
265 initPacket
= &netDevice
->ChannelInitPacket
;
267 memset(initPacket
, 0, sizeof(struct nvsp_message
));
269 initPacket
->Header
.MessageType
= NvspMessage1TypeSendReceiveBuffer
;
270 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->ReceiveBufferGpadlHandle
;
271 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
273 /* Send the gpadl notification request */
274 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
276 sizeof(struct nvsp_message
),
277 (unsigned long)initPacket
,
278 VmbusPacketTypeDataInBand
,
279 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
282 "unable to send receive buffer's gpadl to netvsp");
286 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
288 /* Check the response */
289 if (initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
!= NvspStatusSuccess
) {
290 DPRINT_ERR(NETVSC
, "Unable to complete receive buffer "
291 "initialzation with NetVsp - status %d",
292 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
);
297 /* Parse the response */
298 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
299 /* ASSERT(netDevice->ReceiveSections == NULL); */
301 netDevice
->ReceiveSectionCount
= initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.NumSections
;
303 netDevice
->ReceiveSections
= kmalloc(netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
304 if (netDevice
->ReceiveSections
== NULL
) {
309 memcpy(netDevice
->ReceiveSections
,
310 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Sections
,
311 netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
));
313 DPRINT_INFO(NETVSC
, "Receive sections info (count %d, offset %d, "
314 "endoffset %d, suballoc size %d, num suballocs %d)",
315 netDevice
->ReceiveSectionCount
,
316 netDevice
->ReceiveSections
[0].Offset
,
317 netDevice
->ReceiveSections
[0].EndOffset
,
318 netDevice
->ReceiveSections
[0].SubAllocationSize
,
319 netDevice
->ReceiveSections
[0].NumSubAllocations
);
322 * For 1st release, there should only be 1 section that represents the
323 * entire receive buffer
325 if (netDevice
->ReceiveSectionCount
!= 1 ||
326 netDevice
->ReceiveSections
->Offset
!= 0) {
334 NetVscDestroyReceiveBuffer(netDevice
);
337 PutNetDevice(Device
);
342 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device
*Device
)
345 struct netvsc_device
*netDevice
;
346 struct nvsp_message
*initPacket
;
348 DPRINT_ENTER(NETVSC
);
350 netDevice
= GetOutboundNetDevice(Device
);
352 DPRINT_ERR(NETVSC
, "unable to get net device..."
353 "device being destroyed?");
357 if (netDevice
->SendBufferSize
<= 0) {
362 /* page-size grandularity */
363 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
365 netDevice
->SendBuffer
=
366 osd_PageAlloc(netDevice
->SendBufferSize
>> PAGE_SHIFT
);
367 if (!netDevice
->SendBuffer
) {
368 DPRINT_ERR(NETVSC
, "unable to allocate send buffer of size %d",
369 netDevice
->SendBufferSize
);
373 /* page-aligned buffer */
374 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
376 DPRINT_INFO(NETVSC
, "Establishing send buffer's GPADL...");
379 * Establish the gpadl handle for this buffer on this
380 * channel. Note: This call uses the vmbus connection rather
381 * than the channel to establish the gpadl handle.
383 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
384 netDevice
->SendBuffer
,
385 netDevice
->SendBufferSize
,
386 &netDevice
->SendBufferGpadlHandle
);
388 DPRINT_ERR(NETVSC
, "unable to establish send buffer's gpadl");
392 /* osd_WaitEventWait(ext->ChannelInitEvent); */
394 /* Notify the NetVsp of the gpadl handle */
395 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendSendBuffer...");
397 initPacket
= &netDevice
->ChannelInitPacket
;
399 memset(initPacket
, 0, sizeof(struct nvsp_message
));
401 initPacket
->Header
.MessageType
= NvspMessage1TypeSendSendBuffer
;
402 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->SendBufferGpadlHandle
;
403 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
405 /* Send the gpadl notification request */
406 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
407 initPacket
, sizeof(struct nvsp_message
),
408 (unsigned long)initPacket
,
409 VmbusPacketTypeDataInBand
,
410 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
413 "unable to send receive buffer's gpadl to netvsp");
417 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
419 /* Check the response */
420 if (initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
!= NvspStatusSuccess
) {
421 DPRINT_ERR(NETVSC
, "Unable to complete send buffer "
422 "initialzation with NetVsp - status %d",
423 initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
);
428 netDevice
->SendSectionSize
= initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.SectionSize
;
433 NetVscDestroySendBuffer(netDevice
);
436 PutNetDevice(Device
);
441 static int NetVscDestroyReceiveBuffer(struct netvsc_device
*NetDevice
)
443 struct nvsp_message
*revokePacket
;
446 DPRINT_ENTER(NETVSC
);
449 * If we got a section count, it means we received a
450 * SendReceiveBufferComplete msg (ie sent
451 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
452 * to send a revoke msg here
454 if (NetDevice
->ReceiveSectionCount
) {
456 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
458 /* Send the revoke receive buffer */
459 revokePacket
= &NetDevice
->RevokePacket
;
460 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
462 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeReceiveBuffer
;
463 revokePacket
->Messages
.Version1Messages
.RevokeReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
465 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(
468 sizeof(struct nvsp_message
),
469 (unsigned long)revokePacket
,
470 VmbusPacketTypeDataInBand
, 0);
472 * If we failed here, we might as well return and
473 * have a leak rather than continue and a bugchk
476 DPRINT_ERR(NETVSC
, "unable to send revoke receive "
483 /* Teardown the gpadl on the vsp end */
484 if (NetDevice
->ReceiveBufferGpadlHandle
) {
485 DPRINT_INFO(NETVSC
, "Tearing down receive buffer's GPADL...");
487 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(
489 NetDevice
->ReceiveBufferGpadlHandle
);
491 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
494 "unable to teardown receive buffer's gpadl");
498 NetDevice
->ReceiveBufferGpadlHandle
= 0;
501 if (NetDevice
->ReceiveBuffer
) {
502 DPRINT_INFO(NETVSC
, "Freeing up receive buffer...");
504 /* Free up the receive buffer */
505 osd_PageFree(NetDevice
->ReceiveBuffer
,
506 NetDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
507 NetDevice
->ReceiveBuffer
= NULL
;
510 if (NetDevice
->ReceiveSections
) {
511 NetDevice
->ReceiveSectionCount
= 0;
512 kfree(NetDevice
->ReceiveSections
);
513 NetDevice
->ReceiveSections
= NULL
;
521 static int NetVscDestroySendBuffer(struct netvsc_device
*NetDevice
)
523 struct nvsp_message
*revokePacket
;
526 DPRINT_ENTER(NETVSC
);
529 * If we got a section count, it means we received a
530 * SendReceiveBufferComplete msg (ie sent
531 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
532 * to send a revoke msg here
534 if (NetDevice
->SendSectionSize
) {
536 "Sending NvspMessage1TypeRevokeSendBuffer...");
538 /* Send the revoke send buffer */
539 revokePacket
= &NetDevice
->RevokePacket
;
540 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
542 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeSendBuffer
;
543 revokePacket
->Messages
.Version1Messages
.RevokeSendBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
545 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(NetDevice
->Device
,
547 sizeof(struct nvsp_message
),
548 (unsigned long)revokePacket
,
549 VmbusPacketTypeDataInBand
, 0);
551 * If we failed here, we might as well return and have a leak
552 * rather than continue and a bugchk
555 DPRINT_ERR(NETVSC
, "unable to send revoke send buffer "
562 /* Teardown the gpadl on the vsp end */
563 if (NetDevice
->SendBufferGpadlHandle
) {
564 DPRINT_INFO(NETVSC
, "Tearing down send buffer's GPADL...");
566 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(NetDevice
->Device
, NetDevice
->SendBufferGpadlHandle
);
569 * If we failed here, we might as well return and have a leak
570 * rather than continue and a bugchk
573 DPRINT_ERR(NETVSC
, "unable to teardown send buffer's "
578 NetDevice
->SendBufferGpadlHandle
= 0;
581 if (NetDevice
->SendBuffer
) {
582 DPRINT_INFO(NETVSC
, "Freeing up send buffer...");
584 /* Free up the receive buffer */
585 osd_PageFree(NetDevice
->SendBuffer
,
586 NetDevice
->SendBufferSize
>> PAGE_SHIFT
);
587 NetDevice
->SendBuffer
= NULL
;
596 static int NetVscConnectToVsp(struct hv_device
*Device
)
599 struct netvsc_device
*netDevice
;
600 struct nvsp_message
*initPacket
;
603 DPRINT_ENTER(NETVSC
);
605 netDevice
= GetOutboundNetDevice(Device
);
607 DPRINT_ERR(NETVSC
, "unable to get net device..."
608 "device being destroyed?");
613 initPacket
= &netDevice
->ChannelInitPacket
;
615 memset(initPacket
, 0, sizeof(struct nvsp_message
));
616 initPacket
->Header
.MessageType
= NvspMessageTypeInit
;
617 initPacket
->Messages
.InitMessages
.Init
.MinProtocolVersion
= NVSP_MIN_PROTOCOL_VERSION
;
618 initPacket
->Messages
.InitMessages
.Init
.MaxProtocolVersion
= NVSP_MAX_PROTOCOL_VERSION
;
620 DPRINT_INFO(NETVSC
, "Sending NvspMessageTypeInit...");
622 /* Send the init request */
623 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
625 sizeof(struct nvsp_message
),
626 (unsigned long)initPacket
,
627 VmbusPacketTypeDataInBand
,
628 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
631 DPRINT_ERR(NETVSC
, "unable to send NvspMessageTypeInit");
635 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
637 /* Now, check the response */
638 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
639 DPRINT_INFO(NETVSC
, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
640 initPacket
->Messages
.InitMessages
.InitComplete
.Status
,
641 initPacket
->Messages
.InitMessages
.InitComplete
.MaximumMdlChainLength
);
643 if (initPacket
->Messages
.InitMessages
.InitComplete
.Status
!=
646 "unable to initialize with netvsp (status 0x%x)",
647 initPacket
->Messages
.InitMessages
.InitComplete
.Status
);
652 if (initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
!= NVSP_PROTOCOL_VERSION_1
) {
653 DPRINT_ERR(NETVSC
, "unable to initialize with netvsp "
654 "(version expected 1 got %d)",
655 initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
);
659 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendNdisVersion...");
661 /* Send the ndis version */
662 memset(initPacket
, 0, sizeof(struct nvsp_message
));
664 ndisVersion
= 0x00050000;
666 initPacket
->Header
.MessageType
= NvspMessage1TypeSendNdisVersion
;
667 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMajorVersion
=
668 (ndisVersion
& 0xFFFF0000) >> 16;
669 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMinorVersion
=
670 ndisVersion
& 0xFFFF;
672 /* Send the init request */
673 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
675 sizeof(struct nvsp_message
),
676 (unsigned long)initPacket
,
677 VmbusPacketTypeDataInBand
, 0);
680 "unable to send NvspMessage1TypeSendNdisVersion");
685 * BUGBUG - We have to wait for the above msg since the
686 * netvsp uses KMCL which acknowledges packet (completion
687 * packet) since our Vmbus always set the
688 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
690 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
692 /* Post the big receive buffer to NetVSP */
693 ret
= NetVscInitializeReceiveBufferWithNetVsp(Device
);
695 ret
= NetVscInitializeSendBufferWithNetVsp(Device
);
698 PutNetDevice(Device
);
703 static void NetVscDisconnectFromVsp(struct netvsc_device
*NetDevice
)
705 DPRINT_ENTER(NETVSC
);
707 NetVscDestroyReceiveBuffer(NetDevice
);
708 NetVscDestroySendBuffer(NetDevice
);
714 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
716 static int NetVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
720 struct netvsc_device
*netDevice
;
721 struct hv_netvsc_packet
*packet
, *pos
;
722 struct netvsc_driver
*netDriver
=
723 (struct netvsc_driver
*)Device
->Driver
;
725 DPRINT_ENTER(NETVSC
);
727 netDevice
= AllocNetDevice(Device
);
733 DPRINT_DBG(NETVSC
, "netvsc channel object allocated - %p", netDevice
);
735 /* Initialize the NetVSC channel extension */
736 netDevice
->ReceiveBufferSize
= NETVSC_RECEIVE_BUFFER_SIZE
;
737 spin_lock_init(&netDevice
->receive_packet_list_lock
);
739 netDevice
->SendBufferSize
= NETVSC_SEND_BUFFER_SIZE
;
741 INIT_LIST_HEAD(&netDevice
->ReceivePacketList
);
743 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
744 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
745 (NETVSC_RECEIVE_SG_COUNT
*
746 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
748 DPRINT_DBG(NETVSC
, "unable to allocate netvsc pkts "
749 "for receive pool (wanted %d got %d)",
750 NETVSC_RECEIVE_PACKETLIST_COUNT
, i
);
753 list_add_tail(&packet
->ListEntry
,
754 &netDevice
->ReceivePacketList
);
756 netDevice
->ChannelInitEvent
= osd_WaitEventCreate();
757 if (!netDevice
->ChannelInitEvent
) {
762 /* Open the channel */
763 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
764 netDriver
->RingBufferSize
,
765 netDriver
->RingBufferSize
,
767 NetVscOnChannelCallback
,
771 DPRINT_ERR(NETVSC
, "unable to open channel: %d", ret
);
776 /* Channel is opened */
777 DPRINT_INFO(NETVSC
, "*** NetVSC channel opened successfully! ***");
779 /* Connect with the NetVsp */
780 ret
= NetVscConnectToVsp(Device
);
782 DPRINT_ERR(NETVSC
, "unable to connect to NetVSP - %d", ret
);
787 DPRINT_INFO(NETVSC
, "*** NetVSC channel handshake result - %d ***",
794 /* Now, we can close the channel safely */
795 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
800 kfree(netDevice
->ChannelInitEvent
);
802 list_for_each_entry_safe(packet
, pos
,
803 &netDevice
->ReceivePacketList
,
805 list_del(&packet
->ListEntry
);
809 ReleaseOutboundNetDevice(Device
);
810 ReleaseInboundNetDevice(Device
);
812 FreeNetDevice(netDevice
);
820 * NetVscOnDeviceRemove - Callback when the root bus device is removed
822 static int NetVscOnDeviceRemove(struct hv_device
*Device
)
824 struct netvsc_device
*netDevice
;
825 struct hv_netvsc_packet
*netvscPacket
, *pos
;
827 DPRINT_ENTER(NETVSC
);
829 DPRINT_INFO(NETVSC
, "Disabling outbound traffic on net device (%p)...",
832 /* Stop outbound traffic ie sends and receives completions */
833 netDevice
= ReleaseOutboundNetDevice(Device
);
835 DPRINT_ERR(NETVSC
, "No net device present!!");
839 /* Wait for all send completions */
840 while (atomic_read(&netDevice
->NumOutstandingSends
)) {
841 DPRINT_INFO(NETVSC
, "waiting for %d requests to complete...",
842 atomic_read(&netDevice
->NumOutstandingSends
));
846 DPRINT_INFO(NETVSC
, "Disconnecting from netvsp...");
848 NetVscDisconnectFromVsp(netDevice
);
850 DPRINT_INFO(NETVSC
, "Disabling inbound traffic on net device (%p)...",
853 /* Stop inbound traffic ie receives and sends completions */
854 netDevice
= ReleaseInboundNetDevice(Device
);
856 /* At this point, no one should be accessing netDevice except in here */
857 DPRINT_INFO(NETVSC
, "net device (%p) safe to remove", netDevice
);
859 /* Now, we can close the channel safely */
860 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
862 /* Release all resources */
863 list_for_each_entry_safe(netvscPacket
, pos
,
864 &netDevice
->ReceivePacketList
, ListEntry
) {
865 list_del(&netvscPacket
->ListEntry
);
869 kfree(netDevice
->ChannelInitEvent
);
870 FreeNetDevice(netDevice
);
877 * NetVscOnCleanup - Perform any cleanup when the driver is removed
879 static void NetVscOnCleanup(struct hv_driver
*drv
)
881 DPRINT_ENTER(NETVSC
);
885 static void NetVscOnSendCompletion(struct hv_device
*Device
,
886 struct vmpacket_descriptor
*Packet
)
888 struct netvsc_device
*netDevice
;
889 struct nvsp_message
*nvspPacket
;
890 struct hv_netvsc_packet
*nvscPacket
;
892 DPRINT_ENTER(NETVSC
);
894 netDevice
= GetInboundNetDevice(Device
);
896 DPRINT_ERR(NETVSC
, "unable to get net device..."
897 "device being destroyed?");
902 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+ (Packet
->DataOffset8
<< 3));
904 DPRINT_DBG(NETVSC
, "send completion packet - type %d",
905 nvspPacket
->Header
.MessageType
);
907 if ((nvspPacket
->Header
.MessageType
== NvspMessageTypeInitComplete
) ||
908 (nvspPacket
->Header
.MessageType
==
909 NvspMessage1TypeSendReceiveBufferComplete
) ||
910 (nvspPacket
->Header
.MessageType
==
911 NvspMessage1TypeSendSendBufferComplete
)) {
912 /* Copy the response back */
913 memcpy(&netDevice
->ChannelInitPacket
, nvspPacket
,
914 sizeof(struct nvsp_message
));
915 osd_WaitEventSet(netDevice
->ChannelInitEvent
);
916 } else if (nvspPacket
->Header
.MessageType
==
917 NvspMessage1TypeSendRNDISPacketComplete
) {
918 /* Get the send context */
919 nvscPacket
= (struct hv_netvsc_packet
*)(unsigned long)Packet
->TransactionId
;
920 /* ASSERT(nvscPacket); */
922 /* Notify the layer above us */
923 nvscPacket
->Completion
.Send
.OnSendCompletion(nvscPacket
->Completion
.Send
.SendCompletionContext
);
925 atomic_dec(&netDevice
->NumOutstandingSends
);
927 DPRINT_ERR(NETVSC
, "Unknown send completion packet type - "
928 "%d received!!", nvspPacket
->Header
.MessageType
);
931 PutNetDevice(Device
);
935 static int NetVscOnSend(struct hv_device
*Device
,
936 struct hv_netvsc_packet
*Packet
)
938 struct netvsc_device
*netDevice
;
941 struct nvsp_message sendMessage
;
943 DPRINT_ENTER(NETVSC
);
945 netDevice
= GetOutboundNetDevice(Device
);
947 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
948 "ignoring outbound packets", netDevice
);
953 sendMessage
.Header
.MessageType
= NvspMessage1TypeSendRNDISPacket
;
954 if (Packet
->IsDataPacket
) {
956 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 0;
958 /* 1 is RMC_CONTROL; */
959 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 1;
962 /* Not using send buffer section */
963 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionIndex
= 0xFFFFFFFF;
964 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionSize
= 0;
966 if (Packet
->PageBufferCount
) {
967 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacketPageBuffer(
968 Device
, Packet
->PageBuffers
,
969 Packet
->PageBufferCount
,
971 sizeof(struct nvsp_message
),
972 (unsigned long)Packet
);
974 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
976 sizeof(struct nvsp_message
),
977 (unsigned long)Packet
,
978 VmbusPacketTypeDataInBand
,
979 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
984 DPRINT_ERR(NETVSC
, "Unable to send packet %p ret %d",
987 atomic_inc(&netDevice
->NumOutstandingSends
);
988 PutNetDevice(Device
);
994 static void NetVscOnReceive(struct hv_device
*Device
,
995 struct vmpacket_descriptor
*Packet
)
997 struct netvsc_device
*netDevice
;
998 struct vmtransfer_page_packet_header
*vmxferpagePacket
;
999 struct nvsp_message
*nvspPacket
;
1000 struct hv_netvsc_packet
*netvscPacket
= NULL
;
1001 unsigned long start
;
1002 unsigned long end
, endVirtual
;
1003 /* struct netvsc_driver *netvscDriver; */
1004 struct xferpage_packet
*xferpagePacket
= NULL
;
1006 int count
= 0, bytesRemain
= 0;
1007 unsigned long flags
;
1008 LIST_HEAD(listHead
);
1010 DPRINT_ENTER(NETVSC
);
1012 netDevice
= GetInboundNetDevice(Device
);
1014 DPRINT_ERR(NETVSC
, "unable to get net device..."
1015 "device being destroyed?");
1016 DPRINT_EXIT(NETVSC
);
1021 * All inbound packets other than send completion should be xfer page
1024 if (Packet
->Type
!= VmbusPacketTypeDataUsingTransferPages
) {
1025 DPRINT_ERR(NETVSC
, "Unknown packet type received - %d",
1027 PutNetDevice(Device
);
1031 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+
1032 (Packet
->DataOffset8
<< 3));
1034 /* Make sure this is a valid nvsp packet */
1035 if (nvspPacket
->Header
.MessageType
!= NvspMessage1TypeSendRNDISPacket
) {
1036 DPRINT_ERR(NETVSC
, "Unknown nvsp packet type received - %d",
1037 nvspPacket
->Header
.MessageType
);
1038 PutNetDevice(Device
);
1042 DPRINT_DBG(NETVSC
, "NVSP packet received - type %d",
1043 nvspPacket
->Header
.MessageType
);
1045 vmxferpagePacket
= (struct vmtransfer_page_packet_header
*)Packet
;
1047 if (vmxferpagePacket
->TransferPageSetId
!= NETVSC_RECEIVE_BUFFER_ID
) {
1048 DPRINT_ERR(NETVSC
, "Invalid xfer page set id - "
1049 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
1050 vmxferpagePacket
->TransferPageSetId
);
1051 PutNetDevice(Device
);
1055 DPRINT_DBG(NETVSC
, "xfer page - range count %d",
1056 vmxferpagePacket
->RangeCount
);
1059 * Grab free packets (range count + 1) to represent this xfer
1060 * page packet. +1 to represent the xfer page packet itself.
1061 * We grab it here so that we know exactly how many we can
1064 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1065 while (!list_empty(&netDevice
->ReceivePacketList
)) {
1066 list_move_tail(netDevice
->ReceivePacketList
.next
, &listHead
);
1067 if (++count
== vmxferpagePacket
->RangeCount
+ 1)
1070 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1073 * We need at least 2 netvsc pkts (1 to represent the xfer
1074 * page and at least 1 for the range) i.e. we can handled
1075 * some of the xfer page packet ranges...
1078 DPRINT_ERR(NETVSC
, "Got only %d netvsc pkt...needed %d pkts. "
1079 "Dropping this xfer page packet completely!",
1080 count
, vmxferpagePacket
->RangeCount
+ 1);
1082 /* Return it to the freelist */
1083 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1084 for (i
= count
; i
!= 0; i
--) {
1085 list_move_tail(listHead
.next
,
1086 &netDevice
->ReceivePacketList
);
1088 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
,
1091 NetVscSendReceiveCompletion(Device
,
1092 vmxferpagePacket
->d
.TransactionId
);
1094 PutNetDevice(Device
);
1098 /* Remove the 1st packet to represent the xfer page packet itself */
1099 xferpagePacket
= (struct xferpage_packet
*)listHead
.next
;
1100 list_del(&xferpagePacket
->ListEntry
);
1102 /* This is how much we can satisfy */
1103 xferpagePacket
->Count
= count
- 1;
1104 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1105 /* vmxferpagePacket->RangeCount); */
1107 if (xferpagePacket
->Count
!= vmxferpagePacket
->RangeCount
) {
1108 DPRINT_INFO(NETVSC
, "Needed %d netvsc pkts to satisy this xfer "
1109 "page...got %d", vmxferpagePacket
->RangeCount
,
1110 xferpagePacket
->Count
);
1113 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1114 for (i
= 0; i
< (count
- 1); i
++) {
1115 netvscPacket
= (struct hv_netvsc_packet
*)listHead
.next
;
1116 list_del(&netvscPacket
->ListEntry
);
1118 /* Initialize the netvsc packet */
1119 netvscPacket
->XferPagePacket
= xferpagePacket
;
1120 netvscPacket
->Completion
.Recv
.OnReceiveCompletion
=
1121 NetVscOnReceiveCompletion
;
1122 netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
=
1124 netvscPacket
->Device
= Device
;
1125 /* Save this so that we can send it back */
1126 netvscPacket
->Completion
.Recv
.ReceiveCompletionTid
=
1127 vmxferpagePacket
->d
.TransactionId
;
1129 netvscPacket
->TotalDataBufferLength
=
1130 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1131 netvscPacket
->PageBufferCount
= 1;
1133 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1134 /* vmxferpagePacket->Ranges[i].ByteCount < */
1135 /* netDevice->ReceiveBufferSize); */
1137 netvscPacket
->PageBuffers
[0].Length
=
1138 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1140 start
= virt_to_phys((void *)((unsigned long)netDevice
->ReceiveBuffer
+ vmxferpagePacket
->Ranges
[i
].ByteOffset
));
1142 netvscPacket
->PageBuffers
[0].Pfn
= start
>> PAGE_SHIFT
;
1143 endVirtual
= (unsigned long)netDevice
->ReceiveBuffer
1144 + vmxferpagePacket
->Ranges
[i
].ByteOffset
1145 + vmxferpagePacket
->Ranges
[i
].ByteCount
- 1;
1146 end
= virt_to_phys((void *)endVirtual
);
1148 /* Calculate the page relative offset */
1149 netvscPacket
->PageBuffers
[0].Offset
=
1150 vmxferpagePacket
->Ranges
[i
].ByteOffset
& (PAGE_SIZE
- 1);
1151 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1152 /* Handle frame across multiple pages: */
1153 netvscPacket
->PageBuffers
[0].Length
=
1154 (netvscPacket
->PageBuffers
[0].Pfn
<< PAGE_SHIFT
)
1155 + PAGE_SIZE
- start
;
1156 bytesRemain
= netvscPacket
->TotalDataBufferLength
-
1157 netvscPacket
->PageBuffers
[0].Length
;
1158 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1159 netvscPacket
->PageBuffers
[j
].Offset
= 0;
1160 if (bytesRemain
<= PAGE_SIZE
) {
1161 netvscPacket
->PageBuffers
[j
].Length
= bytesRemain
;
1164 netvscPacket
->PageBuffers
[j
].Length
= PAGE_SIZE
;
1165 bytesRemain
-= PAGE_SIZE
;
1167 netvscPacket
->PageBuffers
[j
].Pfn
=
1168 virt_to_phys((void *)(endVirtual
- bytesRemain
)) >> PAGE_SHIFT
;
1169 netvscPacket
->PageBufferCount
++;
1170 if (bytesRemain
== 0)
1173 /* ASSERT(bytesRemain == 0); */
1175 DPRINT_DBG(NETVSC
, "[%d] - (abs offset %u len %u) => "
1176 "(pfn %llx, offset %u, len %u)", i
,
1177 vmxferpagePacket
->Ranges
[i
].ByteOffset
,
1178 vmxferpagePacket
->Ranges
[i
].ByteCount
,
1179 netvscPacket
->PageBuffers
[0].Pfn
,
1180 netvscPacket
->PageBuffers
[0].Offset
,
1181 netvscPacket
->PageBuffers
[0].Length
);
1183 /* Pass it to the upper layer */
1184 ((struct netvsc_driver
*)Device
->Driver
)->OnReceiveCallback(Device
, netvscPacket
);
1186 NetVscOnReceiveCompletion(netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
);
1189 /* ASSERT(list_empty(&listHead)); */
1191 PutNetDevice(Device
);
1192 DPRINT_EXIT(NETVSC
);
1195 static void NetVscSendReceiveCompletion(struct hv_device
*Device
,
1198 struct nvsp_message recvcompMessage
;
1202 DPRINT_DBG(NETVSC
, "Sending receive completion pkt - %llx",
1205 recvcompMessage
.Header
.MessageType
=
1206 NvspMessage1TypeSendRNDISPacketComplete
;
1208 /* FIXME: Pass in the status */
1209 recvcompMessage
.Messages
.Version1Messages
.SendRNDISPacketComplete
.Status
= NvspStatusSuccess
;
1212 /* Send the completion */
1213 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
1215 sizeof(struct nvsp_message
),
1217 VmbusPacketTypeCompletion
, 0);
1221 } else if (ret
== -1) {
1222 /* no more room...wait a bit and attempt to retry 3 times */
1224 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt "
1225 "(tid %llx)...retrying %d", TransactionId
, retries
);
1229 goto retry_send_cmplt
;
1231 DPRINT_ERR(NETVSC
, "unable to send receive completion "
1232 "pkt (tid %llx)...give up retrying",
1236 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt - "
1237 "%llx", TransactionId
);
1241 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1242 static void NetVscOnReceiveCompletion(void *Context
)
1244 struct hv_netvsc_packet
*packet
= Context
;
1245 struct hv_device
*device
= (struct hv_device
*)packet
->Device
;
1246 struct netvsc_device
*netDevice
;
1247 u64 transactionId
= 0;
1248 bool fSendReceiveComp
= false;
1249 unsigned long flags
;
1251 DPRINT_ENTER(NETVSC
);
1253 /* ASSERT(packet->XferPagePacket); */
1256 * Even though it seems logical to do a GetOutboundNetDevice() here to
1257 * send out receive completion, we are using GetInboundNetDevice()
1258 * since we may have disable outbound traffic already.
1260 netDevice
= GetInboundNetDevice(device
);
1262 DPRINT_ERR(NETVSC
, "unable to get net device..."
1263 "device being destroyed?");
1264 DPRINT_EXIT(NETVSC
);
1268 /* Overloading use of the lock. */
1269 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1271 /* ASSERT(packet->XferPagePacket->Count > 0); */
1272 packet
->XferPagePacket
->Count
--;
1275 * Last one in the line that represent 1 xfer page packet.
1276 * Return the xfer page packet itself to the freelist
1278 if (packet
->XferPagePacket
->Count
== 0) {
1279 fSendReceiveComp
= true;
1280 transactionId
= packet
->Completion
.Recv
.ReceiveCompletionTid
;
1281 list_add_tail(&packet
->XferPagePacket
->ListEntry
,
1282 &netDevice
->ReceivePacketList
);
1286 /* Put the packet back */
1287 list_add_tail(&packet
->ListEntry
, &netDevice
->ReceivePacketList
);
1288 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1290 /* Send a receive completion for the xfer page packet */
1291 if (fSendReceiveComp
)
1292 NetVscSendReceiveCompletion(device
, transactionId
);
1294 PutNetDevice(device
);
1295 DPRINT_EXIT(NETVSC
);
1298 static void NetVscOnChannelCallback(void *Context
)
1301 struct hv_device
*device
= Context
;
1302 struct netvsc_device
*netDevice
;
1305 unsigned char *packet
;
1306 struct vmpacket_descriptor
*desc
;
1307 unsigned char *buffer
;
1308 int bufferlen
= NETVSC_PACKET_SIZE
;
1311 DPRINT_ENTER(NETVSC
);
1313 /* ASSERT(device); */
1315 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
1321 netDevice
= GetInboundNetDevice(device
);
1323 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
1324 "ignoring inbound packets", netDevice
);
1325 DPRINT_EXIT(NETVSC
);
1330 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacketRaw(
1331 device
, buffer
, bufferlen
,
1332 &bytesRecvd
, &requestId
);
1334 if (bytesRecvd
> 0) {
1335 DPRINT_DBG(NETVSC
, "receive %d bytes, tid %llx",
1336 bytesRecvd
, requestId
);
1338 desc
= (struct vmpacket_descriptor
*)buffer
;
1339 switch (desc
->Type
) {
1340 case VmbusPacketTypeCompletion
:
1341 NetVscOnSendCompletion(device
, desc
);
1344 case VmbusPacketTypeDataUsingTransferPages
:
1345 NetVscOnReceive(device
, desc
);
1350 "unhandled packet type %d, "
1351 "tid %llx len %d\n",
1352 desc
->Type
, requestId
,
1358 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1361 bufferlen
= NETVSC_PACKET_SIZE
;
1365 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1368 bufferlen
= NETVSC_PACKET_SIZE
;
1373 } else if (ret
== -2) {
1374 /* Handle large packet */
1375 buffer
= kmalloc(bytesRecvd
, GFP_ATOMIC
);
1376 if (buffer
== NULL
) {
1377 /* Try again next time around */
1379 "unable to allocate buffer of size "
1380 "(%d)!!", bytesRecvd
);
1384 bufferlen
= bytesRecvd
;
1388 PutNetDevice(device
);
1389 DPRINT_EXIT(NETVSC
);