writeback: split writeback_inodes_wb
[linux-2.6/next.git] / drivers / staging / hv / netvsc.c
blobba15059c45b2c46313e1b22a517834f7c460ff44
1 /*
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
11 * more details.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/delay.h>
24 #include <linux/io.h>
25 #include <linux/slab.h>
26 #include "osd.h"
27 #include "logging.h"
28 #include "netvsc.h"
29 #include "rndis_filter.h"
32 /* Globals */
33 static const char *gDriverName = "netvsc";
35 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
36 static const struct hv_guid gNetVscDeviceType = {
37 .data = {
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,
73 u64 TransactionId);
76 static struct netvsc_device *AllocNetDevice(struct hv_device *Device)
78 struct netvsc_device *netDevice;
80 netDevice = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
81 if (!netDevice)
82 return NULL;
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;
90 return netDevice;
93 static void FreeNetDevice(struct netvsc_device *Device)
95 WARN_ON(atomic_read(&Device->RefCount) == 0);
96 Device->Device->Extension = NULL;
97 kfree(Device);
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);
109 else
110 netDevice = NULL;
112 return netDevice;
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);
123 else
124 netDevice = NULL;
126 return netDevice;
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)
145 return NULL;
147 /* Busy wait until the ref drop to 2, then set it to 1 */
148 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
149 udelay(100);
151 return netDevice;
154 static struct netvsc_device *ReleaseInboundNetDevice(struct hv_device *Device)
156 struct netvsc_device *netDevice;
158 netDevice = Device->Extension;
159 if (netDevice == NULL)
160 return NULL;
162 /* Busy wait until the ref drop to 1, then set it to 0 */
163 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
164 udelay(100);
166 Device->Extension = NULL;
167 return netDevice;
171 * NetVscInitialize - Main entry point
173 int NetVscInitialize(struct hv_driver *drv)
175 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
177 DPRINT_ENTER(NETVSC);
179 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
180 "sizeof(struct nvsp_message)=%zd, "
181 "sizeof(struct vmtransfer_page_packet_header)=%zd",
182 sizeof(struct hv_netvsc_packet),
183 sizeof(struct nvsp_message),
184 sizeof(struct vmtransfer_page_packet_header));
186 /* Make sure we are at least 2 pages since 1 page is used for control */
187 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
189 drv->name = gDriverName;
190 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
192 /* Make sure it is set by the caller */
193 /* FIXME: These probably should still be tested in some way */
194 /* ASSERT(driver->OnReceiveCallback); */
195 /* ASSERT(driver->OnLinkStatusChanged); */
197 /* Setup the dispatch table */
198 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
199 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
200 driver->Base.OnCleanup = NetVscOnCleanup;
202 driver->OnSend = NetVscOnSend;
204 RndisFilterInit(driver);
206 DPRINT_EXIT(NETVSC);
208 return 0;
211 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
213 int ret = 0;
214 struct netvsc_device *netDevice;
215 struct nvsp_message *initPacket;
217 DPRINT_ENTER(NETVSC);
219 netDevice = GetOutboundNetDevice(Device);
220 if (!netDevice) {
221 DPRINT_ERR(NETVSC, "unable to get net device..."
222 "device being destroyed?");
223 DPRINT_EXIT(NETVSC);
224 return -1;
226 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
227 /* page-size grandularity */
228 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
230 netDevice->ReceiveBuffer =
231 osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
232 if (!netDevice->ReceiveBuffer) {
233 DPRINT_ERR(NETVSC,
234 "unable to allocate receive buffer of size %d",
235 netDevice->ReceiveBufferSize);
236 ret = -1;
237 goto Cleanup;
239 /* page-aligned buffer */
240 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
241 /* 0); */
243 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
246 * Establish the gpadl handle for this buffer on this
247 * channel. Note: This call uses the vmbus connection rather
248 * than the channel to establish the gpadl handle.
250 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
251 netDevice->ReceiveBuffer,
252 netDevice->ReceiveBufferSize,
253 &netDevice->ReceiveBufferGpadlHandle);
254 if (ret != 0) {
255 DPRINT_ERR(NETVSC,
256 "unable to establish receive buffer's gpadl");
257 goto Cleanup;
260 /* osd_WaitEventWait(ext->ChannelInitEvent); */
262 /* Notify the NetVsp of the gpadl handle */
263 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
265 initPacket = &netDevice->ChannelInitPacket;
267 memset(initPacket, 0, sizeof(struct nvsp_message));
269 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
270 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
271 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
273 /* Send the gpadl notification request */
274 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
275 initPacket,
276 sizeof(struct nvsp_message),
277 (unsigned long)initPacket,
278 VmbusPacketTypeDataInBand,
279 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
280 if (ret != 0) {
281 DPRINT_ERR(NETVSC,
282 "unable to send receive buffer's gpadl to netvsp");
283 goto Cleanup;
286 osd_WaitEventWait(netDevice->ChannelInitEvent);
288 /* Check the response */
289 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess) {
290 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
291 "initialzation with NetVsp - status %d",
292 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
293 ret = -1;
294 goto Cleanup;
297 /* Parse the response */
298 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
299 /* ASSERT(netDevice->ReceiveSections == NULL); */
301 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
303 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
304 if (netDevice->ReceiveSections == NULL) {
305 ret = -1;
306 goto Cleanup;
309 memcpy(netDevice->ReceiveSections,
310 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
311 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
313 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
314 "endoffset %d, suballoc size %d, num suballocs %d)",
315 netDevice->ReceiveSectionCount,
316 netDevice->ReceiveSections[0].Offset,
317 netDevice->ReceiveSections[0].EndOffset,
318 netDevice->ReceiveSections[0].SubAllocationSize,
319 netDevice->ReceiveSections[0].NumSubAllocations);
322 * For 1st release, there should only be 1 section that represents the
323 * entire receive buffer
325 if (netDevice->ReceiveSectionCount != 1 ||
326 netDevice->ReceiveSections->Offset != 0) {
327 ret = -1;
328 goto Cleanup;
331 goto Exit;
333 Cleanup:
334 NetVscDestroyReceiveBuffer(netDevice);
336 Exit:
337 PutNetDevice(Device);
338 DPRINT_EXIT(NETVSC);
339 return ret;
342 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
344 int ret = 0;
345 struct netvsc_device *netDevice;
346 struct nvsp_message *initPacket;
348 DPRINT_ENTER(NETVSC);
350 netDevice = GetOutboundNetDevice(Device);
351 if (!netDevice) {
352 DPRINT_ERR(NETVSC, "unable to get net device..."
353 "device being destroyed?");
354 DPRINT_EXIT(NETVSC);
355 return -1;
357 if (netDevice->SendBufferSize <= 0) {
358 ret = -EINVAL;
359 goto Cleanup;
362 /* page-size grandularity */
363 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
365 netDevice->SendBuffer =
366 osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
367 if (!netDevice->SendBuffer) {
368 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
369 netDevice->SendBufferSize);
370 ret = -1;
371 goto Cleanup;
373 /* page-aligned buffer */
374 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
376 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
379 * Establish the gpadl handle for this buffer on this
380 * channel. Note: This call uses the vmbus connection rather
381 * than the channel to establish the gpadl handle.
383 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
384 netDevice->SendBuffer,
385 netDevice->SendBufferSize,
386 &netDevice->SendBufferGpadlHandle);
387 if (ret != 0) {
388 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
389 goto Cleanup;
392 /* osd_WaitEventWait(ext->ChannelInitEvent); */
394 /* Notify the NetVsp of the gpadl handle */
395 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
397 initPacket = &netDevice->ChannelInitPacket;
399 memset(initPacket, 0, sizeof(struct nvsp_message));
401 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
402 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
403 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
405 /* Send the gpadl notification request */
406 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
407 initPacket, sizeof(struct nvsp_message),
408 (unsigned long)initPacket,
409 VmbusPacketTypeDataInBand,
410 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
411 if (ret != 0) {
412 DPRINT_ERR(NETVSC,
413 "unable to send receive buffer's gpadl to netvsp");
414 goto Cleanup;
417 osd_WaitEventWait(netDevice->ChannelInitEvent);
419 /* Check the response */
420 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess) {
421 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
422 "initialzation with NetVsp - status %d",
423 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
424 ret = -1;
425 goto Cleanup;
428 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
430 goto Exit;
432 Cleanup:
433 NetVscDestroySendBuffer(netDevice);
435 Exit:
436 PutNetDevice(Device);
437 DPRINT_EXIT(NETVSC);
438 return ret;
441 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
443 struct nvsp_message *revokePacket;
444 int ret = 0;
446 DPRINT_ENTER(NETVSC);
449 * If we got a section count, it means we received a
450 * SendReceiveBufferComplete msg (ie sent
451 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
452 * to send a revoke msg here
454 if (NetDevice->ReceiveSectionCount) {
455 DPRINT_INFO(NETVSC,
456 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
458 /* Send the revoke receive buffer */
459 revokePacket = &NetDevice->RevokePacket;
460 memset(revokePacket, 0, sizeof(struct nvsp_message));
462 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
463 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
465 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
466 NetDevice->Device,
467 revokePacket,
468 sizeof(struct nvsp_message),
469 (unsigned long)revokePacket,
470 VmbusPacketTypeDataInBand, 0);
472 * If we failed here, we might as well return and
473 * have a leak rather than continue and a bugchk
475 if (ret != 0) {
476 DPRINT_ERR(NETVSC, "unable to send revoke receive "
477 "buffer to netvsp");
478 DPRINT_EXIT(NETVSC);
479 return -1;
483 /* Teardown the gpadl on the vsp end */
484 if (NetDevice->ReceiveBufferGpadlHandle) {
485 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
487 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
488 NetDevice->Device,
489 NetDevice->ReceiveBufferGpadlHandle);
491 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
492 if (ret != 0) {
493 DPRINT_ERR(NETVSC,
494 "unable to teardown receive buffer's gpadl");
495 DPRINT_EXIT(NETVSC);
496 return -1;
498 NetDevice->ReceiveBufferGpadlHandle = 0;
501 if (NetDevice->ReceiveBuffer) {
502 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
504 /* Free up the receive buffer */
505 osd_PageFree(NetDevice->ReceiveBuffer,
506 NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
507 NetDevice->ReceiveBuffer = NULL;
510 if (NetDevice->ReceiveSections) {
511 NetDevice->ReceiveSectionCount = 0;
512 kfree(NetDevice->ReceiveSections);
513 NetDevice->ReceiveSections = NULL;
516 DPRINT_EXIT(NETVSC);
518 return ret;
521 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
523 struct nvsp_message *revokePacket;
524 int ret = 0;
526 DPRINT_ENTER(NETVSC);
529 * If we got a section count, it means we received a
530 * SendReceiveBufferComplete msg (ie sent
531 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
532 * to send a revoke msg here
534 if (NetDevice->SendSectionSize) {
535 DPRINT_INFO(NETVSC,
536 "Sending NvspMessage1TypeRevokeSendBuffer...");
538 /* Send the revoke send buffer */
539 revokePacket = &NetDevice->RevokePacket;
540 memset(revokePacket, 0, sizeof(struct nvsp_message));
542 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
543 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
545 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
546 revokePacket,
547 sizeof(struct nvsp_message),
548 (unsigned long)revokePacket,
549 VmbusPacketTypeDataInBand, 0);
551 * If we failed here, we might as well return and have a leak
552 * rather than continue and a bugchk
554 if (ret != 0) {
555 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
556 "to netvsp");
557 DPRINT_EXIT(NETVSC);
558 return -1;
562 /* Teardown the gpadl on the vsp end */
563 if (NetDevice->SendBufferGpadlHandle) {
564 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
566 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
569 * If we failed here, we might as well return and have a leak
570 * rather than continue and a bugchk
572 if (ret != 0) {
573 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
574 "gpadl");
575 DPRINT_EXIT(NETVSC);
576 return -1;
578 NetDevice->SendBufferGpadlHandle = 0;
581 if (NetDevice->SendBuffer) {
582 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
584 /* Free up the receive buffer */
585 osd_PageFree(NetDevice->SendBuffer,
586 NetDevice->SendBufferSize >> PAGE_SHIFT);
587 NetDevice->SendBuffer = NULL;
590 DPRINT_EXIT(NETVSC);
592 return ret;
596 static int NetVscConnectToVsp(struct hv_device *Device)
598 int ret;
599 struct netvsc_device *netDevice;
600 struct nvsp_message *initPacket;
601 int ndisVersion;
603 DPRINT_ENTER(NETVSC);
605 netDevice = GetOutboundNetDevice(Device);
606 if (!netDevice) {
607 DPRINT_ERR(NETVSC, "unable to get net device..."
608 "device being destroyed?");
609 DPRINT_EXIT(NETVSC);
610 return -1;
613 initPacket = &netDevice->ChannelInitPacket;
615 memset(initPacket, 0, sizeof(struct nvsp_message));
616 initPacket->Header.MessageType = NvspMessageTypeInit;
617 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
618 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
620 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
622 /* Send the init request */
623 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
624 initPacket,
625 sizeof(struct nvsp_message),
626 (unsigned long)initPacket,
627 VmbusPacketTypeDataInBand,
628 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
630 if (ret != 0) {
631 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
632 goto Cleanup;
635 osd_WaitEventWait(netDevice->ChannelInitEvent);
637 /* Now, check the response */
638 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
639 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
640 initPacket->Messages.InitMessages.InitComplete.Status,
641 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
643 if (initPacket->Messages.InitMessages.InitComplete.Status !=
644 NvspStatusSuccess) {
645 DPRINT_ERR(NETVSC,
646 "unable to initialize with netvsp (status 0x%x)",
647 initPacket->Messages.InitMessages.InitComplete.Status);
648 ret = -1;
649 goto Cleanup;
652 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) {
653 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
654 "(version expected 1 got %d)",
655 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
656 ret = -1;
657 goto Cleanup;
659 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
661 /* Send the ndis version */
662 memset(initPacket, 0, sizeof(struct nvsp_message));
664 ndisVersion = 0x00050000;
666 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
667 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
668 (ndisVersion & 0xFFFF0000) >> 16;
669 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
670 ndisVersion & 0xFFFF;
672 /* Send the init request */
673 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
674 initPacket,
675 sizeof(struct nvsp_message),
676 (unsigned long)initPacket,
677 VmbusPacketTypeDataInBand, 0);
678 if (ret != 0) {
679 DPRINT_ERR(NETVSC,
680 "unable to send NvspMessage1TypeSendNdisVersion");
681 ret = -1;
682 goto Cleanup;
685 * BUGBUG - We have to wait for the above msg since the
686 * netvsp uses KMCL which acknowledges packet (completion
687 * packet) since our Vmbus always set the
688 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
690 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
692 /* Post the big receive buffer to NetVSP */
693 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
694 if (ret == 0)
695 ret = NetVscInitializeSendBufferWithNetVsp(Device);
697 Cleanup:
698 PutNetDevice(Device);
699 DPRINT_EXIT(NETVSC);
700 return ret;
703 static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
705 DPRINT_ENTER(NETVSC);
707 NetVscDestroyReceiveBuffer(NetDevice);
708 NetVscDestroySendBuffer(NetDevice);
710 DPRINT_EXIT(NETVSC);
714 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
716 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
718 int ret = 0;
719 int i;
720 struct netvsc_device *netDevice;
721 struct hv_netvsc_packet *packet, *pos;
722 struct netvsc_driver *netDriver =
723 (struct netvsc_driver *)Device->Driver;
725 DPRINT_ENTER(NETVSC);
727 netDevice = AllocNetDevice(Device);
728 if (!netDevice) {
729 ret = -1;
730 goto Cleanup;
733 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
735 /* Initialize the NetVSC channel extension */
736 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
737 spin_lock_init(&netDevice->receive_packet_list_lock);
739 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
741 INIT_LIST_HEAD(&netDevice->ReceivePacketList);
743 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
744 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
745 (NETVSC_RECEIVE_SG_COUNT *
746 sizeof(struct hv_page_buffer)), GFP_KERNEL);
747 if (!packet) {
748 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
749 "for receive pool (wanted %d got %d)",
750 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
751 break;
753 list_add_tail(&packet->ListEntry,
754 &netDevice->ReceivePacketList);
756 netDevice->ChannelInitEvent = osd_WaitEventCreate();
757 if (!netDevice->ChannelInitEvent) {
758 ret = -ENOMEM;
759 goto Cleanup;
762 /* Open the channel */
763 ret = Device->Driver->VmbusChannelInterface.Open(Device,
764 netDriver->RingBufferSize,
765 netDriver->RingBufferSize,
766 NULL, 0,
767 NetVscOnChannelCallback,
768 Device);
770 if (ret != 0) {
771 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
772 ret = -1;
773 goto Cleanup;
776 /* Channel is opened */
777 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
779 /* Connect with the NetVsp */
780 ret = NetVscConnectToVsp(Device);
781 if (ret != 0) {
782 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
783 ret = -1;
784 goto Close;
787 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
788 ret);
790 DPRINT_EXIT(NETVSC);
791 return ret;
793 Close:
794 /* Now, we can close the channel safely */
795 Device->Driver->VmbusChannelInterface.Close(Device);
797 Cleanup:
799 if (netDevice) {
800 kfree(netDevice->ChannelInitEvent);
802 list_for_each_entry_safe(packet, pos,
803 &netDevice->ReceivePacketList,
804 ListEntry) {
805 list_del(&packet->ListEntry);
806 kfree(packet);
809 ReleaseOutboundNetDevice(Device);
810 ReleaseInboundNetDevice(Device);
812 FreeNetDevice(netDevice);
815 DPRINT_EXIT(NETVSC);
816 return ret;
820 * NetVscOnDeviceRemove - Callback when the root bus device is removed
822 static int NetVscOnDeviceRemove(struct hv_device *Device)
824 struct netvsc_device *netDevice;
825 struct hv_netvsc_packet *netvscPacket, *pos;
827 DPRINT_ENTER(NETVSC);
829 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
830 Device->Extension);
832 /* Stop outbound traffic ie sends and receives completions */
833 netDevice = ReleaseOutboundNetDevice(Device);
834 if (!netDevice) {
835 DPRINT_ERR(NETVSC, "No net device present!!");
836 return -1;
839 /* Wait for all send completions */
840 while (atomic_read(&netDevice->NumOutstandingSends)) {
841 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
842 atomic_read(&netDevice->NumOutstandingSends));
843 udelay(100);
846 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
848 NetVscDisconnectFromVsp(netDevice);
850 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
851 Device->Extension);
853 /* Stop inbound traffic ie receives and sends completions */
854 netDevice = ReleaseInboundNetDevice(Device);
856 /* At this point, no one should be accessing netDevice except in here */
857 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
859 /* Now, we can close the channel safely */
860 Device->Driver->VmbusChannelInterface.Close(Device);
862 /* Release all resources */
863 list_for_each_entry_safe(netvscPacket, pos,
864 &netDevice->ReceivePacketList, ListEntry) {
865 list_del(&netvscPacket->ListEntry);
866 kfree(netvscPacket);
869 kfree(netDevice->ChannelInitEvent);
870 FreeNetDevice(netDevice);
872 DPRINT_EXIT(NETVSC);
873 return 0;
877 * NetVscOnCleanup - Perform any cleanup when the driver is removed
879 static void NetVscOnCleanup(struct hv_driver *drv)
881 DPRINT_ENTER(NETVSC);
882 DPRINT_EXIT(NETVSC);
885 static void NetVscOnSendCompletion(struct hv_device *Device,
886 struct vmpacket_descriptor *Packet)
888 struct netvsc_device *netDevice;
889 struct nvsp_message *nvspPacket;
890 struct hv_netvsc_packet *nvscPacket;
892 DPRINT_ENTER(NETVSC);
894 netDevice = GetInboundNetDevice(Device);
895 if (!netDevice) {
896 DPRINT_ERR(NETVSC, "unable to get net device..."
897 "device being destroyed?");
898 DPRINT_EXIT(NETVSC);
899 return;
902 nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
904 DPRINT_DBG(NETVSC, "send completion packet - type %d",
905 nvspPacket->Header.MessageType);
907 if ((nvspPacket->Header.MessageType == NvspMessageTypeInitComplete) ||
908 (nvspPacket->Header.MessageType ==
909 NvspMessage1TypeSendReceiveBufferComplete) ||
910 (nvspPacket->Header.MessageType ==
911 NvspMessage1TypeSendSendBufferComplete)) {
912 /* Copy the response back */
913 memcpy(&netDevice->ChannelInitPacket, nvspPacket,
914 sizeof(struct nvsp_message));
915 osd_WaitEventSet(netDevice->ChannelInitEvent);
916 } else if (nvspPacket->Header.MessageType ==
917 NvspMessage1TypeSendRNDISPacketComplete) {
918 /* Get the send context */
919 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
920 /* ASSERT(nvscPacket); */
922 /* Notify the layer above us */
923 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
925 atomic_dec(&netDevice->NumOutstandingSends);
926 } else {
927 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
928 "%d received!!", nvspPacket->Header.MessageType);
931 PutNetDevice(Device);
932 DPRINT_EXIT(NETVSC);
935 static int NetVscOnSend(struct hv_device *Device,
936 struct hv_netvsc_packet *Packet)
938 struct netvsc_device *netDevice;
939 int ret = 0;
941 struct nvsp_message sendMessage;
943 DPRINT_ENTER(NETVSC);
945 netDevice = GetOutboundNetDevice(Device);
946 if (!netDevice) {
947 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
948 "ignoring outbound packets", netDevice);
949 DPRINT_EXIT(NETVSC);
950 return -2;
953 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
954 if (Packet->IsDataPacket) {
955 /* 0 is RMC_DATA; */
956 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
957 } else {
958 /* 1 is RMC_CONTROL; */
959 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
962 /* Not using send buffer section */
963 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
964 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
966 if (Packet->PageBufferCount) {
967 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(
968 Device, Packet->PageBuffers,
969 Packet->PageBufferCount,
970 &sendMessage,
971 sizeof(struct nvsp_message),
972 (unsigned long)Packet);
973 } else {
974 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
975 &sendMessage,
976 sizeof(struct nvsp_message),
977 (unsigned long)Packet,
978 VmbusPacketTypeDataInBand,
979 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
983 if (ret != 0)
984 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
985 Packet, ret);
987 atomic_inc(&netDevice->NumOutstandingSends);
988 PutNetDevice(Device);
990 DPRINT_EXIT(NETVSC);
991 return ret;
994 static void NetVscOnReceive(struct hv_device *Device,
995 struct vmpacket_descriptor *Packet)
997 struct netvsc_device *netDevice;
998 struct vmtransfer_page_packet_header *vmxferpagePacket;
999 struct nvsp_message *nvspPacket;
1000 struct hv_netvsc_packet *netvscPacket = NULL;
1001 unsigned long start;
1002 unsigned long end, endVirtual;
1003 /* struct netvsc_driver *netvscDriver; */
1004 struct xferpage_packet *xferpagePacket = NULL;
1005 int i, j;
1006 int count = 0, bytesRemain = 0;
1007 unsigned long flags;
1008 LIST_HEAD(listHead);
1010 DPRINT_ENTER(NETVSC);
1012 netDevice = GetInboundNetDevice(Device);
1013 if (!netDevice) {
1014 DPRINT_ERR(NETVSC, "unable to get net device..."
1015 "device being destroyed?");
1016 DPRINT_EXIT(NETVSC);
1017 return;
1021 * All inbound packets other than send completion should be xfer page
1022 * packet
1024 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
1025 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
1026 Packet->Type);
1027 PutNetDevice(Device);
1028 return;
1031 nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
1032 (Packet->DataOffset8 << 3));
1034 /* Make sure this is a valid nvsp packet */
1035 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket) {
1036 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1037 nvspPacket->Header.MessageType);
1038 PutNetDevice(Device);
1039 return;
1042 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1043 nvspPacket->Header.MessageType);
1045 vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
1047 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1048 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1049 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1050 vmxferpagePacket->TransferPageSetId);
1051 PutNetDevice(Device);
1052 return;
1055 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1056 vmxferpagePacket->RangeCount);
1059 * Grab free packets (range count + 1) to represent this xfer
1060 * page packet. +1 to represent the xfer page packet itself.
1061 * We grab it here so that we know exactly how many we can
1062 * fulfil
1064 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1065 while (!list_empty(&netDevice->ReceivePacketList)) {
1066 list_move_tail(netDevice->ReceivePacketList.next, &listHead);
1067 if (++count == vmxferpagePacket->RangeCount + 1)
1068 break;
1070 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1073 * We need at least 2 netvsc pkts (1 to represent the xfer
1074 * page and at least 1 for the range) i.e. we can handled
1075 * some of the xfer page packet ranges...
1077 if (count < 2) {
1078 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1079 "Dropping this xfer page packet completely!",
1080 count, vmxferpagePacket->RangeCount + 1);
1082 /* Return it to the freelist */
1083 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1084 for (i = count; i != 0; i--) {
1085 list_move_tail(listHead.next,
1086 &netDevice->ReceivePacketList);
1088 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1089 flags);
1091 NetVscSendReceiveCompletion(Device,
1092 vmxferpagePacket->d.TransactionId);
1094 PutNetDevice(Device);
1095 return;
1098 /* Remove the 1st packet to represent the xfer page packet itself */
1099 xferpagePacket = (struct xferpage_packet *)listHead.next;
1100 list_del(&xferpagePacket->ListEntry);
1102 /* This is how much we can satisfy */
1103 xferpagePacket->Count = count - 1;
1104 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1105 /* vmxferpagePacket->RangeCount); */
1107 if (xferpagePacket->Count != vmxferpagePacket->RangeCount) {
1108 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1109 "page...got %d", vmxferpagePacket->RangeCount,
1110 xferpagePacket->Count);
1113 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1114 for (i = 0; i < (count - 1); i++) {
1115 netvscPacket = (struct hv_netvsc_packet *)listHead.next;
1116 list_del(&netvscPacket->ListEntry);
1118 /* Initialize the netvsc packet */
1119 netvscPacket->XferPagePacket = xferpagePacket;
1120 netvscPacket->Completion.Recv.OnReceiveCompletion =
1121 NetVscOnReceiveCompletion;
1122 netvscPacket->Completion.Recv.ReceiveCompletionContext =
1123 netvscPacket;
1124 netvscPacket->Device = Device;
1125 /* Save this so that we can send it back */
1126 netvscPacket->Completion.Recv.ReceiveCompletionTid =
1127 vmxferpagePacket->d.TransactionId;
1129 netvscPacket->TotalDataBufferLength =
1130 vmxferpagePacket->Ranges[i].ByteCount;
1131 netvscPacket->PageBufferCount = 1;
1133 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1134 /* vmxferpagePacket->Ranges[i].ByteCount < */
1135 /* netDevice->ReceiveBufferSize); */
1137 netvscPacket->PageBuffers[0].Length =
1138 vmxferpagePacket->Ranges[i].ByteCount;
1140 start = virt_to_phys((void *)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1142 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1143 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1144 + vmxferpagePacket->Ranges[i].ByteOffset
1145 + vmxferpagePacket->Ranges[i].ByteCount - 1;
1146 end = virt_to_phys((void *)endVirtual);
1148 /* Calculate the page relative offset */
1149 netvscPacket->PageBuffers[0].Offset =
1150 vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE - 1);
1151 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1152 /* Handle frame across multiple pages: */
1153 netvscPacket->PageBuffers[0].Length =
1154 (netvscPacket->PageBuffers[0].Pfn << PAGE_SHIFT)
1155 + PAGE_SIZE - start;
1156 bytesRemain = netvscPacket->TotalDataBufferLength -
1157 netvscPacket->PageBuffers[0].Length;
1158 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1159 netvscPacket->PageBuffers[j].Offset = 0;
1160 if (bytesRemain <= PAGE_SIZE) {
1161 netvscPacket->PageBuffers[j].Length = bytesRemain;
1162 bytesRemain = 0;
1163 } else {
1164 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1165 bytesRemain -= PAGE_SIZE;
1167 netvscPacket->PageBuffers[j].Pfn =
1168 virt_to_phys((void *)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1169 netvscPacket->PageBufferCount++;
1170 if (bytesRemain == 0)
1171 break;
1173 /* ASSERT(bytesRemain == 0); */
1175 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1176 "(pfn %llx, offset %u, len %u)", i,
1177 vmxferpagePacket->Ranges[i].ByteOffset,
1178 vmxferpagePacket->Ranges[i].ByteCount,
1179 netvscPacket->PageBuffers[0].Pfn,
1180 netvscPacket->PageBuffers[0].Offset,
1181 netvscPacket->PageBuffers[0].Length);
1183 /* Pass it to the upper layer */
1184 ((struct netvsc_driver *)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1186 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1189 /* ASSERT(list_empty(&listHead)); */
1191 PutNetDevice(Device);
1192 DPRINT_EXIT(NETVSC);
1195 static void NetVscSendReceiveCompletion(struct hv_device *Device,
1196 u64 TransactionId)
1198 struct nvsp_message recvcompMessage;
1199 int retries = 0;
1200 int ret;
1202 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1203 TransactionId);
1205 recvcompMessage.Header.MessageType =
1206 NvspMessage1TypeSendRNDISPacketComplete;
1208 /* FIXME: Pass in the status */
1209 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1211 retry_send_cmplt:
1212 /* Send the completion */
1213 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1214 &recvcompMessage,
1215 sizeof(struct nvsp_message),
1216 TransactionId,
1217 VmbusPacketTypeCompletion, 0);
1218 if (ret == 0) {
1219 /* success */
1220 /* no-op */
1221 } else if (ret == -1) {
1222 /* no more room...wait a bit and attempt to retry 3 times */
1223 retries++;
1224 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1225 "(tid %llx)...retrying %d", TransactionId, retries);
1227 if (retries < 4) {
1228 udelay(100);
1229 goto retry_send_cmplt;
1230 } else {
1231 DPRINT_ERR(NETVSC, "unable to send receive completion "
1232 "pkt (tid %llx)...give up retrying",
1233 TransactionId);
1235 } else {
1236 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1237 "%llx", TransactionId);
1241 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1242 static void NetVscOnReceiveCompletion(void *Context)
1244 struct hv_netvsc_packet *packet = Context;
1245 struct hv_device *device = (struct hv_device *)packet->Device;
1246 struct netvsc_device *netDevice;
1247 u64 transactionId = 0;
1248 bool fSendReceiveComp = false;
1249 unsigned long flags;
1251 DPRINT_ENTER(NETVSC);
1253 /* ASSERT(packet->XferPagePacket); */
1256 * Even though it seems logical to do a GetOutboundNetDevice() here to
1257 * send out receive completion, we are using GetInboundNetDevice()
1258 * since we may have disable outbound traffic already.
1260 netDevice = GetInboundNetDevice(device);
1261 if (!netDevice) {
1262 DPRINT_ERR(NETVSC, "unable to get net device..."
1263 "device being destroyed?");
1264 DPRINT_EXIT(NETVSC);
1265 return;
1268 /* Overloading use of the lock. */
1269 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1271 /* ASSERT(packet->XferPagePacket->Count > 0); */
1272 packet->XferPagePacket->Count--;
1275 * Last one in the line that represent 1 xfer page packet.
1276 * Return the xfer page packet itself to the freelist
1278 if (packet->XferPagePacket->Count == 0) {
1279 fSendReceiveComp = true;
1280 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1281 list_add_tail(&packet->XferPagePacket->ListEntry,
1282 &netDevice->ReceivePacketList);
1286 /* Put the packet back */
1287 list_add_tail(&packet->ListEntry, &netDevice->ReceivePacketList);
1288 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1290 /* Send a receive completion for the xfer page packet */
1291 if (fSendReceiveComp)
1292 NetVscSendReceiveCompletion(device, transactionId);
1294 PutNetDevice(device);
1295 DPRINT_EXIT(NETVSC);
1298 static void NetVscOnChannelCallback(void *Context)
1300 int ret;
1301 struct hv_device *device = Context;
1302 struct netvsc_device *netDevice;
1303 u32 bytesRecvd;
1304 u64 requestId;
1305 unsigned char *packet;
1306 struct vmpacket_descriptor *desc;
1307 unsigned char *buffer;
1308 int bufferlen = NETVSC_PACKET_SIZE;
1311 DPRINT_ENTER(NETVSC);
1313 /* ASSERT(device); */
1315 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1316 GFP_KERNEL);
1317 if (!packet)
1318 return;
1319 buffer = packet;
1321 netDevice = GetInboundNetDevice(device);
1322 if (!netDevice) {
1323 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1324 "ignoring inbound packets", netDevice);
1325 DPRINT_EXIT(NETVSC);
1326 goto out;
1329 do {
1330 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(
1331 device, buffer, bufferlen,
1332 &bytesRecvd, &requestId);
1333 if (ret == 0) {
1334 if (bytesRecvd > 0) {
1335 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1336 bytesRecvd, requestId);
1338 desc = (struct vmpacket_descriptor *)buffer;
1339 switch (desc->Type) {
1340 case VmbusPacketTypeCompletion:
1341 NetVscOnSendCompletion(device, desc);
1342 break;
1344 case VmbusPacketTypeDataUsingTransferPages:
1345 NetVscOnReceive(device, desc);
1346 break;
1348 default:
1349 DPRINT_ERR(NETVSC,
1350 "unhandled packet type %d, "
1351 "tid %llx len %d\n",
1352 desc->Type, requestId,
1353 bytesRecvd);
1354 break;
1357 /* reset */
1358 if (bufferlen > NETVSC_PACKET_SIZE) {
1359 kfree(buffer);
1360 buffer = packet;
1361 bufferlen = NETVSC_PACKET_SIZE;
1363 } else {
1364 /* reset */
1365 if (bufferlen > NETVSC_PACKET_SIZE) {
1366 kfree(buffer);
1367 buffer = packet;
1368 bufferlen = NETVSC_PACKET_SIZE;
1371 break;
1373 } else if (ret == -2) {
1374 /* Handle large packet */
1375 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
1376 if (buffer == NULL) {
1377 /* Try again next time around */
1378 DPRINT_ERR(NETVSC,
1379 "unable to allocate buffer of size "
1380 "(%d)!!", bytesRecvd);
1381 break;
1384 bufferlen = bytesRecvd;
1386 } while (1);
1388 PutNetDevice(device);
1389 DPRINT_EXIT(NETVSC);
1390 out:
1391 kfree(buffer);
1392 return;