- patches.arch/x86_mce_intel_decode_physical_address.patch:
[linux-flexiantxendom0-3.2.10.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_ENTER(NETVSC);
178
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));
185
186         /* Make sure we are at least 2 pages since 1 page is used for control */
187         /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
188
189         drv->name = gDriverName;
190         memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
191
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); */
196
197         /* Setup the dispatch table */
198         driver->Base.OnDeviceAdd        = NetVscOnDeviceAdd;
199         driver->Base.OnDeviceRemove     = NetVscOnDeviceRemove;
200         driver->Base.OnCleanup          = NetVscOnCleanup;
201
202         driver->OnSend                  = NetVscOnSend;
203
204         RndisFilterInit(driver);
205
206         DPRINT_EXIT(NETVSC);
207
208         return 0;
209 }
210
211 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
212 {
213         int ret = 0;
214         struct netvsc_device *netDevice;
215         struct nvsp_message *initPacket;
216
217         DPRINT_ENTER(NETVSC);
218
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;
225         }
226         /* ASSERT(netDevice->ReceiveBufferSize > 0); */
227         /* page-size grandularity */
228         /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
229
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;
238         }
239         /* page-aligned buffer */
240         /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
241         /*      0); */
242
243         DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
244
245         /*
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.
249          */
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;
258         }
259
260         /* osd_WaitEventWait(ext->ChannelInitEvent); */
261
262         /* Notify the NetVsp of the gpadl handle */
263         DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
264
265         initPacket = &netDevice->ChannelInitPacket;
266
267         memset(initPacket, 0, sizeof(struct nvsp_message));
268
269         initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
270         initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
271         initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
272
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;
284         }
285
286         osd_WaitEventWait(netDevice->ChannelInitEvent);
287
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;
295         }
296
297         /* Parse the response */
298         /* ASSERT(netDevice->ReceiveSectionCount == 0); */
299         /* ASSERT(netDevice->ReceiveSections == NULL); */
300
301         netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
302
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;
307         }
308
309         memcpy(netDevice->ReceiveSections,
310                 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
311                 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
312
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);
320
321         /*
322          * For 1st release, there should only be 1 section that represents the
323          * entire receive buffer
324          */
325         if (netDevice->ReceiveSectionCount != 1 ||
326             netDevice->ReceiveSections->Offset != 0) {
327                 ret = -1;
328                 goto Cleanup;
329         }
330
331         goto Exit;
332
333 Cleanup:
334         NetVscDestroyReceiveBuffer(netDevice);
335
336 Exit:
337         PutNetDevice(Device);
338         DPRINT_EXIT(NETVSC);
339         return ret;
340 }
341
342 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
343 {
344         int ret = 0;
345         struct netvsc_device *netDevice;
346         struct nvsp_message *initPacket;
347
348         DPRINT_ENTER(NETVSC);
349
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;
356         }
357         if (netDevice->SendBufferSize <= 0) {
358                 ret = -EINVAL;
359                 goto Cleanup;
360         }
361
362         /* page-size grandularity */
363         /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
364
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;
372         }
373         /* page-aligned buffer */
374         /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
375
376         DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
377
378         /*
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.
382          */
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;
390         }
391
392         /* osd_WaitEventWait(ext->ChannelInitEvent); */
393
394         /* Notify the NetVsp of the gpadl handle */
395         DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
396
397         initPacket = &netDevice->ChannelInitPacket;
398
399         memset(initPacket, 0, sizeof(struct nvsp_message));
400
401         initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
402         initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
403         initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
404
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;
415         }
416
417         osd_WaitEventWait(netDevice->ChannelInitEvent);
418
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;
426         }
427
428         netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
429
430         goto Exit;
431
432 Cleanup:
433         NetVscDestroySendBuffer(netDevice);
434
435 Exit:
436         PutNetDevice(Device);
437         DPRINT_EXIT(NETVSC);
438         return ret;
439 }
440
441 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
442 {
443         struct nvsp_message *revokePacket;
444         int ret = 0;
445
446         DPRINT_ENTER(NETVSC);
447
448         /*
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
453          */
454         if (NetDevice->ReceiveSectionCount) {
455                 DPRINT_INFO(NETVSC,
456                             "Sending NvspMessage1TypeRevokeReceiveBuffer...");
457
458                 /* Send the revoke receive buffer */
459                 revokePacket = &NetDevice->RevokePacket;
460                 memset(revokePacket, 0, sizeof(struct nvsp_message));
461
462                 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
463                 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
464
465                 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
466                                                 NetDevice->Device,
467                                                 revokePacket,
468                                                 sizeof(struct nvsp_message),
469                                                 (unsigned long)revokePacket,
470                                                 VmbusPacketTypeDataInBand, 0);
471                 /*
472                  * If we failed here, we might as well return and
473                  * have a leak rather than continue and a bugchk
474                  */
475                 if (ret != 0) {
476                         DPRINT_ERR(NETVSC, "unable to send revoke receive "
477                                    "buffer to netvsp");
478                         DPRINT_EXIT(NETVSC);
479                         return -1;
480                 }
481         }
482
483         /* Teardown the gpadl on the vsp end */
484         if (NetDevice->ReceiveBufferGpadlHandle) {
485                 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
486
487                 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
488                                         NetDevice->Device,
489                                         NetDevice->ReceiveBufferGpadlHandle);
490
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;
497                 }
498                 NetDevice->ReceiveBufferGpadlHandle = 0;
499         }
500
501         if (NetDevice->ReceiveBuffer) {
502                 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
503
504                 /* Free up the receive buffer */
505                 osd_PageFree(NetDevice->ReceiveBuffer,
506                              NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
507                 NetDevice->ReceiveBuffer = NULL;
508         }
509
510         if (NetDevice->ReceiveSections) {
511                 NetDevice->ReceiveSectionCount = 0;
512                 kfree(NetDevice->ReceiveSections);
513                 NetDevice->ReceiveSections = NULL;
514         }
515
516         DPRINT_EXIT(NETVSC);
517
518         return ret;
519 }
520
521 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
522 {
523         struct nvsp_message *revokePacket;
524         int ret = 0;
525
526         DPRINT_ENTER(NETVSC);
527
528         /*
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
533          */
534         if (NetDevice->SendSectionSize) {
535                 DPRINT_INFO(NETVSC,
536                             "Sending NvspMessage1TypeRevokeSendBuffer...");
537
538                 /* Send the revoke send buffer */
539                 revokePacket = &NetDevice->RevokePacket;
540                 memset(revokePacket, 0, sizeof(struct nvsp_message));
541
542                 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
543                 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
544
545                 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
546                                         revokePacket,
547                                         sizeof(struct nvsp_message),
548                                         (unsigned long)revokePacket,
549                                         VmbusPacketTypeDataInBand, 0);
550                 /*
551                  * If we failed here, we might as well return and have a leak
552                  * rather than continue and a bugchk
553                  */
554                 if (ret != 0) {
555                         DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
556                                    "to netvsp");
557                         DPRINT_EXIT(NETVSC);
558                         return -1;
559                 }
560         }
561
562         /* Teardown the gpadl on the vsp end */
563         if (NetDevice->SendBufferGpadlHandle) {
564                 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
565
566                 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
567
568                 /*
569                  * If we failed here, we might as well return and have a leak
570                  * rather than continue and a bugchk
571                  */
572                 if (ret != 0) {
573                         DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
574                                    "gpadl");
575                         DPRINT_EXIT(NETVSC);
576                         return -1;
577                 }
578                 NetDevice->SendBufferGpadlHandle = 0;
579         }
580
581         if (NetDevice->SendBuffer) {
582                 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
583
584                 /* Free up the receive buffer */
585                 osd_PageFree(NetDevice->SendBuffer,
586                              NetDevice->SendBufferSize >> PAGE_SHIFT);
587                 NetDevice->SendBuffer = NULL;
588         }
589
590         DPRINT_EXIT(NETVSC);
591
592         return ret;
593 }
594
595
596 static int NetVscConnectToVsp(struct hv_device *Device)
597 {
598         int ret;
599         struct netvsc_device *netDevice;
600         struct nvsp_message *initPacket;
601         int ndisVersion;
602
603         DPRINT_ENTER(NETVSC);
604
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;
611         }
612
613         initPacket = &netDevice->ChannelInitPacket;
614
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;
619
620         DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
621
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);
629
630         if (ret != 0) {
631                 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
632                 goto Cleanup;
633         }
634
635         osd_WaitEventWait(netDevice->ChannelInitEvent);
636
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);
642
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;
650         }
651
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;
658         }
659         DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
660
661         /* Send the ndis version */
662         memset(initPacket, 0, sizeof(struct nvsp_message));
663
664         ndisVersion = 0x00050000;
665
666         initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
667         initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
668                                 (ndisVersion & 0xFFFF0000) >> 16;
669         initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
670                                 ndisVersion & 0xFFFF;
671
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;
683         }
684         /*
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
689          */
690          /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
691
692         /* Post the big receive buffer to NetVSP */
693         ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
694         if (ret == 0)
695                 ret = NetVscInitializeSendBufferWithNetVsp(Device);
696
697 Cleanup:
698         PutNetDevice(Device);
699         DPRINT_EXIT(NETVSC);
700         return ret;
701 }
702
703 static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
704 {
705         DPRINT_ENTER(NETVSC);
706
707         NetVscDestroyReceiveBuffer(NetDevice);
708         NetVscDestroySendBuffer(NetDevice);
709
710         DPRINT_EXIT(NETVSC);
711 }
712
713 /*
714  * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
715  */
716 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
717 {
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;
724
725         DPRINT_ENTER(NETVSC);
726
727         netDevice = AllocNetDevice(Device);
728         if (!netDevice) {
729                 ret = -1;
730                 goto Cleanup;
731         }
732
733         DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
734
735         /* Initialize the NetVSC channel extension */
736         netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
737         spin_lock_init(&netDevice->receive_packet_list_lock);
738
739         netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
740
741         INIT_LIST_HEAD(&netDevice->ReceivePacketList);
742
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;
752                 }
753                 list_add_tail(&packet->ListEntry,
754                               &netDevice->ReceivePacketList);
755         }
756         netDevice->ChannelInitEvent = osd_WaitEventCreate();
757         if (!netDevice->ChannelInitEvent) {
758                 ret = -ENOMEM;
759                 goto Cleanup;
760         }
761
762         /* Open the channel */
763         ret = Device->Driver->VmbusChannelInterface.Open(Device,
764                                                 netDriver->RingBufferSize,
765                                                 netDriver->RingBufferSize,
766                                                 NULL, 0,
767                                                 NetVscOnChannelCallback,
768                                                 Device);
769
770         if (ret != 0) {
771                 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
772                 ret = -1;
773                 goto Cleanup;
774         }
775
776         /* Channel is opened */
777         DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
778
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;
785         }
786
787         DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
788                     ret);
789
790         DPRINT_EXIT(NETVSC);
791         return ret;
792
793 Close:
794         /* Now, we can close the channel safely */
795         Device->Driver->VmbusChannelInterface.Close(Device);
796
797 Cleanup:
798
799         if (netDevice) {
800                 kfree(netDevice->ChannelInitEvent);
801
802                 list_for_each_entry_safe(packet, pos,
803                                          &netDevice->ReceivePacketList,
804                                          ListEntry) {
805                         list_del(&packet->ListEntry);
806                         kfree(packet);
807                 }
808
809                 ReleaseOutboundNetDevice(Device);
810                 ReleaseInboundNetDevice(Device);
811
812                 FreeNetDevice(netDevice);
813         }
814
815         DPRINT_EXIT(NETVSC);
816         return ret;
817 }
818
819 /*
820  * NetVscOnDeviceRemove - Callback when the root bus device is removed
821  */
822 static int NetVscOnDeviceRemove(struct hv_device *Device)
823 {
824         struct netvsc_device *netDevice;
825         struct hv_netvsc_packet *netvscPacket, *pos;
826
827         DPRINT_ENTER(NETVSC);
828
829         DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
830                     Device->Extension);
831
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;
837         }
838
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);
844         }
845
846         DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
847
848         NetVscDisconnectFromVsp(netDevice);
849
850         DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
851                     Device->Extension);
852
853         /* Stop inbound traffic ie receives and sends completions */
854         netDevice = ReleaseInboundNetDevice(Device);
855
856         /* At this point, no one should be accessing netDevice except in here */
857         DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
858
859         /* Now, we can close the channel safely */
860         Device->Driver->VmbusChannelInterface.Close(Device);
861
862         /* Release all resources */
863         list_for_each_entry_safe(netvscPacket, pos,
864                                  &netDevice->ReceivePacketList, ListEntry) {
865                 list_del(&netvscPacket->ListEntry);
866                 kfree(netvscPacket);
867         }
868
869         kfree(netDevice->ChannelInitEvent);
870         FreeNetDevice(netDevice);
871
872         DPRINT_EXIT(NETVSC);
873         return 0;
874 }
875
876 /*
877  * NetVscOnCleanup - Perform any cleanup when the driver is removed
878  */
879 static void NetVscOnCleanup(struct hv_driver *drv)
880 {
881         DPRINT_ENTER(NETVSC);
882         DPRINT_EXIT(NETVSC);
883 }
884
885 static void NetVscOnSendCompletion(struct hv_device *Device,
886                                    struct vmpacket_descriptor *Packet)
887 {
888         struct netvsc_device *netDevice;
889         struct nvsp_message *nvspPacket;
890         struct hv_netvsc_packet *nvscPacket;
891
892         DPRINT_ENTER(NETVSC);
893
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;
900         }
901
902         nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
903
904         DPRINT_DBG(NETVSC, "send completion packet - type %d",
905                    nvspPacket->Header.MessageType);
906
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); */
921
922                 /* Notify the layer above us */
923                 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
924
925                 atomic_dec(&netDevice->NumOutstandingSends);
926         } else {
927                 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
928                            "%d received!!", nvspPacket->Header.MessageType);
929         }
930
931         PutNetDevice(Device);
932         DPRINT_EXIT(NETVSC);
933 }
934
935 static int NetVscOnSend(struct hv_device *Device,
936                         struct hv_netvsc_packet *Packet)
937 {
938         struct netvsc_device *netDevice;
939         int ret = 0;
940
941         struct nvsp_message sendMessage;
942
943         DPRINT_ENTER(NETVSC);
944
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;
951         }
952
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;
960         }
961
962         /* Not using send buffer section */
963         sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
964         sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
965
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);
980
981         }
982
983         if (ret != 0)
984                 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
985                            Packet, ret);
986
987         atomic_inc(&netDevice->NumOutstandingSends);
988         PutNetDevice(Device);
989
990         DPRINT_EXIT(NETVSC);
991         return ret;
992 }
993
994 static void NetVscOnReceive(struct hv_device *Device,
995                             struct vmpacket_descriptor *Packet)
996 {
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);
1009
1010         DPRINT_ENTER(NETVSC);
1011
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;
1018         }
1019
1020         /*
1021          * All inbound packets other than send completion should be xfer page
1022          * packet
1023          */
1024         if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
1025                 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
1026                            Packet->Type);
1027                 PutNetDevice(Device);
1028                 return;
1029         }
1030
1031         nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
1032                         (Packet->DataOffset8 << 3));
1033
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;
1040         }
1041
1042         DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1043                    nvspPacket->Header.MessageType);
1044
1045         vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
1046
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;
1053         }
1054
1055         DPRINT_DBG(NETVSC, "xfer page - range count %d",
1056                    vmxferpagePacket->RangeCount);
1057
1058         /*
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
1063          */
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;
1069         }
1070         spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1071
1072         /*
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...
1076          */
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);
1081
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);
1087                 }
1088                 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1089                                        flags);
1090
1091                 NetVscSendReceiveCompletion(Device,
1092                                             vmxferpagePacket->d.TransactionId);
1093
1094                 PutNetDevice(Device);
1095                 return;
1096         }
1097
1098         /* Remove the 1st packet to represent the xfer page packet itself */
1099         xferpagePacket = (struct xferpage_packet *)listHead.next;
1100         list_del(&xferpagePacket->ListEntry);
1101
1102         /* This is how much we can satisfy */
1103         xferpagePacket->Count = count - 1;
1104         /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1105         /*      vmxferpagePacket->RangeCount); */
1106
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);
1111         }
1112
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);
1117
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;
1128
1129                 netvscPacket->TotalDataBufferLength =
1130                                         vmxferpagePacket->Ranges[i].ByteCount;
1131                 netvscPacket->PageBufferCount = 1;
1132
1133                 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1134                 /*      vmxferpagePacket->Ranges[i].ByteCount < */
1135                 /*      netDevice->ReceiveBufferSize); */
1136
1137                 netvscPacket->PageBuffers[0].Length =
1138                                         vmxferpagePacket->Ranges[i].ByteCount;
1139
1140                 start = virt_to_phys((void *)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1141
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);
1147
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;
1166                                 }
1167                                 netvscPacket->PageBuffers[j].Pfn =
1168                                     virt_to_phys((void *)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1169                                 netvscPacket->PageBufferCount++;
1170                                 if (bytesRemain == 0)
1171                                         break;
1172                         }
1173                         /* ASSERT(bytesRemain == 0); */
1174                 }
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);
1182
1183                 /* Pass it to the upper layer */
1184                 ((struct netvsc_driver *)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1185
1186                 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1187         }
1188
1189         /* ASSERT(list_empty(&listHead)); */
1190
1191         PutNetDevice(Device);
1192         DPRINT_EXIT(NETVSC);
1193 }
1194
1195 static void NetVscSendReceiveCompletion(struct hv_device *Device,
1196                                         u64 TransactionId)
1197 {
1198         struct nvsp_message recvcompMessage;
1199         int retries = 0;
1200         int ret;
1201
1202         DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1203                    TransactionId);
1204
1205         recvcompMessage.Header.MessageType =
1206                                 NvspMessage1TypeSendRNDISPacketComplete;
1207
1208         /* FIXME: Pass in the status */
1209         recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1210
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);
1226
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);
1234                 }
1235         } else {
1236                 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1237                            "%llx", TransactionId);
1238         }
1239 }
1240
1241 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1242 static void NetVscOnReceiveCompletion(void *Context)
1243 {
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;
1250
1251         DPRINT_ENTER(NETVSC);
1252
1253         /* ASSERT(packet->XferPagePacket); */
1254
1255         /*
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.
1259          */
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;
1266         }
1267
1268         /* Overloading use of the lock. */
1269         spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1270
1271         /* ASSERT(packet->XferPagePacket->Count > 0); */
1272         packet->XferPagePacket->Count--;
1273
1274         /*
1275          * Last one in the line that represent 1 xfer page packet.
1276          * Return the xfer page packet itself to the freelist
1277          */
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);
1283
1284         }
1285
1286         /* Put the packet back */
1287         list_add_tail(&packet->ListEntry, &netDevice->ReceivePacketList);
1288         spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1289
1290         /* Send a receive completion for the xfer page packet */
1291         if (fSendReceiveComp)
1292                 NetVscSendReceiveCompletion(device, transactionId);
1293
1294         PutNetDevice(device);
1295         DPRINT_EXIT(NETVSC);
1296 }
1297
1298 static void NetVscOnChannelCallback(void *Context)
1299 {
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;
1309
1310
1311         DPRINT_ENTER(NETVSC);
1312
1313         /* ASSERT(device); */
1314
1315         packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1316                          GFP_KERNEL);
1317         if (!packet)
1318                 return;
1319         buffer = packet;
1320
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;
1327         }
1328
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);
1337
1338                                 desc = (struct vmpacket_descriptor *)buffer;
1339                                 switch (desc->Type) {
1340                                 case VmbusPacketTypeCompletion:
1341                                         NetVscOnSendCompletion(device, desc);
1342                                         break;
1343
1344                                 case VmbusPacketTypeDataUsingTransferPages:
1345                                         NetVscOnReceive(device, desc);
1346                                         break;
1347
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;
1355                                 }
1356
1357                                 /* reset */
1358                                 if (bufferlen > NETVSC_PACKET_SIZE) {
1359                                         kfree(buffer);
1360                                         buffer = packet;
1361                                         bufferlen = NETVSC_PACKET_SIZE;
1362                                 }
1363                         } else {
1364                                 /* reset */
1365                                 if (bufferlen > NETVSC_PACKET_SIZE) {
1366                                         kfree(buffer);
1367                                         buffer = packet;
1368                                         bufferlen = NETVSC_PACKET_SIZE;
1369                                 }
1370
1371                                 break;
1372                         }
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;
1382                         }
1383
1384                         bufferlen = bytesRecvd;
1385                 }
1386         } while (1);
1387
1388         PutNetDevice(device);
1389         DPRINT_EXIT(NETVSC);
1390 out:
1391         kfree(buffer);
1392         return;
1393 }