Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / drivers / staging / hv / netvsc.c
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
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.
7  *
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.
12  *
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.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
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"
30
31
32 /* Globals */
33 static const char *gDriverName = "netvsc";
34
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
40         }
41 };
42
43 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
44
45 static int NetVscOnDeviceRemove(struct hv_device *Device);
46
47 static void NetVscOnCleanup(struct hv_driver *Driver);
48
49 static void NetVscOnChannelCallback(void *context);
50
51 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device);
52
53 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device);
54
55 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice);
56
57 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice);
58
59 static int NetVscConnectToVsp(struct hv_device *Device);
60
61 static void NetVscOnSendCompletion(struct hv_device *Device,
62                                    struct vmpacket_descriptor *Packet);
63
64 static int NetVscOnSend(struct hv_device *Device,
65                         struct hv_netvsc_packet *Packet);
66
67 static void NetVscOnReceive(struct hv_device *Device,
68                             struct vmpacket_descriptor *Packet);
69
70 static void NetVscOnReceiveCompletion(void *Context);
71
72 static void NetVscSendReceiveCompletion(struct hv_device *Device,
73                                         u64 TransactionId);
74
75
76 static struct netvsc_device *AllocNetDevice(struct hv_device *Device)
77 {
78         struct netvsc_device *netDevice;
79
80         netDevice = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
81         if (!netDevice)
82                 return NULL;
83
84         /* Set to 2 to allow both inbound and outbound traffic */
85         atomic_cmpxchg(&netDevice->RefCount, 0, 2);
86
87         netDevice->Device = Device;
88         Device->Extension = netDevice;
89
90         return netDevice;
91 }
92
93 static void FreeNetDevice(struct netvsc_device *Device)
94 {
95         WARN_ON(atomic_read(&Device->RefCount) == 0);
96         Device->Device->Extension = NULL;
97         kfree(Device);
98 }
99
100
101 /* Get the net device object iff exists and its refcount > 1 */
102 static struct netvsc_device *GetOutboundNetDevice(struct hv_device *Device)
103 {
104         struct netvsc_device *netDevice;
105
106         netDevice = Device->Extension;
107         if (netDevice && atomic_read(&netDevice->RefCount) > 1)
108                 atomic_inc(&netDevice->RefCount);
109         else
110                 netDevice = NULL;
111
112         return netDevice;
113 }
114
115 /* Get the net device object iff exists and its refcount > 0 */
116 static struct netvsc_device *GetInboundNetDevice(struct hv_device *Device)
117 {
118         struct netvsc_device *netDevice;
119
120         netDevice = Device->Extension;
121         if (netDevice && atomic_read(&netDevice->RefCount))
122                 atomic_inc(&netDevice->RefCount);
123         else
124                 netDevice = NULL;
125
126         return netDevice;
127 }
128
129 static void PutNetDevice(struct hv_device *Device)
130 {
131         struct netvsc_device *netDevice;
132
133         netDevice = Device->Extension;
134         /* ASSERT(netDevice); */
135
136         atomic_dec(&netDevice->RefCount);
137 }
138
139 static struct netvsc_device *ReleaseOutboundNetDevice(struct hv_device *Device)
140 {
141         struct netvsc_device *netDevice;
142
143         netDevice = Device->Extension;
144         if (netDevice == NULL)
145                 return NULL;
146
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);
150
151         return netDevice;
152 }
153
154 static struct netvsc_device *ReleaseInboundNetDevice(struct hv_device *Device)
155 {
156         struct netvsc_device *netDevice;
157
158         netDevice = Device->Extension;
159         if (netDevice == NULL)
160                 return NULL;
161
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);
165
166         Device->Extension = NULL;
167         return netDevice;
168 }
169
170 /*
171  * NetVscInitialize - Main entry point
172  */
173 int NetVscInitialize(struct hv_driver *drv)
174 {
175         struct netvsc_driver *driver = (struct netvsc_driver *)drv;
176
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));
183
184         /* Make sure we are at least 2 pages since 1 page is used for control */
185         /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
186
187         drv->name = gDriverName;
188         memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
189
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); */
194
195         /* Setup the dispatch table */
196         driver->Base.OnDeviceAdd        = NetVscOnDeviceAdd;
197         driver->Base.OnDeviceRemove     = NetVscOnDeviceRemove;
198         driver->Base.OnCleanup          = NetVscOnCleanup;
199
200         driver->OnSend                  = NetVscOnSend;
201
202         RndisFilterInit(driver);
203         return 0;
204 }
205
206 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
207 {
208         int ret = 0;
209         struct netvsc_device *netDevice;
210         struct nvsp_message *initPacket;
211
212         netDevice = GetOutboundNetDevice(Device);
213         if (!netDevice) {
214                 DPRINT_ERR(NETVSC, "unable to get net device..."
215                            "device being destroyed?");
216                 return -1;
217         }
218         /* ASSERT(netDevice->ReceiveBufferSize > 0); */
219         /* page-size grandularity */
220         /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
221
222         netDevice->ReceiveBuffer =
223                 osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
224         if (!netDevice->ReceiveBuffer) {
225                 DPRINT_ERR(NETVSC,
226                            "unable to allocate receive buffer of size %d",
227                            netDevice->ReceiveBufferSize);
228                 ret = -1;
229                 goto Cleanup;
230         }
231         /* page-aligned buffer */
232         /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
233         /*      0); */
234
235         DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
236
237         /*
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.
241          */
242         ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
243                                         netDevice->ReceiveBuffer,
244                                         netDevice->ReceiveBufferSize,
245                                         &netDevice->ReceiveBufferGpadlHandle);
246         if (ret != 0) {
247                 DPRINT_ERR(NETVSC,
248                            "unable to establish receive buffer's gpadl");
249                 goto Cleanup;
250         }
251
252         /* osd_WaitEventWait(ext->ChannelInitEvent); */
253
254         /* Notify the NetVsp of the gpadl handle */
255         DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
256
257         initPacket = &netDevice->ChannelInitPacket;
258
259         memset(initPacket, 0, sizeof(struct nvsp_message));
260
261         initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
262         initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
263         initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
264
265         /* Send the gpadl notification request */
266         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
267                                 initPacket,
268                                 sizeof(struct nvsp_message),
269                                 (unsigned long)initPacket,
270                                 VmbusPacketTypeDataInBand,
271                                 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
272         if (ret != 0) {
273                 DPRINT_ERR(NETVSC,
274                            "unable to send receive buffer's gpadl to netvsp");
275                 goto Cleanup;
276         }
277
278         osd_WaitEventWait(netDevice->ChannelInitEvent);
279
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);
285                 ret = -1;
286                 goto Cleanup;
287         }
288
289         /* Parse the response */
290         /* ASSERT(netDevice->ReceiveSectionCount == 0); */
291         /* ASSERT(netDevice->ReceiveSections == NULL); */
292
293         netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
294
295         netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
296         if (netDevice->ReceiveSections == NULL) {
297                 ret = -1;
298                 goto Cleanup;
299         }
300
301         memcpy(netDevice->ReceiveSections,
302                 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
303                 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
304
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);
312
313         /*
314          * For 1st release, there should only be 1 section that represents the
315          * entire receive buffer
316          */
317         if (netDevice->ReceiveSectionCount != 1 ||
318             netDevice->ReceiveSections->Offset != 0) {
319                 ret = -1;
320                 goto Cleanup;
321         }
322
323         goto Exit;
324
325 Cleanup:
326         NetVscDestroyReceiveBuffer(netDevice);
327
328 Exit:
329         PutNetDevice(Device);
330         return ret;
331 }
332
333 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
334 {
335         int ret = 0;
336         struct netvsc_device *netDevice;
337         struct nvsp_message *initPacket;
338
339         netDevice = GetOutboundNetDevice(Device);
340         if (!netDevice) {
341                 DPRINT_ERR(NETVSC, "unable to get net device..."
342                            "device being destroyed?");
343                 return -1;
344         }
345         if (netDevice->SendBufferSize <= 0) {
346                 ret = -EINVAL;
347                 goto Cleanup;
348         }
349
350         /* page-size grandularity */
351         /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
352
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);
358                 ret = -1;
359                 goto Cleanup;
360         }
361         /* page-aligned buffer */
362         /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
363
364         DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
365
366         /*
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.
370          */
371         ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
372                                         netDevice->SendBuffer,
373                                         netDevice->SendBufferSize,
374                                         &netDevice->SendBufferGpadlHandle);
375         if (ret != 0) {
376                 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
377                 goto Cleanup;
378         }
379
380         /* osd_WaitEventWait(ext->ChannelInitEvent); */
381
382         /* Notify the NetVsp of the gpadl handle */
383         DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
384
385         initPacket = &netDevice->ChannelInitPacket;
386
387         memset(initPacket, 0, sizeof(struct nvsp_message));
388
389         initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
390         initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
391         initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
392
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);
399         if (ret != 0) {
400                 DPRINT_ERR(NETVSC,
401                            "unable to send receive buffer's gpadl to netvsp");
402                 goto Cleanup;
403         }
404
405         osd_WaitEventWait(netDevice->ChannelInitEvent);
406
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);
412                 ret = -1;
413                 goto Cleanup;
414         }
415
416         netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
417
418         goto Exit;
419
420 Cleanup:
421         NetVscDestroySendBuffer(netDevice);
422
423 Exit:
424         PutNetDevice(Device);
425         return ret;
426 }
427
428 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
429 {
430         struct nvsp_message *revokePacket;
431         int ret = 0;
432
433         /*
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
438          */
439         if (NetDevice->ReceiveSectionCount) {
440                 DPRINT_INFO(NETVSC,
441                             "Sending NvspMessage1TypeRevokeReceiveBuffer...");
442
443                 /* Send the revoke receive buffer */
444                 revokePacket = &NetDevice->RevokePacket;
445                 memset(revokePacket, 0, sizeof(struct nvsp_message));
446
447                 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
448                 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
449
450                 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
451                                                 NetDevice->Device,
452                                                 revokePacket,
453                                                 sizeof(struct nvsp_message),
454                                                 (unsigned long)revokePacket,
455                                                 VmbusPacketTypeDataInBand, 0);
456                 /*
457                  * If we failed here, we might as well return and
458                  * have a leak rather than continue and a bugchk
459                  */
460                 if (ret != 0) {
461                         DPRINT_ERR(NETVSC, "unable to send revoke receive "
462                                    "buffer to netvsp");
463                         return -1;
464                 }
465         }
466
467         /* Teardown the gpadl on the vsp end */
468         if (NetDevice->ReceiveBufferGpadlHandle) {
469                 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
470
471                 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
472                                         NetDevice->Device,
473                                         NetDevice->ReceiveBufferGpadlHandle);
474
475                 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
476                 if (ret != 0) {
477                         DPRINT_ERR(NETVSC,
478                                    "unable to teardown receive buffer's gpadl");
479                         return -1;
480                 }
481                 NetDevice->ReceiveBufferGpadlHandle = 0;
482         }
483
484         if (NetDevice->ReceiveBuffer) {
485                 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
486
487                 /* Free up the receive buffer */
488                 osd_PageFree(NetDevice->ReceiveBuffer,
489                              NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
490                 NetDevice->ReceiveBuffer = NULL;
491         }
492
493         if (NetDevice->ReceiveSections) {
494                 NetDevice->ReceiveSectionCount = 0;
495                 kfree(NetDevice->ReceiveSections);
496                 NetDevice->ReceiveSections = NULL;
497         }
498
499         return ret;
500 }
501
502 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
503 {
504         struct nvsp_message *revokePacket;
505         int ret = 0;
506
507         /*
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
512          */
513         if (NetDevice->SendSectionSize) {
514                 DPRINT_INFO(NETVSC,
515                             "Sending NvspMessage1TypeRevokeSendBuffer...");
516
517                 /* Send the revoke send buffer */
518                 revokePacket = &NetDevice->RevokePacket;
519                 memset(revokePacket, 0, sizeof(struct nvsp_message));
520
521                 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
522                 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
523
524                 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
525                                         revokePacket,
526                                         sizeof(struct nvsp_message),
527                                         (unsigned long)revokePacket,
528                                         VmbusPacketTypeDataInBand, 0);
529                 /*
530                  * If we failed here, we might as well return and have a leak
531                  * rather than continue and a bugchk
532                  */
533                 if (ret != 0) {
534                         DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
535                                    "to netvsp");
536                         return -1;
537                 }
538         }
539
540         /* Teardown the gpadl on the vsp end */
541         if (NetDevice->SendBufferGpadlHandle) {
542                 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
543
544                 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
545
546                 /*
547                  * If we failed here, we might as well return and have a leak
548                  * rather than continue and a bugchk
549                  */
550                 if (ret != 0) {
551                         DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
552                                    "gpadl");
553                         return -1;
554                 }
555                 NetDevice->SendBufferGpadlHandle = 0;
556         }
557
558         if (NetDevice->SendBuffer) {
559                 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
560
561                 /* Free up the receive buffer */
562                 osd_PageFree(NetDevice->SendBuffer,
563                              NetDevice->SendBufferSize >> PAGE_SHIFT);
564                 NetDevice->SendBuffer = NULL;
565         }
566
567         return ret;
568 }
569
570
571 static int NetVscConnectToVsp(struct hv_device *Device)
572 {
573         int ret;
574         struct netvsc_device *netDevice;
575         struct nvsp_message *initPacket;
576         int ndisVersion;
577
578         netDevice = GetOutboundNetDevice(Device);
579         if (!netDevice) {
580                 DPRINT_ERR(NETVSC, "unable to get net device..."
581                            "device being destroyed?");
582                 return -1;
583         }
584
585         initPacket = &netDevice->ChannelInitPacket;
586
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;
591
592         DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
593
594         /* Send the init request */
595         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
596                                 initPacket,
597                                 sizeof(struct nvsp_message),
598                                 (unsigned long)initPacket,
599                                 VmbusPacketTypeDataInBand,
600                                 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
601
602         if (ret != 0) {
603                 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
604                 goto Cleanup;
605         }
606
607         osd_WaitEventWait(netDevice->ChannelInitEvent);
608
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);
614
615         if (initPacket->Messages.InitMessages.InitComplete.Status !=
616             NvspStatusSuccess) {
617                 DPRINT_ERR(NETVSC,
618                         "unable to initialize with netvsp (status 0x%x)",
619                         initPacket->Messages.InitMessages.InitComplete.Status);
620                 ret = -1;
621                 goto Cleanup;
622         }
623
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);
628                 ret = -1;
629                 goto Cleanup;
630         }
631         DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
632
633         /* Send the ndis version */
634         memset(initPacket, 0, sizeof(struct nvsp_message));
635
636         ndisVersion = 0x00050000;
637
638         initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
639         initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
640                                 (ndisVersion & 0xFFFF0000) >> 16;
641         initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
642                                 ndisVersion & 0xFFFF;
643
644         /* Send the init request */
645         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
646                                         initPacket,
647                                         sizeof(struct nvsp_message),
648                                         (unsigned long)initPacket,
649                                         VmbusPacketTypeDataInBand, 0);
650         if (ret != 0) {
651                 DPRINT_ERR(NETVSC,
652                            "unable to send NvspMessage1TypeSendNdisVersion");
653                 ret = -1;
654                 goto Cleanup;
655         }
656         /*
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
661          */
662          /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
663
664         /* Post the big receive buffer to NetVSP */
665         ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
666         if (ret == 0)
667                 ret = NetVscInitializeSendBufferWithNetVsp(Device);
668
669 Cleanup:
670         PutNetDevice(Device);
671         return ret;
672 }
673
674 static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
675 {
676         NetVscDestroyReceiveBuffer(NetDevice);
677         NetVscDestroySendBuffer(NetDevice);
678 }
679
680 /*
681  * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
682  */
683 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
684 {
685         int ret = 0;
686         int i;
687         struct netvsc_device *netDevice;
688         struct hv_netvsc_packet *packet, *pos;
689         struct netvsc_driver *netDriver =
690                                 (struct netvsc_driver *)Device->Driver;
691
692         netDevice = AllocNetDevice(Device);
693         if (!netDevice) {
694                 ret = -1;
695                 goto Cleanup;
696         }
697
698         DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
699
700         /* Initialize the NetVSC channel extension */
701         netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
702         spin_lock_init(&netDevice->receive_packet_list_lock);
703
704         netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
705
706         INIT_LIST_HEAD(&netDevice->ReceivePacketList);
707
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);
712                 if (!packet) {
713                         DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
714                                    "for receive pool (wanted %d got %d)",
715                                    NETVSC_RECEIVE_PACKETLIST_COUNT, i);
716                         break;
717                 }
718                 list_add_tail(&packet->ListEntry,
719                               &netDevice->ReceivePacketList);
720         }
721         netDevice->ChannelInitEvent = osd_WaitEventCreate();
722         if (!netDevice->ChannelInitEvent) {
723                 ret = -ENOMEM;
724                 goto Cleanup;
725         }
726
727         /* Open the channel */
728         ret = Device->Driver->VmbusChannelInterface.Open(Device,
729                                                 netDriver->RingBufferSize,
730                                                 netDriver->RingBufferSize,
731                                                 NULL, 0,
732                                                 NetVscOnChannelCallback,
733                                                 Device);
734
735         if (ret != 0) {
736                 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
737                 ret = -1;
738                 goto Cleanup;
739         }
740
741         /* Channel is opened */
742         DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
743
744         /* Connect with the NetVsp */
745         ret = NetVscConnectToVsp(Device);
746         if (ret != 0) {
747                 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
748                 ret = -1;
749                 goto Close;
750         }
751
752         DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
753                     ret);
754
755         return ret;
756
757 Close:
758         /* Now, we can close the channel safely */
759         Device->Driver->VmbusChannelInterface.Close(Device);
760
761 Cleanup:
762
763         if (netDevice) {
764                 kfree(netDevice->ChannelInitEvent);
765
766                 list_for_each_entry_safe(packet, pos,
767                                          &netDevice->ReceivePacketList,
768                                          ListEntry) {
769                         list_del(&packet->ListEntry);
770                         kfree(packet);
771                 }
772
773                 ReleaseOutboundNetDevice(Device);
774                 ReleaseInboundNetDevice(Device);
775
776                 FreeNetDevice(netDevice);
777         }
778
779         return ret;
780 }
781
782 /*
783  * NetVscOnDeviceRemove - Callback when the root bus device is removed
784  */
785 static int NetVscOnDeviceRemove(struct hv_device *Device)
786 {
787         struct netvsc_device *netDevice;
788         struct hv_netvsc_packet *netvscPacket, *pos;
789
790         DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
791                     Device->Extension);
792
793         /* Stop outbound traffic ie sends and receives completions */
794         netDevice = ReleaseOutboundNetDevice(Device);
795         if (!netDevice) {
796                 DPRINT_ERR(NETVSC, "No net device present!!");
797                 return -1;
798         }
799
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));
804                 udelay(100);
805         }
806
807         DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
808
809         NetVscDisconnectFromVsp(netDevice);
810
811         DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
812                     Device->Extension);
813
814         /* Stop inbound traffic ie receives and sends completions */
815         netDevice = ReleaseInboundNetDevice(Device);
816
817         /* At this point, no one should be accessing netDevice except in here */
818         DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
819
820         /* Now, we can close the channel safely */
821         Device->Driver->VmbusChannelInterface.Close(Device);
822
823         /* Release all resources */
824         list_for_each_entry_safe(netvscPacket, pos,
825                                  &netDevice->ReceivePacketList, ListEntry) {
826                 list_del(&netvscPacket->ListEntry);
827                 kfree(netvscPacket);
828         }
829
830         kfree(netDevice->ChannelInitEvent);
831         FreeNetDevice(netDevice);
832         return 0;
833 }
834
835 /*
836  * NetVscOnCleanup - Perform any cleanup when the driver is removed
837  */
838 static void NetVscOnCleanup(struct hv_driver *drv)
839 {
840 }
841
842 static void NetVscOnSendCompletion(struct hv_device *Device,
843                                    struct vmpacket_descriptor *Packet)
844 {
845         struct netvsc_device *netDevice;
846         struct nvsp_message *nvspPacket;
847         struct hv_netvsc_packet *nvscPacket;
848
849         netDevice = GetInboundNetDevice(Device);
850         if (!netDevice) {
851                 DPRINT_ERR(NETVSC, "unable to get net device..."
852                            "device being destroyed?");
853                 return;
854         }
855
856         nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
857
858         DPRINT_DBG(NETVSC, "send completion packet - type %d",
859                    nvspPacket->Header.MessageType);
860
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); */
875
876                 /* Notify the layer above us */
877                 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
878
879                 atomic_dec(&netDevice->NumOutstandingSends);
880         } else {
881                 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
882                            "%d received!!", nvspPacket->Header.MessageType);
883         }
884
885         PutNetDevice(Device);
886 }
887
888 static int NetVscOnSend(struct hv_device *Device,
889                         struct hv_netvsc_packet *Packet)
890 {
891         struct netvsc_device *netDevice;
892         int ret = 0;
893
894         struct nvsp_message sendMessage;
895
896         netDevice = GetOutboundNetDevice(Device);
897         if (!netDevice) {
898                 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
899                            "ignoring outbound packets", netDevice);
900                 return -2;
901         }
902
903         sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
904         if (Packet->IsDataPacket) {
905                 /* 0 is RMC_DATA; */
906                 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
907         } else {
908                 /* 1 is RMC_CONTROL; */
909                 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
910         }
911
912         /* Not using send buffer section */
913         sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
914         sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
915
916         if (Packet->PageBufferCount) {
917                 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(
918                                         Device, Packet->PageBuffers,
919                                         Packet->PageBufferCount,
920                                         &sendMessage,
921                                         sizeof(struct nvsp_message),
922                                         (unsigned long)Packet);
923         } else {
924                 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
925                                 &sendMessage,
926                                 sizeof(struct nvsp_message),
927                                 (unsigned long)Packet,
928                                 VmbusPacketTypeDataInBand,
929                                 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
930
931         }
932
933         if (ret != 0)
934                 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
935                            Packet, ret);
936
937         atomic_inc(&netDevice->NumOutstandingSends);
938         PutNetDevice(Device);
939         return ret;
940 }
941
942 static void NetVscOnReceive(struct hv_device *Device,
943                             struct vmpacket_descriptor *Packet)
944 {
945         struct netvsc_device *netDevice;
946         struct vmtransfer_page_packet_header *vmxferpagePacket;
947         struct nvsp_message *nvspPacket;
948         struct hv_netvsc_packet *netvscPacket = NULL;
949         unsigned long start;
950         unsigned long end, endVirtual;
951         /* struct netvsc_driver *netvscDriver; */
952         struct xferpage_packet *xferpagePacket = NULL;
953         int i, j;
954         int count = 0, bytesRemain = 0;
955         unsigned long flags;
956         LIST_HEAD(listHead);
957
958         netDevice = GetInboundNetDevice(Device);
959         if (!netDevice) {
960                 DPRINT_ERR(NETVSC, "unable to get net device..."
961                            "device being destroyed?");
962                 return;
963         }
964
965         /*
966          * All inbound packets other than send completion should be xfer page
967          * packet
968          */
969         if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
970                 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
971                            Packet->Type);
972                 PutNetDevice(Device);
973                 return;
974         }
975
976         nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
977                         (Packet->DataOffset8 << 3));
978
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);
984                 return;
985         }
986
987         DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
988                    nvspPacket->Header.MessageType);
989
990         vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
991
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);
997                 return;
998         }
999
1000         DPRINT_DBG(NETVSC, "xfer page - range count %d",
1001                    vmxferpagePacket->RangeCount);
1002
1003         /*
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
1007          * fulfil
1008          */
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)
1013                         break;
1014         }
1015         spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1016
1017         /*
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...
1021          */
1022         if (count < 2) {
1023                 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1024                            "Dropping this xfer page packet completely!",
1025                            count, vmxferpagePacket->RangeCount + 1);
1026
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);
1032                 }
1033                 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1034                                        flags);
1035
1036                 NetVscSendReceiveCompletion(Device,
1037                                             vmxferpagePacket->d.TransactionId);
1038
1039                 PutNetDevice(Device);
1040                 return;
1041         }
1042
1043         /* Remove the 1st packet to represent the xfer page packet itself */
1044         xferpagePacket = (struct xferpage_packet *)listHead.next;
1045         list_del(&xferpagePacket->ListEntry);
1046
1047         /* This is how much we can satisfy */
1048         xferpagePacket->Count = count - 1;
1049         /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1050         /*      vmxferpagePacket->RangeCount); */
1051
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);
1056         }
1057
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);
1062
1063                 /* Initialize the netvsc packet */
1064                 netvscPacket->XferPagePacket = xferpagePacket;
1065                 netvscPacket->Completion.Recv.OnReceiveCompletion =
1066                                         NetVscOnReceiveCompletion;
1067                 netvscPacket->Completion.Recv.ReceiveCompletionContext =
1068                                         netvscPacket;
1069                 netvscPacket->Device = Device;
1070                 /* Save this so that we can send it back */
1071                 netvscPacket->Completion.Recv.ReceiveCompletionTid =
1072                                         vmxferpagePacket->d.TransactionId;
1073
1074                 netvscPacket->TotalDataBufferLength =
1075                                         vmxferpagePacket->Ranges[i].ByteCount;
1076                 netvscPacket->PageBufferCount = 1;
1077
1078                 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1079                 /*      vmxferpagePacket->Ranges[i].ByteCount < */
1080                 /*      netDevice->ReceiveBufferSize); */
1081
1082                 netvscPacket->PageBuffers[0].Length =
1083                                         vmxferpagePacket->Ranges[i].ByteCount;
1084
1085                 start = virt_to_phys((void *)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1086
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);
1092
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;
1107                                         bytesRemain = 0;
1108                                 } else {
1109                                         netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1110                                         bytesRemain -= PAGE_SIZE;
1111                                 }
1112                                 netvscPacket->PageBuffers[j].Pfn =
1113                                     virt_to_phys((void *)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1114                                 netvscPacket->PageBufferCount++;
1115                                 if (bytesRemain == 0)
1116                                         break;
1117                         }
1118                         /* ASSERT(bytesRemain == 0); */
1119                 }
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);
1127
1128                 /* Pass it to the upper layer */
1129                 ((struct netvsc_driver *)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1130
1131                 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1132         }
1133
1134         /* ASSERT(list_empty(&listHead)); */
1135
1136         PutNetDevice(Device);
1137 }
1138
1139 static void NetVscSendReceiveCompletion(struct hv_device *Device,
1140                                         u64 TransactionId)
1141 {
1142         struct nvsp_message recvcompMessage;
1143         int retries = 0;
1144         int ret;
1145
1146         DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1147                    TransactionId);
1148
1149         recvcompMessage.Header.MessageType =
1150                                 NvspMessage1TypeSendRNDISPacketComplete;
1151
1152         /* FIXME: Pass in the status */
1153         recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1154
1155 retry_send_cmplt:
1156         /* Send the completion */
1157         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1158                                         &recvcompMessage,
1159                                         sizeof(struct nvsp_message),
1160                                         TransactionId,
1161                                         VmbusPacketTypeCompletion, 0);
1162         if (ret == 0) {
1163                 /* success */
1164                 /* no-op */
1165         } else if (ret == -1) {
1166                 /* no more room...wait a bit and attempt to retry 3 times */
1167                 retries++;
1168                 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1169                            "(tid %llx)...retrying %d", TransactionId, retries);
1170
1171                 if (retries < 4) {
1172                         udelay(100);
1173                         goto retry_send_cmplt;
1174                 } else {
1175                         DPRINT_ERR(NETVSC, "unable to send receive completion "
1176                                   "pkt (tid %llx)...give up retrying",
1177                                   TransactionId);
1178                 }
1179         } else {
1180                 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1181                            "%llx", TransactionId);
1182         }
1183 }
1184
1185 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1186 static void NetVscOnReceiveCompletion(void *Context)
1187 {
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;
1194
1195         /* ASSERT(packet->XferPagePacket); */
1196
1197         /*
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.
1201          */
1202         netDevice = GetInboundNetDevice(device);
1203         if (!netDevice) {
1204                 DPRINT_ERR(NETVSC, "unable to get net device..."
1205                            "device being destroyed?");
1206                 return;
1207         }
1208
1209         /* Overloading use of the lock. */
1210         spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1211
1212         /* ASSERT(packet->XferPagePacket->Count > 0); */
1213         packet->XferPagePacket->Count--;
1214
1215         /*
1216          * Last one in the line that represent 1 xfer page packet.
1217          * Return the xfer page packet itself to the freelist
1218          */
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);
1224
1225         }
1226
1227         /* Put the packet back */
1228         list_add_tail(&packet->ListEntry, &netDevice->ReceivePacketList);
1229         spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1230
1231         /* Send a receive completion for the xfer page packet */
1232         if (fSendReceiveComp)
1233                 NetVscSendReceiveCompletion(device, transactionId);
1234
1235         PutNetDevice(device);
1236 }
1237
1238 static void NetVscOnChannelCallback(void *Context)
1239 {
1240         int ret;
1241         struct hv_device *device = Context;
1242         struct netvsc_device *netDevice;
1243         u32 bytesRecvd;
1244         u64 requestId;
1245         unsigned char *packet;
1246         struct vmpacket_descriptor *desc;
1247         unsigned char *buffer;
1248         int bufferlen = NETVSC_PACKET_SIZE;
1249
1250         /* ASSERT(device); */
1251
1252         packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1253                          GFP_KERNEL);
1254         if (!packet)
1255                 return;
1256         buffer = packet;
1257
1258         netDevice = GetInboundNetDevice(device);
1259         if (!netDevice) {
1260                 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1261                            "ignoring inbound packets", netDevice);
1262                 goto out;
1263         }
1264
1265         do {
1266                 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(
1267                                                 device, buffer, bufferlen,
1268                                                 &bytesRecvd, &requestId);
1269                 if (ret == 0) {
1270                         if (bytesRecvd > 0) {
1271                                 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1272                                            bytesRecvd, requestId);
1273
1274                                 desc = (struct vmpacket_descriptor *)buffer;
1275                                 switch (desc->Type) {
1276                                 case VmbusPacketTypeCompletion:
1277                                         NetVscOnSendCompletion(device, desc);
1278                                         break;
1279
1280                                 case VmbusPacketTypeDataUsingTransferPages:
1281                                         NetVscOnReceive(device, desc);
1282                                         break;
1283
1284                                 default:
1285                                         DPRINT_ERR(NETVSC,
1286                                                    "unhandled packet type %d, "
1287                                                    "tid %llx len %d\n",
1288                                                    desc->Type, requestId,
1289                                                    bytesRecvd);
1290                                         break;
1291                                 }
1292
1293                                 /* reset */
1294                                 if (bufferlen > NETVSC_PACKET_SIZE) {
1295                                         kfree(buffer);
1296                                         buffer = packet;
1297                                         bufferlen = NETVSC_PACKET_SIZE;
1298                                 }
1299                         } else {
1300                                 /* reset */
1301                                 if (bufferlen > NETVSC_PACKET_SIZE) {
1302                                         kfree(buffer);
1303                                         buffer = packet;
1304                                         bufferlen = NETVSC_PACKET_SIZE;
1305                                 }
1306
1307                                 break;
1308                         }
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 */
1314                                 DPRINT_ERR(NETVSC,
1315                                            "unable to allocate buffer of size "
1316                                            "(%d)!!", bytesRecvd);
1317                                 break;
1318                         }
1319
1320                         bufferlen = bytesRecvd;
1321                 }
1322         } while (1);
1323
1324         PutNetDevice(device);
1325 out:
1326         kfree(buffer);
1327         return;
1328 }