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 /* ASSERT(driver->OnReceiveCallback); */
192 /* ASSERT(driver->OnLinkStatusChanged); */
194 /* Setup the dispatch table */
195 driver
->Base
.OnDeviceAdd
= NetVscOnDeviceAdd
;
196 driver
->Base
.OnDeviceRemove
= NetVscOnDeviceRemove
;
197 driver
->Base
.OnCleanup
= NetVscOnCleanup
;
199 driver
->OnSend
= NetVscOnSend
;
201 RndisFilterInit(driver
);
205 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device
*Device
)
208 struct netvsc_device
*netDevice
;
209 struct nvsp_message
*initPacket
;
211 netDevice
= GetOutboundNetDevice(Device
);
213 DPRINT_ERR(NETVSC
, "unable to get net device..."
214 "device being destroyed?");
217 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
218 /* page-size grandularity */
219 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
221 netDevice
->ReceiveBuffer
=
222 osd_PageAlloc(netDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
223 if (!netDevice
->ReceiveBuffer
) {
225 "unable to allocate receive buffer of size %d",
226 netDevice
->ReceiveBufferSize
);
230 /* page-aligned buffer */
231 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
234 DPRINT_INFO(NETVSC
, "Establishing receive buffer's GPADL...");
237 * Establish the gpadl handle for this buffer on this
238 * channel. Note: This call uses the vmbus connection rather
239 * than the channel to establish the gpadl handle.
241 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
242 netDevice
->ReceiveBuffer
,
243 netDevice
->ReceiveBufferSize
,
244 &netDevice
->ReceiveBufferGpadlHandle
);
247 "unable to establish receive buffer's gpadl");
251 /* osd_WaitEventWait(ext->ChannelInitEvent); */
253 /* Notify the NetVsp of the gpadl handle */
254 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendReceiveBuffer...");
256 initPacket
= &netDevice
->ChannelInitPacket
;
258 memset(initPacket
, 0, sizeof(struct nvsp_message
));
260 initPacket
->Header
.MessageType
= NvspMessage1TypeSendReceiveBuffer
;
261 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->ReceiveBufferGpadlHandle
;
262 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
264 /* Send the gpadl notification request */
265 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
267 sizeof(struct nvsp_message
),
268 (unsigned long)initPacket
,
269 VmbusPacketTypeDataInBand
,
270 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
273 "unable to send receive buffer's gpadl to netvsp");
277 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
279 /* Check the response */
280 if (initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
!= NvspStatusSuccess
) {
281 DPRINT_ERR(NETVSC
, "Unable to complete receive buffer "
282 "initialzation with NetVsp - status %d",
283 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
);
288 /* Parse the response */
289 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
290 /* ASSERT(netDevice->ReceiveSections == NULL); */
292 netDevice
->ReceiveSectionCount
= initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.NumSections
;
294 netDevice
->ReceiveSections
= kmalloc(netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
295 if (netDevice
->ReceiveSections
== NULL
) {
300 memcpy(netDevice
->ReceiveSections
,
301 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Sections
,
302 netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
));
304 DPRINT_INFO(NETVSC
, "Receive sections info (count %d, offset %d, "
305 "endoffset %d, suballoc size %d, num suballocs %d)",
306 netDevice
->ReceiveSectionCount
,
307 netDevice
->ReceiveSections
[0].Offset
,
308 netDevice
->ReceiveSections
[0].EndOffset
,
309 netDevice
->ReceiveSections
[0].SubAllocationSize
,
310 netDevice
->ReceiveSections
[0].NumSubAllocations
);
313 * For 1st release, there should only be 1 section that represents the
314 * entire receive buffer
316 if (netDevice
->ReceiveSectionCount
!= 1 ||
317 netDevice
->ReceiveSections
->Offset
!= 0) {
325 NetVscDestroyReceiveBuffer(netDevice
);
328 PutNetDevice(Device
);
332 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device
*Device
)
335 struct netvsc_device
*netDevice
;
336 struct nvsp_message
*initPacket
;
338 netDevice
= GetOutboundNetDevice(Device
);
340 DPRINT_ERR(NETVSC
, "unable to get net device..."
341 "device being destroyed?");
344 if (netDevice
->SendBufferSize
<= 0) {
349 /* page-size grandularity */
350 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
352 netDevice
->SendBuffer
=
353 osd_PageAlloc(netDevice
->SendBufferSize
>> PAGE_SHIFT
);
354 if (!netDevice
->SendBuffer
) {
355 DPRINT_ERR(NETVSC
, "unable to allocate send buffer of size %d",
356 netDevice
->SendBufferSize
);
360 /* page-aligned buffer */
361 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
363 DPRINT_INFO(NETVSC
, "Establishing send buffer's GPADL...");
366 * Establish the gpadl handle for this buffer on this
367 * channel. Note: This call uses the vmbus connection rather
368 * than the channel to establish the gpadl handle.
370 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
371 netDevice
->SendBuffer
,
372 netDevice
->SendBufferSize
,
373 &netDevice
->SendBufferGpadlHandle
);
375 DPRINT_ERR(NETVSC
, "unable to establish send buffer's gpadl");
379 /* osd_WaitEventWait(ext->ChannelInitEvent); */
381 /* Notify the NetVsp of the gpadl handle */
382 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendSendBuffer...");
384 initPacket
= &netDevice
->ChannelInitPacket
;
386 memset(initPacket
, 0, sizeof(struct nvsp_message
));
388 initPacket
->Header
.MessageType
= NvspMessage1TypeSendSendBuffer
;
389 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->SendBufferGpadlHandle
;
390 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
392 /* Send the gpadl notification request */
393 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
394 initPacket
, sizeof(struct nvsp_message
),
395 (unsigned long)initPacket
,
396 VmbusPacketTypeDataInBand
,
397 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
400 "unable to send receive buffer's gpadl to netvsp");
404 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
406 /* Check the response */
407 if (initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
!= NvspStatusSuccess
) {
408 DPRINT_ERR(NETVSC
, "Unable to complete send buffer "
409 "initialzation with NetVsp - status %d",
410 initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
);
415 netDevice
->SendSectionSize
= initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.SectionSize
;
420 NetVscDestroySendBuffer(netDevice
);
423 PutNetDevice(Device
);
427 static int NetVscDestroyReceiveBuffer(struct netvsc_device
*NetDevice
)
429 struct nvsp_message
*revokePacket
;
433 * If we got a section count, it means we received a
434 * SendReceiveBufferComplete msg (ie sent
435 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
436 * to send a revoke msg here
438 if (NetDevice
->ReceiveSectionCount
) {
440 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
442 /* Send the revoke receive buffer */
443 revokePacket
= &NetDevice
->RevokePacket
;
444 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
446 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeReceiveBuffer
;
447 revokePacket
->Messages
.Version1Messages
.RevokeReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
449 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(
452 sizeof(struct nvsp_message
),
453 (unsigned long)revokePacket
,
454 VmbusPacketTypeDataInBand
, 0);
456 * If we failed here, we might as well return and
457 * have a leak rather than continue and a bugchk
460 DPRINT_ERR(NETVSC
, "unable to send revoke receive "
466 /* Teardown the gpadl on the vsp end */
467 if (NetDevice
->ReceiveBufferGpadlHandle
) {
468 DPRINT_INFO(NETVSC
, "Tearing down receive buffer's GPADL...");
470 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(
472 NetDevice
->ReceiveBufferGpadlHandle
);
474 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
477 "unable to teardown receive buffer's gpadl");
480 NetDevice
->ReceiveBufferGpadlHandle
= 0;
483 if (NetDevice
->ReceiveBuffer
) {
484 DPRINT_INFO(NETVSC
, "Freeing up receive buffer...");
486 /* Free up the receive buffer */
487 osd_PageFree(NetDevice
->ReceiveBuffer
,
488 NetDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
489 NetDevice
->ReceiveBuffer
= NULL
;
492 if (NetDevice
->ReceiveSections
) {
493 NetDevice
->ReceiveSectionCount
= 0;
494 kfree(NetDevice
->ReceiveSections
);
495 NetDevice
->ReceiveSections
= NULL
;
501 static int NetVscDestroySendBuffer(struct netvsc_device
*NetDevice
)
503 struct nvsp_message
*revokePacket
;
507 * If we got a section count, it means we received a
508 * SendReceiveBufferComplete msg (ie sent
509 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
510 * to send a revoke msg here
512 if (NetDevice
->SendSectionSize
) {
514 "Sending NvspMessage1TypeRevokeSendBuffer...");
516 /* Send the revoke send buffer */
517 revokePacket
= &NetDevice
->RevokePacket
;
518 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
520 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeSendBuffer
;
521 revokePacket
->Messages
.Version1Messages
.RevokeSendBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
523 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(NetDevice
->Device
,
525 sizeof(struct nvsp_message
),
526 (unsigned long)revokePacket
,
527 VmbusPacketTypeDataInBand
, 0);
529 * If we failed here, we might as well return and have a leak
530 * rather than continue and a bugchk
533 DPRINT_ERR(NETVSC
, "unable to send revoke send buffer "
539 /* Teardown the gpadl on the vsp end */
540 if (NetDevice
->SendBufferGpadlHandle
) {
541 DPRINT_INFO(NETVSC
, "Tearing down send buffer's GPADL...");
543 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(NetDevice
->Device
, NetDevice
->SendBufferGpadlHandle
);
546 * If we failed here, we might as well return and have a leak
547 * rather than continue and a bugchk
550 DPRINT_ERR(NETVSC
, "unable to teardown send buffer's "
554 NetDevice
->SendBufferGpadlHandle
= 0;
557 if (NetDevice
->SendBuffer
) {
558 DPRINT_INFO(NETVSC
, "Freeing up send buffer...");
560 /* Free up the receive buffer */
561 osd_PageFree(NetDevice
->SendBuffer
,
562 NetDevice
->SendBufferSize
>> PAGE_SHIFT
);
563 NetDevice
->SendBuffer
= NULL
;
570 static int NetVscConnectToVsp(struct hv_device
*Device
)
573 struct netvsc_device
*netDevice
;
574 struct nvsp_message
*initPacket
;
577 netDevice
= GetOutboundNetDevice(Device
);
579 DPRINT_ERR(NETVSC
, "unable to get net device..."
580 "device being destroyed?");
584 initPacket
= &netDevice
->ChannelInitPacket
;
586 memset(initPacket
, 0, sizeof(struct nvsp_message
));
587 initPacket
->Header
.MessageType
= NvspMessageTypeInit
;
588 initPacket
->Messages
.InitMessages
.Init
.MinProtocolVersion
= NVSP_MIN_PROTOCOL_VERSION
;
589 initPacket
->Messages
.InitMessages
.Init
.MaxProtocolVersion
= NVSP_MAX_PROTOCOL_VERSION
;
591 DPRINT_INFO(NETVSC
, "Sending NvspMessageTypeInit...");
593 /* Send the init request */
594 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
596 sizeof(struct nvsp_message
),
597 (unsigned long)initPacket
,
598 VmbusPacketTypeDataInBand
,
599 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
602 DPRINT_ERR(NETVSC
, "unable to send NvspMessageTypeInit");
606 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
608 /* Now, check the response */
609 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
610 DPRINT_INFO(NETVSC
, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
611 initPacket
->Messages
.InitMessages
.InitComplete
.Status
,
612 initPacket
->Messages
.InitMessages
.InitComplete
.MaximumMdlChainLength
);
614 if (initPacket
->Messages
.InitMessages
.InitComplete
.Status
!=
617 "unable to initialize with netvsp (status 0x%x)",
618 initPacket
->Messages
.InitMessages
.InitComplete
.Status
);
623 if (initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
!= NVSP_PROTOCOL_VERSION_1
) {
624 DPRINT_ERR(NETVSC
, "unable to initialize with netvsp "
625 "(version expected 1 got %d)",
626 initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
);
630 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendNdisVersion...");
632 /* Send the ndis version */
633 memset(initPacket
, 0, sizeof(struct nvsp_message
));
635 ndisVersion
= 0x00050000;
637 initPacket
->Header
.MessageType
= NvspMessage1TypeSendNdisVersion
;
638 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMajorVersion
=
639 (ndisVersion
& 0xFFFF0000) >> 16;
640 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMinorVersion
=
641 ndisVersion
& 0xFFFF;
643 /* Send the init request */
644 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
646 sizeof(struct nvsp_message
),
647 (unsigned long)initPacket
,
648 VmbusPacketTypeDataInBand
, 0);
651 "unable to send NvspMessage1TypeSendNdisVersion");
656 * BUGBUG - We have to wait for the above msg since the
657 * netvsp uses KMCL which acknowledges packet (completion
658 * packet) since our Vmbus always set the
659 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
661 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
663 /* Post the big receive buffer to NetVSP */
664 ret
= NetVscInitializeReceiveBufferWithNetVsp(Device
);
666 ret
= NetVscInitializeSendBufferWithNetVsp(Device
);
669 PutNetDevice(Device
);
673 static void NetVscDisconnectFromVsp(struct netvsc_device
*NetDevice
)
675 NetVscDestroyReceiveBuffer(NetDevice
);
676 NetVscDestroySendBuffer(NetDevice
);
680 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
682 static int NetVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
686 struct netvsc_device
*netDevice
;
687 struct hv_netvsc_packet
*packet
, *pos
;
688 struct netvsc_driver
*netDriver
=
689 (struct netvsc_driver
*)Device
->Driver
;
691 netDevice
= AllocNetDevice(Device
);
697 DPRINT_DBG(NETVSC
, "netvsc channel object allocated - %p", netDevice
);
699 /* Initialize the NetVSC channel extension */
700 netDevice
->ReceiveBufferSize
= NETVSC_RECEIVE_BUFFER_SIZE
;
701 spin_lock_init(&netDevice
->receive_packet_list_lock
);
703 netDevice
->SendBufferSize
= NETVSC_SEND_BUFFER_SIZE
;
705 INIT_LIST_HEAD(&netDevice
->ReceivePacketList
);
707 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
708 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
709 (NETVSC_RECEIVE_SG_COUNT
*
710 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
712 DPRINT_DBG(NETVSC
, "unable to allocate netvsc pkts "
713 "for receive pool (wanted %d got %d)",
714 NETVSC_RECEIVE_PACKETLIST_COUNT
, i
);
717 list_add_tail(&packet
->ListEntry
,
718 &netDevice
->ReceivePacketList
);
720 netDevice
->ChannelInitEvent
= osd_WaitEventCreate();
721 if (!netDevice
->ChannelInitEvent
) {
726 /* Open the channel */
727 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
728 netDriver
->RingBufferSize
,
729 netDriver
->RingBufferSize
,
731 NetVscOnChannelCallback
,
735 DPRINT_ERR(NETVSC
, "unable to open channel: %d", ret
);
740 /* Channel is opened */
741 DPRINT_INFO(NETVSC
, "*** NetVSC channel opened successfully! ***");
743 /* Connect with the NetVsp */
744 ret
= NetVscConnectToVsp(Device
);
746 DPRINT_ERR(NETVSC
, "unable to connect to NetVSP - %d", ret
);
751 DPRINT_INFO(NETVSC
, "*** NetVSC channel handshake result - %d ***",
757 /* Now, we can close the channel safely */
758 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
763 kfree(netDevice
->ChannelInitEvent
);
765 list_for_each_entry_safe(packet
, pos
,
766 &netDevice
->ReceivePacketList
,
768 list_del(&packet
->ListEntry
);
772 ReleaseOutboundNetDevice(Device
);
773 ReleaseInboundNetDevice(Device
);
775 FreeNetDevice(netDevice
);
782 * NetVscOnDeviceRemove - Callback when the root bus device is removed
784 static int NetVscOnDeviceRemove(struct hv_device
*Device
)
786 struct netvsc_device
*netDevice
;
787 struct hv_netvsc_packet
*netvscPacket
, *pos
;
789 DPRINT_INFO(NETVSC
, "Disabling outbound traffic on net device (%p)...",
792 /* Stop outbound traffic ie sends and receives completions */
793 netDevice
= ReleaseOutboundNetDevice(Device
);
795 DPRINT_ERR(NETVSC
, "No net device present!!");
799 /* Wait for all send completions */
800 while (atomic_read(&netDevice
->NumOutstandingSends
)) {
801 DPRINT_INFO(NETVSC
, "waiting for %d requests to complete...",
802 atomic_read(&netDevice
->NumOutstandingSends
));
806 DPRINT_INFO(NETVSC
, "Disconnecting from netvsp...");
808 NetVscDisconnectFromVsp(netDevice
);
810 DPRINT_INFO(NETVSC
, "Disabling inbound traffic on net device (%p)...",
813 /* Stop inbound traffic ie receives and sends completions */
814 netDevice
= ReleaseInboundNetDevice(Device
);
816 /* At this point, no one should be accessing netDevice except in here */
817 DPRINT_INFO(NETVSC
, "net device (%p) safe to remove", netDevice
);
819 /* Now, we can close the channel safely */
820 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
822 /* Release all resources */
823 list_for_each_entry_safe(netvscPacket
, pos
,
824 &netDevice
->ReceivePacketList
, ListEntry
) {
825 list_del(&netvscPacket
->ListEntry
);
829 kfree(netDevice
->ChannelInitEvent
);
830 FreeNetDevice(netDevice
);
835 * NetVscOnCleanup - Perform any cleanup when the driver is removed
837 static void NetVscOnCleanup(struct hv_driver
*drv
)
841 static void NetVscOnSendCompletion(struct hv_device
*Device
,
842 struct vmpacket_descriptor
*Packet
)
844 struct netvsc_device
*netDevice
;
845 struct nvsp_message
*nvspPacket
;
846 struct hv_netvsc_packet
*nvscPacket
;
848 netDevice
= GetInboundNetDevice(Device
);
850 DPRINT_ERR(NETVSC
, "unable to get net device..."
851 "device being destroyed?");
855 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+ (Packet
->DataOffset8
<< 3));
857 DPRINT_DBG(NETVSC
, "send completion packet - type %d",
858 nvspPacket
->Header
.MessageType
);
860 if ((nvspPacket
->Header
.MessageType
== NvspMessageTypeInitComplete
) ||
861 (nvspPacket
->Header
.MessageType
==
862 NvspMessage1TypeSendReceiveBufferComplete
) ||
863 (nvspPacket
->Header
.MessageType
==
864 NvspMessage1TypeSendSendBufferComplete
)) {
865 /* Copy the response back */
866 memcpy(&netDevice
->ChannelInitPacket
, nvspPacket
,
867 sizeof(struct nvsp_message
));
868 osd_WaitEventSet(netDevice
->ChannelInitEvent
);
869 } else if (nvspPacket
->Header
.MessageType
==
870 NvspMessage1TypeSendRNDISPacketComplete
) {
871 /* Get the send context */
872 nvscPacket
= (struct hv_netvsc_packet
*)(unsigned long)Packet
->TransactionId
;
873 /* ASSERT(nvscPacket); */
875 /* Notify the layer above us */
876 nvscPacket
->Completion
.Send
.OnSendCompletion(nvscPacket
->Completion
.Send
.SendCompletionContext
);
878 atomic_dec(&netDevice
->NumOutstandingSends
);
880 DPRINT_ERR(NETVSC
, "Unknown send completion packet type - "
881 "%d received!!", nvspPacket
->Header
.MessageType
);
884 PutNetDevice(Device
);
887 static int NetVscOnSend(struct hv_device
*Device
,
888 struct hv_netvsc_packet
*Packet
)
890 struct netvsc_device
*netDevice
;
893 struct nvsp_message sendMessage
;
895 netDevice
= GetOutboundNetDevice(Device
);
897 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
898 "ignoring outbound packets", netDevice
);
902 sendMessage
.Header
.MessageType
= NvspMessage1TypeSendRNDISPacket
;
903 if (Packet
->IsDataPacket
) {
905 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 0;
907 /* 1 is RMC_CONTROL; */
908 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 1;
911 /* Not using send buffer section */
912 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionIndex
= 0xFFFFFFFF;
913 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionSize
= 0;
915 if (Packet
->PageBufferCount
) {
916 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacketPageBuffer(
917 Device
, Packet
->PageBuffers
,
918 Packet
->PageBufferCount
,
920 sizeof(struct nvsp_message
),
921 (unsigned long)Packet
);
923 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
925 sizeof(struct nvsp_message
),
926 (unsigned long)Packet
,
927 VmbusPacketTypeDataInBand
,
928 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
933 DPRINT_ERR(NETVSC
, "Unable to send packet %p ret %d",
936 atomic_inc(&netDevice
->NumOutstandingSends
);
937 PutNetDevice(Device
);
941 static void NetVscOnReceive(struct hv_device
*Device
,
942 struct vmpacket_descriptor
*Packet
)
944 struct netvsc_device
*netDevice
;
945 struct vmtransfer_page_packet_header
*vmxferpagePacket
;
946 struct nvsp_message
*nvspPacket
;
947 struct hv_netvsc_packet
*netvscPacket
= NULL
;
949 unsigned long end
, endVirtual
;
950 /* struct netvsc_driver *netvscDriver; */
951 struct xferpage_packet
*xferpagePacket
= NULL
;
953 int count
= 0, bytesRemain
= 0;
957 netDevice
= GetInboundNetDevice(Device
);
959 DPRINT_ERR(NETVSC
, "unable to get net device..."
960 "device being destroyed?");
965 * All inbound packets other than send completion should be xfer page
968 if (Packet
->Type
!= VmbusPacketTypeDataUsingTransferPages
) {
969 DPRINT_ERR(NETVSC
, "Unknown packet type received - %d",
971 PutNetDevice(Device
);
975 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+
976 (Packet
->DataOffset8
<< 3));
978 /* Make sure this is a valid nvsp packet */
979 if (nvspPacket
->Header
.MessageType
!= NvspMessage1TypeSendRNDISPacket
) {
980 DPRINT_ERR(NETVSC
, "Unknown nvsp packet type received - %d",
981 nvspPacket
->Header
.MessageType
);
982 PutNetDevice(Device
);
986 DPRINT_DBG(NETVSC
, "NVSP packet received - type %d",
987 nvspPacket
->Header
.MessageType
);
989 vmxferpagePacket
= (struct vmtransfer_page_packet_header
*)Packet
;
991 if (vmxferpagePacket
->TransferPageSetId
!= NETVSC_RECEIVE_BUFFER_ID
) {
992 DPRINT_ERR(NETVSC
, "Invalid xfer page set id - "
993 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
994 vmxferpagePacket
->TransferPageSetId
);
995 PutNetDevice(Device
);
999 DPRINT_DBG(NETVSC
, "xfer page - range count %d",
1000 vmxferpagePacket
->RangeCount
);
1003 * Grab free packets (range count + 1) to represent this xfer
1004 * page packet. +1 to represent the xfer page packet itself.
1005 * We grab it here so that we know exactly how many we can
1008 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1009 while (!list_empty(&netDevice
->ReceivePacketList
)) {
1010 list_move_tail(netDevice
->ReceivePacketList
.next
, &listHead
);
1011 if (++count
== vmxferpagePacket
->RangeCount
+ 1)
1014 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1017 * We need at least 2 netvsc pkts (1 to represent the xfer
1018 * page and at least 1 for the range) i.e. we can handled
1019 * some of the xfer page packet ranges...
1022 DPRINT_ERR(NETVSC
, "Got only %d netvsc pkt...needed %d pkts. "
1023 "Dropping this xfer page packet completely!",
1024 count
, vmxferpagePacket
->RangeCount
+ 1);
1026 /* Return it to the freelist */
1027 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1028 for (i
= count
; i
!= 0; i
--) {
1029 list_move_tail(listHead
.next
,
1030 &netDevice
->ReceivePacketList
);
1032 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
,
1035 NetVscSendReceiveCompletion(Device
,
1036 vmxferpagePacket
->d
.TransactionId
);
1038 PutNetDevice(Device
);
1042 /* Remove the 1st packet to represent the xfer page packet itself */
1043 xferpagePacket
= (struct xferpage_packet
*)listHead
.next
;
1044 list_del(&xferpagePacket
->ListEntry
);
1046 /* This is how much we can satisfy */
1047 xferpagePacket
->Count
= count
- 1;
1048 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1049 /* vmxferpagePacket->RangeCount); */
1051 if (xferpagePacket
->Count
!= vmxferpagePacket
->RangeCount
) {
1052 DPRINT_INFO(NETVSC
, "Needed %d netvsc pkts to satisy this xfer "
1053 "page...got %d", vmxferpagePacket
->RangeCount
,
1054 xferpagePacket
->Count
);
1057 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1058 for (i
= 0; i
< (count
- 1); i
++) {
1059 netvscPacket
= (struct hv_netvsc_packet
*)listHead
.next
;
1060 list_del(&netvscPacket
->ListEntry
);
1062 /* Initialize the netvsc packet */
1063 netvscPacket
->XferPagePacket
= xferpagePacket
;
1064 netvscPacket
->Completion
.Recv
.OnReceiveCompletion
=
1065 NetVscOnReceiveCompletion
;
1066 netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
=
1068 netvscPacket
->Device
= Device
;
1069 /* Save this so that we can send it back */
1070 netvscPacket
->Completion
.Recv
.ReceiveCompletionTid
=
1071 vmxferpagePacket
->d
.TransactionId
;
1073 netvscPacket
->TotalDataBufferLength
=
1074 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1075 netvscPacket
->PageBufferCount
= 1;
1077 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1078 /* vmxferpagePacket->Ranges[i].ByteCount < */
1079 /* netDevice->ReceiveBufferSize); */
1081 netvscPacket
->PageBuffers
[0].Length
=
1082 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1084 start
= virt_to_phys((void *)((unsigned long)netDevice
->ReceiveBuffer
+ vmxferpagePacket
->Ranges
[i
].ByteOffset
));
1086 netvscPacket
->PageBuffers
[0].Pfn
= start
>> PAGE_SHIFT
;
1087 endVirtual
= (unsigned long)netDevice
->ReceiveBuffer
1088 + vmxferpagePacket
->Ranges
[i
].ByteOffset
1089 + vmxferpagePacket
->Ranges
[i
].ByteCount
- 1;
1090 end
= virt_to_phys((void *)endVirtual
);
1092 /* Calculate the page relative offset */
1093 netvscPacket
->PageBuffers
[0].Offset
=
1094 vmxferpagePacket
->Ranges
[i
].ByteOffset
& (PAGE_SIZE
- 1);
1095 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1096 /* Handle frame across multiple pages: */
1097 netvscPacket
->PageBuffers
[0].Length
=
1098 (netvscPacket
->PageBuffers
[0].Pfn
<< PAGE_SHIFT
)
1099 + PAGE_SIZE
- start
;
1100 bytesRemain
= netvscPacket
->TotalDataBufferLength
-
1101 netvscPacket
->PageBuffers
[0].Length
;
1102 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1103 netvscPacket
->PageBuffers
[j
].Offset
= 0;
1104 if (bytesRemain
<= PAGE_SIZE
) {
1105 netvscPacket
->PageBuffers
[j
].Length
= bytesRemain
;
1108 netvscPacket
->PageBuffers
[j
].Length
= PAGE_SIZE
;
1109 bytesRemain
-= PAGE_SIZE
;
1111 netvscPacket
->PageBuffers
[j
].Pfn
=
1112 virt_to_phys((void *)(endVirtual
- bytesRemain
)) >> PAGE_SHIFT
;
1113 netvscPacket
->PageBufferCount
++;
1114 if (bytesRemain
== 0)
1117 /* ASSERT(bytesRemain == 0); */
1119 DPRINT_DBG(NETVSC
, "[%d] - (abs offset %u len %u) => "
1120 "(pfn %llx, offset %u, len %u)", i
,
1121 vmxferpagePacket
->Ranges
[i
].ByteOffset
,
1122 vmxferpagePacket
->Ranges
[i
].ByteCount
,
1123 netvscPacket
->PageBuffers
[0].Pfn
,
1124 netvscPacket
->PageBuffers
[0].Offset
,
1125 netvscPacket
->PageBuffers
[0].Length
);
1127 /* Pass it to the upper layer */
1128 ((struct netvsc_driver
*)Device
->Driver
)->OnReceiveCallback(Device
, netvscPacket
);
1130 NetVscOnReceiveCompletion(netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
);
1133 /* ASSERT(list_empty(&listHead)); */
1135 PutNetDevice(Device
);
1138 static void NetVscSendReceiveCompletion(struct hv_device
*Device
,
1141 struct nvsp_message recvcompMessage
;
1145 DPRINT_DBG(NETVSC
, "Sending receive completion pkt - %llx",
1148 recvcompMessage
.Header
.MessageType
=
1149 NvspMessage1TypeSendRNDISPacketComplete
;
1151 recvcompMessage
.Messages
.Version1Messages
.SendRNDISPacketComplete
.Status
= NvspStatusSuccess
;
1154 /* Send the completion */
1155 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
1157 sizeof(struct nvsp_message
),
1159 VmbusPacketTypeCompletion
, 0);
1163 } else if (ret
== -1) {
1164 /* no more room...wait a bit and attempt to retry 3 times */
1166 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt "
1167 "(tid %llx)...retrying %d", TransactionId
, retries
);
1171 goto retry_send_cmplt
;
1173 DPRINT_ERR(NETVSC
, "unable to send receive completion "
1174 "pkt (tid %llx)...give up retrying",
1178 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt - "
1179 "%llx", TransactionId
);
1183 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1184 static void NetVscOnReceiveCompletion(void *Context
)
1186 struct hv_netvsc_packet
*packet
= Context
;
1187 struct hv_device
*device
= (struct hv_device
*)packet
->Device
;
1188 struct netvsc_device
*netDevice
;
1189 u64 transactionId
= 0;
1190 bool fSendReceiveComp
= false;
1191 unsigned long flags
;
1193 /* ASSERT(packet->XferPagePacket); */
1196 * Even though it seems logical to do a GetOutboundNetDevice() here to
1197 * send out receive completion, we are using GetInboundNetDevice()
1198 * since we may have disable outbound traffic already.
1200 netDevice
= GetInboundNetDevice(device
);
1202 DPRINT_ERR(NETVSC
, "unable to get net device..."
1203 "device being destroyed?");
1207 /* Overloading use of the lock. */
1208 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1210 /* ASSERT(packet->XferPagePacket->Count > 0); */
1211 packet
->XferPagePacket
->Count
--;
1214 * Last one in the line that represent 1 xfer page packet.
1215 * Return the xfer page packet itself to the freelist
1217 if (packet
->XferPagePacket
->Count
== 0) {
1218 fSendReceiveComp
= true;
1219 transactionId
= packet
->Completion
.Recv
.ReceiveCompletionTid
;
1220 list_add_tail(&packet
->XferPagePacket
->ListEntry
,
1221 &netDevice
->ReceivePacketList
);
1225 /* Put the packet back */
1226 list_add_tail(&packet
->ListEntry
, &netDevice
->ReceivePacketList
);
1227 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1229 /* Send a receive completion for the xfer page packet */
1230 if (fSendReceiveComp
)
1231 NetVscSendReceiveCompletion(device
, transactionId
);
1233 PutNetDevice(device
);
1236 static void NetVscOnChannelCallback(void *Context
)
1239 struct hv_device
*device
= Context
;
1240 struct netvsc_device
*netDevice
;
1243 unsigned char *packet
;
1244 struct vmpacket_descriptor
*desc
;
1245 unsigned char *buffer
;
1246 int bufferlen
= NETVSC_PACKET_SIZE
;
1248 /* ASSERT(device); */
1250 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
1256 netDevice
= GetInboundNetDevice(device
);
1258 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
1259 "ignoring inbound packets", netDevice
);
1264 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacketRaw(
1265 device
, buffer
, bufferlen
,
1266 &bytesRecvd
, &requestId
);
1268 if (bytesRecvd
> 0) {
1269 DPRINT_DBG(NETVSC
, "receive %d bytes, tid %llx",
1270 bytesRecvd
, requestId
);
1272 desc
= (struct vmpacket_descriptor
*)buffer
;
1273 switch (desc
->Type
) {
1274 case VmbusPacketTypeCompletion
:
1275 NetVscOnSendCompletion(device
, desc
);
1278 case VmbusPacketTypeDataUsingTransferPages
:
1279 NetVscOnReceive(device
, desc
);
1284 "unhandled packet type %d, "
1285 "tid %llx len %d\n",
1286 desc
->Type
, requestId
,
1292 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1295 bufferlen
= NETVSC_PACKET_SIZE
;
1299 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1302 bufferlen
= NETVSC_PACKET_SIZE
;
1307 } else if (ret
== -2) {
1308 /* Handle large packet */
1309 buffer
= kmalloc(bytesRecvd
, GFP_ATOMIC
);
1310 if (buffer
== NULL
) {
1311 /* Try again next time around */
1313 "unable to allocate buffer of size "
1314 "(%d)!!", bytesRecvd
);
1318 bufferlen
= bytesRecvd
;
1322 PutNetDevice(device
);