2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
23 #include <linux/delay.h>
25 #include <linux/slab.h>
29 #include "rndis_filter.h"
33 static const char *gDriverName
= "netvsc";
35 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
36 static const struct hv_guid gNetVscDeviceType
= {
38 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
39 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
43 static int NetVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
);
45 static int NetVscOnDeviceRemove(struct hv_device
*Device
);
47 static void NetVscOnCleanup(struct hv_driver
*Driver
);
49 static void NetVscOnChannelCallback(void *context
);
51 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device
*Device
);
53 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device
*Device
);
55 static int NetVscDestroySendBuffer(struct netvsc_device
*NetDevice
);
57 static int NetVscDestroyReceiveBuffer(struct netvsc_device
*NetDevice
);
59 static int NetVscConnectToVsp(struct hv_device
*Device
);
61 static void NetVscOnSendCompletion(struct hv_device
*Device
,
62 struct vmpacket_descriptor
*Packet
);
64 static int NetVscOnSend(struct hv_device
*Device
,
65 struct hv_netvsc_packet
*Packet
);
67 static void NetVscOnReceive(struct hv_device
*Device
,
68 struct vmpacket_descriptor
*Packet
);
70 static void NetVscOnReceiveCompletion(void *Context
);
72 static void NetVscSendReceiveCompletion(struct hv_device
*Device
,
76 static struct netvsc_device
*AllocNetDevice(struct hv_device
*Device
)
78 struct netvsc_device
*netDevice
;
80 netDevice
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
84 /* Set to 2 to allow both inbound and outbound traffic */
85 atomic_cmpxchg(&netDevice
->RefCount
, 0, 2);
87 netDevice
->Device
= Device
;
88 Device
->Extension
= netDevice
;
93 static void FreeNetDevice(struct netvsc_device
*Device
)
95 WARN_ON(atomic_read(&Device
->RefCount
) == 0);
96 Device
->Device
->Extension
= NULL
;
101 /* Get the net device object iff exists and its refcount > 1 */
102 static struct netvsc_device
*GetOutboundNetDevice(struct hv_device
*Device
)
104 struct netvsc_device
*netDevice
;
106 netDevice
= Device
->Extension
;
107 if (netDevice
&& atomic_read(&netDevice
->RefCount
) > 1)
108 atomic_inc(&netDevice
->RefCount
);
115 /* Get the net device object iff exists and its refcount > 0 */
116 static struct netvsc_device
*GetInboundNetDevice(struct hv_device
*Device
)
118 struct netvsc_device
*netDevice
;
120 netDevice
= Device
->Extension
;
121 if (netDevice
&& atomic_read(&netDevice
->RefCount
))
122 atomic_inc(&netDevice
->RefCount
);
129 static void PutNetDevice(struct hv_device
*Device
)
131 struct netvsc_device
*netDevice
;
133 netDevice
= Device
->Extension
;
134 /* ASSERT(netDevice); */
136 atomic_dec(&netDevice
->RefCount
);
139 static struct netvsc_device
*ReleaseOutboundNetDevice(struct hv_device
*Device
)
141 struct netvsc_device
*netDevice
;
143 netDevice
= Device
->Extension
;
144 if (netDevice
== NULL
)
147 /* Busy wait until the ref drop to 2, then set it to 1 */
148 while (atomic_cmpxchg(&netDevice
->RefCount
, 2, 1) != 2)
154 static struct netvsc_device
*ReleaseInboundNetDevice(struct hv_device
*Device
)
156 struct netvsc_device
*netDevice
;
158 netDevice
= Device
->Extension
;
159 if (netDevice
== NULL
)
162 /* Busy wait until the ref drop to 1, then set it to 0 */
163 while (atomic_cmpxchg(&netDevice
->RefCount
, 1, 0) != 1)
166 Device
->Extension
= NULL
;
171 * NetVscInitialize - Main entry point
173 int NetVscInitialize(struct hv_driver
*drv
)
175 struct netvsc_driver
*driver
= (struct netvsc_driver
*)drv
;
177 DPRINT_DBG(NETVSC
, "sizeof(struct hv_netvsc_packet)=%zd, "
178 "sizeof(struct nvsp_message)=%zd, "
179 "sizeof(struct vmtransfer_page_packet_header)=%zd",
180 sizeof(struct hv_netvsc_packet
),
181 sizeof(struct nvsp_message
),
182 sizeof(struct vmtransfer_page_packet_header
));
184 /* Make sure we are at least 2 pages since 1 page is used for control */
185 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
187 drv
->name
= gDriverName
;
188 memcpy(&drv
->deviceType
, &gNetVscDeviceType
, sizeof(struct hv_guid
));
190 /* Make sure it is set by the caller */
191 /* FIXME: These probably should still be tested in some way */
192 /* ASSERT(driver->OnReceiveCallback); */
193 /* ASSERT(driver->OnLinkStatusChanged); */
195 /* Setup the dispatch table */
196 driver
->Base
.OnDeviceAdd
= NetVscOnDeviceAdd
;
197 driver
->Base
.OnDeviceRemove
= NetVscOnDeviceRemove
;
198 driver
->Base
.OnCleanup
= NetVscOnCleanup
;
200 driver
->OnSend
= NetVscOnSend
;
202 RndisFilterInit(driver
);
206 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device
*Device
)
209 struct netvsc_device
*netDevice
;
210 struct nvsp_message
*initPacket
;
212 netDevice
= GetOutboundNetDevice(Device
);
214 DPRINT_ERR(NETVSC
, "unable to get net device..."
215 "device being destroyed?");
218 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
219 /* page-size grandularity */
220 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
222 netDevice
->ReceiveBuffer
=
223 osd_PageAlloc(netDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
224 if (!netDevice
->ReceiveBuffer
) {
226 "unable to allocate receive buffer of size %d",
227 netDevice
->ReceiveBufferSize
);
231 /* page-aligned buffer */
232 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
235 DPRINT_INFO(NETVSC
, "Establishing receive buffer's GPADL...");
238 * Establish the gpadl handle for this buffer on this
239 * channel. Note: This call uses the vmbus connection rather
240 * than the channel to establish the gpadl handle.
242 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
243 netDevice
->ReceiveBuffer
,
244 netDevice
->ReceiveBufferSize
,
245 &netDevice
->ReceiveBufferGpadlHandle
);
248 "unable to establish receive buffer's gpadl");
252 /* osd_WaitEventWait(ext->ChannelInitEvent); */
254 /* Notify the NetVsp of the gpadl handle */
255 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendReceiveBuffer...");
257 initPacket
= &netDevice
->ChannelInitPacket
;
259 memset(initPacket
, 0, sizeof(struct nvsp_message
));
261 initPacket
->Header
.MessageType
= NvspMessage1TypeSendReceiveBuffer
;
262 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->ReceiveBufferGpadlHandle
;
263 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
265 /* Send the gpadl notification request */
266 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
268 sizeof(struct nvsp_message
),
269 (unsigned long)initPacket
,
270 VmbusPacketTypeDataInBand
,
271 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
274 "unable to send receive buffer's gpadl to netvsp");
278 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
280 /* Check the response */
281 if (initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
!= NvspStatusSuccess
) {
282 DPRINT_ERR(NETVSC
, "Unable to complete receive buffer "
283 "initialzation with NetVsp - status %d",
284 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Status
);
289 /* Parse the response */
290 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
291 /* ASSERT(netDevice->ReceiveSections == NULL); */
293 netDevice
->ReceiveSectionCount
= initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.NumSections
;
295 netDevice
->ReceiveSections
= kmalloc(netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
296 if (netDevice
->ReceiveSections
== NULL
) {
301 memcpy(netDevice
->ReceiveSections
,
302 initPacket
->Messages
.Version1Messages
.SendReceiveBufferComplete
.Sections
,
303 netDevice
->ReceiveSectionCount
* sizeof(struct nvsp_1_receive_buffer_section
));
305 DPRINT_INFO(NETVSC
, "Receive sections info (count %d, offset %d, "
306 "endoffset %d, suballoc size %d, num suballocs %d)",
307 netDevice
->ReceiveSectionCount
,
308 netDevice
->ReceiveSections
[0].Offset
,
309 netDevice
->ReceiveSections
[0].EndOffset
,
310 netDevice
->ReceiveSections
[0].SubAllocationSize
,
311 netDevice
->ReceiveSections
[0].NumSubAllocations
);
314 * For 1st release, there should only be 1 section that represents the
315 * entire receive buffer
317 if (netDevice
->ReceiveSectionCount
!= 1 ||
318 netDevice
->ReceiveSections
->Offset
!= 0) {
326 NetVscDestroyReceiveBuffer(netDevice
);
329 PutNetDevice(Device
);
333 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device
*Device
)
336 struct netvsc_device
*netDevice
;
337 struct nvsp_message
*initPacket
;
339 netDevice
= GetOutboundNetDevice(Device
);
341 DPRINT_ERR(NETVSC
, "unable to get net device..."
342 "device being destroyed?");
345 if (netDevice
->SendBufferSize
<= 0) {
350 /* page-size grandularity */
351 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
353 netDevice
->SendBuffer
=
354 osd_PageAlloc(netDevice
->SendBufferSize
>> PAGE_SHIFT
);
355 if (!netDevice
->SendBuffer
) {
356 DPRINT_ERR(NETVSC
, "unable to allocate send buffer of size %d",
357 netDevice
->SendBufferSize
);
361 /* page-aligned buffer */
362 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
364 DPRINT_INFO(NETVSC
, "Establishing send buffer's GPADL...");
367 * Establish the gpadl handle for this buffer on this
368 * channel. Note: This call uses the vmbus connection rather
369 * than the channel to establish the gpadl handle.
371 ret
= Device
->Driver
->VmbusChannelInterface
.EstablishGpadl(Device
,
372 netDevice
->SendBuffer
,
373 netDevice
->SendBufferSize
,
374 &netDevice
->SendBufferGpadlHandle
);
376 DPRINT_ERR(NETVSC
, "unable to establish send buffer's gpadl");
380 /* osd_WaitEventWait(ext->ChannelInitEvent); */
382 /* Notify the NetVsp of the gpadl handle */
383 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendSendBuffer...");
385 initPacket
= &netDevice
->ChannelInitPacket
;
387 memset(initPacket
, 0, sizeof(struct nvsp_message
));
389 initPacket
->Header
.MessageType
= NvspMessage1TypeSendSendBuffer
;
390 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.GpadlHandle
= netDevice
->SendBufferGpadlHandle
;
391 initPacket
->Messages
.Version1Messages
.SendReceiveBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
393 /* Send the gpadl notification request */
394 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
395 initPacket
, sizeof(struct nvsp_message
),
396 (unsigned long)initPacket
,
397 VmbusPacketTypeDataInBand
,
398 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
401 "unable to send receive buffer's gpadl to netvsp");
405 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
407 /* Check the response */
408 if (initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
!= NvspStatusSuccess
) {
409 DPRINT_ERR(NETVSC
, "Unable to complete send buffer "
410 "initialzation with NetVsp - status %d",
411 initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.Status
);
416 netDevice
->SendSectionSize
= initPacket
->Messages
.Version1Messages
.SendSendBufferComplete
.SectionSize
;
421 NetVscDestroySendBuffer(netDevice
);
424 PutNetDevice(Device
);
428 static int NetVscDestroyReceiveBuffer(struct netvsc_device
*NetDevice
)
430 struct nvsp_message
*revokePacket
;
434 * If we got a section count, it means we received a
435 * SendReceiveBufferComplete msg (ie sent
436 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
437 * to send a revoke msg here
439 if (NetDevice
->ReceiveSectionCount
) {
441 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
443 /* Send the revoke receive buffer */
444 revokePacket
= &NetDevice
->RevokePacket
;
445 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
447 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeReceiveBuffer
;
448 revokePacket
->Messages
.Version1Messages
.RevokeReceiveBuffer
.Id
= NETVSC_RECEIVE_BUFFER_ID
;
450 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(
453 sizeof(struct nvsp_message
),
454 (unsigned long)revokePacket
,
455 VmbusPacketTypeDataInBand
, 0);
457 * If we failed here, we might as well return and
458 * have a leak rather than continue and a bugchk
461 DPRINT_ERR(NETVSC
, "unable to send revoke receive "
467 /* Teardown the gpadl on the vsp end */
468 if (NetDevice
->ReceiveBufferGpadlHandle
) {
469 DPRINT_INFO(NETVSC
, "Tearing down receive buffer's GPADL...");
471 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(
473 NetDevice
->ReceiveBufferGpadlHandle
);
475 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
478 "unable to teardown receive buffer's gpadl");
481 NetDevice
->ReceiveBufferGpadlHandle
= 0;
484 if (NetDevice
->ReceiveBuffer
) {
485 DPRINT_INFO(NETVSC
, "Freeing up receive buffer...");
487 /* Free up the receive buffer */
488 osd_PageFree(NetDevice
->ReceiveBuffer
,
489 NetDevice
->ReceiveBufferSize
>> PAGE_SHIFT
);
490 NetDevice
->ReceiveBuffer
= NULL
;
493 if (NetDevice
->ReceiveSections
) {
494 NetDevice
->ReceiveSectionCount
= 0;
495 kfree(NetDevice
->ReceiveSections
);
496 NetDevice
->ReceiveSections
= NULL
;
502 static int NetVscDestroySendBuffer(struct netvsc_device
*NetDevice
)
504 struct nvsp_message
*revokePacket
;
508 * If we got a section count, it means we received a
509 * SendReceiveBufferComplete msg (ie sent
510 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
511 * to send a revoke msg here
513 if (NetDevice
->SendSectionSize
) {
515 "Sending NvspMessage1TypeRevokeSendBuffer...");
517 /* Send the revoke send buffer */
518 revokePacket
= &NetDevice
->RevokePacket
;
519 memset(revokePacket
, 0, sizeof(struct nvsp_message
));
521 revokePacket
->Header
.MessageType
= NvspMessage1TypeRevokeSendBuffer
;
522 revokePacket
->Messages
.Version1Messages
.RevokeSendBuffer
.Id
= NETVSC_SEND_BUFFER_ID
;
524 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.SendPacket(NetDevice
->Device
,
526 sizeof(struct nvsp_message
),
527 (unsigned long)revokePacket
,
528 VmbusPacketTypeDataInBand
, 0);
530 * If we failed here, we might as well return and have a leak
531 * rather than continue and a bugchk
534 DPRINT_ERR(NETVSC
, "unable to send revoke send buffer "
540 /* Teardown the gpadl on the vsp end */
541 if (NetDevice
->SendBufferGpadlHandle
) {
542 DPRINT_INFO(NETVSC
, "Tearing down send buffer's GPADL...");
544 ret
= NetDevice
->Device
->Driver
->VmbusChannelInterface
.TeardownGpadl(NetDevice
->Device
, NetDevice
->SendBufferGpadlHandle
);
547 * If we failed here, we might as well return and have a leak
548 * rather than continue and a bugchk
551 DPRINT_ERR(NETVSC
, "unable to teardown send buffer's "
555 NetDevice
->SendBufferGpadlHandle
= 0;
558 if (NetDevice
->SendBuffer
) {
559 DPRINT_INFO(NETVSC
, "Freeing up send buffer...");
561 /* Free up the receive buffer */
562 osd_PageFree(NetDevice
->SendBuffer
,
563 NetDevice
->SendBufferSize
>> PAGE_SHIFT
);
564 NetDevice
->SendBuffer
= NULL
;
571 static int NetVscConnectToVsp(struct hv_device
*Device
)
574 struct netvsc_device
*netDevice
;
575 struct nvsp_message
*initPacket
;
578 netDevice
= GetOutboundNetDevice(Device
);
580 DPRINT_ERR(NETVSC
, "unable to get net device..."
581 "device being destroyed?");
585 initPacket
= &netDevice
->ChannelInitPacket
;
587 memset(initPacket
, 0, sizeof(struct nvsp_message
));
588 initPacket
->Header
.MessageType
= NvspMessageTypeInit
;
589 initPacket
->Messages
.InitMessages
.Init
.MinProtocolVersion
= NVSP_MIN_PROTOCOL_VERSION
;
590 initPacket
->Messages
.InitMessages
.Init
.MaxProtocolVersion
= NVSP_MAX_PROTOCOL_VERSION
;
592 DPRINT_INFO(NETVSC
, "Sending NvspMessageTypeInit...");
594 /* Send the init request */
595 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
597 sizeof(struct nvsp_message
),
598 (unsigned long)initPacket
,
599 VmbusPacketTypeDataInBand
,
600 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
603 DPRINT_ERR(NETVSC
, "unable to send NvspMessageTypeInit");
607 osd_WaitEventWait(netDevice
->ChannelInitEvent
);
609 /* Now, check the response */
610 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
611 DPRINT_INFO(NETVSC
, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
612 initPacket
->Messages
.InitMessages
.InitComplete
.Status
,
613 initPacket
->Messages
.InitMessages
.InitComplete
.MaximumMdlChainLength
);
615 if (initPacket
->Messages
.InitMessages
.InitComplete
.Status
!=
618 "unable to initialize with netvsp (status 0x%x)",
619 initPacket
->Messages
.InitMessages
.InitComplete
.Status
);
624 if (initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
!= NVSP_PROTOCOL_VERSION_1
) {
625 DPRINT_ERR(NETVSC
, "unable to initialize with netvsp "
626 "(version expected 1 got %d)",
627 initPacket
->Messages
.InitMessages
.InitComplete
.NegotiatedProtocolVersion
);
631 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendNdisVersion...");
633 /* Send the ndis version */
634 memset(initPacket
, 0, sizeof(struct nvsp_message
));
636 ndisVersion
= 0x00050000;
638 initPacket
->Header
.MessageType
= NvspMessage1TypeSendNdisVersion
;
639 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMajorVersion
=
640 (ndisVersion
& 0xFFFF0000) >> 16;
641 initPacket
->Messages
.Version1Messages
.SendNdisVersion
.NdisMinorVersion
=
642 ndisVersion
& 0xFFFF;
644 /* Send the init request */
645 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
647 sizeof(struct nvsp_message
),
648 (unsigned long)initPacket
,
649 VmbusPacketTypeDataInBand
, 0);
652 "unable to send NvspMessage1TypeSendNdisVersion");
657 * BUGBUG - We have to wait for the above msg since the
658 * netvsp uses KMCL which acknowledges packet (completion
659 * packet) since our Vmbus always set the
660 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
662 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
664 /* Post the big receive buffer to NetVSP */
665 ret
= NetVscInitializeReceiveBufferWithNetVsp(Device
);
667 ret
= NetVscInitializeSendBufferWithNetVsp(Device
);
670 PutNetDevice(Device
);
674 static void NetVscDisconnectFromVsp(struct netvsc_device
*NetDevice
)
676 NetVscDestroyReceiveBuffer(NetDevice
);
677 NetVscDestroySendBuffer(NetDevice
);
681 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
683 static int NetVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
687 struct netvsc_device
*netDevice
;
688 struct hv_netvsc_packet
*packet
, *pos
;
689 struct netvsc_driver
*netDriver
=
690 (struct netvsc_driver
*)Device
->Driver
;
692 netDevice
= AllocNetDevice(Device
);
698 DPRINT_DBG(NETVSC
, "netvsc channel object allocated - %p", netDevice
);
700 /* Initialize the NetVSC channel extension */
701 netDevice
->ReceiveBufferSize
= NETVSC_RECEIVE_BUFFER_SIZE
;
702 spin_lock_init(&netDevice
->receive_packet_list_lock
);
704 netDevice
->SendBufferSize
= NETVSC_SEND_BUFFER_SIZE
;
706 INIT_LIST_HEAD(&netDevice
->ReceivePacketList
);
708 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
709 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
710 (NETVSC_RECEIVE_SG_COUNT
*
711 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
713 DPRINT_DBG(NETVSC
, "unable to allocate netvsc pkts "
714 "for receive pool (wanted %d got %d)",
715 NETVSC_RECEIVE_PACKETLIST_COUNT
, i
);
718 list_add_tail(&packet
->ListEntry
,
719 &netDevice
->ReceivePacketList
);
721 netDevice
->ChannelInitEvent
= osd_WaitEventCreate();
722 if (!netDevice
->ChannelInitEvent
) {
727 /* Open the channel */
728 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
729 netDriver
->RingBufferSize
,
730 netDriver
->RingBufferSize
,
732 NetVscOnChannelCallback
,
736 DPRINT_ERR(NETVSC
, "unable to open channel: %d", ret
);
741 /* Channel is opened */
742 DPRINT_INFO(NETVSC
, "*** NetVSC channel opened successfully! ***");
744 /* Connect with the NetVsp */
745 ret
= NetVscConnectToVsp(Device
);
747 DPRINT_ERR(NETVSC
, "unable to connect to NetVSP - %d", ret
);
752 DPRINT_INFO(NETVSC
, "*** NetVSC channel handshake result - %d ***",
758 /* Now, we can close the channel safely */
759 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
764 kfree(netDevice
->ChannelInitEvent
);
766 list_for_each_entry_safe(packet
, pos
,
767 &netDevice
->ReceivePacketList
,
769 list_del(&packet
->ListEntry
);
773 ReleaseOutboundNetDevice(Device
);
774 ReleaseInboundNetDevice(Device
);
776 FreeNetDevice(netDevice
);
783 * NetVscOnDeviceRemove - Callback when the root bus device is removed
785 static int NetVscOnDeviceRemove(struct hv_device
*Device
)
787 struct netvsc_device
*netDevice
;
788 struct hv_netvsc_packet
*netvscPacket
, *pos
;
790 DPRINT_INFO(NETVSC
, "Disabling outbound traffic on net device (%p)...",
793 /* Stop outbound traffic ie sends and receives completions */
794 netDevice
= ReleaseOutboundNetDevice(Device
);
796 DPRINT_ERR(NETVSC
, "No net device present!!");
800 /* Wait for all send completions */
801 while (atomic_read(&netDevice
->NumOutstandingSends
)) {
802 DPRINT_INFO(NETVSC
, "waiting for %d requests to complete...",
803 atomic_read(&netDevice
->NumOutstandingSends
));
807 DPRINT_INFO(NETVSC
, "Disconnecting from netvsp...");
809 NetVscDisconnectFromVsp(netDevice
);
811 DPRINT_INFO(NETVSC
, "Disabling inbound traffic on net device (%p)...",
814 /* Stop inbound traffic ie receives and sends completions */
815 netDevice
= ReleaseInboundNetDevice(Device
);
817 /* At this point, no one should be accessing netDevice except in here */
818 DPRINT_INFO(NETVSC
, "net device (%p) safe to remove", netDevice
);
820 /* Now, we can close the channel safely */
821 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
823 /* Release all resources */
824 list_for_each_entry_safe(netvscPacket
, pos
,
825 &netDevice
->ReceivePacketList
, ListEntry
) {
826 list_del(&netvscPacket
->ListEntry
);
830 kfree(netDevice
->ChannelInitEvent
);
831 FreeNetDevice(netDevice
);
836 * NetVscOnCleanup - Perform any cleanup when the driver is removed
838 static void NetVscOnCleanup(struct hv_driver
*drv
)
842 static void NetVscOnSendCompletion(struct hv_device
*Device
,
843 struct vmpacket_descriptor
*Packet
)
845 struct netvsc_device
*netDevice
;
846 struct nvsp_message
*nvspPacket
;
847 struct hv_netvsc_packet
*nvscPacket
;
849 netDevice
= GetInboundNetDevice(Device
);
851 DPRINT_ERR(NETVSC
, "unable to get net device..."
852 "device being destroyed?");
856 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+ (Packet
->DataOffset8
<< 3));
858 DPRINT_DBG(NETVSC
, "send completion packet - type %d",
859 nvspPacket
->Header
.MessageType
);
861 if ((nvspPacket
->Header
.MessageType
== NvspMessageTypeInitComplete
) ||
862 (nvspPacket
->Header
.MessageType
==
863 NvspMessage1TypeSendReceiveBufferComplete
) ||
864 (nvspPacket
->Header
.MessageType
==
865 NvspMessage1TypeSendSendBufferComplete
)) {
866 /* Copy the response back */
867 memcpy(&netDevice
->ChannelInitPacket
, nvspPacket
,
868 sizeof(struct nvsp_message
));
869 osd_WaitEventSet(netDevice
->ChannelInitEvent
);
870 } else if (nvspPacket
->Header
.MessageType
==
871 NvspMessage1TypeSendRNDISPacketComplete
) {
872 /* Get the send context */
873 nvscPacket
= (struct hv_netvsc_packet
*)(unsigned long)Packet
->TransactionId
;
874 /* ASSERT(nvscPacket); */
876 /* Notify the layer above us */
877 nvscPacket
->Completion
.Send
.OnSendCompletion(nvscPacket
->Completion
.Send
.SendCompletionContext
);
879 atomic_dec(&netDevice
->NumOutstandingSends
);
881 DPRINT_ERR(NETVSC
, "Unknown send completion packet type - "
882 "%d received!!", nvspPacket
->Header
.MessageType
);
885 PutNetDevice(Device
);
888 static int NetVscOnSend(struct hv_device
*Device
,
889 struct hv_netvsc_packet
*Packet
)
891 struct netvsc_device
*netDevice
;
894 struct nvsp_message sendMessage
;
896 netDevice
= GetOutboundNetDevice(Device
);
898 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
899 "ignoring outbound packets", netDevice
);
903 sendMessage
.Header
.MessageType
= NvspMessage1TypeSendRNDISPacket
;
904 if (Packet
->IsDataPacket
) {
906 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 0;
908 /* 1 is RMC_CONTROL; */
909 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.ChannelType
= 1;
912 /* Not using send buffer section */
913 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionIndex
= 0xFFFFFFFF;
914 sendMessage
.Messages
.Version1Messages
.SendRNDISPacket
.SendBufferSectionSize
= 0;
916 if (Packet
->PageBufferCount
) {
917 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacketPageBuffer(
918 Device
, Packet
->PageBuffers
,
919 Packet
->PageBufferCount
,
921 sizeof(struct nvsp_message
),
922 (unsigned long)Packet
);
924 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
926 sizeof(struct nvsp_message
),
927 (unsigned long)Packet
,
928 VmbusPacketTypeDataInBand
,
929 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
934 DPRINT_ERR(NETVSC
, "Unable to send packet %p ret %d",
937 atomic_inc(&netDevice
->NumOutstandingSends
);
938 PutNetDevice(Device
);
942 static void NetVscOnReceive(struct hv_device
*Device
,
943 struct vmpacket_descriptor
*Packet
)
945 struct netvsc_device
*netDevice
;
946 struct vmtransfer_page_packet_header
*vmxferpagePacket
;
947 struct nvsp_message
*nvspPacket
;
948 struct hv_netvsc_packet
*netvscPacket
= NULL
;
950 unsigned long end
, endVirtual
;
951 /* struct netvsc_driver *netvscDriver; */
952 struct xferpage_packet
*xferpagePacket
= NULL
;
954 int count
= 0, bytesRemain
= 0;
958 netDevice
= GetInboundNetDevice(Device
);
960 DPRINT_ERR(NETVSC
, "unable to get net device..."
961 "device being destroyed?");
966 * All inbound packets other than send completion should be xfer page
969 if (Packet
->Type
!= VmbusPacketTypeDataUsingTransferPages
) {
970 DPRINT_ERR(NETVSC
, "Unknown packet type received - %d",
972 PutNetDevice(Device
);
976 nvspPacket
= (struct nvsp_message
*)((unsigned long)Packet
+
977 (Packet
->DataOffset8
<< 3));
979 /* Make sure this is a valid nvsp packet */
980 if (nvspPacket
->Header
.MessageType
!= NvspMessage1TypeSendRNDISPacket
) {
981 DPRINT_ERR(NETVSC
, "Unknown nvsp packet type received - %d",
982 nvspPacket
->Header
.MessageType
);
983 PutNetDevice(Device
);
987 DPRINT_DBG(NETVSC
, "NVSP packet received - type %d",
988 nvspPacket
->Header
.MessageType
);
990 vmxferpagePacket
= (struct vmtransfer_page_packet_header
*)Packet
;
992 if (vmxferpagePacket
->TransferPageSetId
!= NETVSC_RECEIVE_BUFFER_ID
) {
993 DPRINT_ERR(NETVSC
, "Invalid xfer page set id - "
994 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
995 vmxferpagePacket
->TransferPageSetId
);
996 PutNetDevice(Device
);
1000 DPRINT_DBG(NETVSC
, "xfer page - range count %d",
1001 vmxferpagePacket
->RangeCount
);
1004 * Grab free packets (range count + 1) to represent this xfer
1005 * page packet. +1 to represent the xfer page packet itself.
1006 * We grab it here so that we know exactly how many we can
1009 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1010 while (!list_empty(&netDevice
->ReceivePacketList
)) {
1011 list_move_tail(netDevice
->ReceivePacketList
.next
, &listHead
);
1012 if (++count
== vmxferpagePacket
->RangeCount
+ 1)
1015 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1018 * We need at least 2 netvsc pkts (1 to represent the xfer
1019 * page and at least 1 for the range) i.e. we can handled
1020 * some of the xfer page packet ranges...
1023 DPRINT_ERR(NETVSC
, "Got only %d netvsc pkt...needed %d pkts. "
1024 "Dropping this xfer page packet completely!",
1025 count
, vmxferpagePacket
->RangeCount
+ 1);
1027 /* Return it to the freelist */
1028 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1029 for (i
= count
; i
!= 0; i
--) {
1030 list_move_tail(listHead
.next
,
1031 &netDevice
->ReceivePacketList
);
1033 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
,
1036 NetVscSendReceiveCompletion(Device
,
1037 vmxferpagePacket
->d
.TransactionId
);
1039 PutNetDevice(Device
);
1043 /* Remove the 1st packet to represent the xfer page packet itself */
1044 xferpagePacket
= (struct xferpage_packet
*)listHead
.next
;
1045 list_del(&xferpagePacket
->ListEntry
);
1047 /* This is how much we can satisfy */
1048 xferpagePacket
->Count
= count
- 1;
1049 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1050 /* vmxferpagePacket->RangeCount); */
1052 if (xferpagePacket
->Count
!= vmxferpagePacket
->RangeCount
) {
1053 DPRINT_INFO(NETVSC
, "Needed %d netvsc pkts to satisy this xfer "
1054 "page...got %d", vmxferpagePacket
->RangeCount
,
1055 xferpagePacket
->Count
);
1058 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1059 for (i
= 0; i
< (count
- 1); i
++) {
1060 netvscPacket
= (struct hv_netvsc_packet
*)listHead
.next
;
1061 list_del(&netvscPacket
->ListEntry
);
1063 /* Initialize the netvsc packet */
1064 netvscPacket
->XferPagePacket
= xferpagePacket
;
1065 netvscPacket
->Completion
.Recv
.OnReceiveCompletion
=
1066 NetVscOnReceiveCompletion
;
1067 netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
=
1069 netvscPacket
->Device
= Device
;
1070 /* Save this so that we can send it back */
1071 netvscPacket
->Completion
.Recv
.ReceiveCompletionTid
=
1072 vmxferpagePacket
->d
.TransactionId
;
1074 netvscPacket
->TotalDataBufferLength
=
1075 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1076 netvscPacket
->PageBufferCount
= 1;
1078 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1079 /* vmxferpagePacket->Ranges[i].ByteCount < */
1080 /* netDevice->ReceiveBufferSize); */
1082 netvscPacket
->PageBuffers
[0].Length
=
1083 vmxferpagePacket
->Ranges
[i
].ByteCount
;
1085 start
= virt_to_phys((void *)((unsigned long)netDevice
->ReceiveBuffer
+ vmxferpagePacket
->Ranges
[i
].ByteOffset
));
1087 netvscPacket
->PageBuffers
[0].Pfn
= start
>> PAGE_SHIFT
;
1088 endVirtual
= (unsigned long)netDevice
->ReceiveBuffer
1089 + vmxferpagePacket
->Ranges
[i
].ByteOffset
1090 + vmxferpagePacket
->Ranges
[i
].ByteCount
- 1;
1091 end
= virt_to_phys((void *)endVirtual
);
1093 /* Calculate the page relative offset */
1094 netvscPacket
->PageBuffers
[0].Offset
=
1095 vmxferpagePacket
->Ranges
[i
].ByteOffset
& (PAGE_SIZE
- 1);
1096 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1097 /* Handle frame across multiple pages: */
1098 netvscPacket
->PageBuffers
[0].Length
=
1099 (netvscPacket
->PageBuffers
[0].Pfn
<< PAGE_SHIFT
)
1100 + PAGE_SIZE
- start
;
1101 bytesRemain
= netvscPacket
->TotalDataBufferLength
-
1102 netvscPacket
->PageBuffers
[0].Length
;
1103 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1104 netvscPacket
->PageBuffers
[j
].Offset
= 0;
1105 if (bytesRemain
<= PAGE_SIZE
) {
1106 netvscPacket
->PageBuffers
[j
].Length
= bytesRemain
;
1109 netvscPacket
->PageBuffers
[j
].Length
= PAGE_SIZE
;
1110 bytesRemain
-= PAGE_SIZE
;
1112 netvscPacket
->PageBuffers
[j
].Pfn
=
1113 virt_to_phys((void *)(endVirtual
- bytesRemain
)) >> PAGE_SHIFT
;
1114 netvscPacket
->PageBufferCount
++;
1115 if (bytesRemain
== 0)
1118 /* ASSERT(bytesRemain == 0); */
1120 DPRINT_DBG(NETVSC
, "[%d] - (abs offset %u len %u) => "
1121 "(pfn %llx, offset %u, len %u)", i
,
1122 vmxferpagePacket
->Ranges
[i
].ByteOffset
,
1123 vmxferpagePacket
->Ranges
[i
].ByteCount
,
1124 netvscPacket
->PageBuffers
[0].Pfn
,
1125 netvscPacket
->PageBuffers
[0].Offset
,
1126 netvscPacket
->PageBuffers
[0].Length
);
1128 /* Pass it to the upper layer */
1129 ((struct netvsc_driver
*)Device
->Driver
)->OnReceiveCallback(Device
, netvscPacket
);
1131 NetVscOnReceiveCompletion(netvscPacket
->Completion
.Recv
.ReceiveCompletionContext
);
1134 /* ASSERT(list_empty(&listHead)); */
1136 PutNetDevice(Device
);
1139 static void NetVscSendReceiveCompletion(struct hv_device
*Device
,
1142 struct nvsp_message recvcompMessage
;
1146 DPRINT_DBG(NETVSC
, "Sending receive completion pkt - %llx",
1149 recvcompMessage
.Header
.MessageType
=
1150 NvspMessage1TypeSendRNDISPacketComplete
;
1152 /* FIXME: Pass in the status */
1153 recvcompMessage
.Messages
.Version1Messages
.SendRNDISPacketComplete
.Status
= NvspStatusSuccess
;
1156 /* Send the completion */
1157 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
1159 sizeof(struct nvsp_message
),
1161 VmbusPacketTypeCompletion
, 0);
1165 } else if (ret
== -1) {
1166 /* no more room...wait a bit and attempt to retry 3 times */
1168 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt "
1169 "(tid %llx)...retrying %d", TransactionId
, retries
);
1173 goto retry_send_cmplt
;
1175 DPRINT_ERR(NETVSC
, "unable to send receive completion "
1176 "pkt (tid %llx)...give up retrying",
1180 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt - "
1181 "%llx", TransactionId
);
1185 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1186 static void NetVscOnReceiveCompletion(void *Context
)
1188 struct hv_netvsc_packet
*packet
= Context
;
1189 struct hv_device
*device
= (struct hv_device
*)packet
->Device
;
1190 struct netvsc_device
*netDevice
;
1191 u64 transactionId
= 0;
1192 bool fSendReceiveComp
= false;
1193 unsigned long flags
;
1195 /* ASSERT(packet->XferPagePacket); */
1198 * Even though it seems logical to do a GetOutboundNetDevice() here to
1199 * send out receive completion, we are using GetInboundNetDevice()
1200 * since we may have disable outbound traffic already.
1202 netDevice
= GetInboundNetDevice(device
);
1204 DPRINT_ERR(NETVSC
, "unable to get net device..."
1205 "device being destroyed?");
1209 /* Overloading use of the lock. */
1210 spin_lock_irqsave(&netDevice
->receive_packet_list_lock
, flags
);
1212 /* ASSERT(packet->XferPagePacket->Count > 0); */
1213 packet
->XferPagePacket
->Count
--;
1216 * Last one in the line that represent 1 xfer page packet.
1217 * Return the xfer page packet itself to the freelist
1219 if (packet
->XferPagePacket
->Count
== 0) {
1220 fSendReceiveComp
= true;
1221 transactionId
= packet
->Completion
.Recv
.ReceiveCompletionTid
;
1222 list_add_tail(&packet
->XferPagePacket
->ListEntry
,
1223 &netDevice
->ReceivePacketList
);
1227 /* Put the packet back */
1228 list_add_tail(&packet
->ListEntry
, &netDevice
->ReceivePacketList
);
1229 spin_unlock_irqrestore(&netDevice
->receive_packet_list_lock
, flags
);
1231 /* Send a receive completion for the xfer page packet */
1232 if (fSendReceiveComp
)
1233 NetVscSendReceiveCompletion(device
, transactionId
);
1235 PutNetDevice(device
);
1238 static void NetVscOnChannelCallback(void *Context
)
1241 struct hv_device
*device
= Context
;
1242 struct netvsc_device
*netDevice
;
1245 unsigned char *packet
;
1246 struct vmpacket_descriptor
*desc
;
1247 unsigned char *buffer
;
1248 int bufferlen
= NETVSC_PACKET_SIZE
;
1250 /* ASSERT(device); */
1252 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
1258 netDevice
= GetInboundNetDevice(device
);
1260 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
1261 "ignoring inbound packets", netDevice
);
1266 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacketRaw(
1267 device
, buffer
, bufferlen
,
1268 &bytesRecvd
, &requestId
);
1270 if (bytesRecvd
> 0) {
1271 DPRINT_DBG(NETVSC
, "receive %d bytes, tid %llx",
1272 bytesRecvd
, requestId
);
1274 desc
= (struct vmpacket_descriptor
*)buffer
;
1275 switch (desc
->Type
) {
1276 case VmbusPacketTypeCompletion
:
1277 NetVscOnSendCompletion(device
, desc
);
1280 case VmbusPacketTypeDataUsingTransferPages
:
1281 NetVscOnReceive(device
, desc
);
1286 "unhandled packet type %d, "
1287 "tid %llx len %d\n",
1288 desc
->Type
, requestId
,
1294 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1297 bufferlen
= NETVSC_PACKET_SIZE
;
1301 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1304 bufferlen
= NETVSC_PACKET_SIZE
;
1309 } else if (ret
== -2) {
1310 /* Handle large packet */
1311 buffer
= kmalloc(bytesRecvd
, GFP_ATOMIC
);
1312 if (buffer
== NULL
) {
1313 /* Try again next time around */
1315 "unable to allocate buffer of size "
1316 "(%d)!!", bytesRecvd
);
1320 bufferlen
= bytesRecvd
;
1324 PutNetDevice(device
);