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"
34 static const char *driver_name = "netvsc";
36 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
37 static const struct hv_guid netvsc_device_type = {
39 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
40 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
44 static int netvsc_device_add(struct hv_device *device, void *additional_info);
46 static int netvsc_device_remove(struct hv_device *device);
48 static void netvsc_cleanup(struct hv_driver *driver);
50 static void netvsc_channel_cb(void *context);
52 static int netvsc_init_send_buf(struct hv_device *device);
54 static int netvsc_init_recv_buf(struct hv_device *device);
56 static int netvsc_destroy_send_buf(struct netvsc_device *net_device);
58 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device);
60 static int netvsc_connect_vsp(struct hv_device *device);
62 static void netvsc_send_completion(struct hv_device *device,
63 struct vmpacket_descriptor *packet);
65 static int netvsc_send(struct hv_device *device,
66 struct hv_netvsc_packet *packet);
68 static void netvsc_receive(struct hv_device *device,
69 struct vmpacket_descriptor *packet);
71 static void netvsc_receive_completion(void *context);
73 static void netvsc_send_recv_completion(struct hv_device *device,
77 static struct netvsc_device *alloc_net_device(struct hv_device *device)
79 struct netvsc_device *net_device;
81 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
85 /* Set to 2 to allow both inbound and outbound traffic */
86 atomic_cmpxchg(&net_device->RefCount, 0, 2);
88 net_device->Device = device;
89 device->Extension = net_device;
94 static void free_net_device(struct netvsc_device *device)
96 WARN_ON(atomic_read(&device->RefCount) == 0);
97 device->Device->Extension = NULL;
102 /* Get the net device object iff exists and its refcount > 1 */
103 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
105 struct netvsc_device *net_device;
107 net_device = device->Extension;
108 if (net_device && atomic_read(&net_device->RefCount) > 1)
109 atomic_inc(&net_device->RefCount);
116 /* Get the net device object iff exists and its refcount > 0 */
117 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
119 struct netvsc_device *net_device;
121 net_device = device->Extension;
122 if (net_device && atomic_read(&net_device->RefCount))
123 atomic_inc(&net_device->RefCount);
130 static void put_net_device(struct hv_device *device)
132 struct netvsc_device *net_device;
134 net_device = device->Extension;
135 /* ASSERT(netDevice); */
137 atomic_dec(&net_device->RefCount);
140 static struct netvsc_device *release_outbound_net_device(
141 struct hv_device *device)
143 struct netvsc_device *net_device;
145 net_device = device->Extension;
146 if (net_device == NULL)
149 /* Busy wait until the ref drop to 2, then set it to 1 */
150 while (atomic_cmpxchg(&net_device->RefCount, 2, 1) != 2)
156 static struct netvsc_device *release_inbound_net_device(
157 struct hv_device *device)
159 struct netvsc_device *net_device;
161 net_device = device->Extension;
162 if (net_device == NULL)
165 /* Busy wait until the ref drop to 1, then set it to 0 */
166 while (atomic_cmpxchg(&net_device->RefCount, 1, 0) != 1)
169 device->Extension = NULL;
174 * netvsc_initialize - Main entry point
176 int netvsc_initialize(struct hv_driver *drv)
178 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
180 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
181 "sizeof(struct nvsp_message)=%zd, "
182 "sizeof(struct vmtransfer_page_packet_header)=%zd",
183 sizeof(struct hv_netvsc_packet),
184 sizeof(struct nvsp_message),
185 sizeof(struct vmtransfer_page_packet_header));
187 /* Make sure we are at least 2 pages since 1 page is used for control */
188 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
190 drv->name = driver_name;
191 memcpy(&drv->deviceType, &netvsc_device_type, sizeof(struct hv_guid));
193 /* Make sure it is set by the caller */
194 /* FIXME: These probably should still be tested in some way */
195 /* ASSERT(driver->OnReceiveCallback); */
196 /* ASSERT(driver->OnLinkStatusChanged); */
198 /* Setup the dispatch table */
199 driver->Base.OnDeviceAdd = netvsc_device_add;
200 driver->Base.OnDeviceRemove = netvsc_device_remove;
201 driver->Base.OnCleanup = netvsc_cleanup;
203 driver->OnSend = netvsc_send;
205 RndisFilterInit(driver);
209 static int netvsc_init_recv_buf(struct hv_device *device)
212 struct netvsc_device *net_device;
213 struct nvsp_message *init_packet;
215 net_device = get_outbound_net_device(device);
217 DPRINT_ERR(NETVSC, "unable to get net device..."
218 "device being destroyed?");
221 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
222 /* page-size grandularity */
223 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
225 net_device->ReceiveBuffer =
226 osd_page_alloc(net_device->ReceiveBufferSize >> PAGE_SHIFT);
227 if (!net_device->ReceiveBuffer) {
229 "unable to allocate receive buffer of size %d",
230 net_device->ReceiveBufferSize);
234 /* page-aligned buffer */
235 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
238 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
241 * Establish the gpadl handle for this buffer on this
242 * channel. Note: This call uses the vmbus connection rather
243 * than the channel to establish the gpadl handle.
245 ret = vmbus_establish_gpadl(device->channel, net_device->ReceiveBuffer,
246 net_device->ReceiveBufferSize,
247 &net_device->ReceiveBufferGpadlHandle);
250 "unable to establish receive buffer's gpadl");
254 /* osd_waitevent_wait(ext->ChannelInitEvent); */
256 /* Notify the NetVsp of the gpadl handle */
257 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
259 init_packet = &net_device->ChannelInitPacket;
261 memset(init_packet, 0, sizeof(struct nvsp_message));
263 init_packet->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
264 init_packet->Messages.Version1Messages.SendReceiveBuffer.
265 GpadlHandle = net_device->ReceiveBufferGpadlHandle;
266 init_packet->Messages.Version1Messages.
267 SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
269 /* Send the gpadl notification request */
270 ret = vmbus_sendpacket(device->channel, init_packet,
271 sizeof(struct nvsp_message),
272 (unsigned long)init_packet,
273 VmbusPacketTypeDataInBand,
274 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
277 "unable to send receive buffer's gpadl to netvsp");
281 osd_waitevent_wait(net_device->ChannelInitEvent);
283 /* Check the response */
284 if (init_packet->Messages.Version1Messages.
285 SendReceiveBufferComplete.Status != NvspStatusSuccess) {
286 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
287 "initialzation with NetVsp - status %d",
288 init_packet->Messages.Version1Messages.
289 SendReceiveBufferComplete.Status);
294 /* Parse the response */
295 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
296 /* ASSERT(netDevice->ReceiveSections == NULL); */
298 net_device->ReceiveSectionCount = init_packet->Messages.
299 Version1Messages.SendReceiveBufferComplete.NumSections;
301 net_device->ReceiveSections = kmalloc(net_device->ReceiveSectionCount
302 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
303 if (net_device->ReceiveSections == NULL) {
308 memcpy(net_device->ReceiveSections,
309 init_packet->Messages.Version1Messages.
310 SendReceiveBufferComplete.Sections,
311 net_device->ReceiveSectionCount *
312 sizeof(struct nvsp_1_receive_buffer_section));
314 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
315 "endoffset %d, suballoc size %d, num suballocs %d)",
316 net_device->ReceiveSectionCount,
317 net_device->ReceiveSections[0].Offset,
318 net_device->ReceiveSections[0].EndOffset,
319 net_device->ReceiveSections[0].SubAllocationSize,
320 net_device->ReceiveSections[0].NumSubAllocations);
323 * For 1st release, there should only be 1 section that represents the
324 * entire receive buffer
326 if (net_device->ReceiveSectionCount != 1 ||
327 net_device->ReceiveSections->Offset != 0) {
335 netvsc_destroy_recv_buf(net_device);
338 put_net_device(device);
342 static int netvsc_init_send_buf(struct hv_device *device)
345 struct netvsc_device *net_device;
346 struct nvsp_message *init_packet;
348 net_device = get_outbound_net_device(device);
350 DPRINT_ERR(NETVSC, "unable to get net device..."
351 "device being destroyed?");
354 if (net_device->SendBufferSize <= 0) {
359 /* page-size grandularity */
360 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
362 net_device->SendBuffer =
363 osd_page_alloc(net_device->SendBufferSize >> PAGE_SHIFT);
364 if (!net_device->SendBuffer) {
365 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
366 net_device->SendBufferSize);
370 /* page-aligned buffer */
371 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
373 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
376 * Establish the gpadl handle for this buffer on this
377 * channel. Note: This call uses the vmbus connection rather
378 * than the channel to establish the gpadl handle.
380 ret = vmbus_establish_gpadl(device->channel, net_device->SendBuffer,
381 net_device->SendBufferSize,
382 &net_device->SendBufferGpadlHandle);
384 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
388 /* osd_waitevent_wait(ext->ChannelInitEvent); */
390 /* Notify the NetVsp of the gpadl handle */
391 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
393 init_packet = &net_device->ChannelInitPacket;
395 memset(init_packet, 0, sizeof(struct nvsp_message));
397 init_packet->Header.MessageType = NvspMessage1TypeSendSendBuffer;
398 init_packet->Messages.Version1Messages.SendReceiveBuffer.
399 GpadlHandle = net_device->SendBufferGpadlHandle;
400 init_packet->Messages.Version1Messages.SendReceiveBuffer.Id =
401 NETVSC_SEND_BUFFER_ID;
403 /* Send the gpadl notification request */
404 ret = vmbus_sendpacket(device->channel, init_packet,
405 sizeof(struct nvsp_message),
406 (unsigned long)init_packet,
407 VmbusPacketTypeDataInBand,
408 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
411 "unable to send receive buffer's gpadl to netvsp");
415 osd_waitevent_wait(net_device->ChannelInitEvent);
417 /* Check the response */
418 if (init_packet->Messages.Version1Messages.
419 SendSendBufferComplete.Status != NvspStatusSuccess) {
420 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
421 "initialzation with NetVsp - status %d",
422 init_packet->Messages.Version1Messages.
423 SendSendBufferComplete.Status);
428 net_device->SendSectionSize = init_packet->
429 Messages.Version1Messages.SendSendBufferComplete.SectionSize;
434 netvsc_destroy_send_buf(net_device);
437 put_net_device(device);
441 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
443 struct nvsp_message *revoke_packet;
447 * If we got a section count, it means we received a
448 * SendReceiveBufferComplete msg (ie sent
449 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
450 * to send a revoke msg here
452 if (net_device->ReceiveSectionCount) {
454 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
456 /* Send the revoke receive buffer */
457 revoke_packet = &net_device->RevokePacket;
458 memset(revoke_packet, 0, sizeof(struct nvsp_message));
460 revoke_packet->Header.MessageType =
461 NvspMessage1TypeRevokeReceiveBuffer;
462 revoke_packet->Messages.Version1Messages.
463 RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
465 ret = vmbus_sendpacket(net_device->Device->channel,
467 sizeof(struct nvsp_message),
468 (unsigned long)revoke_packet,
469 VmbusPacketTypeDataInBand, 0);
471 * If we failed here, we might as well return and
472 * have a leak rather than continue and a bugchk
475 DPRINT_ERR(NETVSC, "unable to send revoke receive "
481 /* Teardown the gpadl on the vsp end */
482 if (net_device->ReceiveBufferGpadlHandle) {
483 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
485 ret = vmbus_teardown_gpadl(net_device->Device->channel,
486 net_device->ReceiveBufferGpadlHandle);
488 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
491 "unable to teardown receive buffer's gpadl");
494 net_device->ReceiveBufferGpadlHandle = 0;
497 if (net_device->ReceiveBuffer) {
498 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
500 /* Free up the receive buffer */
501 osd_page_free(net_device->ReceiveBuffer,
502 net_device->ReceiveBufferSize >> PAGE_SHIFT);
503 net_device->ReceiveBuffer = NULL;
506 if (net_device->ReceiveSections) {
507 net_device->ReceiveSectionCount = 0;
508 kfree(net_device->ReceiveSections);
509 net_device->ReceiveSections = NULL;
515 static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
517 struct nvsp_message *revoke_packet;
521 * If we got a section count, it means we received a
522 * SendReceiveBufferComplete msg (ie sent
523 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
524 * to send a revoke msg here
526 if (net_device->SendSectionSize) {
528 "Sending NvspMessage1TypeRevokeSendBuffer...");
530 /* Send the revoke send buffer */
531 revoke_packet = &net_device->RevokePacket;
532 memset(revoke_packet, 0, sizeof(struct nvsp_message));
534 revoke_packet->Header.MessageType =
535 NvspMessage1TypeRevokeSendBuffer;
536 revoke_packet->Messages.Version1Messages.
537 RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
539 ret = vmbus_sendpacket(net_device->Device->channel,
541 sizeof(struct nvsp_message),
542 (unsigned long)revoke_packet,
543 VmbusPacketTypeDataInBand, 0);
545 * If we failed here, we might as well return and have a leak
546 * rather than continue and a bugchk
549 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
555 /* Teardown the gpadl on the vsp end */
556 if (net_device->SendBufferGpadlHandle) {
557 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
558 ret = vmbus_teardown_gpadl(net_device->Device->channel,
559 net_device->SendBufferGpadlHandle);
562 * If we failed here, we might as well return and have a leak
563 * rather than continue and a bugchk
566 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
570 net_device->SendBufferGpadlHandle = 0;
573 if (net_device->SendBuffer) {
574 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
576 /* Free up the receive buffer */
577 osd_page_free(net_device->SendBuffer,
578 net_device->SendBufferSize >> PAGE_SHIFT);
579 net_device->SendBuffer = NULL;
586 static int netvsc_connect_vsp(struct hv_device *device)
589 struct netvsc_device *net_device;
590 struct nvsp_message *init_packet;
593 net_device = get_outbound_net_device(device);
595 DPRINT_ERR(NETVSC, "unable to get net device..."
596 "device being destroyed?");
600 init_packet = &net_device->ChannelInitPacket;
602 memset(init_packet, 0, sizeof(struct nvsp_message));
603 init_packet->Header.MessageType = NvspMessageTypeInit;
604 init_packet->Messages.InitMessages.Init.MinProtocolVersion =
605 NVSP_MIN_PROTOCOL_VERSION;
606 init_packet->Messages.InitMessages.Init.MaxProtocolVersion =
607 NVSP_MAX_PROTOCOL_VERSION;
609 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
611 /* Send the init request */
612 ret = vmbus_sendpacket(device->channel, init_packet,
613 sizeof(struct nvsp_message),
614 (unsigned long)init_packet,
615 VmbusPacketTypeDataInBand,
616 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
619 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
623 osd_waitevent_wait(net_device->ChannelInitEvent);
625 /* Now, check the response */
626 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
627 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
628 init_packet->Messages.InitMessages.InitComplete.Status,
629 init_packet->Messages.InitMessages.
630 InitComplete.MaximumMdlChainLength);
632 if (init_packet->Messages.InitMessages.InitComplete.Status !=
635 "unable to initialize with netvsp (status 0x%x)",
636 init_packet->Messages.InitMessages.InitComplete.Status);
641 if (init_packet->Messages.InitMessages.InitComplete.
642 NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) {
643 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
644 "(version expected 1 got %d)",
645 init_packet->Messages.InitMessages.
646 InitComplete.NegotiatedProtocolVersion);
650 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
652 /* Send the ndis version */
653 memset(init_packet, 0, sizeof(struct nvsp_message));
655 ndis_version = 0x00050000;
657 init_packet->Header.MessageType = NvspMessage1TypeSendNdisVersion;
658 init_packet->Messages.Version1Messages.
659 SendNdisVersion.NdisMajorVersion =
660 (ndis_version & 0xFFFF0000) >> 16;
661 init_packet->Messages.Version1Messages.
662 SendNdisVersion.NdisMinorVersion =
663 ndis_version & 0xFFFF;
665 /* Send the init request */
666 ret = vmbus_sendpacket(device->channel, init_packet,
667 sizeof(struct nvsp_message),
668 (unsigned long)init_packet,
669 VmbusPacketTypeDataInBand, 0);
672 "unable to send NvspMessage1TypeSendNdisVersion");
677 * BUGBUG - We have to wait for the above msg since the
678 * netvsp uses KMCL which acknowledges packet (completion
679 * packet) since our Vmbus always set the
680 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
682 /* osd_waitevent_wait(NetVscChannel->ChannelInitEvent); */
684 /* Post the big receive buffer to NetVSP */
685 ret = netvsc_init_recv_buf(device);
687 ret = netvsc_init_send_buf(device);
690 put_net_device(device);
694 static void NetVscDisconnectFromVsp(struct netvsc_device *net_device)
696 netvsc_destroy_recv_buf(net_device);
697 netvsc_destroy_send_buf(net_device);
701 * netvsc_device_add - Callback when the device belonging to this
704 static int netvsc_device_add(struct hv_device *device, void *additional_info)
708 struct netvsc_device *net_device;
709 struct hv_netvsc_packet *packet, *pos;
710 struct netvsc_driver *net_driver =
711 (struct netvsc_driver *)device->Driver;
713 net_device = alloc_net_device(device);
719 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", net_device);
721 /* Initialize the NetVSC channel extension */
722 net_device->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
723 spin_lock_init(&net_device->receive_packet_list_lock);
725 net_device->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
727 INIT_LIST_HEAD(&net_device->ReceivePacketList);
729 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
730 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
731 (NETVSC_RECEIVE_SG_COUNT *
732 sizeof(struct hv_page_buffer)), GFP_KERNEL);
734 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
735 "for receive pool (wanted %d got %d)",
736 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
739 list_add_tail(&packet->ListEntry,
740 &net_device->ReceivePacketList);
742 net_device->ChannelInitEvent = osd_waitevent_create();
743 if (!net_device->ChannelInitEvent) {
748 /* Open the channel */
749 ret = vmbus_open(device->channel, net_driver->RingBufferSize,
750 net_driver->RingBufferSize, NULL, 0,
751 netvsc_channel_cb, device);
754 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
759 /* Channel is opened */
760 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
762 /* Connect with the NetVsp */
763 ret = netvsc_connect_vsp(device);
765 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
770 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
776 /* Now, we can close the channel safely */
777 vmbus_close(device->channel);
782 kfree(net_device->ChannelInitEvent);
784 list_for_each_entry_safe(packet, pos,
785 &net_device->ReceivePacketList,
787 list_del(&packet->ListEntry);
791 release_outbound_net_device(device);
792 release_inbound_net_device(device);
794 free_net_device(net_device);
801 * netvsc_device_remove - Callback when the root bus device is removed
803 static int netvsc_device_remove(struct hv_device *device)
805 struct netvsc_device *net_device;
806 struct hv_netvsc_packet *netvsc_packet, *pos;
808 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
811 /* Stop outbound traffic ie sends and receives completions */
812 net_device = release_outbound_net_device(device);
814 DPRINT_ERR(NETVSC, "No net device present!!");
818 /* Wait for all send completions */
819 while (atomic_read(&net_device->NumOutstandingSends)) {
820 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
821 atomic_read(&net_device->NumOutstandingSends));
825 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
827 NetVscDisconnectFromVsp(net_device);
829 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
832 /* Stop inbound traffic ie receives and sends completions */
833 net_device = release_inbound_net_device(device);
835 /* At this point, no one should be accessing netDevice except in here */
836 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", net_device);
838 /* Now, we can close the channel safely */
839 vmbus_close(device->channel);
841 /* Release all resources */
842 list_for_each_entry_safe(netvsc_packet, pos,
843 &net_device->ReceivePacketList, ListEntry) {
844 list_del(&netvsc_packet->ListEntry);
845 kfree(netvsc_packet);
848 kfree(net_device->ChannelInitEvent);
849 free_net_device(net_device);
854 * netvsc_cleanup - Perform any cleanup when the driver is removed
856 static void netvsc_cleanup(struct hv_driver *drv)
860 static void netvsc_send_completion(struct hv_device *device,
861 struct vmpacket_descriptor *packet)
863 struct netvsc_device *net_device;
864 struct nvsp_message *nvsp_packet;
865 struct hv_netvsc_packet *nvsc_packet;
867 net_device = get_inbound_net_device(device);
869 DPRINT_ERR(NETVSC, "unable to get net device..."
870 "device being destroyed?");
874 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
875 (packet->DataOffset8 << 3));
877 DPRINT_DBG(NETVSC, "send completion packet - type %d",
878 nvsp_packet->Header.MessageType);
880 if ((nvsp_packet->Header.MessageType == NvspMessageTypeInitComplete) ||
881 (nvsp_packet->Header.MessageType ==
882 NvspMessage1TypeSendReceiveBufferComplete) ||
883 (nvsp_packet->Header.MessageType ==
884 NvspMessage1TypeSendSendBufferComplete)) {
885 /* Copy the response back */
886 memcpy(&net_device->ChannelInitPacket, nvsp_packet,
887 sizeof(struct nvsp_message));
888 osd_waitevent_set(net_device->ChannelInitEvent);
889 } else if (nvsp_packet->Header.MessageType ==
890 NvspMessage1TypeSendRNDISPacketComplete) {
891 /* Get the send context */
892 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
893 packet->TransactionId;
894 /* ASSERT(nvscPacket); */
896 /* Notify the layer above us */
897 nvsc_packet->Completion.Send.OnSendCompletion(
898 nvsc_packet->Completion.Send.SendCompletionContext);
900 atomic_dec(&net_device->NumOutstandingSends);
902 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
903 "%d received!!", nvsp_packet->Header.MessageType);
906 put_net_device(device);
909 static int netvsc_send(struct hv_device *device,
910 struct hv_netvsc_packet *packet)
912 struct netvsc_device *net_device;
915 struct nvsp_message sendMessage;
917 net_device = get_outbound_net_device(device);
919 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
920 "ignoring outbound packets", net_device);
924 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
925 if (packet->IsDataPacket) {
927 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
929 /* 1 is RMC_CONTROL; */
930 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
933 /* Not using send buffer section */
934 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
935 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
937 if (packet->PageBufferCount) {
938 ret = vmbus_sendpacket_pagebuffer(device->channel,
940 packet->PageBufferCount,
942 sizeof(struct nvsp_message),
943 (unsigned long)packet);
945 ret = vmbus_sendpacket(device->channel, &sendMessage,
946 sizeof(struct nvsp_message),
947 (unsigned long)packet,
948 VmbusPacketTypeDataInBand,
949 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
954 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
957 atomic_inc(&net_device->NumOutstandingSends);
958 put_net_device(device);
962 static void netvsc_receive(struct hv_device *device,
963 struct vmpacket_descriptor *packet)
965 struct netvsc_device *net_device;
966 struct vmtransfer_page_packet_header *vmxferpage_packet;
967 struct nvsp_message *nvsp_packet;
968 struct hv_netvsc_packet *netvsc_packet = NULL;
970 unsigned long end, end_virtual;
971 /* struct netvsc_driver *netvscDriver; */
972 struct xferpage_packet *xferpage_packet = NULL;
974 int count = 0, bytes_remain = 0;
978 net_device = get_inbound_net_device(device);
980 DPRINT_ERR(NETVSC, "unable to get net device..."
981 "device being destroyed?");
986 * All inbound packets other than send completion should be xfer page
989 if (packet->Type != VmbusPacketTypeDataUsingTransferPages) {
990 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
992 put_net_device(device);
996 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
997 (packet->DataOffset8 << 3));
999 /* Make sure this is a valid nvsp packet */
1000 if (nvsp_packet->Header.MessageType !=
1001 NvspMessage1TypeSendRNDISPacket) {
1002 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1003 nvsp_packet->Header.MessageType);
1004 put_net_device(device);
1008 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1009 nvsp_packet->Header.MessageType);
1011 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1013 if (vmxferpage_packet->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1014 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1015 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1016 vmxferpage_packet->TransferPageSetId);
1017 put_net_device(device);
1021 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1022 vmxferpage_packet->RangeCount);
1025 * Grab free packets (range count + 1) to represent this xfer
1026 * page packet. +1 to represent the xfer page packet itself.
1027 * We grab it here so that we know exactly how many we can
1030 spin_lock_irqsave(&net_device->receive_packet_list_lock, flags);
1031 while (!list_empty(&net_device->ReceivePacketList)) {
1032 list_move_tail(net_device->ReceivePacketList.next, &listHead);
1033 if (++count == vmxferpage_packet->RangeCount + 1)
1036 spin_unlock_irqrestore(&net_device->receive_packet_list_lock, flags);
1039 * We need at least 2 netvsc pkts (1 to represent the xfer
1040 * page and at least 1 for the range) i.e. we can handled
1041 * some of the xfer page packet ranges...
1044 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1045 "Dropping this xfer page packet completely!",
1046 count, vmxferpage_packet->RangeCount + 1);
1048 /* Return it to the freelist */
1049 spin_lock_irqsave(&net_device->receive_packet_list_lock, flags);
1050 for (i = count; i != 0; i--) {
1051 list_move_tail(listHead.next,
1052 &net_device->ReceivePacketList);
1054 spin_unlock_irqrestore(&net_device->receive_packet_list_lock,
1057 netvsc_send_recv_completion(device,
1058 vmxferpage_packet->d.TransactionId);
1060 put_net_device(device);
1064 /* Remove the 1st packet to represent the xfer page packet itself */
1065 xferpage_packet = (struct xferpage_packet *)listHead.next;
1066 list_del(&xferpage_packet->ListEntry);
1068 /* This is how much we can satisfy */
1069 xferpage_packet->Count = count - 1;
1070 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1071 /* vmxferpagePacket->RangeCount); */
1073 if (xferpage_packet->Count != vmxferpage_packet->RangeCount) {
1074 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1075 "page...got %d", vmxferpage_packet->RangeCount,
1076 xferpage_packet->Count);
1079 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1080 for (i = 0; i < (count - 1); i++) {
1081 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
1082 list_del(&netvsc_packet->ListEntry);
1084 /* Initialize the netvsc packet */
1085 netvsc_packet->XferPagePacket = xferpage_packet;
1086 netvsc_packet->Completion.Recv.OnReceiveCompletion =
1087 netvsc_receive_completion;
1088 netvsc_packet->Completion.Recv.ReceiveCompletionContext =
1090 netvsc_packet->Device = device;
1091 /* Save this so that we can send it back */
1092 netvsc_packet->Completion.Recv.ReceiveCompletionTid =
1093 vmxferpage_packet->d.TransactionId;
1095 netvsc_packet->TotalDataBufferLength =
1096 vmxferpage_packet->Ranges[i].ByteCount;
1097 netvsc_packet->PageBufferCount = 1;
1099 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1100 /* vmxferpagePacket->Ranges[i].ByteCount < */
1101 /* netDevice->ReceiveBufferSize); */
1103 netvsc_packet->PageBuffers[0].Length =
1104 vmxferpage_packet->Ranges[i].ByteCount;
1106 start = virt_to_phys((void *)((unsigned long)net_device->
1107 ReceiveBuffer + vmxferpage_packet->Ranges[i].ByteOffset));
1109 netvsc_packet->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1110 end_virtual = (unsigned long)net_device->ReceiveBuffer
1111 + vmxferpage_packet->Ranges[i].ByteOffset
1112 + vmxferpage_packet->Ranges[i].ByteCount - 1;
1113 end = virt_to_phys((void *)end_virtual);
1115 /* Calculate the page relative offset */
1116 netvsc_packet->PageBuffers[0].Offset =
1117 vmxferpage_packet->Ranges[i].ByteOffset &
1119 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1120 /* Handle frame across multiple pages: */
1121 netvsc_packet->PageBuffers[0].Length =
1122 (netvsc_packet->PageBuffers[0].Pfn <<
1124 + PAGE_SIZE - start;
1125 bytes_remain = netvsc_packet->TotalDataBufferLength -
1126 netvsc_packet->PageBuffers[0].Length;
1127 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1128 netvsc_packet->PageBuffers[j].Offset = 0;
1129 if (bytes_remain <= PAGE_SIZE) {
1130 netvsc_packet->PageBuffers[j].Length =
1134 netvsc_packet->PageBuffers[j].Length =
1136 bytes_remain -= PAGE_SIZE;
1138 netvsc_packet->PageBuffers[j].Pfn =
1139 virt_to_phys((void *)(end_virtual -
1140 bytes_remain)) >> PAGE_SHIFT;
1141 netvsc_packet->PageBufferCount++;
1142 if (bytes_remain == 0)
1145 /* ASSERT(bytesRemain == 0); */
1147 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1148 "(pfn %llx, offset %u, len %u)", i,
1149 vmxferpage_packet->Ranges[i].ByteOffset,
1150 vmxferpage_packet->Ranges[i].ByteCount,
1151 netvsc_packet->PageBuffers[0].Pfn,
1152 netvsc_packet->PageBuffers[0].Offset,
1153 netvsc_packet->PageBuffers[0].Length);
1155 /* Pass it to the upper layer */
1156 ((struct netvsc_driver *)device->Driver)->
1157 OnReceiveCallback(device, netvsc_packet);
1159 netvsc_receive_completion(netvsc_packet->
1160 Completion.Recv.ReceiveCompletionContext);
1163 /* ASSERT(list_empty(&listHead)); */
1165 put_net_device(device);
1168 static void netvsc_send_recv_completion(struct hv_device *device,
1171 struct nvsp_message recvcompMessage;
1175 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1178 recvcompMessage.Header.MessageType =
1179 NvspMessage1TypeSendRNDISPacketComplete;
1181 /* FIXME: Pass in the status */
1182 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1185 /* Send the completion */
1186 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
1187 sizeof(struct nvsp_message), transaction_id,
1188 VmbusPacketTypeCompletion, 0);
1192 } else if (ret == -1) {
1193 /* no more room...wait a bit and attempt to retry 3 times */
1195 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1196 "(tid %llx)...retrying %d", transaction_id, retries);
1200 goto retry_send_cmplt;
1202 DPRINT_ERR(NETVSC, "unable to send receive completion "
1203 "pkt (tid %llx)...give up retrying",
1207 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1208 "%llx", transaction_id);
1212 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1213 static void netvsc_receive_completion(void *context)
1215 struct hv_netvsc_packet *packet = context;
1216 struct hv_device *device = (struct hv_device *)packet->Device;
1217 struct netvsc_device *net_device;
1218 u64 transaction_id = 0;
1219 bool fsend_receive_comp = false;
1220 unsigned long flags;
1222 /* ASSERT(packet->XferPagePacket); */
1225 * Even though it seems logical to do a GetOutboundNetDevice() here to
1226 * send out receive completion, we are using GetInboundNetDevice()
1227 * since we may have disable outbound traffic already.
1229 net_device = get_inbound_net_device(device);
1231 DPRINT_ERR(NETVSC, "unable to get net device..."
1232 "device being destroyed?");
1236 /* Overloading use of the lock. */
1237 spin_lock_irqsave(&net_device->receive_packet_list_lock, flags);
1239 /* ASSERT(packet->XferPagePacket->Count > 0); */
1240 packet->XferPagePacket->Count--;
1243 * Last one in the line that represent 1 xfer page packet.
1244 * Return the xfer page packet itself to the freelist
1246 if (packet->XferPagePacket->Count == 0) {
1247 fsend_receive_comp = true;
1248 transaction_id = packet->Completion.Recv.ReceiveCompletionTid;
1249 list_add_tail(&packet->XferPagePacket->ListEntry,
1250 &net_device->ReceivePacketList);
1254 /* Put the packet back */
1255 list_add_tail(&packet->ListEntry, &net_device->ReceivePacketList);
1256 spin_unlock_irqrestore(&net_device->receive_packet_list_lock, flags);
1258 /* Send a receive completion for the xfer page packet */
1259 if (fsend_receive_comp)
1260 netvsc_send_recv_completion(device, transaction_id);
1262 put_net_device(device);
1265 static void netvsc_channel_cb(void *context)
1268 struct hv_device *device = context;
1269 struct netvsc_device *net_device;
1272 unsigned char *packet;
1273 struct vmpacket_descriptor *desc;
1274 unsigned char *buffer;
1275 int bufferlen = NETVSC_PACKET_SIZE;
1277 /* ASSERT(device); */
1279 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1285 net_device = get_inbound_net_device(device);
1287 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1288 "ignoring inbound packets", net_device);
1293 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
1294 &bytes_recvd, &request_id);
1296 if (bytes_recvd > 0) {
1297 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1298 bytes_recvd, request_id);
1300 desc = (struct vmpacket_descriptor *)buffer;
1301 switch (desc->Type) {
1302 case VmbusPacketTypeCompletion:
1303 netvsc_send_completion(device, desc);
1306 case VmbusPacketTypeDataUsingTransferPages:
1307 netvsc_receive(device, desc);
1312 "unhandled packet type %d, "
1313 "tid %llx len %d\n",
1314 desc->Type, request_id,
1320 if (bufferlen > NETVSC_PACKET_SIZE) {
1323 bufferlen = NETVSC_PACKET_SIZE;
1327 if (bufferlen > NETVSC_PACKET_SIZE) {
1330 bufferlen = NETVSC_PACKET_SIZE;
1335 } else if (ret == -2) {
1336 /* Handle large packet */
1337 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1338 if (buffer == NULL) {
1339 /* Try again next time around */
1341 "unable to allocate buffer of size "
1342 "(%d)!!", bytes_recvd);
1346 bufferlen = bytes_recvd;
1350 put_net_device(device);